[pypy-svn] r10859 - in pypy/dist/pypy: annotation translator/test
pedronis at codespeak.net
pedronis at codespeak.net
Tue Apr 19 16:38:11 CEST 2005
Author: pedronis
Date: Tue Apr 19 16:38:11 2005
New Revision: 10859
Modified:
pypy/dist/pypy/annotation/unaryop.py
pypy/dist/pypy/translator/test/test_annrpython.py
Log:
empty dict/list -> len = 0 -> false; with tests
avoids two blocked blocks in translate_pypy.py targetpypy
Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py (original)
+++ pypy/dist/pypy/annotation/unaryop.py Tue Apr 19 16:38:11 2005
@@ -198,10 +198,28 @@
def method_pop(lst, s_index=None):
return lst.listdef.read_item()
+ def len(lst):
+ s_item = lst.listdef.read_item()
+ if isinstance(s_item, SomeImpossibleValue):
+ r = SomeInteger(nonneg=True)
+ r.const = 0
+ return r
+ return SomeObject.len(lst)
+
def iter(lst):
return SomeIterator(lst.listdef.read_item())
class __extend__(SomeDict):
+
+ def len(dct):
+ s_key = dct.dictdef.read_key()
+ s_value = dct.dictdef.read_value()
+ if isinstance(s_key, SomeImpossibleValue) and isinstance(s_value, SomeImpossibleValue):
+ r = SomeInteger(nonneg=True)
+ r.const = 0
+ return r
+ return SomeObject.len(dct)
+
def iter(dct):
return SomeIterator(dct.dictdef.read_key())
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 Tue Apr 19 16:38:11 2005
@@ -874,6 +874,47 @@
assert listitem(s.items[0]) == annmodel.SomeImpossibleValue()
assert listitem(s.items[1]).knowntype == int
assert listitem(s.items[2]).knowntype == int
+
+ def test_empty_list(self):
+ def f():
+ l = []
+ return bool(l)
+ def g():
+ l = []
+ x = bool(l)
+ l.append(1)
+ return x, bool(l)
+
+ a = RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert s.const == False
+
+ a = RPythonAnnotator()
+ s = a.build_types(g, [])
+
+ assert s.items[0].knowntype == bool and not s.items[0].is_constant()
+ assert s.items[1].knowntype == bool and not s.items[1].is_constant()
+
+ def test_empty_dict(self):
+ def f():
+ d = {}
+ return bool(d)
+ def g():
+ d = {}
+ x = bool(d)
+ d['a'] = 1
+ return x, bool(d)
+
+ a = RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert s.const == False
+
+ a = RPythonAnnotator()
+ s = a.build_types(g, [])
+
+ assert s.items[0].knowntype == bool and not s.items[0].is_constant()
+ assert s.items[1].knowntype == bool and not s.items[1].is_constant()
+
def g(n):
More information about the Pypy-commit
mailing list