May 15, 2009

A patch for Werkzeug debugger on GAE

I wrote about using werkzeug on gae, but actually there are still several problems in debug console. So I wrote a patch for it.



diff -r 8ffe4e637e17 werkzeug/debug/console.py
--- a/werkzeug/debug/console.py Sat Apr 25 17:06:40 2009 +0000
+++ b/werkzeug/debug/console.py Fri May 15 00:40:32 2009 +0900
@@ -32,6 +32,12 @@
def close(self):
pass

+ def flush(self):
+ pass
+
+ def seek(self, n, mode=0):
+ pass
+
def reset(self):
val = ''.join(self._buffer)
del self._buffer[:]
@@ -48,12 +54,18 @@
def writelines(self, x):
self._write(escape(''.join(x)))

+ def readline(self):
+ if len(self._buffer) == 0:
+ return ''
+ ret = self._buffer[0]
+ del self._buffer[0]
+ return ret

class ThreadedStream(object):
"""Thread-local wrapper for sys.stdout for the interactive console."""

def push():
- if sys.stdout is sys.__stdout__:
+ if not isinstance(sys.stdout, ThreadedStream):
sys.stdout = ThreadedStream()
_local.stream = HTMLStringO()
push = staticmethod(push)

May 14, 2009

Using Werkzeug's debugger on GAE dev server

There is a very good web framework called Werkzeug. The debugger of Werkzeug is pretty nice and we can use inline console on our web browser with it.

The author of Werkzeug wrote an article of how to use this debugger with GAE dev server. However according this article, the inline console is not usable.

After a little struggling, finally I can use the debugger on GAE dev server. GAE dev server creates an instance of DebuggedApplication on every request. I think its the reason why. So, it seems that specifying the instance as the module global singleton made it work.



_debugged_app = None
app = ... build your wsgi app ...

def main():
# Only run the debugger in development.
import os, sys
if 'SERVER_SOFTWARE' in os.environ and os.environ['SERVER_SOFTWARE'].startswith('Dev'):
# use our debug.utils with Jinja2 templates
import debug.utils
sys.modules['werkzeug.debug.utils'] = debug.utils

# don't use inspect.getsourcefile because the imp module is empty
import inspect
inspect.getsourcefile = inspect.getfile

# wrap the application
from werkzeug import DebuggedApplication
global _debugged_app
if _debugged_app is None:
_debugged_app = app = DebuggedApplication(app, evalex=True)
else:
app = _debugged_app

Google App Engine で Werkzeug のデバッガを使う

Werkzeug という Python Web Framework があります。Werkzeug のデバッガはなかなかすぐれもので Web アプリでエラーが出るとインライン console が使えたりします。

このデバッガを GAE の dev server で使う方法を作者の方が書いているのですが、この通りやるとインライン console が使えなかったりします。

ちょっと調べてみたら意外と簡単に使えました。GAE の dev server だとリクエスト毎に DebuggedApplication のインスタンスが作り直されてしまうので、これを module global のシングルトンにしてやればオッケイでした。



_debugged_app = None
app = ... build your wsgi app ...

def main():
# Only run the debugger in development.
import os, sys
if 'SERVER_SOFTWARE' in os.environ and os.environ['SERVER_SOFTWARE'].startswith('Dev'):
# use our debug.utils with Jinja2 templates
import debug.utils
sys.modules['werkzeug.debug.utils'] = debug.utils

# don't use inspect.getsourcefile because the imp module is empty
import inspect
inspect.getsourcefile = inspect.getfile

# wrap the application
from werkzeug import DebuggedApplication
global _debugged_app
if _debugged_app is None:
_debugged_app = app = DebuggedApplication(app, evalex=True)
else:
app = _debugged_app