473,385 Members | 1,798 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,385 software developers and data experts.

Passing a memory address (pointer) to an extension?

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
Oct 22 '08 #1
4 1936
Philip Semanchuk wrote:
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
Oct 23 '08 #2
John Nagle wrote:
Philip Semanchuk wrote:
>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.
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

Oct 23 '08 #3
Philip Semanchuk schrieb:
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
Oct 23 '08 #4

On Oct 23, 2008, at 2:13 AM, Thomas Heller wrote:
Philip Semanchuk schrieb:
>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, this sounds perfect, thank you.
bye
Philip

Oct 23 '08 #5

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

Similar topics

12
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
7
by: shanemh | last post by:
I'm starting out with c++ and for some reason I cant get my brain around this one: If I have the following: void Foo (someClass& x) {} int Main (void) {
15
by: polas | last post by:
Hi everyone - I have a question. I am just playing around with C (I realise there are better ways to do what I want, but I would like to do it this way to increase my understanding of C) and would...
5
by: CapCity | last post by:
I'm sure I'm missing something simple - I do not code in C regularly, and the breaks are long enough for me to forget. The situation I have is I need to create an array but I do not know the...
0
by: Philip Semanchuk | last post by:
On Oct 22, 2008, at 8:33 PM, Robert Kern wrote: I agree. To deny users of this module access to this param of shmat() would be design arrogance on my part. To do so would be to claim that I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.