473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C API: passing by reference

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!

Jun 23 '07 #1
3 1334
st*********@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
Jun 23 '07 #2
On Sat, 2007-06-23 at 18:25 +0000, st*********@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,

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 23 '07 #3
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" <mar...@v.loewis.dewrote:
stuart.t...@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
Jun 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Karthik | last post by:
Hi, I am just curious if there is any existing API in PHP for rendering SVG (Scalable Vector Graphics). A quick search in Google pointed to me a place in zend.com -...
0
by: Cluggas | last post by:
Really need some help with a problem that is bugging me. I am trying to wrtie a C#.NET wrapper for the ODM (Open Document Management) API. I have written wrapping classes for other Windows API's...
0
by: Cluggas | last post by:
Really need some help with a problem that is bugging me. I am trying to wrtie a C#.NET wrapper for the ODM (Open Document Management) API. I have written wrapping classes for other Windows API's...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.