Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the pathlib.Path.parents[-1] didn't receive the self._parts #93156

Open
ramwin opened this issue May 24, 2022 · 0 comments
Open

the pathlib.Path.parents[-1] didn't receive the self._parts #93156

ramwin opened this issue May 24, 2022 · 0 comments
Labels
type-bug

Comments

@ramwin
Copy link

@ramwin ramwin commented May 24, 2022

Bug report
the pathlib.Path.parents[-1] didn't receive the self._parts
example:

[#6#wangx@manjaro-5800:~] $ python
Python 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] on linux
Type "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 in pathlib.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 the self._parts[: -idx -1 ] is empty. hence then the parents[-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
@ramwin ramwin added the type-bug label May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug
Projects
None yet
Development

No branches or pull requests

1 participant