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
*);
41 extern char **completion_matches(char *, CPFunction
*);
47 * It is possible to link the readline module to the readline
48 * emulation library of editline/libedit.
50 * On OSX this emulation library is not 100% API compatible
51 * with the "real" readline and cannot be detected at compile-time,
52 * hence we use a runtime check to detect if we're using libedit
54 * Currently there is one know API incompatibility:
55 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
56 * index with libedit's emulation.
57 * - Note that replace_history and remove_history use a 0-based index
58 * with both implementation.
60 static int using_libedit_emulation
= 0;
61 static const char libedit_version_tag
[] = "EditLine wrapper";
62 #endif /* __APPLE__ */
65 on_completion_display_matches_hook(char **matches
,
66 int num_matches
, int max_length
);
69 /* Exported function to send one line to readline's init file parser */
72 parse_and_bind(PyObject
*self
, PyObject
*args
)
75 if (!PyArg_ParseTuple(args
, "s:parse_and_bind", &s
))
77 /* Make a copy -- rl_parse_and_bind() modifies its argument */
79 copy
= malloc(1 + strlen(s
));
81 return PyErr_NoMemory();
83 rl_parse_and_bind(copy
);
84 free(copy
); /* Free the copy */
88 PyDoc_STRVAR(doc_parse_and_bind
,
89 "parse_and_bind(string) -> None\n\
90 Parse and execute single line of a readline init file.");
93 /* Exported function to parse a readline init file */
96 read_init_file(PyObject
*self
, PyObject
*args
)
99 if (!PyArg_ParseTuple(args
, "|z:read_init_file", &s
))
101 errno
= rl_read_init_file(s
);
103 return PyErr_SetFromErrno(PyExc_IOError
);
107 PyDoc_STRVAR(doc_read_init_file
,
108 "read_init_file([filename]) -> None\n\
109 Parse a readline initialization file.\n\
110 The default filename is the last filename used.");
113 /* Exported function to load a readline history file */
116 read_history_file(PyObject
*self
, PyObject
*args
)
119 if (!PyArg_ParseTuple(args
, "|z:read_history_file", &s
))
121 errno
= read_history(s
);
123 return PyErr_SetFromErrno(PyExc_IOError
);
127 static int _history_length
= -1; /* do not truncate history by default */
128 PyDoc_STRVAR(doc_read_history_file
,
129 "read_history_file([filename]) -> None\n\
130 Load a readline history file.\n\
131 The default filename is ~/.history.");
134 /* Exported function to save a readline history file */
137 write_history_file(PyObject
*self
, PyObject
*args
)
140 if (!PyArg_ParseTuple(args
, "|z:write_history_file", &s
))
142 errno
= write_history(s
);
143 if (!errno
&& _history_length
>= 0)
144 history_truncate_file(s
, _history_length
);
146 return PyErr_SetFromErrno(PyExc_IOError
);
150 PyDoc_STRVAR(doc_write_history_file
,
151 "write_history_file([filename]) -> None\n\
152 Save a readline history file.\n\
153 The default filename is ~/.history.");
156 /* Set history length */
159 set_history_length(PyObject
*self
, PyObject
*args
)
161 int length
= _history_length
;
162 if (!PyArg_ParseTuple(args
, "i:set_history_length", &length
))
164 _history_length
= length
;
168 PyDoc_STRVAR(set_history_length_doc
,
169 "set_history_length(length) -> None\n\
170 set the maximal number of items which will be written to\n\
171 the history file. A negative length is used to inhibit\n\
172 history truncation.");
175 /* Get history length */
178 get_history_length(PyObject
*self
, PyObject
*noarg
)
180 return PyInt_FromLong(_history_length
);
183 PyDoc_STRVAR(get_history_length_doc
,
184 "get_history_length() -> int\n\
185 return the maximum number of items that will be written to\n\
189 /* Generic hook function setter */
192 set_hook(const char *funcname
, PyObject
**hook_var
, PyObject
*args
)
194 PyObject
*function
= Py_None
;
196 PyOS_snprintf(buf
, sizeof(buf
), "|O:set_%.50s", funcname
);
197 if (!PyArg_ParseTuple(args
, buf
, &function
))
199 if (function
== Py_None
) {
200 Py_XDECREF(*hook_var
);
203 else if (PyCallable_Check(function
)) {
204 PyObject
*tmp
= *hook_var
;
206 *hook_var
= function
;
210 PyOS_snprintf(buf
, sizeof(buf
),
211 "set_%.50s(func): argument not callable",
213 PyErr_SetString(PyExc_TypeError
, buf
);
220 /* Exported functions to specify hook functions in Python */
222 static PyObject
*completion_display_matches_hook
= NULL
;
223 static PyObject
*startup_hook
= NULL
;
225 #ifdef HAVE_RL_PRE_INPUT_HOOK
226 static PyObject
*pre_input_hook
= NULL
;
230 set_completion_display_matches_hook(PyObject
*self
, PyObject
*args
)
232 PyObject
*result
= set_hook("completion_display_matches_hook",
233 &completion_display_matches_hook
, args
);
234 #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
235 /* We cannot set this hook globally, since it replaces the
236 default completion display. */
237 rl_completion_display_matches_hook
=
238 completion_display_matches_hook
?
239 #if defined(_RL_FUNCTION_TYPEDEF)
240 (rl_compdisp_func_t
*)on_completion_display_matches_hook
: 0;
242 (VFunction
*)on_completion_display_matches_hook
: 0;
249 PyDoc_STRVAR(doc_set_completion_display_matches_hook
,
250 "set_completion_display_matches_hook([function]) -> None\n\
251 Set or remove the completion display function.\n\
252 The function is called as\n\
253 function(substitution, [matches], longest_match_length)\n\
254 once each time matches need to be displayed.");
257 set_startup_hook(PyObject
*self
, PyObject
*args
)
259 return set_hook("startup_hook", &startup_hook
, args
);
262 PyDoc_STRVAR(doc_set_startup_hook
,
263 "set_startup_hook([function]) -> None\n\
264 Set or remove the startup_hook function.\n\
265 The function is called with no arguments just\n\
266 before readline prints the first prompt.");
269 #ifdef HAVE_RL_PRE_INPUT_HOOK
271 /* Set pre-input hook */
274 set_pre_input_hook(PyObject
*self
, PyObject
*args
)
276 return set_hook("pre_input_hook", &pre_input_hook
, args
);
279 PyDoc_STRVAR(doc_set_pre_input_hook
,
280 "set_pre_input_hook([function]) -> None\n\
281 Set or remove the pre_input_hook function.\n\
282 The function is called with no arguments after the first prompt\n\
283 has been printed and just before readline starts reading input\n\
289 /* Exported function to specify a word completer in Python */
291 static PyObject
*completer
= NULL
;
293 static PyObject
*begidx
= NULL
;
294 static PyObject
*endidx
= NULL
;
297 /* Get the completion type for the scope of the tab-completion */
299 get_completion_type(PyObject
*self
, PyObject
*noarg
)
301 return PyInt_FromLong(rl_completion_type
);
304 PyDoc_STRVAR(doc_get_completion_type
,
305 "get_completion_type() -> int\n\
306 Get the type of completion being attempted.");
309 /* Get the beginning index for the scope of the tab-completion */
312 get_begidx(PyObject
*self
, PyObject
*noarg
)
318 PyDoc_STRVAR(doc_get_begidx
,
319 "get_begidx() -> int\n\
320 get the beginning index of the readline tab-completion scope");
323 /* Get the ending index for the scope of the tab-completion */
326 get_endidx(PyObject
*self
, PyObject
*noarg
)
332 PyDoc_STRVAR(doc_get_endidx
,
333 "get_endidx() -> int\n\
334 get the ending index of the readline tab-completion scope");
337 /* Set the tab-completion word-delimiters that readline uses */
340 set_completer_delims(PyObject
*self
, PyObject
*args
)
344 if(!PyArg_ParseTuple(args
, "s:set_completer_delims", &break_chars
)) {
347 free((void*)rl_completer_word_break_characters
);
348 rl_completer_word_break_characters
= strdup(break_chars
);
352 PyDoc_STRVAR(doc_set_completer_delims
,
353 "set_completer_delims(string) -> None\n\
354 set the readline word delimiters for tab-completion");
357 py_remove_history(PyObject
*self
, PyObject
*args
)
362 if (!PyArg_ParseTuple(args
, "i:remove_history", &entry_number
))
364 if (entry_number
< 0) {
365 PyErr_SetString(PyExc_ValueError
,
366 "History index cannot be negative");
369 entry
= remove_history(entry_number
);
371 PyErr_Format(PyExc_ValueError
,
372 "No history item at position %d",
376 /* free memory allocated for the history entry */
386 PyDoc_STRVAR(doc_remove_history
,
387 "remove_history_item(pos) -> None\n\
388 remove history item given by its position");
391 py_replace_history(PyObject
*self
, PyObject
*args
)
395 HIST_ENTRY
*old_entry
;
397 if (!PyArg_ParseTuple(args
, "is:replace_history", &entry_number
,
401 if (entry_number
< 0) {
402 PyErr_SetString(PyExc_ValueError
,
403 "History index cannot be negative");
406 old_entry
= replace_history_entry(entry_number
, line
, (void *)NULL
);
408 PyErr_Format(PyExc_ValueError
,
409 "No history item at position %d",
413 /* free memory allocated for the old history entry */
415 free(old_entry
->line
);
417 free(old_entry
->data
);
423 PyDoc_STRVAR(doc_replace_history
,
424 "replace_history_item(pos, line) -> None\n\
425 replaces history item given by its position with contents of line");
427 /* Add a line to the history buffer */
430 py_add_history(PyObject
*self
, PyObject
*args
)
434 if(!PyArg_ParseTuple(args
, "s:add_history", &line
)) {
441 PyDoc_STRVAR(doc_add_history
,
442 "add_history(string) -> None\n\
443 add a line to the history buffer");
446 /* Get the tab-completion word-delimiters that readline uses */
449 get_completer_delims(PyObject
*self
, PyObject
*noarg
)
451 return PyString_FromString(rl_completer_word_break_characters
);
454 PyDoc_STRVAR(doc_get_completer_delims
,
455 "get_completer_delims() -> string\n\
456 get the readline word delimiters for tab-completion");
459 /* Set the completer function */
462 set_completer(PyObject
*self
, PyObject
*args
)
464 return set_hook("completer", &completer
, args
);
467 PyDoc_STRVAR(doc_set_completer
,
468 "set_completer([function]) -> None\n\
469 Set or remove the completer function.\n\
470 The function is called as function(text, state),\n\
471 for state in 0, 1, 2, ..., until it returns a non-string.\n\
472 It should return the next possible completion starting with 'text'.");
476 get_completer(PyObject
*self
, PyObject
*noargs
)
478 if (completer
== NULL
) {
481 Py_INCREF(completer
);
485 PyDoc_STRVAR(doc_get_completer
,
486 "get_completer() -> function\n\
488 Returns current completer function.");
490 /* Exported function to get any element of history */
493 get_history_item(PyObject
*self
, PyObject
*args
)
496 HIST_ENTRY
*hist_ent
;
498 if (!PyArg_ParseTuple(args
, "i:index", &idx
))
501 if (using_libedit_emulation
) {
502 /* Libedit emulation uses 0-based indexes,
503 * the real one uses 1-based indexes,
504 * adjust the index to ensure that Python
505 * code doesn't have to worry about the
508 HISTORY_STATE
*hist_st
;
509 hist_st
= history_get_history_state();
514 * Apple's readline emulation crashes when
515 * the index is out of range, therefore
516 * test for that and fail gracefully.
518 if (idx
< 0 || idx
>= hist_st
->length
) {
522 #endif /* __APPLE__ */
523 if ((hist_ent
= history_get(idx
)))
524 return PyString_FromString(hist_ent
->line
);
530 PyDoc_STRVAR(doc_get_history_item
,
531 "get_history_item() -> string\n\
532 return the current contents of history item at index.");
535 /* Exported function to get current length of history */
538 get_current_history_length(PyObject
*self
, PyObject
*noarg
)
540 HISTORY_STATE
*hist_st
;
542 hist_st
= history_get_history_state();
543 return PyInt_FromLong(hist_st
? (long) hist_st
->length
: (long) 0);
546 PyDoc_STRVAR(doc_get_current_history_length
,
547 "get_current_history_length() -> integer\n\
548 return the current (not the maximum) length of history.");
551 /* Exported function to read the current line buffer */
554 get_line_buffer(PyObject
*self
, PyObject
*noarg
)
556 return PyString_FromString(rl_line_buffer
);
559 PyDoc_STRVAR(doc_get_line_buffer
,
560 "get_line_buffer() -> string\n\
561 return the current contents of the line buffer.");
564 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
566 /* Exported function to clear the current history */
569 py_clear_history(PyObject
*self
, PyObject
*noarg
)
575 PyDoc_STRVAR(doc_clear_history
,
576 "clear_history() -> None\n\
577 Clear the current readline history.");
581 /* Exported function to insert text into the line buffer */
584 insert_text(PyObject
*self
, PyObject
*args
)
587 if (!PyArg_ParseTuple(args
, "s:insert_text", &s
))
593 PyDoc_STRVAR(doc_insert_text
,
594 "insert_text(string) -> None\n\
595 Insert text into the command line.");
598 /* Redisplay the line buffer */
601 redisplay(PyObject
*self
, PyObject
*noarg
)
607 PyDoc_STRVAR(doc_redisplay
,
608 "redisplay() -> None\n\
609 Change what's displayed on the screen to reflect the current\n\
610 contents of the line buffer.");
613 /* Table of functions exported by the module */
615 static struct PyMethodDef readline_methods
[] =
617 {"parse_and_bind", parse_and_bind
, METH_VARARGS
, doc_parse_and_bind
},
618 {"get_line_buffer", get_line_buffer
, METH_NOARGS
, doc_get_line_buffer
},
619 {"insert_text", insert_text
, METH_VARARGS
, doc_insert_text
},
620 {"redisplay", redisplay
, METH_NOARGS
, doc_redisplay
},
621 {"read_init_file", read_init_file
, METH_VARARGS
, doc_read_init_file
},
622 {"read_history_file", read_history_file
,
623 METH_VARARGS
, doc_read_history_file
},
624 {"write_history_file", write_history_file
,
625 METH_VARARGS
, doc_write_history_file
},
626 {"get_history_item", get_history_item
,
627 METH_VARARGS
, doc_get_history_item
},
628 {"get_current_history_length", (PyCFunction
)get_current_history_length
,
629 METH_NOARGS
, doc_get_current_history_length
},
630 {"set_history_length", set_history_length
,
631 METH_VARARGS
, set_history_length_doc
},
632 {"get_history_length", get_history_length
,
633 METH_NOARGS
, get_history_length_doc
},
634 {"set_completer", set_completer
, METH_VARARGS
, doc_set_completer
},
635 {"get_completer", get_completer
, METH_NOARGS
, doc_get_completer
},
636 {"get_completion_type", get_completion_type
,
637 METH_NOARGS
, doc_get_completion_type
},
638 {"get_begidx", get_begidx
, METH_NOARGS
, doc_get_begidx
},
639 {"get_endidx", get_endidx
, METH_NOARGS
, doc_get_endidx
},
641 {"set_completer_delims", set_completer_delims
,
642 METH_VARARGS
, doc_set_completer_delims
},
643 {"add_history", py_add_history
, METH_VARARGS
, doc_add_history
},
644 {"remove_history_item", py_remove_history
, METH_VARARGS
, doc_remove_history
},
645 {"replace_history_item", py_replace_history
, METH_VARARGS
, doc_replace_history
},
646 {"get_completer_delims", get_completer_delims
,
647 METH_NOARGS
, doc_get_completer_delims
},
649 {"set_completion_display_matches_hook", set_completion_display_matches_hook
,
650 METH_VARARGS
, doc_set_completion_display_matches_hook
},
651 {"set_startup_hook", set_startup_hook
,
652 METH_VARARGS
, doc_set_startup_hook
},
653 #ifdef HAVE_RL_PRE_INPUT_HOOK
654 {"set_pre_input_hook", set_pre_input_hook
,
655 METH_VARARGS
, doc_set_pre_input_hook
},
657 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
658 {"clear_history", py_clear_history
, METH_NOARGS
, doc_clear_history
},
664 /* C function to call the Python hooks. */
667 on_hook(PyObject
*func
)
673 PyGILState_STATE gilstate
= PyGILState_Ensure();
675 r
= PyObject_CallFunction(func
, NULL
);
681 result
= PyInt_AsLong(r
);
682 if (result
== -1 && PyErr_Occurred())
692 PyGILState_Release(gilstate
);
700 on_startup_hook(void)
702 return on_hook(startup_hook
);
705 #ifdef HAVE_RL_PRE_INPUT_HOOK
707 on_pre_input_hook(void)
709 return on_hook(pre_input_hook
);
714 /* C function to call the Python completion_display_matches */
717 on_completion_display_matches_hook(char **matches
,
718 int num_matches
, int max_length
)
721 PyObject
*m
=NULL
, *s
=NULL
, *r
=NULL
;
723 PyGILState_STATE gilstate
= PyGILState_Ensure();
725 m
= PyList_New(num_matches
);
728 for (i
= 0; i
< num_matches
; i
++) {
729 s
= PyString_FromString(matches
[i
+1]);
732 if (PyList_SetItem(m
, i
, s
) == -1)
736 r
= PyObject_CallFunction(completion_display_matches_hook
,
737 "sOi", matches
[0], m
, max_length
);
739 Py_DECREF(m
); m
=NULL
;
742 (r
!= Py_None
&& PyInt_AsLong(r
) == -1 && PyErr_Occurred())) {
745 Py_XDECREF(r
); r
=NULL
;
754 PyGILState_Release(gilstate
);
759 /* C function to call the Python completer. */
762 on_completion(const char *text
, int state
)
765 if (completer
!= NULL
) {
768 PyGILState_STATE gilstate
= PyGILState_Ensure();
770 rl_attempted_completion_over
= 1;
771 r
= PyObject_CallFunction(completer
, "si", text
, state
);
778 char *s
= PyString_AsString(r
);
790 PyGILState_Release(gilstate
);
798 /* A more flexible constructor that saves the "begidx" and "endidx"
799 * before calling the normal completer */
802 flex_complete(char *text
, int start
, int end
)
804 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
805 rl_completion_append_character
='\0';
806 rl_completion_suppress_append
= 0;
810 begidx
= PyInt_FromLong((long) start
);
811 endidx
= PyInt_FromLong((long) end
);
812 return completion_matches(text
, *on_completion
);
816 /* Helper to initialize GNU readline properly. */
822 char *saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
824 Py_FatalError("not enough memory to save locale");
829 rl_readline_name
= "python";
830 #if defined(PYOS_OS2) && defined(PYCC_GCC)
831 /* Allow $if term= in .inputrc to work */
832 rl_terminal_name
= getenv("TERM");
834 /* Force rebind of TAB to insert-tab */
835 rl_bind_key('\t', rl_insert
);
836 /* Bind both ESC-TAB and ESC-ESC to the completion function */
837 rl_bind_key_in_map ('\t', rl_complete
, emacs_meta_keymap
);
838 rl_bind_key_in_map ('\033', rl_complete
, emacs_meta_keymap
);
839 /* Set our hook functions */
840 rl_startup_hook
= (Function
*)on_startup_hook
;
841 #ifdef HAVE_RL_PRE_INPUT_HOOK
842 rl_pre_input_hook
= (Function
*)on_pre_input_hook
;
844 /* Set our completion function */
845 rl_attempted_completion_function
= (CPPFunction
*)flex_complete
;
846 /* Set Python word break characters */
847 rl_completer_word_break_characters
=
848 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
849 /* All nonalphanums except '.' */
851 begidx
= PyInt_FromLong(0L);
852 endidx
= PyInt_FromLong(0L);
853 /* Initialize (allows .inputrc to override)
855 * XXX: A bug in the readline-2.2 library causes a memory leak
856 * inside this function. Nothing we can do about it.
860 RESTORE_LOCALE(saved_locale
)
863 /* Wrapper around GNU readline that handles signals differently. */
866 #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
868 static char *completed_input_string
;
870 rlhandler(char *text
)
872 completed_input_string
= text
;
873 rl_callback_handler_remove();
876 extern PyThreadState
* _PyOS_ReadlineTState
;
879 readline_until_enter_or_signal(char *prompt
, int *signal
)
881 char * not_done_reading
= "";
885 #ifdef HAVE_RL_CATCH_SIGNAL
886 rl_catch_signals
= 0;
889 rl_callback_handler_install (prompt
, rlhandler
);
892 completed_input_string
= not_done_reading
;
894 while (completed_input_string
== not_done_reading
) {
898 { struct timeval timeout
= {0, 100000}; /* 0.1 seconds */
900 /* [Bug #1552726] Only limit the pause if an input hook has been
902 struct timeval
*timeoutp
= NULL
;
905 FD_SET(fileno(rl_instream
), &selectset
);
906 /* select resets selectset if no input was available */
907 has_input
= select(fileno(rl_instream
) + 1, &selectset
,
908 NULL
, NULL
, timeoutp
);
909 if(PyOS_InputHook
) PyOS_InputHook();
913 rl_callback_read_char();
915 else if (errno
== EINTR
) {
918 PyEval_RestoreThread(_PyOS_ReadlineTState
);
920 s
= PyErr_CheckSignals();
925 rl_free_line_state();
926 rl_cleanup_after_signal();
927 rl_callback_handler_remove();
929 completed_input_string
= NULL
;
934 return completed_input_string
;
940 /* Interrupt handler */
953 readline_until_enter_or_signal(char *prompt
, int *signal
)
955 PyOS_sighandler_t old_inthandler
;
960 old_inthandler
= PyOS_setsig(SIGINT
, onintr
);
963 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
966 PyOS_setsig(SIGINT
, old_inthandler
);
970 rl_event_hook
= PyOS_InputHook
;
971 p
= readline(prompt
);
972 PyOS_setsig(SIGINT
, old_inthandler
);
976 #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
980 call_readline(FILE *sys_stdin
, FILE *sys_stdout
, char *prompt
)
987 char *saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
989 Py_FatalError("not enough memory to save locale");
990 setlocale(LC_CTYPE
, "");
993 if (sys_stdin
!= rl_instream
|| sys_stdout
!= rl_outstream
) {
994 rl_instream
= sys_stdin
;
995 rl_outstream
= sys_stdout
;
996 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
997 rl_prep_terminal (1);
1001 p
= readline_until_enter_or_signal(prompt
, &signal
);
1003 /* we got an interrupt signal */
1005 RESTORE_LOCALE(saved_locale
)
1009 /* We got an EOF, return a empty string. */
1011 p
= PyMem_Malloc(1);
1014 RESTORE_LOCALE(saved_locale
)
1018 /* we have a valid line */
1022 HISTORY_STATE
*state
= history_get_history_state();
1023 if (state
->length
> 0)
1025 if (using_libedit_emulation
) {
1027 * Libedit's emulation uses 0-based indexes,
1028 * the real readline uses 1-based indexes.
1030 line
= history_get(state
->length
- 1)->line
;
1032 #endif /* __APPLE__ */
1033 line
= history_get(state
->length
)->line
;
1036 if (strcmp(p
, line
))
1038 /* the history docs don't say so, but the address of state
1039 changes each time history_get_history_state is called
1040 which makes me think it's freshly malloc'd memory...
1041 on the other hand, the address of the last line stays the
1042 same as long as history isn't extended, so it appears to
1043 be malloc'd but managed by the history package... */
1046 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1047 release the original. */
1049 p
= PyMem_Malloc(n
+2);
1056 RESTORE_LOCALE(saved_locale
)
1061 /* Initialize the module */
1063 PyDoc_STRVAR(doc_module
,
1064 "Importing this module enables command line editing using GNU readline.");
1067 PyDoc_STRVAR(doc_module_le
,
1068 "Importing this module enables command line editing using libedit readline.");
1069 #endif /* __APPLE__ */
1077 if (strncmp(rl_library_version
, libedit_version_tag
, strlen(libedit_version_tag
)) == 0) {
1078 using_libedit_emulation
= 1;
1081 if (using_libedit_emulation
)
1082 m
= Py_InitModule4("readline", readline_methods
, doc_module_le
,
1083 (PyObject
*)NULL
, PYTHON_API_VERSION
);
1086 #endif /* __APPLE__ */
1088 m
= Py_InitModule4("readline", readline_methods
, doc_module
,
1089 (PyObject
*)NULL
, PYTHON_API_VERSION
);
1095 PyOS_ReadlineFunctionPointer
= call_readline
;