[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] PATCH: faster float conversion
- To: pygame-users@xxxxxxxx
- Subject: [pygame] PATCH: faster float conversion
- From: Campbell Barton <ideasman42@xxxxxxxxx>
- Date: Sat, 4 Jul 2009 03:17:22 -0700
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sat, 04 Jul 2009 06:48:51 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com; s=gamma;        h=domainkey-signature:mime-version:received:date:message-id:subject         :from:to:content-type;        bh=QmtHf8AiRger4cSKckzHCujGiALcZwSwkkxSiPkfezw=;        b=Yme1HLfkpAc39JgB4M/U7kDbmhUEubzIv5Qb1Evr24pQrDfH00twuX2QXP8IXl8Qtw         avBaS+i7Ezq0n0UPDzD6YxpeR3EUSIVDC+TKEhtAE9rOvvroTqV8kZQDwxNjmQnauL3F         SML99qBmPSQmVz/+qaeOvGlwxrBOk9OwhutOk=
- Domainkey-signature: a=rsa-sha1; c=nofws;        d=gmail.com; s=gamma;        h=mime-version:date:message-id:subject:from:to:content-type;        b=hKAfm1ncWZuLfYSgCM4RokygMx1DdoYQOsDBC39gMo7YQ+15XJKfOPrwsaZkMGCW1Y         KqBq350ZkGy65N5Pp2b8Gd2rOktf9hG1qO+Z7KX8rW7UoAKB8O0J89Wcd9E6NN46NH7l         R2ieWqLRtyQ2ub+xe18O/6lp7uAvhlbG5xEGk=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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,