@@ -220,16 +220,16 @@ def test_ValueError(self):
220220 # Make sure ValueError is raised when match fails or format is bad
221221 self .assertRaises (ValueError , _strptime ._strptime_time , data_string = "%d" ,
222222 format = "%A" )
223- for bad_format in ("%" , "% " , "%e" ):
224- try :
223+ for bad_format in ("%" , "% " , "%\n " ):
224+ with self .assertRaisesRegex (ValueError , "stray % in format " ):
225+ _strptime ._strptime_time ("2005" , bad_format )
226+ for bad_format in ("%e" , "%Oe" , "%O" , "%O " , "%Ee" , "%E" , "%E " ,
227+ "%." , "%+" , "%_" , "%~" , "%\\ " ,
228+ "%O." , "%O+" , "%O_" , "%O~" , "%O\\ " ):
229+ directive = bad_format [1 :].rstrip ()
230+ with self .assertRaisesRegex (ValueError ,
231+ f"'{ re .escape (directive )} ' is a bad directive in format " ):
225232 _strptime ._strptime_time ("2005" , bad_format )
226- except ValueError :
227- continue
228- except Exception as err :
229- self .fail ("'%s' raised %s, not ValueError" %
230- (bad_format , err .__class__ .__name__ ))
231- else :
232- self .fail ("'%s' did not raise ValueError" % bad_format )
233233
234234 msg_week_no_year_or_weekday = r"ISO week directive '%V' must be used with " \
235235 r"the ISO year directive '%G' and a weekday directive " \
@@ -285,11 +285,11 @@ def test_strptime_exception_context(self):
285285 # check that this doesn't chain exceptions needlessly (see #17572)
286286 with self .assertRaises (ValueError ) as e :
287287 _strptime ._strptime_time ('' , '%D' )
288- self .assertIs (e .exception .__suppress_context__ , True )
289- # additional check for IndexError branch (issue #19545)
288+ self .assertTrue (e .exception .__suppress_context__ )
289+ # additional check for stray % branch
290290 with self .assertRaises (ValueError ) as e :
291- _strptime ._strptime_time ('19 ' , '%Y %' )
292- self .assertIsNone (e .exception .__context__ )
291+ _strptime ._strptime_time ('% ' , '%' )
292+ self .assertTrue (e .exception .__suppress_context__ )
293293
294294 def test_unconverteddata (self ):
295295 # Check ValueError is raised when there is unconverted data
0 commit comments