Commits

Amaury Forgeot d'Arc committed ffc302d

Remove the exec statement, and try to make more tests pass

Comments (0)

Files changed (8)

pypy/interpreter/astcompiler/astbuilder.py

                  for i in range(1, len(global_node.children), 2)]
         return ast.Global(names, global_node.lineno, global_node.column)
 
-    def handle_exec_stmt(self, exec_node):
-        child_count = len(exec_node.children)
-        globs = None
-        locs = None
-        to_execute = self.handle_expr(exec_node.children[1])
-        if child_count >= 4:
-            globs = self.handle_expr(exec_node.children[3])
-        if child_count == 6:
-            locs = self.handle_expr(exec_node.children[5])
-        return ast.Exec(to_execute, globs, locs, exec_node.lineno,
-                        exec_node.column)
-
     def handle_assert_stmt(self, assert_node):
         child_count = len(assert_node.children)
         expr = self.handle_expr(assert_node.children[1])
                 return self.handle_global_stmt(stmt)
             elif stmt_type == syms.assert_stmt:
                 return self.handle_assert_stmt(stmt)
-            elif stmt_type == syms.exec_stmt:
-                return self.handle_exec_stmt(stmt)
             else:
                 raise AssertionError("unhandled small statement")
         elif stmt_type == syms.compound_stmt:

pypy/interpreter/astcompiler/codegen.py

                     arg += 1
         self.emit_op_arg(ops.RAISE_VARARGS, arg)
 
-    def visit_Exec(self, exc):
-        self.update_position(exc.lineno, True)
-        exc.body.walkabout(self)
-        if exc.globals:
-            exc.globals.walkabout(self)
-            if exc.locals:
-                exc.locals.walkabout(self)
-            else:
-                self.emit_op(ops.DUP_TOP)
-        else:
-            self.load_const(self.space.w_None)
-            self.emit_op(ops.DUP_TOP)
-        self.emit_op(ops.EXEC_STMT)
-
     def visit_Global(self, glob):
         # Handled in symbol table building.
         pass

pypy/interpreter/astcompiler/test/test_compiler.py

             ('''
                 class Foo(object): pass
                 foo = Foo()
-                exec "'moduledoc'" in foo.__dict__
+                exec("'moduledoc'", foo.__dict__)
              ''',                            "moduledoc"),
             ]:
             yield self.simple_test, source, "foo.__doc__", expected
 class AppTestCompiler:
 
     def test_docstring_not_loaded(self):
-        import StringIO, dis, sys
+        import io, dis, sys
         ns = {}
-        exec "def f():\n    'hi'" in ns
+        exec("def f():\n    'hi'", ns)
         f = ns["f"]
         save = sys.stdout
-        sys.stdout = output = StringIO.StringIO()
+        sys.stdout = output = io.BytesIO()
         try:
             dis.dis(f)
         finally:
             sys.stdout = save
         assert "0 ('hi')" not in output.getvalue()
 
-    def test_print_to(self):
-         exec """if 1:
-         from StringIO import StringIO
-         s = StringIO()
-         print >> s, "hi", "lovely!"
-         assert s.getvalue() == "hi lovely!\\n"
-         s = StringIO()
-         print >> s, "hi", "lovely!",
-         assert s.getvalue() == "hi lovely!"
-         """ in {}
-
 class TestOptimizations:
     def count_instructions(self, source):
         code, blocks = generate_function_code(source, self.space)
         space = self.space
         w_generator = space.appexec([], """():
             d = {}
-            exec '''def f(x):
+            exec('''def f(x):
                 return
                 yield 6
-            ''' in d
+            ''', d)
             return d['f'](5)
         """)
         assert 'generator' in space.str_w(space.repr(w_generator))

pypy/interpreter/astcompiler/tools/Python.asdl

 	      | Import(alias* names)
 	      | ImportFrom(identifier? module, alias* names, int? level)
 
-	      -- Doesn't capture requirement that locals must be
-	      -- defined if globals is
-	      -- still supports use as a function!
-	      | Exec(expr body, expr? globals, expr? locals)
-
 	      | Global(identifier* names)
 	      | Expr(expr value)
 	      | Pass | Break | Continue

pypy/interpreter/pyopcode.py

     def STORE_LOCALS(self, oparg, next_instr):
         self.w_locals = self.popvalue()
 
-    def EXEC_STMT(self, oparg, next_instr):
-        w_locals = self.popvalue()
-        w_globals = self.popvalue()
-        w_prog = self.popvalue()
+    def exec_(self, w_prog, w_locals, w_globals):
+        """The ___builtin__.exec function."""
         ec = self.space.getexecutioncontext()
         flags = ec.compiler.getcodeflags(self.pycode)
         w_compile_flags = self.space.wrap(flags)

pypy/interpreter/pyparser/data/Grammar3.2

 stmt: simple_stmt | compound_stmt
 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
 small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
-             import_stmt | global_stmt | exec_stmt | assert_stmt)
+             import_stmt | global_stmt | assert_stmt)
 expr_stmt: testlist (augassign (yield_expr|testlist) |
                      ('=' (yield_expr|testlist))*)
 augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
 dotted_as_names: dotted_as_name (',' dotted_as_name)*
 dotted_name: NAME ('.' NAME)*
 global_stmt: 'global' NAME (',' NAME)*
-exec_stmt: 'exec' expr ['in' test [',' test]]
 assert_stmt: 'assert' test [',' test]
 
 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated

pypy/module/__builtin__/__init__.py

 
         'compile'       : 'compiling.compile',
         'eval'          : 'compiling.eval',
+        'exec'          : 'compiling.exec_',
         '__build_class__': 'compiling.build_class',
 
         '__import__'    : 'pypy.module.imp.importing.importhook',

pypy/module/__builtin__/compiling.py

 
     return codeobj.exec_code(space, w_globals, w_locals)
 
+def exec_(space, w_prog, w_globals=None, w_locals=None):
+    ec = space.getexecutioncontext()
+    frame = ec.gettopframe_nohidden()
+    frame.exec_(w_prog, w_globals, w_locals)
+
 def build_class(space, w_func, w_name, __args__):
     bases_w, kwds_w = __args__.unpack()
     w_bases = space.newtuple(bases_w)