1 /* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
3 * Center. The completer interface was inspired by Lele Gaifax. More
4 * recently, it was largely rewritten by Guido van Rossum.
7 /* Standard definitions */
14 #if defined(HAVE_SETLOCALE)
15 /* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
24 # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
26 # define RESTORE_LOCALE(sl)
29 /* GNU readline definitions */
30 #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
31 #include <readline/readline.h>
32 #include <readline/history.h>
34 #ifdef HAVE_RL_COMPLETION_MATCHES
35 #define completion_matches(x, y) \
36 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
38 #if defined(_RL_FUNCTION_TYPEDEF)
39 extern char **completion_matches(char *, rl_compentry_func_t
*);
42 #if !defined(__APPLE__)
43 extern char **completion_matches(char *, CPFunction
*);
50 * It is possible to link the readline module to the readline
51 * emulation library of editline/libedit.
53 * On OSX this emulation library is not 100% API compatible
54 * with the "real" readline and cannot be detected at compile-time,
55 * hence we use a runtime check to detect if we're using libedit
57 * Currently there is one know API incompatibility:
58 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
59 * index with libedit's emulation.
60 * - Note that replace_history and remove_history use a 0-based index
61 * with both implementation.
63 static int using_libedit_emulation
= 0;
64 static const char libedit_version_tag
[] = "EditLine wrapper";
65 #endif /* __APPLE__ */
68 on_completion_display_matches_hook(char **matches
,
69 int num_matches
, int max_length
);
72 /* Exported function to send one line to readline's init file parser */
75 parse_and_bind(PyObject
*self
, PyObject
*args
)
78 if (!PyArg_ParseTuple(args
, "s:parse_and_bind", &s
))
80 /* Make a copy -- rl_parse_and_bind() modifies its argument */
82 copy
= malloc(1 + strlen(s
));
84 return PyErr_NoMemory();
86 rl_parse_and_bind(copy
);
87 free(copy
); /* Free the copy */
91 PyDoc_STRVAR(doc_parse_and_bind
,
92 "parse_and_bind(string) -> None\n\
93 Parse and execute single line of a readline init file.");
96 /* Exported function to parse a readline init file */
99 read_init_file(PyObject
*self
, PyObject
*args
)
102 if (!PyArg_ParseTuple(args
, "|z:read_init_file", &s
))
104 errno
= rl_read_init_file(s
);
106 return PyErr_SetFromErrno(PyExc_IOError
);
110 PyDoc_STRVAR(doc_read_init_file
,
111 "read_init_file([filename]) -> None\n\
112 Parse a readline initialization file.\n\
113 The default filename is the last filename used.");
116 /* Exported function to load a readline history file */
119 read_history_file(PyObject
*self
, PyObject
*args
)
122 if (!PyArg_ParseTuple(args
, "|z:read_history_file", &s
))
124 errno
= read_history(s
);
126 return PyErr_SetFromErrno(PyExc_IOError
);
130 static int _history_length
= -1; /* do not truncate history by default */
131 PyDoc_STRVAR(doc_read_history_file
,
132 "read_history_file([filename]) -> None\n\
133 Load a readline history file.\n\
134 The default filename is ~/.history.");
137 /* Exported function to save a readline history file */
140 write_history_file(PyObject
*self
, PyObject
*args
)
143 if (!PyArg_ParseTuple(args
, "|z:write_history_file", &s
))
145 errno
= write_history(s
);
146 if (!errno
&& _history_length
>= 0)
147 history_truncate_file(s
, _history_length
);
149 return PyErr_SetFromErrno(PyExc_IOError
);
153 PyDoc_STRVAR(doc_write_history_file
,
154 "write_history_file([filename]) -> None\n\
155 Save a readline history file.\n\
156 The default filename is ~/.history.");
159 /* Set history length */
162 set_history_length(PyObject
*self
, PyObject
*args
)
164 int length
= _history_length
;
165 if (!PyArg_ParseTuple(args
, "i:set_history_length", &length
))
167 _history_length
= length
;
171 PyDoc_STRVAR(set_history_length_doc
,
172 "set_history_length(length) -> None\n\
173 set the maximal number of items which will be written to\n\
174 the history file. A negative length is used to inhibit\n\
175 history truncation.");
178 /* Get history length */
181 get_history_length(PyObject
*self
, PyObject
*noarg
)
183 return PyInt_FromLong(_history_length
);
186 PyDoc_STRVAR(get_history_length_doc
,
187 "get_history_length() -> int\n\
188 return the maximum number of items that will be written to\n\
192 /* Generic hook function setter */
195 set_hook(const char *funcname
, PyObject
**hook_var
, PyObject
*args
)
197 PyObject
*function
= Py_None
;
199 PyOS_snprintf(buf
, sizeof(buf
), "|O:set_%.50s", funcname
);
200 if (!PyArg_ParseTuple(args
, buf
, &function
))
202 if (function
== Py_None
) {
203 Py_XDECREF(*hook_var
);
206 else if (PyCallable_Check(function
)) {
207 PyObject
*tmp
= *hook_var
;
209 *hook_var
= function
;
213 PyOS_snprintf(buf
, sizeof(buf
),
214 "set_%.50s(func): argument not callable",
216 PyErr_SetString(PyExc_TypeError
, buf
);
223 /* Exported functions to specify hook functions in Python */
225 static PyObject
*completion_display_matches_hook
= NULL
;
226 static PyObject
*startup_hook
= NULL
;
228 #ifdef HAVE_RL_PRE_INPUT_HOOK
229 static PyObject
*pre_input_hook
= NULL
;
233 set_completion_display_matches_hook(PyObject
*self
, PyObject
*args
)
235 PyObject
*result
= set_hook("completion_display_matches_hook",
236 &completion_display_matches_hook
, args
);
237 #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
238 /* We cannot set this hook globally, since it replaces the
239 default completion display. */
240 rl_completion_display_matches_hook
=
241 completion_display_matches_hook
?
242 #if defined(_RL_FUNCTION_TYPEDEF)
243 (rl_compdisp_func_t
*)on_completion_display_matches_hook
: 0;
245 (VFunction
*)on_completion_display_matches_hook
: 0;
252 PyDoc_STRVAR(doc_set_completion_display_matches_hook
,
253 "set_completion_display_matches_hook([function]) -> None\n\
254 Set or remove the completion display function.\n\
255 The function is called as\n\
256 function(substitution, [matches], longest_match_length)\n\
257 once each time matches need to be displayed.");
260 set_startup_hook(PyObject
*self
, PyObject
*args
)
262 return set_hook("startup_hook", &startup_hook
, args
);
265 PyDoc_STRVAR(doc_set_startup_hook
,
266 "set_startup_hook([function]) -> None\n\
267 Set or remove the startup_hook function.\n\
268 The function is called with no arguments just\n\
269 before readline prints the first prompt.");
272 #ifdef HAVE_RL_PRE_INPUT_HOOK
274 /* Set pre-input hook */
277 set_pre_input_hook(PyObject
*self
, PyObject
*args
)
279 return set_hook("pre_input_hook", &pre_input_hook
, args
);
282 PyDoc_STRVAR(doc_set_pre_input_hook
,
283 "set_pre_input_hook([function]) -> None\n\
284 Set or remove the pre_input_hook function.\n\
285 The function is called with no arguments after the first prompt\n\
286 has been printed and just before readline starts reading input\n\
292 /* Exported function to specify a word completer in Python */
294 static PyObject
*completer
= NULL
;
296 static PyObject
*begidx
= NULL
;
297 static PyObject
*endidx
= NULL
;
300 /* Get the completion type for the scope of the tab-completion */
302 get_completion_type(PyObject
*self
, PyObject
*noarg
)
304 return PyInt_FromLong(rl_completion_type
);
307 PyDoc_STRVAR(doc_get_completion_type
,
308 "get_completion_type() -> int\n\
309 Get the type of completion being attempted.");
312 /* Get the beginning index for the scope of the tab-completion */
315 get_begidx(PyObject
*self
, PyObject
*noarg
)
321 PyDoc_STRVAR(doc_get_begidx
,
322 "get_begidx() -> int\n\
323 get the beginning index of the readline tab-completion scope");
326 /* Get the ending index for the scope of the tab-completion */
329 get_endidx(PyObject
*self
, PyObject
*noarg
)
335 PyDoc_STRVAR(doc_get_endidx
,
336 "get_endidx() -> int\n\
337 get the ending index of the readline tab-completion scope");
340 /* Set the tab-completion word-delimiters that readline uses */
343 set_completer_delims(PyObject
*self
, PyObject
*args
)
347 if(!PyArg_ParseTuple(args
, "s:set_completer_delims", &break_chars
)) {
350 free((void*)rl_completer_word_break_characters
);
351 rl_completer_word_break_characters
= strdup(break_chars
);
355 PyDoc_STRVAR(doc_set_completer_delims
,
356 "set_completer_delims(string) -> None\n\
357 set the readline word delimiters for tab-completion");
359 /* _py_free_history_entry: Utility function to free a history entry. */
361 #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
363 /* Readline version >= 5.0 introduced a timestamp field into the history entry
364 structure; this needs to be freed to avoid a memory leak. This version of
365 readline also introduced the handy 'free_history_entry' function, which
366 takes care of the timestamp. */
369 _py_free_history_entry(HIST_ENTRY
*entry
)
371 histdata_t data
= free_history_entry(entry
);
377 /* No free_history_entry function; free everything manually. */
380 _py_free_history_entry(HIST_ENTRY
*entry
)
383 free((void *)entry
->line
);
392 py_remove_history(PyObject
*self
, PyObject
*args
)
397 if (!PyArg_ParseTuple(args
, "i:remove_history", &entry_number
))
399 if (entry_number
< 0) {
400 PyErr_SetString(PyExc_ValueError
,
401 "History index cannot be negative");
404 entry
= remove_history(entry_number
);
406 PyErr_Format(PyExc_ValueError
,
407 "No history item at position %d",
411 /* free memory allocated for the history entry */
412 _py_free_history_entry(entry
);
416 PyDoc_STRVAR(doc_remove_history
,
417 "remove_history_item(pos) -> None\n\
418 remove history item given by its position");
421 py_replace_history(PyObject
*self
, PyObject
*args
)
425 HIST_ENTRY
*old_entry
;
427 if (!PyArg_ParseTuple(args
, "is:replace_history", &entry_number
,
431 if (entry_number
< 0) {
432 PyErr_SetString(PyExc_ValueError
,
433 "History index cannot be negative");
436 old_entry
= replace_history_entry(entry_number
, line
, (void *)NULL
);
438 PyErr_Format(PyExc_ValueError
,
439 "No history item at position %d",
443 /* free memory allocated for the old history entry */
444 _py_free_history_entry(old_entry
);
448 PyDoc_STRVAR(doc_replace_history
,
449 "replace_history_item(pos, line) -> None\n\
450 replaces history item given by its position with contents of line");
452 /* Add a line to the history buffer */
455 py_add_history(PyObject
*self
, PyObject
*args
)
459 if(!PyArg_ParseTuple(args
, "s:add_history", &line
)) {
466 PyDoc_STRVAR(doc_add_history
,
467 "add_history(string) -> None\n\
468 add a line to the history buffer");
471 /* Get the tab-completion word-delimiters that readline uses */
474 get_completer_delims(PyObject
*self
, PyObject
*noarg
)
476 return PyString_FromString(rl_completer_word_break_characters
);
479 PyDoc_STRVAR(doc_get_completer_delims
,
480 "get_completer_delims() -> string\n\
481 get the readline word delimiters for tab-completion");
484 /* Set the completer function */
487 set_completer(PyObject
*self
, PyObject
*args
)
489 return set_hook("completer", &completer
, args
);
492 PyDoc_STRVAR(doc_set_completer
,
493 "set_completer([function]) -> None\n\
494 Set or remove the completer function.\n\
495 The function is called as function(text, state),\n\
496 for state in 0, 1, 2, ..., until it returns a non-string.\n\
497 It should return the next possible completion starting with 'text'.");
501 get_completer(PyObject
*self
, PyObject
*noargs
)
503 if (completer
== NULL
) {
506 Py_INCREF(completer
);
510 PyDoc_STRVAR(doc_get_completer
,
511 "get_completer() -> function\n\
513 Returns current completer function.");
515 /* Private function to get current length of history. XXX It may be
516 * possible to replace this with a direct use of history_length instead,
517 * but it's not clear whether BSD's libedit keeps history_length up to date.
521 _py_get_history_length(void)
523 HISTORY_STATE
*hist_st
= history_get_history_state();
524 int length
= hist_st
->length
;
525 /* the history docs don't say so, but the address of hist_st changes each
526 time history_get_history_state is called which makes me think it's
527 freshly malloc'd memory... on the other hand, the address of the last
528 line stays the same as long as history isn't extended, so it appears to
529 be malloc'd but managed by the history package... */
534 /* Exported function to get any element of history */
537 get_history_item(PyObject
*self
, PyObject
*args
)
540 HIST_ENTRY
*hist_ent
;
542 if (!PyArg_ParseTuple(args
, "i:index", &idx
))
545 if (using_libedit_emulation
) {
546 /* Libedit emulation uses 0-based indexes,
547 * the real one uses 1-based indexes,
548 * adjust the index to ensure that Python
549 * code doesn't have to worry about the
552 int length
= _py_get_history_length();
556 * Apple's readline emulation crashes when
557 * the index is out of range, therefore
558 * test for that and fail gracefully.
560 if (idx
< 0 || idx
>= length
) {
564 #endif /* __APPLE__ */
565 if ((hist_ent
= history_get(idx
)))
566 return PyString_FromString(hist_ent
->line
);
572 PyDoc_STRVAR(doc_get_history_item
,
573 "get_history_item() -> string\n\
574 return the current contents of history item at index.");
577 /* Exported function to get current length of history */
580 get_current_history_length(PyObject
*self
, PyObject
*noarg
)
582 return PyInt_FromLong((long)_py_get_history_length());
585 PyDoc_STRVAR(doc_get_current_history_length
,
586 "get_current_history_length() -> integer\n\
587 return the current (not the maximum) length of history.");
590 /* Exported function to read the current line buffer */
593 get_line_buffer(PyObject
*self
, PyObject
*noarg
)
595 return PyString_FromString(rl_line_buffer
);
598 PyDoc_STRVAR(doc_get_line_buffer
,
599 "get_line_buffer() -> string\n\
600 return the current contents of the line buffer.");
603 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
605 /* Exported function to clear the current history */
608 py_clear_history(PyObject
*self
, PyObject
*noarg
)
614 PyDoc_STRVAR(doc_clear_history
,
615 "clear_history() -> None\n\
616 Clear the current readline history.");
620 /* Exported function to insert text into the line buffer */
623 insert_text(PyObject
*self
, PyObject
*args
)
626 if (!PyArg_ParseTuple(args
, "s:insert_text", &s
))
632 PyDoc_STRVAR(doc_insert_text
,
633 "insert_text(string) -> None\n\
634 Insert text into the command line.");
637 /* Redisplay the line buffer */
640 redisplay(PyObject
*self
, PyObject
*noarg
)
646 PyDoc_STRVAR(doc_redisplay
,
647 "redisplay() -> None\n\
648 Change what's displayed on the screen to reflect the current\n\
649 contents of the line buffer.");
652 /* Table of functions exported by the module */
654 static struct PyMethodDef readline_methods
[] =
656 {"parse_and_bind", parse_and_bind
, METH_VARARGS
, doc_parse_and_bind
},
657 {"get_line_buffer", get_line_buffer
, METH_NOARGS
, doc_get_line_buffer
},
658 {"insert_text", insert_text
, METH_VARARGS
, doc_insert_text
},
659 {"redisplay", redisplay
, METH_NOARGS
, doc_redisplay
},
660 {"read_init_file", read_init_file
, METH_VARARGS
, doc_read_init_file
},
661 {"read_history_file", read_history_file
,
662 METH_VARARGS
, doc_read_history_file
},
663 {"write_history_file", write_history_file
,
664 METH_VARARGS
, doc_write_history_file
},
665 {"get_history_item", get_history_item
,
666 METH_VARARGS
, doc_get_history_item
},
667 {"get_current_history_length", (PyCFunction
)get_current_history_length
,
668 METH_NOARGS
, doc_get_current_history_length
},
669 {"set_history_length", set_history_length
,
670 METH_VARARGS
, set_history_length_doc
},
671 {"get_history_length", get_history_length
,
672 METH_NOARGS
, get_history_length_doc
},
673 {"set_completer", set_completer
, METH_VARARGS
, doc_set_completer
},
674 {"get_completer", get_completer
, METH_NOARGS
, doc_get_completer
},
675 {"get_completion_type", get_completion_type
,
676 METH_NOARGS
, doc_get_completion_type
},
677 {"get_begidx", get_begidx
, METH_NOARGS
, doc_get_begidx
},
678 {"get_endidx", get_endidx
, METH_NOARGS
, doc_get_endidx
},
680 {"set_completer_delims", set_completer_delims
,
681 METH_VARARGS
, doc_set_completer_delims
},
682 {"add_history", py_add_history
, METH_VARARGS
, doc_add_history
},
683 {"remove_history_item", py_remove_history
, METH_VARARGS
, doc_remove_history
},
684 {"replace_history_item", py_replace_history
, METH_VARARGS
, doc_replace_history
},
685 {"get_completer_delims", get_completer_delims
,
686 METH_NOARGS
, doc_get_completer_delims
},
688 {"set_completion_display_matches_hook", set_completion_display_matches_hook
,
689 METH_VARARGS
, doc_set_completion_display_matches_hook
},
690 {"set_startup_hook", set_startup_hook
,
691 METH_VARARGS
, doc_set_startup_hook
},
692 #ifdef HAVE_RL_PRE_INPUT_HOOK
693 {"set_pre_input_hook", set_pre_input_hook
,
694 METH_VARARGS
, doc_set_pre_input_hook
},
696 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
697 {"clear_history", py_clear_history
, METH_NOARGS
, doc_clear_history
},
703 /* C function to call the Python hooks. */
706 on_hook(PyObject
*func
)
712 PyGILState_STATE gilstate
= PyGILState_Ensure();
714 r
= PyObject_CallFunction(func
, NULL
);
720 result
= PyInt_AsLong(r
);
721 if (result
== -1 && PyErr_Occurred())
731 PyGILState_Release(gilstate
);
739 on_startup_hook(void)
741 return on_hook(startup_hook
);
744 #ifdef HAVE_RL_PRE_INPUT_HOOK
746 on_pre_input_hook(void)
748 return on_hook(pre_input_hook
);
753 /* C function to call the Python completion_display_matches */
756 on_completion_display_matches_hook(char **matches
,
757 int num_matches
, int max_length
)
760 PyObject
*m
=NULL
, *s
=NULL
, *r
=NULL
;
762 PyGILState_STATE gilstate
= PyGILState_Ensure();
764 m
= PyList_New(num_matches
);
767 for (i
= 0; i
< num_matches
; i
++) {
768 s
= PyString_FromString(matches
[i
+1]);
771 if (PyList_SetItem(m
, i
, s
) == -1)
775 r
= PyObject_CallFunction(completion_display_matches_hook
,
776 "sOi", matches
[0], m
, max_length
);
778 Py_DECREF(m
); m
=NULL
;
781 (r
!= Py_None
&& PyInt_AsLong(r
) == -1 && PyErr_Occurred())) {
784 Py_XDECREF(r
); r
=NULL
;
793 PyGILState_Release(gilstate
);
798 /* C function to call the Python completer. */
801 on_completion(const char *text
, int state
)
804 if (completer
!= NULL
) {
807 PyGILState_STATE gilstate
= PyGILState_Ensure();
809 rl_attempted_completion_over
= 1;
810 r
= PyObject_CallFunction(completer
, "si", text
, state
);
817 char *s
= PyString_AsString(r
);
829 PyGILState_Release(gilstate
);
837 /* A more flexible constructor that saves the "begidx" and "endidx"
838 * before calling the normal completer */
841 flex_complete(char *text
, int start
, int end
)
843 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
844 rl_completion_append_character
='\0';
846 #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
847 rl_completion_suppress_append
= 0;
851 begidx
= PyInt_FromLong((long) start
);
852 endidx
= PyInt_FromLong((long) end
);
853 return completion_matches(text
, *on_completion
);
857 /* Helper to initialize GNU readline properly. */
863 char *saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
865 Py_FatalError("not enough memory to save locale");
869 /* the libedit readline emulation resets key bindings etc
870 * when calling rl_initialize. So call it upfront
872 if (using_libedit_emulation
)
874 #endif /* __APPLE__ */
878 rl_readline_name
= "python";
879 #if defined(PYOS_OS2) && defined(PYCC_GCC)
880 /* Allow $if term= in .inputrc to work */
881 rl_terminal_name
= getenv("TERM");
883 /* Force rebind of TAB to insert-tab */
884 rl_bind_key('\t', rl_insert
);
885 /* Bind both ESC-TAB and ESC-ESC to the completion function */
886 rl_bind_key_in_map ('\t', rl_complete
, emacs_meta_keymap
);
887 rl_bind_key_in_map ('\033', rl_complete
, emacs_meta_keymap
);
888 /* Set our hook functions */
889 rl_startup_hook
= (Function
*)on_startup_hook
;
890 #ifdef HAVE_RL_PRE_INPUT_HOOK
891 rl_pre_input_hook
= (Function
*)on_pre_input_hook
;
893 /* Set our completion function */
894 rl_attempted_completion_function
= (CPPFunction
*)flex_complete
;
895 /* Set Python word break characters */
896 rl_completer_word_break_characters
=
897 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
898 /* All nonalphanums except '.' */
900 begidx
= PyInt_FromLong(0L);
901 endidx
= PyInt_FromLong(0L);
902 /* Initialize (allows .inputrc to override)
904 * XXX: A bug in the readline-2.2 library causes a memory leak
905 * inside this function. Nothing we can do about it.
908 if (using_libedit_emulation
)
909 rl_read_init_file(NULL
);
911 #endif /* __APPLE__ */
914 RESTORE_LOCALE(saved_locale
)
917 /* Wrapper around GNU readline that handles signals differently. */
920 #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
922 static char *completed_input_string
;
924 rlhandler(char *text
)
926 completed_input_string
= text
;
927 rl_callback_handler_remove();
930 extern PyThreadState
* _PyOS_ReadlineTState
;
933 readline_until_enter_or_signal(char *prompt
, int *signal
)
935 char * not_done_reading
= "";
939 #ifdef HAVE_RL_CATCH_SIGNAL
940 rl_catch_signals
= 0;
943 rl_callback_handler_install (prompt
, rlhandler
);
946 completed_input_string
= not_done_reading
;
948 while (completed_input_string
== not_done_reading
) {
952 { struct timeval timeout
= {0, 100000}; /* 0.1 seconds */
954 /* [Bug #1552726] Only limit the pause if an input hook has been
956 struct timeval
*timeoutp
= NULL
;
959 FD_SET(fileno(rl_instream
), &selectset
);
960 /* select resets selectset if no input was available */
961 has_input
= select(fileno(rl_instream
) + 1, &selectset
,
962 NULL
, NULL
, timeoutp
);
963 if(PyOS_InputHook
) PyOS_InputHook();
967 rl_callback_read_char();
969 else if (errno
== EINTR
) {
972 PyEval_RestoreThread(_PyOS_ReadlineTState
);
974 s
= PyErr_CheckSignals();
979 rl_free_line_state();
980 rl_cleanup_after_signal();
981 rl_callback_handler_remove();
983 completed_input_string
= NULL
;
988 return completed_input_string
;
994 /* Interrupt handler */
1007 readline_until_enter_or_signal(char *prompt
, int *signal
)
1009 PyOS_sighandler_t old_inthandler
;
1014 old_inthandler
= PyOS_setsig(SIGINT
, onintr
);
1016 #ifdef HAVE_SIGRELSE
1017 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1020 PyOS_setsig(SIGINT
, old_inthandler
);
1024 rl_event_hook
= PyOS_InputHook
;
1025 p
= readline(prompt
);
1026 PyOS_setsig(SIGINT
, old_inthandler
);
1030 #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1034 call_readline(FILE *sys_stdin
, FILE *sys_stdout
, char *prompt
)
1041 char *saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
1043 Py_FatalError("not enough memory to save locale");
1044 setlocale(LC_CTYPE
, "");
1047 if (sys_stdin
!= rl_instream
|| sys_stdout
!= rl_outstream
) {
1048 rl_instream
= sys_stdin
;
1049 rl_outstream
= sys_stdout
;
1050 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
1051 rl_prep_terminal (1);
1055 p
= readline_until_enter_or_signal(prompt
, &signal
);
1057 /* we got an interrupt signal */
1059 RESTORE_LOCALE(saved_locale
)
1063 /* We got an EOF, return a empty string. */
1065 p
= PyMem_Malloc(1);
1068 RESTORE_LOCALE(saved_locale
)
1072 /* we have a valid line */
1076 int length
= _py_get_history_length();
1079 if (using_libedit_emulation
) {
1081 * Libedit's emulation uses 0-based indexes,
1082 * the real readline uses 1-based indexes.
1084 line
= history_get(length
- 1)->line
;
1086 #endif /* __APPLE__ */
1087 line
= history_get(length
)->line
;
1090 if (strcmp(p
, line
))
1093 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1094 release the original. */
1096 p
= PyMem_Malloc(n
+2);
1103 RESTORE_LOCALE(saved_locale
)
1108 /* Initialize the module */
1110 PyDoc_STRVAR(doc_module
,
1111 "Importing this module enables command line editing using GNU readline.");
1114 PyDoc_STRVAR(doc_module_le
,
1115 "Importing this module enables command line editing using libedit readline.");
1116 #endif /* __APPLE__ */
1124 if (strncmp(rl_library_version
, libedit_version_tag
, strlen(libedit_version_tag
)) == 0) {
1125 using_libedit_emulation
= 1;
1128 if (using_libedit_emulation
)
1129 m
= Py_InitModule4("readline", readline_methods
, doc_module_le
,
1130 (PyObject
*)NULL
, PYTHON_API_VERSION
);
1133 #endif /* __APPLE__ */
1135 m
= Py_InitModule4("readline", readline_methods
, doc_module
,
1136 (PyObject
*)NULL
, PYTHON_API_VERSION
);
1142 PyOS_ReadlineFunctionPointer
= call_readline
;