@@ -224,25 +224,49 @@ def test_ValueError(self):
224224 else :
225225 self .fail ("'%s' did not raise ValueError" % bad_format )
226226
227- # Ambiguous or incomplete cases using ISO year/week/weekday directives
228- # 1. ISO week (%V) is specified, but the year is specified with %Y
229- # instead of %G
230- with self .assertRaises (ValueError ):
231- _strptime ._strptime ("1999 50" , "%Y %V" )
232- # 2. ISO year (%G) and ISO week (%V) are specified, but weekday is not
233- with self .assertRaises (ValueError ):
234- _strptime ._strptime ("1999 51" , "%G %V" )
235- # 3. ISO year (%G) and weekday are specified, but ISO week (%V) is not
236- for w in ('A' , 'a' , 'w' , 'u' ):
237- with self .assertRaises (ValueError ):
238- _strptime ._strptime ("1999 51" ,"%G %{}" .format (w ))
239- # 4. ISO year is specified alone (e.g. time.strptime('2015', '%G'))
240- with self .assertRaises (ValueError ):
241- _strptime ._strptime ("2015" , "%G" )
242- # 5. Julian/ordinal day (%j) is specified with %G, but not %Y
243- with self .assertRaises (ValueError ):
244- _strptime ._strptime ("1999 256" , "%G %j" )
227+ msg_week_no_year_or_weekday = r"ISO week directive '%V' must be used with " \
228+ r"the ISO year directive '%G' and a weekday directive " \
229+ r"\('%A', '%a', '%w', or '%u'\)."
230+ msg_week_not_compatible = r"ISO week directive '%V' is incompatible with " \
231+ r"the year directive '%Y'. Use the ISO year '%G' instead."
232+ msg_julian_not_compatible = r"Day of the year directive '%j' is not " \
233+ r"compatible with ISO year directive '%G'. Use '%Y' instead."
234+ msg_year_no_week_or_weekday = r"ISO year directive '%G' must be used with " \
235+ r"the ISO week directive '%V' and a weekday directive " \
236+ r"\('%A', '%a', '%w', or '%u'\)."
237+
238+ locale_time = _strptime .LocaleTime ()
245239
240+ # Ambiguous or incomplete cases using ISO year/week/weekday directives
241+ subtests = [
242+ # 1. ISO week (%V) is specified, but the year is specified with %Y
243+ # instead of %G
244+ ("1999 50" , "%Y %V" , msg_week_no_year_or_weekday ),
245+ ("1999 50 5" , "%Y %V %u" , msg_week_not_compatible ),
246+ # 2. ISO year (%G) and ISO week (%V) are specified, but weekday is not
247+ ("1999 51" , "%G %V" , msg_year_no_week_or_weekday ),
248+ # 3. ISO year (%G) and weekday are specified, but ISO week (%V) is not
249+ ("1999 {}" .format (locale_time .f_weekday [5 ]), "%G %A" ,
250+ msg_year_no_week_or_weekday ),
251+ ("1999 {}" .format (locale_time .a_weekday [5 ]), "%G %a" ,
252+ msg_year_no_week_or_weekday ),
253+ ("1999 5" , "%G %w" , msg_year_no_week_or_weekday ),
254+ ("1999 5" , "%G %u" , msg_year_no_week_or_weekday ),
255+ # 4. ISO year is specified alone (e.g. time.strptime('2015', '%G'))
256+ ("2015" , "%G" , msg_year_no_week_or_weekday ),
257+ # 5. Julian/ordinal day (%j) is specified with %G, but not %Y
258+ ("1999 256" , "%G %j" , msg_julian_not_compatible ),
259+ ("1999 50 5 256" , "%G %V %u %j" , msg_julian_not_compatible ),
260+ # ISO week specified alone
261+ ("50" , "%V" , msg_week_no_year_or_weekday ),
262+ # ISO year is unspecified, falling back to year
263+ ("50 5" , "%V %u" , msg_week_no_year_or_weekday ),
264+ ]
265+
266+ for (data_string , format , message ) in subtests :
267+ with self .subTest (data_string = data_string , format = format ):
268+ with self .assertRaisesRegex (ValueError , message ):
269+ _strptime ._strptime (data_string , format )
246270
247271 def test_strptime_exception_context (self ):
248272 # check that this doesn't chain exceptions needlessly (see #17572)
0 commit comments