blob: ad5903cfb51a54386fe42ed012095a929f04a1ee [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
Michael W. Hudson9a8c3142005-03-30 10:09:12 +00003 * Center. The completer interface was inspired by Lele Gaifax. More
4 * recently, it was largely rewritten by Guido van Rossum.
Guido van Rossum0969d361997-08-05 21:27:50 +00005 */
6
Guido van Rossum290900a1997-09-26 21:51:21 +00007/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +00008#include "Python.h"
9#include <setjmp.h>
10#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* 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.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Neal Norwitz5eaf7722006-07-16 02:15:27 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
Brett Cannon23b581a2010-05-04 00:52:41 +000026# define RESTORE_LOCALE(sl)
Neal Norwitz5eaf7722006-07-16 02:15:27 +000027#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Neal Norwitzbd538702007-04-19 05:52:37 +000037#else
Martin v. Löwisbb86d832008-11-04 20:40:09 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Neal Norwitzbd538702007-04-19 05:52:37 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisbb86d832008-11-04 20:40:09 +000040#else
Ronald Oussoren333fca92010-02-11 13:13:08 +000041
42#if !defined(__APPLE__)
Martin v. Löwisbb86d832008-11-04 20:40:09 +000043extern char **completion_matches(char *, CPFunction *);
44#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000045#endif
Ronald Oussoren333fca92010-02-11 13:13:08 +000046#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000047
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000048#ifdef __APPLE__
49/*
50 * It is possible to link the readline module to the readline
Brett Cannon23b581a2010-05-04 00:52:41 +000051 * emulation library of editline/libedit.
52 *
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000053 * 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
56 *
Ned Deily62a19292013-10-12 15:45:25 -070057 * Currently there is one known API incompatibility:
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000058 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
Ned Deily62a19292013-10-12 15:45:25 -070059 * index with older versions of libedit's emulation.
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000060 * - Note that replace_history and remove_history use a 0-based index
Ned Deily62a19292013-10-12 15:45:25 -070061 * with both implementations.
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000062 */
63static int using_libedit_emulation = 0;
64static const char libedit_version_tag[] = "EditLine wrapper";
Ned Deily62a19292013-10-12 15:45:25 -070065
66static int libedit_history_start = 0;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000067#endif /* __APPLE__ */
68
Martin Panter646b5282016-06-21 23:58:05 +000069#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Martin v. Löwisf3548942007-11-12 04:53:02 +000070static void
71on_completion_display_matches_hook(char **matches,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 int num_matches, int max_length);
Martin Panter646b5282016-06-21 23:58:05 +000073#endif
Guido van Rossum0969d361997-08-05 21:27:50 +000074
Antoine Pitrou31bc8be2013-05-06 21:51:03 +020075/* Memory allocated for rl_completer_word_break_characters
76 (see issue #17289 for the motivation). */
77static char *completer_word_break_characters;
78
Guido van Rossum290900a1997-09-26 21:51:21 +000079/* Exported function to send one line to readline's init file parser */
80
81static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000082parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000083{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000084 char *s, *copy;
85 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
86 return NULL;
87 /* Make a copy -- rl_parse_and_bind() modifies its argument */
88 /* Bernard Herzog */
89 copy = malloc(1 + strlen(s));
90 if (copy == NULL)
91 return PyErr_NoMemory();
92 strcpy(copy, s);
93 rl_parse_and_bind(copy);
94 free(copy); /* Free the copy */
95 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000096}
97
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098PyDoc_STRVAR(doc_parse_and_bind,
99"parse_and_bind(string) -> None\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000100Execute the init line provided in the string argument.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000101
102
103/* Exported function to parse a readline init file */
104
105static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000106read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000107{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000108 char *s = NULL;
109 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
110 return NULL;
111 errno = rl_read_init_file(s);
112 if (errno)
113 return PyErr_SetFromErrno(PyExc_IOError);
114 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000115}
116
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117PyDoc_STRVAR(doc_read_init_file,
118"read_init_file([filename]) -> None\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000119Execute a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000120The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000121
122
Skip Montanaro28067822000-07-06 18:55:12 +0000123/* Exported function to load a readline history file */
124
125static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000126read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000127{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000128 char *s = NULL;
129 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
130 return NULL;
131 errno = read_history(s);
132 if (errno)
133 return PyErr_SetFromErrno(PyExc_IOError);
134 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000135}
136
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000137static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000138PyDoc_STRVAR(doc_read_history_file,
139"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000140Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000141The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000142
143
144/* Exported function to save a readline history file */
145
146static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000147write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000148{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000149 char *s = NULL;
150 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
151 return NULL;
152 errno = write_history(s);
153 if (!errno && _history_length >= 0)
154 history_truncate_file(s, _history_length);
155 if (errno)
156 return PyErr_SetFromErrno(PyExc_IOError);
157 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000158}
159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160PyDoc_STRVAR(doc_write_history_file,
161"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000162Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000163The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000164
165
Guido van Rossum74f31432003-01-07 20:01:29 +0000166/* Set history length */
167
168static PyObject*
169set_history_length(PyObject *self, PyObject *args)
170{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000171 int length = _history_length;
172 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
173 return NULL;
174 _history_length = length;
175 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000176}
177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178PyDoc_STRVAR(set_history_length_doc,
179"set_history_length(length) -> None\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000180set the maximal number of lines which will be written to\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000181the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000182history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000183
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000184
Guido van Rossum74f31432003-01-07 20:01:29 +0000185/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000186
187static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000188get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000189{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000191}
192
Guido van Rossum74f31432003-01-07 20:01:29 +0000193PyDoc_STRVAR(get_history_length_doc,
194"get_history_length() -> int\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000195return the maximum number of lines that will be written to\n\
Guido van Rossum74f31432003-01-07 20:01:29 +0000196the history file.");
197
198
Martin v. Löwis0daad592001-09-30 21:09:59 +0000199/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000200
Martin v. Löwis0daad592001-09-30 21:09:59 +0000201static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000202set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000203{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000204 PyObject *function = Py_None;
205 char buf[80];
206 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
207 if (!PyArg_ParseTuple(args, buf, &function))
208 return NULL;
209 if (function == Py_None) {
Serhiy Storchaka98a97222014-02-09 13:14:04 +0200210 Py_CLEAR(*hook_var);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000211 }
212 else if (PyCallable_Check(function)) {
213 PyObject *tmp = *hook_var;
214 Py_INCREF(function);
215 *hook_var = function;
216 Py_XDECREF(tmp);
217 }
218 else {
219 PyOS_snprintf(buf, sizeof(buf),
220 "set_%.50s(func): argument not callable",
221 funcname);
222 PyErr_SetString(PyExc_TypeError, buf);
223 return NULL;
224 }
225 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000226}
227
Guido van Rossum74f31432003-01-07 20:01:29 +0000228
Martin v. Löwis0daad592001-09-30 21:09:59 +0000229/* Exported functions to specify hook functions in Python */
230
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000231static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000232static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000233
234#ifdef HAVE_RL_PRE_INPUT_HOOK
235static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000236#endif
237
238static PyObject *
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000239set_completion_display_matches_hook(PyObject *self, PyObject *args)
240{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 PyObject *result = set_hook("completion_display_matches_hook",
242 &completion_display_matches_hook, args);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000243#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000244 /* We cannot set this hook globally, since it replaces the
245 default completion display. */
246 rl_completion_display_matches_hook =
247 completion_display_matches_hook ?
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000248#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000250#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000252#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000253#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000254 return result;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000255
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000256}
257
258PyDoc_STRVAR(doc_set_completion_display_matches_hook,
259"set_completion_display_matches_hook([function]) -> None\n\
260Set or remove the completion display function.\n\
261The function is called as\n\
262 function(substitution, [matches], longest_match_length)\n\
263once each time matches need to be displayed.");
264
265static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000266set_startup_hook(PyObject *self, PyObject *args)
267{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000269}
270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271PyDoc_STRVAR(doc_set_startup_hook,
272"set_startup_hook([function]) -> None\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000273Set or remove the function invoked by the rl_startup_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000274The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000275before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000276
Guido van Rossum74f31432003-01-07 20:01:29 +0000277
Martin v. Löwis0daad592001-09-30 21:09:59 +0000278#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000279
280/* Set pre-input hook */
281
Martin v. Löwis0daad592001-09-30 21:09:59 +0000282static PyObject *
283set_pre_input_hook(PyObject *self, PyObject *args)
284{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000285 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000286}
287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288PyDoc_STRVAR(doc_set_pre_input_hook,
289"set_pre_input_hook([function]) -> None\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000290Set or remove the function invoked by the rl_pre_input_hook callback.\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000291The function is called with no arguments after the first prompt\n\
292has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000294
Martin v. Löwis0daad592001-09-30 21:09:59 +0000295#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000296
Guido van Rossum74f31432003-01-07 20:01:29 +0000297
Guido van Rossum290900a1997-09-26 21:51:21 +0000298/* Exported function to specify a word completer in Python */
299
300static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000301
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000302static PyObject *begidx = NULL;
303static PyObject *endidx = NULL;
304
Guido van Rossum74f31432003-01-07 20:01:29 +0000305
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000306/* Get the completion type for the scope of the tab-completion */
307static PyObject *
308get_completion_type(PyObject *self, PyObject *noarg)
309{
310 return PyInt_FromLong(rl_completion_type);
311}
312
313PyDoc_STRVAR(doc_get_completion_type,
314"get_completion_type() -> int\n\
315Get the type of completion being attempted.");
316
317
Guido van Rossum74f31432003-01-07 20:01:29 +0000318/* Get the beginning index for the scope of the tab-completion */
319
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000320static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000321get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000322{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000323 Py_INCREF(begidx);
324 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000325}
326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000327PyDoc_STRVAR(doc_get_begidx,
328"get_begidx() -> int\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000329get the beginning index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000330
Guido van Rossum74f31432003-01-07 20:01:29 +0000331
332/* Get the ending index for the scope of the tab-completion */
333
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000334static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000335get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000336{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000337 Py_INCREF(endidx);
338 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000339}
340
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000341PyDoc_STRVAR(doc_get_endidx,
342"get_endidx() -> int\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000343get the ending index of the completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000344
345
Guido van Rossum74f31432003-01-07 20:01:29 +0000346/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000347
348static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000349set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000350{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000351 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000352
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200353 if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354 return NULL;
355 }
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200356 /* Keep a reference to the allocated memory in the module state in case
357 some other module modifies rl_completer_word_break_characters
358 (see issue #17289). */
Serhiy Storchakaa8041ae2015-09-27 22:34:59 +0300359 break_chars = strdup(break_chars);
360 if (break_chars) {
361 free(completer_word_break_characters);
362 completer_word_break_characters = break_chars;
363 rl_completer_word_break_characters = break_chars;
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200364 Py_RETURN_NONE;
365 }
366 else
367 return PyErr_NoMemory();
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000368}
369
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000370PyDoc_STRVAR(doc_set_completer_delims,
371"set_completer_delims(string) -> None\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000372set the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000373
Mark Dickinson4ee98532010-08-03 16:18:39 +0000374/* _py_free_history_entry: Utility function to free a history entry. */
375
376#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
377
378/* Readline version >= 5.0 introduced a timestamp field into the history entry
379 structure; this needs to be freed to avoid a memory leak. This version of
380 readline also introduced the handy 'free_history_entry' function, which
381 takes care of the timestamp. */
382
383static void
384_py_free_history_entry(HIST_ENTRY *entry)
385{
386 histdata_t data = free_history_entry(entry);
387 free(data);
388}
389
390#else
391
392/* No free_history_entry function; free everything manually. */
393
394static void
395_py_free_history_entry(HIST_ENTRY *entry)
396{
397 if (entry->line)
398 free((void *)entry->line);
399 if (entry->data)
400 free(entry->data);
401 free(entry);
402}
403
404#endif
405
Skip Montanaroe5069012004-08-15 14:32:06 +0000406static PyObject *
407py_remove_history(PyObject *self, PyObject *args)
408{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000409 int entry_number;
410 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000411
Martin Panteraad86a62016-04-05 07:37:22 +0000412 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000413 return NULL;
414 if (entry_number < 0) {
415 PyErr_SetString(PyExc_ValueError,
416 "History index cannot be negative");
417 return NULL;
418 }
419 entry = remove_history(entry_number);
420 if (!entry) {
421 PyErr_Format(PyExc_ValueError,
422 "No history item at position %d",
423 entry_number);
424 return NULL;
425 }
426 /* free memory allocated for the history entry */
Mark Dickinson4ee98532010-08-03 16:18:39 +0000427 _py_free_history_entry(entry);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000428 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000429}
430
431PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000432"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000433remove history item given by its position");
434
435static PyObject *
436py_replace_history(PyObject *self, PyObject *args)
437{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000438 int entry_number;
439 char *line;
440 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000441
Martin Panteraad86a62016-04-05 07:37:22 +0000442 if (!PyArg_ParseTuple(args, "is:replace_history_item", &entry_number,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000443 &line)) {
444 return NULL;
445 }
446 if (entry_number < 0) {
447 PyErr_SetString(PyExc_ValueError,
448 "History index cannot be negative");
449 return NULL;
450 }
451 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
452 if (!old_entry) {
453 PyErr_Format(PyExc_ValueError,
454 "No history item at position %d",
455 entry_number);
456 return NULL;
457 }
458 /* free memory allocated for the old history entry */
Mark Dickinson4ee98532010-08-03 16:18:39 +0000459 _py_free_history_entry(old_entry);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000460 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000461}
462
463PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000464"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000465replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000466
467/* Add a line to the history buffer */
468
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000469static PyObject *
470py_add_history(PyObject *self, PyObject *args)
471{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000472 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000473
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000474 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
475 return NULL;
476 }
477 add_history(line);
478 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000479}
480
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000481PyDoc_STRVAR(doc_add_history,
482"add_history(string) -> None\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000483add an item to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000484
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000485
Guido van Rossum74f31432003-01-07 20:01:29 +0000486/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000487
488static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000489get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000490{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 return PyString_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000492}
Guido van Rossum74f31432003-01-07 20:01:29 +0000493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000494PyDoc_STRVAR(doc_get_completer_delims,
495"get_completer_delims() -> string\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000496get the word delimiters for completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000497
Guido van Rossum74f31432003-01-07 20:01:29 +0000498
499/* Set the completer function */
500
Guido van Rossum290900a1997-09-26 21:51:21 +0000501static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000502set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000503{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000504 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000505}
506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000507PyDoc_STRVAR(doc_set_completer,
508"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000509Set or remove the completer function.\n\
510The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000511for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000512It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000513
Guido van Rossum74f31432003-01-07 20:01:29 +0000514
Michael W. Hudson796df152003-01-30 10:12:51 +0000515static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000516get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000517{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000518 if (completer == NULL) {
519 Py_RETURN_NONE;
520 }
521 Py_INCREF(completer);
522 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000523}
524
525PyDoc_STRVAR(doc_get_completer,
526"get_completer() -> function\n\
527\n\
528Returns current completer function.");
529
Mark Dickinson0f981282010-08-03 16:54:19 +0000530/* Private function to get current length of history. XXX It may be
531 * possible to replace this with a direct use of history_length instead,
532 * but it's not clear whether BSD's libedit keeps history_length up to date.
533 * See issue #8065.*/
534
535static int
536_py_get_history_length(void)
537{
538 HISTORY_STATE *hist_st = history_get_history_state();
539 int length = hist_st->length;
540 /* the history docs don't say so, but the address of hist_st changes each
541 time history_get_history_state is called which makes me think it's
542 freshly malloc'd memory... on the other hand, the address of the last
543 line stays the same as long as history isn't extended, so it appears to
544 be malloc'd but managed by the history package... */
545 free(hist_st);
546 return length;
547}
548
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000549/* Exported function to get any element of history */
550
551static PyObject *
552get_history_item(PyObject *self, PyObject *args)
553{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000554 int idx = 0;
555 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000556
Martin Panteraad86a62016-04-05 07:37:22 +0000557 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000558 return NULL;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000559#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000560 if (using_libedit_emulation) {
Ned Deily62a19292013-10-12 15:45:25 -0700561 /* Older versions of libedit's readline emulation
562 * use 0-based indexes, while readline and newer
563 * versions of libedit use 1-based indexes.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000564 */
Mark Dickinson0f981282010-08-03 16:54:19 +0000565 int length = _py_get_history_length();
Ned Deily62a19292013-10-12 15:45:25 -0700566
567 idx = idx - 1 + libedit_history_start;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000568
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000569 /*
570 * Apple's readline emulation crashes when
571 * the index is out of range, therefore
572 * test for that and fail gracefully.
573 */
Ned Deily62a19292013-10-12 15:45:25 -0700574 if (idx < (0 + libedit_history_start)
575 || idx >= (length + libedit_history_start)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000576 Py_RETURN_NONE;
577 }
578 }
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000579#endif /* __APPLE__ */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000580 if ((hist_ent = history_get(idx)))
581 return PyString_FromString(hist_ent->line);
582 else {
583 Py_RETURN_NONE;
584 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000585}
586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000587PyDoc_STRVAR(doc_get_history_item,
588"get_history_item() -> string\n\
589return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000590
Guido van Rossum74f31432003-01-07 20:01:29 +0000591
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000592/* Exported function to get current length of history */
593
594static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000595get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000596{
Mark Dickinson0f981282010-08-03 16:54:19 +0000597 return PyInt_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000598}
599
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000600PyDoc_STRVAR(doc_get_current_history_length,
601"get_current_history_length() -> integer\n\
602return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000603
Guido van Rossum74f31432003-01-07 20:01:29 +0000604
Guido van Rossum79378ff1997-10-07 14:53:21 +0000605/* Exported function to read the current line buffer */
606
607static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000608get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000609{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000610 return PyString_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000611}
612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000613PyDoc_STRVAR(doc_get_line_buffer,
614"get_line_buffer() -> string\n\
615return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000616
Guido van Rossum74f31432003-01-07 20:01:29 +0000617
Martin v. Löwise7a97962003-09-20 16:08:33 +0000618#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
619
620/* Exported function to clear the current history */
621
622static PyObject *
623py_clear_history(PyObject *self, PyObject *noarg)
624{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000625 clear_history();
626 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000627}
628
629PyDoc_STRVAR(doc_clear_history,
630"clear_history() -> None\n\
631Clear the current readline history.");
632#endif
633
634
Guido van Rossum79378ff1997-10-07 14:53:21 +0000635/* Exported function to insert text into the line buffer */
636
637static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000638insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000639{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000640 char *s;
641 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
642 return NULL;
643 rl_insert_text(s);
644 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000645}
646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000647PyDoc_STRVAR(doc_insert_text,
648"insert_text(string) -> None\n\
Martin Panteraad86a62016-04-05 07:37:22 +0000649Insert text into the line buffer at the cursor position.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000650
Guido van Rossum74f31432003-01-07 20:01:29 +0000651
652/* Redisplay the line buffer */
653
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000654static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000655redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000656{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 rl_redisplay();
658 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000659}
660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000661PyDoc_STRVAR(doc_redisplay,
662"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000663Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000664contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000665
Guido van Rossum74f31432003-01-07 20:01:29 +0000666
Guido van Rossum290900a1997-09-26 21:51:21 +0000667/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000668
669static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000670{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000671 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
672 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
673 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
674 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
675 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
676 {"read_history_file", read_history_file,
677 METH_VARARGS, doc_read_history_file},
678 {"write_history_file", write_history_file,
679 METH_VARARGS, doc_write_history_file},
680 {"get_history_item", get_history_item,
681 METH_VARARGS, doc_get_history_item},
682 {"get_current_history_length", (PyCFunction)get_current_history_length,
683 METH_NOARGS, doc_get_current_history_length},
684 {"set_history_length", set_history_length,
685 METH_VARARGS, set_history_length_doc},
686 {"get_history_length", get_history_length,
687 METH_NOARGS, get_history_length_doc},
688 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
689 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
690 {"get_completion_type", get_completion_type,
691 METH_NOARGS, doc_get_completion_type},
692 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
693 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000694
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000695 {"set_completer_delims", set_completer_delims,
696 METH_VARARGS, doc_set_completer_delims},
697 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
698 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
699 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
700 {"get_completer_delims", get_completer_delims,
701 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000702
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000703 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
704 METH_VARARGS, doc_set_completion_display_matches_hook},
705 {"set_startup_hook", set_startup_hook,
706 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000707#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000708 {"set_pre_input_hook", set_pre_input_hook,
709 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000710#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000711#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000712 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000713#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000714 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000715};
716
Guido van Rossum05ac4492003-01-07 20:04:12 +0000717
Martin v. Löwis0daad592001-09-30 21:09:59 +0000718/* C function to call the Python hooks. */
719
720static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000721on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000722{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000723 int result = 0;
724 if (func != NULL) {
725 PyObject *r;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000726#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000727 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000728#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000729 r = PyObject_CallFunction(func, NULL);
730 if (r == NULL)
731 goto error;
732 if (r == Py_None)
733 result = 0;
734 else {
735 result = PyInt_AsLong(r);
736 if (result == -1 && PyErr_Occurred())
737 goto error;
738 }
739 Py_DECREF(r);
740 goto done;
741 error:
742 PyErr_Clear();
743 Py_XDECREF(r);
744 done:
Christian Heimes1bc4af42007-11-12 18:58:08 +0000745#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000746 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000747#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000748 return result;
749 }
750 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000751}
752
753static int
Ned Deilyb0fd12d2014-02-05 16:52:26 -0800754#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000755on_startup_hook(void)
Ned Deilyb0fd12d2014-02-05 16:52:26 -0800756#else
757on_startup_hook()
758#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000759{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000760 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000761}
762
763#ifdef HAVE_RL_PRE_INPUT_HOOK
764static int
Ned Deilyb0fd12d2014-02-05 16:52:26 -0800765#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000766on_pre_input_hook(void)
Ned Deilyb0fd12d2014-02-05 16:52:26 -0800767#else
768on_pre_input_hook()
769#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000770{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000771 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000772}
773#endif
774
Guido van Rossum05ac4492003-01-07 20:04:12 +0000775
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000776/* C function to call the Python completion_display_matches */
777
Martin Panter646b5282016-06-21 23:58:05 +0000778#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000779static void
780on_completion_display_matches_hook(char **matches,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000781 int num_matches, int max_length)
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000782{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000783 int i;
784 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000785#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000786 PyGILState_STATE gilstate = PyGILState_Ensure();
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000787#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000788 m = PyList_New(num_matches);
789 if (m == NULL)
790 goto error;
791 for (i = 0; i < num_matches; i++) {
792 s = PyString_FromString(matches[i+1]);
793 if (s == NULL)
794 goto error;
795 if (PyList_SetItem(m, i, s) == -1)
796 goto error;
797 }
Martin v. Löwisf3548942007-11-12 04:53:02 +0000798
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000799 r = PyObject_CallFunction(completion_display_matches_hook,
800 "sOi", matches[0], m, max_length);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000801
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000802 Py_DECREF(m); m=NULL;
Brett Cannon23b581a2010-05-04 00:52:41 +0000803
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000804 if (r == NULL ||
805 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
806 goto error;
807 }
808 Py_XDECREF(r); r=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000809
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000810 if (0) {
811 error:
812 PyErr_Clear();
813 Py_XDECREF(m);
814 Py_XDECREF(r);
815 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000816#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000817 PyGILState_Release(gilstate);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000818#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000819}
820
Martin Panter29241242016-06-22 02:04:38 +0000821#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000822
Martin Pantera70c3232016-04-03 02:54:58 +0000823#ifdef HAVE_RL_RESIZE_TERMINAL
824static volatile sig_atomic_t sigwinch_received;
Martin Pantered06e8f2016-04-03 08:00:49 +0000825static PyOS_sighandler_t sigwinch_ohandler;
Martin Pantera70c3232016-04-03 02:54:58 +0000826
827static void
828readline_sigwinch_handler(int signum)
829{
830 sigwinch_received = 1;
831 if (sigwinch_ohandler &&
832 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
833 sigwinch_ohandler(signum);
834
835#ifndef HAVE_SIGACTION
836 /* If the handler was installed with signal() rather than sigaction(),
837 we need to reinstall it. */
838 PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
839#endif
840}
841#endif
842
Guido van Rossum290900a1997-09-26 21:51:21 +0000843/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000844
Guido van Rossum290900a1997-09-26 21:51:21 +0000845static char *
Neal Norwitzbd538702007-04-19 05:52:37 +0000846on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000847{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000848 char *result = NULL;
849 if (completer != NULL) {
850 PyObject *r;
Brett Cannon23b581a2010-05-04 00:52:41 +0000851#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000852 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000853#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000854 rl_attempted_completion_over = 1;
855 r = PyObject_CallFunction(completer, "si", text, state);
856 if (r == NULL)
857 goto error;
858 if (r == Py_None) {
859 result = NULL;
860 }
861 else {
862 char *s = PyString_AsString(r);
863 if (s == NULL)
864 goto error;
865 result = strdup(s);
866 }
867 Py_DECREF(r);
868 goto done;
869 error:
870 PyErr_Clear();
871 Py_XDECREF(r);
872 done:
Brett Cannon23b581a2010-05-04 00:52:41 +0000873#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000874 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000875#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000876 return result;
877 }
878 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000879}
880
Guido van Rossum290900a1997-09-26 21:51:21 +0000881
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000882/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000883 * before calling the normal completer */
884
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000885static char **
Benjamin Peterson0ac0ead2014-01-24 11:44:16 -0500886flex_complete(const char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000887{
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000888#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000889 rl_completion_append_character ='\0';
Antoine Pitroud9ff74e2009-10-26 19:16:46 +0000890#endif
891#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000892 rl_completion_suppress_append = 0;
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000893#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 Py_XDECREF(begidx);
895 Py_XDECREF(endidx);
896 begidx = PyInt_FromLong((long) start);
897 endidx = PyInt_FromLong((long) end);
898 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000899}
900
Guido van Rossum05ac4492003-01-07 20:04:12 +0000901
Guido van Rossum290900a1997-09-26 21:51:21 +0000902/* Helper to initialize GNU readline properly. */
903
904static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000905setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000906{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000907#ifdef SAVE_LOCALE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000908 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
909 if (!saved_locale)
910 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000911#endif
912
R. David Murray7a697252010-12-18 03:52:09 +0000913#ifdef __APPLE__
Victor Stinner63a47472014-07-24 12:22:24 +0200914 /* the libedit readline emulation resets key bindings etc
R. David Murray7a697252010-12-18 03:52:09 +0000915 * when calling rl_initialize. So call it upfront
916 */
917 if (using_libedit_emulation)
918 rl_initialize();
Ned Deily62a19292013-10-12 15:45:25 -0700919
920 /* Detect if libedit's readline emulation uses 0-based
921 * indexing or 1-based indexing.
922 */
923 add_history("1");
924 if (history_get(1) == NULL) {
925 libedit_history_start = 0;
926 } else {
927 libedit_history_start = 1;
928 }
929 clear_history();
R. David Murray7a697252010-12-18 03:52:09 +0000930#endif /* __APPLE__ */
931
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000933
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000935#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000936 /* Allow $if term= in .inputrc to work */
937 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000938#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000939 /* Force rebind of TAB to insert-tab */
940 rl_bind_key('\t', rl_insert);
941 /* Bind both ESC-TAB and ESC-ESC to the completion function */
942 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
943 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin Pantera70c3232016-04-03 02:54:58 +0000944#ifdef HAVE_RL_RESIZE_TERMINAL
945 /* Set up signal handler for window resize */
946 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
947#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 /* Set our hook functions */
Benjamin Peterson0ac0ead2014-01-24 11:44:16 -0500949 rl_startup_hook = on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000950#ifdef HAVE_RL_PRE_INPUT_HOOK
Benjamin Peterson0ac0ead2014-01-24 11:44:16 -0500951 rl_pre_input_hook = on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000952#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000953 /* Set our completion function */
Benjamin Peterson0ac0ead2014-01-24 11:44:16 -0500954 rl_attempted_completion_function = flex_complete;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000955 /* Set Python word break characters */
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200956 completer_word_break_characters =
957 rl_completer_word_break_characters =
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000958 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
959 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000960
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000961 begidx = PyInt_FromLong(0L);
962 endidx = PyInt_FromLong(0L);
Victor Stinner63a47472014-07-24 12:22:24 +0200963
Martin Pantereac40fd2016-08-27 02:54:43 +0000964#ifdef __APPLE__
965 if (!using_libedit_emulation)
Benjamin Petersonb47b54c2014-08-20 17:30:40 -0500966#endif
Martin Pantereac40fd2016-08-27 02:54:43 +0000967 {
968 if (!isatty(STDOUT_FILENO)) {
969 /* Issue #19884: stdout is not a terminal. Disable meta modifier
970 keys to not write the ANSI sequence "\033[1034h" into stdout. On
971 terminals supporting 8 bit characters like TERM=xterm-256color
972 (which is now the default Fedora since Fedora 18), the meta key is
973 used to enable support of 8 bit characters (ANSI sequence
974 "\033[1034h").
975
976 With libedit, this call makes readline() crash. */
977 rl_variable_bind ("enable-meta-key", "off");
978 }
979 }
Victor Stinner63a47472014-07-24 12:22:24 +0200980
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000981 /* Initialize (allows .inputrc to override)
982 *
983 * XXX: A bug in the readline-2.2 library causes a memory leak
984 * inside this function. Nothing we can do about it.
985 */
R. David Murray7a697252010-12-18 03:52:09 +0000986#ifdef __APPLE__
987 if (using_libedit_emulation)
Antoine Pitrou31bc8be2013-05-06 21:51:03 +0200988 rl_read_init_file(NULL);
R. David Murray7a697252010-12-18 03:52:09 +0000989 else
990#endif /* __APPLE__ */
991 rl_initialize();
Victor Stinner63a47472014-07-24 12:22:24 +0200992
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000994}
995
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000996/* Wrapper around GNU readline that handles signals differently. */
997
998
999#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
1000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001001static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001002static void
1003rlhandler(char *text)
1004{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001005 completed_input_string = text;
1006 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001007}
1008
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001009static char *
1010readline_until_enter_or_signal(char *prompt, int *signal)
1011{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001012 char * not_done_reading = "";
1013 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001014
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001015 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001016#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001017 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001018#endif
1019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001020 rl_callback_handler_install (prompt, rlhandler);
1021 FD_ZERO(&selectset);
Brett Cannon23b581a2010-05-04 00:52:41 +00001022
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001023 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001024
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001025 while (completed_input_string == not_done_reading) {
1026 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001028 while (!has_input)
1029 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +00001030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001031 /* [Bug #1552726] Only limit the pause if an input hook has been
1032 defined. */
1033 struct timeval *timeoutp = NULL;
1034 if (PyOS_InputHook)
1035 timeoutp = &timeout;
Martin Pantera70c3232016-04-03 02:54:58 +00001036#ifdef HAVE_RL_RESIZE_TERMINAL
1037 /* Update readline's view of the window size after SIGWINCH */
1038 if (sigwinch_received) {
1039 sigwinch_received = 0;
1040 rl_resize_terminal();
1041 }
1042#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001043 FD_SET(fileno(rl_instream), &selectset);
1044 /* select resets selectset if no input was available */
1045 has_input = select(fileno(rl_instream) + 1, &selectset,
1046 NULL, NULL, timeoutp);
1047 if(PyOS_InputHook) PyOS_InputHook();
1048 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +00001049
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001050 if(has_input > 0) {
1051 rl_callback_read_char();
1052 }
1053 else if (errno == EINTR) {
1054 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001055#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001056 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001057#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001058 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001059#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001060 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +00001061#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001062 if (s < 0) {
1063 rl_free_line_state();
Martin Panterb7036112016-03-22 07:24:05 +00001064#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1065 rl_callback_sigcleanup();
1066#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001067 rl_cleanup_after_signal();
1068 rl_callback_handler_remove();
1069 *signal = 1;
1070 completed_input_string = NULL;
1071 }
1072 }
1073 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001074
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001075 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001076}
1077
1078
1079#else
Guido van Rossum290900a1997-09-26 21:51:21 +00001080
1081/* Interrupt handler */
1082
1083static jmp_buf jbuf;
1084
Guido van Rossum0969d361997-08-05 21:27:50 +00001085/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +00001086static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001087onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +00001088{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001089 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001090}
1091
Guido van Rossum290900a1997-09-26 21:51:21 +00001092
Guido van Rossum0969d361997-08-05 21:27:50 +00001093static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001094readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +00001095{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001096 PyOS_sighandler_t old_inthandler;
1097 char *p;
Brett Cannon23b581a2010-05-04 00:52:41 +00001098
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001099 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +00001100
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001101 old_inthandler = PyOS_setsig(SIGINT, onintr);
1102 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +00001103#ifdef HAVE_SIGRELSE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001104 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
1105 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +00001106#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001107 PyOS_setsig(SIGINT, old_inthandler);
1108 *signal = 1;
1109 return NULL;
1110 }
1111 rl_event_hook = PyOS_InputHook;
1112 p = readline(prompt);
1113 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001114
1115 return p;
1116}
1117#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1118
1119
1120static char *
1121call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1122{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001123 size_t n;
1124 char *p, *q;
1125 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001126
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001127#ifdef SAVE_LOCALE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001128 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1129 if (!saved_locale)
1130 Py_FatalError("not enough memory to save locale");
Nadeem Vawda1efd9822013-02-02 20:52:54 +01001131 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001132#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001133
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001134 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1135 rl_instream = sys_stdin;
1136 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001137#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001138 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001139#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001140 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001141
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001142 p = readline_until_enter_or_signal(prompt, &signal);
Brett Cannon23b581a2010-05-04 00:52:41 +00001143
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001144 /* we got an interrupt signal */
1145 if (signal) {
1146 RESTORE_LOCALE(saved_locale)
1147 return NULL;
1148 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001149
Martin Panterb362f752015-11-02 03:37:02 +00001150 /* We got an EOF, return an empty string. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001151 if (p == NULL) {
1152 p = PyMem_Malloc(1);
1153 if (p != NULL)
1154 *p = '\0';
1155 RESTORE_LOCALE(saved_locale)
1156 return p;
1157 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001158
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001159 /* we have a valid line */
1160 n = strlen(p);
1161 if (n > 0) {
1162 const char *line;
Mark Dickinson0f981282010-08-03 16:54:19 +00001163 int length = _py_get_history_length();
1164 if (length > 0)
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001165#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001166 if (using_libedit_emulation) {
Ned Deily62a19292013-10-12 15:45:25 -07001167 /* handle older 0-based or newer 1-based indexing */
1168 line = history_get(length + libedit_history_start - 1)->line;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001169 } else
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001170#endif /* __APPLE__ */
Mark Dickinson0f981282010-08-03 16:54:19 +00001171 line = history_get(length)->line;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001172 else
1173 line = "";
1174 if (strcmp(p, line))
1175 add_history(p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001176 }
1177 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1178 release the original. */
1179 q = p;
1180 p = PyMem_Malloc(n+2);
1181 if (p != NULL) {
1182 strncpy(p, q, n);
1183 p[n] = '\n';
1184 p[n+1] = '\0';
1185 }
1186 free(q);
1187 RESTORE_LOCALE(saved_locale)
1188 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001189}
1190
Guido van Rossum290900a1997-09-26 21:51:21 +00001191
1192/* Initialize the module */
1193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001194PyDoc_STRVAR(doc_module,
1195"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001196
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001197#ifdef __APPLE__
1198PyDoc_STRVAR(doc_module_le,
1199"Importing this module enables command line editing using libedit readline.");
1200#endif /* __APPLE__ */
1201
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001202PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001203initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001204{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001205 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001206
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001207#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001208 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1209 using_libedit_emulation = 1;
1210 }
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001211
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001212 if (using_libedit_emulation)
1213 m = Py_InitModule4("readline", readline_methods, doc_module_le,
1214 (PyObject *)NULL, PYTHON_API_VERSION);
1215 else
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001216
1217#endif /* __APPLE__ */
1218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001219 m = Py_InitModule4("readline", readline_methods, doc_module,
1220 (PyObject *)NULL, PYTHON_API_VERSION);
1221 if (m == NULL)
1222 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001223
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001224 PyOS_ReadlineFunctionPointer = call_readline;
1225 setup_readline();
Antoine Pitrou06c14972014-11-04 14:52:10 +01001226
1227 PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
1228 PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
Guido van Rossum0969d361997-08-05 21:27:50 +00001229}