From 714b0a467acab54091d8aeb73403d808abfbcf5b Mon Sep 17 00:00:00 2001 From: pgjones Date: Fri, 21 May 2021 15:01:32 +0100 Subject: [PATCH] Fix blueprint self registration By raising a ValueError if attempted. I don't see a use case that makes this worth supporting. --- src/flask/blueprints.py | 2 ++ tests/test_blueprints.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index fc999c7553..2da4d12b60 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -260,6 +260,8 @@ def register_blueprint(self, blueprint: "Blueprint", **options: t.Any) -> None: .. versionadded:: 2.0 """ + if blueprint is self: + raise ValueError("Cannot register a blueprint on itself") self._blueprints.append((blueprint, options)) def register(self, app: "Flask", options: dict) -> None: diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 2977f69c27..73c94ade39 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -883,3 +883,9 @@ def test_unique_blueprint_names(app, client) -> None: app.register_blueprint(bp2) # different bp, same name, error app.register_blueprint(bp2, name="alt") # different bp, different name, ok + + +def test_self_registration(app, client) -> None: + bp = flask.Blueprint("bp", __name__) + with pytest.raises(ValueError): + bp.register_blueprint(bp)