From 82fc553fe77fffe3c1af5f86c32349cae117b71a Mon Sep 17 00:00:00 2001 From: barneygale Date: Tue, 7 Feb 2023 20:38:34 +0000 Subject: [PATCH 1/4] GH-101362: Check pathlib.Path flavour compatibility at import time This saves a comparison in `pathlib.Path.__new__()` and reduces the time taken to run `Path()` by ~5% --- Lib/pathlib.py | 16 +++++++++++----- ...023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 17659bcd3e2d7f..f3ca6b6caab384 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -709,11 +709,7 @@ def __new__(cls, *args, **kwargs): 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 @@ -1258,9 +1254,19 @@ class PosixPath(Path, PurePosixPath): """ __slots__ = () + if posixpath is not os.path: + def __new__(cls, *args, **kwargs): + raise NotImplementedError("cannot instantiate %r on your system" + % (cls.__name__,)) + class WindowsPath(Path, PureWindowsPath): """Path subclass for Windows systems. On a Windows system, instantiating a Path should return this object. """ __slots__ = () + + if ntpath is not os.path: + def __new__(cls, *args, **kwargs): + raise NotImplementedError("cannot instantiate %r on your system" + % (cls.__name__,)) diff --git a/Misc/NEWS.d/next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst b/Misc/NEWS.d/next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst new file mode 100644 index 00000000000000..1dc168a4f9c10f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst @@ -0,0 +1,2 @@ +Speed up :cls:`pathlib.Path` construction by running the path flavour +compatibility check only when pathlib is imported. From a2bc779e9e69e280faa5e25b8482872321bb86df Mon Sep 17 00:00:00 2001 From: barneygale Date: Tue, 7 Feb 2023 21:24:01 +0000 Subject: [PATCH 2/4] Fix news blurb syntax --- .../next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst b/Misc/NEWS.d/next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst index 1dc168a4f9c10f..8421466cdbb3c9 100644 --- a/Misc/NEWS.d/next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst +++ b/Misc/NEWS.d/next/Library/2023-02-07-20-46-08.gh-issue-101362.2ckZ6R.rst @@ -1,2 +1,2 @@ -Speed up :cls:`pathlib.Path` construction by running the path flavour +Speed up :class:`pathlib.Path` construction by running the path flavour compatibility check only when pathlib is imported. From 1faf7e4eab40eb32d7b1ca1c57040288384bb682 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 25 Feb 2023 02:45:40 +0000 Subject: [PATCH 3/4] Switch comparisons to use `os.name`, not `os.path`. --- Lib/pathlib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index f3ca6b6caab384..a2d24aa65afb53 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1254,7 +1254,7 @@ class PosixPath(Path, PurePosixPath): """ __slots__ = () - if posixpath is not os.path: + if os.name == 'nt': def __new__(cls, *args, **kwargs): raise NotImplementedError("cannot instantiate %r on your system" % (cls.__name__,)) @@ -1266,7 +1266,7 @@ class WindowsPath(Path, PureWindowsPath): """ __slots__ = () - if ntpath is not os.path: + if os.name != 'nt': def __new__(cls, *args, **kwargs): raise NotImplementedError("cannot instantiate %r on your system" % (cls.__name__,)) From 84355edf278a2a19d336ec3b17654dd790de2a39 Mon Sep 17 00:00:00 2001 From: Barney Gale Date: Sun, 5 Mar 2023 22:23:24 +0000 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Alex Waygood --- Lib/pathlib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 402486258bfe9d..c37ff21c0352d8 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1259,8 +1259,8 @@ class PosixPath(Path, PurePosixPath): if os.name == 'nt': def __new__(cls, *args, **kwargs): - raise NotImplementedError("cannot instantiate %r on your system" - % (cls.__name__,)) + raise NotImplementedError( + f"cannot instantiate {cls.__name__!r} on your system") class WindowsPath(Path, PureWindowsPath): """Path subclass for Windows systems. @@ -1271,5 +1271,5 @@ class WindowsPath(Path, PureWindowsPath): if os.name != 'nt': def __new__(cls, *args, **kwargs): - raise NotImplementedError("cannot instantiate %r on your system" - % (cls.__name__,)) + raise NotImplementedError( + f"cannot instantiate {cls.__name__!r} on your system")