Skip to content
Myr

No longer swallow important import errors.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This implements a simple but elegant method to find out if an import error can be swallowed to try the next module or if the import error is important and must be reraised.

authored by Armin Ronacher · committed by Armin Ronacher · parent 4573715 · Browse files · 2 files+35 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+35 added−3 removed

  • other:+35 added−3 removed

2 files+35 added−3 removed

modified:flask/ext/__init__.py+18 added-2 removed
45 unmodified lines
46 if fullname.startswith(self.prefix):47 return self4849 def load_module(self, fullname):50 from sys import modules50 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 if self.is_important_traceback(realname, tb):61 raise exc_type, exc_value, tb62 continue63 module = modules[fullname] = modules[realname]64 if '.' not in modname:65 setattr(modules[__name__], modname, module)66 return module64 raise ImportError(fullname)67 raise ImportError('No module named %s' % fullname)6869 def is_important_traceback(self, important_module, tb):70 """Walks a traceback's frames and checks if any of the frames71 originated in the given important module. If that is the case72 then we were able to import the module itself but apparently73 something went wrong when the module was imported. (Eg: import74 of an import failed).75 """76 while tb is not None:77 if tb.tb_frame.f_globals.get('__name__') == important_module:78 return True79 tb = tb.tb_next80 return False818283_ExtensionImporter()84del _ExtensionImporter
modified:scripts/flaskext_compat.py+17 added-1 removed
42 unmodified lines
43 realname = path % modname44 try:45 __import__(realname)46 except ImportError:47 exc_type, exc_value, tb = sys.exc_info()48 if self.is_important_traceback(realname, tb):49 raise exc_type, exc_value, tb50 continue51 module = sys.modules[fullname] = sys.modules[realname]52 if '.' not in modname:53 setattr(ext_module, modname, module)54 return module52 raise ImportError(fullname)55 raise ImportError('No module named %s' % fullname)5657 def is_important_traceback(self, important_module, tb):58 """Walks a traceback's frames and checks if any of the frames59 originated in the given important module. If that is the case60 then we were able to import the module itself but apparently61 something went wrong when the module was imported. (Eg: import62 of an import failed).63 """64 while tb is not None:65 if tb.tb_frame.f_globals.get('__name__') == important_module:66 return True67 tb = tb.tb_next68 return False697071def activate():72 """Activates the compatibility system."""73 import flask74 if hasattr(flask, 'ext'):
3 unmodified lines