Skip to content

Commit

Permalink
Use PyLong_IsNegative()
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Nov 13, 2024
1 parent 53d9e73 commit 12675a9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/gmpy2_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ extern "C" {
# define _PyLong_DigitCount(obj) (((PyLongObject*)obj)->long_value.lv_tag >> 3)
#else
# define GET_OB_DIGIT(obj) obj->ob_digit
# define _PyLong_DigitCount(obj) (_PyLong_Sign(obj)<0 ? -Py_SIZE(obj):Py_SIZE(obj))
# define _PyLong_DigitCount(obj) (PyLong_IsNegative(obj) ? -Py_SIZE(obj):Py_SIZE(obj))
#endif

/* Since the macros are used in gmpy2's codebase, these functions are skipped
Expand Down
10 changes: 4 additions & 6 deletions src/gmpy2_convert_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* some basic types such as C longs or doubles.
*/

#include "pythoncapi_compat.h"

/* ======================================================================== *
* Conversion between native Python objects and MPZ. *
* ======================================================================== */
Expand All @@ -42,13 +44,9 @@
static int
mpz_set_PyLong(mpz_t z, PyObject *obj)
{
int negative;
Py_ssize_t len;
Py_ssize_t len = _PyLong_DigitCount(obj);
PyLongObject *templong = (PyLongObject*)obj;

len = _PyLong_DigitCount(obj);
negative = _PyLong_Sign(obj) < 0;

switch (len) {
case 1:
mpz_set_si(z, (sdigit)GET_OB_DIGIT(templong)[0]);
Expand All @@ -62,7 +60,7 @@ mpz_set_PyLong(mpz_t z, PyObject *obj)
GET_OB_DIGIT(templong));
}

if (negative) {
if (PyLong_IsNegative(obj)) {
mpz_neg(z, z);
}
return 0;
Expand Down

0 comments on commit 12675a9

Please sign in to comment.