[pypy-commit] pypy unpacking-cpython-shortcut: Copy CPython's 'optimization': ignore __iter__ etc. for f(**dict_subclass())

devin.jeanpierre pypy.commits at gmail.com
Wed May 4 18:19:24 EDT 2016


Author: Devin Jeanpierre <jeanpierreda at gmail.com>
Branch: unpacking-cpython-shortcut
Changeset: r84200:4c464c5704eb
Date: 2016-05-04 15:08 -0700
http://bitbucket.org/pypy/pypy/changeset/4c464c5704eb/

Log:	Copy CPython's 'optimization': ignore __iter__ etc. for
	f(**dict_subclass())

	Super unfamiliar with this code, could be we can clean up the
	isinstance check below, or it could be that this is unsafe. :S!

diff --git a/pypy/interpreter/test/test_argument.py b/pypy/interpreter/test/test_argument.py
--- a/pypy/interpreter/test/test_argument.py
+++ b/pypy/interpreter/test/test_argument.py
@@ -688,3 +688,21 @@
         def f(x): pass
         e = raises(TypeError, "f(**{u'ü' : 19})")
         assert "?" in str(e.value)
+
+    def test_starstarargs_dict_subclass(self):
+        def f(**kwargs):
+            return kwargs
+        class DictSubclass(dict):
+            def __iter__(self):
+                yield 'x'
+        # CPython, as an optimization, looks directly into dict internals when
+        # passing one via **kwargs.
+        x =DictSubclass()
+        assert f(**x) == {}
+        x['a'] = 1
+        assert f(**x) == {'a': 1}
+
+    def test_starstarargs_module_dict(self):
+        def f(**kwargs):
+            return kwargs
+        assert f(**globals()) == globals()
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -483,7 +483,7 @@
         return None
 
     def view_as_kwargs(self, w_dict):
-        if type(w_dict) is W_DictObject:
+        if isinstance(w_dict, W_DictObject):
             return w_dict.view_as_kwargs()
         return (None, None)
 


More information about the pypy-commit mailing list