Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c17b3cf

Browse files
committed
Richard Fish <[email protected]>:
Fix the .binary() method of mpz objects for 64-bit systems. [Also removed a lot of trailing whitespace elsewhere in the file. --FLD] This closes SF patch #103547.
1 parent 26d813e commit c17b3cf

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

Modules/mpzmodule.c

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ mpz_divide(mpzobject *a, mpzobject *b)
417417
putc('\n', stderr);
418418
#endif /* def MPZ_TEST_DIV */
419419
return (PyObject *)z;
420-
420+
421421
} /* mpz_divide() */
422422

423423
static PyObject *
@@ -428,7 +428,7 @@ mpz_remainder(mpzobject *a, mpzobject *b)
428428
#endif /* def MPZ_SPARE_MALLOC */
429429
mpzobject *z;
430430

431-
431+
432432
if ((
433433
#ifdef MPZ_SPARE_MALLOC
434434
cmpres =
@@ -486,7 +486,7 @@ mpz_remainder(mpzobject *a, mpzobject *b)
486486
putc('\n', stderr);
487487
#endif /* def MPZ_TEST_DIV */
488488
return (PyObject *)z;
489-
489+
490490
} /* mpz_remainder() */
491491

492492
static PyObject *
@@ -543,7 +543,7 @@ mpz_div_and_mod(mpzobject *a, mpzobject *b)
543543

544544
(void)PyTuple_SetItem(z, 0, (PyObject *)x);
545545
(void)PyTuple_SetItem(z, 1, (PyObject *)y);
546-
546+
547547
return z;
548548
} /* mpz_div_and_mod() */
549549

@@ -570,7 +570,7 @@ mpz_power(mpzobject *a, mpzobject *b, mpzobject *m)
570570
Py_INCREF(mpz_value_one);
571571
return (PyObject *)mpz_value_one;
572572
}
573-
573+
574574
if (cmpres < 0) {
575575
PyErr_SetString(PyExc_ValueError,
576576
"mpz.pow to negative exponent");
@@ -631,9 +631,9 @@ mpz_power(mpzobject *a, mpzobject *b, mpzobject *m)
631631

632632
if ((z = newmpzobject()) == NULL)
633633
return NULL;
634-
634+
635635
mpz_pow_ui(&z->mpz, &a->mpz, mpz_get_ui(&b->mpz));
636-
636+
637637
return (PyObject *)z;
638638
} /* mpz_power() */
639639

@@ -643,7 +643,7 @@ mpz_negative(mpzobject *v)
643643
{
644644
mpzobject *z;
645645

646-
646+
647647
#ifdef MPZ_SPARE_MALLOC
648648
if (mpz_cmp_ui(&v->mpz, (unsigned long int)0) == 0) {
649649
/* -0 == 0 */
@@ -673,7 +673,7 @@ mpz_absolute(mpzobject *v)
673673
{
674674
mpzobject *z;
675675

676-
676+
677677
if (mpz_cmp_ui(&v->mpz, (unsigned long int)0) >= 0) {
678678
Py_INCREF(v);
679679
return (PyObject *)v;
@@ -791,7 +791,7 @@ void
791791
mpz_xor(MP_INT *res, const MP_INT *op1, const MP_INT *op2)
792792
{
793793
MP_INT tmpmpz;
794-
794+
795795
mpz_init(&tmpmpz);
796796

797797
mpz_and(res, op1, op2);
@@ -854,7 +854,7 @@ MPZ_mpz(PyObject *self, PyObject *args)
854854

855855
if (!PyArg_Parse(objp, "l", &lval))
856856
return NULL;
857-
857+
858858
if (lval == (long)0) {
859859
Py_INCREF(mpz_value_zero);
860860
mpzp = mpz_value_zero;
@@ -871,14 +871,14 @@ MPZ_mpz(PyObject *self, PyObject *args)
871871
MP_INT mplongdigit;
872872
int i;
873873
unsigned char isnegative;
874-
874+
875875

876876
if ((mpzp = newmpzobject()) == NULL)
877877
return NULL;
878878

879879
mpz_set_si(&mpzp->mpz, 0L);
880880
mpz_init(&mplongdigit);
881-
881+
882882
/* how we're gonna handle this? */
883883
if ((isnegative =
884884
((i = ((PyLongObject *)objp)->ob_size) < 0) ))
@@ -909,7 +909,7 @@ MPZ_mpz(PyObject *self, PyObject *args)
909909

910910
mpz_set_si(&mpzp->mpz, 0L);
911911
mpz_init(&mplongdigit);
912-
912+
913913
/* let's do it the same way as with the long conversion:
914914
without thinking how it can be faster (-: :-) */
915915

@@ -973,7 +973,7 @@ MPZ_powm(PyObject *self, PyObject *args)
973973
mpzobject *z;
974974
int tstres;
975975

976-
976+
977977
if (!PyArg_Parse(args, "(OOO)", &base, &exp, &mod))
978978
return NULL;
979979

@@ -1007,7 +1007,7 @@ MPZ_powm(PyObject *self, PyObject *args)
10071007
else {
10081008
mpz_powm(&z->mpz, &mpzbase->mpz, &mpzexp->mpz, &mpzmod->mpz);
10091009
}
1010-
1010+
10111011
Py_DECREF(mpzbase);
10121012
Py_DECREF(mpzexp);
10131013
Py_DECREF(mpzmod);
@@ -1023,7 +1023,7 @@ MPZ_gcd(PyObject *self, PyObject *args)
10231023
mpzobject *mpzop1 = NULL, *mpzop2 = NULL;
10241024
mpzobject *z;
10251025

1026-
1026+
10271027
if (!PyArg_Parse(args, "(OO)", &op1, &op2))
10281028
return NULL;
10291029

@@ -1052,7 +1052,7 @@ MPZ_gcdext(PyObject *self, PyObject *args)
10521052
mpzobject *mpzop1 = NULL, *mpzop2 = NULL;
10531053
mpzobject *g = NULL, *s = NULL, *t = NULL;
10541054

1055-
1055+
10561056
if (!PyArg_Parse(args, "(OO)", &op1, &op2))
10571057
return NULL;
10581058

@@ -1091,7 +1091,7 @@ MPZ_sqrt(PyObject *self, PyObject *args)
10911091
mpzobject *mpzop = NULL;
10921092
mpzobject *z;
10931093

1094-
1094+
10951095
if (!PyArg_Parse(args, "O", &op))
10961096
return NULL;
10971097

@@ -1116,7 +1116,7 @@ MPZ_sqrtrem(PyObject *self, PyObject *args)
11161116
mpzobject *mpzop = NULL;
11171117
mpzobject *root = NULL, *rem = NULL;
11181118

1119-
1119+
11201120
if (!PyArg_Parse(args, "O", &op))
11211121
return NULL;
11221122

@@ -1189,7 +1189,7 @@ mpz_divm(MP_INT *res, const MP_INT *num, const MP_INT *den, const MP_INT *mod)
11891189
}
11901190
else
11911191
mpz_mod(res, &s0, mod);
1192-
1192+
11931193
#else /* def MPZ_MDIV_BUG */
11941194
mpz_mmod(res, &s0, mod);
11951195
#endif /* def MPZ_MDIV_BUG else */
@@ -1212,7 +1212,7 @@ MPZ_divm(PyObject *self, PyObject *args)
12121212
mpzobject *mpznum, *mpzden = NULL, *mpzmod = NULL;
12131213
mpzobject *z = NULL;
12141214

1215-
1215+
12161216
if (!PyArg_Parse(args, "(OOO)", &num, &den, &mod))
12171217
return NULL;
12181218

@@ -1225,7 +1225,7 @@ MPZ_divm(PyObject *self, PyObject *args)
12251225
Py_XDECREF(mpzmod);
12261226
return NULL;
12271227
}
1228-
1228+
12291229
mpz_divm(&z->mpz, &mpznum->mpz, &mpzden->mpz, &mpzmod->mpz);
12301230

12311231
Py_DECREF(mpznum);
@@ -1340,19 +1340,19 @@ mpz_long(mpzobject *self)
13401340
while (ldcount > 0 && longobjp->ob_digit[ldcount-1] == 0)
13411341
ldcount--;
13421342
longobjp->ob_size = ldcount;
1343-
1343+
13441344

13451345
if (isnegative)
13461346
longobjp->ob_size = -longobjp->ob_size;
13471347

13481348
return (PyObject *)longobjp;
1349-
1349+
13501350
} /* mpz_long() */
13511351

13521352

13531353
/* I would have avoided pow() anyways, so ... */
13541354
static const double multiplier = 256.0 * 256.0 * 256.0 * 256.0;
1355-
1355+
13561356
#ifdef MPZ_CONVERSIONS_AS_METHODS
13571357
static PyObject *
13581358
mpz_float(mpzobject *self, PyObject *args)
@@ -1373,7 +1373,7 @@ mpz_float(mpzobject *self)
13731373
#endif /* def MPZ_CONVERSIONS_AS_METHODS */
13741374

13751375
i = (int)mpz_size(&self->mpz);
1376-
1376+
13771377
/* determine sign, and copy abs(self) to scratch var */
13781378
if ((isnegative = (mpz_cmp_ui(&self->mpz, (unsigned long int)0) < 0)))
13791379
{
@@ -1404,7 +1404,7 @@ mpz_float(mpzobject *self)
14041404
x = -x;
14051405

14061406
return PyFloat_FromDouble(x);
1407-
1407+
14081408
} /* mpz_float() */
14091409

14101410
#ifdef MPZ_CONVERSIONS_AS_METHODS
@@ -1419,10 +1419,10 @@ mpz_hex(mpzobject *self)
14191419
if (!PyArg_NoArgs(args))
14201420
return NULL;
14211421
#endif /* def MPZ_CONVERSIONS_AS_METHODS */
1422-
1422+
14231423
return mpz_format((PyObject *)self, 16, (unsigned char)1);
14241424
} /* mpz_hex() */
1425-
1425+
14261426
#ifdef MPZ_CONVERSIONS_AS_METHODS
14271427
static PyObject *
14281428
mpz_oct(mpzobject *self, PyObject *args)
@@ -1438,7 +1438,7 @@ mpz_oct(mpzobject *self)
14381438

14391439
return mpz_format((PyObject *)self, 8, (unsigned char)1);
14401440
} /* mpz_oct() */
1441-
1441+
14421442
static PyObject *
14431443
mpz_binary(mpzobject *self, PyObject *args)
14441444
{
@@ -1447,7 +1447,7 @@ mpz_binary(mpzobject *self, PyObject *args)
14471447
char *cp;
14481448
MP_INT mp;
14491449
unsigned long ldigit;
1450-
1450+
14511451
if (!PyArg_NoArgs(args))
14521452
return NULL;
14531453

@@ -1477,14 +1477,20 @@ mpz_binary(mpzobject *self, PyObject *args)
14771477
*cp++ = (unsigned char)((ldigit >>= 8) & 0xFF);
14781478
*cp++ = (unsigned char)((ldigit >>= 8) & 0xFF);
14791479
*cp++ = (unsigned char)((ldigit >>= 8) & 0xFF);
1480+
if (sizeof(ldigit) == 8 && BITS_PER_MP_LIMB == 64) {
1481+
*cp++ = (unsigned char)((ldigit >>= 8) & 0xFF);
1482+
*cp++ = (unsigned char)((ldigit >>= 8) & 0xFF);
1483+
*cp++ = (unsigned char)((ldigit >>= 8) & 0xFF);
1484+
*cp++ = (unsigned char)((ldigit >>= 8) & 0xFF);
1485+
}
14801486
}
14811487

14821488
while (strobjp->ob_size && !*--cp)
14831489
strobjp->ob_size--;
14841490

14851491
return (PyObject *)strobjp;
14861492
} /* mpz_binary() */
1487-
1493+
14881494

14891495
static PyMethodDef mpz_methods[] = {
14901496
#ifdef MPZ_CONVERSIONS_AS_METHODS

0 commit comments

Comments
 (0)