[pypy-commit] pypy rpython-bytearray: support for bytearray annotation, step 1
fijal
noreply at buildbot.pypy.org
Sun Dec 23 19:44:09 CET 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: rpython-bytearray
Changeset: r59540:8fa0e731659a
Date: 2012-12-23 19:40 +0200
http://bitbucket.org/pypy/pypy/changeset/8fa0e731659a/
Log: support for bytearray annotation, step 1
diff --git a/pypy/annotation/binaryop.py b/pypy/annotation/binaryop.py
--- a/pypy/annotation/binaryop.py
+++ b/pypy/annotation/binaryop.py
@@ -10,7 +10,7 @@
from pypy.annotation.model import SomeUnicodeCodePoint, SomeUnicodeString
from pypy.annotation.model import SomeTuple, SomeImpossibleValue, s_ImpossibleValue
from pypy.annotation.model import SomeInstance, SomeBuiltin, SomeIterator
-from pypy.annotation.model import SomePBC, SomeFloat, s_None
+from pypy.annotation.model import SomePBC, SomeFloat, s_None, SomeByteArray
from pypy.annotation.model import SomeExternalObject, SomeWeakRef
from pypy.annotation.model import SomeAddress, SomeTypedAddressAccess
from pypy.annotation.model import SomeSingleFloat, SomeLongFloat, SomeType
@@ -415,6 +415,11 @@
result.const = str1.const + str2.const
return result
+class __extend__(pairtype(SomeByteArray, SomeByteArray)):
+ def union((b1, b2)):
+ can_be_None = b1.can_be_None or b2.can_be_None
+ return SomeByteArray(can_be_None=can_be_None)
+
class __extend__(pairtype(SomeChar, SomeChar)):
def union((chr1, chr2)):
diff --git a/pypy/annotation/bookkeeper.py b/pypy/annotation/bookkeeper.py
--- a/pypy/annotation/bookkeeper.py
+++ b/pypy/annotation/bookkeeper.py
@@ -13,7 +13,7 @@
SomeUnicodeCodePoint, SomeOOStaticMeth, s_None, s_ImpossibleValue, \
SomeLLADTMeth, SomeBool, SomeTuple, SomeOOClass, SomeImpossibleValue, \
SomeUnicodeString, SomeList, SomeObject, HarmlesslyBlocked, \
- SomeWeakRef, lltype_to_annotation, SomeType
+ SomeWeakRef, lltype_to_annotation, SomeType, SomeByteArray
from pypy.annotation.classdef import InstanceSource, ClassDef
from pypy.annotation.listdef import ListDef, ListItem
from pypy.annotation.dictdef import DictDef
@@ -349,6 +349,8 @@
result = SomeUnicodeCodePoint()
else:
result = SomeUnicodeString()
+ elif tp is bytearray:
+ result = SomeByteArray()
elif tp is tuple:
result = SomeTuple(items = [self.immutablevalue(e, need_const) for e in x])
elif tp is float:
diff --git a/pypy/annotation/builtin.py b/pypy/annotation/builtin.py
--- a/pypy/annotation/builtin.py
+++ b/pypy/annotation/builtin.py
@@ -9,7 +9,7 @@
from pypy.annotation.model import SomeFloat, unionof, SomeUnicodeString
from pypy.annotation.model import SomePBC, SomeInstance, SomeDict, SomeList
from pypy.annotation.model import SomeWeakRef, SomeIterator
-from pypy.annotation.model import SomeOOObject
+from pypy.annotation.model import SomeOOObject, SomeByteArray
from pypy.annotation.model import annotation_to_lltype, lltype_to_annotation, ll_to_annotation
from pypy.annotation.model import add_knowntypedata
from pypy.annotation.model import s_ImpossibleValue
@@ -119,6 +119,9 @@
def builtin_unicode(s_unicode):
return constpropagate(unicode, [s_unicode], SomeUnicodeString())
+def builtin_bytearray(s_str):
+ return constpropagate(bytearray, [s_str], SomeByteArray())
+
def our_issubclass(cls1, cls2):
""" we're going to try to be less silly in the face of old-style classes"""
from pypy.annotation.classdef import ClassDef
diff --git a/pypy/annotation/model.py b/pypy/annotation/model.py
--- a/pypy/annotation/model.py
+++ b/pypy/annotation/model.py
@@ -241,6 +241,9 @@
"Stands for an object which is known to be an unicode string"
knowntype = unicode
+class SomeByteArray(SomeStringOrUnicode):
+ knowntype = bytearray
+
class SomeChar(SomeString):
"Stands for an object known to be a string of length 1."
can_be_None = False
diff --git a/pypy/annotation/test/test_annrpython.py b/pypy/annotation/test/test_annrpython.py
--- a/pypy/annotation/test/test_annrpython.py
+++ b/pypy/annotation/test/test_annrpython.py
@@ -3819,6 +3819,13 @@
a = self.RPythonAnnotator()
a.build_types(f, []) # assert did not explode
+ def test_bytearray(self):
+ def f():
+ return bytearray("xyz")
+
+ a = self.RPythonAnnotator()
+ assert isinstance(a.build_types(f, []), annmodel.SomeByteArray)
+
def g(n):
return [0,1,2,n]
More information about the pypy-commit
mailing list