[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] PATCH: faster float conversion



This patch changes how floats are converted by FloatFromObj, this is a
normal way to handle floats in python (python its self uses this).
int's and non float types with __float__ will be converted if possible.

Also made 2 other small improvements.
-- 
- Campbell
Index: src/base.c
===================================================================
--- src/base.c	(revision 2466)
+++ src/base.c	(working copy)
@@ -70,11 +70,9 @@
 
     if (compiled.major != linked->major || compiled.minor != linked->minor)
     {
-        char err[1024];
-        sprintf (err, "SDL compiled with version %d.%d.%d, linked to %d.%d.%d",
+		PyErr_Format(PyExc_RuntimeError, "SDL compiled with version %d.%d.%d, linked to %d.%d.%d",
                  compiled.major, compiled.minor, compiled.patch,
                  linked->major, linked->minor, linked->patch);
-        PyErr_SetString (PyExc_RuntimeError, err);
         return 0;
     }
     return 1;
@@ -99,20 +97,15 @@
 }
 
 static PyObject*
-register_quit (PyObject* self, PyObject* arg)
+register_quit (PyObject* self, PyObject* value)
 {
-    PyObject* quitfunc;
-
-    if (!PyArg_ParseTuple (arg, "O", &quitfunc))
-        return NULL;
-
     if (!quitfunctions)
     {
         quitfunctions = PyList_New (0);
         if (!quitfunctions)
             return NULL;
     }
-    PyList_Append (quitfunctions, quitfunc);
+    PyList_Append (quitfunctions, value);
 
     Py_RETURN_NONE;
 }
@@ -287,19 +280,18 @@
     return 1;
 }
 
-static int FloatFromObj (PyObject* obj, float* val)
+static int
+FloatFromObj (PyObject* obj, float* val)
 {
-    PyObject* floatobj;
+    float f= (float)PyFloat_AsDouble (obj);
 
-    if (PyNumber_Check (obj))
-    {
-        if (!(floatobj = PyNumber_Float (obj)))
-            return 0;
-        *val = (float) PyFloat_AsDouble (floatobj);
-        Py_DECREF (floatobj);
-        return 1;
-    }
-    return 0;
+    if (f==-1 && PyErr_Occurred()) {
+		PyErr_Clear ();
+        return 0;
+	}
+    
+    *val = f;
+    return 1;
 }
 
 static int
@@ -595,7 +587,7 @@
 {
     { "init", (PyCFunction) init, METH_NOARGS, DOC_PYGAMEINIT },
     { "quit", (PyCFunction) quit, METH_NOARGS, DOC_PYGAMEQUIT },
-    { "register_quit", register_quit, METH_VARARGS, DOC_PYGAMEREGISTERQUIT },
+    { "register_quit", register_quit, METH_O, DOC_PYGAMEREGISTERQUIT },
     { "get_error", (PyCFunction) get_error, METH_NOARGS, DOC_PYGAMEGETERROR },
     { "set_error", (PyCFunction) set_error, METH_VARARGS, DOC_PYGAMESETERROR },
     { "get_sdl_version", (PyCFunction) get_sdl_version, METH_NOARGS,