mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
* Add a native _zoneinfo module ZoneInfo reads TZif tuples from zoneinfo._common.load_data. The stdlib module implements utcoffset, dst, tzname, fromutc, and the zone cache. whats_left compares that class. Cache tests no longer expect failure. Assisted-by: Grok:grok-4.7 * Fix ZoneInfo layout and keep its cache on the module utcoffset, dst, and tzname take the object. Zone data is boxed so the payload lines up with tzinfo on 32-bit. The strong, weak, and timedelta caches live on each interpreter's _zoneinfo module. Assisted-by: Grok:grok-4.7
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
import tempfile
|
|
from pathlib import Path
|
|
from zoneinfo import ZoneInfo, reset_tzpath
|
|
|
|
import _interpreters
|
|
|
|
# Fixed-offset UTC TZif, so the cache check does not need system zone data.
|
|
UTC = b"TZif2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00UTC\x00TZif2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00UTC\x00\nUTC0\n"
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
Path(directory, "UTC").write_bytes(UTC)
|
|
reset_tzpath([directory])
|
|
main = ZoneInfo("UTC")
|
|
assert main is ZoneInfo("UTC")
|
|
|
|
interp = _interpreters.create()
|
|
try:
|
|
result = _interpreters.run_string(
|
|
interp,
|
|
"""
|
|
from zoneinfo import ZoneInfo, reset_tzpath
|
|
reset_tzpath([tzpath])
|
|
first = ZoneInfo("UTC")
|
|
second = ZoneInfo("UTC")
|
|
assert first is second
|
|
assert id(first) != main_id
|
|
ZoneInfo.clear_cache()
|
|
""",
|
|
{"tzpath": directory, "main_id": id(main)},
|
|
)
|
|
finally:
|
|
_interpreters.destroy(interp)
|
|
|
|
if result is not None:
|
|
detail = getattr(result, "formatted", None) or repr(result)
|
|
raise AssertionError(detail)
|
|
assert main is ZoneInfo("UTC")
|