[Python-Dev] Suggested API change to PyOS_InputHook

Michael Abbott michael at araneidae.co.uk
Mon Jun 23 09:02:52 CEST 2008


I have a problem with the PyOS_InputHook() API as implemented by 
Modules/readline.c: there is no way to communicate any interruption seen 
while waiting at the keyboard back to the process actually reading the 
line.
I have solved this in my own application by creating a new module which 
reimplements the call_readline function and which has the following patch 
to readline_until_enter_or_signal():
 FD_SET(fileno(rl_instream), &selectset);
 /* select resets selectset if no input was available */
 has_input = select(fileno(rl_instream) + 1, &selectset,
 NULL, NULL, &timeout);
- if(PyOS_InputHook) PyOS_InputHook();
+ if (call_readline_InputHook &&
+ call_readline_InputHook(fileno(rl_instream)))
+ {
+ has_input = -1;
+ PyErr_SetInterrupt();
+ break;
+ }
 }
 if(has_input > 0) {
 rl_callback_read_char();
 }
- else if (errno == EINTR) {
+ else if (errno == EINTR || has_input < 0) {
 int s;
 #ifdef WITH_THREAD
 PyEval_RestoreThread(_PyOS_ReadlineTState);
(I'm sorry, this isn't a proper patch, but hopefully if you've read this 
far you can figure out where it applies.) 
Here I've replaced PyOS_InputHook with effective type signature None->None 
with my own call_readline_InputHook which takes a file descriptor and 
returns a boolean, returning True iff an interrupt occurred. The file 
descriptor argument is a triviality, but the boolean return code is 
clearly crucial.
Now, I don't know how this change might fit into mainstream Python, but I 
think there is a case for making this change. If the incompatibility was 
acceptable it would be better to change the signature of PyOS_InputHook (I 
declared my own variable call_readline_InputHook just to avoid accidents). 
I've checked the current svn trunk, and this change remains applicable.
Please CC me in any replies: I am quite literally unable to cope with the 
volume of traffic that a subscription to python-dev will give me.


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /