Skip to content

Commit 18d1df7

Browse files
authored
Add PyLong_GetSign() function (#99)
1 parent 4c2e17d commit 18d1df7

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

docs/api.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Latest version of the header file:
2626
`pythoncapi_compat.h <https://raw.githubusercontent.com/python/pythoncapi-compat/master/pythoncapi_compat.h>`_.
2727

2828

29+
Python 3.14
30+
-----------
31+
32+
.. c:function:: int PyLong_GetSign(PyObject *obj, int *sign)
33+
34+
See `PyLong_GetSign() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_GetSign>`__.
35+
2936
Python 3.13
3037
-----------
3138

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changelog
22
=========
33

4+
* 2024-06-03: Add ``PyLong_GetSign()``.
45
* 2024-04-23: Drop Python 3.5 support. It cannot be tested anymore (pip fails).
56
* 2024-04-02: Add ``PyDict_SetDefaultRef()`` function.
67
* 2024-03-29: Add ``PyList_GetItemRef()`` function.

pythoncapi_compat.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,21 @@ PyDict_SetDefaultRef(PyObject *d, PyObject *key, PyObject *default_value,
13391339
#endif
13401340

13411341

1342+
// gh-116560 added PyLong_GetSign() to Python 3.14a4
1343+
#if PY_VERSION_HEX < 0x030E00A1
1344+
static inline int PyLong_GetSign(PyObject *obj, int *sign)
1345+
{
1346+
if (!PyLong_Check(obj)) {
1347+
PyErr_Format(PyExc_TypeError, "expect int, got %s", Py_TYPE(obj)->tp_name);
1348+
return -1;
1349+
}
1350+
1351+
*sign = _PyLong_Sign(obj);
1352+
return 0;
1353+
}
1354+
#endif
1355+
1356+
13421357
#ifdef __cplusplus
13431358
}
13441359
#endif

tests/test_pythoncapi_compat_cext.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
// Marker to check that pointer value was set
5454
static const char uninitialized[] = "uninitialized";
5555
#define UNINITIALIZED_OBJ ((PyObject *)uninitialized)
56+
#define UNINITIALIZED_INT 0x83ff979
5657

5758

5859
static PyObject*
@@ -1413,6 +1414,11 @@ test_long_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
14131414
PyErr_Clear();
14141415
Py_DECREF(obj2);
14151416

1417+
// test PyLong_GetSign()
1418+
int sign = UNINITIALIZED_INT;
1419+
assert(PyLong_GetSign(obj, &sign) == 0);
1420+
assert(sign == 1);
1421+
14161422
Py_RETURN_NONE;
14171423
}
14181424

0 commit comments

Comments
 (0)