File python-gmpy2-mpfr421.patch of Package python-gmpy2
--- gmpy2-2.1.5/src/gmpy2_format.c.orig 2022-09-23 22:12:17.000000000 -0600
+++ gmpy2-2.1.5/src/gmpy2_format.c 2024-01-22 12:05:12.917508874 -0700
@@ -24,6 +24,10 @@
* License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+#if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,1)
+#define MPFR_SIGNED_NAN
+#endif
+
PyDoc_STRVAR(GMPy_doc_mpz_format,
"x.__format__(fmt) -> string\n\n"
"Return a Python string by formatting mpz 'x' using the format string\n"
@@ -206,6 +210,7 @@ GMPy_MPFR_Format(PyObject *self, PyObjec
int buflen;
int seensign = 0, seenalign = 0, seendecimal = 0, seendigits = 0;
int seenround = 0, seenconv = 0;
+ int skip = 0;
if (!MPFR_Check(self)) {
TYPE_ERROR("requires mpfr type");
@@ -318,6 +323,12 @@ GMPy_MPFR_Format(PyObject *self, PyObjec
buflen = mpfr_asprintf(&buffer, mpfrfmt, MPFR(self));
+#ifdef MPFR_SIGNED_NAN
+ if ((mpfr_nan_p(MPFR(self)) && (buffer[0] == '-' || buffer[0] == '+')) ||
+ (mpfr_inf_p(MPFR(self)) && buffer[0] == '+'))
+ skip = 1;
+#endif
+
/* If there isn't a decimal point in the output and the output
* only consists of digits, then append .0 */
if (strlen(buffer) == strspn(buffer, "+- 0123456789")) {
@@ -327,14 +338,14 @@ GMPy_MPFR_Format(PyObject *self, PyObjec
return PyErr_NoMemory();
}
*newbuf = '\0';
- strcat(newbuf, buffer);
+ strcat(newbuf, &buffer[skip]);
strcat(newbuf, ".0");
mpfr_free_str(buffer);
mpfrstr = Py_BuildValue("s", newbuf);
free(newbuf);
}
else {
- mpfrstr = Py_BuildValue("s", buffer);
+ mpfrstr = Py_BuildValue("s", &buffer[skip]);
mpfr_free_str(buffer);
}
if (!mpfrstr) {
@@ -385,6 +396,7 @@ GMPy_MPC_Format(PyObject *self, PyObject
int rbuflen, ibuflen;
int seensign = 0, seenalign = 0, seendecimal = 0, seendigits = 0;
int seenround = 0, seenconv = 0, seenstyle = 0, mpcstyle = 0;
+ int realskip = 0, imagskip = 0;
if (!MPC_Check(self)) {
TYPE_ERROR("requires 'mpc' object");
@@ -537,6 +549,12 @@ GMPy_MPC_Format(PyObject *self, PyObject
rbuflen = mpfr_asprintf(&realbuf, rfmt,
mpc_realref(MPC(self)));
+#ifdef MPFR_SIGNED_NAN
+ if ((mpfr_nan_p(mpc_realref(MPC(self))) && (realbuf[0] == '-' || realbuf[0] == '+')) ||
+ (mpfr_inf_p(mpc_realref(MPC(self))) && realbuf[0] == '+'))
+ realskip = 1;
+#endif
+
if (rbuflen < 0) {
mpfr_free_str(realbuf);
SYSTEM_ERROR("Internal error in mpfr_asprintf");
@@ -560,6 +578,12 @@ GMPy_MPC_Format(PyObject *self, PyObject
ibuflen = mpfr_asprintf(&imagbuf, ifmt,
mpc_imagref(MPC(self)));
+#ifdef MPFR_SIGNED_NAN
+ if ((mpfr_nan_p(mpc_imagref(MPC(self))) && (imagbuf[0] == '-' || imagbuf[0] == '+')) ||
+ (mpfr_inf_p(mpc_imagref(MPC(self))) && imagbuf[0] == '+'))
+ imagskip = 1;
+#endif
+
if (ibuflen < 0) {
mpfr_free_str(realbuf);
mpfr_free_str(imagbuf);
@@ -580,7 +604,7 @@ GMPy_MPC_Format(PyObject *self, PyObject
tempbuf[0] = '\00';
if (mpcstyle)
strcat(tempbuf, "(");
- strcat(tempbuf, realbuf);
+ strcat(tempbuf, &realbuf[realskip]);
/* If there isn't a decimal point in the output and the output
* is short and only consists of digits, then append .0 */
@@ -599,7 +623,7 @@ GMPy_MPC_Format(PyObject *self, PyObject
strcat(tempbuf, "+");
}
}
- strcat(tempbuf, imagbuf);
+ strcat(tempbuf, &imagbuf[imagskip]);
if (strlen(imagbuf) < 50 &&
strlen(imagbuf) == strspn(imagbuf, "+- 0123456789")) {
strcat(tempbuf, ".0");