[pypy-svn] r48311 - in pypy/dist/pypy/annotation: . test
fijal at codespeak.net
fijal at codespeak.net
Mon Nov 5 11:25:47 CET 2007
Author: fijal
Date: Mon Nov 5 11:25:47 2007
New Revision: 48311
Modified:
pypy/dist/pypy/annotation/binaryop.py
pypy/dist/pypy/annotation/bookkeeper.py
pypy/dist/pypy/annotation/builtin.py
pypy/dist/pypy/annotation/classdef.py
pypy/dist/pypy/annotation/model.py
pypy/dist/pypy/annotation/test/test_annrpython.py
Log:
Some support for annotating unicode strings in rpython.
Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py (original)
+++ pypy/dist/pypy/annotation/binaryop.py Mon Nov 5 11:25:47 2007
@@ -19,7 +19,7 @@
from pypy.annotation.model import add_knowntypedata, merge_knowntypedata
from pypy.annotation.model import lltype_to_annotation
from pypy.annotation.model import SomeGenericCallable
-from pypy.annotation.model import SomeExternalInstance
+from pypy.annotation.model import SomeExternalInstance, SomeUnicodeString
from pypy.annotation.bookkeeper import getbookkeeper
from pypy.objspace.flow.model import Variable, Constant
from pypy.annotation.listdef import ListDef
@@ -636,12 +636,39 @@
getbookkeeper().count("str_mul", str1, int2)
return SomeString()
+class __extend__(pairtype(SomeUnicodeString, SomeInteger)):
+ def getitem((str1, int2)):
+ getbookkeeper().count("str_getitem", int2)
+ return SomeUnicodeCodePoint()
+ getitem.can_only_throw = []
+
+ getitem_key = getitem
+
+ def getitem_idx((str1, int2)):
+ getbookkeeper().count("str_getitem", int2)
+ return SomeUnicodeCodePoint()
+ getitem_idx.can_only_throw = [IndexError]
+
+ getitem_idx_key = getitem_idx
+
+ # uncomment if we really want to support that
+ #def mul((str1, int2)): # xxx do we want to support this
+ # getbookkeeper().count("str_mul", str1, int2)
+ # return SomeString()
+
class __extend__(pairtype(SomeInteger, SomeString)):
def mul((int1, str2)): # xxx do we want to support this
getbookkeeper().count("str_mul", str2, int1)
return SomeString()
+class __extend__(pairtype(SomeString, SomeUnicodeString),
+ pairtype(SomeUnicodeString, SomeString),
+ pairtype(SomeUnicodeString, SomeUnicodeString)):
+ def union((str1, str2)):
+ return SomeUnicodeString(can_be_None=str1.can_be_None or
+ str2.can_be_None)
+
class __extend__(pairtype(SomeInteger, SomeList)):
def mul((int1, lst2)):
@@ -781,6 +808,7 @@
_make_none_union('SomeInstance', 'classdef=obj.classdef, can_be_None=True')
_make_none_union('SomeString', 'can_be_None=True')
+_make_none_union('SomeUnicodeString', 'can_be_None=True')
_make_none_union('SomeList', 'obj.listdef')
_make_none_union('SomeDict', 'obj.dictdef')
_make_none_union('SomeExternalObject', 'obj.knowntype')
Modified: pypy/dist/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/dist/pypy/annotation/bookkeeper.py (original)
+++ pypy/dist/pypy/annotation/bookkeeper.py Mon Nov 5 11:25:47 2007
@@ -11,7 +11,8 @@
SomeInteger, SomeExternalObject, SomeOOInstance, TLS, SomeAddress, \
SomeUnicodeCodePoint, SomeOOStaticMeth, s_None, s_ImpossibleValue, \
SomeLLADTMeth, SomeBool, SomeTuple, SomeOOClass, SomeImpossibleValue, \
- SomeList, SomeObject, HarmlesslyBlocked, SomeWeakRef, lltype_to_annotation
+ SomeUnicodeString, SomeList, SomeObject, HarmlesslyBlocked, \
+ SomeWeakRef, lltype_to_annotation
from pypy.annotation.classdef import ClassDef, InstanceSource
from pypy.annotation.listdef import ListDef, MOST_GENERAL_LISTDEF
from pypy.annotation.dictdef import DictDef, MOST_GENERAL_DICTDEF
@@ -322,8 +323,11 @@
result = SomeChar()
else:
result = SomeString()
- elif tp is unicode and len(x) == 1:
- result = SomeUnicodeCodePoint()
+ elif tp is unicode:
+ if len(x) == 1:
+ result = SomeUnicodeCodePoint()
+ else:
+ result = SomeUnicodeString()
elif tp is tuple:
result = SomeTuple(items = [self.immutablevalue(e, need_const) for e in x])
elif tp is float:
Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py (original)
+++ pypy/dist/pypy/annotation/builtin.py Mon Nov 5 11:25:47 2007
@@ -6,7 +6,7 @@
from pypy.annotation.model import SomeInteger, SomeObject, SomeChar, SomeBool
from pypy.annotation.model import SomeString, SomeTuple, SomeSlice, s_Bool
from pypy.annotation.model import SomeUnicodeCodePoint, SomeAddress
-from pypy.annotation.model import SomeFloat, unionof
+from pypy.annotation.model import SomeFloat, unionof, SomeUnicodeString
from pypy.annotation.model import SomePBC, SomeInstance, SomeDict
from pypy.annotation.model import SomeExternalObject
from pypy.annotation.model import SomeWeakRef
@@ -107,8 +107,8 @@
def builtin_unichr(s_int):
return constpropagate(unichr, [s_int], SomeUnicodeCodePoint())
-##def builtin_unicode(s_obj):
-## raise TypeError, "unicode() calls should not happen at interp-level"
+def builtin_unicode(s_unicode):
+ return constpropagate(unicode, [s_unicode], SomeUnicodeString())
def our_issubclass(cls1, cls2):
""" we're going to try to be less silly in the face of old-style classes"""
Modified: pypy/dist/pypy/annotation/classdef.py
==============================================================================
--- pypy/dist/pypy/annotation/classdef.py (original)
+++ pypy/dist/pypy/annotation/classdef.py Mon Nov 5 11:25:47 2007
@@ -144,7 +144,7 @@
raise NoSuchAttrError(classdef, self.name)
-class ClassDef:
+class ClassDef(object):
"Wraps a user class."
def __init__(self, bookkeeper, classdesc):
Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py (original)
+++ pypy/dist/pypy/annotation/model.py Mon Nov 5 11:25:47 2007
@@ -204,6 +204,19 @@
def nonnoneify(self):
return SomeString(can_be_None=False)
+class SomeUnicodeString(SomeObject):
+ "Stands for an object which is known to be an unicode string"
+ knowntype = unicode
+ immutable = True
+ def __init__(self, can_be_None=False):
+ self.can_be_None = can_be_None
+
+ def can_be_none(self):
+ return self.can_be_None
+
+ def nonnoneify(self):
+ return SomeUnicodeString(can_be_None=False)
+
class SomeChar(SomeString):
"Stands for an object known to be a string of length 1."
Modified: pypy/dist/pypy/annotation/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/test/test_annrpython.py (original)
+++ pypy/dist/pypy/annotation/test/test_annrpython.py Mon Nov 5 11:25:47 2007
@@ -2872,6 +2872,39 @@
s = a.build_types(g, [int])
assert isinstance(s, annmodel.SomeSingleFloat)
+ def test_unicode_simple(self):
+ def f():
+ return u'xxx'
+
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert isinstance(s, annmodel.SomeUnicodeString)
+
+ def test_unicode(self):
+ def g(n):
+ if n > 0:
+ return "xxx"
+ else:
+ return u"x\xe4x"
+
+ def f(n):
+ x = g(0)
+ return x[n]
+
+ a = self.RPythonAnnotator()
+ s = a.build_types(g, [int])
+ assert isinstance(s, annmodel.SomeUnicodeString)
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [int])
+ assert isinstance(s, annmodel.SomeUnicodeCodePoint)
+
+ def test_unicode_from_string(self):
+ def f(x):
+ return unicode(x)
+
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [str])
+ assert isinstance(s, annmodel.SomeUnicodeString)
def g(n):
return [0,1,2,n]
More information about the pypy-svn
mailing list