Added deprecation warnings for modules
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 5910ef8 · Browse files · 2 files·+47 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+47 added−4 removed
- tests:+30 added−4 removed
- other:+17 added−0 removed
Provenance
- Commit
1446614- Tree
1163166- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5910ef8
2 files changed+47 added−4 removed
modified:flask/app.py+17 added
167 unmodified lines168 #:169 #: .. versionadded:: 0.4170 logger_name = ConfigAttribute('LOGGER_NAME')171 172+ #: Enable the deprecated module support? This is active by default173+ #: in 0.7 but will be changed to False in 0.8. With Flask 1.0 modules174+ #: will be removed in favor of Blueprints175+ enable_modules = True176+177 #: The logging format used for the debug logger. This is only used when178 #: the application is in debug mode, otherwise the attached logging179 #: handler does the formatting.180 #:356 unmodified lines537 of this function are the same as the ones for the constructor of the538 :class:`Module` class and will override the values of the module if539 provided.540 """541+ if not self.enable_modules:542+ raise RuntimeError('Module support was disabled but code '543+ 'attempted to register a module named %r' % module)544+ else:545+ from warnings import warn546+ warn(DeprecationWarning('Modules are deprecated. Upgrade to '547+ 'using blueprints. Have a look into the documentation for '548+ 'more information. If this module was registered by a '549+ 'Flask-Extension upgrade the extension or contact the author '550+ 'of that extension instead. (Registered %r)' % module),551+ stacklevel=2)552+553 options.setdefault('url_prefix', module.url_prefix)554 options.setdefault('subdomain', module.subdomain)555 self.view_functions.update(module.view_functions)556 state = _ModuleSetupState(self, **options)493 unmodified linesmodified:tests/flask_tests.py+30 added-4 removed
13 unmodified lines14 import re15 import sys16 import flask17 import unittest18+import warnings19 from threading import Thread20 from logging import StreamHandler21 from contextlib import contextmanager22+from functools import update_wrapper23 from datetime import datetime24 from werkzeug import parse_date, parse_options_header25 from werkzeug.exceptions import NotFound26 from jinja2 import TemplateNotFound17 unmodified lines44 TEST_KEY = 'foo'45 SECRET_KEY = 'devkey'46 47 48+# import moduleapp here because it uses deprecated features and we don't49+# want to see the warnings50+warnings.simplefilter('ignore', DeprecationWarning)51+from moduleapp import app as moduleapp52+warnings.simplefilter('default', DeprecationWarning)53+54+55 @contextmanager56 def catch_warnings():57 """Catch warnings in a with block in a list"""49- import warnings50-58 # make sure deprecation warnings are active in tests59 warnings.simplefilter('default', category=DeprecationWarning)60 61 filters = warnings.filters20 unmodified lines82 finally:83 sys.stderr = old_stderr84 85 86+def emits_module_deprecation_warning(f):87+ def new_f(*args, **kwargs):88+ with catch_warnings() as log:89+ f(*args, **kwargs)90+ assert log, 'expected deprecation warning'91+ for entry in log:92+ assert 'Modules are deprecated' in str(entry['message'])93+ return update_wrapper(new_f, f)94+95+96 class ContextTestCase(unittest.TestCase):97 98 def test_context_binding(self):99 app = flask.Flask(__name__)877 unmodified lines977 978 979 class ModuleTestCase(unittest.TestCase):980 981+ @emits_module_deprecation_warning982 def test_basic_module(self):983 app = flask.Flask(__name__)984 admin = flask.Module(__name__, 'admin', url_prefix='/admin')985 @admin.route('/')14 unmodified lines1000 assert c.get('/admin/').data == 'admin index'1001 assert c.get('/admin/login').data == 'admin login'1002 assert c.get('/admin/logout').data == 'admin logout'1003 1004+ @emits_module_deprecation_warning1005 def test_default_endpoint_name(self):1006 app = flask.Flask(__name__)1007 mod = flask.Module(__name__, 'frontend')1008 def index():4 unmodified lines1013 assert rv.data == 'Awesome'1014 with app.test_request_context():1015 assert flask.url_for('frontend.index') == '/'1016 1017+ @emits_module_deprecation_warning1018 def test_request_processing(self):1019 catched = []1020 app = flask.Flask(__name__)1021 admin = flask.Module(__name__, 'admin', url_prefix='/admin')27 unmodified lines1049 assert c.get('/admin/').data == 'the admin'1050 assert catched == ['before-app', 'before-admin',1051 'after-admin', 'after-app']1052 1053+ @emits_module_deprecation_warning1054 def test_context_processors(self):1055 app = flask.Flask(__name__)1056 admin = flask.Module(__name__, 'admin', url_prefix='/admin')1057 @app.context_processor15 unmodified lines1073 c = app.test_client()1074 assert c.get('/').data == '13'1075 assert c.get('/admin/').data == '123'1076 1077+ @emits_module_deprecation_warning1078 def test_late_binding(self):1079 app = flask.Flask(__name__)1080 admin = flask.Module(__name__, 'admin')1081 @admin.route('/')1 unmodified line1083 return '42'1084 app.register_module(admin, url_prefix='/admin')1085 assert app.test_client().get('/admin/').data == '42'1086 1087+ @emits_module_deprecation_warning1088 def test_error_handling(self):1089 app = flask.Flask(__name__)1090 admin = flask.Module(__name__, 'admin')1091 @admin.app_errorhandler(404)17 unmodified lines1109 assert rv.status_code == 5001110 assert 'internal server error' == rv.data1111 1112 def test_templates_and_static(self):1090- from moduleapp import app1113+ app = moduleapp1114 c = app.test_client()1115 1116 rv = c.get('/')1117 assert rv.data == 'Hello from the Frontend'21 unmodified lines1139 with flask.Flask(__name__).test_request_context():1140 assert flask.render_template('nested/nested.txt') == 'I\'m nested'1141 1142 def test_safe_access(self):1120- from moduleapp import app1143+ app = moduleapp1144 1145 with app.test_request_context():1146 f = app.view_functions['admin.static']1147 24 unmodified lines1172 assert 0, 'expected exception'1173 finally:1174 os.path = old_path1175 1176+ @emits_module_deprecation_warning1177 def test_endpoint_decorator(self):1178 from werkzeug.routing import Submount, Rule1179 from flask import Module1180 281 unmodified lines1462 1463 rv = c.get('/', 'http://test.localhost/')1464 assert rv.data == 'test index'1465 1466+ @emits_module_deprecation_warning1467 def test_module_static_path_subdomain(self):1468 app = flask.Flask(__name__)1469 app.config['SERVER_NAME'] = 'example.com'1470 from subdomaintestmodule import mod12 unmodified lines1483 c = app.test_client()1484 rv = c.get('/', 'http://mitsuhiko.localhost/')1485 assert rv.data == 'index for mitsuhiko'1486 1487+ @emits_module_deprecation_warning1488 def test_module_subdomain_support(self):1489 app = flask.Flask(__name__)1490 mod = flask.Module(__name__, 'test', subdomain='testing')1491 app.config['SERVER_NAME'] = 'localhost'144 unmodified lines