[pypy-svn] r17291 - in pypy/dist/pypy/rpython: . test
arigo at codespeak.net
arigo at codespeak.net
Tue Sep 6 15:34:35 CEST 2005
Author: arigo
Date: Tue Sep 6 15:34:35 2005
New Revision: 17291
Modified:
pypy/dist/pypy/rpython/rdict.py
pypy/dist/pypy/rpython/test/test_rdict.py
Log:
Support dict-or-None in the rtyper.
Modified: pypy/dist/pypy/rpython/rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/rdict.py (original)
+++ pypy/dist/pypy/rpython/rdict.py Tue Sep 6 15:34:35 2005
@@ -84,6 +84,8 @@
def convert_const(self, dictobj):
# get object from bound dict methods
#dictobj = getattr(dictobj, '__self__', dictobj)
+ if dictobj is None:
+ return nullptr(self.STRDICT)
if not isinstance(dictobj, dict):
raise TyperError("expected a dict: %r" % (dictobj,))
try:
@@ -105,6 +107,10 @@
v_dict, = hop.inputargs(self)
return hop.gendirectcall(ll_strdict_len, v_dict)
+ def rtype_is_true(self, hop):
+ v_dict, = hop.inputargs(self)
+ return hop.gendirectcall(ll_strdict_is_true, v_dict)
+
def make_iterator_repr(self):
return StrDictIteratorRepr(self)
@@ -193,6 +199,10 @@
def ll_strdict_len(d):
return d.num_items
+def ll_strdict_is_true(d):
+ # check if a dict is True, allowing for None
+ return bool(d) and d.num_items != 0
+
def ll_strdict_getitem(d, key):
entry = ll_strdict_lookup(d, key)
if entry.key and entry.key != deleted_entry_marker:
Modified: pypy/dist/pypy/rpython/test/test_rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rdict.py (original)
+++ pypy/dist/pypy/rpython/test/test_rdict.py Tue Sep 6 15:34:35 2005
@@ -254,3 +254,21 @@
res = interpret(func, [1])
assert res is False
+def dict_or_none():
+ class A:
+ pass
+ def negate(d):
+ return not d
+ def func(n):
+ a = A()
+ a.d = None
+ if n > 0:
+ a.d = {str(n): 1, "42": 2}
+ del a.d["42"]
+ return negate(a.d)
+ res = interpret(func, [10])
+ assert res is False
+ res = interpret(func, [0])
+ assert res is True
+ res = interpret(func, [42])
+ assert res is True
More information about the Pypy-commit
mailing list