[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Int function simplified
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;
> }
>