Works as intended: #113829, maybe a new function could be considered to check if a path is rooted?
This behavior changes how os.path.isabs interacts with urllib.parse.urlparse when dealing with absolute paths in Windows URIs. In Python 3.12, paths parsed from local file URIs (drive and network paths) were recognized as absolute, but in Python 3.13, they no longer are
It seems as if os.path.isabs was never intended to be used with ParseResult paths. I suppose a more appropriate solution would be to directly check if the path starts with '/', which would indicate that it's absolute.
It seems as if os.path.isabs was never intended to be used with ParseResult paths. I suppose a more appropriate solution would be to directly check if the path starts with '/', which would indicate that it's absolute.
Checking whether it starts with a / makes most sense to me. Another option is posixpath.isabs().
the doc updated in #113829 still indicates the following behavior:
Return ``True`` if *path* is an absolute pathname. On Unix, that means it
begins with a slash, on Windows that it begins with two (back)slashes, or a
drive letter, colon, and (back)slash together.
whereas the intended behavior, as illustrated in Lib/test/test_ntpath.py, appears to be now :
>>> os.path.isabs("/foo/bar") # path beginning with a slash on windows
False
>>> os.path.isabs("\\foo\\bar") # path beginning with two backslashes on windows
False
Hey @kayychou - "\\foo\\bar" expresses a string that begins with a single backslash. If you want two backslashes, you could use a raw string (r"\\foo\bar") or double the backslashes ("\\\\foo\\bar")
Activity
nineteendo commented on Oct 11, 2024
Works as intended: #113829, maybe a new function could be considered to check if a path is rooted?
ikappaki commented on Oct 11, 2024
This behavior changes how os.path.isabs interacts with urllib.parse.urlparse when dealing with absolute paths in Windows URIs. In Python 3.12, paths parsed from local file URIs (drive and network paths) were recognized as absolute, but in Python 3.13, they no longer are
In Python 3.12, os.path.isabs would correctly identify these paths as absolute, including network paths:
but not any more in 3.13
It seems as if os.path.isabs was never intended to be used with ParseResult paths. I suppose a more appropriate solution would be to directly check if the path starts with '/', which would indicate that it's absolute.
nineteendo commented on Oct 11, 2024
You can use pathlib.Path.from_uri() starting from 3.13:
This doesn't roundtrip:
Also, aren't uri's always absolute?
marc-hb commented on Oct 11, 2024
Relative URLs work perfectly fine.
EDIT: I meant in general in life, not related to Python.
nineteendo commented on Oct 11, 2024
Well, at least pathlib doesn't allow creating them.
barneygale commented on Oct 11, 2024
Checking whether it starts with a / makes most sense to me. Another option is posixpath.isabs().
nineteendo commented on Oct 11, 2024
Use p.scheme and p.netloc and p.path.startswith("/") to check if an uri is absolute, an absolute path doesn't change when joined with another path:
That's also why we changed the behaviour of os.path.isabs():
nineteendo commented on Oct 11, 2024
I think this can be closed.
barneygale commented on Oct 11, 2024
Thanks @nineteendo! Closing.
kayychou commented on Dec 3, 2024
hi @nineteendo, @barneygale,
the doc updated in #113829 still indicates the following behavior:
whereas the intended behavior, as illustrated in Lib/test/test_ntpath.py, appears to be now :
>>> os.path.isabs("/foo/bar") # path beginning with a slash on windows False >>> os.path.isabs("\\foo\\bar") # path beginning with two backslashes on windows Falsemaybe I'm missing something under the hood
Thanks for your help!
barneygale commented on Dec 3, 2024
Hey @kayychou - "\\foo\\bar" expresses a string that begins with a single backslash. If you want two backslashes, you could use a raw string (r"\\foo\bar") or double the backslashes ("\\\\foo\\bar")
For more info: https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences