[pypy-commit] pypy default: Fixes #2508 -- correctly handle dict.pop where the popping key is not the same type as the dict's and pop is called with a default

alex_gaynor pypy.commits at gmail.com
Tue Mar 21 14:02:51 EDT 2017


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r90778:a1b0ce5e4915
Date: 2017-03-21 14:02 -0400
http://bitbucket.org/pypy/pypy/changeset/a1b0ce5e4915/

Log:	Fixes #2508 -- correctly handle dict.pop where the popping key is
	not the same type as the dict's and pop is called with a default

diff --git a/pypy/objspace/std/dictmultiobject.py b/pypy/objspace/std/dictmultiobject.py
--- a/pypy/objspace/std/dictmultiobject.py
+++ b/pypy/objspace/std/dictmultiobject.py
@@ -1049,6 +1049,8 @@
             else:
                 return d.pop(key, w_default)
         elif self._never_equal_to(space.type(w_key)):
+            if w_default is not None:
+                return w_default
             raise KeyError
         else:
             self.switch_to_object_strategy(w_dict)
diff --git a/pypy/objspace/std/test/test_dictmultiobject.py b/pypy/objspace/std/test/test_dictmultiobject.py
--- a/pypy/objspace/std/test/test_dictmultiobject.py
+++ b/pypy/objspace/std/test/test_dictmultiobject.py
@@ -161,7 +161,7 @@
         w_d.initialize_content([(w(1), wb("a")), (w(2), wb("b"))])
         w_l = self.space.call_method(w_d, "keys")
         assert sorted(self.space.listview_int(w_l)) == [1,2]
-        
+
         # make sure that .keys() calls newlist_bytes for string dicts
         def not_allowed(*args):
             assert False, 'should not be called'
@@ -174,7 +174,7 @@
 
         # XXX: it would be nice if the test passed without monkeypatch.undo(),
         # but we need space.newlist_unicode for it
-        monkeypatch.undo() 
+        monkeypatch.undo()
         w_d = self.space.newdict()
         w_d.initialize_content([(w(u"a"), w(1)), (w(u"b"), w(6))])
         w_l = self.space.call_method(w_d, "keys")
@@ -223,6 +223,10 @@
         assert len(dd) == 1
         raises(KeyError, dd.pop, 33)
 
+        assert d.pop("abc", None) is None
+        raises(KeyError, d.pop, "abc")
+        assert len(d) == 2
+
     def test_has_key(self):
         d = {1: 2, 3: 4}
         assert d.has_key(1)
@@ -1466,4 +1470,3 @@
     fakespace = FakeSpace()
     d = fakespace.newdict(module=True)
     assert type(d.get_strategy()) is BytesDictStrategy
-


More information about the pypy-commit mailing list