This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pathlib: remove partial support for preserving accessor when modifying a path
Type: performance Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barneygale, steve.dower
Priority: normal Keywords: patch

Created on 2020-03-22 05:15 by barneygale, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19342 merged barneygale, 2020-04-03 17:40
Messages (2)
msg364783 - (view) Author: Barney Gale (barneygale) * Date: 2020-03-22 05:15
`pathlib.Path._init()` accepts a 'template' argument that pathlib uses - in some cases - to preserve the current accessor object when creating modified path objects. This works for `resolve()`, `absolute()` and `readlink()`, *but no other cases*!

As customizing the accessor is not something we support (yet! see https://discuss.python.org/t/make-pathlib-extensible/3428), and the majority of path methods do not call `_init()` with a 'template' argument (and so do not preserve the accessor), I suggest this internal functionality be removed. Together with bpo-39682 / gh-18846, I believe this would allow us to remove `Path._init()` entirely, which would be a small performance win and a simplification of the code.

Demo:

```
import pathlib


class CustomAccessor(pathlib._NormalAccessor):
    pass


def print_accessor(path):
    if isinstance(path._accessor, CustomAccessor):
        print("    %r: custom" % path)
    else:
        print("    %r: normal" % path)


print("Here's a path with a custom accessor:")
p = pathlib.Path("/tmp")
p._accessor = CustomAccessor()
print_accessor(p)

print("Our accessor type is retained in resolve(), absolute() and readlink():")
print_accessor(p.absolute())
print_accessor(p.resolve())
#print_accessor(p.readlink())

print("But not in any other path-creating methods!")
print_accessor(p.with_name("foo"))
print_accessor(p.with_suffix(".foo"))
print_accessor(p.relative_to("/"))
print_accessor(p / "foo")
print_accessor(p.joinpath("foo"))
print_accessor(p.parent)
print_accessor(p.parents[0])
print_accessor(list(p.iterdir())[0])
print_accessor(list(p.glob("*"))[0])
print_accessor(list(p.rglob("*"))[0])
#print_accessor(p.expanduser())
```
msg390392 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-04-07 00:26
New changeset 2219187cab6bca009c42b63b2f4c30b5b10c916d by Barney Gale in branch 'master':
bpo-40038: pathlib: remove partial support for preserving accessor when modifying a path (GH-19342)
https://github.com/python/cpython/commit/2219187cab6bca009c42b63b2f4c30b5b10c916d
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84219
2021-04-07 11:35:15steve.dowersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-04-07 00:26:41steve.dowersetnosy: + steve.dower
messages: + msg390392
2020-04-03 17:40:35barneygalesetkeywords: + patch
stage: patch review
pull_requests: + pull_request18706
2020-03-22 05:15:29barneygalecreate