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 fileTake it
Added the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167
lines 1–11why, one step back1# -*- 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_compatMerge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption2e816f5
line 12why, one step back12 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 back13 from flask.ext import foo1415 :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 back18import sysRefactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
line 19why, one step back19import osExplicitly activate compat moduleArmin Ronacherbefore adoption7997bae
line 20why, one step back20import impAdded the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167
lines 21–22why, one step back2122Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 23–27why, one step back23class 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 back28Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 29–33why, one step back29 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('.') + 1Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae
line 34why, one step back34Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 35–39why, one step back35 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_choicesAdded the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167
line 40why, one step back40Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 41–46why, one step back41 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]46Merge pull request #583 from d1ffuz0r/issue_524Armin Ronacherbefore adoptionc5f938b
line 47why, one step back47 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 back48 if fullname.startswith(self.prefix):49 return self5051 def load_module(self, fullname):Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae
lines 52–53why, one step back52 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 back54 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 back55 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 back56 realname = path % modname57 try:58 __import__(realname)59 except ImportError:No longer swallow important import errors.Armin Ronacherbefore adoption9a70e62
line 60why, one step back60 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 back61 # since we only establish the entry in sys.modules at theMerge pull request #489 from florentx/patch-1Kenneth Reitzbefore adoption2a6c80a
line 62why, one step back62 # end this seems to be redundant, but if recursive importsRefactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 63–70why, one step back63 # 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 back71 sys.modules.pop(fullname, None)Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 72–74why, one step back7273 # If it's an important traceback we reraise it, otherwise74 # we swallow it and try the next choice. The skipped frameMerge pull request #489 from florentx/patch-1Kenneth Reitzbefore adoption2a6c80a
line 75why, one step back75 # 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 back76 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 back77 raise exc_type, exc_value, tb.tb_nextAdded the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167
line 78why, one step back78 continueExplicitly activate compat moduleArmin Ronacherbefore adoption7997bae
line 79why, one step back79 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 back80 if '.' not in modname:Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
line 81why, one step back81 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 back82 return moduleNo longer swallow important import errors.Armin Ronacherbefore adoption9a70e62
lines 83–85why, one step back83 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 back86 """Walks a traceback's frames and checks if any of the frames87 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 back91 while tb is not None:Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
line 92why, one step back92 if self.is_important_frame(important_module, tb):No longer swallow important import errors.Armin Ronacherbefore adoption9a70e62
lines 93–95why, one step back93 return True94 tb = tb.tb_next95 return FalseAdded the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167
line 96why, one step back96Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 97–101why, one step back97 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 FalseAdded the flaskext_compat module to support flask.ext in 0.7 and earlier.Armin Ronacherbefore adoptione944167
line 102why, one step back102Refactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 103–109why, one step back103 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 True109Merge pull request #489 from florentx/patch-1Kenneth Reitzbefore adoption2a6c80a
line 110why, one step back110 # Some python versions will clean up modules so early that theRefactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 111–118why, one step back111 # module name at that point is no longer set. Try guessing from112 # 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 filename117118Explicitly activate compat moduleArmin Ronacherbefore adoption7997bae
lines 119–120why, one step back119def activate():120 import flaskRefactored flask.ext process to not swallow exceptions on weird Pythons.Armin Ronacherbefore adoption95c4dcb
lines 121–125why, one step back121 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()