Skip to content

Commit c31d187

Browse files
authored
Test PyLong import/export API on limited C API 3.15 and newer (#211)
1 parent f07f6ed commit c31d187

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎docs/api.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ Python 3.13
525525

526526
See `Py_HashPointer() documentation <https://docs.python.org/dev/c-api/hash.html#c.Py_HashPointer>`__.
527527

528+
Not available on the limited C API.
529+
528530
.. c:function:: Py_hash_t PyObject_GenericHash(PyObject *obj)
529531

530532
See `PyObject_GenericHash() documentation <https://docs.python.org/dev/c-api/hash.html#c.PyObject_GenericHash>`__.

‎tests/test_pythoncapi_compat_cext.c‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,21 +1414,33 @@ test_dict_setdefault(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
14141414
}
14151415

14161416

1417-
#if !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)
1417+
#if ((!defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15)) \
1418+
&& !defined(PYPY_VERSION))
14181419
static int
14191420
test_long_import_export()
14201421
{
1421-
// test import/export API
1422-
digit *digits;
1422+
// test PyLong import/export API
1423+
1424+
const PyLongLayout *layout = PyLong_GetNativeLayout();
1425+
assert(layout->digits_order == -1);
1426+
if (layout->digit_size != sizeof(uint32_t)) {
1427+
PyErr_SetString(PyExc_AssertionError,
1428+
"unsupported digit size");
1429+
return -1;
1430+
}
1431+
1432+
uint32_t *digits;
14231433
PyLongWriter *writer;
14241434
static PyLongExport long_export;
14251435

1436+
// test PyLongWriter_Discard()
14261437
writer = PyLongWriter_Create(1, 1, (void **)&digits);
14271438
if (writer == NULL) {
14281439
return -1;
14291440
}
14301441
PyLongWriter_Discard(writer);
14311442

1443+
// test PyLongWriter_Finish()
14321444
writer = PyLongWriter_Create(1, 1, (void **)&digits);
14331445
if (writer == NULL) {
14341446
return -1;
@@ -1467,7 +1479,7 @@ test_long_import_export()
14671479
return -1;
14681480
}
14691481
assert(long_export.value == 0);
1470-
digits = (digit*)long_export.digits;
1482+
digits = (uint32_t*)long_export.digits;
14711483
assert(digits[0] == 1);
14721484
assert(digits[1] == 0);
14731485
assert(digits[2] == 0);
@@ -1476,9 +1488,6 @@ test_long_import_export()
14761488
PyLong_FreeExport(&long_export);
14771489
Py_DECREF(obj);
14781490

1479-
const PyLongLayout *layout = PyLong_GetNativeLayout();
1480-
assert(layout->digits_order == -1);
1481-
assert(layout->digit_size == sizeof(digit));
14821491
return 0;
14831492
}
14841493
#endif
@@ -1519,7 +1528,8 @@ test_long_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
15191528
assert(PyLong_IsNegative(obj) == 0);
15201529
assert(PyLong_IsZero(obj) == 0);
15211530

1522-
#if !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)
1531+
#if ((!defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15)) \
1532+
&& !defined(PYPY_VERSION))
15231533
if (test_long_import_export() < 0) {
15241534
return NULL;
15251535
}

0 commit comments

Comments
 (0)