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