[IPython-dev] Cython based PyOS_InputHook hack

Brian Granger ellisonbg.net at gmail.com
Sat Feb 7 17:42:12 EST 2009


I have figured out how to call PyOS_InputHook from Cython code.  It is
pretty simple.  If you have the latest Cython, just drop the attached
files in a directory, open up IPython and do the commands listed

My hack doesn't do anything with a GUI - it simply shows 1) what
exactly PyOS_InputHook does and 2) Shows how to call it from Cython.

I think the only reason we need to think about this is to get wx
working in the PyOS_InputHook mode.  Ville thought that because wx is
based on gtk on Linux we don't need to worry about this.  But, that
leaves Win32 and OS X out in the dark.  Also, I think the
PyOS_InputHook logic is in the Python bindings (pygtk), which I don't
think wx uses.

So, in order for this to really work, someone who knows wx well
(Gael?, someone at Enthought?) has to create a real PyOS_InputHook
that does the right things for wx.   The basic idea is that the hook
needs to watch for stdin to be active and then run the event loop.
The best place to look for ideas is:

* In the pyqt source code - just search for PyOS_InputHook
* In the _tkinter.c source code of Python itself.

Here is what you type to run my example

In [1]: import pyximport     # This Cython package compiles
inputhook.pyx on the fly!!!

In [2]: pyximport.install()

In [3]: import inputhook

In [4]: inputhook.count
Out[4]: 0

In [5]: inputhook.set_input_hook()

In [6]: inputhook.count
Out[6]: 27

In [7]: inputhook.count
Out[7]: 37

In [8]: inputhook.count
Out[8]: 47

In [9]: inputhook.count
Out[9]: 70

In [10]: inputhook.clear_input_hook()

In [11]: inputhook.count
Out[11]: 124

In [12]: inputhook.count
Out[12]: 124
-------------- next part --------------
#include <Python.h>

static void set_input_hook_c(int (*f)(void))
    PyOS_InputHook = f;

static void clear_input_hook_c(void)
    PyOS_InputHook = NULL;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: inputhook.pyx
Type: application/octet-stream
Size: 723 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20090207/d9bfc691/attachment.obj>

More information about the IPython-dev mailing list