Cleanup of argument type-checking. Tested 2.7, 3.8, and 3.9. Completed 'abs' and '+'.

This commit is contained in:
Case Van Horsen 2020-11-19 21:14:09 -08:00
parent 286fb1aa1d
commit 85f40dba2a
17 changed files with 382 additions and 491 deletions

View file

@ -324,7 +324,7 @@ typedef struct {
#define Py2or3String_1Char(obj) (PyUnicode_READY(obj) ? (Py_UCS4)0 : PyUnicode_READ_CHAR(obj, 0))
#define PyStrOrUnicode_Check(op) (PyBytes_Check(op) || PyUnicode_Check(op))
#define PyIntOrLong_FromLong PyLong_FromLong
#define PyIntOrLong_Check(op) PyLong_Check(op)
#define PyIntOrLong_Check(op) (PyLong_Check(op))
#define PyIntOrLong_CheckExact(op) PyLong_CheckExact(op)
#define PyIntOrLong_FromSize_t PyLong_FromSize_t
#define PyIntOrLong_FromSsize_t PyLong_FromSsize_t

View file

@ -43,20 +43,20 @@
* GMPy_MPFR_Abs_Slot
* GMPy_MPC_Abs_Slot
*
* GMPy_Integer_Abs(Integer, context|NULL)
* GMPy_Rational_Abs(Rational, context|NULL)
* GMPy_Real_Abs(Real, context|NULL)
* GMPy_Complex_Abs(Complex, context|NULL)
* GMPy_Integer_AbsWithType(Integer, xtype, context|NULL)
* GMPy_Rational_AbsWithType(Rational, xtype, context|NULL)
* GMPy_Real_AbsWithType(Real, xtype, context|NULL)
* GMPy_Complex_AbsWithType(Complex, xtype, context|NULL)
*
* GMPy_Context_Abs(context, obj)
*/
static PyObject *
GMPy_Integer_Abs(PyObject *x, CTXT_Object *context)
GMPy_Integer_AbsWithType(PyObject *x, int xtype, CTXT_Object *context)
{
MPZ_Object *result = NULL;
if (MPZ_Check(x)) {
if (IS_TYPE_MPZ(xtype)) {
if (mpz_sgn(MPZ(x)) >= 0) {
Py_INCREF(x);
return x;
@ -76,7 +76,7 @@ GMPy_Integer_Abs(PyObject *x, CTXT_Object *context)
* if passed an MPZ).
*/
if ((result = GMPy_MPZ_From_Integer(x, context))) {
if ((result = GMPy_MPZ_From_IntegerWithType(x, xtype, context))) {
mpz_abs(result->z, result->z);
}
@ -86,15 +86,15 @@ GMPy_Integer_Abs(PyObject *x, CTXT_Object *context)
static PyObject *
GMPy_MPZ_Abs_Slot(MPZ_Object *x)
{
return GMPy_Integer_Abs((PyObject*)x, NULL);
return GMPy_Integer_AbsWithType((PyObject*)x, OBJ_TYPE_MPZ, NULL);
}
static PyObject *
GMPy_Rational_Abs(PyObject *x, CTXT_Object *context)
GMPy_Rational_AbsWithType(PyObject *x, int xtype, CTXT_Object *context)
{
MPQ_Object *result = NULL;
if (MPQ_Check(x)) {
if (IS_TYPE_MPQ(xtype)) {
if (mpz_sgn(mpq_numref(MPQ(x))) >= 0) {
Py_INCREF(x);
return x;
@ -114,7 +114,7 @@ GMPy_Rational_Abs(PyObject *x, CTXT_Object *context)
* would do if passed an MPQ).
*/
if ((result = GMPy_MPQ_From_Rational(x, context))) {
if ((result = GMPy_MPQ_From_RationalWithType(x, xtype, context))) {
mpz_abs(mpq_numref(result->q), mpq_numref(result->q));
}
@ -124,17 +124,17 @@ GMPy_Rational_Abs(PyObject *x, CTXT_Object *context)
static PyObject *
GMPy_MPQ_Abs_Slot(MPQ_Object *x)
{
return GMPy_Rational_Abs((PyObject*)x, NULL);
return GMPy_Rational_AbsWithType((PyObject*)x, OBJ_TYPE_MPQ, NULL);
}
static PyObject *
GMPy_Real_Abs(PyObject *x, CTXT_Object *context)
GMPy_Real_AbsWithType(PyObject *x, int xtype, CTXT_Object *context)
{
MPFR_Object *result = NULL, *tempx = NULL;
CHECK_CONTEXT(context);
if (!(tempx = GMPy_MPFR_From_Real(x, 1, context)) ||
if (!(tempx = GMPy_MPFR_From_RealWithType(x, xtype, 1, context)) ||
!(result = GMPy_MPFR_New(0, context))) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)tempx);
@ -155,18 +155,18 @@ GMPy_Real_Abs(PyObject *x, CTXT_Object *context)
static PyObject *
GMPy_MPFR_Abs_Slot(MPFR_Object *x)
{
return GMPy_Real_Abs((PyObject*)x, NULL);
return GMPy_Real_AbsWithType((PyObject*)x, OBJ_TYPE_MPFR, NULL);
}
static PyObject *
GMPy_Complex_Abs(PyObject *x, CTXT_Object *context)
GMPy_Complex_AbsWithType(PyObject *x, int xtype, CTXT_Object *context)
{
MPFR_Object *result = NULL;
MPC_Object *tempx = NULL;
CHECK_CONTEXT(context);
if (!(tempx = GMPy_MPC_From_Complex(x, 1, 1, context)) ||
if (!(tempx = GMPy_MPC_From_ComplexWithType(x, xtype, 1, 1, context)) ||
!(result = GMPy_MPFR_New(0, context))) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)tempx);
@ -182,32 +182,37 @@ GMPy_Complex_Abs(PyObject *x, CTXT_Object *context)
_GMPy_MPFR_Cleanup(&result, context);
return (PyObject*)result;
}
static PyObject *
GMPy_MPC_Abs_Slot(MPC_Object *x)
{
return GMPy_Complex_Abs((PyObject*)x, NULL);
return GMPy_Complex_AbsWithType((PyObject*)x, OBJ_TYPE_MPC, NULL);
}
static PyObject *
GMPy_Number_AbsWithType(PyObject *x, int xtype, CTXT_Object *context)
{
if (IS_TYPE_INTEGER(xtype))
return GMPy_Integer_AbsWithType(x, xtype, context);
if (IS_TYPE_RATIONAL(xtype))
return GMPy_Rational_AbsWithType(x, xtype, context);
if (IS_TYPE_REAL(xtype))
return GMPy_Real_AbsWithType(x, xtype, context);
if (IS_TYPE_COMPLEX(xtype))
return GMPy_Complex_AbsWithType(x, xtype, context);
TYPE_ERROR("abs() argument type not supported");
return NULL;
}
static PyObject *
GMPy_Number_Abs(PyObject *x, CTXT_Object *context)
{
if (IS_INTEGER(x))
return GMPy_Integer_Abs(x, context);
if (IS_RATIONAL_ONLY(x))
return GMPy_Rational_Abs(x, context);
if (IS_REAL_ONLY(x))
return GMPy_Real_Abs(x, context);
if (IS_COMPLEX_ONLY(x))
return GMPy_Complex_Abs(x, context);
TYPE_ERROR("abs() argument type not supported");
return NULL;
return GMPy_Number_AbsWithType(x, GMPy_ObjectType(x), context);
}
/* Implement context.abs(). The following code assumes it used a as method of

View file

@ -39,10 +39,10 @@ static PyObject * GMPy_Number_Abs(PyObject *x, CTXT_Object *context);
/* Private API */
static PyObject * GMPy_Integer_Abs(PyObject *x, CTXT_Object *context);
static PyObject * GMPy_Rational_Abs(PyObject *x, CTXT_Object *context);
static PyObject * GMPy_Real_Abs(PyObject *x, CTXT_Object *context);
static PyObject * GMPy_Complex_Abs(PyObject *x, CTXT_Object *context);
static PyObject * GMPy_Integer_AbsWithType(PyObject *x, int xtype, CTXT_Object *context);
static PyObject * GMPy_Rational_AbsWithType(PyObject *x, int xtype, CTXT_Object *context);
static PyObject * GMPy_Real_AbsWithType(PyObject *x, int xtype, CTXT_Object *context);
static PyObject * GMPy_Complex_AbsWithType(PyObject *x, int xtype, CTXT_Object *context);
static PyObject * GMPy_MPZ_Abs_Slot(MPZ_Object *x);
static PyObject * GMPy_MPQ_Abs_Slot(MPQ_Object *x);

View file

@ -35,7 +35,7 @@
*/
static PyObject *
GMPy_Integer_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context)
GMPy_Integer_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context)
{
MPZ_Object *result = NULL;
@ -45,13 +45,13 @@ GMPy_Integer_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
/* LCOV_EXCL_STOP */
}
if (xtype == OBJ_TYPE_MPZ) {
if (ytype == OBJ_TYPE_MPZ) {
if (IS_TYPE_MPZANY(xtype)) {
if (IS_TYPE_MPZANY(ytype)) {
mpz_add(result->z, MPZ(x), MPZ(y));
return (PyObject*)result;
}
if (ytype == OBJ_TYPE_PyInteger) {
if (IS_TYPE_PyInteger(ytype)) {
int error;
long temp = PyLong_AsLongAndOverflow(y, &error);
@ -70,10 +70,10 @@ GMPy_Integer_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
return (PyObject*)result;
}
if (ytype == OBJ_TYPE_HAS_MPZ) {
if (IS_TYPE_HAS_MPZ(ytype)) {
MPZ_Object *tempy = NULL;
if (!(tempy = GMPy_MPZ_From_Integer_X(y, ytype, context))) {
if (!(tempy = GMPy_MPZ_From_IntegerWithType(y, ytype, context))) {
/* Conversion of y to MPZ failed. */
Py_DECREF((PyObject*)result);
return NULL;
@ -85,8 +85,8 @@ GMPy_Integer_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
}
}
if (ytype == OBJ_TYPE_MPZ) {
if (xtype == OBJ_TYPE_PyInteger) {
if (IS_TYPE_MPZANY(ytype)) {
if (IS_TYPE_PyInteger(xtype)) {
int error;
long temp = PyLong_AsLongAndOverflow(x, &error);
@ -105,10 +105,10 @@ GMPy_Integer_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
return (PyObject*)result;
}
if (xtype == OBJ_TYPE_HAS_MPZ) {
if (IS_TYPE_HAS_MPZ(xtype)) {
MPZ_Object *tempx = NULL;
if (!(tempx = GMPy_MPZ_From_Integer_X(x, xtype, context))) {
if (!(tempx = GMPy_MPZ_From_IntegerWithType(x, xtype, context))) {
/* Conversion of x to MPZ failed. */
Py_DECREF((PyObject*)result);
return NULL;
@ -120,11 +120,11 @@ GMPy_Integer_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
}
}
if ((xtype <= OBJ_TYPE_HAS_MPZ) && (ytype <= OBJ_TYPE_HAS_MPZ)) {
if (IS_TYPE_INTEGER(xtype) && (IS_TYPE_INTEGER(ytype))) {
MPZ_Object *tempx = NULL, *tempy = NULL;
if (!(tempx = GMPy_MPZ_From_Integer_X(x, xtype, context)) ||
!(tempy = GMPy_MPZ_From_Integer_X(y, ytype, context))) {
if (!(tempx = GMPy_MPZ_From_IntegerWithType(x, xtype, context)) ||
!(tempy = GMPy_MPZ_From_IntegerWithType(y, ytype, context))) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)tempy);
@ -145,7 +145,7 @@ GMPy_Integer_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
}
static PyObject *
GMPy_Rational_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context)
GMPy_Rational_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context)
{
MPQ_Object *result = NULL;
@ -155,16 +155,16 @@ GMPy_Rational_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object
/* LCOV_EXCL_STOP */
}
if ((xtype == OBJ_TYPE_MPQ) && (ytype == OBJ_TYPE_MPQ)) {
if (IS_TYPE_MPQ(xtype) && IS_TYPE_MPQ(ytype)) {
mpq_add(result->q, MPQ(x), MPQ(y));
return (PyObject*)result;
}
if ((xtype <= OBJ_TYPE_HAS_MPQ) && (ytype <= OBJ_TYPE_HAS_MPQ)) {
if (IS_TYPE_RATIONAL(xtype) && IS_TYPE_RATIONAL(ytype)) {
MPQ_Object *tempx = NULL, *tempy = NULL;
if (!(tempx = GMPy_MPQ_From_Rational(x, context)) ||
!(tempy = GMPy_MPQ_From_Rational(y, context))) {
if (!(tempx = GMPy_MPQ_From_RationalWithType(x, xtype, context)) ||
!(tempy = GMPy_MPQ_From_RationalWithType(y, ytype, context))) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)tempy);
@ -191,10 +191,8 @@ GMPy_Rational_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object
* provided context is NULL, then the current context is used. If an error
* occurs, NULL is returned and an exception is set. If either x or y can't
* be converted to an mpfr, then Py_NotImplemented is returned.
* GMPy_Real_Add() will not try to promote the result to a different type
* GMPy_Real_Add() will not try to promote the result to a different type
* (i.e. mpc).
*
* GMPy_mpfr_add_fast(x, y) is the entry point for mpfr.__add__.
*/
/* Attempt to add two numbers and return an mpfr. The code path is optimized by
@ -202,7 +200,8 @@ GMPy_Rational_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object
* are not valid reals. */
static PyObject *
GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context)
GMPy_Real_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype,
CTXT_Object *context)
{
MPFR_Object *result = NULL;
@ -214,15 +213,15 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
/* LCOV_EXCL_STOP */
}
if (xtype == OBJ_TYPE_MPFR) {
if (ytype == OBJ_TYPE_MPFR) {
if (IS_TYPE_MPFR(xtype)) {
if (IS_TYPE_MPFR(ytype)) {
mpfr_clear_flags();
result->rc = mpfr_add(result->f, MPFR(x), MPFR(y), GET_MPFR_ROUND(context));
goto done;
}
if (ytype == OBJ_TYPE_PyInteger) {
if (IS_TYPE_PyInteger(ytype)) {
int error;
long temp = PyLong_AsLongAndOverflow(y, &error);
@ -241,17 +240,17 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
}
}
if (ytype == OBJ_TYPE_MPZ) {
if (IS_TYPE_MPZANY(ytype)) {
mpfr_clear_flags();
result->rc = mpfr_add_z(result->f, MPFR(x), MPZ(y), GET_MPFR_ROUND(context));
goto done;
}
if (ytype == OBJ_TYPE_HAS_MPZ) {
if (IS_TYPE_INTEGER(ytype)) {
MPZ_Object *tempy = NULL;
if (!(tempy = GMPy_MPZ_From_Integer_X(y, ytype, context))) {
if (!(tempy = GMPy_MPZ_From_IntegerWithType(y, ytype, context))) {
/* Conversion of y to MPZ failed. */
Py_DECREF((PyObject*)result);
return NULL;
@ -262,17 +261,17 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
goto done;
}
if (ytype == OBJ_TYPE_MPQ) {
if (IS_TYPE_MPQ(ytype)) {
mpfr_clear_flags();
result->rc = mpfr_add_q(result->f, MPFR(x), MPQ(y), GET_MPFR_ROUND(context));
goto done;
}
if (ytype == OBJ_TYPE_HAS_MPQ) {
if (IS_TYPE_RATIONAL(ytype)) {
MPQ_Object *tempy = NULL;
if (!(tempy = GMPy_MPQ_From_Rational(y, context))) {
if (!(tempy = GMPy_MPQ_From_RationalWithType(y, ytype, context))) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
@ -286,17 +285,17 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
goto done;
}
if (ytype == OBJ_TYPE_PyFloat) {
if (IS_TYPE_PyFloat(ytype)) {
mpfr_clear_flags();
result->rc = mpfr_add_d(result->f, MPFR(x), PyFloat_AS_DOUBLE(y), GET_MPFR_ROUND(context));
goto done;
}
if (ytype == OBJ_TYPE_HAS_MPFR) {
if (IS_TYPE_REAL(ytype)) {
MPFR_Object *tempy = NULL;
if (!(tempy = GMPy_MPFR_From_Real(y, 1, context))) {
if (!(tempy = GMPy_MPFR_From_RealWithType(y, ytype, 1, context))) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
@ -311,8 +310,8 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
}
}
if (ytype == OBJ_TYPE_MPFR) {
if (xtype == OBJ_TYPE_PyInteger) {
if (IS_TYPE_MPFR(ytype)) {
if (IS_TYPE_PyInteger(xtype)) {
int error;
long temp = PyLong_AsLongAndOverflow(x, &error);
@ -331,17 +330,17 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
}
}
if (xtype == OBJ_TYPE_MPZ) {
if (IS_TYPE_MPZANY(xtype)) {
mpfr_clear_flags();
result->rc = mpfr_add_z(result->f, MPFR(y), MPZ(x), GET_MPFR_ROUND(context));
goto done;
}
if (xtype == OBJ_TYPE_HAS_MPZ) {
if (IS_TYPE_INTEGER(xtype)) {
MPZ_Object *tempx = NULL;
if (!(tempx = GMPy_MPZ_From_Integer_X(x, xtype, context))) {
if (!(tempx = GMPy_MPZ_From_IntegerWithType(x, xtype, context))) {
/* Conversion of x to MPZ failed. */
Py_DECREF((PyObject*)result);
return NULL;
@ -352,17 +351,17 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
goto done;
}
if (xtype == OBJ_TYPE_MPQ) {
if (IS_TYPE_MPQ(xtype)) {
mpfr_clear_flags();
result->rc = mpfr_add_q(result->f, MPFR(y), MPQ(x), GET_MPFR_ROUND(context));
goto done;
}
if (xtype == OBJ_TYPE_HAS_MPQ) {
if (IS_TYPE_RATIONAL(xtype)) {
MPQ_Object *tempx = NULL;
if (!(tempx = GMPy_MPQ_From_Rational(x, context))) {
if (!(tempx = GMPy_MPQ_From_RationalWithType(x, xtype, context))) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
@ -376,16 +375,17 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
goto done;
}
if (xtype == OBJ_TYPE_PyFloat) {
if (IS_TYPE_PyFloat(xtype)) {
mpfr_clear_flags();
result->rc = mpfr_add_d(result->f, MPFR(y), PyFloat_AS_DOUBLE(x), GET_MPFR_ROUND(context));
goto done;
}
if (xtype == OBJ_TYPE_HAS_MPFR) {
if (IS_TYPE_REAL(xtype)) {
MPFR_Object *tempx = NULL;
if (!(tempx = GMPy_MPFR_From_Real_X(x, xtype, 1, context))) {
if (!(tempx = GMPy_MPFR_From_RealWithType(x, xtype, 1, context))) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
@ -400,11 +400,11 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
}
}
if (IS_REAL(x) && IS_REAL(y)) {
if (IS_TYPE_REAL(xtype) && IS_TYPE_REAL(ytype)) {
MPFR_Object *tempx = NULL, *tempy = NULL;
if (!(tempx = GMPy_MPFR_From_Real(x, 1, context)) ||
!(tempy = GMPy_MPFR_From_Real(y, 1, context))) {
if (!(tempx = GMPy_MPFR_From_RealWithType(x, xtype, 1, context)) ||
!(tempy = GMPy_MPFR_From_RealWithType(y, ytype, 1, context))) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)tempy);
@ -423,7 +423,7 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
SYSTEM_ERROR("Internal error in GMPy_Real_Add().");
TYPE_ERROR("add() argument type not supported");
return NULL;
/* LCOV_EXCL_STOP */
@ -439,7 +439,8 @@ GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *con
static PyObject *
GMPy_Complex_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context)
GMPy_Complex_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype,
CTXT_Object *context)
{
MPC_Object *result = NULL;
@ -451,7 +452,7 @@ GMPy_Complex_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
/* LCOV_EXCL_STOP */
}
if (MPC_Check(x) && MPC_Check(y)) {
if (IS_TYPE_MPC(xtype) && IS_TYPE_MPC(ytype)) {
result->rc = mpc_add(result->c, MPC(x), MPC(y), GET_MPC_ROUND(context));
@ -459,11 +460,11 @@ GMPy_Complex_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
return (PyObject*)result;
}
if (IS_COMPLEX(x) && IS_COMPLEX(y)) {
if (IS_TYPE_COMPLEX(xtype) && IS_TYPE_COMPLEX(ytype)) {
MPC_Object *tempx = NULL, *tempy = NULL;
if (!(tempx = GMPy_MPC_From_Complex(x, 1, 1, context)) ||
!(tempy = GMPy_MPC_From_Complex(y, 1, 1, context))) {
if (!(tempx = GMPy_MPC_From_ComplexWithType(x, xtype, 1, 1, context)) ||
!(tempy = GMPy_MPC_From_ComplexWithType(y, ytype, 1, 1, context))) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)tempy);
@ -482,41 +483,36 @@ GMPy_Complex_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
SYSTEM_ERROR("Internal error in GMPy_Complex_Add().");
TYPE_ERROR("add() argument type not supported");
return NULL;
/* LCOV_EXCL_STOP */
}
static PyObject *
GMPy_Number_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context)
GMPy_Number_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype,
CTXT_Object *context)
{
if ((!xtype) || (!ytype)) {
TYPE_ERROR("add() argument type not supported");
return NULL;
}
if (IS_TYPE_INTEGER(xtype) && IS_TYPE_INTEGER(ytype))
return GMPy_Integer_AddWithType(x, xtype, y, ytype, context);
if ((xtype < OBJ_TYPE_MPQ) && (ytype < OBJ_TYPE_MPQ))
return GMPy_Integer_Add_X(x, xtype, y, ytype, context);
if (IS_TYPE_RATIONAL(xtype) && IS_TYPE_RATIONAL(ytype))
return GMPy_Rational_AddWithType(x, xtype, y, ytype, context);
if ((xtype < OBJ_TYPE_MPFR) && (ytype < OBJ_TYPE_MPFR))
return GMPy_Rational_Add_X(x, xtype, y, ytype, context);
if (IS_TYPE_REAL(xtype) && IS_TYPE_REAL(ytype))
return GMPy_Real_AddWithType(x, xtype, y, ytype, context);
if (IS_TYPE_COMPLEX(xtype) && IS_TYPE_COMPLEX(ytype))
return GMPy_Complex_AddWithType(x, xtype, y, ytype, context);
if ((xtype < OBJ_TYPE_MPC) && (ytype < OBJ_TYPE_MPC))
return GMPy_Real_Add_X(x, xtype, y, ytype, context);
if ((xtype < OBJ_TYPE_MAX) && (ytype < OBJ_TYPE_MAX))
return GMPy_Complex_Add_X(x, xtype, y, ytype, context);
Py_RETURN_NOTIMPLEMENTED;
TYPE_ERROR("add() argument type not supported");
return NULL;
}
static PyObject *
GMPy_Number_Add(PyObject *x, PyObject *y, CTXT_Object *context)
{
int xtype = GMPy_ObjectType(x);
int ytype = GMPy_ObjectType(y);
return GMPy_Number_Add_X(x, xtype, y, ytype, context);
return GMPy_Number_AddWithType(x, GMPy_ObjectType(x),
y, GMPy_ObjectType(y), context);
}
/* Implement all the slot methods here. */
@ -524,14 +520,34 @@ GMPy_Number_Add(PyObject *x, PyObject *y, CTXT_Object *context)
static PyObject *
GMPy_Number_Add_Slot(PyObject *x, PyObject *y)
{
int xtype = GMPy_ObjectType(x);
int ytype = GMPy_ObjectType(y);
return GMPy_Number_AddWithType(x, GMPy_ObjectType(x),
y, GMPy_ObjectType(y), NULL);
}
if (!xtype || !ytype) {
Py_RETURN_NOTIMPLEMENTED;
/* Creating a custom Add_Slot function for the MPFR type is a small win. It
* doesn't help MPQ or MPC.
*/
static PyObject *
GMPy_MPFR_Add_Slot(PyObject *x, PyObject *y)
{
if (MPFR_Check(x) && MPFR_Check(y)) {
MPFR_Object *result = NULL;
CTXT_Object *context = NULL;
CHECK_CONTEXT(context);
if ((result = GMPy_MPFR_New(0, context))) {
mpfr_clear_flags();
result->rc = mpfr_add(result->f, MPFR(x), MPFR(y), GET_MPFR_ROUND(context));
_GMPy_MPFR_Cleanup(&result, context);
}
return (PyObject*)result;
}
return GMPy_Number_Add_X(x, xtype, y, ytype, NULL);
return GMPy_Number_AddWithType(x, GMPy_ObjectType(x),
y, GMPy_ObjectType(y), NULL);
}
/* Implement context.add() and gmpy2.add(). */

View file

@ -39,11 +39,11 @@ static PyObject * GMPy_Number_Add(PyObject *x, PyObject *y, CTXT_Object *context
/* Private API */
static PyObject * GMPy_Number_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Integer_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Rational_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Real_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Complex_Add_X(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Number_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Integer_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Rational_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Real_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Complex_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype, CTXT_Object *context);
static PyObject * GMPy_Number_Add_Slot(PyObject *x, PyObject *y);
static PyObject * GMPy_Context_Add(PyObject *self, PyObject *args);

View file

@ -73,112 +73,51 @@ static int GMPy_isComplex(PyObject *obj)
#endif
/* GMPy_ObjectType(PyObject *obj) returns an integer that identifies the
* object's type. The return values are:
*
* 0: unknown type
* 1: MPZ or XMPZ type
* 2: Python Integer (for Python 2.x) or Long type
* 3: Has __mpz__ method
* 4 - 15: Future use for other integer types
* 16: MPQ type
* 17: Python Fraction type
* 18: Has __mpq__ method
* 19 - 31: Future use for other integer types
* 32: MPFR type
* 33: PyFloat type
* 34: Has __mpfr__ method
* 35 - 47: Future use for other real floating point types
* 48: MPC type
* 49: PyComplex type
* 50: Has __mpc__ method
* 51 - 63: Future use for other complex float point types
*
* object's type. See gmpy2_convert.h for details.
*
* Exceptions are never raised.
*/
static int GMPy_ObjectType(PyObject *obj)
{
/* Tests are sorted by order by (best guess of) most common argument type.
* Complete tests are done last.
* Tests that require attribute lookups are done last.
*/
/* We only check for exact Python types first. */
if MPZ_Check(obj) return OBJ_TYPE_MPZ;
if MPZ_Check(obj) {
return OBJ_TYPE_MPZ;
}
if MPFR_Check(obj) return OBJ_TYPE_MPFR;
if PyIntOrLong_CheckExact(obj) {
return OBJ_TYPE_PyInteger;
}
if MPC_Check(obj) return OBJ_TYPE_MPC;
if MPQ_Check(obj) return OBJ_TYPE_MPQ;
if MPFR_Check(obj) {
return OBJ_TYPE_MPFR;
}
if XMPZ_Check(obj) return OBJ_TYPE_XMPZ;
if PyFloat_CheckExact(obj) {
return OBJ_TYPE_PyFloat;
}
if PyIntOrLong_Check(obj) return OBJ_TYPE_PyInteger;
if MPC_Check(obj) {
return OBJ_TYPE_MPC;
}
if PyFloat_Check(obj) return OBJ_TYPE_PyFloat;
if PyComplex_CheckExact(obj) {
return OBJ_TYPE_PyComplex;
}
if PyComplex_Check(obj) return OBJ_TYPE_PyComplex;
if MPQ_Check(obj) {
return OBJ_TYPE_MPQ;
}
if IS_FRACTION(obj) {
return OBJ_TYPE_PyFraction;
}
if XMPZ_Check(obj) {
return OBJ_TYPE_MPZ;
}
/* Now we do the non-Exact type check for native types. */
if PyIntOrLong_Check(obj) {
return OBJ_TYPE_PyInteger;
}
if PyFloat_Check(obj) {
return OBJ_TYPE_PyFloat;
}
if PyComplex_Check(obj) {
return OBJ_TYPE_PyComplex;
}
if IS_FRACTION(obj) return OBJ_TYPE_PyFraction;
/* Now we look for the presence of __mpz__, __mpq__, __mpfr__, and __mpc__.
* Since a type may define more than one of the special methods, we perform
* the checks in reverse order.
*/
if HAS_MPC_CONVERSION(obj) {
return OBJ_TYPE_HAS_MPC;
}
if HAS_MPC_CONVERSION(obj) return OBJ_TYPE_HAS_MPC;
if HAS_MPFR_CONVERSION(obj) {
return OBJ_TYPE_HAS_MPFR;
}
if HAS_MPFR_CONVERSION(obj) return OBJ_TYPE_HAS_MPFR;
if HAS_MPQ_CONVERSION(obj) {
return OBJ_TYPE_HAS_MPQ;
}
if HAS_MPQ_CONVERSION(obj) return OBJ_TYPE_HAS_MPQ;
if HAS_MPZ_CONVERSION(obj) {
return OBJ_TYPE_HAS_MPZ;
}
if HAS_MPZ_CONVERSION(obj) return OBJ_TYPE_HAS_MPZ;
return 0;
return OBJ_TYPE_UNKNOWN;
}
/* mpz_set_PyStr converts a Python "string" into a mpz_t structure. It accepts
* a sequence of bytes (i.e. str in Python 2, bytes in Python 3) or a Unicode
* string (i.e. unicode in Python 3, str in Python 3). Returns -1 on error,

View file

@ -48,54 +48,102 @@ extern "C" {
#define IS_FRACTION(x) (!strcmp(Py_TYPE(x)->tp_name, "Fraction"))
#define IS_RATIONAL_ONLY(x) (MPQ_Check(x) || IS_FRACTION(x) || HAS_MPQ_CONVERSION(x))
#define IS_RATIONAL_ONLY(x) (MPQ_Check(x) || IS_FRACTION(x) || \
HAS_MPQ_CONVERSION(x))
#ifdef PY2
#define IS_INTEGER(x) (MPZ_Check(x) || PyInt_Check(x) || \
PyLong_Check(x) || XMPZ_Check(x) || \
HAS_STRICT_MPZ_CONVERSION(x))
PyLong_Check(x) || XMPZ_Check(x) || \
HAS_STRICT_MPZ_CONVERSION(x))
#define IS_RATIONAL(x) (MPQ_Check(x) || IS_FRACTION(x) || \
MPZ_Check(x) || PyInt_Check(x) || PyLong_Check(x) || \
XMPZ_Check(x) || HAS_MPQ_CONVERSION(x) || \
HAS_MPZ_CONVERSION(x))
MPZ_Check(x) || PyInt_Check(x) || \
PyLong_Check(x) || XMPZ_Check(x) || \
HAS_MPQ_CONVERSION(x) || HAS_MPZ_CONVERSION(x))
#else
#define IS_INTEGER(x) (MPZ_Check(x) || PyLong_Check(x) || \
XMPZ_Check(x) || HAS_STRICT_MPZ_CONVERSION(x))
XMPZ_Check(x) || HAS_STRICT_MPZ_CONVERSION(x))
#define IS_RATIONAL(x) (MPQ_Check(x) || IS_FRACTION(x) || \
MPZ_Check(x) || PyLong_Check(x) || \
XMPZ_Check(x) || HAS_MPQ_CONVERSION(x) || \
HAS_MPZ_CONVERSION(x))
#endif
#define IS_REAL_ONLY(x) (MPFR_Check(x) || PyFloat_Check(x) || HAS_STRICT_MPFR_CONVERSION(x))
#define IS_REAL_ONLY(x) (MPFR_Check(x) || PyFloat_Check(x) || \
HAS_STRICT_MPFR_CONVERSION(x))
#define IS_REAL(x) (IS_RATIONAL(x) || IS_REAL_ONLY(x))
#define IS_COMPLEX_ONLY(x) (MPC_Check(x) || PyComplex_Check(x) || HAS_MPC_CONVERSION(x))
#define IS_COMPLEX_ONLY(x) (MPC_Check(x) || PyComplex_Check(x) || \
HAS_MPC_CONVERSION(x))
#define IS_COMPLEX(x) (IS_REAL(x) || IS_COMPLEX_ONLY(x))
/* Define constants used in gmpy2_convert.c->GMPy_ObjectType. */
#define OBJ_TYPE_MPZ 1
#define OBJ_TYPE_PyInteger 2
/* 3 TO 14 reserved for additional integer types. */
#define OBJ_TYPE_HAS_MPZ 15
#define OBJ_TYPE_UNKNOWN 0
#define OBJ_TYPE_MPZ 1
#define OBJ_TYPE_XMPZ 2
#define OBJ_TYPE_PyInteger 3
#define OBJ_TYPE_HAS_MPZ 4
/* 5 TO 14 reserved for additional integer types. */
#define OBJ_TYPE_INTEGER 15
#define OBJ_TYPE_MPQ 16
#define OBJ_TYPE_PyFraction 17
/* 18 to 30 reserved for additional rational types. */
#define OBJ_TYPE_HAS_MPQ 31
#define OBJ_TYPE_MPQ 16
#define OBJ_TYPE_PyFraction 17
#define OBJ_TYPE_HAS_MPQ 18
/* 19 to 30 reserved for additional rational types. */
#define OBJ_TYPE_RATIONAL 31
#define OBJ_TYPE_MPFR 32
#define OBJ_TYPE_PyFloat 33
/* 34 to 46 reserved for additional real types. */
#define OBJ_TYPE_HAS_MPFR 47
#define OBJ_TYPE_MPFR 32
#define OBJ_TYPE_PyFloat 33
#define OBJ_TYPE_HAS_MPFR 34
/* 35 to 46 reserved for additional real types. */
#define OBJ_TYPE_REAL 47
#define OBJ_TYPE_MPC 48
#define OBJ_TYPE_PyComplex 49
#define OBJ_TYPE_MPC 48
#define OBJ_TYPE_PyComplex 49
#define OBJ_TYPE_HAS_MPC 50
/* 50 to 62 reserved for additional complex types. */
#define OBJ_TYPE_HAS_MPC 63
#define OBJ_TYPE_COMPLEX 63
#define OBJ_TYPE_MAX 64
/* The following macros are the recommended method to check the result of the
* object type check.
*/
#define IS_TYPE_UNKNOWN(x) (!OBJ_TYPE_UNKNOWN)
#define IS_TYPE_MPZ(x) (x == OBJ_TYPE_MPZ)
#define IS_TYPE_XMPZ(x) (x == OBJ_TYPE_XMPZ)
#define IS_TYPE_MPZANY(x) ((x == OBJ_TYPE_MPZ) || \
(x == OBJ_TYPE_XMPZ))
#define IS_TYPE_PyInteger(x) (x == OBJ_TYPE_PyInteger)
#define IS_TYPE_HAS_MPZ(x) (x == OBJ_TYPE_HAS_MPZ)
#define IS_TYPE_INTEGER(x) ((x > OBJ_TYPE_UNKNOWN) && \
(x < OBJ_TYPE_INTEGER))
#define IS_TYPE_MPQ(x) (x == OBJ_TYPE_MPQ)
#define IS_TYPE_PyFraction(x) (x == OBJ_TYPE_PyFraction)
#define IS_TYPE_HAS_MPQ(x) (x == OBJ_TYPE_HAS_MPQ)
#define IS_TYPE_RATIONAL(x) ((x > OBJ_TYPE_UNKNOWN) && \
(x < OBJ_TYPE_RATIONAL))
#define IS_TYPE_RATIONAL_ONLY(x) ((x > OBJ_TYPE_INTEGER) && \
(x < OBJ_TYPE_RATIONAL))
#define IS_TYPE_MPFR(x) (x == OBJ_TYPE_MPFR)
#define IS_TYPE_PyFloat(x) (x == OBJ_TYPE_PyFloat)
#define IS_TYPE_HAS_MPFR(x) (x == OBJ_TYPE_HAS_MPFR)
#define IS_TYPE_REAL(x) ((x > OBJ_TYPE_UNKNOWN) && \
(x < OBJ_TYPE_REAL))
#define IS_TYPE_REAL_ONLY(x) ((x > OBJ_TYPE_RATIONAL) && \
(x < OBJ_TYPE_REAL))
#define IS_TYPE_MPC(x) (x == OBJ_TYPE_MPC)
#define IS_TYPE_PyComplex(x) (x == OBJ_TYPE_PyComplex)
#define IS_TYPE_HAS_MPC(x) (x == OBJ_TYPE_HAS_MPC)
#define IS_TYPE_COMPLEX(x) ((x > OBJ_TYPE_UNKNOWN) && \
(x < OBJ_TYPE_COMPLEX))
#define IS_TYPE_COMPLEX_ONLY(x) ((x > OBJ_TYPE_REAL) && \
(x < OBJ_TYPE_COMPLEX))
#define OBJ_TYPE_MAX 64
/* Since the macros are used in gmpy2's codebase, these functions are skipped
* until they are needed for the C API in the future.

View file

@ -49,8 +49,6 @@ GMPy_MPZ_From_PyIntOrLong(PyObject *obj, CTXT_Object *context)
Py_ssize_t len;
PyLongObject *templong = (PyLongObject*)obj;
assert(PyIntOrLong_Check(obj));
if(!(result = GMPy_MPZ_New(context))) {
/* LCOV_EXCL_START */
return NULL;
@ -164,8 +162,6 @@ GMPy_MPZ_From_PyFloat(PyObject *obj, CTXT_Object *context)
{
MPZ_Object *result;
assert(PyFloat_Check(obj));
if ((result = GMPy_MPZ_New(context))) {
double d = PyFloat_AsDouble(obj);
@ -191,10 +187,7 @@ GMPy_PyLong_From_MPZ(MPZ_Object *obj, CTXT_Object *context)
size_t count, size;
PyLongObject *result;
assert(CHECK_MPZANY(obj));
/* Assume gmp uses limbs as least as large as the builtin longs do */
assert(mp_bits_per_limb >= PyLong_SHIFT);
if (mpz_sgn(obj->z) < 0) {
negative = 1;
@ -255,7 +248,6 @@ GMPy_MPZ_Long_Slot(MPZ_Object *self)
static PyObject *
GMPy_PyIntOrLong_From_MPZ(MPZ_Object *obj, CTXT_Object *context)
{
assert(CHECK_MPZANY(obj));
#ifdef PY2
if (mpz_fits_slong_p(obj->z)) {
@ -278,8 +270,6 @@ GMPy_PyFloat_From_MPZ(MPZ_Object *obj, CTXT_Object *context)
{
double res;
assert(CHECK_MPZANY(obj));
res = mpz_get_d(obj->z);
if (Py_IS_INFINITY(res)) {
@ -299,8 +289,6 @@ GMPy_MPZ_Float_Slot(MPZ_Object *self)
static PyObject *
GMPy_PyStr_From_MPZ(MPZ_Object *obj, int base, int option, CTXT_Object *context)
{
assert(CHECK_MPZANY(obj));
return mpz_ascii(obj->z, base, option, 0);
}
@ -338,22 +326,22 @@ GMPy_MPZ_From_Integer(PyObject *obj, CTXT_Object *context)
}
static MPZ_Object *
GMPy_MPZ_From_Integer_X(PyObject *obj, int xtype, CTXT_Object *context)
GMPy_MPZ_From_IntegerWithType(PyObject *obj, int xtype, CTXT_Object *context)
{
MPZ_Object *result = NULL;
if (MPZ_Check(obj)) {
if (IS_TYPE_MPZ(xtype)) {
Py_INCREF(obj);
return (MPZ_Object*)obj;
}
if (xtype == OBJ_TYPE_PyInteger)
if (IS_TYPE_PyInteger(xtype))
return GMPy_MPZ_From_PyIntOrLong(obj, context);
if (XMPZ_Check(obj))
if (IS_TYPE_XMPZ(xtype))
return GMPy_MPZ_From_XMPZ((XMPZ_Object*)obj, context);
if (xtype == OBJ_TYPE_HAS_MPZ) {
if (IS_TYPE_HAS_MPZ(xtype)) {
result = (MPZ_Object *) PyObject_CallMethod(obj, "__mpz__", NULL);
if (result != NULL && MPZ_Check(result)) {
@ -370,8 +358,6 @@ GMPy_MPZ_From_Integer_X(PyObject *obj, int xtype, CTXT_Object *context)
return NULL;
}
static MPZ_Object *
GMPy_MPZ_From_IntegerAndCopy(PyObject *obj, CTXT_Object *context)
{
@ -417,7 +403,7 @@ GMPy_MPZ_Repr_Slot(MPZ_Object *self)
static int
GMPy_MPZ_ConvertArg(PyObject *arg, PyObject **ptr)
{
MPZ_Object *result = GMPy_MPZ_From_Integer(arg, NULL);
MPZ_Object *result = GMPy_MPZ_From_IntegerWithType(arg, GMPy_ObjectType(arg), NULL);
if (result) {
*ptr = (PyObject*)result;
@ -442,8 +428,6 @@ GMPy_XMPZ_From_PyIntOrLong(PyObject *obj, CTXT_Object *context)
Py_ssize_t len;
PyLongObject *templong = (PyLongObject*)obj;
assert(PyIntOrLong_Check(obj));
if(!(result = GMPy_XMPZ_New(context))) {
/* LCOV_EXCL_START */
return NULL;
@ -508,8 +492,6 @@ GMPy_XMPZ_From_PyFloat(PyObject *obj, CTXT_Object *context)
{
XMPZ_Object *result;
assert(PyFloat_Check(obj));
if ((result = GMPy_XMPZ_New(context))) {
double d = PyFloat_AsDouble(obj);
@ -533,8 +515,6 @@ GMPy_XMPZ_From_MPZ(MPZ_Object *obj, CTXT_Object *context)
{
XMPZ_Object *result;
assert(CHECK_MPZANY(obj));
if ((result = GMPy_XMPZ_New(context)))
mpz_set(result->z, obj->z);
@ -546,8 +526,6 @@ GMPy_XMPZ_From_XMPZ(XMPZ_Object *obj, CTXT_Object *context)
{
XMPZ_Object *result;
assert(XMPZ_Check(obj));
if ((result = GMPy_XMPZ_New(context)))
mpz_set(result->z, obj->z);
@ -557,8 +535,6 @@ GMPy_XMPZ_From_XMPZ(XMPZ_Object *obj, CTXT_Object *context)
static PyObject *
GMPy_PyStr_From_XMPZ(XMPZ_Object *obj, int base, int option, CTXT_Object *context)
{
assert(XMPZ_Check(obj));
return mpz_ascii(obj->z, base, option, 1);
}
@ -567,8 +543,6 @@ GMPy_MPZ_From_XMPZ(XMPZ_Object *obj, CTXT_Object *context)
{
MPZ_Object *result;
assert(XMPZ_Check(obj));
if ((result = GMPy_MPZ_New(context)))
mpz_set(result->z, obj->z);
@ -600,8 +574,6 @@ GMPy_MPQ_From_PyIntOrLong(PyObject *obj, CTXT_Object *context)
MPQ_Object *result;
MPZ_Object *temp;
assert(PyIntOrLong_Check(obj));
temp = GMPy_MPZ_From_PyIntOrLong(obj, context);
if (!temp)
@ -797,8 +769,6 @@ GMPy_MPQ_From_PyFloat(PyObject *obj, CTXT_Object *context)
{
MPQ_Object *result;
assert(PyFloat_Check(obj));
if ((result = GMPy_MPQ_New(context))) {
double d = PyFloat_AsDouble(obj);
@ -823,8 +793,6 @@ GMPy_MPQ_From_MPZ(MPZ_Object *obj, CTXT_Object *context)
{
MPQ_Object *result;
assert(CHECK_MPZANY(obj));
if ((result = GMPy_MPQ_New(context)))
mpq_set_z(result->q, obj->z);
@ -836,8 +804,6 @@ GMPy_MPQ_From_XMPZ(XMPZ_Object *obj, CTXT_Object *context)
{
MPQ_Object *result;
assert(XMPZ_Check(obj));
if ((result = GMPy_MPQ_New(context)))
mpq_set_z(result->q, obj->z);
@ -849,8 +815,6 @@ GMPy_MPZ_From_MPQ(MPQ_Object *obj, CTXT_Object *context)
{
MPZ_Object *result;
assert(MPQ_Check(obj));
if ((result = GMPy_MPZ_New(context)))
mpz_set_q(result->z, obj->q);
@ -862,10 +826,7 @@ GMPy_XMPZ_From_MPQ(MPQ_Object *obj, CTXT_Object *context)
{
XMPZ_Object *result;
assert(MPQ_Check(obj));
if ((result = GMPy_XMPZ_New(context)))
mpz_set_q(result->z, obj->q);
return result;
@ -877,8 +838,6 @@ GMPy_PyLong_From_MPQ(MPQ_Object *obj, CTXT_Object *context)
PyObject *result;
MPZ_Object *temp;
assert(MPQ_Check(obj));
temp = GMPy_MPZ_From_MPQ(obj, context);
if (!temp) {
@ -906,8 +865,6 @@ GMPy_PyIntOrLong_From_MPQ(MPQ_Object *obj, CTXT_Object *context)
PyObject *result;
MPZ_Object *temp;
assert(MPQ_Check(obj));
temp = GMPy_MPZ_From_MPQ(obj, context);
if (!temp) {
@ -1002,8 +959,6 @@ GMPy_PyFloat_From_MPQ(MPQ_Object *obj, CTXT_Object *context)
{
double res;
assert(MPQ_Check(obj));
res = mpq_get_d(obj->q);
if (Py_IS_INFINITY(res)) {
@ -1107,32 +1062,32 @@ GMPy_MPQ_From_Number(PyObject *obj, CTXT_Object *context)
}
static MPQ_Object*
GMPy_MPQ_From_Number_X(PyObject *obj, int xtype, CTXT_Object *context)
GMPy_MPQ_From_NumberWithType(PyObject *obj, int xtype, CTXT_Object *context)
{
if (MPQ_Check(obj)) {
if (IS_TYPE_MPQ(xtype)) {
Py_INCREF(obj);
return (MPQ_Object*)obj;
}
if (MPZ_Check(obj))
if (IS_TYPE_MPZ(xtype))
return GMPy_MPQ_From_MPZ((MPZ_Object*)obj, context);
if (MPFR_Check(obj))
if (IS_TYPE_MPFR(xtype))
return GMPy_MPQ_From_MPFR((MPFR_Object*)obj, context);
if (PyFloat_Check(obj))
if (IS_TYPE_PyFloat(xtype))
return GMPy_MPQ_From_PyFloat(obj, context);
if (PyIntOrLong_Check(obj))
if (IS_TYPE_PyInteger(xtype))
return GMPy_MPQ_From_PyIntOrLong(obj, context);
if (XMPZ_Check(obj))
if (IS_TYPE_XMPZ(xtype))
return GMPy_MPQ_From_XMPZ((XMPZ_Object*)obj, context);
if (IS_FRACTION(obj))
if (IS_TYPE_PyFraction(xtype))
return GMPy_MPQ_From_Fraction(obj, context);
if (HAS_MPQ_CONVERSION(obj)) {
if (IS_TYPE_HAS_MPQ(xtype)) {
MPQ_Object * res = (MPQ_Object *) PyObject_CallMethod(obj, "__mpq__", NULL);
if (res != NULL && MPQ_Check(res)) {
@ -1144,7 +1099,7 @@ GMPy_MPQ_From_Number_X(PyObject *obj, int xtype, CTXT_Object *context)
}
}
if (HAS_MPZ_CONVERSION(obj)) {
if (IS_TYPE_HAS_MPZ(xtype)) {
MPZ_Object * res = (MPZ_Object *) PyObject_CallMethod(obj, "__mpz__", NULL);
if (res != NULL && MPZ_Check(res)) {
@ -1239,26 +1194,26 @@ GMPy_MPQ_From_Rational(PyObject *obj, CTXT_Object *context)
}
static MPQ_Object*
GMPy_MPQ_From_Rational_X(PyObject *obj, int xtype, CTXT_Object *context)
GMPy_MPQ_From_RationalWithType(PyObject *obj, int xtype, CTXT_Object *context)
{
if (MPQ_Check(obj)) {
if (IS_TYPE_MPQ(xtype)) {
Py_INCREF(obj);
return (MPQ_Object*)obj;
}
if (MPZ_Check(obj))
if (IS_TYPE_MPZ(xtype))
return GMPy_MPQ_From_MPZ((MPZ_Object*)obj, context);
if (PyIntOrLong_Check(obj))
if (IS_TYPE_PyInteger(xtype))
return GMPy_MPQ_From_PyIntOrLong(obj, context);
if (XMPZ_Check(obj))
if (IS_TYPE_XMPZ(xtype))
return GMPy_MPQ_From_XMPZ((XMPZ_Object*)obj, context);
if (IS_FRACTION(obj))
if (IS_TYPE_PyFraction(xtype))
return GMPy_MPQ_From_Fraction(obj, context);
if (HAS_MPQ_CONVERSION(obj)) {
if (IS_TYPE_HAS_MPQ(xtype)) {
MPQ_Object * res = (MPQ_Object *) PyObject_CallMethod(obj, "__mpq__", NULL);
if (res != NULL && MPQ_Check(res)) {
@ -1270,7 +1225,7 @@ GMPy_MPQ_From_Rational_X(PyObject *obj, int xtype, CTXT_Object *context)
}
}
if (HAS_MPZ_CONVERSION(obj)) {
if (IS_TYPE_HAS_MPZ(xtype)) {
MPZ_Object * res = (MPZ_Object *) PyObject_CallMethod(obj, "__mpz__", NULL);
if (res != NULL && MPZ_Check(res)) {
@ -1299,7 +1254,7 @@ GMPy_MPQ_From_Rational_X(PyObject *obj, int xtype, CTXT_Object *context)
int
GMPy_MPQ_ConvertArg(PyObject *arg, PyObject **ptr)
{
MPQ_Object* result = GMPy_MPQ_From_Number(arg, NULL);
MPQ_Object* result = GMPy_MPQ_From_NumberWithType(arg, GMPy_ObjectType(arg), NULL);
if (result) {
*ptr = (PyObject*)result;

View file

@ -90,10 +90,10 @@ static MPQ_Object * GMPy_MPQ_From_MPZ(MPZ_Object *obj, CTXT_Object *context);
static MPQ_Object * GMPy_MPQ_From_XMPZ(XMPZ_Object *obj, CTXT_Object *context);
static MPQ_Object * GMPy_MPQ_From_Rational(PyObject* obj, CTXT_Object *context);
static MPQ_Object * GMPy_MPQ_From_Number_X(PyObject* obj, int xtype, CTXT_Object *context);
static MPQ_Object * GMPy_MPQ_From_NumberWithType(PyObject* obj, int xtype, CTXT_Object *context);
static MPQ_Object * GMPy_MPQ_From_Number(PyObject* obj, CTXT_Object *context);
static MPQ_Object * GMPy_MPQ_From_RationalAndCopy(PyObject* obj, CTXT_Object *context);
static MPQ_Object * GMPy_MPQ_From_Rational_X(PyObject *obj, int xtype, CTXT_Object *context);
static MPQ_Object * GMPy_MPQ_From_RationalWithType(PyObject *obj, int xtype, CTXT_Object *context);
static PyObject * GMPy_PyIntOrLong_From_MPQ(MPQ_Object *obj, CTXT_Object *context);
static PyObject * GMPy_PyStr_From_MPQ(MPQ_Object *obj, int base, int option, CTXT_Object *context);
static PyObject * GMPy_PyFloat_From_MPQ(MPQ_Object *obj, CTXT_Object *context);

View file

@ -37,12 +37,11 @@ GMPy_MPC_From_MPC(MPC_Object *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
{
MPC_Object *result = NULL;
assert(MPC_Check(obj));
/* Optimize the critical case when prec==1 or obj is NaN or Inf. */
if ((rprec == 1 && iprec == 1) ||
(!mpfr_number_p(mpc_realref(obj->c)) && !mpfr_number_p(mpc_imagref(obj->c)))) {
(!mpfr_number_p(mpc_realref(obj->c)) &&
!mpfr_number_p(mpc_imagref(obj->c)))) {
Py_INCREF((PyObject*)obj);
return obj;
}
@ -91,8 +90,6 @@ GMPy_MPC_From_PyComplex(PyObject *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
{
MPC_Object *result;
assert(PyComplex_Check(obj));
CHECK_CONTEXT(context);
if (rprec == 0)
@ -123,8 +120,6 @@ GMPy_MPC_From_MPFR(MPFR_Object *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
{
MPC_Object *result;
assert(MPFR_Check(obj));
CHECK_CONTEXT(context);
if (rprec == 0)
@ -156,8 +151,6 @@ GMPy_MPC_From_PyFloat(PyObject *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
CHECK_CONTEXT(context);
assert(PyFloat_Check(obj));
if (rprec == 0)
rprec = GET_REAL_PREC(context);
else if (rprec == 1)
@ -186,8 +179,6 @@ GMPy_MPC_From_MPZ(MPZ_Object *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
{
MPC_Object *result = NULL;
assert(CHECK_MPZANY(obj));
CHECK_CONTEXT(context);
if (rprec < 2) {
@ -215,8 +206,6 @@ GMPy_MPC_From_MPQ(MPQ_Object *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
{
MPC_Object *result = NULL;
assert(MPQ_Check(obj));
CHECK_CONTEXT(context);
if (rprec < 2) {
@ -245,8 +234,6 @@ GMPy_MPC_From_Fraction(PyObject *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
MPC_Object *result = NULL;
MPQ_Object *tempq;
assert(IS_RATIONAL(obj));
CHECK_CONTEXT(context);
if ((tempq = GMPy_MPQ_From_Fraction(obj, context))) {
@ -263,8 +250,6 @@ GMPy_MPC_From_PyIntOrLong(PyObject *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
MPC_Object *result = NULL;
MPZ_Object *tempz;
assert(PyIntOrLong_Check(obj));
CHECK_CONTEXT(context);
if ((tempz = GMPy_MPZ_From_PyIntOrLong(obj, context))) {
@ -408,36 +393,36 @@ GMPy_MPC_From_PyStr(PyObject *s, int base, mpfr_prec_t rprec, mpfr_prec_t iprec,
/* See the comments for GMPy_MPFR_From_Real_Temp. */
static MPC_Object *
GMPy_MPC_From_Complex(PyObject* obj, mp_prec_t rprec, mp_prec_t iprec,
CTXT_Object *context)
GMPy_MPC_From_ComplexWithType(PyObject* obj, int xtype, mp_prec_t rprec,
mp_prec_t iprec, CTXT_Object *context)
{
CHECK_CONTEXT(context);
if (MPC_Check(obj))
if (IS_TYPE_MPC(xtype))
return GMPy_MPC_From_MPC((MPC_Object*)obj, rprec, iprec, context);
if (MPFR_Check(obj))
if (IS_TYPE_MPFR(xtype))
return GMPy_MPC_From_MPFR((MPFR_Object*)obj, rprec, iprec, context);
if (PyFloat_Check(obj))
if (IS_TYPE_PyFloat(xtype))
return GMPy_MPC_From_PyFloat(obj, rprec, iprec, context);
if (PyComplex_Check(obj))
if (IS_TYPE_PyComplex(xtype))
return GMPy_MPC_From_PyComplex(obj, rprec, iprec, context);
if (MPQ_Check(obj))
if (IS_TYPE_MPQ(xtype))
return GMPy_MPC_From_MPQ((MPQ_Object*)obj, rprec, iprec, context);
if (MPZ_Check(obj) || XMPZ_Check(obj))
if (IS_TYPE_MPZANY(xtype))
return GMPy_MPC_From_MPZ((MPZ_Object*)obj, rprec, iprec, context);
if (PyIntOrLong_Check(obj))
if (IS_TYPE_PyInteger(xtype))
return GMPy_MPC_From_PyIntOrLong(obj, rprec, iprec, context);
if (IS_FRACTION(obj))
if (IS_TYPE_PyFraction(xtype))
return GMPy_MPC_From_Fraction(obj, rprec, iprec, context);
if (HAS_MPC_CONVERSION(obj)) {
if (IS_TYPE_HAS_MPC(xtype)) {
MPC_Object * res = (MPC_Object *) PyObject_CallMethod(obj, "__mpc__", NULL);
if (res != NULL && MPC_Check(res)) {
@ -449,7 +434,7 @@ GMPy_MPC_From_Complex(PyObject* obj, mp_prec_t rprec, mp_prec_t iprec,
}
}
if (HAS_MPFR_CONVERSION(obj)) {
if (IS_TYPE_HAS_MPFR(xtype)) {
MPFR_Object * res = (MPFR_Object *) PyObject_CallMethod(obj, "__mpfr__", NULL);
if (res != NULL && MPFR_Check(res)) {
@ -463,7 +448,7 @@ GMPy_MPC_From_Complex(PyObject* obj, mp_prec_t rprec, mp_prec_t iprec,
}
}
if (HAS_MPQ_CONVERSION(obj)) {
if (IS_TYPE_HAS_MPQ(xtype)) {
MPQ_Object * res = (MPQ_Object *) PyObject_CallMethod(obj, "__mpq__", NULL);
if (res != NULL && MPQ_Check(res)) {
@ -477,7 +462,7 @@ GMPy_MPC_From_Complex(PyObject* obj, mp_prec_t rprec, mp_prec_t iprec,
}
}
if (HAS_MPZ_CONVERSION(obj)) {
if (IS_TYPE_HAS_MPZ(xtype)) {
MPZ_Object * res = (MPZ_Object *) PyObject_CallMethod(obj, "__mpz__", NULL);
if (res != NULL && MPZ_Check(res)) {
@ -496,10 +481,18 @@ GMPy_MPC_From_Complex(PyObject* obj, mp_prec_t rprec, mp_prec_t iprec,
return NULL;
}
static MPC_Object *
GMPy_MPC_From_Complex(PyObject* obj, mp_prec_t rprec, mp_prec_t iprec,
CTXT_Object *context)
{
return GMPy_MPC_From_ComplexWithType(obj, GMPy_ObjectType(obj),
rprec, iprec, context);
}
#if 0
static MPC_Object *
GMPy_MPC_From_ComplexAndCopy(PyObject* obj, mp_prec_t rprec, mp_prec_t iprec,
CTXT_Object *context)
CTXT_Object *context)
{
MPC_Object *result = NULL, *temp = NULL;

View file

@ -82,8 +82,6 @@ GMPy_MPFR_From_MPFR(MPFR_Object *obj, mpfr_prec_t prec, CTXT_Object *context)
{
MPFR_Object *result = NULL;
assert(MPFR_Check(obj));
/* Optimize the critical case when prec==1 or obj is NaN or Inf. */
if (prec == 1 || !mpfr_number_p(obj->f)) {
@ -128,8 +126,6 @@ GMPy_MPFR_From_PyIntOrLong(PyObject *obj, mpfr_prec_t prec, CTXT_Object *context
int error, was_one = 0;
long temp;
assert(PyIntOrLong_Check(obj));
CHECK_CONTEXT(context);
if (prec == 0)
@ -180,8 +176,6 @@ GMPy_MPFR_From_PyFloat(PyObject *obj, mpfr_prec_t prec, CTXT_Object *context)
{
MPFR_Object *result;
assert(PyFloat_Check(obj));
CHECK_CONTEXT(context);
if (prec == 0)
@ -216,8 +210,6 @@ GMPy_MPFR_From_MPZ(MPZ_Object *obj, mpfr_prec_t prec, CTXT_Object *context)
int was_one = 0;
size_t bitlen;
assert(CHECK_MPZANY(obj));
CHECK_CONTEXT(context);
if (prec == 0)
@ -258,8 +250,6 @@ GMPy_MPFR_From_MPQ(MPQ_Object *obj, mpfr_prec_t prec, CTXT_Object *context)
{
MPFR_Object *result;
assert(MPQ_Check(obj));
CHECK_CONTEXT(context);
if (prec < 2)
@ -287,8 +277,6 @@ GMPy_MPFR_From_Fraction(PyObject *obj, mpfr_prec_t prec, CTXT_Object *context)
MPFR_Object *result = NULL;
MPQ_Object *tempq;
assert(IS_RATIONAL(obj));
CHECK_CONTEXT(context);
if ((tempq = GMPy_MPQ_From_Fraction(obj, context))) {
@ -414,150 +402,87 @@ GMPy_MPFR_From_PyStr(PyObject *s, int base, mpfr_prec_t prec, CTXT_Object *conte
* The return value is guaranteed to have a valid exponent.
*/
static MPFR_Object *
GMPy_MPFR_From_RealWithType(PyObject *obj, int xtype, mp_prec_t prec, CTXT_Object *context)
{
CHECK_CONTEXT(context);
if (IS_TYPE_MPFR(xtype))
return GMPy_MPFR_From_MPFR((MPFR_Object*)obj, prec, context);
if (IS_TYPE_PyFloat(xtype))
return GMPy_MPFR_From_PyFloat(obj, prec, context);
if (IS_TYPE_MPQ(xtype))
return GMPy_MPFR_From_MPQ((MPQ_Object*)obj, prec, context);
if (IS_TYPE_MPZANY(xtype))
return GMPy_MPFR_From_MPZ((MPZ_Object*)obj, prec, context);
if (IS_TYPE_PyInteger(xtype))
return GMPy_MPFR_From_PyIntOrLong(obj, prec, context);
if (IS_TYPE_PyFraction(xtype))
return GMPy_MPFR_From_Fraction(obj, prec, context);
if (IS_TYPE_HAS_MPFR(xtype)) {
MPFR_Object *res = (MPFR_Object *) PyObject_CallMethod(obj, "__mpfr__", NULL);
if (res != NULL && MPFR_Check(res)) {
return res;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
if (IS_TYPE_HAS_MPQ(xtype)) {
MPQ_Object *res = (MPQ_Object *) PyObject_CallMethod(obj, "__mpq__", NULL);
if (res != NULL && MPQ_Check(res)) {
MPFR_Object * temp = GMPy_MPFR_From_MPQ(res, prec, context);
Py_DECREF(res);
return temp;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
if (IS_TYPE_HAS_MPZ(xtype)) {
MPZ_Object *res = (MPZ_Object *) PyObject_CallMethod(obj, "__mpz__", NULL);
if (res != NULL && MPZ_Check(res)) {
MPFR_Object * temp = GMPy_MPFR_From_MPZ(res, prec, context);
Py_DECREF(res);
return temp;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
error:
TYPE_ERROR("object could not be converted to 'mpfr'");
return NULL;
}
static MPFR_Object *
GMPy_MPFR_From_Real(PyObject *obj, mp_prec_t prec, CTXT_Object *context)
{
CHECK_CONTEXT(context);
if (MPFR_Check(obj))
return GMPy_MPFR_From_MPFR((MPFR_Object*)obj, prec, context);
if (PyFloat_Check(obj))
return GMPy_MPFR_From_PyFloat(obj, prec, context);
if (MPQ_Check(obj))
return GMPy_MPFR_From_MPQ((MPQ_Object*)obj, prec, context);
if (MPZ_Check(obj) || XMPZ_Check(obj))
return GMPy_MPFR_From_MPZ((MPZ_Object*)obj, prec, context);
if (PyIntOrLong_Check(obj))
return GMPy_MPFR_From_PyIntOrLong(obj, prec, context);
if (IS_FRACTION(obj))
return GMPy_MPFR_From_Fraction(obj, prec, context);
if (HAS_MPFR_CONVERSION(obj)) {
MPFR_Object *res = (MPFR_Object *) PyObject_CallMethod(obj, "__mpfr__", NULL);
if (res != NULL && MPFR_Check(res)) {
return res;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
if (HAS_MPQ_CONVERSION(obj)) {
MPQ_Object *res = (MPQ_Object *) PyObject_CallMethod(obj, "__mpq__", NULL);
if (res != NULL && MPQ_Check(res)) {
MPFR_Object * temp = GMPy_MPFR_From_MPQ(res, prec, context);
Py_DECREF(res);
return temp;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
if (HAS_MPZ_CONVERSION(obj)) {
MPZ_Object *res = (MPZ_Object *) PyObject_CallMethod(obj, "__mpz__", NULL);
if (res != NULL && MPZ_Check(res)) {
MPFR_Object * temp = GMPy_MPFR_From_MPZ(res, prec, context);
Py_DECREF(res);
return temp;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
error:
TYPE_ERROR("object could not be converted to 'mpfr'");
return NULL;
return GMPy_MPFR_From_RealWithType(obj, GMPy_ObjectType(obj),
prec, context);
}
static MPFR_Object *
GMPy_MPFR_From_Real_X(PyObject *obj, int xtype, mp_prec_t prec, CTXT_Object *context)
{
CHECK_CONTEXT(context);
if (MPFR_Check(obj))
return GMPy_MPFR_From_MPFR((MPFR_Object*)obj, prec, context);
if (PyFloat_Check(obj))
return GMPy_MPFR_From_PyFloat(obj, prec, context);
if (MPQ_Check(obj))
return GMPy_MPFR_From_MPQ((MPQ_Object*)obj, prec, context);
if (MPZ_Check(obj) || XMPZ_Check(obj))
return GMPy_MPFR_From_MPZ((MPZ_Object*)obj, prec, context);
if (PyIntOrLong_Check(obj))
return GMPy_MPFR_From_PyIntOrLong(obj, prec, context);
if (IS_FRACTION(obj))
return GMPy_MPFR_From_Fraction(obj, prec, context);
if (HAS_MPFR_CONVERSION(obj)) {
MPFR_Object *res = (MPFR_Object *) PyObject_CallMethod(obj, "__mpfr__", NULL);
if (res != NULL && MPFR_Check(res)) {
return res;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
if (HAS_MPQ_CONVERSION(obj)) {
MPQ_Object *res = (MPQ_Object *) PyObject_CallMethod(obj, "__mpq__", NULL);
if (res != NULL && MPQ_Check(res)) {
MPFR_Object * temp = GMPy_MPFR_From_MPQ(res, prec, context);
Py_DECREF(res);
return temp;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
if (HAS_MPZ_CONVERSION(obj)) {
MPZ_Object *res = (MPZ_Object *) PyObject_CallMethod(obj, "__mpz__", NULL);
if (res != NULL && MPZ_Check(res)) {
MPFR_Object * temp = GMPy_MPFR_From_MPZ(res, prec, context);
Py_DECREF(res);
return temp;
}
else {
Py_XDECREF((PyObject*)res);
goto error;
}
}
error:
TYPE_ERROR("object could not be converted to 'mpfr'");
return NULL;
}
static MPFR_Object *
GMPy_MPFR_From_RealAndCopy(PyObject *obj, mp_prec_t prec, CTXT_Object *context)
GMPy_MPFR_From_RealAndCopyWithType(PyObject *obj, int xtype, mp_prec_t prec, CTXT_Object *context)
{
MPFR_Object *result = NULL, *temp = NULL;
result = GMPy_MPFR_From_Real(obj, prec, context);
result = GMPy_MPFR_From_RealWithType(obj, xtype, prec, context);
if (result == NULL)
return result;
@ -580,13 +505,18 @@ GMPy_MPFR_From_RealAndCopy(PyObject *obj, mp_prec_t prec, CTXT_Object *context)
return temp;
}
static MPFR_Object *
GMPy_MPFR_From_RealAndCopy(PyObject *obj, mp_prec_t prec, CTXT_Object *context)
{
return GMPy_MPFR_From_RealAndCopyWithType(obj, GMPy_ObjectType(obj),
prec, context);
}
static MPZ_Object *
GMPy_MPZ_From_MPFR(MPFR_Object *obj, CTXT_Object *context)
{
MPZ_Object *result;
assert(MPFR_Check(obj));
CHECK_CONTEXT(context);
if ((result = GMPy_MPZ_New(context))) {
@ -927,7 +857,6 @@ GMPy_PyStr_From_MPFR(MPFR_Object *self, int base, int digits, CTXT_Object *conte
CHECK_CONTEXT(context);
/* check arguments are valid */
assert(MPFR_Check((PyObject*)self));
if (!((base >= 2) && (base <= 62))) {
VALUE_ERROR("base must be in the interval [2,62]");
return NULL;
@ -977,7 +906,8 @@ GMPy_PyStr_From_MPFR(MPFR_Object *self, int base, int digits, CTXT_Object *conte
int
GMPy_MPFR_ConvertArg(PyObject *arg, PyObject **ptr)
{
MPFR_Object* newob = GMPy_MPFR_From_Real(arg, 1, NULL);
MPFR_Object* newob = GMPy_MPFR_From_RealWithType(arg, GMPy_ObjectType(arg),
1, NULL);
if (newob) {
*ptr = (PyObject*)newob;

View file

@ -230,7 +230,7 @@ static PyNumberMethods mpc_number_methods =
(binaryfunc) GMPy_MPC_TrueDiv_Slot, /* nb_divide */
(binaryfunc) GMPy_MPC_Mod_Slot, /* nb_remainder */
(binaryfunc) GMPy_MPC_DivMod_Slot, /* nb_divmod */
(ternaryfunc) GMPy_MPANY_Pow_Slot, /* nb_power */
(ternaryfunc) GMPy_MPC_Pow_Slot, /* nb_power */
(unaryfunc) GMPy_MPC_Minus_Slot, /* nb_negative */
(unaryfunc) GMPy_MPC_Plus_Slot, /* nb_positive */
(unaryfunc) GMPy_MPC_Abs_Slot, /* nb_absolute */

View file

@ -228,7 +228,7 @@ GMPy_Complex_Polar(PyObject *x, CTXT_Object *context)
return NULL;
}
abs = GMPy_Complex_Abs(tempx, context);
abs = GMPy_Complex_AbsWithType(tempx, OBJ_TYPE_MPC, context);
phase = GMPy_Complex_Phase(tempx, context);
Py_DECREF(tempx);
result = PyTuple_New(2);

View file

@ -126,7 +126,7 @@ PyDoc_STRVAR(GMPy_doc_mpfr,
#ifdef PY3
static PyNumberMethods mpfr_number_methods =
{
(binaryfunc) GMPy_Number_Add_Slot, /* nb_add */
(binaryfunc) GMPy_MPFR_Add_Slot, /* nb_add */
(binaryfunc) GMPy_MPFR_Sub_Slot, /* nb_subtract */
(binaryfunc) GMPy_MPFR_Mul_Slot, /* nb_multiply */
(binaryfunc) GMPy_MPFR_Mod_Slot, /* nb_remainder */
@ -164,7 +164,7 @@ static PyNumberMethods mpfr_number_methods =
#else
static PyNumberMethods mpfr_number_methods =
{
(binaryfunc) GMPy_Number_Add_Slot, /* nb_add */
(binaryfunc) GMPy_MPFR_Add_Slot, /* nb_add */
(binaryfunc) GMPy_MPFR_Sub_Slot, /* nb_subtract */
(binaryfunc) GMPy_MPFR_Mul_Slot, /* nb_multiply */
(binaryfunc) GMPy_MPFR_TrueDiv_Slot, /* nb_divide */

View file

@ -70,7 +70,12 @@ __MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_exp_t __gmpfr_emax;
#define MPFR_FLAGS_INEXACT 8
#define MPFR_FLAGS_ERANGE 16
#define MPFR_FLAGS_DIVBY0 32
#define MPFR_FLAGS_ALL 63
#define MPFR_FLAGS_ALL (MPFR_FLAGS_UNDERFLOW | \
MPFR_FLAGS_OVERFLOW | \
MPFR_FLAGS_NAN | \
MPFR_FLAGS_INEXACT | \
MPFR_FLAGS_ERANGE | \
MPFR_FLAGS_DIVBY0)
/* Replace some common functions for direct access to the global vars */
#define mpfr_get_emin() (__gmpfr_emin + 0)
@ -92,7 +97,7 @@ __MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_exp_t __gmpfr_emax;
#define mpfr_check_range(x,t,r) \
((((x)->_mpfr_exp) >= __gmpfr_emin && ((x)->_mpfr_exp) <= __gmpfr_emax) \
? ((t) ? (__gmpfr_flags |= MPFR_FLAGS_INEXACT, (t)) : 0) \
? ((t) ? (__gmpfr_flags |= MPFR_FLAGS_INEXACT, (t)) : 0) \
: mpfr_check_range(x,t,r))
/* End of the really bad code. Hopefully.

View file

@ -85,7 +85,7 @@ static PyNumberMethods GMPy_MPZ_number_methods =
#else
static PyNumberMethods GMPy_MPZ_number_methods =
{
(binaryfunc) GMPy_MPZ_Add_Slot, /* nb_add */
(binaryfunc) GMPy_Number_Add_Slot, /* nb_add */
(binaryfunc) GMPy_MPZ_Sub_Slot, /* nb_subtract */
(binaryfunc) GMPy_MPZ_Mul_Slot, /* nb_multiply */
(binaryfunc) GMPy_MPZ_Div2_Slot, /* nb_divide */

View file

@ -86,7 +86,7 @@ static PyNumberMethods GMPy_XMPZ_number_methods =
#else
static PyNumberMethods GMPy_XMPZ_number_methods =
{
(binaryfunc) GMPy_MPZ_Add_Slot, /* nb_add */
(binaryfunc) GMPy_Number_Add_Slot, /* nb_add */
(binaryfunc) GMPy_MPZ_Sub_Slot, /* nb_subtract */
(binaryfunc) GMPy_MPZ_Mul_Slot, /* nb_multiply */
(binaryfunc) GMPy_MPZ_Div2_Slot, /* nb_divide */