From 3ba0fd5c52c9943c492ce098693bf9e651942fe5 Mon Sep 17 00:00:00 2001
From: midchildan <git@midchildan.org>
Date: Sat, 27 May 2023 01:03:02 +0900
Subject: [PATCH] fix: iri_to_uri fails when the argument is a bytestring

This was caused by the 'charset' variable being used before it was ready.
---
 CHANGES.rst          |  4 ++++
 src/werkzeug/urls.py | 20 ++++++++++----------
 tests/test_urls.py   |  3 +++
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/werkzeug/urls.py b/src/werkzeug/urls.py
index 89ef21943..f5760eb4c 100644
--- a/src/werkzeug/urls.py
+++ b/src/werkzeug/urls.py
@@ -966,6 +966,16 @@ def iri_to_uri(
 
     .. versionadded:: 0.6
     """
+    if charset is not None:
+        warnings.warn(
+            "The 'charset' parameter is deprecated and will be removed"
+            " in Werkzeug 3.0.",
+            DeprecationWarning,
+            stacklevel=2,
+        )
+    else:
+        charset = "utf-8"
+
     if isinstance(iri, tuple):
         warnings.warn(
             "Passing a tuple is deprecated and will not be supported in Werkzeug 3.0.",
@@ -982,16 +992,6 @@ def iri_to_uri(
         )
         iri = iri.decode(charset)
 
-    if charset is not None:
-        warnings.warn(
-            "The 'charset' parameter is deprecated and will be removed"
-            " in Werkzeug 3.0.",
-            DeprecationWarning,
-            stacklevel=2,
-        )
-    else:
-        charset = "utf-8"
-
     if errors is not None:
         warnings.warn(
             "The 'errors' parameter is deprecated and will be removed in Werkzeug 3.0.",
diff --git a/tests/test_urls.py b/tests/test_urls.py
index 56bca8e94..765d42546 100644
--- a/tests/test_urls.py
+++ b/tests/test_urls.py
@@ -231,6 +231,9 @@ def test_iri_support():
 
     assert urls.iri_to_uri("/foo") == "/foo"
 
+    with pytest.deprecated_call():
+        assert urls.iri_to_uri(b"/foo") == "/foo"
+
     assert (
         urls.iri_to_uri("http://föö.com:8080/bam/baz")
         == "http://xn--f-1gaa.com:8080/bam/baz"
