Skip to content
Myr

remove deprecated flask.ext

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 4c56477 · Browse files · 6 files+4 added−495 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+4 added−495 removed

  • tests:+0 added−197 removed
  • other:+4 added−298 removed

6 files+4 added−495 removed

deleted:flask/ext/__init__.py-29 removed
1# -*- coding: utf-8 -*-2"""3 flask.ext4 ~~~~~~~~~56 Redirect imports for extensions. This module basically makes it possible7 for us to transition from flaskext.foo to flask_foo without having to8 force all extensions to upgrade at the same time.910 When a user does ``from flask.ext.foo import bar`` it will attempt to11 import ``from flask_foo import bar`` first and when that fails it will12 try to import ``from flaskext.foo import bar``.1314 We're switching from namespace packages because it was just too painful for15 everybody involved.1617 :copyright: (c) 2015 by Armin Ronacher.18 :license: BSD, see LICENSE for more details.19"""202122def setup():23 from ..exthook import ExtensionImporter24 importer = ExtensionImporter(['flask_%s', 'flaskext.%s'], __name__)25 importer.install()262728setup()29del setup
deleted:flask/exthook.py-143 removed
1# -*- coding: utf-8 -*-2"""3 flask.exthook4 ~~~~~~~~~~~~~56 Redirect imports for extensions. This module basically makes it possible7 for us to transition from flaskext.foo to flask_foo without having to8 force all extensions to upgrade at the same time.910 When a user does ``from flask.ext.foo import bar`` it will attempt to11 import ``from flask_foo import bar`` first and when that fails it will12 try to import ``from flaskext.foo import bar``.1314 We're switching from namespace packages because it was just too painful for15 everybody involved.1617 This is used by `flask.ext`.1819 :copyright: (c) 2015 by Armin Ronacher.20 :license: BSD, see LICENSE for more details.21"""22import sys23import os24import warnings25from ._compat import reraise262728class ExtDeprecationWarning(DeprecationWarning):29 pass3031warnings.simplefilter('always', ExtDeprecationWarning)323334class ExtensionImporter(object):35 """This importer redirects imports from this submodule to other locations.36 This makes it possible to transition from the old flaskext.name to the37 newer flask_name without people having a hard time.38 """3940 def __init__(self, module_choices, wrapper_module):41 self.module_choices = module_choices42 self.wrapper_module = wrapper_module43 self.prefix = wrapper_module + '.'44 self.prefix_cutoff = wrapper_module.count('.') + 14546 def __eq__(self, other):47 return self.__class__.__module__ == other.__class__.__module__ and \48 self.__class__.__name__ == other.__class__.__name__ and \49 self.wrapper_module == other.wrapper_module and \50 self.module_choices == other.module_choices5152 def __ne__(self, other):53 return not self.__eq__(other)5455 def install(self):56 sys.meta_path[:] = [x for x in sys.meta_path if self != x] + [self]5758 def find_module(self, fullname, path=None):59 if fullname.startswith(self.prefix) and \60 fullname != 'flask.ext.ExtDeprecationWarning':61 return self6263 def load_module(self, fullname):64 if fullname in sys.modules:65 return sys.modules[fullname]6667 modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]6869 warnings.warn(70 "Importing flask.ext.{x} is deprecated, use flask_{x} instead."71 .format(x=modname), ExtDeprecationWarning, stacklevel=272 )7374 for path in self.module_choices:75 realname = path % modname76 try:77 __import__(realname)78 except ImportError:79 exc_type, exc_value, tb = sys.exc_info()80 # since we only establish the entry in sys.modules at the81 # very this seems to be redundant, but if recursive imports82 # happen we will call into the move import a second time.83 # On the second invocation we still don't have an entry for84 # fullname in sys.modules, but we will end up with the same85 # fake module name and that import will succeed since this86 # one already has a temporary entry in the modules dict.87 # Since this one "succeeded" temporarily that second88 # invocation now will have created a fullname entry in89 # sys.modules which we have to kill.90 sys.modules.pop(fullname, None)9192 # If it's an important traceback we reraise it, otherwise93 # we swallow it and try the next choice. The skipped frame94 # is the one from __import__ above which we don't care about95 if self.is_important_traceback(realname, tb):96 reraise(exc_type, exc_value, tb.tb_next)97 continue98 module = sys.modules[fullname] = sys.modules[realname]99 if '.' not in modname:100 setattr(sys.modules[self.wrapper_module], modname, module)101102 if realname.startswith('flaskext.'):103 warnings.warn(104 "Detected extension named flaskext.{x}, please rename it "105 "to flask_{x}. The old form is deprecated."106 .format(x=modname), ExtDeprecationWarning107 )108109 return module110 raise ImportError('No module named %s' % fullname)111112 def is_important_traceback(self, important_module, tb):113 """Walks a traceback's frames and checks if any of the frames114 originated in the given important module. If that is the case then we115 were able to import the module itself but apparently something went116 wrong when the module was imported. (Eg: import of an import failed).117 """118 while tb is not None:119 if self.is_important_frame(important_module, tb):120 return True121 tb = tb.tb_next122 return False123124 def is_important_frame(self, important_module, tb):125 """Checks a single frame if it's important."""126 g = tb.tb_frame.f_globals127 if '__name__' not in g:128 return False129130 module_name = g['__name__']131132 # Python 2.7 Behavior. Modules are cleaned up late so the133 # name shows up properly here. Success!134 if module_name == important_module:135 return True136137 # Some python versions will clean up modules so early that the138 # module name at that point is no longer set. Try guessing from139 # the filename then.140 filename = os.path.abspath(tb.tb_frame.f_code.co_filename)141 test_string = os.path.sep + important_module.replace('.', os.path.sep)142 return test_string + '.py' in filename or \143 test_string + os.path.sep + '__init__.py' in filename
deleted:scripts/flaskext_compat.py-125 removed
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_compat12 flaskext_compat.activate()13 from flask.ext import foo1415 :copyright: (c) 2015 by Armin Ronacher.16 :license: BSD, see LICENSE for more details.17"""18import types19import sys20import os212223class 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 """2829 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('.') + 13435 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_choices4041 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]4647 def find_module(self, fullname, path=None):48 if fullname.startswith(self.prefix):49 return self5051 def load_module(self, fullname):52 if fullname in sys.modules:53 return sys.modules[fullname]54 modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]55 for path in self.module_choices:56 realname = path % modname57 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 the62 # end this seems to be redundant, but if recursive imports63 # 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.71 sys.modules.pop(fullname, None)7273 # If it's an important traceback we reraise it, otherwise74 # we swallow it and try the next choice. The skipped frame75 # is the one from __import__ above which we don't care about.76 if self.is_important_traceback(realname, tb):77 raise exc_type, exc_value, tb.tb_next78 continue79 module = sys.modules[fullname] = sys.modules[realname]80 if '.' not in modname:81 setattr(sys.modules[self.wrapper_module], modname, module)82 return module83 raise ImportError('No module named %s' % fullname)8485 def is_important_traceback(self, important_module, tb):86 """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 """91 while tb is not None:92 if self.is_important_frame(important_module, tb):93 return True94 tb = tb.tb_next95 return False9697 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 False102103 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 True109110 # Some python versions will clean up modules so early that the111 # 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 filename117118119def activate():120 import flask121 ext_module = types.ModuleType('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()
deleted:tests/test_ext.py-197 removed
1# -*- coding: utf-8 -*-2"""3 tests.ext4 ~~~~~~~~~~~~~~~~~~~56 Tests the extension import thing.78 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112import sys13import pytest1415try:16 from imp import reload as reload_module17except ImportError:18 reload_module = reload1920from flask._compat import PY2212223@pytest.fixture(autouse=True)24def disable_extwarnings(recwarn):25 from flask.exthook import ExtDeprecationWarning2627 yield2829 assert set(w.category for w in recwarn.list) \30 <= set([ExtDeprecationWarning])31 recwarn.clear()323334@pytest.fixture(autouse=True)35def importhook_setup(monkeypatch):36 # we clear this out for various reasons. The most important one is37 # that a real flaskext could be in there which would disable our38 # fake package. Secondly we want to make sure that the flaskext39 # import hook does not break on reloading.40 for entry, value in list(sys.modules.items()):41 if (42 entry.startswith('flask.ext.') or43 entry.startswith('flask_') or44 entry.startswith('flaskext.') or45 entry == 'flaskext'46 ) and value is not None:47 monkeypatch.delitem(sys.modules, entry)48 from flask import ext49 reload_module(ext)5051 # reloading must not add more hooks52 import_hooks = 053 for item in sys.meta_path:54 cls = type(item)55 if cls.__module__ == 'flask.exthook' and \56 cls.__name__ == 'ExtensionImporter':57 import_hooks += 158 assert import_hooks == 15960 yield6162 from flask import ext63 for key in ext.__dict__:64 assert '.' not in key656667@pytest.fixture68def newext_simple(modules_tmpdir):69 x = modules_tmpdir.join('flask_newext_simple.py')70 x.write('ext_id = "newext_simple"')717273@pytest.fixture74def oldext_simple(modules_tmpdir):75 flaskext = modules_tmpdir.mkdir('flaskext')76 flaskext.join('__init__.py').write('\n')77 flaskext.join('oldext_simple.py').write('ext_id = "oldext_simple"')787980@pytest.fixture81def newext_package(modules_tmpdir):82 pkg = modules_tmpdir.mkdir('flask_newext_package')83 pkg.join('__init__.py').write('ext_id = "newext_package"')84 pkg.join('submodule.py').write('def test_function():\n return 42\n')858687@pytest.fixture88def oldext_package(modules_tmpdir):89 flaskext = modules_tmpdir.mkdir('flaskext')90 flaskext.join('__init__.py').write('\n')91 oldext = flaskext.mkdir('oldext_package')92 oldext.join('__init__.py').write('ext_id = "oldext_package"')93 oldext.join('submodule.py').write('def test_function():\n'94 ' return 42')959697@pytest.fixture98def flaskext_broken(modules_tmpdir):99 ext = modules_tmpdir.mkdir('flask_broken')100 ext.join('b.py').write('\n')101 ext.join('__init__.py').write('import flask.ext.broken.b\n'102 'import missing_module')103104105def test_flaskext_new_simple_import_normal(newext_simple):106 from flask.ext.newext_simple import ext_id107 assert ext_id == 'newext_simple'108109110def test_flaskext_new_simple_import_module(newext_simple):111 from flask.ext import newext_simple112 assert newext_simple.ext_id == 'newext_simple'113 assert newext_simple.__name__ == 'flask_newext_simple'114115116def test_flaskext_new_package_import_normal(newext_package):117 from flask.ext.newext_package import ext_id118 assert ext_id == 'newext_package'119120121def test_flaskext_new_package_import_module(newext_package):122 from flask.ext import newext_package123 assert newext_package.ext_id == 'newext_package'124 assert newext_package.__name__ == 'flask_newext_package'125126127def test_flaskext_new_package_import_submodule_function(newext_package):128 from flask.ext.newext_package.submodule import test_function129 assert test_function() == 42130131132def test_flaskext_new_package_import_submodule(newext_package):133 from flask.ext.newext_package import submodule134 assert submodule.__name__ == 'flask_newext_package.submodule'135 assert submodule.test_function() == 42136137138def test_flaskext_old_simple_import_normal(oldext_simple):139 from flask.ext.oldext_simple import ext_id140 assert ext_id == 'oldext_simple'141142143def test_flaskext_old_simple_import_module(oldext_simple):144 from flask.ext import oldext_simple145 assert oldext_simple.ext_id == 'oldext_simple'146 assert oldext_simple.__name__ == 'flaskext.oldext_simple'147148149def test_flaskext_old_package_import_normal(oldext_package):150 from flask.ext.oldext_package import ext_id151 assert ext_id == 'oldext_package'152153154def test_flaskext_old_package_import_module(oldext_package):155 from flask.ext import oldext_package156 assert oldext_package.ext_id == 'oldext_package'157 assert oldext_package.__name__ == 'flaskext.oldext_package'158159160def test_flaskext_old_package_import_submodule(oldext_package):161 from flask.ext.oldext_package import submodule162 assert submodule.__name__ == 'flaskext.oldext_package.submodule'163 assert submodule.test_function() == 42164165166def test_flaskext_old_package_import_submodule_function(oldext_package):167 from flask.ext.oldext_package.submodule import test_function168 assert test_function() == 42169170171def test_flaskext_broken_package_no_module_caching(flaskext_broken):172 for x in range(2):173 with pytest.raises(ImportError):174 import flask.ext.broken175176177def test_no_error_swallowing(flaskext_broken):178 with pytest.raises(ImportError) as excinfo:179 import flask.ext.broken180 # python3.6 raises a subclass of ImportError: 'ModuleNotFoundError'181 assert issubclass(excinfo.type, ImportError)182 if PY2:183 message = 'No module named missing_module'184 else:185 message = 'No module named \'missing_module\''186 assert str(excinfo.value) == message187 assert excinfo.tb.tb_frame.f_globals is globals()188189 # reraise() adds a second frame so we need to skip that one too.190 # On PY3 we even have another one :(191 next = excinfo.tb.tb_next.tb_next192 if not PY2:193 next = next.tb_next194195 import os.path196 assert os.path.join('flask_broken', '__init__.py') in \197 next.tb_frame.f_code.co_filename
modified:CHANGES+3 added
82 unmodified lines
83- Template auto reloading will honor the ``run`` command's ``debug`` flag even84 if ``app.jinja_env`` was already accessed. (`#2373`_)85- The following old deprecated code was removed. (`#2385`_)8687 - ``flask.ext`` - import extensions directly by their name instead of88 through the ``flask.ext`` namespace. For example,89 ``import flask.ext.sqlalchemy`` becomes ``import flask_sqlalchemy``.90 - ``Flask.init_jinja_globals`` - extend ``Flask.create_jinja_environment``91 instead.92 - ``Flask.error_handlers`` - tracked by ``Flask.error_handler_spec``,93 use ``@app.errorhandler`` to register handlers.
651 unmodified lines
modified:setup.py+1 added-1 removed
62 unmodified lines
63 author_email='armin.ronacher@active-4.com',64 description='A microframework based on Werkzeug, Jinja2 '65 'and good intentions',66 long_description=__doc__,67 packages=['flask', 'flask.ext', 'flask.json'],67 packages=['flask', 'flask.json'],68 include_package_data=True,69 zip_safe=False,70 platforms='any',71 install_requires=[
39 unmodified lines