Added tests for the import hook and fixed a problem with it.
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 8f85a3b · Browse files · 5 files·+89 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+89 added−16 removed
- other:+89 added−16 removed
Provenance
- Commit
c72ca16- Tree
87cf984- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
8f85a3b
5 files changed+89 added−16 removed
modified:flask/ext/__init__.py+24 added-16 removed
6 unmodified lines7 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- imprt ``from flask_foo import bar`` first and when that fails it will11+ 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 :copyright: (c) 2011 by Armin Ronacher.18 :license: BSD, see LICENSE for more details.19 """20-import sys20 21 22 class _ExtensionImporter(object):24- """This importer redirects imports from this submodule to other25- locations. This makes it possible to transition from the old26- flaskext.name to the newer flask_name without people having a27- hard time.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']30- _modules = sys.modules28 29+ def __init__(self):30+ from sys import meta_path31+ self.prefix = __name__ + '.'32+ self.prefix_cutoff = __name__.count('.') + 133+34+ def _name(x):35+ cls = type(x)36+ return cls.__module__ + '.' + cls.__name__37+ this = _name(self)38+ meta_path[:] = [x for x in meta_path if _name(x) != this] + [self]39+40 def find_module(self, fullname, path=None):33- if fullname.rsplit('.', 1)[0] == __name__:41+ if fullname.startswith(self.prefix):42 return self43 44 def load_module(self, fullname):45+ from sys import modules37- if fullname in self._modules:38- return self._modules[fullname]39- packname, modname = fullname.rsplit('.', 1)46+ if fullname in modules:47+ return modules[fullname]48+ modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]49 for path in self._module_choices:50 realname = path % modname51 try:52 __import__(realname)53 except ImportError:54 continue46- module = self._modules[fullname] = self._modules[realname]47- setattr(self._modules[__name__], modname, module)48- module.__loader__ = self55+ module = modules[fullname] = modules[realname]56+ setattr(modules[__name__], modname, module)57 return module58 raise ImportError(fullname)59 60 53-sys.meta_path.append(_ExtensionImporter())54-del sys, _ExtensionImporter61+_ExtensionImporter()62+del _ExtensionImporteradded:flask/testsuite/test_apps/flask_newext_package/__init__.py+1 added
1+ext_id = 'newext_package'added:flask/testsuite/test_apps/flask_newext_package/submodule.py+2 added
1+def test_function():2+ return 42added:flask/testsuite/test_apps/flask_newext_simple.py+1 added
1+ext_id = 'newext_simple'added:flask/testsuite/ext.py+61 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.testsuite.ext4+ ~~~~~~~~~~~~~~~~~~~5+6+ Tests the extension import thing.7+8+ :copyright: (c) 2011 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+import sys12+import unittest13+from flask.testsuite import FlaskTestCase14+15+16+class ExtImportHookTestCase(FlaskTestCase):17+18+ def setup(self):19+ for entry, value in sys.modules.items():20+ if entry.startswith('flask.ext.') and value is not None:21+ sys.modules.pop(entry, None)22+ from flask import ext23+ reload(ext)24+25+ # reloading must not add more hooks26+ import_hooks = 027+ for item in sys.meta_path:28+ cls = type(item)29+ if cls.__module__ == 'flask.ext' and \30+ cls.__name__ == '_ExtensionImporter':31+ import_hooks += 132+ self.assert_equal(import_hooks, 1)33+34+ def test_flaskext_simple_import_normal(self):35+ from flask.ext.newext_simple import ext_id36+ self.assert_equal(ext_id, 'newext_simple')37+38+ def test_flaskext_simple_import_module(self):39+ from flask.ext import newext_simple40+ self.assert_equal(newext_simple.ext_id, 'newext_simple')41+ self.assert_equal(newext_simple.__name__, 'flask_newext_simple')42+43+ def test_flaskext_package_import_normal(self):44+ from flask.ext.newext_package import ext_id45+ self.assert_equal(ext_id, 'newext_package')46+47+ def test_flaskext_package_import_module(self):48+ from flask.ext import newext_package49+ self.assert_equal(newext_package.ext_id, 'newext_package')50+ self.assert_equal(newext_package.__name__, 'flask_newext_package')51+52+ def test_flaskext_package_import_submodule(self):53+ from flask.ext.newext_package import submodule54+ self.assert_equal(submodule.__name__, 'flask_newext_package.submodule')55+ self.assert_equal(submodule.test_function(), 42)56+57+58+def suite():59+ suite = unittest.TestSuite()60+ suite.addTest(unittest.makeSuite(ExtImportHookTestCase))61+ return suite