|
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 |
@@ -344,19 +345,17 @@ def test_bad_timezone(self): |
344 | 345 | tz_name = time.tzname[0] |
345 | 346 | if tz_name.upper() in ("UTC", "GMT"): |
346 | 347 | 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): |
350 | 352 | time.tzname = (tz_name, tz_name) |
351 | 353 | time.daylight = 1 |
352 | 354 | tz_value = _strptime._strptime_time(tz_name, "%Z")[8] |
353 | 355 | self.assertEqual(tz_value, -1, |
354 | 356 | "%s lead to a timezone value of %s instead of -1 when " |
355 | 357 | "time.daylight set to %s and passing in %s" % |
356 | 358 | (time.tzname, tz_value, time.daylight, tz_name)) |
357 | | - finally: |
358 | | - time.tzname = original_tzname |
359 | | - time.daylight = original_daylight |
360 | 359 |
|
361 | 360 | def test_date_time(self): |
362 | 361 | # Test %c directive |
@@ -579,7 +578,7 @@ def test_new_localetime(self): |
579 | 578 | _strptime._strptime_time("10", "%d") |
580 | 579 | self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time) |
581 | 580 |
|
582 | | - def test_TimeRE_recreation(self): |
| 581 | + def test_TimeRE_recreation_locale(self): |
583 | 582 | # The TimeRE instance should be recreated upon changing the locale. |
584 | 583 | locale_info = locale.getlocale(locale.LC_TIME) |
585 | 584 | try: |
@@ -608,6 +607,33 @@ def test_TimeRE_recreation(self): |
608 | 607 | finally: |
609 | 608 | locale.setlocale(locale.LC_TIME, locale_info) |
610 | 609 |
|
| 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 | + |
611 | 637 |
|
612 | 638 | if __name__ == '__main__': |
613 | 639 | unittest.main() |
0 commit comments