diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-05-19 16:27:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-19 16:27:12 (GMT) |
commit | 27b61c17a1d32d4773bb91f31c31e05a54b3b3df (patch) | |
tree | 2afb0e5f8381bb9e1c2e4ef192306787eb336665 /Lib | |
parent | fdc50bac16d3f8dc693488738009de8c86baf6e0 (diff) | |
download | cpython-27b61c17a1d32d4773bb91f31c31e05a54b3b3df.zip cpython-27b61c17a1d32d4773bb91f31c31e05a54b3b3df.tar.gz cpython-27b61c17a1d32d4773bb91f31c31e05a54b3b3df.tar.bz2 |
[3.13] GH-119113: Raise `TypeError` from `pathlib.PurePath.with_suffix(None)` (GH-119124) (#119183)
Restore behaviour from 3.12 when `path.with_suffix(None)` is called.
(cherry picked from commit 3c28510b984392b8dac87a17dfc5887366d5c4ab)
Co-authored-by: Barney Gale <barney.gale@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pathlib/_abc.py | 10 | ||||
-rw-r--r-- | Lib/test/test_pathlib/test_pathlib_abc.py | 4 |
2 files changed, 7 insertions, 7 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 06c10e8..9068452 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -225,15 +225,13 @@ class PurePathBase: string, remove the suffix from the path. """ stem = self.stem - if not suffix: - return self.with_name(stem) - elif not stem: + if not stem: # If the stem is empty, we can't make the suffix non-empty. raise ValueError(f"{self!r} has an empty name") - elif suffix.startswith('.') and len(suffix) > 1: - return self.with_name(stem + suffix) - else: + elif suffix and not (suffix.startswith('.') and len(suffix) > 1): raise ValueError(f"Invalid suffix {suffix!r}") + else: + return self.with_name(stem + suffix) def relative_to(self, other, *, walk_up=False): """Return the relative path to another path identified by the passed diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index aadecbc..d9e51c0 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -999,6 +999,7 @@ class DummyPurePathTest(unittest.TestCase): self.assertRaises(ValueError, P('c:a/b').with_suffix, 'c\\d') self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c/d') self.assertRaises(ValueError, P('c:a/b').with_suffix, '.c\\d') + self.assertRaises(TypeError, P('c:a/b').with_suffix, None) def test_with_suffix_empty(self): P = self.cls @@ -1006,7 +1007,7 @@ class DummyPurePathTest(unittest.TestCase): self.assertRaises(ValueError, P('').with_suffix, '.gz') self.assertRaises(ValueError, P('/').with_suffix, '.gz') - def test_with_suffix_seps(self): + def test_with_suffix_invalid(self): P = self.cls # Invalid suffix. self.assertRaises(ValueError, P('a/b').with_suffix, 'gz') @@ -1017,6 +1018,7 @@ class DummyPurePathTest(unittest.TestCase): self.assertRaises(ValueError, P('a/b').with_suffix, '.c/.d') self.assertRaises(ValueError, P('a/b').with_suffix, './.d') self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.') + self.assertRaises(TypeError, P('a/b').with_suffix, None) def test_relative_to_common(self): P = self.cls |