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

Closed
ramwin opened this issue May 24, 2022 · 4 comments
Closed

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

ramwin opened this issue May 24, 2022 · 4 comments
Assignees
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ramwin
Copy link

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 An unexpected behavior, bug, or error label May 24, 2022
@barneygale
Copy link
Contributor

This looks like a bug in 79d2e62 - negative indices into parents don't work properly with absolute paths.

barneygale added a commit to barneygale/cpython that referenced this issue May 26, 2022
…h().parents`

When a `_PathParents` object has a drive or a root, the length of the
object is *one less* than than the length of `self._parts`, which resulted
in an off-by-one error when `path.parents[-n]` was fed through to
`self._parts[:-n - 1]`. In particular, `path.parents[-1]` was a malformed
path object with spooky properties.

This is addressed by adding `len(self)` to negative indices.
@AA-Turner AA-Turner added stdlib Python modules in the Lib dir 3.11 only security fixes 3.10 only security fixes 3.12 bugs and security fixes labels May 26, 2022
@akulakov
Copy link
Contributor

akulakov commented May 28, 2022

Seems to be a duplicate of #89577 (which doesn't have a PR)

[Edit: after looking through the comments on that issue, @MojoVampire came up with the same fix.]

brettcannon pushed a commit that referenced this issue Jun 3, 2022
…rents` (GH-93273)

When a `_PathParents` object has a drive or a root, the length of the
object is *one less* than than the length of `self._parts`, which resulted
in an off-by-one error when `path.parents[-n]` was fed through to
`self._parts[:-n - 1]`. In particular, `path.parents[-1]` was a malformed
path object with spooky properties.

This is addressed by adding `len(self)` to negative indices.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 3, 2022
…h().parents` (pythonGH-93273)

When a `_PathParents` object has a drive or a root, the length of the
object is *one less* than than the length of `self._parts`, which resulted
in an off-by-one error when `path.parents[-n]` was fed through to
`self._parts[:-n - 1]`. In particular, `path.parents[-1]` was a malformed
path object with spooky properties.

This is addressed by adding `len(self)` to negative indices.
(cherry picked from commit f32e6b4)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 3, 2022
…h().parents` (pythonGH-93273)

When a `_PathParents` object has a drive or a root, the length of the
object is *one less* than than the length of `self._parts`, which resulted
in an off-by-one error when `path.parents[-n]` was fed through to
`self._parts[:-n - 1]`. In particular, `path.parents[-1]` was a malformed
path object with spooky properties.

This is addressed by adding `len(self)` to negative indices.
(cherry picked from commit f32e6b4)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
@brettcannon
Copy link
Member

@brettcannon brettcannon self-assigned this Jun 3, 2022
miss-islington added a commit that referenced this issue Jun 3, 2022
…rents` (GH-93273)

When a `_PathParents` object has a drive or a root, the length of the
object is *one less* than than the length of `self._parts`, which resulted
in an off-by-one error when `path.parents[-n]` was fed through to
`self._parts[:-n - 1]`. In particular, `path.parents[-1]` was a malformed
path object with spooky properties.

This is addressed by adding `len(self)` to negative indices.
(cherry picked from commit f32e6b4)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
miss-islington added a commit that referenced this issue Jun 3, 2022
…rents` (GH-93273)

When a `_PathParents` object has a drive or a root, the length of the
object is *one less* than than the length of `self._parts`, which resulted
in an off-by-one error when `path.parents[-n]` was fed through to
`self._parts[:-n - 1]`. In particular, `path.parents[-1]` was a malformed
path object with spooky properties.

This is addressed by adding `len(self)` to negative indices.
(cherry picked from commit f32e6b4)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
@AA-Turner
Copy link
Member

#93273 has been merged and backported.

A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants