[pypy-commit] pypy py3k: Remove the exec statement, and try to make more tests pass
amauryfa
noreply at buildbot.pypy.org
Wed Oct 12 01:45:39 CEST 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r47961:ffc302d9fec2
Date: 2011-10-12 01:44 +0200
http://bitbucket.org/pypy/pypy/changeset/ffc302d9fec2/
Log: Remove the exec statement, and try to make more tests pass
diff --git a/pypy/interpreter/astcompiler/astbuilder.py b/pypy/interpreter/astcompiler/astbuilder.py
--- a/pypy/interpreter/astcompiler/astbuilder.py
+++ b/pypy/interpreter/astcompiler/astbuilder.py
@@ -272,18 +272,6 @@
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])
@@ -647,8 +635,6 @@
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:
diff --git a/pypy/interpreter/astcompiler/codegen.py b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -757,20 +757,6 @@
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
diff --git a/pypy/interpreter/astcompiler/test/test_compiler.py b/pypy/interpreter/astcompiler/test/test_compiler.py
--- a/pypy/interpreter/astcompiler/test/test_compiler.py
+++ b/pypy/interpreter/astcompiler/test/test_compiler.py
@@ -383,7 +383,7 @@
('''
class Foo(object): pass
foo = Foo()
- exec "'moduledoc'" in foo.__dict__
+ exec("'moduledoc'", foo.__dict__)
''', "moduledoc"),
]:
yield self.simple_test, source, "foo.__doc__", expected
@@ -782,29 +782,18 @@
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)
@@ -870,10 +859,10 @@
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))
diff --git a/pypy/interpreter/astcompiler/tools/Python.asdl b/pypy/interpreter/astcompiler/tools/Python.asdl
--- a/pypy/interpreter/astcompiler/tools/Python.asdl
+++ b/pypy/interpreter/astcompiler/tools/Python.asdl
@@ -42,11 +42,6 @@
| 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
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -514,10 +514,8 @@
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)
diff --git a/pypy/interpreter/pyparser/data/Grammar3.2 b/pypy/interpreter/pyparser/data/Grammar3.2
--- a/pypy/interpreter/pyparser/data/Grammar3.2
+++ b/pypy/interpreter/pyparser/data/Grammar3.2
@@ -33,7 +33,7 @@
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: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
@@ -59,7 +59,6 @@
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
diff --git a/pypy/module/__builtin__/__init__.py b/pypy/module/__builtin__/__init__.py
--- a/pypy/module/__builtin__/__init__.py
+++ b/pypy/module/__builtin__/__init__.py
@@ -77,6 +77,7 @@
'compile' : 'compiling.compile',
'eval' : 'compiling.eval',
+ 'exec' : 'compiling.exec_',
'__build_class__': 'compiling.build_class',
'__import__' : 'pypy.module.imp.importing.importhook',
diff --git a/pypy/module/__builtin__/compiling.py b/pypy/module/__builtin__/compiling.py
--- a/pypy/module/__builtin__/compiling.py
+++ b/pypy/module/__builtin__/compiling.py
@@ -110,6 +110,11 @@
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)
More information about the pypy-commit
mailing list