Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Jinja2 in Google App Engine

When I'm running Jinja2 in Google App Engine, I get useless debugging information. I gather this is because of this item in the FAQ:

My tracebacks look weird. What’s happening?

If the speedups module is not compiled and you are using a Python installation without ctypes (Python 2.4 without ctypes, Jython or Google’s AppEngine) Jinja2 is unable to provide correct debugging information and the traceback may be incomplete. There is currently no good workaround for Jython or the AppEngine as ctypes is unavailable there and it’s not possible to use the speedups extension.

While there is no 'good' workaround for this at the moment, is there any workaround so that the information printed when exceptions arise can be made more helpful?

Thank you for reading.

Brian

like image 539
Brian M. Hunt Avatar asked Jun 21 '10 15:06

Brian M. Hunt


People also ask

What is the difference between Jinja and Jinja2?

from_string . Jinja 2 provides a Template class that can be used to do the same, but with optional additional configuration. Jinja 1 performed automatic conversion of bytes in a given encoding into unicode objects.

Is Jinja a template engine?

Jinja is a web template engine for the Python programming language. It was created by Armin Ronacher and is licensed under a BSD License. Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox.


1 Answers

You can get around this by adding _ctypes and gestalt to the development server's C module whitelist with monkeypatching.

To do so, put the following snippet at the top of your main.py:

import os if os.environ.get('SERVER_SOFTWARE', '').startswith('Dev'):     # Enable ctypes for Jinja debugging     from google.appengine.tools.dev_appserver import HardenedModulesHook     HardenedModulesHook._WHITE_LIST_C_MODULES += ['_ctypes', 'gestalt'] 

You can also use this trick to enable other C modules, if you have similar local-only module needs. Do note that these modules still won't actually work once you deploy, so tread carefully.

On SDK 1.6.3 using python2.7 you need to change the above code to:

import os if os.environ.get('SERVER_SOFTWARE', '').startswith('Dev'):     # Enable ctypes for Jinja debugging     import sys     from google.appengine.tools.dev_appserver import HardenedModulesHook     assert isinstance(sys.meta_path[0], HardenedModulesHook)     sys.meta_path[0]._white_list_c_modules += ['_ctypes', 'gestalt'] 

On SDK 1.8.6 for python 2.7, try this:

PRODUCTION_MODE = not os.environ.get(     'SERVER_SOFTWARE', 'Development').startswith('Development') if not PRODUCTION_MODE:     from google.appengine.tools.devappserver2.python import sandbox     sandbox._WHITE_LIST_C_MODULES += ['_ctypes', 'gestalt'] 
like image 110
Thomas Johansson Avatar answered Sep 27 '22 16:09

Thomas Johansson