From 5af2a9680e2102c51e465ca335d7eb0047c8bbb7 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 29 May 2023 21:48:11 +0100 Subject: [PATCH 1/2] GH-104898: Revert pathlib os.PathLike registration change. Subclassing `os.PathLike` rather than using `register()` makes initialisation slower, due to the additional `__isinstance__` work. This partially reverts commit bd1b6228d132b8e9836fe352cd8dca2b6c1bd98c. --- Lib/pathlib.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 87c2e970a0a881..4b7206962f7559 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -235,7 +235,7 @@ def __repr__(self): return "<{}.parents>".format(type(self._path).__name__) -class PurePath(os.PathLike): +class PurePath: """Base class for manipulating paths without I/O. PurePath represents a filesystem path and offers operations which @@ -715,6 +715,9 @@ def match(self, path_pattern, *, case_sensitive=None): return False return True +# Subclassing os.PathLike makes __instancecheck__ slower. Register instead! +os.PathLike.register(PurePath) + class PurePosixPath(PurePath): """PurePath subclass for non-Windows systems. From 0e89d55e3d26a989dd4f842b8edbf2e8e228b0c2 Mon Sep 17 00:00:00 2001 From: Barney Gale Date: Mon, 29 May 2023 22:18:20 +0100 Subject: [PATCH 2/2] Update Lib/pathlib.py Co-authored-by: Alex Waygood --- Lib/pathlib.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 4b7206962f7559..a57b582a211e06 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -715,7 +715,8 @@ def match(self, path_pattern, *, case_sensitive=None): return False return True -# Subclassing os.PathLike makes __instancecheck__ slower. Register instead! +# Subclassing os.PathLike makes isinstance() checks slower, +# which in turn makes Path construction slower. Register instead! os.PathLike.register(PurePath)