Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ab6c98

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

3 files changed

Lines changed: 50 additions & 15 deletions

File tree

Lib/_strptime.py

Lines changed: 14 additions & 8 deletions
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)
@@ -326,13 +330,15 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
326330

327331
global _TimeRE_cache, _regex_cache
328332
with _cache_lock:
329-
330-
if _getlang() != _TimeRE_cache.locale_time.lang:
333+
locale_time = _TimeRE_cache.locale_time
334+
if (_getlang() != locale_time.lang or
335+
time.tzname != locale_time.tzname or
336+
time.daylight != locale_time.daylight):
331337
_TimeRE_cache = TimeRE()
332338
_regex_cache.clear()
339+
locale_time = _TimeRE_cache.locale_time
333340
if len(_regex_cache) > _CACHE_MAX_SIZE:
334341
_regex_cache.clear()
335-
locale_time = _TimeRE_cache.locale_time
336342
format_regex = _regex_cache.get(format)
337343
if not format_regex:
338344
try:

Lib/test/test_strptime.py

Lines changed: 33 additions & 7 deletions
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
@@ -344,19 +345,17 @@ def test_bad_timezone(self):
344345
tz_name = time.tzname[0]
345346
if tz_name.upper() in ("UTC", "GMT"):
346347
self.skipTest('need non-UTC/GMT timezone')
347-
try:
348-
original_tzname = time.tzname
349-
original_daylight = time.daylight
348+
349+
with support.swap_attr(time, 'tzname', (tz_name, tz_name)), \
350+
support.swap_attr(time, 'daylight', 1), \
351+
support.swap_attr(time, 'tzset', lambda: None):
350352
time.tzname = (tz_name, tz_name)
351353
time.daylight = 1
352354
tz_value = _strptime._strptime_time(tz_name, "%Z")[8]
353355
self.assertEqual(tz_value, -1,
354356
"%s lead to a timezone value of %s instead of -1 when "
355357
"time.daylight set to %s and passing in %s" %
356358
(time.tzname, tz_value, time.daylight, tz_name))
357-
finally:
358-
time.tzname = original_tzname
359-
time.daylight = original_daylight
360359

361360
def test_date_time(self):
362361
# Test %c directive
@@ -579,7 +578,7 @@ def test_new_localetime(self):
579578
_strptime._strptime_time("10", "%d")
580579
self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time)
581580

582-
def test_TimeRE_recreation(self):
581+
def test_TimeRE_recreation_locale(self):
583582
# The TimeRE instance should be recreated upon changing the locale.
584583
locale_info = locale.getlocale(locale.LC_TIME)
585584
try:
@@ -608,6 +607,33 @@ def test_TimeRE_recreation(self):
608607
finally:
609608
locale.setlocale(locale.LC_TIME, locale_info)
610609

610+
@support.run_with_tz('STD-1DST')
611+
def test_TimeRE_recreation_timezone(self):
612+
# The TimeRE instance should be recreated upon changing the timezone.
613+
oldtzname = time.tzname
614+
tm = _strptime._strptime_time(time.tzname[0], '%Z')
615+
self.assertEqual(tm.tm_isdst, 0)
616+
tm = _strptime._strptime_time(time.tzname[1], '%Z')
617+
self.assertEqual(tm.tm_isdst, 1)
618+
# Get id of current cache object.
619+
first_time_re = _strptime._TimeRE_cache
620+
# Change the timezone and force a recreation of the cache.
621+
os.environ['TZ'] = 'EST+05EDT,M3.2.0,M11.1.0'
622+
time.tzset()
623+
tm = _strptime._strptime_time(time.tzname[0], '%Z')
624+
self.assertEqual(tm.tm_isdst, 0)
625+
tm = _strptime._strptime_time(time.tzname[1], '%Z')
626+
self.assertEqual(tm.tm_isdst, 1)
627+
# Get the new cache object's id.
628+
second_time_re = _strptime._TimeRE_cache
629+
# They should not be equal.
630+
self.assertIsNot(first_time_re, second_time_re)
631+
# Make sure old names no longer accepted.
632+
with self.assertRaises(ValueError):
633+
_strptime._strptime_time(oldtzname[0], '%Z')
634+
with self.assertRaises(ValueError):
635+
_strptime._strptime_time(oldtzname[1], '%Z')
636+
611637

612638
if __name__ == '__main__':
613639
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ Core and Builtins
109109
Library
110110
-------
111111

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

0 commit comments

Comments
 (0)