diff options
author | Thomas Perl <m@thp.io> | 2022-07-03 18:58:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-03 18:58:02 (GMT) |
commit | b296c7442be97600dc87819e885e037313883d8e (patch) | |
tree | 1bac0d27ad4aa8f047864ead58cb480057e12243 /Lib | |
parent | 39c29f753e6d6f390dce5a36613c1e03f43d28ea (diff) | |
download | cpython-b296c7442be97600dc87819e885e037313883d8e.zip cpython-b296c7442be97600dc87819e885e037313883d8e.tar.gz cpython-b296c7442be97600dc87819e885e037313883d8e.tar.bz2 |
gh-92869: ctypes: Add c_time_t (#92870)
Adds `ctypes.c_time_t` to represent the C `time_t` type accurately as its size varies.
Primarily-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org> [Google]
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ctypes/__init__.py | 8 | ||||
-rw-r--r-- | Lib/test/test_ctypes/test_sizes.py | 3 |
2 files changed, 11 insertions, 0 deletions
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 26135ad..b94b337 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -11,6 +11,7 @@ from _ctypes import CFuncPtr as _CFuncPtr from _ctypes import __version__ as _ctypes_version from _ctypes import RTLD_LOCAL, RTLD_GLOBAL from _ctypes import ArgumentError +from _ctypes import SIZEOF_TIME_T from struct import calcsize as _calcsize @@ -563,4 +564,11 @@ for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]: elif sizeof(kind) == 8: c_uint64 = kind del(kind) +if SIZEOF_TIME_T == 8: + c_time_t = c_int64 +elif SIZEOF_TIME_T == 4: + c_time_t = c_int32 +else: + raise SystemError(f"Unexpected sizeof(time_t): {SIZEOF_TIME_T=}") + _reset_cache() diff --git a/Lib/test/test_ctypes/test_sizes.py b/Lib/test/test_ctypes/test_sizes.py index 4ceacbc..bf8d6ea 100644 --- a/Lib/test/test_ctypes/test_sizes.py +++ b/Lib/test/test_ctypes/test_sizes.py @@ -28,6 +28,9 @@ class SizesTestCase(unittest.TestCase): def test_ssize_t(self): self.assertEqual(sizeof(c_void_p), sizeof(c_ssize_t)) + def test_time_t(self): + self.assertEqual(sizeof(c_time_t), SIZEOF_TIME_T) + if __name__ == "__main__": unittest.main() |