Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch:
Changeset: r94531:85d7ccafed44
Date: 2018-05-12 16:47 +0200
http://bitbucket.org/pypy/pypy/changeset/85d7ccafed44/
Log: rpython support for dict.get(key) (ie without giving a default)
diff --git a/rpython/annotator/test/test_annrpython.py
b/rpython/annotator/test/test_annrpython.py
--- a/rpython/annotator/test/test_annrpython.py
+++ b/rpython/annotator/test/test_annrpython.py
@@ -688,6 +688,16 @@
assert isinstance(dictvalue(s), annmodel.SomeInteger)
assert not dictvalue(s).nonneg
+ def test_dict_get(self):
+ def f1(i, j):
+ d = {i: ''}
+ return d.get(j)
+ a = self.RPythonAnnotator()
+ s = a.build_types(f1, [int, int])
+ assert isinstance(s, annmodel.SomeString)
+ assert s.can_be_None
+
+
def test_exception_deduction(self):
a = self.RPythonAnnotator()
s = a.build_types(snippet.exception_deduction, [])
diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -496,7 +496,7 @@
return SomeTuple((s_key, s_Int))
raise ValueError(variant)
- def method_get(self, key, dfl):
+ def method_get(self, key, dfl=s_None):
position = getbookkeeper().position_key
self.dictdef.generalize_key(key)
self.dictdef.generalize_value(dfl)
diff --git a/rpython/rtyper/lltypesystem/rordereddict.py
b/rpython/rtyper/lltypesystem/rordereddict.py
--- a/rpython/rtyper/lltypesystem/rordereddict.py
+++ b/rpython/rtyper/lltypesystem/rordereddict.py
@@ -283,8 +283,12 @@
return DictIteratorRepr(self, *variant)
def rtype_method_get(self, hop):
- v_dict, v_key, v_default = hop.inputargs(self, self.key_repr,
- self.value_repr)
+ if hop.nb_args == 3:
+ v_dict, v_key, v_default = hop.inputargs(self, self.key_repr,
+ self.value_repr)
+ else:
+ v_dict, v_key = hop.inputargs(self, self.key_repr)
+ v_default = hop.inputconst(self.value_repr, None)
hop.exception_cannot_occur()
v_res = hop.gendirectcall(ll_dict_get, v_dict, v_key, v_default)
return self.recast_value(hop.llops, v_res)
diff --git a/rpython/rtyper/test/test_rdict.py
b/rpython/rtyper/test/test_rdict.py
--- a/rpython/rtyper/test/test_rdict.py
+++ b/rpython/rtyper/test/test_rdict.py
@@ -208,6 +208,16 @@
res = self.interpret(func, ())
assert res == 421
+ def test_dict_get_no_second_arg(self):
+ def func():
+ dic = self.newdict()
+ x1 = dic.get('hi', 'a')
+ x2 = dic.get('blah')
+ return (x1 == 'a') * 10 + (x2 is None)
+ return x1 * 10 + x2
+ res = self.interpret(func, ())
+ assert res == 11
+
def test_dict_get_empty(self):
def func():
# this time without writing to the dict
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit