Fix using non-strings as anchors

Previously, non-strings passed to _anchor would be converted to strings.
Restore this ability. The logic is the same as in url_quote.

Fixes: c4b39ba2 ("replace werkzeug.urls with urllib.parse")
This commit is contained in:
Sean Anderson 2023-07-18 00:01:19 -04:00
parent cb825687a5
commit 547f2b7f1e
3 changed files with 4 additions and 0 deletions

View file

@ -7,6 +7,7 @@ Unreleased
- Require Werkzeug >= 2.3.6.
- Use ``flit_core`` instead of ``setuptools`` as build backend.
- Refactor how an app's root and instance paths are determined. :issue:`5160`
- Fix passing non-string values to ``url_for``. :issue:`5206`
Version 2.3.2

View file

@ -1697,6 +1697,8 @@ class Flask(Scaffold):
return self.handle_url_build_error(error, endpoint, values)
if _anchor is not None:
if not isinstance(_anchor, (str, bytes, bytearray)):
_anchor = str(_anchor)
_anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
rv = f"{rv}#{_anchor}"

View file

@ -106,6 +106,7 @@ class TestUrlFor:
return "42"
assert flask.url_for("index", _anchor="x y") == "/#x%20y"
assert flask.url_for("index", _anchor=5) == "/#5"
def test_url_for_with_scheme(self, app, req_ctx):
@app.route("/")