| 1 |
import contextlib |
| 2 |
import datetime |
| 3 |
import os |
| 4 |
import pickle |
| 5 |
import unittest |
| 6 |
import zoneinfo |
| 7 |
|
| 8 |
from test.support.hypothesis_helper import hypothesis |
| 9 |
|
| 10 |
import test.test_zoneinfo._support as test_support |
| 11 |
|
| 12 |
ZoneInfoTestBase = test_support.ZoneInfoTestBase |
| 13 |
|
| 14 |
py_zoneinfo, c_zoneinfo = test_support.get_modules() |
| 15 |
|
| 16 |
UTC = datetime.timezone.utc |
| 17 |
MIN_UTC = datetime.datetime.min.replace(tzinfo=UTC) |
| 18 |
MAX_UTC = datetime.datetime.max.replace(tzinfo=UTC) |
| 19 |
ZERO = datetime.timedelta(0) |
| 20 |
|
| 21 |
|
| 22 |
def _valid_keys(): |
| 23 |
"""Get available time zones, including posix/ and right/ directories.""" |
| 24 |
from importlib import resources |
| 25 |
|
| 26 |
available_zones = sorted(zoneinfo.available_timezones()) |
| 27 |
TZPATH = zoneinfo.TZPATH |
| 28 |
|
| 29 |
def valid_key(key): |
| 30 |
for root in TZPATH: |
| 31 |
key_file = os.path.join(root, key) |
| 32 |
if os.path.exists(key_file): |
| 33 |
return True |
| 34 |
|
| 35 |
components = key.split("/") |
| 36 |
package_name = ".".join(["tzdata.zoneinfo"] + components[:-1]) |
| 37 |
resource_name = components[-1] |
| 38 |
|
| 39 |
try: |
| 40 |
return resources.files(package_name).joinpath(resource_name).is_file() |
| 41 |
except ModuleNotFoundError: |
| 42 |
return False |
| 43 |
|
| 44 |
# This relies on the fact that dictionaries maintain insertion order — for |
| 45 |
# shrinking purposes, it is preferable to start with the standard version, |
| 46 |
# then move to the posix/ version, then to the right/ version. |
| 47 |
out_zones = {"": available_zones} |
| 48 |
for prefix in ["posix", "right"]: |
| 49 |
prefix_out = [] |
| 50 |
for key in available_zones: |
| 51 |
prefix_key = f"{prefix}/{key}" |
| 52 |
if valid_key(prefix_key): |
| 53 |
prefix_out.append(prefix_key) |
| 54 |
|
| 55 |
out_zones[prefix] = prefix_out |
| 56 |
|
| 57 |
output = [] |
| 58 |
for keys in out_zones.values(): |
| 59 |
output.extend(keys) |
| 60 |
|
| 61 |
return output |
| 62 |
|
| 63 |
|
| 64 |
VALID_KEYS = _valid_keys() |
| 65 |
if not VALID_KEYS: |
| 66 |
raise unittest.SkipTest("No time zone data available") |
| 67 |
|
| 68 |
|
| 69 |
def valid_keys(): |
| 70 |
return hypothesis.strategies.sampled_from(VALID_KEYS) |
| 71 |
|
| 72 |
|
| 73 |
KEY_EXAMPLES = [ |
| 74 |
"Africa/Abidjan", |
| 75 |
"Africa/Casablanca", |
| 76 |
"America/Los_Angeles", |
| 77 |
"America/Santiago", |
| 78 |
"Asia/Tokyo", |
| 79 |
"Australia/Sydney", |
| 80 |
"Europe/Dublin", |
| 81 |
"Europe/Lisbon", |
| 82 |
"Europe/London", |
| 83 |
"Pacific/Kiritimati", |
| 84 |
"UTC", |
| 85 |
] |
| 86 |
|
| 87 |
|
| 88 |
def add_key_examples(f): |
| 89 |
for key in KEY_EXAMPLES: |
| 90 |
f = hypothesis.example(key)(f) |
| 91 |
return f |
| 92 |
|
| 93 |
|
| 94 |
class ZoneInfoTest(ZoneInfoTestBase): |
| 95 |
module = py_zoneinfo |
| 96 |
|
| 97 |
@hypothesis.given(key=valid_keys()) |
| 98 |
@add_key_examples |
| 99 |
def test_str(self, key): |
| 100 |
zi = self.klass(key) |
| 101 |
self.assertEqual(str(zi), key) |
| 102 |
|
| 103 |
@hypothesis.given(key=valid_keys()) |
| 104 |
@add_key_examples |
| 105 |
def test_key(self, key): |
| 106 |
zi = self.klass(key) |
| 107 |
|
| 108 |
self.assertEqual(zi.key, key) |
| 109 |
|
| 110 |
@hypothesis.given( |
| 111 |
dt=hypothesis.strategies.one_of( |
| 112 |
hypothesis.strategies.datetimes(), hypothesis.strategies.times() |
| 113 |
) |
| 114 |
) |
| 115 |
@hypothesis.example(dt=datetime.datetime.min) |
| 116 |
@hypothesis.example(dt=datetime.datetime.max) |
| 117 |
@hypothesis.example(dt=datetime.datetime(1970, 1, 1)) |
| 118 |
@hypothesis.example(dt=datetime.datetime(2039, 1, 1)) |
| 119 |
@hypothesis.example(dt=datetime.time(0)) |
| 120 |
@hypothesis.example(dt=datetime.time(12, 0)) |
| 121 |
@hypothesis.example(dt=datetime.time(23, 59, 59, 999999)) |
| 122 |
def test_utc(self, dt): |
| 123 |
zi = self.klass("UTC") |
| 124 |
dt_zi = dt.replace(tzinfo=zi) |
| 125 |
|
| 126 |
self.assertEqual(dt_zi.utcoffset(), ZERO) |
| 127 |
self.assertEqual(dt_zi.dst(), ZERO) |
| 128 |
self.assertEqual(dt_zi.tzname(), "UTC") |
| 129 |
|
| 130 |
|
| 131 |
class CZoneInfoTest(ZoneInfoTest): |
| 132 |
module = c_zoneinfo |
| 133 |
|
| 134 |
|
| 135 |
class ZoneInfoPickleTest(ZoneInfoTestBase): |
| 136 |
module = py_zoneinfo |
| 137 |
|
| 138 |
def setUp(self): |
| 139 |
with contextlib.ExitStack() as stack: |
| 140 |
stack.enter_context(test_support.set_zoneinfo_module(self.module)) |
| 141 |
self.addCleanup(stack.pop_all().close) |
| 142 |
|
| 143 |
super().setUp() |
| 144 |
|
| 145 |
@hypothesis.given(key=valid_keys()) |
| 146 |
@add_key_examples |
| 147 |
def test_pickle_unpickle_cache(self, key): |
| 148 |
zi = self.klass(key) |
| 149 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 150 |
pkl_str = pickle.dumps(zi, proto) |
| 151 |
zi_rt = pickle.loads(pkl_str) |
| 152 |
|
| 153 |
self.assertIs(zi, zi_rt) |
| 154 |
|
| 155 |
@hypothesis.given(key=valid_keys()) |
| 156 |
@add_key_examples |
| 157 |
def test_pickle_unpickle_no_cache(self, key): |
| 158 |
zi = self.klass.no_cache(key) |
| 159 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 160 |
pkl_str = pickle.dumps(zi, proto) |
| 161 |
zi_rt = pickle.loads(pkl_str) |
| 162 |
|
| 163 |
self.assertIsNot(zi, zi_rt) |
| 164 |
self.assertEqual(str(zi), str(zi_rt)) |
| 165 |
|
| 166 |
@hypothesis.given(key=valid_keys()) |
| 167 |
@add_key_examples |
| 168 |
def test_pickle_unpickle_cache_multiple_rounds(self, key): |
| 169 |
"""Test that pickle/unpickle is idempotent.""" |
| 170 |
zi_0 = self.klass(key) |
| 171 |
pkl_str_0 = pickle.dumps(zi_0) |
| 172 |
zi_1 = pickle.loads(pkl_str_0) |
| 173 |
pkl_str_1 = pickle.dumps(zi_1) |
| 174 |
zi_2 = pickle.loads(pkl_str_1) |
| 175 |
pkl_str_2 = pickle.dumps(zi_2) |
| 176 |
|
| 177 |
self.assertEqual(pkl_str_0, pkl_str_1) |
| 178 |
self.assertEqual(pkl_str_1, pkl_str_2) |
| 179 |
|
| 180 |
self.assertIs(zi_0, zi_1) |
| 181 |
self.assertIs(zi_0, zi_2) |
| 182 |
self.assertIs(zi_1, zi_2) |
| 183 |
|
| 184 |
@hypothesis.given(key=valid_keys()) |
| 185 |
@add_key_examples |
| 186 |
def test_pickle_unpickle_no_cache_multiple_rounds(self, key): |
| 187 |
"""Test that pickle/unpickle is idempotent.""" |
| 188 |
zi_cache = self.klass(key) |
| 189 |
|
| 190 |
zi_0 = self.klass.no_cache(key) |
| 191 |
pkl_str_0 = pickle.dumps(zi_0) |
| 192 |
zi_1 = pickle.loads(pkl_str_0) |
| 193 |
pkl_str_1 = pickle.dumps(zi_1) |
| 194 |
zi_2 = pickle.loads(pkl_str_1) |
| 195 |
pkl_str_2 = pickle.dumps(zi_2) |
| 196 |
|
| 197 |
self.assertEqual(pkl_str_0, pkl_str_1) |
| 198 |
self.assertEqual(pkl_str_1, pkl_str_2) |
| 199 |
|
| 200 |
self.assertIsNot(zi_0, zi_1) |
| 201 |
self.assertIsNot(zi_0, zi_2) |
| 202 |
self.assertIsNot(zi_1, zi_2) |
| 203 |
|
| 204 |
self.assertIsNot(zi_0, zi_cache) |
| 205 |
self.assertIsNot(zi_1, zi_cache) |
| 206 |
self.assertIsNot(zi_2, zi_cache) |
| 207 |
|
| 208 |
|
| 209 |
class CZoneInfoPickleTest(ZoneInfoPickleTest): |
| 210 |
module = c_zoneinfo |
| 211 |
|
| 212 |
|
| 213 |
class ZoneInfoCacheTest(ZoneInfoTestBase): |
| 214 |
module = py_zoneinfo |
| 215 |
|
| 216 |
@hypothesis.given(key=valid_keys()) |
| 217 |
@add_key_examples |
| 218 |
def test_cache(self, key): |
| 219 |
zi_0 = self.klass(key) |
| 220 |
zi_1 = self.klass(key) |
| 221 |
|
| 222 |
self.assertIs(zi_0, zi_1) |
| 223 |
|
| 224 |
@hypothesis.given(key=valid_keys()) |
| 225 |
@add_key_examples |
| 226 |
def test_no_cache(self, key): |
| 227 |
zi_0 = self.klass.no_cache(key) |
| 228 |
zi_1 = self.klass.no_cache(key) |
| 229 |
|
| 230 |
self.assertIsNot(zi_0, zi_1) |
| 231 |
|
| 232 |
|
| 233 |
class CZoneInfoCacheTest(ZoneInfoCacheTest): |
| 234 |
klass = c_zoneinfo.ZoneInfo |
| 235 |
|
| 236 |
|
| 237 |
class PythonCConsistencyTest(unittest.TestCase): |
| 238 |
"""Tests that the C and Python versions do the same thing.""" |
| 239 |
|
| 240 |
def _is_ambiguous(self, dt): |
| 241 |
return dt.replace(fold=not dt.fold).utcoffset() == dt.utcoffset() |
| 242 |
|
| 243 |
@hypothesis.given(dt=hypothesis.strategies.datetimes(), key=valid_keys()) |
| 244 |
@hypothesis.example(dt=datetime.datetime.min, key="America/New_York") |
| 245 |
@hypothesis.example(dt=datetime.datetime.max, key="America/New_York") |
| 246 |
@hypothesis.example(dt=datetime.datetime(1970, 1, 1), key="America/New_York") |
| 247 |
@hypothesis.example(dt=datetime.datetime(2020, 1, 1), key="Europe/Paris") |
| 248 |
@hypothesis.example(dt=datetime.datetime(2020, 6, 1), key="Europe/Paris") |
| 249 |
def test_same_str(self, dt, key): |
| 250 |
py_dt = dt.replace(tzinfo=py_zoneinfo.ZoneInfo(key)) |
| 251 |
c_dt = dt.replace(tzinfo=c_zoneinfo.ZoneInfo(key)) |
| 252 |
|
| 253 |
self.assertEqual(str(py_dt), str(c_dt)) |
| 254 |
|
| 255 |
@hypothesis.given(dt=hypothesis.strategies.datetimes(), key=valid_keys()) |
| 256 |
@hypothesis.example(dt=datetime.datetime(1970, 1, 1), key="America/New_York") |
| 257 |
@hypothesis.example(dt=datetime.datetime(2020, 2, 5), key="America/New_York") |
| 258 |
@hypothesis.example(dt=datetime.datetime(2020, 8, 12), key="America/New_York") |
| 259 |
@hypothesis.example(dt=datetime.datetime(2040, 1, 1), key="Africa/Casablanca") |
| 260 |
@hypothesis.example(dt=datetime.datetime(1970, 1, 1), key="Europe/Paris") |
| 261 |
@hypothesis.example(dt=datetime.datetime(2040, 1, 1), key="Europe/Paris") |
| 262 |
@hypothesis.example(dt=datetime.datetime.min, key="Asia/Tokyo") |
| 263 |
@hypothesis.example(dt=datetime.datetime.max, key="Asia/Tokyo") |
| 264 |
def test_same_offsets_and_names(self, dt, key): |
| 265 |
py_dt = dt.replace(tzinfo=py_zoneinfo.ZoneInfo(key)) |
| 266 |
c_dt = dt.replace(tzinfo=c_zoneinfo.ZoneInfo(key)) |
| 267 |
|
| 268 |
self.assertEqual(py_dt.tzname(), c_dt.tzname()) |
| 269 |
self.assertEqual(py_dt.utcoffset(), c_dt.utcoffset()) |
| 270 |
self.assertEqual(py_dt.dst(), c_dt.dst()) |
| 271 |
|
| 272 |
@hypothesis.given( |
| 273 |
dt=hypothesis.strategies.datetimes(timezones=hypothesis.strategies.just(UTC)), |
| 274 |
key=valid_keys(), |
| 275 |
) |
| 276 |
@hypothesis.example(dt=MIN_UTC, key="Asia/Tokyo") |
| 277 |
@hypothesis.example(dt=MAX_UTC, key="Asia/Tokyo") |
| 278 |
@hypothesis.example(dt=MIN_UTC, key="America/New_York") |
| 279 |
@hypothesis.example(dt=MAX_UTC, key="America/New_York") |
| 280 |
@hypothesis.example( |
| 281 |
dt=datetime.datetime(2006, 10, 29, 5, 15, tzinfo=UTC), |
| 282 |
key="America/New_York", |
| 283 |
) |
| 284 |
def test_same_from_utc(self, dt, key): |
| 285 |
py_zi = py_zoneinfo.ZoneInfo(key) |
| 286 |
c_zi = c_zoneinfo.ZoneInfo(key) |
| 287 |
|
| 288 |
# Convert to UTC: This can overflow, but we just care about consistency |
| 289 |
py_overflow_exc = None |
| 290 |
c_overflow_exc = None |
| 291 |
try: |
| 292 |
py_dt = dt.astimezone(py_zi) |
| 293 |
except OverflowError as e: |
| 294 |
py_overflow_exc = e |
| 295 |
|
| 296 |
try: |
| 297 |
c_dt = dt.astimezone(c_zi) |
| 298 |
except OverflowError as e: |
| 299 |
c_overflow_exc = e |
| 300 |
|
| 301 |
if (py_overflow_exc is not None) != (c_overflow_exc is not None): |
| 302 |
raise py_overflow_exc or c_overflow_exc # pragma: nocover |
| 303 |
|
| 304 |
if py_overflow_exc is not None: |
| 305 |
return # Consistently raises the same exception |
| 306 |
|
| 307 |
# PEP 495 says that an inter-zone comparison between ambiguous |
| 308 |
# datetimes is always False. |
| 309 |
if py_dt != c_dt: |
| 310 |
self.assertEqual( |
| 311 |
self._is_ambiguous(py_dt), |
| 312 |
self._is_ambiguous(c_dt), |
| 313 |
(py_dt, c_dt), |
| 314 |
) |
| 315 |
|
| 316 |
self.assertEqual(py_dt.tzname(), c_dt.tzname()) |
| 317 |
self.assertEqual(py_dt.utcoffset(), c_dt.utcoffset()) |
| 318 |
self.assertEqual(py_dt.dst(), c_dt.dst()) |
| 319 |
|
| 320 |
@hypothesis.given(dt=hypothesis.strategies.datetimes(), key=valid_keys()) |
| 321 |
@hypothesis.example(dt=datetime.datetime.max, key="America/New_York") |
| 322 |
@hypothesis.example(dt=datetime.datetime.min, key="America/New_York") |
| 323 |
@hypothesis.example(dt=datetime.datetime.min, key="Asia/Tokyo") |
| 324 |
@hypothesis.example(dt=datetime.datetime.max, key="Asia/Tokyo") |
| 325 |
def test_same_to_utc(self, dt, key): |
| 326 |
py_dt = dt.replace(tzinfo=py_zoneinfo.ZoneInfo(key)) |
| 327 |
c_dt = dt.replace(tzinfo=c_zoneinfo.ZoneInfo(key)) |
| 328 |
|
| 329 |
# Convert from UTC: Overflow OK if it happens in both implementations |
| 330 |
py_overflow_exc = None |
| 331 |
c_overflow_exc = None |
| 332 |
try: |
| 333 |
py_utc = py_dt.astimezone(UTC) |
| 334 |
except OverflowError as e: |
| 335 |
py_overflow_exc = e |
| 336 |
|
| 337 |
try: |
| 338 |
c_utc = c_dt.astimezone(UTC) |
| 339 |
except OverflowError as e: |
| 340 |
c_overflow_exc = e |
| 341 |
|
| 342 |
if (py_overflow_exc is not None) != (c_overflow_exc is not None): |
| 343 |
raise py_overflow_exc or c_overflow_exc # pragma: nocover |
| 344 |
|
| 345 |
if py_overflow_exc is not None: |
| 346 |
return # Consistently raises the same exception |
| 347 |
|
| 348 |
self.assertEqual(py_utc, c_utc) |
| 349 |
|
| 350 |
@hypothesis.given(key=valid_keys()) |
| 351 |
@add_key_examples |
| 352 |
def test_cross_module_pickle(self, key): |
| 353 |
py_zi = py_zoneinfo.ZoneInfo(key) |
| 354 |
c_zi = c_zoneinfo.ZoneInfo(key) |
| 355 |
|
| 356 |
with test_support.set_zoneinfo_module(py_zoneinfo): |
| 357 |
py_pkl = pickle.dumps(py_zi) |
| 358 |
|
| 359 |
with test_support.set_zoneinfo_module(c_zoneinfo): |
| 360 |
c_pkl = pickle.dumps(c_zi) |
| 361 |
|
| 362 |
with test_support.set_zoneinfo_module(c_zoneinfo): |
| 363 |
# Python → C |
| 364 |
py_to_c_zi = pickle.loads(py_pkl) |
| 365 |
self.assertIs(py_to_c_zi, c_zi) |
| 366 |
|
| 367 |
with test_support.set_zoneinfo_module(py_zoneinfo): |
| 368 |
# C → Python |
| 369 |
c_to_py_zi = pickle.loads(c_pkl) |
| 370 |
self.assertIs(c_to_py_zi, py_zi) |