• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
5  */
6 
7 #ifndef Py_BUILD_CORE_BUILTIN
8 #  define Py_BUILD_CORE_MODULE 1
9 #endif
10 
11 /* Standard definitions */
12 #include "Python.h"
13 #include "pycore_pylifecycle.h"   // _Py_SetLocaleFromEnv()
14 
15 #include <errno.h>                // errno
16 #include <signal.h>               // SIGWINCH
17 #include <stdlib.h>               // free()
18 #include <string.h>               // strdup()
19 #ifdef HAVE_SYS_SELECT_H
20 #  include <sys/select.h>         // select()
21 #endif
22 
23 #if defined(HAVE_SETLOCALE)
24 /* GNU readline() mistakenly sets the LC_CTYPE locale.
25  * This is evil.  Only the user or the app's main() should do this!
26  * We must save and restore the locale around the rl_initialize() call.
27  */
28 #define SAVE_LOCALE
29 #  include <locale.h>             // setlocale()
30 #endif
31 
32 #ifdef SAVE_LOCALE
33 #  define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
34 #else
35 #  define RESTORE_LOCALE(sl)
36 #endif
37 
38 #ifdef WITH_EDITLINE
39 #  include <editline/readline.h>
40 #else
41 /* GNU readline definitions */
42 #  undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
43 #  include <readline/readline.h>
44 #  include <readline/history.h>
45 #endif
46 
47 #ifdef HAVE_RL_COMPLETION_MATCHES
48 #define completion_matches(x, y) \
49     rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
50 #else
51 #if defined(_RL_FUNCTION_TYPEDEF)
52 extern char **completion_matches(char *, rl_compentry_func_t *);
53 #else
54 
55 #if !defined(__APPLE__)
56 extern char **completion_matches(char *, CPFunction *);
57 #endif
58 #endif
59 #endif
60 
61 /*
62  * It is possible to link the readline module to the readline
63  * emulation library of editline/libedit.
64  *
65  * This emulation library is not 100% API compatible with the "real" readline
66  * and cannot be detected at compile-time,
67  * hence we use a runtime check to detect if the Python readline module is
68  * linked to libedit.
69  *
70  * Currently there is one known API incompatibility:
71  * - 'get_history' has a 1-based index with GNU readline, and a 0-based
72  *   index with older versions of libedit's emulation.
73  * - Note that replace_history and remove_history use a 0-based index
74  *   with both implementations.
75  */
76 static int using_libedit_emulation = 0;
77 static const char libedit_version_tag[] = "EditLine wrapper";
78 
79 static int8_t libedit_history_start = 0;
80 static int8_t libedit_append_replace_history_offset = 0;
81 
82 #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
83 static void
84 on_completion_display_matches_hook(char **matches,
85                                    int num_matches, int max_length);
86 #endif
87 
88 /* Memory allocated for rl_completer_word_break_characters
89    (see issue #17289 for the motivation). */
90 static char *completer_word_break_characters;
91 
92 typedef struct {
93   /* Specify hook functions in Python */
94   PyObject *completion_display_matches_hook;
95   PyObject *startup_hook;
96   PyObject *pre_input_hook;
97 
98   PyObject *completer; /* Specify a word completer in Python */
99   PyObject *begidx;
100   PyObject *endidx;
101 } readlinestate;
102 
103 static inline readlinestate*
get_readline_state(PyObject * module)104 get_readline_state(PyObject *module)
105 {
106     void *state = PyModule_GetState(module);
107     assert(state != NULL);
108     return (readlinestate *)state;
109 }
110 
111 /*[clinic input]
112 module readline
113 [clinic start generated code]*/
114 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad49da781b9c8721]*/
115 
116 static int
readline_clear(PyObject * m)117 readline_clear(PyObject *m)
118 {
119    readlinestate *state = get_readline_state(m);
120    Py_CLEAR(state->completion_display_matches_hook);
121    Py_CLEAR(state->startup_hook);
122    Py_CLEAR(state->pre_input_hook);
123    Py_CLEAR(state->completer);
124    Py_CLEAR(state->begidx);
125    Py_CLEAR(state->endidx);
126    return 0;
127 }
128 
129 static int
readline_traverse(PyObject * m,visitproc visit,void * arg)130 readline_traverse(PyObject *m, visitproc visit, void *arg)
131 {
132     readlinestate *state = get_readline_state(m);
133     Py_VISIT(state->completion_display_matches_hook);
134     Py_VISIT(state->startup_hook);
135     Py_VISIT(state->pre_input_hook);
136     Py_VISIT(state->completer);
137     Py_VISIT(state->begidx);
138     Py_VISIT(state->endidx);
139     return 0;
140 }
141 
142 static void
readline_free(void * m)143 readline_free(void *m)
144 {
145     readline_clear((PyObject *)m);
146 }
147 
148 static PyModuleDef readlinemodule;
149 
150 static inline readlinestate*
get_hook_module_state(void)151 get_hook_module_state(void)
152 {
153     PyObject *mod = PyState_FindModule(&readlinemodule);
154     if (mod == NULL){
155         PyErr_Clear();
156         return NULL;
157     }
158     Py_INCREF(mod);
159     readlinestate *state = get_readline_state(mod);
160     Py_DECREF(mod);
161     return state;
162 }
163 
164 /* Convert to/from multibyte C strings */
165 
166 static PyObject *
encode(PyObject * b)167 encode(PyObject *b)
168 {
169     return PyUnicode_EncodeLocale(b, "surrogateescape");
170 }
171 
172 static PyObject *
decode(const char * s)173 decode(const char *s)
174 {
175     return PyUnicode_DecodeLocale(s, "surrogateescape");
176 }
177 
178 
179 /*
180 Explicitly disable bracketed paste in the interactive interpreter, even if it's
181 set in the inputrc, is enabled by default (eg GNU Readline 8.1), or a user calls
182 readline.read_init_file(). The Python REPL has not implemented bracketed
183 paste support. Also, bracketed mode writes the "\x1b[?2004h" escape sequence
184 into stdout which causes test failures in applications that don't support it.
185 It can still be explicitly enabled by calling readline.parse_and_bind("set
186 enable-bracketed-paste on"). See bpo-42819 for more details.
187 
188 This should be removed if bracketed paste mode is implemented (bpo-39820).
189 */
190 
191 static void
disable_bracketed_paste(void)192 disable_bracketed_paste(void)
193 {
194     if (!using_libedit_emulation) {
195         rl_variable_bind ("enable-bracketed-paste", "off");
196     }
197 }
198 
199 /* Exported function to send one line to readline's init file parser */
200 
201 /*[clinic input]
202 readline.parse_and_bind
203 
204     string: object
205     /
206 
207 Execute the init line provided in the string argument.
208 [clinic start generated code]*/
209 
210 static PyObject *
readline_parse_and_bind(PyObject * module,PyObject * string)211 readline_parse_and_bind(PyObject *module, PyObject *string)
212 /*[clinic end generated code: output=1a1ede8afb9546c1 input=8a28a00bb4d61eec]*/
213 {
214     char *copy;
215     PyObject *encoded = encode(string);
216     if (encoded == NULL) {
217         return NULL;
218     }
219     /* Make a copy -- rl_parse_and_bind() modifies its argument */
220     /* Bernard Herzog */
221     copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded));
222     if (copy == NULL) {
223         Py_DECREF(encoded);
224         return PyErr_NoMemory();
225     }
226     strcpy(copy, PyBytes_AS_STRING(encoded));
227     Py_DECREF(encoded);
228     rl_parse_and_bind(copy);
229     PyMem_Free(copy); /* Free the copy */
230     Py_RETURN_NONE;
231 }
232 
233 /* Exported function to parse a readline init file */
234 
235 /*[clinic input]
236 readline.read_init_file
237 
238     filename as filename_obj: object = None
239     /
240 
241 Execute a readline initialization file.
242 
243 The default filename is the last filename used.
244 [clinic start generated code]*/
245 
246 static PyObject *
readline_read_init_file_impl(PyObject * module,PyObject * filename_obj)247 readline_read_init_file_impl(PyObject *module, PyObject *filename_obj)
248 /*[clinic end generated code: output=8e059b676142831e input=4c80c473e448139d]*/
249 {
250     PyObject *filename_bytes;
251     if (filename_obj != Py_None) {
252         if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
253             return NULL;
254         errno = rl_read_init_file(PyBytes_AS_STRING(filename_bytes));
255         Py_DECREF(filename_bytes);
256     } else
257         errno = rl_read_init_file(NULL);
258     if (errno)
259         return PyErr_SetFromErrno(PyExc_OSError);
260     disable_bracketed_paste();
261     Py_RETURN_NONE;
262 }
263 
264 /* Exported function to load a readline history file */
265 
266 /*[clinic input]
267 readline.read_history_file
268 
269     filename as filename_obj: object = None
270     /
271 
272 Load a readline history file.
273 
274 The default filename is ~/.history.
275 [clinic start generated code]*/
276 
277 static PyObject *
readline_read_history_file_impl(PyObject * module,PyObject * filename_obj)278 readline_read_history_file_impl(PyObject *module, PyObject *filename_obj)
279 /*[clinic end generated code: output=66a951836fb54fbb input=3d29d755b7e6932e]*/
280 {
281     PyObject *filename_bytes;
282     if (filename_obj != Py_None) {
283         if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
284             return NULL;
285         errno = read_history(PyBytes_AS_STRING(filename_bytes));
286         Py_DECREF(filename_bytes);
287     } else
288         errno = read_history(NULL);
289     if (errno)
290         return PyErr_SetFromErrno(PyExc_OSError);
291     Py_RETURN_NONE;
292 }
293 
294 static int _history_length = -1; /* do not truncate history by default */
295 
296 /* Exported function to save a readline history file */
297 
298 /*[clinic input]
299 readline.write_history_file
300 
301     filename as filename_obj: object = None
302     /
303 
304 Save a readline history file.
305 
306 The default filename is ~/.history.
307 [clinic start generated code]*/
308 
309 static PyObject *
readline_write_history_file_impl(PyObject * module,PyObject * filename_obj)310 readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
311 /*[clinic end generated code: output=fbcad13d8ef59ae6 input=28a8e062fe363703]*/
312 {
313     PyObject *filename_bytes;
314     const char *filename;
315     int err;
316     if (filename_obj != Py_None) {
317         if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
318             return NULL;
319         filename = PyBytes_AS_STRING(filename_bytes);
320     } else {
321         filename_bytes = NULL;
322         filename = NULL;
323     }
324     errno = err = write_history(filename);
325     if (!err && _history_length >= 0)
326         history_truncate_file(filename, _history_length);
327     Py_XDECREF(filename_bytes);
328     errno = err;
329     if (errno)
330         return PyErr_SetFromErrno(PyExc_OSError);
331     Py_RETURN_NONE;
332 }
333 
334 #ifdef HAVE_RL_APPEND_HISTORY
335 /* Exported function to save part of a readline history file */
336 
337 /*[clinic input]
338 readline.append_history_file
339 
340     nelements: int
341     filename as filename_obj: object = None
342     /
343 
344 Append the last nelements items of the history list to file.
345 
346 The default filename is ~/.history.
347 [clinic start generated code]*/
348 
349 static PyObject *
readline_append_history_file_impl(PyObject * module,int nelements,PyObject * filename_obj)350 readline_append_history_file_impl(PyObject *module, int nelements,
351                                   PyObject *filename_obj)
352 /*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
353 {
354     PyObject *filename_bytes;
355     const char *filename;
356     int err;
357     if (filename_obj != Py_None) {
358         if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
359             return NULL;
360         filename = PyBytes_AS_STRING(filename_bytes);
361     } else {
362         filename_bytes = NULL;
363         filename = NULL;
364     }
365     errno = err = append_history(
366         nelements - libedit_append_replace_history_offset, filename);
367     if (!err && _history_length >= 0)
368         history_truncate_file(filename, _history_length);
369     Py_XDECREF(filename_bytes);
370     errno = err;
371     if (errno)
372         return PyErr_SetFromErrno(PyExc_OSError);
373     Py_RETURN_NONE;
374 }
375 #endif
376 
377 
378 /* Set history length */
379 
380 /*[clinic input]
381 readline.set_history_length
382 
383     length: int
384     /
385 
386 Set the maximal number of lines which will be written to the history file.
387 
388 A negative length is used to inhibit history truncation.
389 [clinic start generated code]*/
390 
391 static PyObject *
readline_set_history_length_impl(PyObject * module,int length)392 readline_set_history_length_impl(PyObject *module, int length)
393 /*[clinic end generated code: output=e161a53e45987dc7 input=b8901bf16488b760]*/
394 {
395     _history_length = length;
396     Py_RETURN_NONE;
397 }
398 
399 /* Get history length */
400 
401 /*[clinic input]
402 readline.get_history_length
403 
404 Return the maximum number of lines that will be written to the history file.
405 [clinic start generated code]*/
406 
407 static PyObject *
readline_get_history_length_impl(PyObject * module)408 readline_get_history_length_impl(PyObject *module)
409 /*[clinic end generated code: output=83a2eeae35b6d2b9 input=5dce2eeba4327817]*/
410 {
411     return PyLong_FromLong(_history_length);
412 }
413 
414 /* Generic hook function setter */
415 
416 static PyObject *
set_hook(const char * funcname,PyObject ** hook_var,PyObject * function)417 set_hook(const char *funcname, PyObject **hook_var, PyObject *function)
418 {
419     if (function == Py_None) {
420         Py_CLEAR(*hook_var);
421     }
422     else if (PyCallable_Check(function)) {
423         Py_XSETREF(*hook_var, Py_NewRef(function));
424     }
425     else {
426         PyErr_Format(PyExc_TypeError,
427                      "set_%.50s(func): argument not callable",
428                      funcname);
429         return NULL;
430     }
431     Py_RETURN_NONE;
432 }
433 
434 /*[clinic input]
435 readline.set_completion_display_matches_hook
436 
437     function: object = None
438     /
439 
440 Set or remove the completion display function.
441 
442 The function is called as
443   function(substitution, [matches], longest_match_length)
444 once each time matches need to be displayed.
445 [clinic start generated code]*/
446 
447 static PyObject *
readline_set_completion_display_matches_hook_impl(PyObject * module,PyObject * function)448 readline_set_completion_display_matches_hook_impl(PyObject *module,
449                                                   PyObject *function)
450 /*[clinic end generated code: output=516e5cb8db75a328 input=4f0bfd5ab0179a26]*/
451 {
452     readlinestate *state = get_readline_state(module);
453     PyObject *result = set_hook("completion_display_matches_hook",
454                     &state->completion_display_matches_hook,
455                     function);
456 #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
457     /* We cannot set this hook globally, since it replaces the
458        default completion display. */
459     rl_completion_display_matches_hook =
460         state->completion_display_matches_hook ?
461 #if defined(HAVE_RL_COMPDISP_FUNC_T)
462         (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
463 #else
464         (VFunction *)on_completion_display_matches_hook : 0;
465 #endif
466 #endif
467     return result;
468 
469 }
470 
471 /*[clinic input]
472 readline.set_startup_hook
473 
474     function: object = None
475     /
476 
477 Set or remove the function invoked by the rl_startup_hook callback.
478 
479 The function is called with no arguments just
480 before readline prints the first prompt.
481 [clinic start generated code]*/
482 
483 static PyObject *
readline_set_startup_hook_impl(PyObject * module,PyObject * function)484 readline_set_startup_hook_impl(PyObject *module, PyObject *function)
485 /*[clinic end generated code: output=02cd0e0c4fa082ad input=7783b4334b26d16d]*/
486 {
487     readlinestate *state = get_readline_state(module);
488     return set_hook("startup_hook", &state->startup_hook,
489             function);
490 }
491 
492 #ifdef HAVE_RL_PRE_INPUT_HOOK
493 
494 /* Set pre-input hook */
495 
496 /*[clinic input]
497 readline.set_pre_input_hook
498 
499     function: object = None
500     /
501 
502 Set or remove the function invoked by the rl_pre_input_hook callback.
503 
504 The function is called with no arguments after the first prompt
505 has been printed and just before readline starts reading input
506 characters.
507 [clinic start generated code]*/
508 
509 static PyObject *
readline_set_pre_input_hook_impl(PyObject * module,PyObject * function)510 readline_set_pre_input_hook_impl(PyObject *module, PyObject *function)
511 /*[clinic end generated code: output=fe1a96505096f464 input=4f3eaeaf7ce1fdbe]*/
512 {
513     readlinestate *state = get_readline_state(module);
514     return set_hook("pre_input_hook", &state->pre_input_hook,
515             function);
516 }
517 #endif
518 
519 
520 /* Get the completion type for the scope of the tab-completion */
521 
522 /*[clinic input]
523 readline.get_completion_type
524 
525 Get the type of completion being attempted.
526 [clinic start generated code]*/
527 
528 static PyObject *
readline_get_completion_type_impl(PyObject * module)529 readline_get_completion_type_impl(PyObject *module)
530 /*[clinic end generated code: output=5c54d58a04997c07 input=04b92bc7a82dac91]*/
531 {
532   return PyLong_FromLong(rl_completion_type);
533 }
534 
535 /* Get the beginning index for the scope of the tab-completion */
536 
537 /*[clinic input]
538 readline.get_begidx
539 
540 Get the beginning index of the completion scope.
541 [clinic start generated code]*/
542 
543 static PyObject *
readline_get_begidx_impl(PyObject * module)544 readline_get_begidx_impl(PyObject *module)
545 /*[clinic end generated code: output=362616ee8ed1b2b1 input=e083b81c8eb4bac3]*/
546 {
547     readlinestate *state = get_readline_state(module);
548     return Py_NewRef(state->begidx);
549 }
550 
551 /* Get the ending index for the scope of the tab-completion */
552 
553 /*[clinic input]
554 readline.get_endidx
555 
556 Get the ending index of the completion scope.
557 [clinic start generated code]*/
558 
559 static PyObject *
readline_get_endidx_impl(PyObject * module)560 readline_get_endidx_impl(PyObject *module)
561 /*[clinic end generated code: output=7f763350b12d7517 input=d4c7e34a625fd770]*/
562 {
563     readlinestate *state = get_readline_state(module);
564     return Py_NewRef(state->endidx);
565 }
566 
567 /* Set the tab-completion word-delimiters that readline uses */
568 
569 /*[clinic input]
570 readline.set_completer_delims
571 
572     string: object
573     /
574 
575 Set the word delimiters for completion.
576 [clinic start generated code]*/
577 
578 static PyObject *
readline_set_completer_delims(PyObject * module,PyObject * string)579 readline_set_completer_delims(PyObject *module, PyObject *string)
580 /*[clinic end generated code: output=4305b266106c4f1f input=ae945337ebd01e20]*/
581 {
582     char *break_chars;
583     PyObject *encoded = encode(string);
584     if (encoded == NULL) {
585         return NULL;
586     }
587     /* Keep a reference to the allocated memory in the module state in case
588        some other module modifies rl_completer_word_break_characters
589        (see issue #17289). */
590     break_chars = strdup(PyBytes_AS_STRING(encoded));
591     Py_DECREF(encoded);
592     if (break_chars) {
593         free(completer_word_break_characters);
594         completer_word_break_characters = break_chars;
595 #ifdef WITH_EDITLINE
596         rl_basic_word_break_characters = break_chars;
597 #else
598         if (using_libedit_emulation) {
599             rl_basic_word_break_characters = break_chars;
600         }
601 #endif
602         rl_completer_word_break_characters = break_chars;
603         Py_RETURN_NONE;
604     }
605     else
606         return PyErr_NoMemory();
607 }
608 
609 /* _py_free_history_entry: Utility function to free a history entry. */
610 
611 #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
612 
613 /* Readline version >= 5.0 introduced a timestamp field into the history entry
614    structure; this needs to be freed to avoid a memory leak.  This version of
615    readline also introduced the handy 'free_history_entry' function, which
616    takes care of the timestamp. */
617 
618 static void
_py_free_history_entry(HIST_ENTRY * entry)619 _py_free_history_entry(HIST_ENTRY *entry)
620 {
621     histdata_t data = free_history_entry(entry);
622     free(data);
623 }
624 
625 #else
626 
627 /* No free_history_entry function;  free everything manually. */
628 
629 static void
_py_free_history_entry(HIST_ENTRY * entry)630 _py_free_history_entry(HIST_ENTRY *entry)
631 {
632     if (entry->line)
633         free((void *)entry->line);
634     if (entry->data)
635         free(entry->data);
636     free(entry);
637 }
638 
639 #endif
640 
641 /*[clinic input]
642 readline.remove_history_item
643 
644     pos as entry_number: int
645     /
646 
647 Remove history item given by its zero-based position.
648 [clinic start generated code]*/
649 
650 static PyObject *
readline_remove_history_item_impl(PyObject * module,int entry_number)651 readline_remove_history_item_impl(PyObject *module, int entry_number)
652 /*[clinic end generated code: output=ab114f029208c7e8 input=f248beb720ff1838]*/
653 {
654     HIST_ENTRY *entry;
655 
656     if (entry_number < 0) {
657         PyErr_SetString(PyExc_ValueError,
658                         "History index cannot be negative");
659         return NULL;
660     }
661     entry = remove_history(entry_number);
662     if (!entry) {
663         PyErr_Format(PyExc_ValueError,
664                      "No history item at position %d",
665                       entry_number);
666         return NULL;
667     }
668     /* free memory allocated for the history entry */
669     _py_free_history_entry(entry);
670     Py_RETURN_NONE;
671 }
672 
673 /*[clinic input]
674 readline.replace_history_item
675 
676     pos as entry_number: int
677     line: unicode
678     /
679 
680 Replaces history item given by its position with contents of line.
681 
682 pos is zero-based.
683 [clinic start generated code]*/
684 
685 static PyObject *
readline_replace_history_item_impl(PyObject * module,int entry_number,PyObject * line)686 readline_replace_history_item_impl(PyObject *module, int entry_number,
687                                    PyObject *line)
688 /*[clinic end generated code: output=f8cec2770ca125eb input=368bb66fe5ee5222]*/
689 {
690     PyObject *encoded;
691     HIST_ENTRY *old_entry;
692 
693     if (entry_number < 0) {
694         PyErr_SetString(PyExc_ValueError,
695                         "History index cannot be negative");
696         return NULL;
697     }
698     encoded = encode(line);
699     if (encoded == NULL) {
700         return NULL;
701     }
702     old_entry = replace_history_entry(
703         entry_number + libedit_append_replace_history_offset,
704         PyBytes_AS_STRING(encoded), (void *)NULL);
705     Py_DECREF(encoded);
706     if (!old_entry) {
707         PyErr_Format(PyExc_ValueError,
708                      "No history item at position %d",
709                      entry_number);
710         return NULL;
711     }
712     /* free memory allocated for the old history entry */
713     _py_free_history_entry(old_entry);
714     Py_RETURN_NONE;
715 }
716 
717 /* Add a line to the history buffer */
718 
719 /*[clinic input]
720 readline.add_history
721 
722     string: object
723     /
724 
725 Add an item to the history buffer.
726 [clinic start generated code]*/
727 
728 static PyObject *
readline_add_history(PyObject * module,PyObject * string)729 readline_add_history(PyObject *module, PyObject *string)
730 /*[clinic end generated code: output=b107b7e8106e803d input=e57c1cf6bc68d7e3]*/
731 {
732     PyObject *encoded = encode(string);
733     if (encoded == NULL) {
734         return NULL;
735     }
736     add_history(PyBytes_AS_STRING(encoded));
737     Py_DECREF(encoded);
738     Py_RETURN_NONE;
739 }
740 
741 static int should_auto_add_history = 1;
742 
743 /* Enable or disable automatic history */
744 
745 /*[clinic input]
746 readline.set_auto_history
747 
748     enabled as _should_auto_add_history: bool
749     /
750 
751 Enables or disables automatic history.
752 [clinic start generated code]*/
753 
754 static PyObject *
readline_set_auto_history_impl(PyObject * module,int _should_auto_add_history)755 readline_set_auto_history_impl(PyObject *module,
756                                int _should_auto_add_history)
757 /*[clinic end generated code: output=619c6968246fd82b input=3d413073a1a03355]*/
758 {
759     should_auto_add_history = _should_auto_add_history;
760     Py_RETURN_NONE;
761 }
762 
763 
764 /* Get the tab-completion word-delimiters that readline uses */
765 
766 /*[clinic input]
767 readline.get_completer_delims
768 
769 Get the word delimiters for completion.
770 [clinic start generated code]*/
771 
772 static PyObject *
readline_get_completer_delims_impl(PyObject * module)773 readline_get_completer_delims_impl(PyObject *module)
774 /*[clinic end generated code: output=6b060280fa68ef43 input=e36eb14fb8a1f08a]*/
775 {
776     return decode(rl_completer_word_break_characters);
777 }
778 
779 /* Set the completer function */
780 
781 /*[clinic input]
782 readline.set_completer
783 
784     function: object = None
785     /
786 
787 Set or remove the completer function.
788 
789 The function is called as function(text, state),
790 for state in 0, 1, 2, ..., until it returns a non-string.
791 It should return the next possible completion starting with 'text'.
792 [clinic start generated code]*/
793 
794 static PyObject *
readline_set_completer_impl(PyObject * module,PyObject * function)795 readline_set_completer_impl(PyObject *module, PyObject *function)
796 /*[clinic end generated code: output=171a2a60f81d3204 input=51e81e13118eb877]*/
797 {
798     readlinestate *state = get_readline_state(module);
799     return set_hook("completer", &state->completer, function);
800 }
801 
802 /*[clinic input]
803 readline.get_completer
804 
805 Get the current completer function.
806 [clinic start generated code]*/
807 
808 static PyObject *
readline_get_completer_impl(PyObject * module)809 readline_get_completer_impl(PyObject *module)
810 /*[clinic end generated code: output=6e6bbd8226d14475 input=6457522e56d70d13]*/
811 {
812     readlinestate *state = get_readline_state(module);
813     if (state->completer == NULL) {
814         Py_RETURN_NONE;
815     }
816     return Py_NewRef(state->completer);
817 }
818 
819 /* Private function to get current length of history.  XXX It may be
820  * possible to replace this with a direct use of history_length instead,
821  * but it's not clear whether BSD's libedit keeps history_length up to date.
822  * See issue #8065.*/
823 
824 static int
_py_get_history_length(void)825 _py_get_history_length(void)
826 {
827     HISTORY_STATE *hist_st = history_get_history_state();
828     int length = hist_st->length;
829     /* the history docs don't say so, but the address of hist_st changes each
830        time history_get_history_state is called which makes me think it's
831        freshly malloc'd memory...  on the other hand, the address of the last
832        line stays the same as long as history isn't extended, so it appears to
833        be malloc'd but managed by the history package... */
834     free(hist_st);
835     return length;
836 }
837 
838 /* Exported function to get any element of history */
839 
840 /*[clinic input]
841 readline.get_history_item
842 
843     index as idx: int
844     /
845 
846 Return the current contents of history item at one-based index.
847 [clinic start generated code]*/
848 
849 static PyObject *
readline_get_history_item_impl(PyObject * module,int idx)850 readline_get_history_item_impl(PyObject *module, int idx)
851 /*[clinic end generated code: output=83d3e53ea5f34b3d input=8adf5c80e6c7ff2b]*/
852 {
853     HIST_ENTRY *hist_ent;
854 
855     if (using_libedit_emulation) {
856         /* Older versions of libedit's readline emulation
857          * use 0-based indexes, while readline and newer
858          * versions of libedit use 1-based indexes.
859          */
860         int length = _py_get_history_length();
861 
862         idx = idx - 1 + libedit_history_start;
863 
864         /*
865          * Apple's readline emulation crashes when
866          * the index is out of range, therefore
867          * test for that and fail gracefully.
868          */
869         if (idx < (0 + libedit_history_start)
870                 || idx >= (length + libedit_history_start)) {
871             Py_RETURN_NONE;
872         }
873     }
874     if ((hist_ent = history_get(idx)))
875         return decode(hist_ent->line);
876     else {
877         Py_RETURN_NONE;
878     }
879 }
880 
881 /* Exported function to get current length of history */
882 
883 /*[clinic input]
884 readline.get_current_history_length
885 
886 Return the current (not the maximum) length of history.
887 [clinic start generated code]*/
888 
889 static PyObject *
readline_get_current_history_length_impl(PyObject * module)890 readline_get_current_history_length_impl(PyObject *module)
891 /*[clinic end generated code: output=436b294f12ba1e3f input=9cb3f431a68d071f]*/
892 {
893     return PyLong_FromLong((long)_py_get_history_length());
894 }
895 
896 /* Exported function to read the current line buffer */
897 
898 /*[clinic input]
899 readline.get_line_buffer
900 
901 Return the current contents of the line buffer.
902 [clinic start generated code]*/
903 
904 static PyObject *
readline_get_line_buffer_impl(PyObject * module)905 readline_get_line_buffer_impl(PyObject *module)
906 /*[clinic end generated code: output=d22f9025ecad80e4 input=5f5fbc0d12c69412]*/
907 {
908     return decode(rl_line_buffer);
909 }
910 
911 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
912 
913 /* Exported function to clear the current history */
914 
915 /*[clinic input]
916 readline.clear_history
917 
918 Clear the current readline history.
919 [clinic start generated code]*/
920 
921 static PyObject *
readline_clear_history_impl(PyObject * module)922 readline_clear_history_impl(PyObject *module)
923 /*[clinic end generated code: output=1f2dbb0dfa5d5ebb input=208962c4393f5d16]*/
924 {
925     clear_history();
926     Py_RETURN_NONE;
927 }
928 #endif
929 
930 
931 /* Exported function to insert text into the line buffer */
932 
933 /*[clinic input]
934 readline.insert_text
935 
936     string: object
937     /
938 
939 Insert text into the line buffer at the cursor position.
940 [clinic start generated code]*/
941 
942 static PyObject *
readline_insert_text(PyObject * module,PyObject * string)943 readline_insert_text(PyObject *module, PyObject *string)
944 /*[clinic end generated code: output=23d792821d320c19 input=bc96c3c848d5ccb5]*/
945 {
946     PyObject *encoded = encode(string);
947     if (encoded == NULL) {
948         return NULL;
949     }
950     rl_insert_text(PyBytes_AS_STRING(encoded));
951     Py_DECREF(encoded);
952     Py_RETURN_NONE;
953 }
954 
955 /* Redisplay the line buffer */
956 
957 /*[clinic input]
958 readline.redisplay
959 
960 Change what's displayed on the screen to reflect contents of the line buffer.
961 [clinic start generated code]*/
962 
963 static PyObject *
readline_redisplay_impl(PyObject * module)964 readline_redisplay_impl(PyObject *module)
965 /*[clinic end generated code: output=a8b9725827c3c34b input=b485151058d75edc]*/
966 {
967     rl_redisplay();
968     Py_RETURN_NONE;
969 }
970 
971 #include "clinic/readline.c.h"
972 
973 /* Table of functions exported by the module */
974 
975 static struct PyMethodDef readline_methods[] =
976 {
977     READLINE_PARSE_AND_BIND_METHODDEF
978     READLINE_GET_LINE_BUFFER_METHODDEF
979     READLINE_INSERT_TEXT_METHODDEF
980     READLINE_REDISPLAY_METHODDEF
981     READLINE_READ_INIT_FILE_METHODDEF
982     READLINE_READ_HISTORY_FILE_METHODDEF
983     READLINE_WRITE_HISTORY_FILE_METHODDEF
984 #ifdef HAVE_RL_APPEND_HISTORY
985     READLINE_APPEND_HISTORY_FILE_METHODDEF
986 #endif
987     READLINE_GET_HISTORY_ITEM_METHODDEF
988     READLINE_GET_CURRENT_HISTORY_LENGTH_METHODDEF
989     READLINE_SET_HISTORY_LENGTH_METHODDEF
990     READLINE_GET_HISTORY_LENGTH_METHODDEF
991     READLINE_SET_COMPLETER_METHODDEF
992     READLINE_GET_COMPLETER_METHODDEF
993     READLINE_GET_COMPLETION_TYPE_METHODDEF
994     READLINE_GET_BEGIDX_METHODDEF
995     READLINE_GET_ENDIDX_METHODDEF
996     READLINE_SET_COMPLETER_DELIMS_METHODDEF
997     READLINE_SET_AUTO_HISTORY_METHODDEF
998     READLINE_ADD_HISTORY_METHODDEF
999     READLINE_REMOVE_HISTORY_ITEM_METHODDEF
1000     READLINE_REPLACE_HISTORY_ITEM_METHODDEF
1001     READLINE_GET_COMPLETER_DELIMS_METHODDEF
1002     READLINE_SET_COMPLETION_DISPLAY_MATCHES_HOOK_METHODDEF
1003     READLINE_SET_STARTUP_HOOK_METHODDEF
1004 #ifdef HAVE_RL_PRE_INPUT_HOOK
1005     READLINE_SET_PRE_INPUT_HOOK_METHODDEF
1006 #endif
1007 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
1008     READLINE_CLEAR_HISTORY_METHODDEF
1009 #endif
1010     {0, 0}
1011 };
1012 
1013 
1014 /* C function to call the Python hooks. */
1015 
1016 static int
on_hook(PyObject * func)1017 on_hook(PyObject *func)
1018 {
1019     int result = 0;
1020     if (func != NULL) {
1021         PyObject *r;
1022         r = PyObject_CallNoArgs(func);
1023         if (r == NULL)
1024             goto error;
1025         if (r == Py_None)
1026             result = 0;
1027         else {
1028             result = PyLong_AsInt(r);
1029             if (result == -1 && PyErr_Occurred())
1030                 goto error;
1031         }
1032         Py_DECREF(r);
1033         goto done;
1034       error:
1035         PyErr_Clear();
1036         Py_XDECREF(r);
1037       done:
1038         return result;
1039     }
1040     return result;
1041 }
1042 
1043 static int
1044 #if defined(_RL_FUNCTION_TYPEDEF) || !defined(Py_RL_STARTUP_HOOK_TAKES_ARGS)
on_startup_hook(void)1045 on_startup_hook(void)
1046 #else
1047 on_startup_hook(const char *Py_UNUSED(text), int Py_UNUSED(state))
1048 #endif
1049 {
1050     int r;
1051     PyGILState_STATE gilstate = PyGILState_Ensure();
1052     readlinestate *state = get_hook_module_state();
1053     if (state == NULL) {
1054         PyGILState_Release(gilstate);
1055         return -1;
1056     }
1057     r = on_hook(state->startup_hook);
1058     PyGILState_Release(gilstate);
1059     return r;
1060 }
1061 
1062 #ifdef HAVE_RL_PRE_INPUT_HOOK
1063 static int
1064 #if defined(_RL_FUNCTION_TYPEDEF) || !defined(Py_RL_STARTUP_HOOK_TAKES_ARGS)
on_pre_input_hook(void)1065 on_pre_input_hook(void)
1066 #else
1067 on_pre_input_hook(const char *Py_UNUSED(text), int Py_UNUSED(state))
1068 #endif
1069 {
1070     int r;
1071     PyGILState_STATE gilstate = PyGILState_Ensure();
1072     readlinestate *state = get_hook_module_state();
1073     if (state == NULL) {
1074         PyGILState_Release(gilstate);
1075         return -1;
1076     }
1077     r = on_hook(state->pre_input_hook);
1078     PyGILState_Release(gilstate);
1079     return r;
1080 }
1081 #endif
1082 
1083 
1084 /* C function to call the Python completion_display_matches */
1085 
1086 #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
1087 static void
on_completion_display_matches_hook(char ** matches,int num_matches,int max_length)1088 on_completion_display_matches_hook(char **matches,
1089                                    int num_matches, int max_length)
1090 {
1091     int i;
1092     PyObject *sub, *m=NULL, *s=NULL, *r=NULL;
1093     PyGILState_STATE gilstate = PyGILState_Ensure();
1094     readlinestate *state = get_hook_module_state();
1095     if (state == NULL) {
1096         PyGILState_Release(gilstate);
1097         return;
1098     }
1099     m = PyList_New(num_matches);
1100     if (m == NULL)
1101         goto error;
1102     for (i = 0; i < num_matches; i++) {
1103         s = decode(matches[i+1]);
1104         if (s == NULL)
1105             goto error;
1106         PyList_SET_ITEM(m, i, s);
1107     }
1108     sub = decode(matches[0]);
1109     r = PyObject_CallFunction(state->completion_display_matches_hook,
1110                               "NNi", sub, m, max_length);
1111 
1112     m=NULL;
1113 
1114     if (r == NULL ||
1115         (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
1116         goto error;
1117     }
1118     Py_CLEAR(r);
1119 
1120     if (0) {
1121     error:
1122         PyErr_Clear();
1123         Py_XDECREF(m);
1124         Py_XDECREF(r);
1125     }
1126     PyGILState_Release(gilstate);
1127 }
1128 
1129 #endif
1130 
1131 #ifdef HAVE_RL_RESIZE_TERMINAL
1132 static volatile sig_atomic_t sigwinch_received;
1133 static PyOS_sighandler_t sigwinch_ohandler;
1134 
1135 static void
readline_sigwinch_handler(int signum)1136 readline_sigwinch_handler(int signum)
1137 {
1138     sigwinch_received = 1;
1139     if (sigwinch_ohandler &&
1140             sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
1141         sigwinch_ohandler(signum);
1142 
1143 #ifndef HAVE_SIGACTION
1144     /* If the handler was installed with signal() rather than sigaction(),
1145     we need to reinstall it. */
1146     PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1147 #endif
1148 }
1149 #endif
1150 
1151 /* C function to call the Python completer. */
1152 
1153 static char *
on_completion(const char * text,int state)1154 on_completion(const char *text, int state)
1155 {
1156     char *result = NULL;
1157     PyGILState_STATE gilstate = PyGILState_Ensure();
1158     readlinestate *module_state = get_hook_module_state();
1159     if (module_state == NULL) {
1160         PyGILState_Release(gilstate);
1161         return NULL;
1162     }
1163     if (module_state->completer != NULL) {
1164         PyObject *r = NULL, *t;
1165         rl_attempted_completion_over = 1;
1166         t = decode(text);
1167         r = PyObject_CallFunction(module_state->completer, "Ni", t, state);
1168         if (r == NULL)
1169             goto error;
1170         if (r == Py_None) {
1171             result = NULL;
1172         }
1173         else {
1174             PyObject *encoded = encode(r);
1175             if (encoded == NULL)
1176                 goto error;
1177             result = strdup(PyBytes_AS_STRING(encoded));
1178             Py_DECREF(encoded);
1179         }
1180         Py_DECREF(r);
1181         goto done;
1182       error:
1183         PyErr_Clear();
1184         Py_XDECREF(r);
1185       done:
1186         PyGILState_Release(gilstate);
1187         return result;
1188     }
1189     PyGILState_Release(gilstate);
1190     return result;
1191 }
1192 
1193 
1194 /* A more flexible constructor that saves the "begidx" and "endidx"
1195  * before calling the normal completer */
1196 
1197 static char **
flex_complete(const char * text,int start,int end)1198 flex_complete(const char *text, int start, int end)
1199 {
1200     char **result;
1201     char saved;
1202     size_t start_size, end_size;
1203     wchar_t *s;
1204     PyGILState_STATE gilstate = PyGILState_Ensure();
1205     readlinestate *state = get_hook_module_state();
1206 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
1207     rl_completion_append_character ='\0';
1208 #endif
1209 #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
1210     rl_completion_suppress_append = 0;
1211 #endif
1212 
1213     saved = rl_line_buffer[start];
1214     rl_line_buffer[start] = 0;
1215     s = Py_DecodeLocale(rl_line_buffer, &start_size);
1216     rl_line_buffer[start] = saved;
1217     if (s == NULL) {
1218         goto done;
1219     }
1220     PyMem_RawFree(s);
1221     saved = rl_line_buffer[end];
1222     rl_line_buffer[end] = 0;
1223     s = Py_DecodeLocale(rl_line_buffer + start, &end_size);
1224     rl_line_buffer[end] = saved;
1225     if (s == NULL) {
1226         goto done;
1227     }
1228     PyMem_RawFree(s);
1229     start = (int)start_size;
1230     end = start + (int)end_size;
1231 
1232 done:
1233     if (state) {
1234         Py_XDECREF(state->begidx);
1235         Py_XDECREF(state->endidx);
1236         state->begidx = PyLong_FromLong((long) start);
1237         state->endidx = PyLong_FromLong((long) end);
1238     }
1239     result = completion_matches((char *)text, *on_completion);
1240     PyGILState_Release(gilstate);
1241     return result;
1242 }
1243 
1244 
1245 /* Helper to initialize GNU readline properly.
1246    Return -1 on memory allocation failure, return 0 on success. */
1247 static int
setup_readline(readlinestate * mod_state)1248 setup_readline(readlinestate *mod_state)
1249 {
1250 #ifdef SAVE_LOCALE
1251     char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1252     if (!saved_locale) {
1253         return -1;
1254     }
1255 #endif
1256 
1257     /* The name must be defined before initialization */
1258     rl_readline_name = "python";
1259 
1260     /* the libedit readline emulation resets key bindings etc
1261      * when calling rl_initialize.  So call it upfront
1262      */
1263     if (using_libedit_emulation)
1264         rl_initialize();
1265 
1266     /* Detect if libedit's readline emulation uses 0-based
1267      * indexing or 1-based indexing.
1268      */
1269     add_history("1");
1270     if (history_get(1) == NULL) {
1271         libedit_history_start = 0;
1272     } else {
1273         libedit_history_start = 1;
1274     }
1275     /* Some libedit implementations use 1 based indexing on
1276      * replace_history_entry where libreadline uses 0 based.
1277      * The API our module presents is supposed to be 0 based.
1278      * It's a mad mad mad mad world.
1279      */
1280     {
1281         add_history("2");
1282         HIST_ENTRY *old_entry = replace_history_entry(1, "X", NULL);
1283         _py_free_history_entry(old_entry);
1284         HIST_ENTRY *item = history_get(libedit_history_start);
1285         if (item && item->line && strcmp(item->line, "X")) {
1286             libedit_append_replace_history_offset = 0;
1287         } else {
1288             libedit_append_replace_history_offset = 1;
1289         }
1290     }
1291     clear_history();
1292 
1293     using_history();
1294 
1295     /* Force rebind of TAB to insert-tab */
1296     rl_bind_key('\t', rl_insert);
1297     /* Bind both ESC-TAB and ESC-ESC to the completion function */
1298     rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
1299     rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
1300 #ifdef HAVE_RL_RESIZE_TERMINAL
1301     /* Set up signal handler for window resize */
1302     sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1303 #endif
1304     /* Set our hook functions */
1305     rl_startup_hook = on_startup_hook;
1306 #ifdef HAVE_RL_PRE_INPUT_HOOK
1307     rl_pre_input_hook = on_pre_input_hook;
1308 #endif
1309     /* Set our completion function */
1310     rl_attempted_completion_function = flex_complete;
1311     /* Set Python word break characters */
1312     completer_word_break_characters =
1313         strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
1314         /* All nonalphanums except '.' */
1315 #ifdef WITH_EDITLINE
1316     // libedit uses rl_basic_word_break_characters instead of
1317     // rl_completer_word_break_characters as complete delimiter
1318     rl_basic_word_break_characters = completer_word_break_characters;
1319 #else
1320     if (using_libedit_emulation) {
1321         rl_basic_word_break_characters = completer_word_break_characters;
1322     }
1323 #endif
1324     rl_completer_word_break_characters = completer_word_break_characters;
1325 
1326     mod_state->begidx = PyLong_FromLong(0L);
1327     mod_state->endidx = PyLong_FromLong(0L);
1328 
1329     if (!using_libedit_emulation)
1330     {
1331         if (!isatty(STDOUT_FILENO)) {
1332             /* Issue #19884: stdout is not a terminal. Disable meta modifier
1333                keys to not write the ANSI sequence "\033[1034h" into stdout. On
1334                terminals supporting 8 bit characters like TERM=xterm-256color
1335                (which is now the default Fedora since Fedora 18), the meta key is
1336                used to enable support of 8 bit characters (ANSI sequence
1337                "\033[1034h").
1338 
1339                With libedit, this call makes readline() crash. */
1340             rl_variable_bind ("enable-meta-key", "off");
1341         }
1342     }
1343 
1344     /* Initialize (allows .inputrc to override)
1345      *
1346      * XXX: A bug in the readline-2.2 library causes a memory leak
1347      * inside this function.  Nothing we can do about it.
1348      */
1349     if (using_libedit_emulation)
1350         rl_read_init_file(NULL);
1351     else
1352         rl_initialize();
1353 
1354     disable_bracketed_paste();
1355 
1356     RESTORE_LOCALE(saved_locale)
1357     return 0;
1358 }
1359 
1360 /* Wrapper around GNU readline that handles signals differently. */
1361 
1362 static char *completed_input_string;
1363 static void
rlhandler(char * text)1364 rlhandler(char *text)
1365 {
1366     completed_input_string = text;
1367     rl_callback_handler_remove();
1368 }
1369 
1370 static char *
readline_until_enter_or_signal(const char * prompt,int * signal)1371 readline_until_enter_or_signal(const char *prompt, int *signal)
1372 {
1373     // Defined in Parser/myreadline.c
1374     extern PyThreadState *_PyOS_ReadlineTState;
1375 
1376     char * not_done_reading = "";
1377     fd_set selectset;
1378 
1379     *signal = 0;
1380 #ifdef HAVE_RL_CATCH_SIGNAL
1381     rl_catch_signals = 0;
1382 #endif
1383 
1384     rl_callback_handler_install (prompt, rlhandler);
1385     FD_ZERO(&selectset);
1386 
1387     completed_input_string = not_done_reading;
1388 
1389     while (completed_input_string == not_done_reading) {
1390         int has_input = 0, err = 0;
1391 
1392         while (!has_input)
1393         {
1394             struct timeval timeout = {0, 100000};  // 100 ms (0.1 seconds)
1395 
1396             /* [Bug #1552726] Only limit the pause if an input hook has been
1397                defined.  */
1398             struct timeval *timeoutp = NULL;
1399             if (PyOS_InputHook)
1400                 timeoutp = &timeout;
1401 #ifdef HAVE_RL_RESIZE_TERMINAL
1402             /* Update readline's view of the window size after SIGWINCH */
1403             if (sigwinch_received) {
1404                 sigwinch_received = 0;
1405                 rl_resize_terminal();
1406             }
1407 #endif
1408             FD_SET(fileno(rl_instream), &selectset);
1409             /* select resets selectset if no input was available */
1410             has_input = select(fileno(rl_instream) + 1, &selectset,
1411                                NULL, NULL, timeoutp);
1412             err = errno;
1413             if(PyOS_InputHook) PyOS_InputHook();
1414         }
1415 
1416         if (has_input > 0) {
1417             rl_callback_read_char();
1418         }
1419         else if (err == EINTR) {
1420             int s;
1421             PyEval_RestoreThread(_PyOS_ReadlineTState);
1422             s = PyErr_CheckSignals();
1423             PyEval_SaveThread();
1424             if (s < 0) {
1425                 rl_free_line_state();
1426 #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
1427                 rl_callback_sigcleanup();
1428 #endif
1429                 rl_cleanup_after_signal();
1430                 rl_callback_handler_remove();
1431                 *signal = 1;
1432                 completed_input_string = NULL;
1433             }
1434         }
1435     }
1436 
1437     return completed_input_string;
1438 }
1439 
1440 
1441 static char *
call_readline(FILE * sys_stdin,FILE * sys_stdout,const char * prompt)1442 call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
1443 {
1444     size_t n;
1445     char *p;
1446     int signal;
1447 
1448 #ifdef SAVE_LOCALE
1449     char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1450     if (!saved_locale)
1451         Py_FatalError("not enough memory to save locale");
1452     _Py_SetLocaleFromEnv(LC_CTYPE);
1453 #endif
1454 
1455     if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1456         rl_instream = sys_stdin;
1457         rl_outstream = sys_stdout;
1458 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
1459         rl_prep_terminal (1);
1460 #endif
1461     }
1462 
1463     p = readline_until_enter_or_signal(prompt, &signal);
1464 
1465     /* we got an interrupt signal */
1466     if (signal) {
1467         RESTORE_LOCALE(saved_locale)
1468         return NULL;
1469     }
1470 
1471     /* We got an EOF, return an empty string. */
1472     if (p == NULL) {
1473         p = PyMem_RawMalloc(1);
1474         if (p != NULL)
1475             *p = '\0';
1476         RESTORE_LOCALE(saved_locale)
1477         return p;
1478     }
1479 
1480     /* we have a valid line */
1481     n = strlen(p);
1482     if (should_auto_add_history && n > 0) {
1483         const char *line;
1484         int length = _py_get_history_length();
1485         if (length > 0) {
1486             HIST_ENTRY *hist_ent;
1487             if (using_libedit_emulation) {
1488                 /* handle older 0-based or newer 1-based indexing */
1489                 hist_ent = history_get(length + libedit_history_start - 1);
1490             } else
1491                 hist_ent = history_get(length);
1492             line = hist_ent ? hist_ent->line : "";
1493         } else
1494             line = "";
1495         if (strcmp(p, line))
1496             add_history(p);
1497     }
1498     /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1499        release the original. */
1500     char *q = p;
1501     p = PyMem_RawMalloc(n+2);
1502     if (p != NULL) {
1503         memcpy(p, q, n);
1504         p[n] = '\n';
1505         p[n+1] = '\0';
1506     }
1507     free(q);
1508     RESTORE_LOCALE(saved_locale)
1509     return p;
1510 }
1511 
1512 
1513 /* Initialize the module */
1514 
1515 PyDoc_STRVAR(doc_module,
1516 "Importing this module enables command line editing using GNU readline.");
1517 
1518 PyDoc_STRVAR(doc_module_le,
1519 "Importing this module enables command line editing using libedit readline.");
1520 
1521 static struct PyModuleDef readlinemodule = {
1522     PyModuleDef_HEAD_INIT,
1523     "readline",
1524     doc_module,
1525     sizeof(readlinestate),
1526     readline_methods,
1527     NULL,
1528     readline_traverse,
1529     readline_clear,
1530     readline_free
1531 };
1532 
1533 
1534 PyMODINIT_FUNC
PyInit_readline(void)1535 PyInit_readline(void)
1536 {
1537     const char *backend = "readline";
1538     PyObject *m;
1539     readlinestate *mod_state;
1540 
1541     if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1542         using_libedit_emulation = 1;
1543     }
1544 
1545     if (using_libedit_emulation) {
1546         readlinemodule.m_doc = doc_module_le;
1547         backend = "editline";
1548     }
1549 
1550 
1551     m = PyModule_Create(&readlinemodule);
1552 
1553     if (m == NULL)
1554         return NULL;
1555 #ifdef Py_GIL_DISABLED
1556     PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
1557 #endif
1558 
1559     if (PyModule_AddIntConstant(m, "_READLINE_VERSION",
1560                                 RL_READLINE_VERSION) < 0) {
1561         goto error;
1562     }
1563     if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION",
1564                                 rl_readline_version) < 0) {
1565         goto error;
1566     }
1567     if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION",
1568                                    rl_library_version) < 0)
1569     {
1570         goto error;
1571     }
1572 
1573     if (PyModule_AddStringConstant(m, "backend", backend) < 0) {
1574         goto error;
1575     }
1576 
1577     mod_state = (readlinestate *) PyModule_GetState(m);
1578     if (mod_state == NULL){
1579         goto error;
1580     }
1581     PyOS_ReadlineFunctionPointer = call_readline;
1582     if (setup_readline(mod_state) < 0) {
1583         PyErr_NoMemory();
1584         goto error;
1585     }
1586     if (PyErr_Occurred()){
1587         goto error;
1588     }
1589     return m;
1590 
1591 error:
1592     Py_DECREF(m);
1593     return NULL;
1594 }
1595