Skip to content

Commit b1f64e7

Browse files
Issue #6478: _strptime's regexp cache now is reset after changing timezone
with time.tzset().
2 parents 657257e + c7217d7 commit b1f64e7

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

Lib/_strptime.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def __init__(self):
7777
self.__calc_date_time()
7878
if _getlang() != self.lang:
7979
raise ValueError("locale changed during initialization")
80+
if time.tzname != self.tzname or time.daylight != self.daylight:
81+
raise ValueError("timezone changed during initialization")
8082

8183
def __pad(self, seq, front):
8284
# Add '' to seq to either the front (is True), else the back.
@@ -161,15 +163,17 @@ def __calc_date_time(self):
161163

162164
def __calc_timezone(self):
163165
# Set self.timezone by using time.tzname.
164-
# Do not worry about possibility of time.tzname[0] == timetzname[1]
165-
# and time.daylight; handle that in strptime .
166+
# Do not worry about possibility of time.tzname[0] == time.tzname[1]
167+
# and time.daylight; handle that in strptime.
166168
try:
167169
time.tzset()
168170
except AttributeError:
169171
pass
170-
no_saving = frozenset({"utc", "gmt", time.tzname[0].lower()})
171-
if time.daylight:
172-
has_saving = frozenset({time.tzname[1].lower()})
172+
self.tzname = time.tzname
173+
self.daylight = time.daylight
174+
no_saving = frozenset({"utc", "gmt", self.tzname[0].lower()})
175+
if self.daylight:
176+
has_saving = frozenset({self.tzname[1].lower()})
173177
else:
174178
has_saving = frozenset()
175179
self.timezone = (no_saving, has_saving)
@@ -307,13 +311,15 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
307311

308312
global _TimeRE_cache, _regex_cache
309313
with _cache_lock:
310-
311-
if _getlang() != _TimeRE_cache.locale_time.lang:
314+
locale_time = _TimeRE_cache.locale_time
315+
if (_getlang() != locale_time.lang or
316+
time.tzname != locale_time.tzname or
317+
time.daylight != locale_time.daylight):
312318
_TimeRE_cache = TimeRE()
313319
_regex_cache.clear()
320+
locale_time = _TimeRE_cache.locale_time
314321
if len(_regex_cache) > _CACHE_MAX_SIZE:
315322
_regex_cache.clear()
316-
locale_time = _TimeRE_cache.locale_time
317323
format_regex = _regex_cache.get(format)
318324
if not format_regex:
319325
try:

Lib/test/test_strptime.py

+33-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import locale
66
import re
7+
import os
78
import sys
89
from test import support
910
from datetime import date as datetime_date
@@ -324,19 +325,17 @@ def test_bad_timezone(self):
324325
tz_name = time.tzname[0]
325326
if tz_name.upper() in ("UTC", "GMT"):
326327
self.skipTest('need non-UTC/GMT timezone')
327-
try:
328-
original_tzname = time.tzname
329-
original_daylight = time.daylight
328+
329+
with support.swap_attr(time, 'tzname', (tz_name, tz_name)), \
330+
support.swap_attr(time, 'daylight', 1), \
331+
support.swap_attr(time, 'tzset', lambda: None):
330332
time.tzname = (tz_name, tz_name)
331333
time.daylight = 1
332334
tz_value = _strptime._strptime_time(tz_name, "%Z")[8]
333335
self.assertEqual(tz_value, -1,
334336
"%s lead to a timezone value of %s instead of -1 when "
335337
"time.daylight set to %s and passing in %s" %
336338
(time.tzname, tz_value, time.daylight, tz_name))
337-
finally:
338-
time.tzname = original_tzname
339-
time.daylight = original_daylight
340339

341340
def test_date_time(self):
342341
# Test %c directive
@@ -548,7 +547,7 @@ def test_new_localetime(self):
548547
_strptime._strptime_time("10", "%d")
549548
self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time)
550549

551-
def test_TimeRE_recreation(self):
550+
def test_TimeRE_recreation_locale(self):
552551
# The TimeRE instance should be recreated upon changing the locale.
553552
locale_info = locale.getlocale(locale.LC_TIME)
554553
try:
@@ -577,6 +576,33 @@ def test_TimeRE_recreation(self):
577576
finally:
578577
locale.setlocale(locale.LC_TIME, locale_info)
579578

579+
@support.run_with_tz('STD-1DST')
580+
def test_TimeRE_recreation_timezone(self):
581+
# The TimeRE instance should be recreated upon changing the timezone.
582+
oldtzname = time.tzname
583+
tm = _strptime._strptime_time(time.tzname[0], '%Z')
584+
self.assertEqual(tm.tm_isdst, 0)
585+
tm = _strptime._strptime_time(time.tzname[1], '%Z')
586+
self.assertEqual(tm.tm_isdst, 1)
587+
# Get id of current cache object.
588+
first_time_re = _strptime._TimeRE_cache
589+
# Change the timezone and force a recreation of the cache.
590+
os.environ['TZ'] = 'EST+05EDT,M3.2.0,M11.1.0'
591+
time.tzset()
592+
tm = _strptime._strptime_time(time.tzname[0], '%Z')
593+
self.assertEqual(tm.tm_isdst, 0)
594+
tm = _strptime._strptime_time(time.tzname[1], '%Z')
595+
self.assertEqual(tm.tm_isdst, 1)
596+
# Get the new cache object's id.
597+
second_time_re = _strptime._TimeRE_cache
598+
# They should not be equal.
599+
self.assertIsNot(first_time_re, second_time_re)
600+
# Make sure old names no longer accepted.
601+
with self.assertRaises(ValueError):
602+
_strptime._strptime_time(oldtzname[0], '%Z')
603+
with self.assertRaises(ValueError):
604+
_strptime._strptime_time(oldtzname[1], '%Z')
605+
580606

581607
if __name__ == '__main__':
582608
unittest.main()

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Issue #6478: _strptime's regexp cache now is reset after changing timezone
26+
with time.tzset().
27+
2528
- Issue #14285: When executing a package with the "python -m package" option,
2629
and package initialization fails, a proper traceback is now reported. The
2730
"runpy" module now lets exceptions from package initialization pass back to

0 commit comments

Comments
 (0)