[Bf-python] checking PyInt_FromLong..

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


Campbell Barton wrote:
> 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.

I read through the Python 2.5.1 source to see how it deals with this, 
and it only checks when PyInt_FromLong for NULL when building values 
within a loop. (a few exceptions and these may be calld from a loop)

intobject.c its self returns ints without checking, this makes sense 
because returning NULL will raise an error anyway. where as setting the 
value of a list for instance wont.





-- 
Campbell J Barton (ideasman42)



More information about the Bf-python mailing list