Skip to content

Commit 9e23476

Browse files
committed
avoid using platform.architecture() to detect 32-bit-ness in datetime tests
Same reasoning as the previous commit.
1 parent f3876a9 commit 9e23476

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

examples/rustapi_module/tests/test_datetime.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime as pdt
2-
import sys
32
import platform
3+
import struct
4+
import sys
45

56
import pytest
67
import rustapi_module.datetime as rdt
@@ -40,16 +41,27 @@ def tzname(self, dt):
4041
MAX_MICROSECONDS = int(pdt.timedelta.max.total_seconds() * 1e6)
4142
MIN_MICROSECONDS = int(pdt.timedelta.min.total_seconds() * 1e6)
4243

43-
IS_X86 = platform.architecture()[0] == "32bit"
44+
# The reason we don't use platform.architecture() here is that it's not
45+
# reliable on macOS. See https://stackoverflow.com/a/1405971/823869. Similarly,
46+
# sys.maxsize is not reliable on Windows. See
47+
# https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os/1405971#comment6209952_1405971
48+
# and https://stackoverflow.com/a/3411134/823869.
49+
_pointer_size = struct.calcsize("P")
50+
if _pointer_size == 8:
51+
IS_32_BIT = False
52+
elif _pointer_size == 4:
53+
IS_32_BIT = True
54+
else:
55+
raise RuntimeError("unexpected pointer size: " + repr(_pointer_size))
4456
IS_WINDOWS = sys.platform == "win32"
4557
if IS_WINDOWS:
4658
MIN_DATETIME = pdt.datetime(1970, 1, 2, 0, 0)
47-
if IS_X86:
59+
if IS_32_BIT:
4860
MAX_DATETIME = pdt.datetime(3001, 1, 19, 4, 59, 59)
4961
else:
5062
MAX_DATETIME = pdt.datetime(3001, 1, 19, 7, 59, 59)
5163
else:
52-
if IS_X86:
64+
if IS_32_BIT:
5365
# TS ±2147483648 (2**31)
5466
MIN_DATETIME = pdt.datetime(1901, 12, 13, 20, 45, 52)
5567
MAX_DATETIME = pdt.datetime(2038, 1, 19, 3, 14, 8)

0 commit comments

Comments
 (0)