Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-114610: Fixpathlib._abc.PurePathBase.with_suffix('.ext') handling of stems#114613

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
barneygale merged 3 commits intopython:mainfrombarneygale:gh-114610-abc
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
GH-114610: Fixpathlib._abc.PurePathBase.with_suffix('.ext') handli…
…ng of stemsRaise `ValueError` if `with_suffix('.ext')` is called on a path without astem. Paths may only have a non-empty suffix if they also have a non-emptystem.ABC-only bugfix; no effect on public classes.
  • Loading branch information
@barneygale
barneygale committedJan 26, 2024
commit043c64d43148ae0248907502cf09a4c5638b95e9
7 changes: 5 additions & 2 deletionsLib/pathlib/_abc.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -299,10 +299,13 @@ def with_suffix(self, suffix):
has no suffix, add given suffix. If the given suffix is an empty
string, remove the suffix from the path.
"""
stem = self.stem
if not suffix:
return self.with_name(self.stem)
return self.with_name(stem)
elif not stem:
raise ValueError(f"{self!r} has an empty name")
elif suffix.startswith('.') and len(suffix) > 1:
return self.with_name(self.stem + suffix)
return self.with_name(stem + suffix)
else:
raise ValueError(f"Invalid suffix {suffix!r}")

Expand Down
7 changes: 0 additions & 7 deletionsLib/test/test_pathlib/test_pathlib.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -329,13 +329,6 @@ def test_with_stem_empty(self):
self.assertRaises(ValueError, P('a/b').with_stem, '')
self.assertRaises(ValueError, P('a/b').with_stem, '.')

def test_with_suffix_empty(self):
# Path doesn't have a "filename" component.
P = self.cls
self.assertRaises(ValueError, P('').with_suffix, '.gz')
self.assertRaises(ValueError, P('.').with_suffix, '.gz')
self.assertRaises(ValueError, P('/').with_suffix, '.gz')

def test_relative_to_several_args(self):
P = self.cls
p = P('a/b')
Expand Down
11 changes: 2 additions & 9 deletionsLib/test/test_pathlib/test_pathlib_abc.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -556,16 +556,9 @@ def test_with_suffix_common(self):
# Stripping suffix.
self.assertEqual(P('a/b.py').with_suffix(''), P('a/b'))
self.assertEqual(P('/a/b').with_suffix(''), P('/a/b'))

def test_with_suffix_empty(self):
P = self.cls
# Path doesn't have a "filename" component.
self.assertEqual(P('').with_suffix('.gz'), P('.gz'))
self.assertEqual(P('.').with_suffix('.gz'), P('..gz'))
self.assertEqual(P('/').with_suffix('.gz'), P('/.gz'))

def test_with_suffix_seps(self):
P = self.cls
self.assertRaises(ValueError, P('').with_suffix, '.gz')
self.assertRaises(ValueError, P('/').with_suffix, '.gz')
# Invalid suffix.
self.assertRaises(ValueError, P('a/b').with_suffix, 'gz')
self.assertRaises(ValueError, P('a/b').with_suffix, '/')
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp