Skip to content
Myr

move url matching after opening session

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 1ff98a2 · Browse files · 4 files+26 added−17 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+26 added−17 removed

  • tests:+17 added−12 removed
  • other:+9 added−5 removed

4 files+26 added−17 removed

modified:src/flask/ctx.py+5 added-5 removed
345 unmodified lines
346 """Can be overridden by a subclass to hook into the matching347 of the request.348 """349 try:350 url_rule, self.request.view_args = self.url_adapter.match(return_rule=True)351 self.request.url_rule = url_rule350 result = self.url_adapter.match(return_rule=True)351 self.request.url_rule, self.request.view_args = result352 except HTTPException as e:353 self.request.routing_exception = e354355 def push(self):
24 unmodified lines
380 sys.exc_clear()381382 _request_ctx_stack.push(self)383384 if self.url_adapter is not None:385 self.match_request()386384 # Open the session at the moment that the request context is available.385 # This allows a custom open_session method to use the request context.386 # Only open a new session if this is the first time the request was387 # pushed, otherwise stream_with_context loses the session.
3 unmodified lines
391392 if self.session is None:393 self.session = session_interface.make_null_session(self.app)394395 if self.url_adapter is not None:396 self.match_request()397398 def pop(self, exc=_sentinel):399 """Pops the request context and unbinds it by doing that. This will400 also trigger the execution of functions registered by the401 :meth:`~flask.Flask.teardown_request` decorator.
74 unmodified lines
modified:tests/test_converters.py+14 added-12 removed
1from flask.globals import _app_ctx_stack1from werkzeug.routing import BaseConverter23from flask import has_request_context4from flask import url_for54def test_custom_converters(app, client):5 from werkzeug.routing import BaseConverter67def test_custom_converters(app, client):8 class ListConverter(BaseConverter):9 def to_python(self, value):10 return value.split(",")11
8 unmodified lines
20 return "|".join(args)2122 assert client.get("/1,2,3").data == b"1|2|3"2324 with app.test_request_context():25 assert url_for("index", args=[4, 5, 6]) == "/4,5,6"2624def test_model_converters(app, client):25 from werkzeug.routing import BaseConverter2728def test_context_available(app, client):27 class ModelConverterTester(BaseConverter):29 class ContextConverter(BaseConverter):30 def to_python(self, value):29 assert _app_ctx_stack.top is not None31 assert has_request_context()32 return value3332 app.url_map.converters["model"] = ModelConverterTester34 app.url_map.converters["ctx"] = ContextConverter3534 @app.route("/<model:user_name>")35 def index(user_name):36 return user_name, 20036 @app.route("/<ctx:name>")37 def index(name):38 return name3938 client.get("/admin").data40 assert client.get("/admin").data == b"admin"
modified:tests/test_testing.py+3 added
152 unmodified lines
153154 ctx = app.test_request_context("/", subdomain="xxx")155 assert ctx.request.url == "http://xxx.example.com:1234/foo/"156157 with ctx:158 assert ctx.request.blueprint == bp.name159160 rv = client.get("/", subdomain="xxx")161 assert rv.data == b"http://xxx.example.com:1234/foo/"162163
284 unmodified lines
modified:CHANGES.rst+4 added
69 unmodified lines
70 favour of a new class ``flask.testing.EnvironBuilder``. :pr:`3232`71- The ``flask run`` command no longer fails if Python is not built72 with SSL support. Using the ``--cert`` option will show an73 appropriate error message. :issue:`3211`74- URL matching now occurs after the request context is pushed, rather75 than when it's created. This allows custom URL converters to access76 the app and request contexts, such as to query a database for an id.77 :issue:`3088`7879.. _#2935: https://github.com/pallets/flask/issues/293580.. _#2957: https://github.com/pallets/flask/issues/295781.. _#2994: https://github.com/pallets/flask/pull/2994
947 unmodified lines