473,756 Members | 4,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ParseTupl e(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_ParseTupl e.

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 1351
st*********@gma il.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*********@gma il.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_ParseTupl e(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_ParseTupl e.
Not with PyArg_ParseTupl e, 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,vertexi d) 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,vertexi d).
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,v 2)
v3 = createVertex((0 ,1,0))
e2,v2,v3 = insertEdge(v2,v 3)
e3,v3,v1 = insertEdge(v3,v 1)

Here is the C++ code:

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

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

if( currObj ) {
edgeId = DLFL::insertEdg e( currObj, faceId1, vertId1, faceId2,
vertId2 );
currObj->clearSelecte d( );
}

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.loewi s.dewrote:
stuart.t...@gma il.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
3819
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 - http://www.zend.com/zend/trick/tricks12apr.php . where I feel the support for SVG is not very clear to be exact. Just curious if there is a library that comes with PHP that can help us to create PHP without exactly boggling down in the SVG syntax ( XML ) as given in the...
0
319
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 in the past in VB6 and have not had a problem. However, VB6 is not a 'strictly typed' language, and I think it is the type conversions from C# to the ODM API that is cauing my problem.
0
1320
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 in the past in VB6 and have not had a problem. However, VB6 is not a 'strictly typed' language, and I think it is the type conversions from C# to the ODM API that is cauing my problem.
61
3152
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 with "" it is - correct? The system seems to handle a null the same as "".
17
2595
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 something with b //possibly store in a list
0
9325
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9716
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9716
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.