8000 Add PyLong_IsPositive/Negative/Zero() functions by skirpichev · Pull Request #119 · python/pythoncapi-compat · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ Latest version of the header file:
Python 3.14
-----------

.. c:function:: int PyLong_IsPositive(PyObject *obj)

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

.. c:function:: int PyLong_IsNegative(PyObject *obj)

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

.. c:function:: int PyLong_IsZero(PyObject *obj)

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

.. 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>`__.
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

* 2024-11-12: Add functions:

* ``PyLong_IsPositive()``
* ``PyLong_IsNegative()``
* ``PyLong_IsZero()``

* 2024-10-09: Add functions:

* ``PyBytes_Join()``
Expand Down
30 changes: 30 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,36 @@ static inline int PyLong_GetSign(PyObject *obj, int *sign)
}
#endif

// gh-126061 added PyLong_IsPositive/Negative/Zero() to Python in 3.14.0a2
#if PY_VERSION_HEX < 0x030E00A2
static inline int PyLong_IsPositive(PyObject *obj)
{
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
return -1;
}
return _PyLong_Sign(obj) == 1;
}

static inline int PyLong_IsNegative(PyObject *obj)
{
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
return -1;
}
return _PyLong_Sign(obj) == -1;
}

static inline int PyLong_IsZero(PyObject *obj)
{
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
return -1;
}
return _PyLong_Sign(obj) == 0;
}
#endif


// gh-124502 added PyUnicode_Equal() to Python 3.14.0a0
#if PY_VERSION_HEX < 0x030E00A0
Expand Down
5 changes: 5 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,11 @@ test_long_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
assert(PyLong_GetSign(obj, &sign) == 0);
assert(sign == 1);

// test PyLong_IsPositive(), PyLong_IsNegative() and PyLong_IsZero()
assert(PyLong_IsPositive(obj) == 1);
assert(PyLong_IsNegative(obj) == 0);
assert(PyLong_IsZero(obj) == 0);

Py_RETURN_NONE;
}

Expand Down
0