Refactored flask.ext process to not swallow exceptions on weird Pythons.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent f01b654 · Browse files · 4 files·+210 added−108 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+210 added−108 removed
- other:+210 added−108 removed
Provenance
- Commit
95c4dcb- Tree
155f5f1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f01b654
4 files changed+210 added−108 removed
modified:flask/ext/__init__.py+6 added-86 removed
17 unmodified lines18 :license: BSD, see LICENSE for more details.19 """20 21 22-class _ExtensionImporter(object):23- """This importer redirects imports from this submodule to other locations.24- This makes it possible to transition from the old flaskext.name to the25- newer flask_name without people having a hard time.26- """27- _module_choices = ['flask_%s', 'flaskext.%s']22+def setup():23+ from ..exthook import ExtensionImporter24+ importer = ExtensionImporter(['flask_%s', 'flaskext.%s'], __name__)25+ importer.install()26 29- def __init__(self):30- from sys import meta_path31- self.prefix = __name__ + '.'32- self.prefix_cutoff = __name__.count('.') + 127 34- # since people might reload the flask.ext module (by accident or35- # intentionally) we have to make sure to not add more than one36- # import hook. We can't check class types here either since a new37- # class will be created on reload. As a result of that we check38- # the name of the class and remove stale instances.39- def _name(x):40- cls = type(x)41- return cls.__module__ + '.' + cls.__name__42- this = _name(self)43- meta_path[:] = [x for x in meta_path if _name(x) != this] + [self]44-45- def find_module(self, fullname, path=None):46- if fullname.startswith(self.prefix):47- return self48-49- def load_module(self, fullname):50- from sys import modules, exc_info51- if fullname in modules:52- return modules[fullname]53- modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]54- for path in self._module_choices:55- realname = path % modname56- try:57- __import__(realname)58- except ImportError:59- exc_type, exc_value, tb = exc_info()60- # since we only establish the entry in sys.modules at the61- # very this seems to be redundant, but if recursive imports62- # happen we will call into the move import a second time.63- # On the second invocation we still don't have an entry for64- # fullname in sys.modules, but we will end up with the same65- # fake module name and that import will succeed since this66- # one already has a temporary entry in the modules dict.67- # Since this one "succeeded" temporarily that second68- # invocation now will have created a fullname entry in69- # sys.modules which we have to kill.70- modules.pop(fullname, None)71- if self.is_important_traceback(realname, tb):72- raise exc_type, exc_value, tb73- continue74- module = modules[fullname] = modules[realname]75- if '.' not in modname:76- setattr(modules[__name__], modname, module)77- return module78- raise ImportError('No module named %s' % fullname)79-80- def is_important_traceback(self, important_module, tb):81- """Walks a traceback's frames and checks if any of the frames82- originated in the given important module. If that is the case83- then we were able to import the module itself but apparently84- something went wrong when the module was imported. (Eg: import85- of an import failed).86- """87- # Why can we access f_globals' __name__ here and the value is88- # not None? I honestly don't know but here is my thinking.89- # The module owns a reference to globals and the frame has one.90- # Each function only keeps a reference to the globals not do the91- # module which normally causes the problem that when the module92- # shuts down all globals are set to None. Now however when the93- # import system fails Python takes the short way out and does not94- # actually properly shut down the module by Noneing the values95- # but by just removing the entry from sys.modules. This means96- # that the regular reference based cleanup kicks in.97- #98- # The good thing: At worst we will swallow an exception we should99- # not and the error message will be messed up. However I think100- # this should be sufficiently reliable.101- while tb is not None:102- if tb.tb_frame.f_globals.get('__name__') == important_module:103- return True104- tb = tb.tb_next105- return False106-107-108-_ExtensionImporter()109-del _ExtensionImporter28+setup()29+del setupmodified:flask/testsuite/ext.py+14 added-2 removed
33 unmodified lines34 # reloading must not add more hooks35 import_hooks = 036 for item in sys.meta_path:37 cls = type(item)38- if cls.__module__ == 'flask.ext' and \39- cls.__name__ == '_ExtensionImporter':38+ if cls.__module__ == 'flask.exthook' and \39+ cls.__name__ == 'ExtensionImporter':40 import_hooks += 141 self.assert_equal(import_hooks, 1)42 43 def teardown(self):59 unmodified lines103 for x in xrange(2):104 with self.assert_raises(ImportError):105 import flask.ext.broken106 107+ def test_no_error_swallowing(self):108+ try:109+ import flask.ext.broken110+ except ImportError:111+ exc_type, exc_value, tb = sys.exc_info()112+ self.assert_(exc_type is ImportError)113+ self.assert_equal(str(exc_value), 'No module named missing_module')114+ self.assert_(tb.tb_frame.f_globals is globals())115 116+ next = tb.tb_next117+ self.assert_('flask_broken/__init__.py' in next.tb_frame.f_code.co_filename)118+119+120 def suite():121 suite = unittest.TestSuite()122 suite.addTest(unittest.makeSuite(ExtImportHookTestCase))123 return suiteadded:flask/exthook.py+119 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.exthook4+ ~~~~~~~~~~~~~5+6+ Redirect imports for extensions. This module basically makes it possible7+ for us to transition from flaskext.foo to flask_foo without having to8+ force all extensions to upgrade at the same time.9+10+ When a user does ``from flask.ext.foo import bar`` it will attempt to11+ import ``from flask_foo import bar`` first and when that fails it will12+ try to import ``from flaskext.foo import bar``.13+14+ We're switching from namespace packages because it was just too painful for15+ everybody involved.16+17+ This is used by `flask.ext`.18+19+ :copyright: (c) 2011 by Armin Ronacher.20+ :license: BSD, see LICENSE for more details.21+"""22+import sys23+import os24+25+26+class ExtensionImporter(object):27+ """This importer redirects imports from this submodule to other locations.28+ This makes it possible to transition from the old flaskext.name to the29+ newer flask_name without people having a hard time.30+ """31+32+ def __init__(self, module_choices, wrapper_module):33+ self.module_choices = module_choices34+ self.wrapper_module = wrapper_module35+ self.prefix = wrapper_module + '.'36+ self.prefix_cutoff = wrapper_module.count('.') + 137+38+ def __eq__(self, other):39+ return self.__class__.__module__ == other.__class__.__module__ and \40+ self.__class__.__name__ == other.__class__.__name__ and \41+ self.wrapper_module == other.wrapper_module and \42+ self.module_choices == other.module_choices43+44+ def __ne__(self, other):45+ return not self.__eq__(other)46+47+ def install(self):48+ sys.meta_path[:] = [x for x in sys.meta_path if self != x] + [self]49+50+ def find_module(self, fullname, path=None):51+ if fullname.startswith(self.prefix):52+ return self53+54+ def load_module(self, fullname):55+ if fullname in sys.modules:56+ return sys.modules[fullname]57+ modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]58+ for path in self.module_choices:59+ realname = path % modname60+ try:61+ __import__(realname)62+ except ImportError:63+ exc_type, exc_value, tb = sys.exc_info()64+ # since we only establish the entry in sys.modules at the65+ # very this seems to be redundant, but if recursive imports66+ # happen we will call into the move import a second time.67+ # On the second invocation we still don't have an entry for68+ # fullname in sys.modules, but we will end up with the same69+ # fake module name and that import will succeed since this70+ # one already has a temporary entry in the modules dict.71+ # Since this one "succeeded" temporarily that second72+ # invocation now will have created a fullname entry in73+ # sys.modules which we have to kill.74+ sys.modules.pop(fullname, None)75+76+ # If it's an important traceback we reraise it, otherwise77+ # we swallow it and try the next choice. The skipped frame78+ # is the one from __import__ above which we don't care about79+ if self.is_important_traceback(realname, tb):80+ raise exc_type, exc_value, tb.tb_next81+ continue82+ module = sys.modules[fullname] = sys.modules[realname]83+ if '.' not in modname:84+ setattr(sys.modules[self.wrapper_module], modname, module)85+ return module86+ raise ImportError('No module named %s' % fullname)87+88+ def is_important_traceback(self, important_module, tb):89+ """Walks a traceback's frames and checks if any of the frames90+ originated in the given important module. If that is the case then we91+ were able to import the module itself but apparently something went92+ wrong when the module was imported. (Eg: import of an import failed).93+ """94+ while tb is not None:95+ if self.is_important_frame(important_module, tb):96+ return True97+ tb = tb.tb_next98+ return False99+100+ def is_important_frame(self, important_module, tb):101+ """Checks a single frame if it's important."""102+ g = tb.tb_frame.f_globals103+ if '__name__' not in g:104+ return False105+106+ module_name = g['__name__']107+108+ # Python 2.7 Behavior. Modules are cleaned up late so the109+ # name shows up properly here. Success!110+ if module_name == important_module:111+ return True112+113+ # Some python verisons will will clean up modules so early that the114+ # module name at that point is no longer set. Try guessing from115+ # the filename then.116+ filename = os.path.abspath(tb.tb_frame.f_code.co_filename)117+ test_string = os.path.sep + important_module.replace('.', os.path.sep)118+ return test_string + '.py' in filename or \119+ test_string + os.path.sep + '__init__.py' in filenamemodified:scripts/flaskext_compat.py+71 added-20 removed
13 unmodified lines14 :copyright: (c) 2011 by Armin Ronacher.15 :license: BSD, see LICENSE for more details.16 """17 import sys18+import os19 import imp20 21 21-ext_module = imp.new_module('flask.ext')22-ext_module.__path__ = []23-ext_module.__package__ = ext_module.__name__22+class ExtensionImporter(object):23+ """This importer redirects imports from this submodule to other locations.24+ This makes it possible to transition from the old flaskext.name to the25+ newer flask_name without people having a hard time.26+ """27 28+ def __init__(self, module_choices, wrapper_module):29+ self.module_choices = module_choices30+ self.wrapper_module = wrapper_module31+ self.prefix = wrapper_module + '.'32+ self.prefix_cutoff = wrapper_module.count('.') + 133 26-class _ExtensionImporter(object):27- """This importer redirects imports from the flask.ext module to other28- locations. For implementation details see the code in Flask 0.829- that does the same.30- """31- _module_choices = ['flask_%s', 'flaskext.%s']32- prefix = ext_module.__name__ + '.'33- prefix_cutoff = prefix.count('.')34+ def __eq__(self, other):35+ return self.__class__.__module__ == other.__class__.__module__ and \36+ self.__class__.__name__ == other.__class__.__name__ and \37+ self.wrapper_module == other.wrapper_module and \38+ self.module_choices == other.module_choices39 40+ def __ne__(self, other):41+ return not self.__eq__(other)42+43+ def install(self):44+ sys.meta_path[:] = [x for x in sys.meta_path if self != x] + [self]45+46 def find_module(self, fullname, path=None):47 if fullname.startswith(self.prefix):48 return self49 50 def load_module(self, fullname):51 if fullname in sys.modules:52 return sys.modules[fullname]53 modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]43- for path in self._module_choices:54+ for path in self.module_choices:55 realname = path % modname56 try:57 __import__(realname)58 except ImportError:59 exc_type, exc_value, tb = sys.exc_info()60+ # since we only establish the entry in sys.modules at the61+ # very this seems to be redundant, but if recursive imports62+ # happen we will call into the move import a second time.63+ # On the second invocation we still don't have an entry for64+ # fullname in sys.modules, but we will end up with the same65+ # fake module name and that import will succeed since this66+ # one already has a temporary entry in the modules dict.67+ # Since this one "succeeded" temporarily that second68+ # invocation now will have created a fullname entry in69+ # sys.modules which we have to kill.70 sys.modules.pop(fullname, None)71+72+ # If it's an important traceback we reraise it, otherwise73+ # we swallow it and try the next choice. The skipped frame74+ # is the one from __import__ above which we don't care about75 if self.is_important_traceback(realname, tb):51- raise exc_type, exc_value, tb76+ raise exc_type, exc_value, tb.tb_next77 continue78 module = sys.modules[fullname] = sys.modules[realname]79 if '.' not in modname:55- setattr(ext_module, modname, module)80+ setattr(sys.modules[self.wrapper_module], modname, module)81 return module82 raise ImportError('No module named %s' % fullname)83 84 def is_important_traceback(self, important_module, tb):85+ """Walks a traceback's frames and checks if any of the frames86+ originated in the given important module. If that is the case then we87+ were able to import the module itself but apparently something went88+ wrong when the module was imported. (Eg: import of an import failed).89+ """90 while tb is not None:61- if tb.tb_frame.f_globals.get('__name__') == important_module:91+ if self.is_important_frame(important_module, tb):92 return True93 tb = tb.tb_next94 return False95 96+ def is_important_frame(self, important_module, tb):97+ """Checks a single frame if it's important."""98+ g = tb.tb_frame.f_globals99+ if '__name__' not in g:100+ return False101 102+ module_name = g['__name__']103+104+ # Python 2.7 Behavior. Modules are cleaned up late so the105+ # name shows up properly here. Success!106+ if module_name == important_module:107+ return True108+109+ # Some python verisons will will clean up modules so early that the110+ # module name at that point is no longer set. Try guessing from111+ # the filename then.112+ filename = os.path.abspath(tb.tb_frame.f_code.co_filename)113+ test_string = os.path.sep + important_module.replace('.', os.path.sep)114+ return test_string + '.py' in filename or \115+ test_string + os.path.sep + '__init__.py' in filename116+117+118 def activate():68- """Activates the compatibility system."""119 import flask70- if hasattr(flask, 'ext'):71- return72- sys.modules['flask.ext'] = flask.ext = ext_module73- sys.meta_path.append(_ExtensionImporter())120+ ext_module = imp.new_module('flask.ext')121+ ext_module.__path__ = []122+ flask.ext = sys.modules['flask.ext'] = ext_module123+ importer = ExtensionImporter(['flask_%s', 'flaskext.%s'], 'flask.ext')124+ importer.install()