Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix test_strptime raises a DeprecationWarning
  • Loading branch information
nineteendo committed Apr 9, 2024
commit 9fc8691a0d491b1428188c1f8171f2a4369b5d89
28 changes: 15 additions & 13 deletions Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def setUp(self):

def test_pattern(self):
# 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,
"did not find abbreviated weekday in pattern string '%s'" %
pattern_string)
Expand Down Expand Up @@ -160,10 +160,11 @@ def test_compile(self):
found.group('b')))
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','%'):
compiled = self.time_re.compile("%" + directive)
found = compiled.match(time.strftime("%" + directive))
fmt = "%d %Y" if directive == 'd' else "%" + directive
compiled = self.time_re.compile(fmt)
found = compiled.match(time.strftime(fmt))
self.assertTrue(found, "Matching failed on '%s' using '%s' regex" %
(time.strftime("%" + directive),
(time.strftime(fmt),
compiled.pattern))

def test_blankpattern(self):
Expand Down Expand Up @@ -290,8 +291,9 @@ def test_unconverteddata(self):

def helper(self, directive, position):
"""Helper fxn in testing."""
strf_output = time.strftime("%" + directive, self.time_tuple)
strp_output = _strptime._strptime_time(strf_output, "%" + directive)
fmt = "%d %Y" if directive == 'd' else "%" + 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],
"testing of '%s' directive failed; '%s' -> %s != %s" %
(directive, strf_output, strp_output[position],
Expand Down Expand Up @@ -679,33 +681,33 @@ class CacheTests(unittest.TestCase):
def test_time_re_recreation(self):
# Make sure cache is recreated when current locale does not match what
# cached object was created with.
_strptime._strptime_time("10", "%d")
_strptime._strptime_time("10 2004", "%d %Y")
_strptime._strptime_time("2005", "%Y")
_strptime._TimeRE_cache.locale_time.lang = "Ni"
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.assertEqual(len(_strptime._regex_cache), 1)

def test_regex_cleanup(self):
# Make sure cached regexes are discarded when cache becomes "full".
try:
del _strptime._regex_cache['%d']
del _strptime._regex_cache['%d %Y']
except KeyError:
pass
bogus_key = 0
while len(_strptime._regex_cache) <= _strptime._CACHE_MAX_SIZE:
_strptime._regex_cache[bogus_key] = None
bogus_key += 1
_strptime._strptime_time("10", "%d")
_strptime._strptime_time("10 2004", "%d %Y")
self.assertEqual(len(_strptime._regex_cache), 1)

def test_new_localetime(self):
# A new LocaleTime instance should be created when a new TimeRE object
# is created.
locale_time_id = _strptime._TimeRE_cache.locale_time
_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)

def test_TimeRE_recreation_locale(self):
Expand All @@ -716,13 +718,13 @@ def test_TimeRE_recreation_locale(self):
except locale.Error:
self.skipTest('test needs en_US.UTF8 locale')
try:
_strptime._strptime_time('10', '%d')
_strptime._strptime_time('10 2004', '%d %Y')
# Get id of current cache object.
first_time_re = _strptime._TimeRE_cache
try:
# Change the locale and force a recreation of the cache.
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.
second_time_re = _strptime._TimeRE_cache
# They should not be equal.
Expand Down