[ python-Bugs-1574588 ] ctypes: Pointer-to-pointer unchanged in callback

SourceForge.net noreply at sourceforge.net
Fri Jan 26 12:02:35 CET 2007


Bugs item #1574588, was opened at 2006-10-10 17:16
Message generated for change (Comment added) made by theller
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1574588&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
>Status: Closed
>Resolution: Invalid
Priority: 5
Private: No
Submitted By: Albert Strasheim (albertstrasheim)
Assigned to: Thomas Heller (theller)
Summary: ctypes: Pointer-to-pointer unchanged in callback

Initial Comment:
This problem is from another post I made to
ctypes-users that didn't show up in the ctypes-users
archive.

C function:

extern CALLBACK_API void foo(void(*callback)(void**)) {
    void* p = 123;
    printf("foo calling callback\n");
    callback(&p);
    printf("callback returned in foo\n");
    printf("p = 0x%p\n", p);
}

I figured that while I try to find out why returning
c_void_p from a callback gives an error, I might as
well return the address via a pointer to a pointer.

In the Python code I have:

import sys
print sys.version
from ctypes import *
x_t = c_int*10
x = x_t()
def callback(ptr):
    print x
    print ptr
    ptr.contents = cast(addressof(x), c_void_p)
    print ptr.contents
#lib = cdll['libcallback.so']
lib = cdll['callback.dll']
lib.foo.argtypes = [CFUNCTYPE(None, POINTER(c_void_p))]
lib.foo(lib.foo.argtypes[0](callback))

Output when I running this script under Python 2.4.3
with ctypes 1.0.0 (I get identical results with Python
2.5 and ctypes 1.0.1):

2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit
(Intel)]
foo calling callback
<__main__.c_long_Array_10 object at 0x00963E90>
<ctypes.LP_c_void_p object at 0x00963EE0>
c_void_p(10048496)
callback returned in foo
p = 0x0000007B

For some reason, the value I assigned to ptr.contents
isn't present when we return to the C code.

----------------------------------------------------------------------

>Comment By: Thomas Heller (theller)
Date: 2007-01-26 12:02

Message:
Logged In: YES 
user_id=11105
Originator: NO

Sorry for the late reply.

This is not a bug.  To dereference a pointer in ctypes you should index
with 0:
  print ptr[0]
  ptr[0] = <somethingelse>
When you replace 'ptr.contents' with 'ptr[0]' in your code then it works
as expected.
In C, these two idioms are identical:

   ptr[0]
   *ptr


The sematics of ptr.contents is different although somewhat difficult to
explain.
Changing ptr.contents does not change the value that the pointer points
to, instead
it changes the location that the pointer points to.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1574588&group_id=5470


More information about the Python-bugs-list mailing list