Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main(args=None):
translations.init()
gtk_options = (_('gtk-specific options'),
_("Options specific to bpython's Gtk+ front end"),
[optparse.Option('--socket-id', dest='socket_id',
type='int', help=_('Embed bpython'))])
config, options, exec_args = bpython.args.parse(args, gtk_options,
True)
interpreter = repl.Interpreter(None, getpreferredencoding())
repl_widget = ReplWidget(interpreter, config)
repl_widget.connect('exit-event', gtk.main_quit)
gobject.idle_add(init_import_completion)
if not exec_args:
sys.path.insert(0, '')
gobject.idle_add(repl_widget.startup)
else:
if options.interactive:
gobject.idle_add(bpython.args.exec_code, interpreter, exec_args)
else:
bpython.args.exec_code(interpreter, exec_args)
return 0
sys.stderr = repl_widget
'twisted for twisted plugin support.\n')
return
for plug in plugin.getPlugins(service.IServiceMaker):
if plug.tapname == options.plugin:
break
else:
sys.stderr.write('Plugin %s does not exist\n' % (options.plugin,))
return
plugopts = plug.options()
plugopts.parseOptions(exec_args)
serv = plug.makeService(plugopts)
locals_['service'] = serv
reactor.callWhenRunning(serv.startService)
exec_args = []
interpreter = repl.Interpreter(locals_, locale.getpreferredencoding())
# This nabs sys.stdin/out via urwid.MainLoop
myrepl = URWIDRepl(event_loop, palette, interpreter, config)
if options.server:
factory = EvalFactory(myrepl)
reactor.listenTCP(options.server, factory, interface='127.0.0.1')
if options.reactor:
# Twisted sets a sigInt handler that stops the reactor unless
# it sees a different custom signal handler.
def sigint(*args):
reactor.callFromThread(myrepl.keyboard_interrupt)
signal.signal(signal.SIGINT, sigint)
# Save stdin, stdout and stderr for later restoration
l = traceback.format_list(tblist)
if l:
l.insert(0, "Traceback (most recent call last peter):\n")
l[len(l):] = traceback.format_exception_only(t, v)
# finally:
# tblist = tb = None
#self.writetb(l)
#self.write("Over-riding bpython's showtraceback with the p4 version to invoke emacsclient...\n")
for line in l:
self.write(line)
if var.excepthookEditor:
invoke_editor(tblist)
from bpython.repl import Interpreter
Interpreter.showtraceback = my_showtraceback
del(my_showtraceback)
def myExceptHook(exctype, excvalue, tb):
try:
te = traceback.extract_tb(tb)
except:
print("tb is %s" % tb)
te = traceback.extract_tb(tb)
# This next line does a regular traceback.
sys.__excepthook__(exctype, excvalue, tb)
if var.excepthookEditor:
invoke_editor(te)
sys.excepthook = myExceptHook
)
return
for plug in plugin.getPlugins(service.IServiceMaker):
if plug.tapname == options.plugin:
break
else:
sys.stderr.write("Plugin %s does not exist\n" % (options.plugin,))
return
plugopts = plug.options()
plugopts.parseOptions(exec_args)
serv = plug.makeService(plugopts)
extend_locals["service"] = serv
reactor.callWhenRunning(serv.startService)
exec_args = []
interpreter = repl.Interpreter(locals_, locale.getpreferredencoding())
# TODO: replace with something less hack-ish
interpreter.locals.update(extend_locals)
# This nabs sys.stdin/out via urwid.MainLoop
myrepl = URWIDRepl(event_loop, palette, interpreter, config)
if options.server:
factory = EvalFactory(myrepl)
reactor.listenTCP(options.server, factory, interface="127.0.0.1")
if options.reactor:
# Twisted sets a sigInt handler that stops the reactor unless
# it sees a different custom signal handler.
def sigint(*args):
reactor.callFromThread(myrepl.keyboard_interrupt)
try:
curses.start_color()
curses.use_default_colors()
cols = make_colors(config)
except curses.error:
cols = FakeDict(-1)
# FIXME: Gargh, bad design results in using globals without a refactor :(
colors = cols
scr.timeout(300)
curses.raw(True)
main_win, statusbar = init_wins(scr, config)
interpreter = repl.Interpreter(locals_, getpreferredencoding())
clirepl = CLIRepl(main_win, interpreter, statusbar, config, idle)
clirepl._C = cols
sys.stdin = FakeStdin(clirepl)
sys.stdout = FakeStream(clirepl, lambda: sys.stdout)
sys.stderr = FakeStream(clirepl, lambda: sys.stderr)
if args:
exit_value = ()
try:
bpargs.exec_code(interpreter, args)
except SystemExit as e:
# The documentation of code.InteractiveInterpreter.runcode claims
# that it reraises SystemExit. However, I can't manage to trigger
# that. To be one the safe side let's catch SystemExit here anyway.
cols = make_colors(config)
except curses.error:
cols = FakeDict(-1)
# FIXME: Gargh, bad design results in using globals without a refactor :(
colors = cols
scr.timeout(300)
curses.raw(True)
main_win, statusbar = init_wins(scr, cols, config)
if locals_ is None:
sys.modules['__main__'] = ModuleType('__main__')
locals_ = sys.modules['__main__'].__dict__
interpreter = Interpreter(locals_, getpreferredencoding())
repl = CLIRepl(main_win, interpreter, statusbar, config, idle)
repl._C = cols
sys.stdin = FakeStdin(repl)
sys.stdout = repl
sys.stderr = repl
if args:
bpython.args.exec_code(interpreter, args)
if not interactive:
curses.raw(False)
return repl.getstdout()
repl.repl()
if config.hist_length:
import Pyro4
import sys
import calculon
from .env import *
from .voltron_integration import *
import repl
if 'bpython' in sys.modules.keys():
# patch repl
import bpython
bpython.repl.Interpreter.runsource = repl.CalculonInterpreter().runsource
# retrieve vended display object
calculon.disp = Pyro4.Proxy(env.main_dir.uri.content)
repl.disp = calculon.disp
print("Connected to calculon")
# connect to voltron
try:
calculon.V = VoltronProxy()
calculon.V.disp = calculon.disp
calculon.formatter = None
except NameError:
pass
self.f_strings = {}
for k, v in iteritems(color_scheme):
self.f_strings[k] = "\x01%s" % (v,)
super(BPythonFormatter, self).__init__(**options)
def format(self, tokensource, outfile):
o = ""
for token, text in tokensource:
while token not in self.f_strings:
token = token.parent
o += "%s\x03%s\x04" % (self.f_strings[token], text)
outfile.write(parse(o.rstrip()))
class Interp(ReplInterpreter):
def __init__(self, locals=None, encoding=None):
"""Constructor.
We include an argument for the outfile to pass to the formatter for it
to write to.
"""
super(Interp, self).__init__(locals, encoding)
# typically changed after being instantiated
# but used when interpreter used corresponding REPL
def write(err_line):
"""Default stderr handler for tracebacks
Accepts FmtStrs so interpreters can output them"""
sys.stderr.write(text_type(err_line))