Skip to content

Commit

Permalink
Update mpz_set_PyLong() signature (return an integer)
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Dec 9, 2024
1 parent 436d695 commit ed0c342
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 31 deletions.
14 changes: 12 additions & 2 deletions src/gmpy2_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ GMPy_Integer_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_add(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -88,7 +93,12 @@ GMPy_Integer_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, x);
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_add(result->z, result->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down
40 changes: 30 additions & 10 deletions src/gmpy2_convert_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* ======================================================================== */

/* To support creation of temporary mpz objects. */
static void
static int
mpz_set_PyLong(mpz_t z, PyObject *obj)
{
Py_ssize_t len = _PyLong_DigitCount(obj);
Expand All @@ -63,7 +63,7 @@ mpz_set_PyLong(mpz_t z, PyObject *obj)
if (PyLong_IsNegative(obj)) {
mpz_neg(z, z);
}
return;
return 0;
}

static MPZ_Object *
Expand All @@ -77,7 +77,12 @@ GMPy_MPZ_From_PyLong(PyObject *obj, CTXT_Object *context)
/* LCOV_EXCL_STOP */
}

mpz_set_PyLong(MPZ(result), obj);
if (mpz_set_PyLong(MPZ(result), obj)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}

return result;
}
Expand Down Expand Up @@ -367,7 +372,12 @@ GMPy_XMPZ_From_PyLong(PyObject *obj, CTXT_Object *context)
/* LCOV_EXCL_STOP */
}

mpz_set_PyLong(result->z, obj);
if (mpz_set_PyLong(result->z, obj)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}

return result;
}
Expand Down Expand Up @@ -830,16 +840,26 @@ GMPy_MPQ_From_Fraction(PyObject* obj, CTXT_Object *context)
den = PyObject_GetAttrString(obj, "denominator");
if (!num || !PyLong_Check(num) || !den || !PyLong_Check(den)) {
SYSTEM_ERROR("Object does not appear to be Fraction");
Py_XDECREF(num);
Py_XDECREF(den);
Py_DECREF((PyObject*)result);
return NULL;
goto error;
}
if (mpz_set_PyLong(mpq_numref(result->q), num)) {
/* LCOV_EXCL_START */
goto error;
/* LCOV_EXCL_STOP */
}
if (mpz_set_PyLong(mpq_denref(result->q), den)) {
/* LCOV_EXCL_START */
goto error;
/* LCOV_EXCL_STOP */
}
mpz_set_PyLong(mpq_numref(result->q), num);
mpz_set_PyLong(mpq_denref(result->q), den);
Py_DECREF(num);
Py_DECREF(den);
return result;
error:
Py_XDECREF(num);
Py_XDECREF(den);
Py_DECREF((PyObject*)result);
return NULL;
}

static MPQ_Object*
Expand Down
18 changes: 16 additions & 2 deletions src/gmpy2_divmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ GMPy_Integer_DivModWithType(PyObject *x, int xtype, PyObject *y, int ytype,

if (error) {
/* Use quo->z as a temporary variable. */
mpz_set_PyLong(quo->z, y);
if (mpz_set_PyLong(quo->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)rem);
Py_DECREF((PyObject*)quo);
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_qr(quo->z, rem->z, MPZ(x), quo->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -94,7 +101,14 @@ GMPy_Integer_DivModWithType(PyObject *x, int xtype, PyObject *y, int ytype,
goto error;
}
else {
mpz_set_PyLong(quo->z, x);
if (mpz_set_PyLong(quo->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)rem);
Py_DECREF((PyObject*)quo);
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_qr(quo->z, rem->z, quo->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down
14 changes: 12 additions & 2 deletions src/gmpy2_floordiv.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ GMPy_Integer_FloorDivWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_q(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -91,7 +96,12 @@ GMPy_Integer_FloorDivWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}

if (IS_TYPE_PyInteger(xtype)) {
mpz_set_PyLong(result->z, x);
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_q(result->z, result->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down
14 changes: 12 additions & 2 deletions src/gmpy2_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ GMPy_Integer_ModWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_r(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -90,7 +95,12 @@ GMPy_Integer_ModWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}

if (PyLong_Check(x)) {
mpz_set_PyLong(result->z, x);
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
mpz_fdiv_r(result->z, result->z, MPZ(y));
return (PyObject*)result;
}
Expand Down
14 changes: 12 additions & 2 deletions src/gmpy2_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ GMPy_Integer_MulWithType(PyObject *x, int xtype, PyObject *y, int ytype,
mpz_mul_si(result->z, MPZ(x), temp);
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_mul(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -77,7 +82,12 @@ GMPy_Integer_MulWithType(PyObject *x, int xtype, PyObject *y, int ytype,
mpz_mul_si(result->z, MPZ(y), temp);
}
else {
mpz_set_PyLong(result->z, x);
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_mul(result->z, result->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down
7 changes: 6 additions & 1 deletion src/gmpy2_richcompare.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ GMPy_RichCompare_Slot(PyObject *a, PyObject *b, int op)
else {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, b);
if (mpz_set_PyLong(tempz, b)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
c = mpz_cmp(MPZ(a), tempz);
mpz_clear(tempz);
}
Expand Down
14 changes: 12 additions & 2 deletions src/gmpy2_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ GMPy_Integer_SubWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_sub(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -87,8 +92,13 @@ GMPy_Integer_SubWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_set_PyLong(result->z, x);
mpz_sub(result->z, result->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
}
Expand Down
56 changes: 48 additions & 8 deletions src/gmpy2_xmpz_inplace.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ GMPy_XMPZ_IAdd_Slot(PyObject *self, PyObject *other)
else {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, other);
if (mpz_set_PyLong(tempz, other)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_add(MPZ(self), MPZ(self), tempz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -103,7 +108,12 @@ GMPy_XMPZ_ISub_Slot(PyObject *self, PyObject *other)
else {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, other);
if (mpz_set_PyLong(tempz, other)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_sub(MPZ(self), MPZ(self), tempz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -145,7 +155,12 @@ GMPy_XMPZ_IMul_Slot(PyObject *self, PyObject *other)
else {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, other);
if (mpz_set_PyLong(tempz, other)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_mul(MPZ(self), MPZ(self), tempz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -199,7 +214,12 @@ GMPy_XMPZ_IFloorDiv_Slot(PyObject *self, PyObject *other)
else {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, other);
if (mpz_set_PyLong(tempz, other)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_q(MPZ(self), MPZ(self), tempz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -254,7 +274,12 @@ GMPy_XMPZ_IRem_Slot(PyObject *self, PyObject *other)
else {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, other);
if (mpz_set_PyLong(tempz, other)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_r(MPZ(self), MPZ(self), tempz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -350,7 +375,12 @@ GMPy_XMPZ_IAnd_Slot(PyObject *self, PyObject *other)
if (PyLong_Check(other)) {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, other);
if (mpz_set_PyLong(tempz, other)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_and(MPZ(self), MPZ(self), tempz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -382,7 +412,12 @@ GMPy_XMPZ_IXor_Slot(PyObject *self, PyObject *other)
if(PyLong_Check(other)) {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, other);
if (mpz_set_PyLong(tempz, other)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_xor(MPZ(self), MPZ(self), tempz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -414,7 +449,12 @@ GMPy_XMPZ_IIor_Slot(PyObject *self, PyObject *other)
if(PyLong_Check(other)) {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, other);
if (mpz_set_PyLong(tempz, other)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_ior(MPZ(self), MPZ(self), tempz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down

0 comments on commit ed0c342

Please sign in to comment.