Skip to content

Commit f189da1

Browse files
committed
Consolidate common locals setup (fixes bpython#665)
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent 70477ef commit f189da1

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

bpython/cli.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import unicodedata
5959
import errno
6060

61-
from types import ModuleType
6261
from six.moves import range
6362

6463
# These are used for syntax highlighting
@@ -1891,9 +1890,6 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
18911890
curses.raw(True)
18921891
main_win, statusbar = init_wins(scr, config)
18931892

1894-
if locals_ is None:
1895-
sys.modules['__main__'] = ModuleType('__main__')
1896-
locals_ = sys.modules['__main__'].__dict__
18971893
interpreter = repl.Interpreter(locals_, getpreferredencoding())
18981894

18991895
clirepl = CLIRepl(main_win, interpreter, statusbar, config, idle)

bpython/curtsiesfrontend/interpreter.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ class Interp(ReplInterpreter):
6464
def __init__(self, locals=None, encoding=None):
6565
"""Constructor.
6666
67-
The optional 'locals' argument specifies the dictionary in
68-
which code will be executed; it defaults to a newly created
69-
dictionary with key "__name__" set to "__console__" and key
70-
"__doc__" set to None.
71-
7267
We include an argument for the outfile to pass to the formatter for it
7368
to write to.
7469
"""

bpython/repl.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import traceback
4040
from itertools import takewhile
4141
from six import itervalues
42+
from types import ModuleType
4243

4344
from pygments.token import Token
4445

@@ -77,9 +78,16 @@ def estimate(self):
7778

7879

7980
class Interpreter(code.InteractiveInterpreter):
81+
"""Source code interpreter for use in bpython."""
8082

8183
def __init__(self, locals=None, encoding=None):
82-
"""The syntaxerror callback can be set at any time and will be called
84+
"""Constructor.
85+
86+
The optional 'locals' argument specifies the dictionary in which code
87+
will be executed; it defaults to a newly created dictionary with key
88+
"__name__" set to "__main__".
89+
90+
The syntaxerror callback can be set at any time and will be called
8391
on a caught syntax error. The purpose for this in bpython is so that
8492
the repl can be instantiated after the interpreter (which it
8593
necessarily must be with the current factoring) and then an exception
@@ -95,6 +103,13 @@ def __init__(self, locals=None, encoding=None):
95103

96104
self.encoding = encoding or sys.getdefaultencoding()
97105
self.syntaxerror_callback = None
106+
107+
if locals is None:
108+
# instead of messing with sys.modules, we should modify sys.modules
109+
# in the interpreter instance
110+
sys.modules['__main__'] = main_mod = ModuleType('__main__')
111+
locals = main_mod.__dict__
112+
98113
# Unfortunately code.InteractiveInterpreter is a classic class, so no
99114
# super()
100115
code.InteractiveInterpreter.__init__(self, locals)

bpython/urwid.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import time
4141
import locale
4242
import signal
43-
from types import ModuleType
4443
from optparse import Option
4544
from six.moves import range
4645
from six import iteritems, string_types
@@ -1192,11 +1191,7 @@ def main(args=None, locals_=None, banner=None):
11921191
event_loop = None
11931192
# TODO: there is also a glib event loop. Do we want that one?
11941193

1195-
# __main__ construction from bpython.cli
1196-
if locals_ is None:
1197-
main_mod = sys.modules['__main__'] = ModuleType('__main__')
1198-
locals_ = main_mod.__dict__
1199-
1194+
extend_locals = {}
12001195
if options.plugin:
12011196
try:
12021197
from twisted import plugin
@@ -1215,10 +1210,12 @@ def main(args=None, locals_=None, banner=None):
12151210
plugopts = plug.options()
12161211
plugopts.parseOptions(exec_args)
12171212
serv = plug.makeService(plugopts)
1218-
locals_['service'] = serv
1213+
extend_locals['service'] = serv
12191214
reactor.callWhenRunning(serv.startService)
12201215
exec_args = []
12211216
interpreter = repl.Interpreter(locals_, locale.getpreferredencoding())
1217+
# TODO: replace with something less hack-ish
1218+
interpreter.locals.update(extend_locals)
12221219

12231220
# This nabs sys.stdin/out via urwid.MainLoop
12241221
myrepl = URWIDRepl(event_loop, palette, interpreter, config)

0 commit comments

Comments
 (0)