473,465 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1380

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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
0
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.