Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r58873:65d95e70dcb9
Date: 2012-11-13 17:37 -0800
http://bitbucket.org/pypy/pypy/changeset/65d95e70dcb9/

Log:    restrict globals to an exact dict in exec/eval

diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -1514,12 +1514,15 @@
             locals = globals
 
         if not isinstance(globals, dict):
-            if not hasattr(globals, '__getitem__'):
-                raise TypeError("exec: arg 2 must be a dictionary or None")
+            raise TypeError(
+                "exec: arg 2 must be a dictionary or None, not %s" %
+                type(globals).__name__)
         globals.setdefault('__builtins__', builtin)
         if not isinstance(locals, dict):
             if not hasattr(locals, '__getitem__'):
-                raise TypeError("exec: arg 3 must be a dictionary or None")
+                raise TypeError(
+                    "exec: arg 3 must be a mapping or None, not %s" %
+                    type(locals.__name__))
 
         if not isinstance(prog, codetype):
             filename = '<string>'
diff --git a/pypy/module/__builtin__/test/test_builtin.py 
b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -482,6 +482,19 @@
         raises(ValueError, compile, "\n", "<string>", "exec", 0xff)
         raises(TypeError, compile, '1+2', 12, 34)
 
+        class M:
+            def __getitem__(self, key):
+                pass
+            def keys(self):
+                pass
+        m = M()
+        try:
+            exec('pass', m)
+        except TypeError:
+            pass
+        else:
+            assert False, 'Expected TypeError'
+
     def test_unicode_encoding_compile(self):
         code = "# -*- coding: utf-8 -*-\npass\n"
         compile(code, "tmp", "exec")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to