How to modify object attribute by python C API

H

Huayang Xia

I get a python object by running a class' constructor. Then I need to
modify the instance's attribute just like obj.attr1.attr2 = 'a' if in
python's term.

PyObject* py_obj_attr1 = PyObject_GetAttrString(obj, "attr1");
PyObject_SetAttrString(py_obj_attr1, "attr2",
PyString_FromString("a"));
Py_DECREF(py_obj_attr1);

The object py_obj_attr1 is said to be a "New reference". It's
confusing, does it refer to the same object as "obj.attr1" in python's
term? So that the above code equals: obj.attr1.attr2 = 'a' in python's
term.

I
 
G

Gabriel Genellina

I get a python object by running a class' constructor. Then I need to
modify the instance's attribute just like obj.attr1.attr2 = 'a' if in
python's term.

PyObject* py_obj_attr1 = PyObject_GetAttrString(obj, "attr1");
PyObject_SetAttrString(py_obj_attr1, "attr2",
PyString_FromString("a"));
Py_DECREF(py_obj_attr1);

The object py_obj_attr1 is said to be a "New reference". It's
confusing, does it refer to the same object as "obj.attr1" in python's
term? So that the above code equals: obj.attr1.attr2 = 'a' in python's
term.


Read the Introduction in the "Python/C API Reference Manual". If you
plan to use Python from C code, better learn carefully how reference
counting works, or your progam won't work at all (or crash, in
unrelated places, at a later time...)
Going back to the example, yes, it's like obj.attr1.attr2 = 'a'. But
you should check that py_obj_attr1 is not NULL, and the same for
PyString_FromString. There are examples in the doc cited and in
"Extending and Embedding".


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
B

Ben Sizer

Huayang said:
PyObject* py_obj_attr1 = PyObject_GetAttrString(obj, "attr1");
PyObject_SetAttrString(py_obj_attr1, "attr2",
PyString_FromString("a"));
Py_DECREF(py_obj_attr1);

The object py_obj_attr1 is said to be a "New reference". It's
confusing, does it refer to the same object as "obj.attr1" in python's
term?

Yes, it refers to the same object. Each object can have many
references, and is deleted when all the references are gone. The new
reference in this case means that Python has taken note that there's a
new use of that object - your C code. It means it won't delete that
object, even if no more Python code refers to it, because it knows your
C code holds a reference to it. Therefore, when your C code no longer
needs to access the object, you call Py_DECREF.
 
H

Huayang Xia

Thanks for the replies.

For the checking, I found we have to spend a lot of codes to check the
Python API's results. That is a pain. Is it reasonable to ignore the
result check of APIs like PyString_FromString and PyString_AsString
which will fail only when out of memory. Unfortunately there is no
exception thrown (in C++) so that we can integrate the error handling.
What is the best way(less code) to handle the error checking in normal
practice?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top