[pypy-svn] r16734 - in pypy/release/0.7.x/pypy: interpreter interpreter/test lib

arigo at codespeak.net arigo at codespeak.net
Sat Aug 27 12:51:02 CEST 2005


Author: arigo
Date: Sat Aug 27 12:51:00 2005
New Revision: 16734

Modified:
   pypy/release/0.7.x/pypy/interpreter/pyopcode.py
   pypy/release/0.7.x/pypy/interpreter/test/test_exec.py
   pypy/release/0.7.x/pypy/lib/operator.py
Log:
Fix the 'exec' statement refusing general mappings as globals and locals.
Should help make CPython's test_builtin more happy.


Modified: pypy/release/0.7.x/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/release/0.7.x/pypy/interpreter/pyopcode.py	(original)
+++ pypy/release/0.7.x/pypy/interpreter/pyopcode.py	Sat Aug 27 12:51:00 2005
@@ -874,14 +874,12 @@
             locals = globals
 
         if not isinstance(globals, dict):
-            if not (hasattr(globals, '__getitem__') and
-                    hasattr(globals, 'keys')):
+            if hasattr(globals, '__getitem__'):
                 raise TypeError("exec: arg 2 must be a dictionary or None")
         if '__builtins__' not in globals:
             globals['__builtins__'] = builtin
         if not isinstance(locals, dict):
-            if not (hasattr(locals, '__getitem__') and
-                    hasattr(locals, 'keys')):
+            if hasattr(locals, '__getitem__'):
                 raise TypeError("exec: arg 3 must be a dictionary or None")
 
         if not isinstance(prog, codetype):

Modified: pypy/release/0.7.x/pypy/interpreter/test/test_exec.py
==============================================================================
--- pypy/release/0.7.x/pypy/interpreter/test/test_exec.py	(original)
+++ pypy/release/0.7.x/pypy/interpreter/test/test_exec.py	Sat Aug 27 12:51:00 2005
@@ -174,3 +174,16 @@
         d = {}
         exec "x=5 " in d
         assert d['x'] == 5
+
+    def test_mapping_as_locals(self):
+        class M(object):
+            def __getitem__(self, key):
+                return key
+            def __setitem__(self, key, value):
+                self.result[key] = value
+        m = M()
+        m.result = {}
+        exec "x=m" in {}, m
+        assert m.result == {'x': 'm'}
+        exec "y=n" in m   # NOTE: this doesn't work in CPython 2.4
+        assert m.result == {'x': 'm', 'y': 'n'}

Modified: pypy/release/0.7.x/pypy/lib/operator.py
==============================================================================
--- pypy/release/0.7.x/pypy/lib/operator.py	(original)
+++ pypy/release/0.7.x/pypy/lib/operator.py	Sat Aug 27 12:51:00 2005
@@ -101,6 +101,7 @@
 # XXX the following is approximative
 def isMappingType(obj,):
     'isMappingType(a) -- Return True if a has a mapping type, False otherwise.'
+    # XXX this is fragile and approximative anyway
     return hasattr(obj, '__getitem__') and hasattr(obj, 'keys')
 def isNumberType(obj,):
     'isNumberType(a) -- Return True if a has a numeric type, False otherwise.'



More information about the Pypy-commit mailing list