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

thepathlib.Path.parents[-1] didn't receive theself._parts #93156

Closed
Assignees
brettcannon
Labels
3.10only security fixes3.11only security fixes3.12only security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error
@ramwin

Description

@ramwin

Bug report
thepathlib.Path.parents[-1] didn't receive theself._parts
example:

[#6#wangx@manjaro-5800:~] $ pythonPython 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>> from pathlib import Path>>> Path("/home/wangx").parents[-1]PosixPath('/')>>> Path("/home/wangx").parents[-1] == Path("/")False>>> Path("/home/wangx").parents[1] == Path("/")True>>> Path("/home/wangx").parents[1]PosixPath('/')>>>

source code inpathlib.py:

 422 class _PathParents(Sequence):                                                    423     """This object provides sequence-like access to the logical ancestors        424     of a path.  Don't try to construct it yourself."""                           425     __slots__ = ('_pathcls', '_drv', '_root', '_parts')                          426                                                                                  427     def __init__(self, path):                                                    428         # We don't store the instance to avoid reference cycles                  429         self._pathcls = type(path)                                               430         self._drv = path._drv                                                    431         self._root = path._root                                                  432         self._parts = path._parts                                                433                                                                                  434     def __len__(self):                                                           435         if self._drv or self._root:                                              436             return len(self._parts) - 1                                          437         else:                                                                    438             return len(self._parts)                                              439                                                                                  440     def __getitem__(self, idx):                                                  441         if isinstance(idx, slice):                                               442             return tuple(self[i] for i in range(*idx.indices(len(self))))        443                                                                                  444         if idx >= len(self) or idx < -len(self):                                 445             raise IndexError(idx)                                                446         return self._pathcls._from_parsed_parts(self._drv, self._root,           447                                                 self._parts[:-idx - 1])

when the idx is -1,-idx - 1 = 0. so theself._parts[: -idx -1 ] is empty. hence then theparents[-1] != parents[2] because the_parts is different.

Your environment

  • CPython versions tested on: 3.10.4 and 3.12.0a0(main branch)
  • Operating system and architecture:Manjaro Linux 21.2.6 Linux manjaro-5800 5.4.195-1-MANJARO #1 SMP PREEMPT Wed May 18 09:23:31 UTC 2022 x86_64 GNU/Linux

Metadata

Metadata

Assignees

Labels

3.10only security fixes3.11only security fixes3.12only security fixesstdlibPython modules in the Lib dirtype-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