Skip to content

Commit 207a24f

Browse files
committed
Add PyLong_IsPositive/Zero/Negative() functions
1 parent 782217f commit 207a24f

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

Doc/c-api/long.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,42 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
571571
.. versionadded:: 3.14
572572
573573
574+
.. c:function:: int PyLong_IsPositive(PyObject *obj)
575+
576+
Check if the integer object *obj* is positive.
577+
578+
On success, return 1 if *obj* is positive, and 0 otherwise.
579+
580+
On failure, return -1 with an exception set. This function always succeeds
581+
if *obj* is a :c:type:`PyLongObject` or its subtype.
582+
583+
.. versionadded:: 3.14
584+
585+
586+
.. c:function:: int PyLong_IsNegative(PyObject *obj)
587+
588+
Check if the integer object *obj* is negative.
589+
590+
On success, return 1 if *obj* is negative, and 0 otherwise.
591+
592+
On failure, return -1 with an exception set. This function always succeeds
593+
if *obj* is a :c:type:`PyLongObject` or its subtype.
594+
595+
.. versionadded:: 3.14
596+
597+
598+
.. c:function:: int PyLong_IsZero(PyObject *obj)
599+
600+
Check if the integer object *obj* is zero.
601+
602+
On success, return 1 if *obj* is zero, and 0 if it is non-zero.
603+
604+
On failure, return -1 with an exception set. This function always succeeds
605+
if *obj* is a :c:type:`PyLongObject` or its subtype.
606+
607+
.. versionadded:: 3.14
608+
609+
574610
.. c:function:: PyObject* PyLong_GetInfo(void)
575611
576612
On success, return a read only :term:`named tuple`, that holds

Doc/whatsnew/3.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ New Features
479479
an interned string and deallocate it during module shutdown.
480480
(Contribued by Eddie Elizondo in :gh:`113601`.)
481481

482+
* Add :c:func:`PyLong_IsPositive`, :c:func:`PyLong_IsNegative`
483+
and :c:func:`PyLong_IsZero` for checking if :c:type:`PyLongObject`
484+
is positive, negative, or zero.
485+
(Contribued by James Roy in :gh:`126061`.)
486+
482487
* Add new functions to convert C ``<stdint.h>`` numbers from/to Python
483488
:class:`int`:
484489

Include/cpython/longobject.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ PyAPI_FUNC(PyObject*) PyLong_FromUnsignedNativeBytes(const void* buffer,
5858
PyAPI_FUNC(int) PyUnstable_Long_IsCompact(const PyLongObject* op);
5959
PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(const PyLongObject* op);
6060

61+
/* PyLong_IsPositive. Check if the integer object is positive.
62+
63+
- On success, return 1 if *obj is positive, and 0 otherwise.
64+
- On failure, set an exception, and return -1. */
65+
PyAPI_FUNC(int) PyLong_IsPositive(PyObject *obj);
66+
67+
/* PyLong_IsNegative. Check if the integer object is negative.
68+
69+
- On success, return 1 if *obj is negative, and 0 otherwise.
70+
- On failure, set an exception, and return -1. */
71+
PyAPI_FUNC(int) PyLong_IsNegative(PyObject *obj);
72+
73+
/* PyLong_IsZero. Check if the integer object is zero.
74+
75+
- On success, return 1 if *obj is zero, and 0 if it is non-zero.
76+
- On failure, set an exception, and return -1. */
77+
PyAPI_FUNC(int) PyLong_IsZero(PyObject *obj);
78+
6179
/* PyLong_GetSign. Get the sign of an integer object:
6280
0, -1 or +1 for zero, negative or positive integer, respectively.
6381
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :c:func:`PyLong_IsPositive`, :c:func:`PyLong_IsNegative`
2+
and :c:func:`PyLong_IsZero` for checking if :c:type:`PyLongObject`
3+
is positive, negative, or zero.

Objects/longobject.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,36 @@ PyLong_AsUnsignedLongMask(PyObject *op)
769769
return val;
770770
}
771771

772+
int
773+
PyLong_IsPositive(PyObject *obj)
774+
{
775+
if (!PyLong_Check(obj)) {
776+
PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
777+
return -1;
778+
}
779+
return _PyLong_IsPositive((PyLongObject *)obj);
780+
}
781+
782+
int
783+
PyLong_IsNegative(PyObject *obj)
784+
{
785+
if (!PyLong_Check(obj)) {
786+
PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
787+
return -1;
788+
}
789+
return _PyLong_IsNegative((PyLongObject *)obj);
790+
}
791+
792+
int
793+
PyLong_IsZero(PyObject *obj)
794+
{
795+
if (!PyLong_Check(obj)) {
796+
PyErr_Format(PyExc_TypeError, "expect int, got %T", obj);
797+
return -1;
798+
}
799+
return _PyLong_IsZero((PyLongObject *)obj);
800+
}
801+
772802
int
773803
_PyLong_Sign(PyObject *vv)
774804
{

0 commit comments

Comments
 (0)