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 https://github.com/python/cpython/issues/70647 added.
This commit is contained in:
Nice Zombies 2024-05-11 23:46:07 +02:00 committed by GitHub
parent cd4cfa6ed2
commit abead548af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 14 deletions

View File

@ -22,6 +22,7 @@ from operator import lt, le, gt, ge, eq, ne, truediv, floordiv, mod
from test import support from test import support
from test.support import is_resource_enabled, ALWAYS_EQ, LARGEST, SMALLEST from test.support import is_resource_enabled, ALWAYS_EQ, LARGEST, SMALLEST
from test.support import warnings_helper
import datetime as datetime_module import datetime as datetime_module
from datetime import MINYEAR, MAXYEAR from datetime import MINYEAR, MAXYEAR
@ -2797,6 +2798,7 @@ class TestDateTime(TestDate):
newdate = strptime(string, format) newdate = strptime(string, format)
self.assertEqual(newdate, target, msg=reason) self.assertEqual(newdate, target, msg=reason)
@warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_strptime_leap_year(self): def test_strptime_leap_year(self):
# GH-70647: warns if parsing a format with a day and no year. # GH-70647: warns if parsing a format with a day and no year.
with self.assertRaises(ValueError): with self.assertRaises(ValueError):

View File

@ -7,7 +7,7 @@ import re
import os import os
import sys import sys
from test import support from test import support
from test.support import skip_if_buggy_ucrt_strfptime from test.support import skip_if_buggy_ucrt_strfptime, warnings_helper
from datetime import date as datetime_date from datetime import date as datetime_date
import _strptime import _strptime
@ -120,7 +120,7 @@ class TimeRETests(unittest.TestCase):
def test_pattern(self): def test_pattern(self):
# Test TimeRE.pattern # Test TimeRE.pattern
pattern_string = self.time_re.pattern(r"%a %A %d") pattern_string = self.time_re.pattern(r"%a %A %d %Y")
self.assertTrue(pattern_string.find(self.locale_time.a_weekday[2]) != -1, self.assertTrue(pattern_string.find(self.locale_time.a_weekday[2]) != -1,
"did not find abbreviated weekday in pattern string '%s'" % "did not find abbreviated weekday in pattern string '%s'" %
pattern_string) pattern_string)
@ -160,10 +160,11 @@ class TimeRETests(unittest.TestCase):
found.group('b'))) found.group('b')))
for directive in ('a','A','b','B','c','d','G','H','I','j','m','M','p', for directive in ('a','A','b','B','c','d','G','H','I','j','m','M','p',
'S','u','U','V','w','W','x','X','y','Y','Z','%'): 'S','u','U','V','w','W','x','X','y','Y','Z','%'):
compiled = self.time_re.compile("%" + directive) fmt = "%d %Y" if directive == 'd' else "%" + directive
found = compiled.match(time.strftime("%" + directive)) compiled = self.time_re.compile(fmt)
found = compiled.match(time.strftime(fmt))
self.assertTrue(found, "Matching failed on '%s' using '%s' regex" % self.assertTrue(found, "Matching failed on '%s' using '%s' regex" %
(time.strftime("%" + directive), (time.strftime(fmt),
compiled.pattern)) compiled.pattern))
def test_blankpattern(self): def test_blankpattern(self):
@ -290,8 +291,9 @@ class StrptimeTests(unittest.TestCase):
def helper(self, directive, position): def helper(self, directive, position):
"""Helper fxn in testing.""" """Helper fxn in testing."""
strf_output = time.strftime("%" + directive, self.time_tuple) fmt = "%d %Y" if directive == 'd' else "%" + directive
strp_output = _strptime._strptime_time(strf_output, "%" + directive) strf_output = time.strftime(fmt, self.time_tuple)
strp_output = _strptime._strptime_time(strf_output, fmt)
self.assertTrue(strp_output[position] == self.time_tuple[position], self.assertTrue(strp_output[position] == self.time_tuple[position],
"testing of '%s' directive failed; '%s' -> %s != %s" % "testing of '%s' directive failed; '%s' -> %s != %s" %
(directive, strf_output, strp_output[position], (directive, strf_output, strp_output[position],
@ -497,9 +499,11 @@ class StrptimeTests(unittest.TestCase):
need_escaping = r".^$*+?{}\[]|)(" need_escaping = r".^$*+?{}\[]|)("
self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping)) self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping))
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-70647
def test_feb29_on_leap_year_without_year(self): def test_feb29_on_leap_year_without_year(self):
time.strptime("Feb 29", "%b %d") time.strptime("Feb 29", "%b %d")
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-70647
def test_mar1_comes_after_feb29_even_when_omitting_the_year(self): def test_mar1_comes_after_feb29_even_when_omitting_the_year(self):
self.assertLess( self.assertLess(
time.strptime("Feb 29", "%b %d"), time.strptime("Feb 29", "%b %d"),
@ -679,25 +683,25 @@ class CacheTests(unittest.TestCase):
def test_time_re_recreation(self): def test_time_re_recreation(self):
# Make sure cache is recreated when current locale does not match what # Make sure cache is recreated when current locale does not match what
# cached object was created with. # cached object was created with.
_strptime._strptime_time("10", "%d") _strptime._strptime_time("10 2004", "%d %Y")
_strptime._strptime_time("2005", "%Y") _strptime._strptime_time("2005", "%Y")
_strptime._TimeRE_cache.locale_time.lang = "Ni" _strptime._TimeRE_cache.locale_time.lang = "Ni"
original_time_re = _strptime._TimeRE_cache original_time_re = _strptime._TimeRE_cache
_strptime._strptime_time("10", "%d") _strptime._strptime_time("10 2004", "%d %Y")
self.assertIsNot(original_time_re, _strptime._TimeRE_cache) self.assertIsNot(original_time_re, _strptime._TimeRE_cache)
self.assertEqual(len(_strptime._regex_cache), 1) self.assertEqual(len(_strptime._regex_cache), 1)
def test_regex_cleanup(self): def test_regex_cleanup(self):
# Make sure cached regexes are discarded when cache becomes "full". # Make sure cached regexes are discarded when cache becomes "full".
try: try:
del _strptime._regex_cache['%d'] del _strptime._regex_cache['%d %Y']
except KeyError: except KeyError:
pass pass
bogus_key = 0 bogus_key = 0
while len(_strptime._regex_cache) <= _strptime._CACHE_MAX_SIZE: while len(_strptime._regex_cache) <= _strptime._CACHE_MAX_SIZE:
_strptime._regex_cache[bogus_key] = None _strptime._regex_cache[bogus_key] = None
bogus_key += 1 bogus_key += 1
_strptime._strptime_time("10", "%d") _strptime._strptime_time("10 2004", "%d %Y")
self.assertEqual(len(_strptime._regex_cache), 1) self.assertEqual(len(_strptime._regex_cache), 1)
def test_new_localetime(self): def test_new_localetime(self):
@ -705,7 +709,7 @@ class CacheTests(unittest.TestCase):
# is created. # is created.
locale_time_id = _strptime._TimeRE_cache.locale_time locale_time_id = _strptime._TimeRE_cache.locale_time
_strptime._TimeRE_cache.locale_time.lang = "Ni" _strptime._TimeRE_cache.locale_time.lang = "Ni"
_strptime._strptime_time("10", "%d") _strptime._strptime_time("10 2004", "%d %Y")
self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time) self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time)
def test_TimeRE_recreation_locale(self): def test_TimeRE_recreation_locale(self):
@ -716,13 +720,13 @@ class CacheTests(unittest.TestCase):
except locale.Error: except locale.Error:
self.skipTest('test needs en_US.UTF8 locale') self.skipTest('test needs en_US.UTF8 locale')
try: try:
_strptime._strptime_time('10', '%d') _strptime._strptime_time('10 2004', '%d %Y')
# Get id of current cache object. # Get id of current cache object.
first_time_re = _strptime._TimeRE_cache first_time_re = _strptime._TimeRE_cache
try: try:
# Change the locale and force a recreation of the cache. # Change the locale and force a recreation of the cache.
locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8')) locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8'))
_strptime._strptime_time('10', '%d') _strptime._strptime_time('10 2004', '%d %Y')
# Get the new cache object's id. # Get the new cache object's id.
second_time_re = _strptime._TimeRE_cache second_time_re = _strptime._TimeRE_cache
# They should not be equal. # They should not be equal.