Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mpz_set_PyLong() signature (return an integer) #535

Merged
merged 2 commits into from
Dec 10, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/build_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04, macos-12, macos-14, windows-2022]
os: [ubuntu-22.04, macos-13, macos-14, windows-2022]
steps:
- uses: actions/checkout@v4
- if: runner.os == 'macOS'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pip_install_gmpy2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
python-version: [3.11, 3.12, 3.13]
os: [macos-12]
os: [macos-13]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
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
Loading