Py_INCREF() incomprehension

Hegedüs Ervin airween at gmail.com
Tue Apr 26 13:28:00 EDT 2011


Dear Thomas,

thank you again,

> The ownership rules say that the input parameter belongs to the
> caller who holds it at least until we return. (We just "borrow" it.)
> So no action needed.

ok, its' clear, I understand,
 
> >>* Py_BuildValue()
> 
> This function "transfers ownership", as it is none of
> (PyTuple_GetItem(), PyList_GetItem(), PyDict_GetItem(),
> PyDict_GetItemString()).
> 
> So the value it returns belongs to us, for now.
> 
> We do transfer ownership to our caller (implicitly), so no action is
> required as well here.

also,
 
> >so, it means when I implicit allocate a new object (whit
> >Py_BuildValue()), Python's GC will free that pointer when it
> >doesn't require anymore?
> 
> In a way, yes. But you have to obey ownership: whom belongs the
> current reference? If it is not ours, and we need it, we do
> Py_(X)INCREF(). If we got it, but don't need it, we do
> Py_(X)DECREF().

right, it's clear again,
 
> >>BTW: Is there any reason for using calloc()? malloc() would probably
> >>be faster...
> >
> >may be, I didn't measure it ever... but calloc() gives clear
> >space... :)
> 
> Ok. (But as sizeof(char) is, by C standard definition, always 1, you
> can write it shorter.)

oh' well, thanks, I just wrote "from a wrist" :), I just realize
it now... :)

Another question: here is an another part ot my code:

static PyObject*
mycrypt_decrypt(PyObject *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, "ss", &data, &path)) {
        return NULL;
    }

...

}

When I call this function from Python without argument or more
than it expects, I get an exception, eg.:
TypeError: function takes exactly 2 arguments (0 given)

But, when I don't read input arguments (there isn't
PyArg_ParseTuple), there isn't exception.

How Python handle the number of arguments? I just ask this,
because I don't set errstring with PyErr_SetString, but I get
TypeError - how does Python knows, this error raised?

Hope you understand my question... :)

thanks for all:


a.



More information about the Python-list mailing list