473,605 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extracting null pointer address from PyCObject with ctypes

Hello :)

The result of various incompatibiliti es has left me needing to somehow
extract the address that a null pointer is pointing to with the null
pointer being exposed to python via PyCObject_FromV oidPtr

the code that creates the PyCObject is as follows:
tmp = PyCObject_FromV oidPtr (info.info.x11. display, NULL);
PyDict_SetItemS tring (dict, "display", tmp);
Py_DECREF (tmp);

which is exposed to python via a dictionary (the 'display' key). python
identifies that its a PyCObject but doesn't give any way to expose the
functionality. Essentially I am after the address that the void pointer
'info.info.x11. display' points to (as a long type)

As far as I can tell ctypes will only expose the pyObject type to me and
not actually let me deal with the data I am after, but being rather new
to ctypes I'm not sure weather this is correct.

--
Gord Allott (go********@gma il.com)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFI7y2SlVw 7T9GIvRsRArcbAK CjmewtdkEiahPXT HhhCUMy9D7NEgCf YTIQ
dE7a69MTHmR/3WUUSCzoSmc=
=Outo
-----END PGP SIGNATURE-----

Oct 10 '08 #1
14 2967
On Oct 10, 5:24*am, Gordon Allott <gordall...@gma il.comwrote:
Hello :)

The result of various incompatibiliti es has left me needing to somehow
extract the address that a null pointer is pointing to with the null
pointer being exposed to python via PyCObject_FromV oidPtr

the code that creates the PyCObject is as follows:
* * tmp = PyCObject_FromV oidPtr (info.info.x11. display, NULL);
* * PyDict_SetItemS tring (dict, "display", tmp);
* * Py_DECREF (tmp);
Did you try:

tmp= PyLong_FromLong ( ( long ) info.info.x11.d isplay );
PyDict_SetItemS tring (dict, "display", tmp);
Py_DECREF (tmp);

Or also try:

PyCObject_AsVoi dPtr( tmp );
Oct 10 '08 #2
Aaron "Castironpi " Brady wrote:
Did you try:

tmp= PyLong_FromLong ( ( long ) info.info.x11.d isplay );
PyDict_SetItemS tring (dict, "display", tmp);
Py_DECREF (tmp);

Or also try:

PyCObject_AsVoi dPtr( tmp );
--
http://mail.python.org/mailman/listinfo/python-list
the problem is that I can't edit the C code - well I can and might
submit a patch to the project but I also need a solution that works from
the python side of things.
--
Gord Allott (go********@gma il.com)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFI74sQlVw 7T9GIvRsRAtw+AJ 4i0lte6DnzeJ6Nm +BbaYGUCDDEIQCe IW4m
rlzxbYBLYI4j3hF ceifTuLM=
=nfIm
-----END PGP SIGNATURE-----

Oct 10 '08 #3
On Oct 10, 12:04*pm, Gordon Allott <gordall...@gma il.comwrote:
Aaron "Castironpi " Brady wrote:
Did you try:
tmp= PyLong_FromLong ( ( long ) info.info.x11.d isplay );
PyDict_SetItemS tring (dict, "display", tmp);
Py_DECREF (tmp);
Or also try:
PyCObject_AsVoi dPtr( tmp );
--
http://mail.python.org/mailman/listinfo/python-list

the problem is that I can't edit the C code - well I can and might
submit a patch to the project but I also need a solution that works from
the python side of things.
I see. If I understand, you have a PyCObject in a dictionary.

Look at the 'ctypes' module and try calling PyCObject_AsVoi dPtr. Its
return type should be 'c_void_p', and you can use 'result.value' to
get the original pointer.
Oct 10 '08 #4
Aaron "Castironpi " Brady wrote:
I see. If I understand, you have a PyCObject in a dictionary.

Look at the 'ctypes' module and try calling PyCObject_AsVoi dPtr. Its
return type should be 'c_void_p', and you can use 'result.value' to
get the original pointer.
--
http://mail.python.org/mailman/listinfo/python-list
I have a hard time following that, if using ctypes you used PyDLL to
call PyCObject_AsVoi dPtr on the PyCObject I already have surely it would
give me back a pointer (void for sake of simplicity) but it would be a
pointer to a new PyCObject and thus calling result.value on it would
only return the memory address of the new PyCObject?
--
Gord Allott (go********@gma il.com)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFI78YSlVw 7T9GIvRsRAm3eAJ 0c9k7HFhXbEcq3O 3jGYlMsziKIHACf S09u
5QTYKVR/SH6fPqToXhK9WVA =
=uL6v
-----END PGP SIGNATURE-----

Oct 10 '08 #5
On Oct 10, 4:16*pm, Gordon Allott <gordall...@gma il.comwrote:
Aaron "Castironpi " Brady wrote:
I see. *If I understand, you have a PyCObject in a dictionary.
Look at the 'ctypes' module and try calling PyCObject_AsVoi dPtr. *Its
return type should be 'c_void_p', and you can use 'result.value' to
get the original pointer.
--
http://mail.python.org/mailman/listinfo/python-list

I have a hard time following that, if using ctypes you used PyDLL to
call PyCObject_AsVoi dPtr on the PyCObject I already have surely it would
*give me back a pointer (void for sake of simplicity) but it would be a
pointer to a new PyCObject and thus calling result.value on it would
only return the memory address of the new PyCObject?
--
Gord Allott (gordall...@gma il.com)

*signature.asc
< 1KViewDownload

Yes, well said. But no, not true, not necessarily. You can choose/
change return types with your code. If the call is defined already
and you can't change the return, just define a new one that returns
long.
Oct 11 '08 #6
Aaron "Castironpi " Brady wrote:
Yes, well said. But no, not true, not necessarily. You can choose/
change return types with your code. If the call is defined already
and you can't change the return, just define a new one that returns
long.
--
http://mail.python.org/mailman/listinfo/python-list
the problem is that the pointer or long or whatever it is thats returned
won't be the data I am after. the code for PyCObject_FromV oidPtr is as
follows:

PyObject *
PyCObject_FromV oidPtr(void *cobj, void (*destr)(void *))
{
PyCObject *self;

self = PyObject_NEW(Py CObject, &PyCObject_Type );
if (self == NULL)
return NULL;
self->cobject=cobj ;
self->destructor=des tr;
self->desc=NULL;

return (PyObject *)self;
}
it obviously creates a new PyObject and returns that, which has already
happened once (the address I am after is passed to python via
PyCObject_FromV oidPtr(adress_i _am_after, NULL), doing that puts the
address I am after into the .cobject attribute of a new pyobject
structure and passes that to the python script via the 'display' key in
a dictionary.

If I were to then take the pycobject in this display key and pass it via
ctypes into PyCObject_FromV oidPtr it would simply create a new pycobject
and put a pointer to the old pycobject in the new pycobject's .cobject
attribute. it just means that I am getting further and further away from
where I want to be surely? if I were to take the current pointer at this
stage, to get to the address I actually want in C it would have to
follow something along the lines of
long address_i_want = (long)(new_pyco bj->cobject->cobject);

What would be great is if there is some easy simple way of accessing the
.cobject attribute of the first pycobject thats passed via the
dictionary to python.

--
Gord Allott (go********@gma il.com)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFI7/p9lVw7T9GIvRsRA r2rAJ9gxOUnLb8o lXN6siH88ZLMwv2 erwCfWD1q
+Y5jmOl0PrUzLTv mc3nMK/U=
=rL5l
-----END PGP SIGNATURE-----

Oct 11 '08 #7
On Oct 10, 7:59*pm, Gordon Allott <gordall...@gma il.comwrote:
Aaron "Castironpi " Brady wrote:
Yes, well said. *But no, not true, not necessarily. *You can choose/
change return types with your code. *If the call is defined already
and you can't change the return, just define a new one that returns
long.
--
http://mail.python.org/mailman/listinfo/python-list

the problem is that the pointer or long or whatever it is thats returned
won't be the data I am after. the code for PyCObject_FromV oidPtr is as
follows:

PyObject *
PyCObject_FromV oidPtr(void *cobj, void (*destr)(void *))
{
* * PyCObject *self;

* * self = PyObject_NEW(Py CObject, &PyCObject_Type );
* * if (self == NULL)
* * * * return NULL;
* * self->cobject=cobj ;
* * self->destructor=des tr;
* * self->desc=NULL;

* * return (PyObject *)self;

}

it obviously creates a new PyObject and returns that, which has already
happened once (the address I am after is passed to python via
PyCObject_FromV oidPtr(adress_i _am_after, NULL), doing that puts the
address I am after into the .cobject attribute of a new pyobject
structure and passes that to the python script via the 'display' key in
a dictionary.

If I were to then take the pycobject in this display key and pass it via
ctypes into PyCObject_FromV oidPtr it would simply create a new pycobject
*and put a pointer to the old pycobject in the new pycobject's .cobject
attribute. it just means that I am getting further and further away from
where I want to be surely? if I were to take the current pointer at this
stage, to get to the address I actually want in C it would have to
follow something along the lines of
long address_i_want = (long)(new_pyco bj->cobject->cobject);

What would be great is if there is some easy simple way of accessing the
.cobject attribute of the first pycobject thats passed via the
dictionary to python.

--
Gord Allott (gordall...@gma il.com)

*signature.asc
< 1KViewDownload
You are hard to follow. There is the 'cast' function, which I've had
some success with, even in adding pointers and offsets. It took a
look at the code for it though, and calling an undocumented version of
it. I can post that later if you don't have luck the same. You can
write extension modules to do that as well, and there's always a
Google search which personally I forget half the time too. Last, you
haven't mentioned an attempt with PyCObject_AsVoi dPtr yet:

void* PyCObject_AsVoi dPtr(PyObject* self)
Return the object void * that the PyCObject self was created with.

Where does that get you?
Oct 11 '08 #8
Aaron "Castironpi " Brady wrote:

You are hard to follow. There is the 'cast' function, which I've had
some success with, even in adding pointers and offsets. It took a
look at the code for it though, and calling an undocumented version of
it. I can post that later if you don't have luck the same. You can
write extension modules to do that as well, and there's always a
Google search which personally I forget half the time too. Last, you
haven't mentioned an attempt with PyCObject_AsVoi dPtr yet:

void* PyCObject_AsVoi dPtr(PyObject* self)
Return the object void * that the PyCObject self was created with.

Where does that get you?
--
http://mail.python.org/mailman/listinfo/python-list
sorry yes you were right, I was reading PyCObject_AsVoi dPtr as
PyCObject_FromV oidPtr :)

using AsVoidPtr is a little confusing, this is the code I am using:
display = pygame.display. get_wm_info()['display']
pyobj = py_object(displ ay)
ref = pointer(pyobj)

print pythonapi.PyCOb ject_AsVoidPtr( ref)

it produces the following traceback:
Traceback (most recent call last):
File "pygametest.py" , line 125, in <module>
app = PyGameOGREApp()
File "pygametest.py" , line 33, in __init__
self._createWin dow(width, height, fullscreen)
File "pygametest.py" , line 64, in _createWindow
print pythonapi.PyCOb ject_AsVoidPtr( ref)
TypeError: PyCObject_AsVoi dPtr with non-C-object

- I think that's because its a pointer to the ctypes py_object() rather
than the PyCObject we are dealing with but I have no idea how to create
a pointer to that.

--
Gord Allott (go********@gma il.com)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFI8CN1lVw 7T9GIvRsRAuo0AK CSzSQgnnRPanYLS tdBLGGIA669rQCe Jsm7
G/Dnr2uDs8CaZJ988 UnF2kU=
=/HxT
-----END PGP SIGNATURE-----

Oct 11 '08 #9
On Oct 10, 10:54*pm, Gordon Allott <gordall...@gma il.comwrote:
Aaron "Castironpi " Brady wrote:
snip
*Last, you
haven't mentioned an attempt with PyCObject_AsVoi dPtr yet:
void* PyCObject_AsVoi dPtr(PyObject* self)
* * Return the object void * that the PyCObject self was created with.
Where does that get you?
--
http://mail.python.org/mailman/listinfo/python-list

sorry yes you were right, I was reading PyCObject_AsVoi dPtr as
PyCObject_FromV oidPtr :)

using AsVoidPtr is a little confusing, this is the code I am using:
* * * * display = pygame.display. get_wm_info()['display']
* * * * pyobj = py_object(displ ay)
* * * * ref = pointer(pyobj)

* * * * print pythonapi.PyCOb ject_AsVoidPtr( ref)

it produces the following traceback:
Traceback (most recent call last):
* File "pygametest.py" , line 125, in <module>
* * app = PyGameOGREApp()
* File "pygametest.py" , line 33, in __init__
* * self._createWin dow(width, height, fullscreen)
* File "pygametest.py" , line 64, in _createWindow
* * print pythonapi.PyCOb ject_AsVoidPtr( ref)
TypeError: PyCObject_AsVoi dPtr with non-C-object

- I think that's because its a pointer to the ctypes py_object() rather
than the PyCObject we are dealing with but I have no idea how to create
a pointer to that.
My pygame install just returns an integer in get_wm_info. Take a
look:
>>pygame.displa y.get_wm_info()
{'window': 1180066, 'hglrc': 0}
>>pygame.displa y.get_wm_info()['window']
1180066
>>ctypes.c_void _p( _ )
c_void_p(118006 6)

You're suggesting yours looks like this:
>>pygame.displa y.get_wm_info()
{ ... 'display': ctypes.py_objec t( 1180066 ), ... }

What does type( display ) give you?
Oct 11 '08 #10

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

Similar topics

9
2798
by: Chris Jankowski | last post by:
Hi all, I am still new to using Python, so bare with me. I am trying to call a DLL from my program to use some of it's functions. The only parameter is supposed to be a pointer to an image. I have tried several variations on passing this parameter to the DLL and I am not having much luck. This is going to call other functions later for OCR, but I want to get a simple one working first. Here is my code: (Very basic)
3
5531
by: Lenard Lindstrom | last post by:
Posted in a previous thread was some Python code for accessing Window's Simple MAPI api using the ctypes module. http://groups-beta.google.com/group/comp.lang.python/msg/56fa74cdba9b7be9 This Simple MAPI module was Ian's completed version of an example I had posted in an earlier message. In it I had to set some pointer fields in a C structure to NULL. I was not happy with the solution I used so I made an inquiry on the ctypes-users...
102
5950
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
24
7301
by: Daniel Crespo | last post by:
Hi, I tried: import ctypes import socket import struct def get_macaddress(host): """ Returns the MAC address of a network host, requires >= WIN2K. """
12
10096
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv, ctypes.c_void_p).value != None) Yes?
0
2442
by: kpoman | last post by:
Hi to all, I am trying to use some dll which needs some data types that I can't find in python. From the dll documentation, I am trying to use this: HRESULT IMKWsq::Compress ( VARIANT rawImage, short sizeX, short sizeY, VARIANT * wsqImage, short * result )
6
12783
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how I should declare the return type of the functions and read the fields. Any hint is appreciated. typedef struct { char *country_short; char *country_long;
4
8469
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value) err = fcntl.ioctl(eos_fd, request, args) Now to do the same with ctypes, I have one problem.
2
16492
by: Jean-Paul Calderone | last post by:
On Mon, 30 Jun 2008 09:13:42 -0700 (PDT), gianluca <geonomica@gmail.comwrote: POINTER takes a class and returns a new class which represents a pointer to input class. pointer takes an instance and returns a new object which represents a pointer to that instance. Jean-Paul
0
8425
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8418
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.