Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PurePosixPath no longer correctly parses PureWindowsPath #103631

Closed
Assignees
barneygale
Labels
3.12only security fixes3.13bugs and security fixestopic-pathlibtype-bugAn unexpected behavior, bug, or error
@domdfcoding

Description

@domdfcoding

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:

cpython/Lib/pathlib.py

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

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixestopic-pathlibtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp