@@ -835,25 +835,17 @@ static PyObject *
835835MPZ_mpz (PyObject * self , PyObject * args )
836836{
837837 mpzobject * mpzp ;
838- PyObject * objp ;
839838
840839
841840#ifdef MPZ_DEBUG
842841 fputs ("MPZ_mpz() called...\n" , stderr );
843842#endif /* def MPZ_DEBUG */
844843
845- if (!PyArg_Parse (args , "O" , & objp ))
846- return NULL ;
847-
848844 /* at least we know it's some object */
849- /* note DON't Py_DECREF args NEITHER objp */
850-
851- if (PyInt_Check (objp )) {
852- long lval ;
853-
854- if (!PyArg_Parse (objp , "l" , & lval ))
855- return NULL ;
845+ /* note DON't Py_DECREF args */
856846
847+ if (PyInt_Check (args )) {
848+ long lval = PyInt_AS_LONG (args );
857849 if (lval == (long )0 ) {
858850 Py_INCREF (mpz_value_zero );
859851 mpzp = mpz_value_zero ;
@@ -866,7 +858,7 @@ MPZ_mpz(PyObject *self, PyObject *args)
866858 return NULL ;
867859 else mpz_set_si (& mpzp -> mpz , lval );
868860 }
869- else if (PyLong_Check (objp )) {
861+ else if (PyLong_Check (args )) {
870862 MP_INT mplongdigit ;
871863 int i ;
872864 unsigned char isnegative ;
@@ -880,13 +872,13 @@ MPZ_mpz(PyObject *self, PyObject *args)
880872
881873 /* how we're gonna handle this? */
882874 if ((isnegative =
883- ((i = ((PyLongObject * )objp )-> ob_size ) < 0 ) ))
875+ ((i = ((PyLongObject * )args )-> ob_size ) < 0 ) ))
884876 i = - i ;
885877
886878 while (i -- ) {
887879 mpz_set_ui (& mplongdigit ,
888880 (unsigned long )
889- ((PyLongObject * )objp )-> ob_digit [i ]);
881+ ((PyLongObject * )args )-> ob_digit [i ]);
890882 mpz_mul_2exp (& mplongdigit ,& mplongdigit ,
891883 (unsigned long int )i * SHIFT );
892884 mpz_ior (& mpzp -> mpz , & mpzp -> mpz , & mplongdigit );
@@ -898,9 +890,9 @@ MPZ_mpz(PyObject *self, PyObject *args)
898890 /* get rid of allocation for tmp variable */
899891 mpz_clear (& mplongdigit );
900892 }
901- else if (PyString_Check (objp )) {
902- unsigned char * cp = (unsigned char * )PyString_AS_STRING (objp );
903- int len = PyString_GET_SIZE (objp );
893+ else if (PyString_Check (args )) {
894+ unsigned char * cp = (unsigned char * )PyString_AS_STRING (args );
895+ int len = PyString_GET_SIZE (args );
904896 MP_INT mplongdigit ;
905897
906898 if ((mpzp = newmpzobject ()) == NULL )
@@ -923,9 +915,9 @@ MPZ_mpz(PyObject *self, PyObject *args)
923915 /* get rid of allocation for tmp variable */
924916 mpz_clear (& mplongdigit );
925917 }
926- else if (is_mpzobject (objp )) {
927- Py_INCREF (objp );
928- mpzp = (mpzobject * )objp ;
918+ else if (is_mpzobject (args )) {
919+ Py_INCREF (args );
920+ mpzp = (mpzobject * )args ;
929921 }
930922 else {
931923 PyErr_SetString (PyExc_TypeError ,
@@ -973,7 +965,7 @@ MPZ_powm(PyObject *self, PyObject *args)
973965 int tstres ;
974966
975967
976- if (!PyArg_Parse (args , "( OOO) " , & base , & exp , & mod ))
968+ if (!PyArg_ParseTuple (args , "OOO" , & base , & exp , & mod ))
977969 return NULL ;
978970
979971 if ((mpzbase = mpz_mpzcoerce (base )) == NULL
@@ -991,6 +983,14 @@ MPZ_powm(PyObject *self, PyObject *args)
991983 return (PyObject * )mpz_value_one ;
992984 }
993985
986+ if (mpz_cmp_ui (& mpzmod -> mpz , 0 ) == 0 ) {
987+ Py_DECREF (mpzbase );
988+ Py_DECREF (mpzexp );
989+ Py_DECREF (mpzmod );
990+ PyErr_SetString (PyExc_ValueError , "modulus cannot be 0" );
991+ return NULL ;
992+ }
993+
994994 if (tstres < 0 ) {
995995 MP_INT absexp ;
996996 /* negative exp */
@@ -1023,7 +1023,7 @@ MPZ_gcd(PyObject *self, PyObject *args)
10231023 mpzobject * z ;
10241024
10251025
1026- if (!PyArg_Parse (args , "(OO) " , & op1 , & op2 ))
1026+ if (!PyArg_ParseTuple (args , "OO " , & op1 , & op2 ))
10271027 return NULL ;
10281028
10291029 if ((mpzop1 = mpz_mpzcoerce (op1 )) == NULL
@@ -1052,7 +1052,7 @@ MPZ_gcdext(PyObject *self, PyObject *args)
10521052 mpzobject * g = NULL , * s = NULL , * t = NULL ;
10531053
10541054
1055- if (!PyArg_Parse (args , "(OO) " , & op1 , & op2 ))
1055+ if (!PyArg_ParseTuple (args , "OO " , & op1 , & op2 ))
10561056 return NULL ;
10571057
10581058 if ((mpzop1 = mpz_mpzcoerce (op1 )) == NULL
@@ -1086,15 +1086,11 @@ MPZ_gcdext(PyObject *self, PyObject *args)
10861086static PyObject *
10871087MPZ_sqrt (PyObject * self , PyObject * args )
10881088{
1089- PyObject * op ;
10901089 mpzobject * mpzop = NULL ;
10911090 mpzobject * z ;
10921091
10931092
1094- if (!PyArg_Parse (args , "O" , & op ))
1095- return NULL ;
1096-
1097- if ((mpzop = mpz_mpzcoerce (op )) == NULL
1093+ if ((mpzop = mpz_mpzcoerce (args )) == NULL
10981094 || (z = newmpzobject ()) == NULL ) {
10991095 Py_XDECREF (mpzop );
11001096 return NULL ;
@@ -1111,15 +1107,11 @@ MPZ_sqrt(PyObject *self, PyObject *args)
11111107static PyObject *
11121108MPZ_sqrtrem (PyObject * self , PyObject * args )
11131109{
1114- PyObject * op , * z = NULL ;
1110+ PyObject * z = NULL ;
11151111 mpzobject * mpzop = NULL ;
11161112 mpzobject * root = NULL , * rem = NULL ;
11171113
1118-
1119- if (!PyArg_Parse (args , "O" , & op ))
1120- return NULL ;
1121-
1122- if ((mpzop = mpz_mpzcoerce (op )) == NULL
1114+ if ((mpzop = mpz_mpzcoerce (args )) == NULL
11231115 || (z = PyTuple_New (2 )) == NULL
11241116 || (root = newmpzobject ()) == NULL
11251117 || (rem = newmpzobject ()) == NULL ) {
@@ -1212,7 +1204,7 @@ MPZ_divm(PyObject *self, PyObject *args)
12121204 mpzobject * z = NULL ;
12131205
12141206
1215- if (!PyArg_Parse (args , "( OOO) " , & num , & den , & mod ))
1207+ if (!PyArg_ParseTuple (args , "OOO" , & num , & den , & mod ))
12161208 return NULL ;
12171209
12181210 if ((mpznum = mpz_mpzcoerce (num )) == NULL
@@ -1550,17 +1542,17 @@ static PyTypeObject MPZtype = {
15501542
15511543static PyMethodDef mpz_functions [] = {
15521544#if 0
1553- {initialiser_name , MPZ_mpz , METH_OLDARGS },
1545+ {initialiser_name , MPZ_mpz , METH_O },
15541546#else /* 0 */
15551547 /* until guido ``fixes'' struct PyMethodDef */
1556- {(char * )initialiser_name , MPZ_mpz , METH_OLDARGS },
1548+ {(char * )initialiser_name , MPZ_mpz , METH_O },
15571549#endif /* 0 else */
1558- {"powm" , MPZ_powm , METH_OLDARGS },
1559- {"gcd" , MPZ_gcd , METH_OLDARGS },
1560- {"gcdext" , MPZ_gcdext , METH_OLDARGS },
1561- {"sqrt" , MPZ_sqrt , METH_OLDARGS },
1562- {"sqrtrem" , MPZ_sqrtrem , METH_OLDARGS },
1563- {"divm" , MPZ_divm , METH_OLDARGS },
1550+ {"powm" , MPZ_powm , METH_VARARGS },
1551+ {"gcd" , MPZ_gcd , METH_VARARGS },
1552+ {"gcdext" , MPZ_gcdext , METH_VARARGS },
1553+ {"sqrt" , MPZ_sqrt , METH_O },
1554+ {"sqrtrem" , MPZ_sqrtrem , METH_O },
1555+ {"divm" , MPZ_divm , METH_VARARGS },
15641556 {NULL , NULL } /* Sentinel */
15651557};
15661558
0 commit comments