[Bf-python] checking PyInt_FromLong..

Stephen Swaney sswaney at centurytel.net
Tue Jun 26 06:21:21 CEST 2007


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.


-- 
Stephen Swaney			
sswaney at centurytel.net





More information about the Bf-python mailing list