Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-101362: Check pathlib.Path flavour compatibility at import time #101664

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,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
barneygale marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -1261,9 +1257,19 @@ class PosixPath(Path, PurePosixPath):
"""
__slots__ = ()

if os.name == 'nt':
def __new__(cls, *args, **kwargs):
raise NotImplementedError("cannot instantiate %r on your system"
% (cls.__name__,))
barneygale marked this conversation as resolved.
Show resolved Hide resolved

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("cannot instantiate %r on your system"
% (cls.__name__,))
barneygale marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up :class:`pathlib.Path` construction by running the path flavour
compatibility check only when pathlib is imported.