from cStringIO import StringIO
import traceback
import threading
import pdb
import sys
exec_lock = threading.Lock()
class EvalContext(object):
"""
Class that represents a interactive interface. It has its own
namespace. Use eval_context.exec_expr(expr) to run commands; the
output of those commands is returned, as are print statements.
This is essentially what doctest does, and is taken directly from
doctest.
"""
def __init__(self, namespace, globs):
self.namespace = namespace
self.globs = globs
def exec_expr(self, s):
out = StringIO()
exec_lock.acquire()
save_stdout = sys.stdout
try:
debugger = _OutputRedirectingPdb(save_stdout)
debugger.reset()
pdb.set_trace = debugger.set_trace
sys.stdout = out
try:
code = compile(s, '