Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Description
Bug report
Inpathlib
prior to Python 3.12, passing aPureWindowsPath
to aPurePosixPath
resulted in the Windows separator (\
) being converted to the POSIX separator (/
). However, in the current main branch the backslashes are preserved in thePurePosixPath
object.
Here is an example which illustrates this:
Python 3.12.0a7 (tags/v3.12.0a7:b861ba4, Apr 6 2023, 16:09:18) [Clang 10.0.0 ] on linuxType "help", "copyright", "credits" or "license" for more information.>>>import pathlib>>>print(pathlib.PurePosixPath(pathlib.PureWindowsPath(r"a\b\c")))a\b\c>>>print(pathlib.PurePosixPath(pathlib.PureWindowsPath(r"a\b\c")).as_posix())a\b\c
Python 3.11.2 (tags/v3.11.2:878ead1, Mar 9 2023, 16:26:59) [Clang 10.0.0 ] on linuxType "help", "copyright", "credits" or "license" for more information.>>>import pathlib>>>print(pathlib.PurePosixPath(pathlib.PureWindowsPath(r"a\b\c")))a/b/c>>>print(pathlib.PurePosixPath(pathlib.PureWindowsPath(r"a\b\c")).as_posix())a/b/c
(The behaviour is the same if usingPosixPath
orWindowsPath
on the relevant platform; it's not specific to the "pure" variants.)
Before the recent refactoring of the module, passing onePath
orPurePath
object to another resulted in the_parts
attribute being inspected. This was a list of the individual path elements (e.g.['a', 'b', 'c']
for the patha\b\c
). The_parts
attribute was removed inGH-102476 and replaced with_tail
, but with slightly different semantics.
The current code replaces anyos.altsep
in the path withos.sep
, which forWindowsPath
replaces/
with\
but forPosixPath
does nothing as there is no alternative separator. However, the following will produce a correct result:
Python 3.12.0a7+ (heads/main:bd2ed06, Apr 19 2023, 15:27:47) [GCC 9.4.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>>import pathlib>>> pathlib.PurePosixPath._flavour.altsep="\\">>> pathlib.PurePosixPath(pathlib.PureWindowsPath(r"a\b\c")).as_posix()'a/b/c'
Thus I think the problem can be isolated to these lines here:
Lines 316 to 324 inda2273f
@classmethod | |
def_parse_path(cls,path): | |
ifnotpath: | |
return'','', [] | |
sep=cls._flavour.sep | |
altsep=cls._flavour.altsep | |
ifaltsep: | |
path=path.replace(altsep,sep) | |
drv,root,rel=cls._flavour.splitroot(path) |
Your environment
- CPython versions tested on: 3.8, 3.11 and 3.12 (alpha 7 and HEAD)
- Operating system and architecture: Ubuntu 20.04 and Windows 10