Is one correct in stating the following:
If a Python object is created in a C function, but the function doesn't return it, no INCREF
is needed, but a DECREF
is.
[false]If the function does return it, you do need to INCREF
, in the function that receives the return value.[/false]
When assigning C typed variables as attributes, like double
, int
etc., to the Python object, no INCREF
or DECREF
is needed.
Is the following safe to use? Assigning Python objects as attributes to your other Python objects goes like this:
PyObject *foo;
foo = bar; // A Python object
tmp = self->foo;
Py_INCREF(foo);
self->foo = foo;
Py_XDECREF(tmp);
// taken from the manual, but it is unclear if this works in every situation
Deallocation of a Python object needs to DECREF
for every other Python object that it has as an attribute, but not for attributes that are C types.
EDIT:
With regards to 'C type as an attribute', I mean bar and baz:
typedef struct {
PyObject_HEAD
PyObject *foo;
int bar;
double baz;
} FooBarBaz;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…