]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-68320, gh-88302 - Allow for private `pathlib.Path` subclassing (GH-31691)
authorBarney Gale <barney.gale@gmail.com>
Fri, 23 Dec 2022 22:52:23 +0000 (22:52 +0000)
committerGitHub <noreply@github.com>
Fri, 23 Dec 2022 22:52:23 +0000 (14:52 -0800)
commita68e585c8b7b27323f67905868467ce0588a1dae
treeb6a5151685291445590c584dd1f02c5c6684b9eb
parent5d84966cce6c596da22922a07f49bde959ff5201
gh-68320, gh-88302 - Allow for private `pathlib.Path` subclassing (GH-31691)

Users may wish to define subclasses of `pathlib.Path` to add or modify
existing methods. Before this change, attempting to instantiate a subclass
raised an exception like:

    AttributeError: type object 'PPath' has no attribute '_flavour'

Previously the `_flavour` attribute was assigned as follows:

    PurePath._flavour        = xxx not set!! xxx
    PurePosixPath._flavour   = _PosixFlavour()
    PureWindowsPath._flavour = _WindowsFlavour()

This change replaces it with a `_pathmod` attribute, set as follows:

    PurePath._pathmod        = os.path
    PurePosixPath._pathmod   = posixpath
    PureWindowsPath._pathmod = ntpath

Functionality from `_PosixFlavour` and `_WindowsFlavour` is moved into
`PurePath` as underscored-prefixed classmethods. Flavours are removed.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Brett Cannon <brett@python.org>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Eryk Sun <eryksun@gmail.com>
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2022-03-05-02-14-09.bpo-24132.W6iORO.rst [new file with mode: 0644]