Skip to content

Commit 9d454de

Browse files
committed
Avoid ctypes import on unsupported platforms
1 parent dd9e50b commit 9d454de

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import annotationlib
77
import contextlib
8-
import ctypes
98
import functools
109
import inspect
1110
import logging
@@ -3187,20 +3186,25 @@ def linked_to_musl():
31873186
return _linked_to_musl
31883187

31893188

3190-
class _py_buffer(ctypes.Structure):
3191-
_fields_ = [
3192-
("buf", ctypes.c_void_p),
3193-
("obj", ctypes.py_object),
3194-
("len", ctypes.c_ssize_t),
3195-
("itemsize", ctypes.c_ssize_t),
3196-
("readonly", ctypes.c_int),
3197-
("ndim", ctypes.c_int),
3198-
("format", ctypes.c_char_p),
3199-
("shape", ctypes.POINTER(ctypes.c_ssize_t)),
3200-
("strides", ctypes.POINTER(ctypes.c_ssize_t)),
3201-
("suboffsets", ctypes.POINTER(ctypes.c_ssize_t)),
3202-
("internal", ctypes.c_void_p),
3203-
]
3189+
try:
3190+
import ctypes
3191+
3192+
class _py_buffer(ctypes.Structure):
3193+
_fields_ = [
3194+
("buf", ctypes.c_void_p),
3195+
("obj", ctypes.py_object),
3196+
("len", ctypes.c_ssize_t),
3197+
("itemsize", ctypes.c_ssize_t),
3198+
("readonly", ctypes.c_int),
3199+
("ndim", ctypes.c_int),
3200+
("format", ctypes.c_char_p),
3201+
("shape", ctypes.POINTER(ctypes.c_ssize_t)),
3202+
("strides", ctypes.POINTER(ctypes.c_ssize_t)),
3203+
("suboffsets", ctypes.POINTER(ctypes.c_ssize_t)),
3204+
("internal", ctypes.c_void_p),
3205+
]
3206+
except ImportError:
3207+
_py_buffer = None
32043208

32053209

32063210
@contextlib.contextmanager
@@ -3210,7 +3214,12 @@ def ctypes_py_buffer(ob, flags=inspect.BufferFlags.SIMPLE):
32103214
32113215
`ob` must implement the buffer protocol, and the retrieved buffer is
32123216
released on exit of the context manager.
3217+
3218+
Skips any test using it if `ctypes` is unavailable.
32133219
"""
3220+
from .import_helper import import_module
3221+
3222+
ctypes = import_module("ctypes")
32143223
buf = _py_buffer()
32153224
ctypes.pythonapi.PyObject_GetBuffer(ctypes.py_object(ob),
32163225
ctypes.byref(buf),

Lib/test/test_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from test.support import os_helper
1010
from test.support import _2G
1111
from test.support import ctypes_py_buffer
12-
import ctypes
1312
import weakref
1413
import pickle
1514
import operator
@@ -71,6 +70,7 @@ def test_empty(self):
7170

7271
def test_empty_alignment(self):
7372
# gh-140557: pointer alignment of empty allocation
73+
ctypes = import_helper.import_module("ctypes")
7474
self.enterContext(warnings.catch_warnings())
7575
warnings.filterwarnings(
7676
"ignore",

Lib/test/test_builtin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import builtins
55
import collections
66
import contextlib
7-
import ctypes
87
import decimal
98
import fractions
109
import gc
@@ -2408,6 +2407,7 @@ def iterator():
24082407

24092408
def test_bytearray_empty_alignment(self):
24102409
# gh-140557: alignment of pointer in empty allocation
2410+
ctypes = import_module("ctypes")
24112411
max_align = ctypes.alignment(ctypes.c_longdouble)
24122412
array = bytearray()
24132413
with ctypes_py_buffer(array) as buf:

0 commit comments

Comments
 (0)