passing PyIntType objects by reference

P

Patrick Stinson

when passing an python integer value to a C function
ie.
x = 1
mymod.myfunc(x)

is it possible to change the value of the python object "x" as happens in c
when you pass a pointer to an int? Is there something fundamentally wrong
with this idea, as this does not happen in pure python anyway?

Better yet, I'm wrapping a C api where some of the values are returned by
passing values by reference. Is the only way to simulate this to return a
list with the returned values?

Cheers
 
L

Larry Bates

Why not do following:

result=mymod.myfunc(x)

That way you get a proper return value and I also think
it is clearer what you are doing.

You can pass a memory buffer (see dynawin's membuf)

from dynwin.windll import membuf
import struct

t=membuf(x)
mymod.myfunct(t.address())
x=struct.unpack('l',t.read())

I'm assuming x is a long (interger)

Normally I combine the two methods and return the value
passed back as a normal Python return value.

HTH,
Larry Bates
Syscon, Inc.
 
P

Peter Otten

Patrick said:
when passing an python integer value to a C function
ie.
x = 1
mymod.myfunc(x)

is it possible to change the value of the python object "x" as happens in
c when you pass a pointer to an int? Is there something fundamentally
wrong with this idea, as this does not happen in pure python anyway?
True

If you somehow manage to change a's value into 42, you will change every 1
to 42.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top