Skip to content

Commit

Permalink
fix the error type
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Dec 31, 2024
1 parent 377974a commit 991f21f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/pdm/models/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def _parse_url(self) -> None:
if relpath is None:
try:
self.path = Path(url_to_path(url))
except AssertionError:
except ValueError:
pass
else:
self.path = Path(relpath)
Expand Down
76 changes: 34 additions & 42 deletions src/pdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,52 +179,44 @@ def cd(path: str | Path) -> Iterator:
os.chdir(_old_cwd)


if sys.version_info >= (3, 13):

def url_to_path(url: str) -> str:
"""
Convert a file: URL to a path.
"""
return Path.from_uri(url).as_posix()
else:

def url_to_path(url: str) -> str:
"""
Convert a file: URL to a path.
"""
from urllib.request import url2pathname
def url_to_path(url: str) -> str:
"""
Convert a file: URL to a path.
"""
from urllib.request import url2pathname

WINDOWS = sys.platform == "win32"
WINDOWS = sys.platform == "win32"

assert url.startswith("file:"), f"You can only turn file: urls into filenames (not {url!r})"
if not url.startswith("file:"):
raise ValueError(f"You can only turn file: urls into filenames (not {url!r})")

_, netloc, path, _, _ = parse.urlsplit(url)
_, netloc, path, _, _ = parse.urlsplit(url)

if not netloc or netloc == "localhost":
# According to RFC 8089, same as empty authority.
netloc = ""
elif WINDOWS:
# If we have a UNC path, prepend UNC share notation.
netloc = "\\\\" + netloc
else:
raise ValueError(f"non-local file URIs are not supported on this platform: {url!r}")

path = url2pathname(netloc + path)

# On Windows, urlsplit parses the path as something like "/C:/Users/foo".
# This creates issues for path-related functions like io.open(), so we try
# to detect and strip the leading slash.
if (
WINDOWS
and not netloc # Not UNC.
and len(path) >= 3
and path[0] == "/" # Leading slash to strip.
and path[1].isalpha() # Drive letter.
and path[2:4] in (":", ":/") # Colon + end of string, or colon + absolute path.
):
path = path[1:]

return path
if not netloc or netloc == "localhost":
# According to RFC 8089, same as empty authority.
netloc = ""
elif WINDOWS:
# If we have a UNC path, prepend UNC share notation.
netloc = "\\\\" + netloc
else:
raise ValueError(f"non-local file URIs are not supported on this platform: {url!r}")

path = url2pathname(netloc + path)

# On Windows, urlsplit parses the path as something like "/C:/Users/foo".
# This creates issues for path-related functions like io.open(), so we try
# to detect and strip the leading slash.
if (
WINDOWS
and not netloc # Not UNC.
and len(path) >= 3
and path[0] == "/" # Leading slash to strip.
and path[1].isalpha() # Drive letter.
and path[2:4] in (":", ":/") # Colon + end of string, or colon + absolute path.
):
path = path[1:]

return path


def split_path_fragments(path: Path) -> tuple[Path, str]:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_add_ssh_scheme_to_git_uri(given, expected):

class TestUrlToPath:
def test_non_file_url(self):
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
utils.url_to_path("not_a_file_scheme://netloc/path")

@pytest.mark.skipif(sys.platform.startswith("win"), reason="Non-Windows test")
Expand Down

0 comments on commit 991f21f

Please sign in to comment.