Debug Jinja2 in Google App Engine

前端 未结 6 1487
执笔经年
执笔经年 2020-12-13 02:24

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 l

6条回答
  •  攒了一身酷
    2020-12-13 02:57

    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']
    

提交回复
热议问题