473,320 Members | 2,094 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.

Re: Passing a memory address (pointer) to an extension?

On Oct 22, 2008, at 8:33 PM, Robert Kern 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.
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 recommend not giving the user access to that argument. Just use
NULL and let shmat() pick a starting address. I don't think it's
really safe to let the user pick, even in C. Perhaps if you're doing
*really* low level stuff. Of course, being that low level seems to
be the point of posix_ipc, so maybe I should let you get on with it.
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 know better than everyone who might want to use my module. Using
values other than NULL might be unwise (the man page I'm looking at
states that clearly) but isn't one of the core design philosophies of
Python "we're all consenting adults"?
Anyways, the format "n" in Python >= 2.5 will correspond to a
Py_ssize_t integer, which will always be the size of a pointer.
Thanks, I had looked at that but rejected it for two reasons. First,
it requires Python 2.5. (But I could make that a minimum requirement
if necessary.) Second, PEP 353 says this:

"Conceptually, Py_intptr_t and Py_ssize_t are different things:
Py_intptr_t needs to be the same size as void*, and Py_ssize_t the
same size as size_t. These could differ, e.g. on machines where
pointers have segment and offset. On current flat-address space
machines, there is no difference, so for all practical purposes,
Py_intptr_t would have worked as well."

I guess what they're saying is that presently there's no practical
difference between the size of a Py_intptr_t (which seems to be what I
need) and a Py_ssize_t. I can go with that, but to me it still smacks
of the cowboy coding I did on 16-bit platforms that assumed that longs
and pointers were the same size and happily cast between them. It
works *now*, but...

Thanks also to Gabriel Genellina who wrote much the same.
Thomas Heller's suggestion of using PyLong_AsVoidPtr() is perfect; I
was not aware of that function before.

You can return the void* that you get from shmat() with a PyCPointer
object or make a new, small type that encapsulates a pointer
attached via shmat(). The benefit of a real type is that you can
type-check the input to shmdt() for safety. I strongly recommend
either of those approaches over returning an integer.
Fortunately shmdt() will be wrapped inside detach() which is a method
object so I can use the pointer stored inside my internal C data
structure. But you're right, I will need to expose the attached
address to the caller as an attribute if nothing else. That's been a
feature request for this module.

bye
Philip
Oct 23 '08 #1
0 1375

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

Similar topics

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...
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...
4
by: Philip Semanchuk | last post by:
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...
0
by: Robert Kern | last post by:
Philip Semanchuk wrote: I recommend not giving the user access to that argument. Just use NULL and let shmat() pick a starting address. I don't think it's really safe to let the user pick, even...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.