8000 Revert "GH-100502: Add `pathlib.PurePath.pathmod` attribute (GH-106533)" · barneygale/cpython@cfa2e96 · GitHub
[go: up one dir, main page]

Skip to content

Commit cfa2e96

Browse files
committed
Revert "pythonGH-100502: Add pathlib.PurePath.pathmod attribute (pythonGH-106533)"
This reverts commit c6c5665.
1 parent 2f0ec7f commit cfa2e96

File tree

6 files changed

+42
-46
lines changed

6 files changed

+42
-46
lines changed

Doc/library/pathlib.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,6 @@ Methods and properties
303303

304304
Pure paths provide the following methods and properties:
305305

306-
.. attribute:: PurePath.pathmod
307-
308-
The implementation of the :mod:`os.path` module used for low-level path
309-
operations: either :mod:`posixpath` or :mod:`ntpath`.
310-
311-
.. versionadded:: 3.13
312-
313306
.. attribute:: PurePath.drive
314307

315308
A string representing the drive letter or name, if any::

Lib/pathlib/__init__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def __init__(self, *args):
7474
paths = []
7575
for arg in args:
7676
if isinstance(arg, PurePath):
77-
if arg.pathmod is ntpath and self.pathmod is posixpath:
77+
if arg._pathmod is ntpath and self._pathmod is posixpath:
7878
# GH-103631: Convert separators for backwards compatibility.
7979
paths.extend(path.replace('\\', '/') for path in arg._raw_paths)
8080
else:
@@ -113,7 +113,7 @@ def _str_normcase(self):
113113
try:
114114
return self._str_normcase_cached
115115
except AttributeError:
116-
if _abc._is_case_sensitive(self.pathmod):
116+
if _abc._is_case_sensitive(self._pathmod):
117117
self._str_normcase_cached = str(self)
118118
else:
119119
self._str_normcase_cached = str(self).lower()
@@ -129,34 +129,34 @@ def __hash__(self):
129129
def __eq__(self, other):
130130
if not isinstance(other, PurePath):
131131
return NotImplemented
132-
return self._str_normcase == other._str_normcase and self.pathmod is other.pathmod
132+
return self._str_normcase == other._str_normcase and self._pathmod is other._pathmod
133133

134134
@property
135135
def _parts_normcase(self):
136136
# Cached parts with normalized case, for comparisons.
137137
try:
138138
return self._parts_normcase_cached
139139
except AttributeError:
140-
self._parts_normcase_cached = self._str_normcase.split(self.pathmod.sep)
140+
self._parts_normcase_cached = self._str_normcase.split(self._pathmod.sep)
141141
return self._parts_normcase_cached
142142

143143
def __lt__(self, other):
144-
if not isinstance(other, PurePath) or self.pathmod is not other.pathmod:
144+
if not isinstance(other, PurePath) or self._pathmod is not other._pathmod:
145145
return NotImplemented
146146
return self._parts_normcase < other._parts_normcase
147147

148148
def __le__(self, other):
149-
if not isinstance(other, PurePath) or self.pathmod is not other.pathmod:
149+
if not isinstance(other, PurePath) or self._pathmod is not other._pathmod:
150150
return NotImplemented
151151
return self._parts_normcase <= other._parts_normcase
152152

153153
def __gt__(self, other):
154-
if not isinstance(other, PurePath) or self.pathmod is not other.pathmod:
154+
if not isinstance(other, PurePath) or self._pathmod is not other._pathmod:
155155
return NotImplemented
156156
return self._parts_normcase > other._parts_normcase
157157

158158
def __ge__(self, other):
159-
if not isinstance(other, PurePath) or self.pathmod is not other.pathmod:
159+
if not isinstance(other, PurePath) or self._pathmod is not other._pathmod:
160160
return NotImplemented
161161
return self._parts_normcase >= other._parts_normcase
162162

@@ -193,7 +193,7 @@ class PurePosixPath(PurePath):
193193
On a POSIX system, instantiating a PurePath should return this object.
194194
However, you can also instantiate it directly on any system.
195195
"""
196-
pathmod = posixpath
196+
_pathmod = posixpath
197197
__slots__ = ()
198198

199199

@@ -203,7 +203,7 @@ class PureWindowsPath(PurePath):
203203
On a Windows system, instantiating a PurePath should return this object.
204204
However, you can also instantiate it directly on any system.
205205
"""
206-
pathmod = ntpath
206+
_pathmod = ntpath
207207
__slots__ = ()
208208

209209

@@ -305,7 +305,7 @@ def absolute(self):
305305
drive, root, rel = os.path.splitroot(cwd)
306306
if not rel:
307307
return self._from_parsed_parts(drive, root, self._tail)
308-
tail = rel.split(self.pathmod.sep)
308+
tail = rel.split(self._pathmod.sep)
309309
tail.extend(self._tail)
310310
return self._from_parsed_parts(drive, root, tail)
311311

Lib/pathlib/_abc.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class PurePathBase:
204204
# work from occurring when `resolve()` calls `stat()` or `readlink()`.
205205
'_resolving',
206206
)
207-
pathmod = os.path
207+
_pathmod = os.path
208208

209209
def __init__(self, *paths):
210210
self._raw_paths = paths
@@ -221,11 +221,11 @@ def with_segments(self, *pathsegments):
221221
def _parse_path(cls, path):
222222
if not path:
223223
return '', '', []
224-
sep = cls.pathmod.sep
225-
altsep = cls.pathmod.altsep
224+
sep = cls._pathmod.sep
225+
altsep = cls._pathmod.altsep
226226
if altsep:
227227
path = path.replace(altsep, sep)
228-
drv, root, rel = cls.pathmod.splitroot(path)
228+
drv, root, rel = cls._pathmod.splitroot(path)
229229
if not root and drv.startswith(sep) and not drv.endswith(sep):
230230
drv_parts = drv.split(sep)
231231
if len(drv_parts) == 4 and drv_parts[2] not in '?.':
@@ -244,7 +244,7 @@ def _load_parts(self):
244244
elif len(paths) == 1:
245245
path = paths[0]
246246
else:
247-
path = self.pathmod.join(*paths)
247+
path = self._pathmod.join(*paths)
248248
drv, root, tail = self._parse_path(path)
249249
self._drv = drv
250250
self._root = root
@@ -262,10 +262,10 @@ def _from_parsed_parts(self, drv, root, tail):
262262
@classmethod
263263
def _format_parsed_parts(cls, drv, root, tail):
264264
if drv or root:
265-
return drv + root + cls.pathmod.sep.join(tail)
266-
elif tail and cls.pathmod.splitdrive(tail[0])[0]:
265+
return drv + root + cls._pathmod.sep.join(tail)
266+
elif tail and cls._pathmod.splitdrive(tail[0])[0]:
267267
tail = ['.'] + tail
268-
return cls.pathmod.sep.join(tail)
268+
return cls._pathmod.sep.join(tail)
269269

270270
def __str__(self):
271271
"""Return the string representation of the path, suitable for
@@ -280,7 +280,7 @@ def __str__(self):
280280
def as_posix(self):
281281
"""Return the string representation of the path with forward (/)
282282
slashes."""
283-
return str(self).replace(self.pathmod.sep, '/')
283+
return str(self).replace(self._pathmod.sep, '/')
284284

285285
def __repr__(self):
286286
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
@@ -364,7 +364,7 @@ def stem(self):
364364

365365
def with_name(self, name):
366366
"""Return a new path with the file name changed."""
367-
m = self.pathmod
367+
m = self._pathmod
368368
if not name or m.sep in name or (m.altsep and m.altsep in name) or name == '.':
369369
raise ValueError(f"Invalid name {name!r}")
370370
tail = self._tail.copy()
@@ -483,22 +483,22 @@ def parents(self):
483483
def is_absolute(self):
484484
"""True if the path is absolute (has both a root and, if applicable,
485485
a drive)."""
486-
if self.pathmod is ntpath:
486+
if self._pathmod is ntpath:
487487
# ntpath.isabs() is defective - see GH-44626.
488488
return bool(self.drive and self.root)
489-
elif self.pathmod is posixpath:
489+
elif self._pathmod is posixpath:
490490
# Optimization: work with raw paths on POSIX.
491491
for path in self._raw_paths:
492492
if path.startswith('/'):
493493
return True
494494
return False
495495
else:
496-
return self.pathmod.isabs(str(self))
496+
return self._pathmod.isabs(str(self))
497497

498498
def is_reserved(self):
499499
"""Return True if the path contains one of the special names reserved
500500
by the system, if any."""
501-
if self.pathmod is posixpath or not self._tail:
501+
if self._pathmod is posixpath or not self._tail:
502502
return False
503503

504504
# NOTE: the rules for reserved names seem somewhat complicated
@@ -518,8 +518,8 @@ def match(self, path_pattern, *, case_sensitive=None):
518518
if not isinstance(path_pattern, PurePathBase):
519519
path_pattern = self.with_segments(path_pattern)
520520
if case_sensitive is None:
521-
case_sensitive = _is_case_sensitive(self.pathmod)
522-
sep = path_pattern.pathmod.sep
521+
case_sensitive = _is_case_sensitive(self._pathmod)
522+
sep = path_pattern._pathmod.sep
523523
pattern_str = str(path_pattern)
524524
if path_pattern.drive or path_pattern.root:
525525
pass
@@ -801,7 +801,7 @@ def _make_child_relpath(self, name):
801801
path_str = str(self)
802802
tail = self._tail
803803
if tail:
804-
path_str = f'{path_str}{self.pathmod.sep}{name}'
804+
path_str = f'{path_str}{self._pathmod.sep}{name}'
805805
elif path_str != '.':
806806
path_str = f'{path_str}{name}'
807807
else:
@@ -836,7 +836,7 @@ def _glob(self, pattern, case_sensitive, follow_symlinks):
836836
raise ValueError("Unacceptable pattern: {!r}".format(pattern))
837837

838838
pattern_parts = path_pattern._tail.copy()
839-
if pattern[-1] in (self.pathmod.sep, self.pathmod.altsep):
839+
if pattern[-1] in (self._pathmod.sep, self._pathmod.altsep):
840840
# GH-65238: pathlib doesn't preserve trailing slash. Add it back.
841841
pattern_parts.append('')
842842
if pattern_parts[-1] == '**':
@@ -850,7 +850,7 @@ def _glob(self, pattern, case_sensitive, follow_symlinks):
850850

851851
if case_sensitive is None:
852852
# TODO: evaluate case-sensitivity of each directory in _select_children().
853-
case_sensitive = _is_case_sensitive(self.pathmod)
853+
case_sensitive = _is_case_sensitive(self._pathmod)
854854

855855
# If symlinks are handled consistently, and the pattern does not
856856
# contain '..' components, then we can use a 'walk-and-match' strategy
@@ -861,7 +861,7 @@ def _glob(self, pattern, case_sensitive, follow_symlinks):
861861
# do not perform any filesystem access, which can be much faster!
862862
filter_paths = follow_symlinks is not None and '..' not in pattern_parts
863863
deduplicate_paths = False
864-
sep = self.pathmod.sep
864+
sep = self._pathmod.sep
865865
paths = iter([self] if self.is_dir() else [])
866866
part_idx = 0
867867
while part_idx < len(pattern_parts):

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ def test_concrete_class(self):
953953
self.assertIs(type(p), expected)
954954

955955
def test_unsupported_pathmod(self):
956-
if self.cls.pathmod is os.path:
956+
if self.cls._pathmod is os.path:
957957
self.skipTest("path flavour is supported")
958958
else:
959959
self.assertRaises(pathlib.UnsupportedOperation, self.cls)
@@ -1456,9 +1456,9 @@ def test_symlink_to_unsupported(self):
14561456
def test_is_junction(self):
14571457
P = self.cls(self.base)
14581458

1459-
with mock.patch.object(P.pathmod, 'isjunction'):
1460-
self.assertEqual(P.is_junction(), P.pathmod.isjunction.return_value)
1461-
P.pathmod.isjunction.assert_called_once_with(P)
1459+
with mock.patch.object(P._pathmod, 'isjunction'):
1460+
self.assertEqual(P.is_junction(), P._pathmod.isjunction.return_value)
1461+
P._pathmod.isjunction.assert_called_once_with(P)
14621462

14631463
@unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required")
14641464
@unittest.skipIf(sys.platform == "vxworks",

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class DummyPurePathTest(unittest.TestCase):
7373

7474
def setUp(self):
7575
p = self.cls('a')
76-
self.pathmod = p.pathmod
76+
self.pathmod = p._pathmod
7777
self.sep = self.pathmod.sep
7878
self.altsep = self.pathmod.altsep
7979

@@ -96,15 +96,15 @@ def test_concrete_class(self):
9696

9797
def test_different_pathmods_unequal(self):
9898
p = self.cls('a')
99-
if p.pathmod is posixpath:
99+
if p._pathmod is posixpath:
100100
q = pathlib.PureWindowsPath('a')
101101
else:
102102
q = pathlib.PurePosixPath('a')
103103
self.assertNotEqual(p, q)
104104

105105
def test_different_pathmods_unordered(self):
106106
p = self.cls('a')
107-
if p.pathmod is posixpath:
107+
if p._pathmod is posixpath:
108108
q = pathlib.PureWindowsPath('a')
109109
else:
110110
q = pathlib.PurePosixPath('a')
@@ -874,7 +874,7 @@ class DummyPathTest(DummyPurePathTest):
874874

875875
def setUp(self):
876876
super().setUp()
877-
pathmod = self.cls.pathmod
877+
pathmod = self.cls._pathmod
878878
p = self.cls(self.base)
879879
p.mkdir(parents=True)
880880
p.joinpath('dirA').mkdir()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Remove ``pathlib.PurePath.pathmod`` class attribute, which was added earlier
2+
in Python 3.13 development. This attribute may not be the best way to
3+
publicly represent differences in path flavours.

0 commit comments

Comments
 (0)
0