Skip to content

Commit f42ad9b

Browse files
fix reload in Python 3
1 parent 5619544 commit f42ad9b

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

bpython/curtsies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from bpython.curtsiesfrontend.repl import BaseRepl
1616
from bpython.curtsiesfrontend.coderunner import SystemExitFromCodeGreenlet
17+
from bpython.curtsiesfrontend.interpreter import Interp
1718
from bpython import args as bpargs
1819
from bpython import translations
1920
from bpython.translations import _
@@ -174,7 +175,7 @@ def main(args=None, locals_=None, banner=None, welcome_message=None):
174175
paste.events.extend(sourcecode)
175176
else:
176177
try:
177-
interp = code.InteractiveInterpreter(locals=locals_)
178+
interp = Interp(locals=locals_)
178179
bpargs.exec_code(interp, exec_args)
179180
except SystemExit as e:
180181
exit_value = e.args

bpython/curtsiesfrontend/sitefix.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from six.moves import builtins
55

6+
from bpython._py3compat import py3
7+
68

79
def resetquit(builtins):
810
"""Redefine builtins 'quit' and 'exit' not so close stdin
@@ -18,25 +20,32 @@ def monkeypatch_quit():
1820
if 'site' in sys.modules:
1921
resetquit(builtins)
2022

21-
22-
orig_reload = builtins.reload
23-
24-
25-
def reload(module):
26-
if module is sys:
27-
orig_stdout = sys.stdout
28-
orig_stderr = sys.stderr
29-
orig_stdin = sys.stdin
30-
orig_reload(sys)
31-
sys.stdout = orig_stdout
32-
sys.stderr = orig_stderr
33-
sys.stdin = orig_stdin
34-
else:
35-
builtins.reload(sys)
36-
37-
38-
functools.update_wrapper(reload, orig_reload)
23+
orig_reload = None
24+
if py3:
25+
import importlib
26+
if hasattr(importlib, 'reload'):
27+
orig_reload = importlib.reload
28+
else:
29+
orig_reload = builtins.reload
30+
31+
if orig_reload:
32+
def reload(module):
33+
if module is sys:
34+
orig_stdout = sys.stdout
35+
orig_stderr = sys.stderr
36+
orig_stdin = sys.stdin
37+
orig_reload(sys)
38+
sys.stdout = orig_stdout
39+
sys.stderr = orig_stderr
40+
sys.stdin = orig_stdin
41+
else:
42+
builtins.reload(sys)
43+
44+
functools.update_wrapper(reload, orig_reload)
3945

4046

4147
def monkeypatch_reload():
42-
builtins.reload = reload
48+
if py3 and hasattr(importlib, 'reload'):
49+
importlib.reload = reload
50+
else:
51+
builtins.reload = reload

0 commit comments

Comments
 (0)