By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

How to modify object attribute by python C API

P: n/a
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

Jan 11 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
At Thursday 11/1/2007 18:37, Huayang Xia wrote:
>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

Jan 12 '07 #2

P: n/a
Huayang Xia wrote:
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.

--
Ben Sizer

Jan 12 '07 #3

P: n/a
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?

On Jan 11, 4:37 pm, "Huayang Xia" <huayang....@gmail.comwrote:
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
Jan 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.