Skip to content

Commit

Permalink
Fix urlunparse for salt file urls
Browse files Browse the repository at this point in the history
Previously, the `salt.utils.url.create()` method naively assumed the
compiled URL provided by `urllib.parse.urlunparse()` always starts with
the literal string `file:///`, that is, the begininning of the URL
string starting with the `scheme` and including an empty `authority`
section.  Beginning with
[python-3.12](python/cpython@0edfc66),
however, generating a URL with `urlunparse()` results in the syntax
`file:` for the portion of the URL including the `scheme` and
`authority` sections.
  • Loading branch information
jfindlay committed Oct 8, 2024
1 parent 58c89a8 commit 4caba2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion salt/utils/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ def create(path, saltenv=None):

query = f"saltenv={saltenv}" if saltenv else ""
url = salt.utils.data.decode(urlunparse(("file", "", path, "", query, "")))
return "salt://{}".format(url[len("file:///") :])
# urllib returns `file:` or `file:///` but salt wants `salt://` so normalize here
if url.startswith(f"file:{path}"):
return f"salt://{url.split('file:')[1]}"
if url.startswith(f"file://{path}"):
return f"salt://{url.split('file://')[1]}"
if url.startswith(f"file:///:{path}"):
return f"salt://{url.split('file:///')[1]}"
raise ValueError(f"Cannot interpret {url!r} as a 'salt://' URL")


def is_escaped(url):
Expand Down
File renamed without changes.

0 comments on commit 4caba2d

Please sign in to comment.