[New-bugs-announce] [issue40038] pathlib: remove partial support for preserving accessor when modifying a path

Barney Gale report at bugs.python.org
Sun Mar 22 01:15:30 EDT 2020


New submission from Barney Gale <barney.gale at 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 <report at bugs.python.org>
<https://bugs.python.org/issue40038>
_______________________________________


More information about the New-bugs-announce mailing list