Skip to content

Commit e69cfe3

Browse files
committed
Behave like python3 when executing files with -i (fixes #919)
1 parent b66a29f commit e69cfe3

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

bpython/args.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,17 @@ def callback(group):
216216

217217
def exec_code(interpreter, args):
218218
"""
219-
Helper to execute code in a given interpreter. args should be a [faked]
220-
sys.argv
219+
Helper to execute code in a given interpreter, e.g. to implement the behavior of python3 [-i] file.py
220+
221+
args should be a [faked] sys.argv.
221222
"""
222-
with open(args[0]) as sourcefile:
223-
source = sourcefile.read()
223+
try:
224+
with open(args[0]) as sourcefile:
225+
source = sourcefile.read()
226+
except OSError as e:
227+
# print an error and exit (if -i is specified the calling code will continue)
228+
print(f"bpython: can't open file '{args[0]}: {e}", file=sys.stderr)
229+
raise SystemExit(e.errno)
224230
old_argv, sys.argv = sys.argv, args
225231
sys.path.insert(0, os.path.abspath(os.path.dirname(args[0])))
226232
spec = importlib.util.spec_from_loader("__console__", loader=None)

0 commit comments

Comments
 (0)