Skip to content

Commit

Permalink
Execute code with from __future__ import annotations
Browse files Browse the repository at this point in the history
Also allow passing compiler flags to smart_eval().
  • Loading branch information
asmeurer committed Jul 2, 2024
1 parent 8f0eea3 commit ca9cf20
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions mypython/mypython.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Mypython: a Python REPL the way I like it"""

from __future__ import annotations

# Define globals first so that names from this module don't get included
_default_globals = globals().copy()
import builtins as builtins_mod
Expand Down Expand Up @@ -565,7 +567,8 @@ class NoResult:

mypython_dir = os.path.dirname(__file__)

def smart_eval(stmt, _globals, _locals, filename=None, *, ast_transformer=None):
def smart_eval(stmt, _globals, _locals, filename=None, *,
flags=annotations.compiler_flag, ast_transformer=None):
"""
Automatically exec/eval stmt.
Expand All @@ -580,6 +583,9 @@ def smart_eval(stmt, _globals, _locals, filename=None, *, ast_transformer=None):
linecache. To work properly, "fake" filenames should start with < and end
with >, and be unique for each stmt.
flags is a set of flags to be passed to compile(). The default is to use
from __future__ import annotations.
Note that classes defined with this will have their module set to
'__main__'. To change this, set _globals['__name__'] to the desired
module.
Expand Down Expand Up @@ -614,10 +620,10 @@ def smart_eval(stmt, _globals, _locals, filename=None, *, ast_transformer=None):
res = NoResult
if p.body and isinstance(p.body[-1], ast.Expr):
expr = p.body.pop()
code = compile(p, filename, 'exec')
code = compile(p, filename, 'exec', flags=flags)
exec(code, _globals, _locals)
if expr:
code = compile(ast.Expression(expr.value), filename, 'eval')
code = compile(ast.Expression(expr.value), filename, 'eval', flags=flags)
res = eval(code, _globals, _locals)

return res
Expand Down

0 comments on commit ca9cf20

Please sign in to comment.