Skip to content

Instantly share code, notes, and snippets.

@felix021
Created October 28, 2014 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felix021/a9b5aa6a8d9de53c67f9 to your computer and use it in GitHub Desktop.
Save felix021/a9b5aa6a8d9de53c67f9 to your computer and use it in GitHub Desktop.
扔这里存档吧,侵入式的调试工具: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