8000 Add PyLong_IsPositive/Negative/Zero() functions (#119) · python/pythoncapi-compat@77abeec · GitHub
[go: up one dir, main page]

Skip to content

Commit 77abeec

Browse files
skirpichevvstinner
andauthored
Add PyLong_IsPositive/Negative/Zero() functions (#119)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 0041177 commit 77abeec

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

docs/api.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ Latest version of the header file:
2929
Python 3.14
3030
-----------
3131

32+
.. c:function:: int PyLong_IsPositive(PyObject *obj)
33+
34+
See `PyLong_IsPositive() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_IsPositive>`__.
35+
36+
.. c:function:: int PyLong_IsNegative(PyObject *obj)
37+
38+
See `PyLong_IsNegative() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_IsNegative>`__.
39+
40+
.. c:function:: int PyLong_IsZero(PyObject *obj)
41+
42+
See `PyLong_IsZero() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_IsZero>`__.
43+
3244
.. c:function:: int PyLong_GetSign(PyObject *obj, int *sign)
3345

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

docs/changelog.rst

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

4+
* 2024-11-12: Add functions:
5+
6+
* ``PyLong_IsPositive()``
7+
* ``PyLong_IsNegative()``
8+
* ``PyLong_IsZero()``
9+
410
* 2024-10-09: Add functions:
511

612
* ``PyBytes_Join()``

pythoncapi_compat.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,36 @@ static inline int PyLong_GetSign(PyObject *obj, int *sign)
15201520
}
15211521
#endif
15221522

1523+
// gh-126061 added PyLong_IsPositive/Negative/Zero() to Python in 3.14.0a2
1524+
#if PY_VERSION_HEX < 0x030E00A2
1525+
static inline int PyLong_IsPositive(PyObject *obj)
1526+
{
1527+
if (!PyLong_Check(obj)) {
1528+
PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1529+
return -1;
1530+
}
1531+
return _PyLong_Sign(obj) == 1;
1532+
}
1533+
1534+
static inline int PyLong_IsNegative(PyObject *obj)
1535+
{
1536+
if (!PyLong_Check(obj)) {
1537+
PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1538+
return -1;
1539+
}
1540+
return _PyLong_Sign(obj) == -1;
1541+
}
1542+
1543+
static inline int PyLong_IsZero(PyObject *obj)
1544+
{
1545+
if (!PyLong_Check(obj)) {
1546+
PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1547+
return -1;
1548+
}
1549+
return _PyLong_Sign(obj) == 0;
1550+
}
1551+
#endif
1552+
15231553

15241554
// gh-124502 added PyUnicode_Equal() to Python 3.14.0a0
15251555
#if PY_VERSION_HEX < 0x030E00A0

tests/test_pythoncapi_compat_cext.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,11 @@ test_long_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
14211421
assert(PyLong_GetSign(obj, &sign) == 0);
14221422
assert(sign == 1);
14231423

1424+
// test PyLong_IsPositive(), PyLong_IsNegative() and PyLong_IsZero()
1425+
assert(PyLong_IsPositive(obj) == 1);
1426+
assert(PyLong_IsNegative(obj) == 0);
1427+
assert(PyLong_IsZero(obj) == 0);
1428+
14241429
Py_RETURN_NONE;
14251430
}
14261431

0 commit comments

Comments
 (0)
0