Deal with partially setup packages in the redirect hook.
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 ee9b401 · Browse files · 6 files·+49 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+49 added−7 removed
- other:+49 added−7 removed
Provenance
- Commit
9691b7f- Tree
8d4b9d1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ee9b401
6 files changed+49 added−7 removed
modified:flask/ext/__init__.py+11 added
55 unmodified lines56 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]35 unmodified linesadded:flask/testsuite/test_apps/flask_broken/__init__.py+2 added
1+import flask.ext.broken.b2+import missing_moduleadded:flask/testsuite/test_apps/flask_broken/b.py
modified:flask/testsuite/__init__.py+26 added
132 unmodified lines133 134 def assert_equal(self, x, y):135 return self.assertEqual(x, y)136 137+ def assert_raises(self, exc_type, callable=None, *args, **kwargs):138+ catcher = _ExceptionCatcher(self, exc_type)139+ if callable is None:140+ return catcher141+ with catcher:142+ callable(*args, **kwargs)143 144+145+class _ExceptionCatcher(object):146+147+ def __init__(self, test_case, exc_type):148+ self.test_case = test_case149+ self.exc_type = exc_type150+151+ def __enter__(self):152+ return self153+154+ def __exit__(self, exc_type, exc_value, tb):155+ exception_name = self.exc_type.__name__156+ if exc_type is None:157+ self.test_case.fail('Expected exception of type %r' %158+ exception_name)159+ elif not issubclass(exc_type, self.exc_type):160+ raise exc_type, exc_value, tb161+ return True162+163+164 class BetterLoader(unittest.TestLoader):165 """A nicer loader that solves two problems. First of all we are setting166 up tests from different sources and we're doing this programmatically167 which breaks the default loading logic so this is required anyways.56 unmodified linesmodified:flask/testsuite/ext.py+7 added
6 unmodified lines7 8 :copyright: (c) 2011 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11+from __future__ import with_statement12+13 import sys14 import unittest15 from flask.testsuite import FlaskTestCase16 76 unmodified lines93 def test_flaskext_old_package_import_submodule_function(self):94 from flask.ext.oldext_package.submodule import test_function95 self.assert_equal(test_function(), 42)96 97+ def test_flaskext_broken_package_no_module_caching(self):98+ for x in xrange(2):99+ with self.assert_raises(ImportError):100+ import flask.ext.broken101 102+103 def suite():104 suite = unittest.TestSuite()105 suite.addTest(unittest.makeSuite(ExtImportHookTestCase))106 return suitemodified:scripts/flaskext_compat.py+3 added-7 removed
23 unmodified lines24 25 26 class _ExtensionImporter(object):27 """This importer redirects imports from the flask.ext module to other28- locations.28+ 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('.')11 unmodified lines45 try:46 __import__(realname)47 except ImportError:48 exc_type, exc_value, tb = sys.exc_info()49+ sys.modules.pop(fullname, None)50 if self.is_important_traceback(realname, tb):51 raise exc_type, exc_value, tb52 continue53 module = sys.modules[fullname] = sys.modules[realname]2 unmodified lines56 return module57 raise ImportError('No module named %s' % fullname)58 59 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- """60 while tb is not None:61 if tb.tb_frame.f_globals.get('__name__') == important_module:62 return True63 tb = tb.tb_next10 unmodified lines