Skip to content
Myr

wait until app ctx is ready before matching url

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

`RequestContext.match_request` is moved from `__init__` to `push`. This causes matching to happen later, when the app context is available. This enables URL converters that use things such as the database.

authored by Elad Moshe · committed by David Lord · parent 29111a3 · Browse files · 4 files+41 added−24 removed
This commit

State

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

Diff stats

4 files+41 added−24 removed

  • tests:+38 added−21 removed
  • other:+3 added−3 removed

4 files+41 added−24 removed

modified:src/flask/ctx.py+3 added-3 removed
312 unmodified lines
313 # object. These will be called before the regular "after_request"314 # functions.315 self._after_request_functions = []316317 if self.url_adapter is not None:318 self.match_request()319317 @property318 def g(self):319 return _app_ctx_stack.top.g320
59 unmodified lines
380 sys.exc_clear()381382 _request_ctx_stack.push(self)383384 if self.url_adapter is not None:385 self.match_request()386387 # Open the session at the moment that the request context is available.388 # This allows a custom open_session method to use the request context.389 # Only open a new session if this is the first time the request was390 # pushed, otherwise stream_with_context loses the session.
85 unmodified lines
modified:tests/test_basic.py-20 removed
1382 unmodified lines
1383 with app.test_request_context():1384 flask.url_for("/")138513861387def test_custom_converters(app, client):1388 from werkzeug.routing import BaseConverter13891390 class ListConverter(BaseConverter):1391 def to_python(self, value):1392 return value.split(",")13931394 def to_url(self, value):1395 base_to_url = super(ListConverter, self).to_url1396 return ",".join(base_to_url(x) for x in value)13971398 app.url_map.converters["list"] = ListConverter13991400 @app.route("/<list:args>")1401 def index(args):1402 return "|".join(args)14031404 assert client.get("/1,2,3").data == b"1|2|3"140514061387def test_static_files(app, client):1388 rv = client.get("/static/index.html")1389 assert rv.status_code == 2001390 assert rv.data.strip() == b"<h1>Hello World!</h1>"
579 unmodified lines
added:tests/test_converters.py+38 added
1from flask.globals import _app_ctx_stack234def test_custom_converters(app, client):5 from werkzeug.routing import BaseConverter67 class ListConverter(BaseConverter):8 def to_python(self, value):9 return value.split(",")1011 def to_url(self, value):12 base_to_url = super(ListConverter, self).to_url13 return ",".join(base_to_url(x) for x in value)1415 app.url_map.converters["list"] = ListConverter1617 @app.route("/<list:args>")18 def index(args):19 return "|".join(args)2021 assert client.get("/1,2,3").data == b"1|2|3"222324def test_model_converters(app, client):25 from werkzeug.routing import BaseConverter2627 class ModelConverterTester(BaseConverter):28 def to_python(self, value):29 assert _app_ctx_stack.top is not None30 return value3132 app.url_map.converters["model"] = ModelConverterTester3334 @app.route("/<model:user_name>")35 def index(user_name):36 return user_name, 2003738 client.get("/admin").data
modified:tests/test_testing.py-1 removed
151 unmodified lines
152 app.register_blueprint(bp)153154 ctx = app.test_request_context("/", subdomain="xxx")155 assert ctx.request.url == "http://xxx.example.com:1234/foo/"156 assert ctx.request.blueprint == bp.name156157 rv = client.get("/", subdomain="xxx")158 assert rv.data == b"http://xxx.example.com:1234/foo/"159
285 unmodified lines