2
Implementation of the interpreter-level compile/eval builtins.
5
from pypy.interpreter.pycode import PyCode
6
from pypy.interpreter.baseobjspace import W_Root, ObjSpace
7
from pypy.interpreter.error import OperationError
8
from pypy.interpreter.gateway import NoneNotWrapped
10
def compile(space, w_source, filename, mode, flags=0, dont_inherit=0):
11
"""Compile the source string (a Python module, statement or expression)
12
into a code object that can be executed by the exec statement or eval().
13
The filename will be used for run-time error messages.
14
The mode must be 'exec' to compile a module, 'single' to compile a
15
single (interactive) statement, or 'eval' to compile an expression.
16
The flags argument, if present, controls which future statements influence
17
the compilation of the code.
18
The dont_inherit argument, if non-zero, stops the compilation inheriting
19
the effects of any future statements in effect in the code calling
20
compile; if absent or zero these statements do influence the compilation,
21
in addition to any features explicitly specified.
23
if space.is_true(space.isinstance(w_source, space.w_unicode)):
24
# hack: encode the unicode string as UTF-8 and attach
26
w_source = space.call_method(w_source, 'encode', space.wrap('utf-8'))
27
str_ = space.str_w(w_source)
28
str_ = '\xEF\xBB\xBF' + str_
30
str_ = space.str_w(w_source)
32
ec = space.getexecutioncontext()
35
caller = ec.framestack.top()
39
flags |= ec.compiler.getcodeflags(caller.getcode())
41
if mode not in ('exec', 'eval', 'single'):
42
raise OperationError(space.w_ValueError,
43
space.wrap("compile() arg 3 must be 'exec' "
44
"or 'eval' or 'single'"))
46
code = ec.compiler.compile(str_, filename, mode, flags)
47
return space.wrap(code)
49
compile.unwrap_spec = [ObjSpace,W_Root,str,str,int,int]
52
def eval(space, w_code, w_globals=NoneNotWrapped, w_locals=NoneNotWrapped):
53
"""Evaluate the source in the context of globals and locals.
54
The source may be a string representing a Python expression
55
or a code object as returned by compile(). The globals and locals
56
are dictionaries, defaulting to the current current globals and locals.
57
If only globals is given, locals defaults to it.
61
if (space.is_true(space.isinstance(w_code, space.w_str)) or
62
space.is_true(space.isinstance(w_code, space.w_unicode))):
64
w_code = compile(space,
65
space.call_method(w_code, 'lstrip',
68
except OperationError, e:
69
if e.match(space, space.w_SyntaxError):
70
e_value_w = space.unpacktuple(e.w_value)
71
if len(e_value_w) == 2:
72
e_loc_w = space.unpacktuple(e_value_w[1])
73
e.w_value = space.newtuple([e_value_w[0],
74
space.newtuple([space.w_None]+
80
codeobj = space.interpclass_w(w_code)
81
if not isinstance(codeobj, PyCode):
82
raise OperationError(space.w_TypeError,
83
w('eval() arg 1 must be a string or code object'))
86
caller = space.getexecutioncontext().framestack.top()
90
if w_globals is None or space.is_w(w_globals, space.w_None):
92
w_globals = w_locals = space.newdict()
94
w_globals = caller.w_globals
95
w_locals = caller.getdictscope()
96
elif w_locals is None:
100
space.getitem(w_globals, space.wrap('__builtins__'))
101
except OperationError, e:
102
if not e.match(space, space.w_KeyError):
104
if caller is not None:
105
w_builtin = space.builtin.pick_builtin(caller.w_globals)
106
space.setitem(w_globals, space.wrap('__builtins__'), w_builtin)
108
return codeobj.exec_code(space, w_globals, w_locals)