|
4 | 4 | import time |
5 | 5 | import locale |
6 | 6 | import re |
| 7 | +import os |
7 | 8 | import sys |
8 | 9 | from test import support |
9 | 10 | from datetime import date as datetime_date |
@@ -324,19 +325,17 @@ def test_bad_timezone(self): |
324 | 325 | tz_name = time.tzname[0] |
325 | 326 | if tz_name.upper() in ("UTC", "GMT"): |
326 | 327 | 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): |
330 | 332 | time.tzname = (tz_name, tz_name) |
331 | 333 | time.daylight = 1 |
332 | 334 | tz_value = _strptime._strptime_time(tz_name, "%Z")[8] |
333 | 335 | self.assertEqual(tz_value, -1, |
334 | 336 | "%s lead to a timezone value of %s instead of -1 when " |
335 | 337 | "time.daylight set to %s and passing in %s" % |
336 | 338 | (time.tzname, tz_value, time.daylight, tz_name)) |
337 | | - finally: |
338 | | - time.tzname = original_tzname |
339 | | - time.daylight = original_daylight |
340 | 339 |
|
341 | 340 | def test_date_time(self): |
342 | 341 | # Test %c directive |
@@ -548,7 +547,7 @@ def test_new_localetime(self): |
548 | 547 | _strptime._strptime_time("10", "%d") |
549 | 548 | self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time) |
550 | 549 |
|
551 | | - def test_TimeRE_recreation(self): |
| 550 | + def test_TimeRE_recreation_locale(self): |
552 | 551 | # The TimeRE instance should be recreated upon changing the locale. |
553 | 552 | locale_info = locale.getlocale(locale.LC_TIME) |
554 | 553 | try: |
@@ -577,6 +576,33 @@ def test_TimeRE_recreation(self): |
577 | 576 | finally: |
578 | 577 | locale.setlocale(locale.LC_TIME, locale_info) |
579 | 578 |
|
| 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 | + |
580 | 606 |
|
581 | 607 | if __name__ == '__main__': |
582 | 608 | unittest.main() |
0 commit comments