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

Re: [pygame] Int function simplified



The attached patch applies the same change for float and uint

Campbell Barton wrote:
Cool, I should have mentioned this same change works for floats.

René Dudfield wrote:
cool, thanks :)

Committed revision 1026.

On 7/27/07, Campbell Barton <cbarton@xxxxxxxxxx> wrote:
Hi, I have been looking into parsing  numbers with Blender3D's api and
thought Id take a look at how PyGame parses numbers.

Even though youd think PyInt_AsLong would only work with PyInt's it
actually converts any type that supports the PyNumber protocol into an
in. - so theres no advantage in using PyNumber_Int.

the code below is from base.c, a smaller replacement for IntFromObj is
below. This avoids creating and removing a PyObject every time.

_________________________________________________
/* internal C API utility functions */
static int
IntFromObj (PyObject* obj, int* val)
{
     PyObject* intobj;

     if (PyNumber_Check (obj))
     {
         if (!(intobj = PyNumber_Int (obj)))
         {
             PyErr_Clear ();
             return 0;
         }
         *val = PyInt_AsLong (intobj);
         Py_DECREF (intobj);
         if (PyErr_Occurred ())
         {
             PyErr_Clear ();
             return 0;
         }
         return 1;
     }
     return 0;
}


_____________________________________________-

/* internal C API utility functions */
static int
IntFromObj (PyObject* obj, int* val)
{
     int tmp_val;
     tmp_val = PyInt_AsLong (obj);
     if (tmp_val == -1 && PyErr_Occurred ())
     {
         PyErr_Clear ();
         return 0;
     }
     *val = tmp_val;
     return 1;
}






--
Campbell J Barton (ideasman42)
Index: src/base.c
===================================================================
--- src/base.c	(revision 1026)
+++ src/base.c	(working copy)
@@ -222,8 +222,7 @@
 /* internal C API utility functions */
 static int
 IntFromObj (PyObject* obj, int* val) {
-    int tmp_val;
-    tmp_val = PyInt_AsLong (obj);
+    int tmp_val = (int)PyInt_AsLong (obj);
     if (tmp_val == -1 && PyErr_Occurred ())
     {
         PyErr_Clear ();
@@ -264,17 +263,14 @@
 
 static int FloatFromObj (PyObject* obj, float* val)
 {
-    PyObject* floatobj;
-
-    if (PyNumber_Check (obj))
+    float tmp_val = (float)PyFloat_AsDouble (obj);
+    if (tmp_val == -1 && PyErr_Occurred ())
     {
-        if (!(floatobj = PyNumber_Float (obj)))
-            return 0;
-        *val = (float) PyFloat_AsDouble (floatobj);
-        Py_DECREF (floatobj);
-        return 1;
+        PyErr_Clear ();
+        return 0;
     }
-    return 0;
+    *val = tmp_val;
+    return 1;
 }
 
 static int
@@ -309,17 +305,14 @@
 static int
 UintFromObj (PyObject* obj, Uint32* val)
 {
-    PyObject* intobj;
-
-    if (PyNumber_Check (obj))
+    Uint32 tmp_val = (Uint32)PyInt_AsLong (obj);
+    if (tmp_val == -1 && PyErr_Occurred ())
     {
-        if (!(intobj = PyNumber_Int (obj)))
-            return 0;
-        *val = (Uint32) PyInt_AsLong (intobj);
-        Py_DECREF (intobj);
-        return 1;
+        PyErr_Clear ();
+        return 0;
     }
-    return 0;
+    *val = tmp_val;
+    return 1;
 }
 
 static Uint32