Skip to content
Myr

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

5 files+89 added−16 removed

modified:flask/ext/__init__.py+24 added-16 removed
6 unmodified lines
7 for us to transition from flaskext.foo to flask_foo without having to8 force all extensions to upgrade at the same time.910 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``.1314 We're switching from namespace packages because it was just too painful for15 everybody involved.1617 :copyright: (c) 2011 by Armin Ronacher.18 :license: BSD, see LICENSE for more details.19"""20import sys202122class _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.modules2829 def __init__(self):30 from sys import meta_path31 self.prefix = __name__ + '.'32 self.prefix_cutoff = __name__.count('.') + 13334 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]3940 def find_module(self, fullname, path=None):33 if fullname.rsplit('.', 1)[0] == __name__:41 if fullname.startswith(self.prefix):42 return self4344 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)596053sys.meta_path.append(_ExtensionImporter())54del sys, _ExtensionImporter61_ExtensionImporter()62del _ExtensionImporter
added:flask/testsuite/test_apps/flask_newext_package/__init__.py+1 added
1ext_id = 'newext_package'
added:flask/testsuite/test_apps/flask_newext_package/submodule.py+2 added
1def test_function():2 return 42
added:flask/testsuite/test_apps/flask_newext_simple.py+1 added
1ext_id = 'newext_simple'
added:flask/testsuite/ext.py+61 added
1# -*- coding: utf-8 -*-2"""3 flask.testsuite.ext4 ~~~~~~~~~~~~~~~~~~~56 Tests the extension import thing.78 :copyright: (c) 2011 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11import sys12import unittest13from flask.testsuite import FlaskTestCase141516class ExtImportHookTestCase(FlaskTestCase):1718 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)2425 # 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)3334 def test_flaskext_simple_import_normal(self):35 from flask.ext.newext_simple import ext_id36 self.assert_equal(ext_id, 'newext_simple')3738 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')4243 def test_flaskext_package_import_normal(self):44 from flask.ext.newext_package import ext_id45 self.assert_equal(ext_id, 'newext_package')4647 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')5152 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)565758def suite():59 suite = unittest.TestSuite()60 suite.addTest(unittest.makeSuite(ExtImportHookTestCase))61 return suite