FAQ
I'm writing my own python extension module with the C API. In python
all functions pass arguments by reference, but how can I make use of
this in C? Right now, I am using:

PyArg_ParseTuple(args, "(ii)(ii)", &faceId1, &vertId1, &faceId2,
&vertId2)

I want the to change the faceId's in my function. From what I've seen
you can't do this safely with PyArg_ParseTuple.

Do I have another option?

Otherwise, I just have to return an extra variable. But it would be
much much nicer to just have the faceId's change if the arguments
passed were variables.

Thanks!

Search Discussions

  • Martin v. Löwis at Jun 23, 2007 at 6:31 pm

    stuart.tett at gmail.com schrieb:
    I'm writing my own python extension module with the C API. In python
    all functions pass arguments by reference
    Can you please show an example what you mean by that? There is no
    "pass-by-reference" in Python: a function can not normally modify
    the variable in the caller.

    When you show what precisely you want to achieve, it should be easy
    to say how to do that in C.

    Regards,
    Martin
  • Stuart Tett at Jun 23, 2007 at 6:56 pm
    Thanks for that clarification Martin. When I googled it before, the
    first page I read said "Python passes all arguments using 'pass by
    reference'." However, after seeing your reply and further searching I
    see that this is not true.

    I have a python function insertEdge which takes to 2-tuples of
    (faceid,vertexid) and returns the edgeid. But upon execution of the
    function the two vertexid's end up sharing the same faceid. So right
    now my solution is just to also return the two new (faceid,vertexid).
    These will both have the same vertexid as before, but have a different
    faceid then before (sharing the same faceid).

    Here for example is a script to create a triangle:

    v1 = createVertex((0,0,0))
    v2 = createVertex((1,0,0))
    e1,v1,v2 = insertEdge(v1,v2)
    v3 = createVertex((0,1,0))
    e2,v2,v3 = insertEdge(v2,v3)
    e3,v3,v1 = insertEdge(v3,v1)

    Here is the C++ code:

    static PyObject *
    dlfl_insert_edge(PyObject *self, PyObject *args)
    {
    uint faceId1;
    int vertId1;
    uint faceId2;
    int vertId2;
    int edgeId = -1;

    if( !PyArg_ParseTuple(args, "(ii)(ii)", &faceId1, &vertId1,
    &faceId2, &vertId2) )
    return NULL;

    if( currObj ) {
    edgeId = DLFL::insertEdge( currObj, faceId1, vertId1, faceId2,
    vertId2 );
    currObj->clearSelected( );
    }

    return Py_BuildValue("i,(ii)(ii)", edgeId, faceId1, vertId1,
    faceId2, vertId2 );
    }

    This works, but...
    Any suggestions if there is a cleaner way?

    Thanks!
    On Jun 23, 1:31 pm, "Martin v. L?wis" wrote:
    stuart.t... at gmail.com schrieb:
    I'm writing my own python extension module with the C API. In python
    all functions pass arguments by reference
    Can you please show an example what you mean by that? There is no
    "pass-by-reference" in Python: a function can not normally modify
    the variable in the caller.

    When you show what precisely you want to achieve, it should be easy
    to say how to do that in C.

    Regards,
    Martin
  • Carsten Haese at Jun 23, 2007 at 6:47 pm

    On Sat, 2007-06-23 at 18:25 +0000, stuart.tett at gmail.com wrote:
    I'm writing my own python extension module with the C API. In python
    all functions pass arguments by reference,
    "Pass by reference", while correct from a certain standpoint, is to be
    taken with a large grain of salt. It is correct in so far as the value
    is not copied. It is incorrect in so far as, in general, you may not be
    able to modify the object that's passed.

    The reference you receive can only be used to call methods of the
    referenced object, if it has any, or manipulate attributes of the
    object, if it has any. It can not be used to replace the object with
    another object.
    but how can I make use of
    this in C? Right now, I am using:
    You can't.
    PyArg_ParseTuple(args, "(ii)(ii)", &faceId1, &vertId1, &faceId2,
    &vertId2)

    I want the to change the faceId's in my function. From what I've seen
    you can't do this safely with PyArg_ParseTuple.
    Not with PyArg_ParseTuple, not with anything. Your function receives a
    reference to an int object. Since int objects are immutable, you can't
    replace the number that's in the int object.
    Do I have another option?
    Return the value instead of trying to produce side-effects in the
    caller's name space.

    HTH,

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 23, '07 at 6:25p
activeJun 23, '07 at 6:56p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase