Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
扔这里存档吧,侵入式的调试工具:D 用法:pyinterpreter.thread_interpreter(globals(), locals())
#!/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
You can’t perform that action at this time.