[pypy-svn] r17462 - in pypy/dist/pypy: annotation translator/test
pedronis at codespeak.net
pedronis at codespeak.net
Sun Sep 11 17:37:59 CEST 2005
Author: pedronis
Date: Sun Sep 11 17:37:56 2005
New Revision: 17462
Modified:
pypy/dist/pypy/annotation/model.py
pypy/dist/pypy/annotation/unaryop.py
pypy/dist/pypy/translator/test/test_annrpython.py
Log:
implement generic if true then not None for non-numbers
Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py (original)
+++ pypy/dist/pypy/annotation/model.py Sun Sep 11 17:37:56 2005
@@ -171,20 +171,6 @@
unsigned = False
def __init__(self):
pass
- #def __eq__(self, other):
- # if self.__class__ is not other.__class__:
- # return False
- # if 'knowntypedata' in self.__dict__:
- # selfdic = self.__dict__.copy()
- # del selfdic['knowntypedata']
- # else:
- # selfdic = self.__dict__
- # if 'knowntypedata' in other.__dict__:
- # otherdic = other.__dict__.copy()
- # del otherdic['knowntypedata']
- # else:
- # otherdic = other.__dict__
- # return selfdic == otherdic
class SomeString(SomeObject):
"Stands for an object which is known to be a string."
Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py (original)
+++ pypy/dist/pypy/annotation/unaryop.py Sun Sep 11 17:37:56 2005
@@ -69,7 +69,7 @@
def len(obj):
return SomeInteger(nonneg=True)
- def is_true(obj):
+ def is_true_behavior(obj):
if obj.is_constant():
return immutablevalue(bool(obj.const))
else:
@@ -79,6 +79,24 @@
else:
return SomeBool()
+ def is_true(s_obj):
+ r = s_obj.is_true_behavior()
+ assert isinstance(r, SomeBool)
+
+ bk = getbookkeeper()
+ knowntypedata = r.knowntypedata = {}
+ fn, block, i = bk.position_key
+ op = block.operations[i]
+ assert op.opname == "is_true" or op.opname == "nonzero"
+ assert len(op.args) == 1
+ arg = op.args[0]
+ s_nonnone_obj = s_obj
+ if s_obj.can_be_none():
+ s_nonnone_obj = s_obj.nonnoneify()
+ add_knowntypedata(knowntypedata, True, [arg], s_nonnone_obj)
+ return r
+
+
def nonzero(obj):
return obj.is_true()
@@ -162,6 +180,21 @@
def op_contains(obj, s_element):
return SomeBool()
+class __extend__(SomeFloat):
+
+ def pos(flt):
+ return flt
+
+ def neg(flt):
+ return SomeFloat()
+
+ abs = neg
+
+ def is_true(self):
+ if self.is_constant():
+ return getbookkeeper().immutablevalue(bool(self.const))
+ return SomeBool()
+
class __extend__(SomeInteger):
def invert(self):
@@ -195,23 +228,11 @@
abs.can_only_throw = []
abs_ovf = _clone(abs, [OverflowError])
-
class __extend__(SomeBool):
def is_true(self):
return self
-class __extend__(SomeFloat):
-
- def pos(flt):
- return flt
-
- def neg(flt):
- return SomeFloat()
-
- abs = neg
-
-
class __extend__(SomeTuple):
def len(tup):
@@ -381,24 +402,6 @@
def method_upper(str):
return SomeString()
- #def is_true(str):
- # r = SomeObject.is_true(str)
- # if not isinstance(r, SomeBool):
- # return r
- # bk = getbookkeeper()
- # knowntypedata = r.knowntypedata = {}
- # fn, block, i = bk.position_key
- #
- # annotator = bk.annotator
- # op = block.operations[i]
- # assert op.opname == "is_true" or op.opname == "nonzero"
- # assert len(op.args) == 1
- # arg = op.args[0]
- # add_knowntypedata(knowntypedata, False, [arg], str)
- # add_knowntypedata(knowntypedata, True, [arg], str.nonnoneify())
- # return r
-
-
class __extend__(SomeChar):
@@ -522,7 +525,7 @@
d[func] = value
return SomePBC(d)
- def is_true(pbc):
+ def is_true_behavior(pbc):
outcome = None
for c in pbc.prebuiltinstances:
if c is not None and not bool(c):
Modified: pypy/dist/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/translator/test/test_annrpython.py (original)
+++ pypy/dist/pypy/translator/test/test_annrpython.py Sun Sep 11 17:37:56 2005
@@ -1629,6 +1629,42 @@
s = a.build_types(f5, [])
assert listitem(s.items[1]).__class__ == annmodel.SomeString
+ def test_true_str_is_not_none(self):
+ def f(s):
+ if s:
+ return s
+ else:
+ return ''
+ def g(i):
+ if i:
+ return f(None)
+ else:
+ return f('')
+ a = self.RPythonAnnotator()
+ s = a.build_types(g, [int])
+ assert s.knowntype == str
+ assert not s.can_be_None
+
+ def test_true_func_is_not_none(self):
+ def a1():
+ pass
+ def a2():
+ pass
+ def f(a):
+ if a:
+ return a
+ else:
+ return a2
+ def g(i):
+ if i:
+ return f(None)
+ else:
+ return f(a1)
+ a = self.RPythonAnnotator()
+ s = a.build_types(g, [int])
+ assert None not in s.prebuiltinstances
+
+
def g(n):
return [0,1,2,n]
More information about the Pypy-commit
mailing list