Passing a memory address (pointer) to an extension?

P

Philip Semanchuk

I'm writing a Python extension in C that wraps a function which takes
a void * as a parameter. (The function is shmat() which attaches a
chunk of shared memory to the process at the address supplied by the
caller.) I would like to expose this function to Python, but I don't
know how to define the interface.

Specifically, when calling PyArg_ParseTuple(), what letter should I
use to represent the pointer in the format string? The best idea I can
come up with is to use a long and then cast it to a void *, but
assuming that a long is big enough to store a void * is a shaky
assumption. I could use a long long (technically still risky, but
practically probably OK) but I'm not sure how widespread long longs are.

Any advice appreciated.

Thanks
Philip
 
R

Robert Kern

John said:
Use message passing, not shared memory. CPython is so slow that you'll
never notice any performance gain from using shared memory.

Also, if you're asking this question, you probably don't know enough
to keep out of trouble in this area. You need to thoroughly understand
concurrency, locking, shared memory locking, the Python Global Interpreter
Lock, Python memory management, and the implications of the interactions
between all of them. Otherwise you'll get race conditions, crashes within
the Python system, lockups, and/or terrible performance.

And none of the usual debugging tools for Python will help you.

I'm pretty sure the OP is fairly aware of the issues involved in shared memory
and the rest.

http://semanchuk.com/philip/posix_ipc/

He's asking about a detail of Python C API for parsing function arguments. His
ignorance in that area doesn't entail ignorance in other matters. You're being
abusive without cause.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
J

John Nagle

Philip said:
I'm writing a Python extension in C that wraps a function which takes a
void * as a parameter. (The function is shmat() which attaches a chunk
of shared memory to the process at the address supplied by the caller.)
I would like to expose this function to Python, but I don't know how to
define the interface.

Use message passing, not shared memory. CPython is so slow that you'll
never notice any performance gain from using shared memory.

Also, if you're asking this question, you probably don't know enough
to keep out of trouble in this area. You need to thoroughly understand
concurrency, locking, shared memory locking, the Python Global Interpreter
Lock, Python memory management, and the implications of the interactions
between all of them. Otherwise you'll get race conditions, crashes within
the Python system, lockups, and/or terrible performance.

And none of the usual debugging tools for Python will help you.

John Nagle
 
T

Thomas Heller

Philip said:
I'm writing a Python extension in C that wraps a function which takes
a void * as a parameter. (The function is shmat() which attaches a
chunk of shared memory to the process at the address supplied by the
caller.) I would like to expose this function to Python, but I don't
know how to define the interface.

Specifically, when calling PyArg_ParseTuple(), what letter should I
use to represent the pointer in the format string? The best idea I can
come up with is to use a long and then cast it to a void *, but
assuming that a long is big enough to store a void * is a shaky
assumption. I could use a long long (technically still risky, but
practically probably OK) but I'm not sure how widespread long longs are.

I suggest "O!" and a converter function calling PyLong_AsVoidPtr().

Thomas
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top