def test_list(self): e, env = parse_string({}, "[]") self.assertEqual(e, [[]]) e, env = parse_string({}, "[3, 4, 5]") self.assertEqual(e, [[3, 4, 5]]) e, env = parse_string({}, "[3*4, 4*4, 5*4]") self.assertEqual(e, [[12, 16, 20]])
def test_compare_op(self): e, env = parse_string({}, "3 > 4") self.assertEqual(e, [False]) e, env = parse_string({}, "(2*3) > 4") self.assertEqual(e, [True]) e, env = parse_string({}, "[3,4,5] == [5,6,7]") self.assertEqual(e, [False])
def repl(self): while True: try: line = input(">>> ") if self.parse_builtins(line): continue else: e, tenv = parse_string(self.env, line) if ('print_env' in tenv and tenv['print_env']): print(tenv) self.env.update(tenv) for exp in e: print("{}: {}".format(type(exp).__name__, repr(exp))) except SyntaxError as e: print("Syntax error.") print("{}{}^".format(e.text + "\n" if e.text else "", " " * (e.offset))) print(e) except KeyboardInterrupt: print("Interrupt.") except EOFError: # On EOF (Control-D) break the loop. print("Exiting.\n") self.save_history() break except Exception as e: print("{}: {}".format(type(e).__name__, e))
def main(): locals_ = {} globals_ = globalenv eventloop = create_eventloop() try: python_input = PythonInput(get_globals=lambda: globals_, get_locals=lambda: locals_, history_filename=".sevalhist", _completer=BodgedPythonCompleter( lambda: locals_, lambda: globals_)) cli = PythonCommandLineInterface(eventloop=eventloop, python_input=python_input) while 1: python_code = cli.run() if python_code.text == "exit": break try: result, env = parse_string(locals_, python_code.text) for x in result: print(repr(x)) except Exception as e: _handle_exception(cli=cli, e=e, style=python_input._current_style) except EOFError: pass finally: eventloop.close()
def main(): env = global_env.globalenv while True: x = input("> ") responses, env = parse_string(env, x) for r in responses: print(repr(r))
async def seval(self, args, inpipe, outpipe): """this is a sandboxed python interpreter, it is mostly complete""" called = False try: while 1: x = await inpipe.recv() response, _ = parse_string( ChainMap(self.bot.get_env(x), {"print": printer(args, outpipe)}), args.text) called = True if response: for r in response: outpipe.send(args.reply(data=r, str_fn=repr)) except PipeClosed: if not called: response, _ = parse_string( ChainMap(self.bot.get_env(args.reply()), {"print": printer(args, outpipe)}), args.text) if response: for r in response: outpipe.send(args.reply(data=r, str_fn=repr)) finally: outpipe.close() inpipe.close()
def test_bin_op(self): e, env = parse_string({}, "1 + 3") self.assertEqual(e, [4]) e, _ = parse_string({}, "3 // 4") self.assertEqual(e, [0])
def test_unary_op(self): e, env = parse_string({}, "not True") self.assertEqual(e, [False])
def test_dict(self): e, env = parse_string({}, "{'x': 4}") self.assertEqual(e, [{'x': 4}])
def test_tuple(self): e, env = parse_string({}, "(3, 4)") self.assertEqual(e, [(3, 4)])
def test_env_lookup(self): e, env = parse_string({'x': 3}, "x") self.assertEqual(e, [3]) e, env = parse_string({'x': {'y': 3}}, "x") self.assertEqual(e, [{'y': 3}])