diff options
| author | ianb <devnull@localhost> | 2005-05-07 07:47:55 +0000 |
|---|---|---|
| committer | ianb <devnull@localhost> | 2005-05-07 07:47:55 +0000 |
| commit | 76fcfe11f5268dffd0b3f2aae256e3f8e83a8ed3 (patch) | |
| tree | 6eb84757d4fd4c4f363a304231ba65dc86d214a7 /examples/console | |
| parent | 672a4dd88a1764a9d1a4342d06e2dec35c9b8c10 (diff) | |
| download | paste-76fcfe11f5268dffd0b3f2aae256e3f8e83a8ed3.tar.gz | |
Moved evaluation into its own module
Diffstat (limited to 'examples/console')
| -rw-r--r-- | examples/console/evalcontext.py | 65 | ||||
| -rw-r--r-- | examples/console/web/index.py | 57 |
2 files changed, 71 insertions, 51 deletions
diff --git a/examples/console/evalcontext.py b/examples/console/evalcontext.py new file mode 100644 index 0000000..70cd35f --- /dev/null +++ b/examples/console/evalcontext.py @@ -0,0 +1,65 @@ +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): + self.namespace = {} + + 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, '<web>', "single", 0, 1) + exec code in self.namespace + debugger.set_continue() + except KeyboardInterrupt: + raise + except: + traceback.print_exc(file=out) + debugger.set_continue() + finally: + sys.stdout = save_stdout + exec_lock.release() + return out.getvalue() + +# From doctest +class _OutputRedirectingPdb(pdb.Pdb): + """ + A specialized version of the python debugger that redirects stdout + to a given stream when interacting with the user. Stdout is *not* + redirected when traced code is executed. + """ + def __init__(self, out): + self.__out = out + pdb.Pdb.__init__(self) + + def trace_dispatch(self, *args): + # Redirect stdout to the given stream. + save_stdout = sys.stdout + sys.stdout = self.__out + # Call Pdb's trace dispatch method. + try: + return pdb.Pdb.trace_dispatch(self, *args) + finally: + sys.stdout = save_stdout diff --git a/examples/console/web/index.py b/examples/console/web/index.py index d181857..aec3498 100644 --- a/examples/console/web/index.py +++ b/examples/console/web/index.py @@ -1,34 +1,8 @@ -from cStringIO import StringIO -import traceback -import pdb -import threading -import sys from console.sitepage import SitePage from console.JSONComponent import JSONComponent +from console.evalcontext import EvalContext -global_namespace = {} -namespace_lock = threading.Lock() - -# From doctest -class _OutputRedirectingPdb(pdb.Pdb): - """ - A specialized version of the python debugger that redirects stdout - to a given stream when interacting with the user. Stdout is *not* - redirected when traced code is executed. - """ - def __init__(self, out): - self.__out = out - pdb.Pdb.__init__(self) - - def trace_dispatch(self, *args): - # Redirect stdout to the given stream. - save_stdout = sys.stdout - sys.stdout = self.__out - # Call Pdb's trace dispatch method. - try: - return pdb.Pdb.trace_dispatch(self, *args) - finally: - sys.stdout = save_stdout +context = EvalContext() class index(SitePage): @@ -41,26 +15,7 @@ class index(SitePage): def jsonMethods(self): return ['run'] - def run(self, value): - out = StringIO() - namespace_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(value, '<web>', "single", 0, 1) - exec code in global_namespace - debugger.set_continue() - except KeyboardInterrupt: - raise - except: - traceback.print_exc(file=out) - debugger.set_continue() - finally: - sys.stdout = save_stdout - namespace_lock.release() - value = out.getvalue() - return value + def run(self, expr): + if not expr.endswith('\n'): + expr += '\n' + return context.exec_expr(expr) |
