Skip to content
Myr

flaskext_compat.py

This file

Path

scripts/flaskext_compat.py

125 lines · 4.9 KiB · Python

Last changed by

Merge pull request #583 from d1ffuz0r/issue_524Armin Ronacherbefore adoptionc5f938b

History of this file

Take it

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

lines 1–11why, one step back
1# -*- coding: utf-8 -*-
2"""3 flaskext_compat4 ~~~~~~~~~~~~~~~56 Implements the ``flask.ext`` virtual package for versions of Flask7 older than 0.7. This module is a noop if Flask 0.8 was detected.89 Usage::1011 import flaskext_compat

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption2e816f5

line 12why, one step back
12 flaskext_compat.activate()

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

lines 13–17why, one step back
13 from flask.ext import foo
1415 :copyright: (c) 2011 by Armin Ronacher.16 :license: BSD, see LICENSE for more details.17"""

Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae

line 18why, one step back
18import sys

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

line 19why, one step back
19import os

Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae

line 20why, one step back
20import imp

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

lines 21–22why, one step back
21
22

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 23–27why, one step back
23class ExtensionImporter(object):
24 """This importer redirects imports from this submodule to other locations.25 This makes it possible to transition from the old flaskext.name to the26 newer flask_name without people having a hard time.27 """

Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae

line 28why, one step back
28

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 29–33why, one step back
29 def __init__(self, module_choices, wrapper_module):
30 self.module_choices = module_choices31 self.wrapper_module = wrapper_module32 self.prefix = wrapper_module + '.'33 self.prefix_cutoff = wrapper_module.count('.') + 1

Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae

line 34why, one step back
34

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 35–39why, one step back
35 def __eq__(self, other):
36 return self.__class__.__module__ == other.__class__.__module__ and \37 self.__class__.__name__ == other.__class__.__name__ and \38 self.wrapper_module == other.wrapper_module and \39 self.module_choices == other.module_choices

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

line 40why, one step back
40

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 41–46why, one step back
41 def __ne__(self, other):
42 return not self.__eq__(other)4344 def install(self):45 sys.meta_path[:] = [x for x in sys.meta_path if self != x] + [self]46

Merge pull request #583 from d1ffuz0r/issue_524Armin Ronacherbefore adoptionc5f938b

line 47why, one step back
47 def find_module(self, fullname):

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

lines 48–51why, one step back
48 if fullname.startswith(self.prefix):
49 return self5051 def load_module(self, fullname):

Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae

lines 52–53why, one step back
52 if fullname in sys.modules:
53 return sys.modules[fullname]

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

line 54why, one step back
54 modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

line 55why, one step back
55 for path in self.module_choices:

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

lines 56–59why, one step back
56 realname = path % modname
57 try:58 __import__(realname)59 except ImportError:

No longer swallow important import errors.Armin Ronacherbefore adoption9a70e62

line 60why, one step back
60 exc_type, exc_value, tb = sys.exc_info()

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

line 61why, one step back
61 # since we only establish the entry in sys.modules at the

Merge pull request #489 from florentx/patch-1Kenneth Reitzbefore adoption2a6c80a

line 62why, one step back
62 # end this seems to be redundant, but if recursive imports

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 63–70why, one step back
63 # happen we will call into the move import a second time.
64 # On the second invocation we still don't have an entry for65 # fullname in sys.modules, but we will end up with the same66 # fake module name and that import will succeed since this67 # one already has a temporary entry in the modules dict.68 # Since this one "succeeded" temporarily that second69 # invocation now will have created a fullname entry in70 # sys.modules which we have to kill.

Deal with partially setup packages in the redirect hook.Armin Ronacherbefore adoption9691b7f

line 71why, one step back
71 sys.modules.pop(fullname, None)

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 72–74why, one step back
72
73 # If it's an important traceback we reraise it, otherwise74 # we swallow it and try the next choice. The skipped frame

Merge pull request #489 from florentx/patch-1Kenneth Reitzbefore adoption2a6c80a

line 75why, one step back
75 # is the one from __import__ above which we don't care about.

No longer swallow important import errors.Armin Ronacherbefore adoption9a70e62

line 76why, one step back
76 if self.is_important_traceback(realname, tb):

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

line 77why, one step back
77 raise exc_type, exc_value, tb.tb_next

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

line 78why, one step back
78 continue

Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae

line 79why, one step back
79 module = sys.modules[fullname] = sys.modules[realname]

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

line 80why, one step back
80 if '.' not in modname:

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

line 81why, one step back
81 setattr(sys.modules[self.wrapper_module], modname, module)

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

line 82why, one step back
82 return module

No longer swallow important import errors.Armin Ronacherbefore adoption9a70e62

lines 83–85why, one step back
83 raise ImportError('No module named %s' % fullname)
8485 def is_important_traceback(self, important_module, tb):

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 86–90why, one step back
86 """Walks a traceback's frames and checks if any of the frames
87 originated in the given important module. If that is the case then we88 were able to import the module itself but apparently something went89 wrong when the module was imported. (Eg: import of an import failed).90 """

No longer swallow important import errors.Armin Ronacherbefore adoption9a70e62

line 91why, one step back
91 while tb is not None:

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

line 92why, one step back
92 if self.is_important_frame(important_module, tb):

No longer swallow important import errors.Armin Ronacherbefore adoption9a70e62

lines 93–95why, one step back
93 return True
94 tb = tb.tb_next95 return False

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

line 96why, one step back
96

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 97–101why, one step back
97 def is_important_frame(self, important_module, tb):
98 """Checks a single frame if it's important."""99 g = tb.tb_frame.f_globals100 if '__name__' not in g:101 return False

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167

line 102why, one step back
102

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 103–109why, one step back
103 module_name = g['__name__']
104105 # Python 2.7 Behavior. Modules are cleaned up late so the106 # name shows up properly here. Success!107 if module_name == important_module:108 return True109

Merge pull request #489 from florentx/patch-1Kenneth Reitzbefore adoption2a6c80a

line 110why, one step back
110 # Some python versions will clean up modules so early that the

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 111–118why, one step back
111 # module name at that point is no longer set. Try guessing from
112 # the filename then.113 filename = os.path.abspath(tb.tb_frame.f_code.co_filename)114 test_string = os.path.sep + important_module.replace('.', os.path.sep)115 return test_string + '.py' in filename or \116 test_string + os.path.sep + '__init__.py' in filename117118

Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae

lines 119–120why, one step back
119def activate():
120 import flask

Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb

lines 121–125why, one step back
121 ext_module = imp.new_module('flask.ext')
122 ext_module.__path__ = []123 flask.ext = sys.modules['flask.ext'] = ext_module124 importer = ExtensionImporter(['flask_%s', 'flaskext.%s'], 'flask.ext')125 importer.install()