Re: Awkwardness of C API for making tuples

2005-02-03 Thread Fredrik Lundh
Steve Holden wrote: >> in theory, if PyInt_FromLong succeeds, and PyTuple_SetItem fails, you'll leak >> an object. > > And in practice this will only happen during a period when you are relying > critically on it *not* > to ... yeah, but if PyTuple_SetItem fails in this case, you better move th

Re: Awkwardness of C API for making tuples

2005-02-03 Thread Jeremy Bowers
On Thu, 03 Feb 2005 07:45:22 -0500, Steve Holden wrote: > Fredrik Lundh wrote: >> in theory, if PyInt_FromLong succeeds, and PyTuple_SetItem fails, you'll leak >> an object. >> >> >> > And in practice this will only happen during a period when you are > relying critically on it *not* to ...

Re: Awkwardness of C API for making tuples

2005-02-03 Thread Steve Holden
Fredrik Lundh wrote: Dave Cole wrote: for (i = 0; i < num_values; i++) { PyObject *obj; obj = PyInt_FromLong(value[i]); if (obj == NULL || PyTuple_SetItem(tuple, i, obj) != 0) { Py_DECREF(tuple); return NULL; } } in theory, if PyInt_FromLong succeeds, and PyTuple_SetItem fa

Re: Awkwardness of C API for making tuples

2005-02-02 Thread Fredrik Lundh
Dave Cole wrote: > for (i = 0; i < num_values; i++) { > PyObject *obj; > > obj = PyInt_FromLong(value[i]); > if (obj == NULL > || PyTuple_SetItem(tuple, i, obj) != 0) { > Py_DECREF(tuple); > return NULL; > } > } in theory, if PyInt_FromLong succeeds, and PyTuple_SetIte

Re: Awkwardness of C API for making tuples

2005-02-02 Thread Dave Cole
John Machin wrote: Dave Opstad wrote: One of the functions in a C extension I'm writing needs to return a tuple of integers, where the length of the tuple is only known at runtime. I'm currently doing a loop calling PyInt_FromLong to make the integers, What is the purpose of this first loop? In wh

Re: Awkwardness of C API for making tuples

2005-02-02 Thread Fredrik Lundh
Dave Opstad wrote: > This would certainly be simpler, although I'm not sure I'm as clear as > to what happens if, say, in the middle of this loop a PyInt_FromLong > fails. I know that PyTuple_SetItem steals the reference; does that mean > I could just Py_DECREF the tuple and all the pieces will be

Re: Awkwardness of C API for making tuples

2005-02-01 Thread John Machin
Dave Opstad wrote: > In article <[EMAIL PROTECTED]>, > "John Machin" <[EMAIL PROTECTED]> wrote: > > > What is the purpose of this first loop? > > Error handling. If I can't successfully create all the PyInts then I can > dispose the ones I've made and not bother making the tuple at all. > > > > I

Re: Awkwardness of C API for making tuples

2005-02-01 Thread Dave Opstad
In article <[EMAIL PROTECTED]>, "John Machin" <[EMAIL PROTECTED]> wrote: > What is the purpose of this first loop? Error handling. If I can't successfully create all the PyInts then I can dispose the ones I've made and not bother making the tuple at all. > > In what variable-length storage are

Re: Awkwardness of C API for making tuples

2005-02-01 Thread John Machin
Dave Opstad wrote: > One of the functions in a C extension I'm writing needs to return a > tuple of integers, where the length of the tuple is only known at > runtime. I'm currently doing a loop calling PyInt_FromLong to make the > integers, What is the purpose of this first loop? In what variabl

Re: Awkwardness of C API for making tuples

2005-02-01 Thread Diez B. Roggisch
> One of the functions in a C extension I'm writing needs to return a > tuple of integers, where the length of the tuple is only known at > runtime. I'm currently doing a loop calling PyInt_FromLong to make the > integers, then PyTuple_New, and finally a loop calling PyTuple_SET_ITEM > to set the t