472,811 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

ctypes: Setting callback functions in C libraries

Hey,

I'm trying to wrap GNU readline with ctypes (the Python readline
library doesn't support the callback interface), but I can't figure out
how to set values to a variable inside the library. This is Python 2.5
on Linux. Here's what I have so far--if you comment out the memmove
call (3 lines) it works as expected:

# START
#!/usr/local/bin/python2.5
import ctypes

ctypes.cdll.LoadLibrary("libcurses.so")#, mode=ctypes.RTLD_GLOBAL)
ctypes.CDLL("libcurses.so", mode=ctypes.RTLD_GLOBAL)
ctypes.cdll.LoadLibrary("libreadline.so")
readline = ctypes.CDLL("libreadline.so")

RL_COMPLETION_FUNC_T =
ctypes.CFUNCTYPE(ctypes.POINTER(ctypes.c_char_p),
ctypes.c_char_p, ctypes.c_int, ctypes.c_int)
RL_LINE_BUFFER = ctypes.c_char_p.in_dll(readline, "rl_line_buffer")
RL_ATTEMPTED_COMPLETION_FUNCTION =
RL_COMPLETION_FUNC_T.in_dll(readline,

"rl_attempted_completion_function")

def our_complete(text, start, end):
print "Test", text, start, end
rv = None
arrtype = ctypes.c_char_p*4
globals()["rv"] = arrtype("hello", "hatpin", "hammer", None)
globals()["crv"]=ctypes.cast(rv, ctypes.POINTER(ctypes.c_char_p))
return rv
ourfunc = RL_COMPLETION_FUNC_T(our_complete)

_readline = readline.readline
_readline.argtypes = [ctypes.c_char_p]
_readline.restype = ctypes.c_char_p

ctypes.memmove(ctypes.addressof(RL_ATTEMPTED_COMPL ETION_FUNCTION),
ctypes.addressof(ourfunc),
4)
line = _readline("Input: ")
print "INPUT was: ", line
#END

I need to assign ourfunc to RL_ATTEMPTED_COMPLETION_FUNCTION, but
obviously simple assignment rebinds the name rather than assigning it
to the C variable.

Using ctypes.memmove to overwrite it(as I have here) will run the
function but segfault when ourfunc goes to return (poking around with
the debugger shows it's dying in ctypes' callbacks.c at line 216, "keep
= setfunc(mem, result, 0);" because setfunc is NULL). I'm not entirely
sure that's not because of some error in the prototyping or restype
setting rather than as a result of memmove, but the memmove seems
sketchy enough (only sets the function pointer itself presumably, not
anything that ctypes wrappers need) that I'm looking for a better way
to do it.

As it is, returning rv directly or simply returning None causes the
same segfault.
Any ideas?

Thanks very much for your time!

Jan 25 '07 #1
1 4635
I realized my wrapping was broken, fixing that below...

On Jan 25, 12:47 am, "sjdevn...@yahoo.com" <sjdevn...@yahoo.com>
wrote:
I'm trying to wrap GNU readline withctypes(the Python readline
library doesn't support the callback interface), but I can't figure out
how to set values to a variable inside the library. This is Python 2.5
on Linux. Here's what I have so far--if you comment out the memmove
call (3 lines) it works as expected:
[SNIP]
I need to assign ourfunc to RL_ATTEMPTED_COMPLETION_FUNCTION, but
obviously simple assignment rebinds the name rather than assigning it
to the C variable.

Usingctypes.memmove to overwrite it(as I have here) will run the
function but segfault when ourfunc goes to return (poking around with
the debugger shows it's dying inctypes' callbacks.c at line 216, "keep
= setfunc(mem, result, 0);" because setfunc is NULL). I'm not entirely
sure that's not because of some error in the prototyping or restype
setting rather than as a result of memmove, but the memmove seems
sketchy enough (only sets the function pointer itself presumably, not
anything thatctypeswrappers need) that I'm looking for a better way
to do it.

As it is, returning rv directly or simply returning None causes the
same segfault.

Any idea?

Thanks very much for your time!
# START
#!/usr/local/bin/python2.5
import ctypes

ctypes.cdll.LoadLibrary("libcurses.so")#, mode=ctypes.RTLD_GLOBAL)
ctypes.CDLL("libcurses.so", mode=ctypes.RTLD_GLOBAL)
ctypes.cdll.LoadLibrary("libreadline.so")
readline = ctypes.CDLL("libreadline.so")

RL_COMPLETION_FUNC_T = \
ctypes.CFUNCTYPE(ctypes.POINTER(ctypes.c_char_p),
ctypes.c_char_p, ctypes.c_int, ctypes.c_int)
RL_LINE_BUFFER = ctypes.c_char_p.in_dll(readline, "rl_line_buffer")
RL_ATTEMPTED_COMPLETION_FUNCTION = \
RL_COMPLETION_FUNC_T.in_dll(readline,
"rl_attempted_completion_function")

def our_complete(text, start, end):
print "Test", text, start, end
rv = None
arrtype = ctypes.c_char_p*4
globals()["rv"] = arrtype("hello", "hatpin", "hammer", None)
globals()["crv"]=ctypes.cast(rv, ctypes.POINTER(ctypes.c_char_p))
return rv
ourfunc = RL_COMPLETION_FUNC_T(our_complete)

_readline = readline.readline
_readline.argtypes = [ctypes.c_char_p]
_readline.restype = ctypes.c_char_p

ctypes.memmove(ctypes.addressof(RL_ATTEMPTED_COMPL ETION_FUNCTION),
ctypes.addressof(ourfunc),
4)
line = _readline("Input: ")
print "INPUT was: ", line
#END

Jan 29 '07 #2

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

Similar topics

8
by: achrist | last post by:
I'm aving some trouble getting callbacks from a Delphi DLL back to python working through ctypes. The call from python to the DLL works fine. In the Delphi(5) code: type TCallbackFunc =...
2
by: Thomas Heller | last post by:
It's release day ;-) ctypes 0.6.3 released ===================== Overview 'ctypes' is a Python package to create and manipulate C data types in Python, and to call functions in dynamic...
1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
19
by: Thomas Heller | last post by:
ctypes 0.9.2 released - Oct 28, 2004 ==================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
3
by: James Hu | last post by:
Hi, gurus, I would like to use ctypes to implement callback function for QImage Camera to capture image asynchronously, and I have the c++ code of callback, but I am totally in the dark, the...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
1
by: m2i3k | last post by:
Hi I have C code with requires me to register a python callback. I am able to get the callback working well using ctypes if I use global functions without any context argument for the callback. ...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance...
3
by: waldek | last post by:
Hi, I have module A.py and B.dll which exports C functions by cdecl_ In A.py I pass callback (py callable) to dll. Next, thread inside dll simply calls my callback (in a loop). After few secs...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.