1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
From: Sergey B Kirpichev <skirpichev@gmail.com>
Date: Mon, 22 Sep 2025 07:48:54 +0300
Subject: Adjust to_bytes() behavior to match CPython
Upstream cherry-pick. Fixes #1117764.
---
src/gmpy2_mpz_misc.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/gmpy2_mpz_misc.c b/src/gmpy2_mpz_misc.c
index 4beade5..8b3c772 100644
--- a/src/gmpy2_mpz_misc.c
+++ b/src/gmpy2_mpz_misc.c
@@ -1975,7 +1975,12 @@ GMPy_MPZ_Method_To_Bytes(PyObject *self, PyObject *const *args,
gap = length - size;
if (gap < 0 || sign < 0 ||
- (is_signed && length && mpz_tstbit(*px, 8*length - 1) == !is_negative))
+ (is_signed &&
+#if (PY_VERSION_HEX < 0x030D08F0 || (PY_VERSION_HEX >= 0x030E0000 \
+ && PY_VERSION_HEX < 0x030E00C3))
+ length &&
+#endif
+ mpz_tstbit(*px, 8*length - 1) == !is_negative))
{
OVERFLOW_ERROR("mpz too big to convert");
return NULL;
|