Created
October 28, 2014 09:16
-
-
Save felix021/a9b5aa6a8d9de53c67f9 to your computer and use it in GitHub Desktop.
扔这里存档吧,侵入式的调试工具:D 用法:pyinterpreter.thread_interpreter(globals(), locals())
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding:utf-8 | |
import sys | |
import compiler | |
import thread | |
def thread_interpreter(globals_dict, locals_dict): | |
thread.start_new_thread(interpreter, (globals_dict, locals_dict)) | |
def interpreter(globals_dict, locals_dict): | |
while True: | |
block = [] | |
first_line = True | |
while True: | |
if first_line: | |
sys.stdout.write('>>> ') | |
first_line = False | |
else: | |
sys.stdout.write('... ') | |
line = sys.stdin.readline() | |
block.append(line) | |
line = line.rstrip() | |
if line.startswith('\t') or line.startswith(' ') \ | |
or line.endswith(':') or line.endswith('\\'): | |
continue | |
else: | |
break | |
code = ''.join(block) | |
if code == '': | |
break | |
try: | |
stmt = compiler.parse(code).getChildNodes()[0].getChildNodes()[0] | |
except: | |
stmt = None | |
if isinstance(stmt, compiler.ast.Discard): | |
code = '____ret = ' + code | |
try: | |
exec(code, globals_dict, locals_dict) | |
if isinstance(stmt, compiler.ast.Discard): | |
print locals_dict['____ret'] | |
except Exception, e: | |
print """Traceback (most recent call last): \n File "<stdin>", line 1, in <module>""" | |
print '%s: %s' % (e.__class__.__name__, e) | |
if __name__ == "__main__": | |
interpreter(globals(), locals()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment