Skip to content

Commit

Permalink
Add PyLong_GetSign() function (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner authored Jun 3, 2024
1 parent 4c2e17d commit 18d1df7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Latest version of the header file:
`pythoncapi_compat.h <https://raw.githubusercontent.com/python/pythoncapi-compat/master/pythoncapi_compat.h>`_.


Python 3.14
-----------

.. c:function:: int PyLong_GetSign(PyObject *obj, int *sign)
See `PyLong_GetSign() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_GetSign>`__.
Python 3.13
-----------
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog
=========

* 2024-06-03: Add ``PyLong_GetSign()``.
* 2024-04-23: Drop Python 3.5 support. It cannot be tested anymore (pip fails).
* 2024-04-02: Add ``PyDict_SetDefaultRef()`` function.
* 2024-03-29: Add ``PyList_GetItemRef()`` function.
Expand Down
15 changes: 15 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,21 @@ PyDict_SetDefaultRef(PyObject *d, PyObject *key, PyObject *default_value,
#endif


// gh-116560 added PyLong_GetSign() to Python 3.14a4
#if PY_VERSION_HEX < 0x030E00A1
static inline int PyLong_GetSign(PyObject *obj, int *sign)
{
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expect int, got %s", Py_TYPE(obj)->tp_name);
return -1;
}

*sign = _PyLong_Sign(obj);
return 0;
}
#endif


#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
// Marker to check that pointer value was set
static const char uninitialized[] = "uninitialized";
#define UNINITIALIZED_OBJ ((PyObject *)uninitialized)
#define UNINITIALIZED_INT 0x83ff979


static PyObject*
Expand Down Expand Up @@ -1413,6 +1414,11 @@ test_long_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
PyErr_Clear();
Py_DECREF(obj2);

// test PyLong_GetSign()
int sign = UNINITIALIZED_INT;
assert(PyLong_GetSign(obj, &sign) == 0);
assert(sign == 1);

Py_RETURN_NONE;
}

Expand Down

0 comments on commit 18d1df7

Please sign in to comment.