Skip to content

__future__ statements in bpython -i test.py #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thomasballinger opened this issue Aug 30, 2014 · 2 comments
Closed

__future__ statements in bpython -i test.py #369

thomasballinger opened this issue Aug 30, 2014 · 2 comments
Labels
Milestone

Comments

@thomasballinger
Copy link
Member

While looking into the implications of #284 I found that bpython differs in behavior from python when a file test.py contains

from __future__ import division

and I run (b)python -i test.py and then enter 1/2.

I'm not sure how to go about fixing this - if interpreter.runsource (our current technique) doesn't work, I'm not sure what would - maybe we should be ast inspecting test.py for future statements?

@Trundle
Copy link
Member

Trundle commented Sep 24, 2014

The problem is that bpython.args.exec_code uses the built-in compile function instead of using the interpreter object's compile method. The compile method on interpreter objects is an instance of codeop.CommandCompiler, which remembers what __future__ imports it has already seen.

The following path should fix the issue:

diff --git a/bpython/args.py b/bpython/args.py
index dcf3c7b..ff02635 100644
--- a/bpython/args.py
+++ b/bpython/args.py
@@ -104,8 +104,8 @@ def exec_code(interpreter, args):
     sys.argv
     """
     with open(args[0], 'r') as sourcefile:
-        code_obj = compile(sourcefile.read(), args[0], 'exec')
+        source = sourcefile.read()
     old_argv, sys.argv = sys.argv, args
     sys.path.insert(0, os.path.abspath(os.path.dirname(args[0])))
-    interpreter.runcode(code_obj)
+    interpreter.runsource(source, args[0], 'exec')
     sys.argv = old_argv

@thomasballinger
Copy link
Member Author

This looks good to me! We should stop skipping bpython.test.test_curtsies_repl.TestFutureImports.test_interactive if this is merged, this makes it pass.

@sebastinas sebastinas added this to the 0.14 milestone Feb 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants