Skip to content

Commit 7997bae

Browse files
committed
Explicitly activate compat module
1 parent 9c82840 commit 7997bae

File tree

2 files changed

+27
-37
lines changed

2 files changed

+27
-37
lines changed

docs/extensions.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ depending on how the extension is distributed.
3333
We recommend importing from ``flask.ext`` even with older versions of
3434
Flask however. If you have an application that needs to work with
3535
versions of Flask older than 0.8 you should activate the
36-
``flaskext_compat`` module which provides the ``flask.ext`` module. You
37-
can download it from github: `flaskext_compat.py`_
36+
``flaskext_compat`` module which provides the ``flask.ext`` module if
37+
you activate it. You can download it from github: `flaskext_compat.py`_
3838

39-
You can use it like this::
39+
And here is how you can use it::
4040

4141
import flaskext_compat
42+
flaskext_compat.activate()
43+
4244
from flask.ext import foo
4345

4446
Once the ``flaskext_compat`` module is imported the :data:`flask.ext` will

scripts/flaskext_compat.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,48 @@
1414
:copyright: (c) 2011 by Armin Ronacher.
1515
:license: BSD, see LICENSE for more details.
1616
"""
17+
import sys
18+
import imp
19+
20+
21+
ext_module = imp.new_module('flask.ext')
22+
ext_module.__path__ = []
23+
ext_module.__package__ = ext_module.__name__
1724

1825

1926
class _ExtensionImporter(object):
20-
"""This importer redirects imports from this submodule to other locations.
21-
This makes it possible to transition from the old flaskext.name to the
22-
newer flask_name without people having a hard time.
27+
"""This importer redirects imports from the flask.ext module to other
28+
locations.
2329
"""
2430
_module_choices = ['flask_%s', 'flaskext.%s']
25-
26-
def __init__(self):
27-
from sys import meta_path
28-
self.prefix = __name__ + '.'
29-
self.prefix_cutoff = __name__.count('.') + 1
30-
31-
# since people might reload the flask.ext module (by accident or
32-
# intentionally) we have to make sure to not add more than one
33-
# import hook. We can't check class types here either since a new
34-
# class will be created on reload. As a result of that we check
35-
# the name of the class and remove stale instances.
36-
def _name(x):
37-
cls = type(x)
38-
return cls.__module__ + '.' + cls.__name__
39-
this = _name(self)
40-
meta_path[:] = [x for x in meta_path if _name(x) != this] + [self]
31+
prefix = ext_module.__name__ + '.'
32+
prefix_cutoff = prefix.count('.')
4133

4234
def find_module(self, fullname, path=None):
4335
if fullname.startswith(self.prefix):
4436
return self
4537

4638
def load_module(self, fullname):
47-
from sys import modules
48-
if fullname in modules:
49-
return modules[fullname]
39+
if fullname in sys.modules:
40+
return sys.modules[fullname]
5041
modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]
5142
for path in self._module_choices:
5243
realname = path % modname
5344
try:
5445
__import__(realname)
5546
except ImportError:
5647
continue
57-
module = modules[fullname] = modules[realname]
48+
module = sys.modules[fullname] = sys.modules[realname]
5849
if '.' not in modname:
59-
setattr(modules[__name__], modname, module)
50+
setattr(ext_module, modname, module)
6051
return module
6152
raise ImportError(fullname)
6253

6354

64-
import sys
65-
import flask
66-
try:
67-
__import__('flask.ext')
68-
except ImportError:
69-
sys.modules['flask.ext'] = flask.ext = sys.modules[__name__]
70-
__name__ = __package__ = 'flask.ext'
71-
__path__ = []
72-
_ExtensionImporter()
73-
del _ExtensionImporter, sys, flask
55+
def activate():
56+
"""Activates the compatibility system."""
57+
import flask
58+
if hasattr(flask, 'ext'):
59+
return
60+
sys.modules['flask.ext'] = flask.ext = ext_module
61+
sys.meta_path.append(_ExtensionImporter())

0 commit comments

Comments
 (0)