pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/574291963f6b0eb7da3fde1ae9763236e7ece306

5097560d244c08.css" /> pathlib ABCs: drop partial, broken, untested support for `bytes` path… · python/cpython@5742919 · GitHub
Skip to content

Commit 5742919

Browse files
authored
pathlib ABCs: drop partial, broken, untested support for bytes paths. (#114777)
Methods like `full_match()`, `glob()`, etc, are difficult to make work with byte paths, and it's not worth the effort. This patch makes `PurePathBase` raise `TypeError` when given non-`str` path segments.
1 parent 1667c28 commit 5742919

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

Lib/pathlib/_abc.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ class PurePathBase:
207207

208208
def __init__(self, path, *paths):
209209
self._raw_path = self.pathmod.join(path, *paths) if paths else path
210+
if not isinstance(self._raw_path, str):
211+
raise TypeError(
212+
f"path should be a str, not {type(self._raw_path).__name__!r}")
210213
self._resolving = False
211214

212215
def with_segments(self, *pathsegments):
@@ -321,8 +324,6 @@ def relative_to(self, other, *, walk_up=False):
321324
other = self.with_segments(other)
322325
anchor0, parts0 = self._stack
323326
anchor1, parts1 = other._stack
324-
if isinstance(anchor0, str) != isinstance(anchor1, str):
325-
raise TypeError(f"{self._raw_path!r} and {other._raw_path!r} have different types")
326327
if anchor0 != anchor1:
327328
raise ValueError(f"{self._raw_path!r} and {other._raw_path!r} have different anchors")
328329
while parts0 and parts1 and parts0[-1] == parts1[-1]:
@@ -346,8 +347,6 @@ def is_relative_to(self, other):
346347
other = self.with_segments(other)
347348
anchor0, parts0 = self._stack
348349
anchor1, parts1 = other._stack
349-
if isinstance(anchor0, str) != isinstance(anchor1, str):
350-
raise TypeError(f"{self._raw_path!r} and {other._raw_path!r} have different types")
351350
if anchor0 != anchor1:
352351
return False
353352
while parts0 and parts1 and parts0[-1] == parts1[-1]:

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def test_fspath_common(self):
189189
self._check_str(p.__fspath__(), ('a/b',))
190190
self._check_str(os.fspath(p), ('a/b',))
191191

192-
def test_bytes(self):
192+
def test_bytes_exc_message(self):
193193
P = self.cls
194194
message = (r"argument should be a str or an os\.PathLike object "
195195
r"where __fspath__ returns a str, not 'bytes'")
@@ -199,22 +199,6 @@ def test_bytes(self):
199199
P(b'a', 'b')
200200
with self.assertRaisesRegex(TypeError, message):
201201
P('a', b'b')
202-
with self.assertRaises(TypeError):
203-
P('a').joinpath(b'b')
204-
with self.assertRaises(TypeError):
205-
P('a') / b'b'
206-
with self.assertRaises(TypeError):
207-
b'a' / P('b')
208-
with self.assertRaises(TypeError):
209-
P('a').match(b'b')
210-
with self.assertRaises(TypeError):
211-
P('a').relative_to(b'b')
212-
with self.assertRaises(TypeError):
213-
P('a').with_name(b'b')
214-
with self.assertRaises(TypeError):
215-
P('a').with_stem(b'b')
216-
with self.assertRaises(TypeError):
217-
P('a').with_suffix(b'b')
218202

219203
def test_as_bytes_common(self):
220204
sep = os.fsencode(self.sep)

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,31 @@ def test_constructor_common(self):
155155
P('a/b/c')
156156
P('/a/b/c')
157157

158+
def test_bytes(self):
159+
P = self.cls
160+
with self.assertRaises(TypeError):
161+
P(b'a')
162+
with self.assertRaises(TypeError):
163+
P(b'a', 'b')
164+
with self.assertRaises(TypeError):
165+
P('a', b'b')
166+
with self.assertRaises(TypeError):
167+
P('a').joinpath(b'b')
168+
with self.assertRaises(TypeError):
169+
P('a') / b'b'
170+
with self.assertRaises(TypeError):
171+
b'a' / P('b')
172+
with self.assertRaises(TypeError):
173+
P('a').match(b'b')
174+
with self.assertRaises(TypeError):
175+
P('a').relative_to(b'b')
176+
with self.assertRaises(TypeError):
177+
P('a').with_name(b'b')
178+
with self.assertRaises(TypeError):
179+
P('a').with_stem(b'b')
180+
with self.assertRaises(TypeError):
181+
P('a').with_suffix(b'b')
182+
158183
def _check_str_subclass(self, *args):
159184
# Issue #21127: it should be possible to construct a PurePath object
160185
# from a str subclass instance, and it then gets converted to

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy