[Numpy-svn] r5029 - trunk/numpy/core/src

numpy-svn@scip... numpy-svn@scip...
Sun Apr 13 12:43:33 CDT 2008


Author: charris
Date: 2008-04-13 12:43:32 -0500 (Sun, 13 Apr 2008)
New Revision: 5029

Modified:
   trunk/numpy/core/src/arraytypes.inc.src
Log:
Simplify code in MyPyFloat_AsDouble and MyPyLong_As*.
Add MyPyLong_AsLong.
Use code generator for repeated code.


Modified: trunk/numpy/core/src/arraytypes.inc.src
===================================================================
--- trunk/numpy/core/src/arraytypes.inc.src	2008-04-13 10:04:14 UTC (rev 5028)
+++ trunk/numpy/core/src/arraytypes.inc.src	2008-04-13 17:43:32 UTC (rev 5029)
@@ -1,84 +1,6 @@
 /* -*- c -*- */
 #include "config.h"
 
-
-static longlong
-MyPyLong_AsLongLong(PyObject *obj)
-{
-    longlong ret;
-
-    if (!PyLong_Check(obj)) {
-        PyObject *mylong;
-        mylong = PyNumber_Long(obj);
-        if (mylong == NULL) {
-            return (longlong) -1;
-        }
-        obj = mylong;
-    }
-    else {
-        Py_INCREF(obj);
-    }
-
-    ret = PyLong_AsLongLong(obj);
-    Py_DECREF(obj);
-    return ret;
-}
-
-
-static ulong
-MyPyLong_AsUnsignedLong(PyObject *obj)
-{
-    ulong ret;
-
-    if (!PyLong_Check(obj)) {
-        PyObject *mylong;
-        mylong = PyNumber_Long(obj);
-        if (mylong == NULL) {
-            return (ulong) -1;
-        }
-        obj = mylong;
-    }
-    else {
-        Py_INCREF(obj);
-    }
-
-    ret = PyLong_AsUnsignedLong(obj);
-    if (PyErr_Occurred()) {
-        PyErr_Clear();
-        ret = (ulong) PyLong_AsLong(obj);
-    }
-    Py_DECREF(obj);
-    return ret;
-}
-
-
-static ulonglong
-MyPyLong_AsUnsignedLongLong(PyObject *obj)
-{
-    ulonglong ret;
-
-    if (!PyLong_Check(obj)) {
-        PyObject *mylong;
-        mylong = PyNumber_Long(obj);
-        if (mylong == NULL) {
-            return (ulonglong) -1;
-        }
-        obj = mylong;
-    }
-    else {
-        Py_INCREF(obj);
-    }
-
-    ret = PyLong_AsUnsignedLongLong(obj);
-    if (PyErr_Occurred()) {
-        PyErr_Clear();
-        ret = (ulonglong) PyLong_AsLongLong(obj);
-    }
-    Py_DECREF(obj);
-    return ret;
-}
-
-
 static double
 _getNAN(void) {
 #ifdef NAN
@@ -106,23 +28,59 @@
 static double
 MyPyFloat_AsDouble(PyObject *obj)
 {
-    if (PyString_Check(obj)) {
-        PyObject *tmp = PyFloat_FromString(obj, 0);
-        if (tmp) {
-            double d = PyFloat_AsDouble(tmp);
-            Py_DECREF(tmp);
-            return d;
-        }
-        else {
-            return _getNAN();
-        }
-    }
-    if (obj == Py_None)
+    double ret = 0;
+    PyObject *num = PyNumber_Float(obj);
+
+    if (num == NULL) {
         return _getNAN();
-    return PyFloat_AsDouble(obj);
+    }
+    ret = PyFloat_AsDouble(num);
+    Py_DECREF(num);
+    return ret;
 }
 
 
+/**begin repeat
+#type=long,longlong#
+#Type=Long,LongLong#
+*/
+
+static @type@
+MyPyLong_As@Type@ (PyObject *obj)
+{
+    @type@ ret;
+    PyObject *num = PyNumber_Long(obj);
+
+    if (num == NULL) {
+        return -1;
+    }
+    ret = PyLong_As@Type@(num);
+    Py_DECREF(num);
+    return ret;
+}
+
+
+static u@type@
+MyPyLong_AsUnsigned@Type@ (PyObject *obj)
+{
+    u@type@ ret;
+    PyObject *num = PyNumber_Long(obj);
+
+    if (num == NULL) {
+        return -1;
+    }
+    ret = PyLong_AsUnsigned@Type@(num);
+    if (PyErr_Occurred()) {
+        PyErr_Clear();
+        ret = PyLong_As@Type@(num);
+    }
+    Py_DECREF(num);
+    return ret;
+}
+
+/**end repeat**/
+
+
 /****************** getitem and setitem **********************/
 
 /**begin repeat



More information about the Numpy-svn mailing list