How to modify object attribute by python C API

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Huayang Xia

    #1

    How to modify object attribute by python C API

    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_GetAtt rString(obj, "attr1");
    PyObject_SetAtt rString(py_obj_ attr1, "attr2",
    PyString_FromSt ring("a"));
    Py_DECREF(py_ob j_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

  • Gabriel Genellina

    #2
    Re: How to modify object attribute by python C API

    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_GetAtt rString(obj, "attr1");
    PyObject_SetAtt rString(py_obj_ attr1, "attr2",
    >PyString_FromS tring("a"));
    Py_DECREF(py_ob j_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_FromSt ring. 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!


    Comment

    • Ben Sizer

      #3
      Re: How to modify object attribute by python C API

      Huayang Xia wrote:
      PyObject* py_obj_attr1 = PyObject_GetAtt rString(obj, "attr1");
      PyObject_SetAtt rString(py_obj_ attr1, "attr2",
      PyString_FromSt ring("a"));
      Py_DECREF(py_ob j_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

      Comment

      • Huayang Xia

        #4
        Re: How to modify object attribute by python C API

        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_FromSt ring and PyString_AsStri ng
        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....@gm ail.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_GetAtt rString(obj, "attr1");
        PyObject_SetAtt rString(py_obj_ attr1, "attr2",
        PyString_FromSt ring("a"));
        Py_DECREF(py_ob j_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

        Comment

        Working...