exthook.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # -*- coding: utf-8 -*-
  2. """
  3. flask.exthook
  4. ~~~~~~~~~~~~~
  5. Redirect imports for extensions. This module basically makes it possible
  6. for us to transition from flaskext.foo to flask_foo without having to
  7. force all extensions to upgrade at the same time.
  8. When a user does ``from flask.ext.foo import bar`` it will attempt to
  9. import ``from flask_foo import bar`` first and when that fails it will
  10. try to import ``from flaskext.foo import bar``.
  11. We're switching from namespace packages because it was just too painful for
  12. everybody involved.
  13. This is used by `flask.ext`.
  14. :copyright: (c) 2015 by Armin Ronacher.
  15. :license: BSD, see LICENSE for more details.
  16. """
  17. import sys
  18. import os
  19. import warnings
  20. from ._compat import reraise
  21. class ExtDeprecationWarning(DeprecationWarning):
  22. pass
  23. warnings.simplefilter('always', ExtDeprecationWarning)
  24. class ExtensionImporter(object):
  25. """This importer redirects imports from this submodule to other locations.
  26. This makes it possible to transition from the old flaskext.name to the
  27. newer flask_name without people having a hard time.
  28. """
  29. def __init__(self, module_choices, wrapper_module):
  30. self.module_choices = module_choices
  31. self.wrapper_module = wrapper_module
  32. self.prefix = wrapper_module + '.'
  33. self.prefix_cutoff = wrapper_module.count('.') + 1
  34. def __eq__(self, other):
  35. return self.__class__.__module__ == other.__class__.__module__ and \
  36. self.__class__.__name__ == other.__class__.__name__ and \
  37. self.wrapper_module == other.wrapper_module and \
  38. self.module_choices == other.module_choices
  39. def __ne__(self, other):
  40. return not self.__eq__(other)
  41. def install(self):
  42. sys.meta_path[:] = [x for x in sys.meta_path if self != x] + [self]
  43. def find_module(self, fullname, path=None):
  44. if fullname.startswith(self.prefix) and \
  45. fullname != 'flask.ext.ExtDeprecationWarning':
  46. return self
  47. def load_module(self, fullname):
  48. if fullname in sys.modules:
  49. return sys.modules[fullname]
  50. modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]
  51. warnings.warn(
  52. "Importing flask.ext.{x} is deprecated, use flask_{x} instead."
  53. .format(x=modname), ExtDeprecationWarning
  54. )
  55. for path in self.module_choices:
  56. realname = path % modname
  57. try:
  58. __import__(realname)
  59. except ImportError:
  60. exc_type, exc_value, tb = sys.exc_info()
  61. # since we only establish the entry in sys.modules at the
  62. # very this seems to be redundant, but if recursive imports
  63. # happen we will call into the move import a second time.
  64. # On the second invocation we still don't have an entry for
  65. # fullname in sys.modules, but we will end up with the same
  66. # fake module name and that import will succeed since this
  67. # one already has a temporary entry in the modules dict.
  68. # Since this one "succeeded" temporarily that second
  69. # invocation now will have created a fullname entry in
  70. # sys.modules which we have to kill.
  71. sys.modules.pop(fullname, None)
  72. # If it's an important traceback we reraise it, otherwise
  73. # we swallow it and try the next choice. The skipped frame
  74. # is the one from __import__ above which we don't care about
  75. if self.is_important_traceback(realname, tb):
  76. reraise(exc_type, exc_value, tb.tb_next)
  77. continue
  78. module = sys.modules[fullname] = sys.modules[realname]
  79. if '.' not in modname:
  80. setattr(sys.modules[self.wrapper_module], modname, module)
  81. if realname.startswith('flaskext.'):
  82. warnings.warn(
  83. "Detected extension named flaskext.{x}, please rename it "
  84. "to flask_{x}. The old form is deprecated."
  85. .format(x=modname), ExtDeprecationWarning
  86. )
  87. return module
  88. raise ImportError('No module named %s' % fullname)
  89. def is_important_traceback(self, important_module, tb):
  90. """Walks a traceback's frames and checks if any of the frames
  91. originated in the given important module. If that is the case then we
  92. were able to import the module itself but apparently something went
  93. wrong when the module was imported. (Eg: import of an import failed).
  94. """
  95. while tb is not None:
  96. if self.is_important_frame(important_module, tb):
  97. return True
  98. tb = tb.tb_next
  99. return False
  100. def is_important_frame(self, important_module, tb):
  101. """Checks a single frame if it's important."""
  102. g = tb.tb_frame.f_globals
  103. if '__name__' not in g:
  104. return False
  105. module_name = g['__name__']
  106. # Python 2.7 Behavior. Modules are cleaned up late so the
  107. # name shows up properly here. Success!
  108. if module_name == important_module:
  109. return True
  110. # Some python versions will clean up modules so early that the
  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 filename