From 7997bae5bb048e4623647c57faae447b28da907f Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 19 Sep 2011 21:00:28 +0200 Subject: [PATCH] Explicitly activate compat module --- docs/extensions.rst | 8 ++++-- scripts/flaskext_compat.py | 56 +++++++++++++++----------------------- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/docs/extensions.rst b/docs/extensions.rst index 9d00468a..5ae80457 100644 --- a/docs/extensions.rst +++ b/docs/extensions.rst @@ -33,12 +33,14 @@ depending on how the extension is distributed. We recommend importing from ``flask.ext`` even with older versions of Flask however. If you have an application that needs to work with versions of Flask older than 0.8 you should activate the -``flaskext_compat`` module which provides the ``flask.ext`` module. You -can download it from github: `flaskext_compat.py`_ +``flaskext_compat`` module which provides the ``flask.ext`` module if +you activate it. You can download it from github: `flaskext_compat.py`_ -You can use it like this:: +And here is how you can use it:: import flaskext_compat + flaskext_compat.activate() + from flask.ext import foo Once the ``flaskext_compat`` module is imported the :data:`flask.ext` will diff --git a/scripts/flaskext_compat.py b/scripts/flaskext_compat.py index ddc7e097..03601a1a 100644 --- a/scripts/flaskext_compat.py +++ b/scripts/flaskext_compat.py @@ -14,39 +14,30 @@ :copyright: (c) 2011 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ +import sys +import imp + + +ext_module = imp.new_module('flask.ext') +ext_module.__path__ = [] +ext_module.__package__ = ext_module.__name__ class _ExtensionImporter(object): - """This importer redirects imports from this submodule to other locations. - This makes it possible to transition from the old flaskext.name to the - newer flask_name without people having a hard time. + """This importer redirects imports from the flask.ext module to other + locations. """ _module_choices = ['flask_%s', 'flaskext.%s'] - - def __init__(self): - from sys import meta_path - self.prefix = __name__ + '.' - self.prefix_cutoff = __name__.count('.') + 1 - - # since people might reload the flask.ext module (by accident or - # intentionally) we have to make sure to not add more than one - # import hook. We can't check class types here either since a new - # class will be created on reload. As a result of that we check - # the name of the class and remove stale instances. - def _name(x): - cls = type(x) - return cls.__module__ + '.' + cls.__name__ - this = _name(self) - meta_path[:] = [x for x in meta_path if _name(x) != this] + [self] + prefix = ext_module.__name__ + '.' + prefix_cutoff = prefix.count('.') def find_module(self, fullname, path=None): if fullname.startswith(self.prefix): return self def load_module(self, fullname): - from sys import modules - if fullname in modules: - return modules[fullname] + if fullname in sys.modules: + return sys.modules[fullname] modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff] for path in self._module_choices: realname = path % modname @@ -54,20 +45,17 @@ class _ExtensionImporter(object): __import__(realname) except ImportError: continue - module = modules[fullname] = modules[realname] + module = sys.modules[fullname] = sys.modules[realname] if '.' not in modname: - setattr(modules[__name__], modname, module) + setattr(ext_module, modname, module) return module raise ImportError(fullname) -import sys -import flask -try: - __import__('flask.ext') -except ImportError: - sys.modules['flask.ext'] = flask.ext = sys.modules[__name__] - __name__ = __package__ = 'flask.ext' - __path__ = [] - _ExtensionImporter() -del _ExtensionImporter, sys, flask +def activate(): + """Activates the compatibility system.""" + import flask + if hasattr(flask, 'ext'): + return + sys.modules['flask.ext'] = flask.ext = ext_module + sys.meta_path.append(_ExtensionImporter())