File tree 3 files changed +7
-12
lines changed 3 files changed +7
-12
lines changed Original file line number Diff line number Diff line change @@ -299,10 +299,13 @@ def with_suffix(self, suffix):
299
299
has no suffix, add given suffix. If the given suffix is an empty
300
300
string, remove the suffix from the path.
301
301
"""
302
+ stem = self .stem
302
303
if not suffix :
303
- return self .with_name (self .stem )
304
+ return self .with_name (stem )
305
+ elif not stem :
306
+ raise ValueError (f"{ self !r} has an empty name" )
304
307
elif suffix .startswith ('.' ) and len (suffix ) > 1 :
305
- return self .with_name (self . stem + suffix )
308
+ return self .with_name (stem + suffix )
306
309
else :
307
310
raise ValueError (f"Invalid suffix { suffix !r} " )
308
311
Original file line number Diff line number Diff line change @@ -327,13 +327,6 @@ def test_with_stem_empty(self):
327
327
self .assertRaises (ValueError , P ('a/b' ).with_stem , '' )
328
328
self .assertRaises (ValueError , P ('a/b' ).with_stem , '.' )
329
329
330
- def test_with_suffix_empty (self ):
331
- # Path doesn't have a "filename" component.
332
- P = self .cls
333
- self .assertRaises (ValueError , P ('' ).with_suffix , '.gz' )
334
- self .assertRaises (ValueError , P ('.' ).with_suffix , '.gz' )
335
- self .assertRaises (ValueError , P ('/' ).with_suffix , '.gz' )
336
-
337
330
def test_relative_to_several_args (self ):
338
331
P = self .cls
339
332
p = P ('a/b' )
Original file line number Diff line number Diff line change @@ -977,9 +977,8 @@ def test_with_suffix_windows(self):
977
977
def test_with_suffix_empty (self ):
978
978
P = self .cls
979
979
# Path doesn't have a "filename" component.
980
- self .assertEqual (P ('' ).with_suffix ('.gz' ), P ('.gz' ))
981
- self .assertEqual (P ('.' ).with_suffix ('.gz' ), P ('..gz' ))
982
- self .assertEqual (P ('/' ).with_suffix ('.gz' ), P ('/.gz' ))
980
+ self .assertRaises (ValueError , P ('' ).with_suffix , '.gz' )
981
+ self .assertRaises (ValueError , P ('/' ).with_suffix , '.gz' )
983
982
984
983
def test_with_suffix_seps (self ):
985
984
P = self .cls
You can’t perform that action at this time.
0 commit comments