diff options
author | Bas van Beek <b.f.van.beek@vu.nl> | 2021-10-29 17:35:13 +0200 |
---|---|---|
committer | Bas van Beek <b.f.van.beek@vu.nl> | 2021-10-29 17:41:15 +0200 |
commit | aac516a50b64bd1b25d9d5216c89de10b4baf61f (patch) | |
tree | c5fedd4a0ee0ffbcf35bdc7269e3f3fd944c564c /numpy/typing/tests/test_typing.py | |
parent | a2b4bb7b67651d91a7bc9831c0a053800b658293 (diff) | |
download | numpy-aac516a50b64bd1b25d9d5216c89de10b4baf61f.tar.gz |
TST: Account for the platform-specific mapping of numpy- to ctypes-types
Diffstat (limited to 'numpy/typing/tests/test_typing.py')
-rw-r--r-- | numpy/typing/tests/test_typing.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index 71e459df6..e0bcacd06 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -187,6 +187,38 @@ def _test_fail( raise AssertionError(_FAIL_MSG2.format(lineno, expression, expected_error, error)) +def _construct_ctypes_dict() -> dict[str, str]: + dct = { + "ubyte": "c_ubyte", + "ushort": "c_ushort", + "uintc": "c_uint", + "uint": "c_ulong", + "ulonglong": "c_ulonglong", + "byte": "c_byte", + "short": "c_short", + "intc": "c_int", + "int_": "c_long", + "longlong": "c_longlong", + "single": "c_float", + "double": "c_double", + "longdouble": "c_longdouble", + } + + # Match `ctypes` names to the first ctypes type with a given kind and + # precision, e.g. {"c_double": "c_double", "c_longdouble": "c_double"} + # if both types represent 64-bit floats. + # In this context "first" is defined by the order of `dct` + ret = {} + visited: dict[tuple[str, int], str] = {} + for np_name, ct_name in dct.items(): + np_scalar = getattr(np, np_name)() + + # Find the first `ctypes` type for a given `kind`/`itemsize` combo + key = (np_scalar.dtype.kind, np_scalar.dtype.itemsize) + ret[ct_name] = visited.setdefault(key, f"ctypes.{ct_name}") + return ret + + def _construct_format_dict() -> dict[str, str]: dct = {k.split(".")[-1]: v.replace("numpy", "numpy.typing") for k, v in _PRECISION_DICT.items()} @@ -261,6 +293,7 @@ def _construct_format_dict() -> dict[str, str]: #: A dictionary with all supported format keys (as keys) #: and matching values FORMAT_DICT: dict[str, str] = _construct_format_dict() +FORMAT_DICT.update(_construct_ctypes_dict()) def _parse_reveals(file: IO[str]) -> tuple[npt.NDArray[np.str_], list[str]]: |