| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 70477ef commit f189da1
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,7 +58,6 @@ | |||
| 58 | 58 | import unicodedata | |
| 59 | 59 | import errno | |
| 60 | 60 | ||
| 61 | - from types import ModuleType | ||
| 62 | 61 | from six.moves import range | |
| 63 | 62 | ||
| 64 | 63 | # These are used for syntax highlighting | |
@@ -1891,9 +1890,6 @@ def main_curses(scr, args, config, interactive=True, locals_=None, | |||
| 1891 | 1890 | curses.raw(True) | |
| 1892 | 1891 | main_win, statusbar = init_wins(scr, config) | |
| 1893 | 1892 | ||
| 1894 | - if locals_ is None: | ||
| 1895 | - sys.modules['__main__'] = ModuleType('__main__') | ||
| 1896 | - locals_ = sys.modules['__main__'].__dict__ | ||
| 1897 | 1893 | interpreter = repl.Interpreter(locals_, getpreferredencoding()) | |
| 1898 | 1894 | ||
| 1899 | 1895 | clirepl = CLIRepl(main_win, interpreter, statusbar, config, idle) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -64,11 +64,6 @@ class Interp(ReplInterpreter): | |||
| 64 | 64 | def __init__(self, locals=None, encoding=None): | |
| 65 | 65 | """Constructor. | |
| 66 | 66 | ||
| 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 | - | ||
| 72 | 67 | We include an argument for the outfile to pass to the formatter for it | |
| 73 | 68 | to write to. | |
| 74 | 69 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,6 +39,7 @@ | |||
| 39 | 39 | import traceback | |
| 40 | 40 | from itertools import takewhile | |
| 41 | 41 | from six import itervalues | |
| 42 | + from types import ModuleType | ||
| 42 | 43 | ||
| 43 | 44 | from pygments.token import Token | |
| 44 | 45 | ||
@@ -77,9 +78,16 @@ def estimate(self): | |||
| 77 | 78 | ||
| 78 | 79 | ||
| 79 | 80 | class Interpreter(code.InteractiveInterpreter): | |
| 81 | + """Source code interpreter for use in bpython.""" | ||
| 80 | 82 | ||
| 81 | 83 | 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 | ||
| 83 | 91 | on a caught syntax error. The purpose for this in bpython is so that | |
| 84 | 92 | the repl can be instantiated after the interpreter (which it | |
| 85 | 93 | necessarily must be with the current factoring) and then an exception | |
@@ -95,6 +103,13 @@ def __init__(self, locals=None, encoding=None): | |||
| 95 | 103 | ||
| 96 | 104 | self.encoding = encoding or sys.getdefaultencoding() | |
| 97 | 105 | 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 | + | ||
| 98 | 113 | # Unfortunately code.InteractiveInterpreter is a classic class, so no | |
| 99 | 114 | # super() | |
| 100 | 115 | code.InteractiveInterpreter.__init__(self, locals) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,7 +40,6 @@ | |||
| 40 | 40 | import time | |
| 41 | 41 | import locale | |
| 42 | 42 | import signal | |
| 43 | - from types import ModuleType | ||
| 44 | 43 | from optparse import Option | |
| 45 | 44 | from six.moves import range | |
| 46 | 45 | from six import iteritems, string_types | |
@@ -1192,11 +1191,7 @@ def main(args=None, locals_=None, banner=None): | |||
| 1192 | 1191 | event_loop = None | |
| 1193 | 1192 | # TODO: there is also a glib event loop. Do we want that one? | |
| 1194 | 1193 | ||
| 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 = {} | ||
| 1200 | 1195 | if options.plugin: | |
| 1201 | 1196 | try: | |
| 1202 | 1197 | from twisted import plugin | |
@@ -1215,10 +1210,12 @@ def main(args=None, locals_=None, banner=None): | |||
| 1215 | 1210 | plugopts = plug.options() | |
| 1216 | 1211 | plugopts.parseOptions(exec_args) | |
| 1217 | 1212 | serv = plug.makeService(plugopts) | |
| 1218 | - locals_['service'] = serv | ||
| 1213 | + extend_locals['service'] = serv | ||
| 1219 | 1214 | reactor.callWhenRunning(serv.startService) | |
| 1220 | 1215 | exec_args = [] | |
| 1221 | 1216 | interpreter = repl.Interpreter(locals_, locale.getpreferredencoding()) | |
| 1217 | + # TODO: replace with something less hack-ish | ||
| 1218 | + interpreter.locals.update(extend_locals) | ||
| 1222 | 1219 | ||
| 1223 | 1220 | # This nabs sys.stdin/out via urwid.MainLoop | |
| 1224 | 1221 | myrepl = URWIDRepl(event_loop, palette, interpreter, config) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments