Skip to content

Commit abead54

Browse files
authored
gh-117655: Prevent test_strptime from raising a DeprecationWarning (GH-117668)
* Fix `test_strptime` raises a DeprecationWarning * Ignore deprecation warnings where appropriate. * Update Lib/test/datetimetester.py This is follow on work to silence unnecessary warnings from the test suite that changes for #70647 added.
1 parent cd4cfa6 commit abead54

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

Lib/test/datetimetester.py

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from test import support
2424
from test.support import is_resource_enabled, ALWAYS_EQ, LARGEST, SMALLEST
25+
from test.support import warnings_helper
2526

2627
import datetime as datetime_module
2728
from datetime import MINYEAR, MAXYEAR
@@ -2797,6 +2798,7 @@ def test_strptime_single_digit(self):
27972798
newdate = strptime(string, format)
27982799
self.assertEqual(newdate, target, msg=reason)
27992800

2801+
@warnings_helper.ignore_warnings(category=DeprecationWarning)
28002802
def test_strptime_leap_year(self):
28012803
# GH-70647: warns if parsing a format with a day and no year.
28022804
with self.assertRaises(ValueError):

Lib/test/test_strptime.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
import sys
99
from test import support
10-
from test.support import skip_if_buggy_ucrt_strfptime
10+
from test.support import skip_if_buggy_ucrt_strfptime, warnings_helper
1111
from datetime import date as datetime_date
1212

1313
import _strptime
@@ -120,7 +120,7 @@ def setUp(self):
120120

121121
def test_pattern(self):
122122
# Test TimeRE.pattern
123-
pattern_string = self.time_re.pattern(r"%a %A %d")
123+
pattern_string = self.time_re.pattern(r"%a %A %d %Y")
124124
self.assertTrue(pattern_string.find(self.locale_time.a_weekday[2]) != -1,
125125
"did not find abbreviated weekday in pattern string '%s'" %
126126
pattern_string)
@@ -160,10 +160,11 @@ def test_compile(self):
160160
found.group('b')))
161161
for directive in ('a','A','b','B','c','d','G','H','I','j','m','M','p',
162162
'S','u','U','V','w','W','x','X','y','Y','Z','%'):
163-
compiled = self.time_re.compile("%" + directive)
164-
found = compiled.match(time.strftime("%" + directive))
163+
fmt = "%d %Y" if directive == 'd' else "%" + directive
164+
compiled = self.time_re.compile(fmt)
165+
found = compiled.match(time.strftime(fmt))
165166
self.assertTrue(found, "Matching failed on '%s' using '%s' regex" %
166-
(time.strftime("%" + directive),
167+
(time.strftime(fmt),
167168
compiled.pattern))
168169

169170
def test_blankpattern(self):
@@ -290,8 +291,9 @@ def test_unconverteddata(self):
290291

291292
def helper(self, directive, position):
292293
"""Helper fxn in testing."""
293-
strf_output = time.strftime("%" + directive, self.time_tuple)
294-
strp_output = _strptime._strptime_time(strf_output, "%" + directive)
294+
fmt = "%d %Y" if directive == 'd' else "%" + directive
295+
strf_output = time.strftime(fmt, self.time_tuple)
296+
strp_output = _strptime._strptime_time(strf_output, fmt)
295297
self.assertTrue(strp_output[position] == self.time_tuple[position],
296298
"testing of '%s' directive failed; '%s' -> %s != %s" %
297299
(directive, strf_output, strp_output[position],
@@ -497,9 +499,11 @@ def test_escaping(self):
497499
need_escaping = r".^$*+?{}\[]|)("
498500
self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping))
499501

502+
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-70647
500503
def test_feb29_on_leap_year_without_year(self):
501504
time.strptime("Feb 29", "%b %d")
502505

506+
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-70647
503507
def test_mar1_comes_after_feb29_even_when_omitting_the_year(self):
504508
self.assertLess(
505509
time.strptime("Feb 29", "%b %d"),
@@ -679,33 +683,33 @@ class CacheTests(unittest.TestCase):
679683
def test_time_re_recreation(self):
680684
# Make sure cache is recreated when current locale does not match what
681685
# cached object was created with.
682-
_strptime._strptime_time("10", "%d")
686+
_strptime._strptime_time("10 2004", "%d %Y")
683687
_strptime._strptime_time("2005", "%Y")
684688
_strptime._TimeRE_cache.locale_time.lang = "Ni"
685689
original_time_re = _strptime._TimeRE_cache
686-
_strptime._strptime_time("10", "%d")
690+
_strptime._strptime_time("10 2004", "%d %Y")
687691
self.assertIsNot(original_time_re, _strptime._TimeRE_cache)
688692
self.assertEqual(len(_strptime._regex_cache), 1)
689693

690694
def test_regex_cleanup(self):
691695
# Make sure cached regexes are discarded when cache becomes "full".
692696
try:
693-
del _strptime._regex_cache['%d']
697+
del _strptime._regex_cache['%d %Y']
694698
except KeyError:
695699
pass
696700
bogus_key = 0
697701
while len(_strptime._regex_cache) <= _strptime._CACHE_MAX_SIZE:
698702
_strptime._regex_cache[bogus_key] = None
699703
bogus_key += 1
700-
_strptime._strptime_time("10", "%d")
704+
_strptime._strptime_time("10 2004", "%d %Y")
701705
self.assertEqual(len(_strptime._regex_cache), 1)
702706

703707
def test_new_localetime(self):
704708
# A new LocaleTime instance should be created when a new TimeRE object
705709
# is created.
706710
locale_time_id = _strptime._TimeRE_cache.locale_time
707711
_strptime._TimeRE_cache.locale_time.lang = "Ni"
708-
_strptime._strptime_time("10", "%d")
712+
_strptime._strptime_time("10 2004", "%d %Y")
709713
self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time)
710714

711715
def test_TimeRE_recreation_locale(self):
@@ -716,13 +720,13 @@ def test_TimeRE_recreation_locale(self):
716720
except locale.Error:
717721
self.skipTest('test needs en_US.UTF8 locale')
718722
try:
719-
_strptime._strptime_time('10', '%d')
723+
_strptime._strptime_time('10 2004', '%d %Y')
720724
# Get id of current cache object.
721725
first_time_re = _strptime._TimeRE_cache
722726
try:
723727
# Change the locale and force a recreation of the cache.
724728
locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8'))
725-
_strptime._strptime_time('10', '%d')
729+
_strptime._strptime_time('10 2004', '%d %Y')
726730
# Get the new cache object's id.
727731
second_time_re = _strptime._TimeRE_cache
728732
# They should not be equal.

0 commit comments

Comments
 (0)