mp.c: Tighten up the `MP' and `GF' implicit conversions.
[catacomb-python] / mp.c
diff --git a/mp.c b/mp.c
index 163e690..ce04819 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -289,24 +289,27 @@ int convgf(PyObject *o, void *p)
 
 mp *implicitmp(PyObject *o)
 {
-  if (!o ||
-      GF_PYCHECK(o) ||
-      ECPT_PYCHECK(o) ||
-      FE_PYCHECK(o) ||
-      GE_PYCHECK(o))
-    return (0);
-  return (tomp(o));
+  PyObject *l;
+
+  if (!o || GF_PYCHECK(o) || FE_PYCHECK(o)) return (0);
+  else if (MP_PYCHECK(o)) return (MP_COPY(MP_X(o)));
+  else if (PFILT_PYCHECK(o)) return (MP_COPY(PFILT_F(o)->m));
+#ifdef PY2
+  else if (PyInt_Check(o)) return (mp_fromlong(MP_NEW, PyInt_AS_LONG(o)));
+#endif
+  else if ((l = PyNumber_Index(o)) != 0) {
+#ifdef PY2
+    if (PyInt_Check(o)) return (mp_fromlong(MP_NEW, PyInt_AS_LONG(o)));
+#endif
+    if (PyLong_Check(o)) return (mp_frompylong(o));
+  }
+  PyErr_Clear(); return (0);
 }
 
 mp *implicitgf(PyObject *o)
 {
-  if (!o ||
-      MP_PYCHECK(o) ||
-      ECPT_PYCHECK(o) ||
-      FE_PYCHECK(o) ||
-      GE_PYCHECK(o))
-    return (0);
-  return (tomp(o));
+  if (GF_PYCHECK(o)) return (MP_COPY(MP_X(o)));
+  return (0);
 }
 
 static int mpbinop(PyObject *x, PyObject *y, mp **xx, mp **yy)