From 0f69a5a227bfba6ced8a3826d69d556967967fcc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?=
 <marmarek@invisiblethingslab.com>
Date: Wed, 18 Sep 2024 04:54:24 +0200
Subject: [PATCH] Fix Python3.13 compatibility regarding urllib.parse module

Python 3.13 fixed handling relative paths in urllib.parse module.
Specifically, relative file URL is now constructed as file:path instead
of converting it to absolute file:///path. This breaks
salt.utils.url.create which expects file:/// specifically. The mismatch
results in for example changing salt://top.sls into salt://.sls and thus
not finding the top file.

Fix this by handling both prefixes.

Relevant python change: https://github.com/python/cpython/issues/85110
Fixes: #66898
---
 changelog/66898.fixed.md | 1 +
 salt/utils/url.py        | 5 ++---
 2 files changed, 3 insertions(+), 3 deletions(-)
 create mode 100644 changelog/66898.fixed.md

diff --git a/changelog/66898.fixed.md b/changelog/66898.fixed.md
new file mode 100644
index 000000000000..2549d5e00ed1
--- /dev/null
+++ b/changelog/66898.fixed.md
@@ -0,0 +1 @@
+Fixed Python 3.13 compatibility regarding urllib.parse module
diff --git a/salt/utils/url.py b/salt/utils/url.py
index 478d8e911c2b..839db611c972 100644
--- a/salt/utils/url.py
+++ b/salt/utils/url.py
@@ -4,7 +4,7 @@
 
 import re
 import sys
-from urllib.parse import urlparse, urlunparse
+from urllib.parse import urlparse, urlunparse, urlunsplit
 
 import salt.utils.data
 import salt.utils.path
@@ -46,8 +46,7 @@ def create(path, saltenv=None):
     path = salt.utils.data.decode(path)
 
     query = f"saltenv={saltenv}" if saltenv else ""
-    url = salt.utils.data.decode(urlunparse(("file", "", path, "", query, "")))
-    return "salt://{}".format(url[len("file:///") :])
+    return f'salt://{salt.utils.data.decode(urlunsplit(("", "", path, query, "")))}'
 
 
 def is_escaped(url):
