C-API: A beginner's problem

Fabian Steiner lists at fabis-site.net
Mon Mar 20 00:19:54 EST 2006


Georg Brandl wrote:
> Fabian Steiner wrote:
>> [...]
>> 	for (i = 0; i <= seqlen; i++) {
> 
> That is one iteration too much. Use
> 
>         for (i = 0; i < seglen; i++)
> 
>> 		item = PySequence_Fast_GET_ITEM(seq, i);
> 
> Now item is a PyObject*. You'll have to convert it to an integer now:
> 
>                 it = PyInt_AsLong(item);

Why do you use PyInt_AsLong() here? As the documentation says it returns 
a long type: long PyInt_AsLong(PyObject *io)
On the other hand I can't find anything like PyInt_AsInt().

>                 if (it == -1 && PyErr_Occurred()) {
>                     Py_DECREF(seq);

Why is this Py_DECREF() needed? What does it do exactly? When do I have 
to call this function? Obviously, there is also Py_INCREF(). When do you 
need this function?

> [...]
> There's quite a bit you can overlook, especially stale references to PyObjects.
> I'm not even sure the code compiles or runs correctly with my corrections ;)

Now, it compiles fine, without any warnings and using it in Python works 
either :-) Now I have to try to understand what the different parts are 
doing and why they are necessary.

Thank you very much so far!

Cheers,
Fabian



More information about the Python-list mailing list