diff options
author | Barney Gale <barney.gale@gmail.com> | 2023-03-05 22:46:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-05 22:46:45 (GMT) |
commit | 3e60e0213e9d884a2f4cef4df96956c5d4adde0a (patch) | |
tree | 8a5bb774128ec2d8add4fec68caacd9385b2414b /Lib/pathlib.py | |
parent | 3572c861d8e8f03c34793608e7e2221e116aaec0 (diff) | |
download | cpython-3e60e0213e9d884a2f4cef4df96956c5d4adde0a.zip cpython-3e60e0213e9d884a2f4cef4df96956c5d4adde0a.tar.gz cpython-3e60e0213e9d884a2f4cef4df96956c5d4adde0a.tar.bz2 |
GH-101362: Check pathlib.Path flavour compatibility at import time (GH-101664)
This saves a comparison in `pathlib.Path.__new__()` and reduces the time taken to run `Path()` by ~5%.
Automerge-Triggered-By: GH:AlexWaygood
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r-- | Lib/pathlib.py | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py index ed0f2cc..c37ff21 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -707,11 +707,7 @@ class Path(PurePath): warnings._deprecated("pathlib.PurePath(**kwargs)", msg, remove=(3, 14)) if cls is Path: cls = WindowsPath if os.name == 'nt' else PosixPath - self = cls._from_parts(args) - if self._flavour is not os.path: - raise NotImplementedError("cannot instantiate %r on your system" - % (cls.__name__,)) - return self + return cls._from_parts(args) def _make_child_relpath(self, part): # This is an optimization used for dir walking. `part` must be @@ -1261,9 +1257,19 @@ class PosixPath(Path, PurePosixPath): """ __slots__ = () + if os.name == 'nt': + def __new__(cls, *args, **kwargs): + raise NotImplementedError( + f"cannot instantiate {cls.__name__!r} on your system") + class WindowsPath(Path, PureWindowsPath): """Path subclass for Windows systems. On a Windows system, instantiating a Path should return this object. """ __slots__ = () + + if os.name != 'nt': + def __new__(cls, *args, **kwargs): + raise NotImplementedError( + f"cannot instantiate {cls.__name__!r} on your system") |