From b314f85e9e7df341bfb44caf67c207885b6938cd Mon Sep 17 00:00:00 2001 From: Zev Goldstein Date: Thu, 1 Aug 2019 17:46:13 -0400 Subject: [PATCH] fix module init issues caused by import cycle webapp and webapp2 import eachother. Currently if they aren't imported in the correct order, they wouldn't initiaze properly. By deferring all of the webapp imports in webapp2 until after initialization has finished, we sidestep the problem. --- webapp2.py | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/webapp2.py b/webapp2.py index e48e7bf..64a7b4b 100755 --- a/webapp2.py +++ b/webapp2.py @@ -46,7 +46,7 @@ from webob import exc -_webapp = _webapp_util = _local = None +_local = None try: # pragma: no cover @@ -65,15 +65,6 @@ html = cgi -# google.appengine.ext.webapp imports webapp2 in the -# App Engine Python 2.7 runtime. -if os.environ.get('APPENGINE_RUNTIME') != 'python27': # pragma: no cover - try: - from google.appengine.ext import webapp as _webapp - except ImportError: # pragma: no cover - # Running webapp2 outside of GAE. - pass - try: # pragma: no cover # Thread-local variables container. from webapp2_extras import local @@ -1341,12 +1332,18 @@ class and function views or, for compatibility purposes, a A wrapped handler callable. """ if inspect.isclass(handler): - if _webapp and issubclass(handler, _webapp.RequestHandler): - # Compatible with webapp.RequestHandler. - adapter = WebappHandlerAdapter - else: - # Default, compatible with webapp2.RequestHandler. - adapter = Webapp2HandlerAdapter + # Default, compatible with webapp2.RequestHandler. + adapter = Webapp2HandlerAdapter + try: + # There is an import cycle between webapp and webapp2. + # We import webapp lazily so the cycle doesn't cause problems + # during module initialization. + from google.appengine.ext import webapp as _webapp + if issubclass(handler, _webapp.RequestHandler): + # Compatible with webapp.RequestHandler. + adapter = WebappHandlerAdapter + except ImportError: # pragma: no cover + pass else: # A "view" function. adapter = BaseHandlerAdapter @@ -1659,12 +1656,16 @@ def run(self, bare=False): If True, doesn't add registered WSGI middleware: use ``run_bare_wsgi_app`` instead of ``run_wsgi_app``. """ - if _webapp_util: + try: + # There is an import cycle between webapp and webapp2. + # We import webapp lazily so the cycle doesn't cause problems + # during module initialization. + from google.appengine.ext.webapp import util as _webapp_util if bare: _webapp_util.run_bare_wsgi_app(self) else: _webapp_util.run_wsgi_app(self) - else: # pragma: no cover + except ImportError: # pragma: no cover handlers.CGIHandler().run(self) def get_response(self, *args, **kwargs): @@ -2053,11 +2054,3 @@ def _set_thread_safe_app(): _abort = abort # Thread-safety support. _set_thread_safe_app() - -# Defer importing google.appengine.ext.webapp.util until every public symbol -# has been defined since google.appengine.ext.webapp in App Engine Python 2.7 -# runtime imports this module to provide its public interface. -try: - from google.appengine.ext.webapp import util as _webapp_util -except ImportError: # pragma: no cover - pass