[Bf-python] checking PyInt_FromLong..

Campbell Barton cbarton at metavr.com
Tue Jun 26 06:25:39 CEST 2007


Stephen Swaney wrote:
> On Tue, Jun 26, 2007 at 01:43:24PM +1000, Campbell Barton wrote:
>> While looking through the API
>>
>> I noticed some functions check that the result of PyInt_FromLong is NULL 
>> (rather then a valid PyObject pointer)
>>
>> I dont think theres any point in this since python its self dosnt check 
>> if its valid before assigning an the int value to a member of the 
>> pointer - see
>>
>> "v->ob_ival = ival;"
>>
>> if the pointer was invalid it could crash anyway-
> 
> Any error was returned before this point.
> 
> The error comes from Python running out of memory. See below:
> 
>   if (free_list == NULL) {
>  		if ((free_list = fill_free_list()) == NULL)
>  			return NULL;
>   }
>   /* Inline PyObject_New */
>   v = free_list;
>   free_list = (PyIntObject *)v->ob_type;
>   PyObject_INIT(v, &PyInt_Type);
>   v->ob_ival = ival;
>  	return (PyObject *) v;
> 
>> Can we remove these checks from our own code?
> 
> I suppose you can look at this as a philosophical question:
> 
> Do you want to write bullet-proof code or are you willing to
> crash under exceptional circumstances?
> 
> Seems only polite to at least say goodbye before leaving the party.

Ah your right, still- we should be consistant
* Blender dosnt check most of its mallocs are successful (though it could)

* Our own api code only checks around half of PyInt_FromLong, in many 
places it just assumes it worked. and thats across the codebase.



More information about the Bf-python mailing list