diff options
| author | ianb <devnull@localhost> | 2005-05-07 03:08:11 +0000 |
|---|---|---|
| committer | ianb <devnull@localhost> | 2005-05-07 03:08:11 +0000 |
| commit | 247ba69c4614955cf28c022ec34673fa48200538 (patch) | |
| tree | 572fd943d15c958f0ebe4733f7a20a9217565a64 /examples/console | |
| parent | d3245b6b9b3050b29233fbf31ec134cbb6c65940 (diff) | |
| download | paste-247ba69c4614955cf28c022ec34673fa48200538.tar.gz | |
Use evaluation similar to doctest's
Diffstat (limited to 'examples/console')
| -rw-r--r-- | examples/console/web/index.py | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/examples/console/web/index.py b/examples/console/web/index.py index 81f2759..d181857 100644 --- a/examples/console/web/index.py +++ b/examples/console/web/index.py @@ -1,7 +1,34 @@ +from cStringIO import StringIO +import traceback +import pdb +import threading +import sys from console.sitepage import SitePage from console.JSONComponent import JSONComponent 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 class index(SitePage): @@ -14,5 +41,26 @@ class index(SitePage): def jsonMethods(self): return ['run'] - def run(self): - return 'Cool dude!' + 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 |
