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
Provenance
- Commit
c658639- Tree
085dd99- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1ff98a2
4 files changed+26 added−17 removed
modified:src/flask/ctx.py+5 added-5 removed
345 unmodified lines346 """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 = e354 355 def push(self):24 unmodified lines380 sys.exc_clear()381 382 _request_ctx_stack.push(self)383 384- if self.url_adapter is not None:385- self.match_request()386-384 # 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 lines391 392 if self.session is None:393 self.session = session_interface.make_null_session(self.app)394 395+ if self.url_adapter is not None:396+ self.match_request()397+398 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 linesmodified:tests/test_converters.py+14 added-12 removed
1-from flask.globals import _app_ctx_stack1+from werkzeug.routing import BaseConverter2 3+from flask import has_request_context4+from flask import url_for5 4-def test_custom_converters(app, client):5- from werkzeug.routing import BaseConverter6 7+def test_custom_converters(app, client):8 class ListConverter(BaseConverter):9 def to_python(self, value):10 return value.split(",")11 8 unmodified lines20 return "|".join(args)21 22 assert client.get("/1,2,3").data == b"1|2|3"23 24+ with app.test_request_context():25+ assert url_for("index", args=[4, 5, 6]) == "/4,5,6"26 24-def test_model_converters(app, client):25- from werkzeug.routing import BaseConverter27 28+def 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 value33 32- app.url_map.converters["model"] = ModelConverterTester34+ app.url_map.converters["ctx"] = ContextConverter35 34- @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 name39 38- client.get("/admin").data40+ assert client.get("/admin").data == b"admin"modified:tests/test_testing.py+3 added
152 unmodified lines153 154 ctx = app.test_request_context("/", subdomain="xxx")155 assert ctx.request.url == "http://xxx.example.com:1234/foo/"156 157+ with ctx:158+ assert ctx.request.blueprint == bp.name159+160 rv = client.get("/", subdomain="xxx")161 assert rv.data == b"http://xxx.example.com:1234/foo/"162 163 284 unmodified linesmodified:CHANGES.rst+4 added
69 unmodified lines70 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`78 79 .. _#2935: https://github.com/pallets/flask/issues/293580 .. _#2957: https://github.com/pallets/flask/issues/295781 .. _#2994: https://github.com/pallets/flask/pull/2994947 unmodified lines