New submission from Barney Gale <barney.g...@gmail.com>:

`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())
```

----------
components: Library (Lib)
messages: 364783
nosy: barneygale
priority: normal
severity: normal
status: open
title: pathlib: remove partial support for preserving accessor when modifying a 
path
type: performance
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40038>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to