Skip to content
Myr

Fix registering a blueprint twice with differing names

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

Previously the blueprint recorded aspects (before request, after request etc) would only be added to the app if it was the first registration of the blueprint instance. However only the record-once aspects (app-before requests, app-after request) should be added once on registration of the instance, whereas everything else should be added on every unique name registration. This ensures that these trigger under the new name as well as the old.

authored by pgjones · committed by Phil Jones · parent 399b851 · Browse files · 3 files+16 added−3 removed
This commit

State

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

Diff stats

3 files+16 added−3 removed

  • tests:+10 added−0 removed
  • other:+6 added−3 removed

3 files+16 added−3 removed

modified:src/flask/blueprints.py+5 added-3 removed
291 unmodified lines
292 .. versionchanged:: 2.0.1293 Registering the same blueprint with the same name multiple294 times is deprecated and will become an error in Flask 2.1.295 """296 first_registration = not any(bp is self for bp in app.blueprints.values())296 name_prefix = options.get("name_prefix", "")297 self_name = options.get("name", self.name)298 name = f"{name_prefix}.{self_name}".lstrip(".")299
16 unmodified lines
316 " in Flask 2.1.",317 stacklevel=4,318 )319320 first_bp_registration = not any(bp is self for bp in app.blueprints.values())321 first_name_registration = name not in app.blueprints322323 app.blueprints[name] = self324 self._got_registered_once = True323 state = self.make_setup_state(app, options, first_registration)325 state = self.make_setup_state(app, options, first_bp_registration)326327 if self.has_static_folder:328 state.add_url_rule(329 f"{self.static_url_path}/<path:filename>",
1 unmodified line
331 endpoint="static",332 )333334 # Merge blueprint data into parent.333 if first_registration:335 if first_bp_registration or first_name_registration:336337 def extend(bp_dict, parent_dict):338 for key, values in bp_dict.items():339 key = name if key is None else f"{name}.{key}"
269 unmodified lines
modified:tests/test_blueprints.py+10 added
897 unmodified lines
898 @bp.get("/")899 def index():900 return flask.request.endpoint901902 @bp.get("/error")903 def error():904 flask.abort(403)905906 @bp.errorhandler(403)907 def forbidden(_: Exception):908 return "Error", 403909910 @bp2.get("/")911 def index2():912 return flask.request.endpoint913
4 unmodified lines
918 assert client.get("/a/").data == b"bp.index"919 assert client.get("/b/").data == b"alt.index"920 assert client.get("/a/a/").data == b"bp.sub.index2"921 assert client.get("/b/a/").data == b"alt.sub.index2"922 assert client.get("/a/error").data == b"Error"923 assert client.get("/b/error").data == b"Error"
modified:CHANGES.rst+1 added
10 unmodified lines
11- Fixed the issue where typing requires template global12 decorators to accept functions with no arguments. :issue:`4098`13- Support View and MethodView instances with async handlers. :issue:`4112`14- Enhance typing of ``app.errorhandler`` decorator. :issue:`4095`15- Fix registering a blueprint twice with differing names. :issue:`4124`161718Version 2.0.119-------------
1154 unmodified lines