cpython: b6b8a2171aa3 (original) (raw)
Mercurial > cpython
changeset 85581:b6b8a2171aa3
Issue #18458: Prevent crashes with newer versions of libedit. Its readline emulation has changed from 0-based indexing to 1-based like gnu readline. Original patch by Ronald Oussoren. [#18458]
Ned Deily nad@acm.org | |
---|---|
date | Fri, 06 Sep 2013 15:16:19 -0700 |
parents | 4b64166d5abb |
children | a55cbaf9a581 |
files | Misc/NEWS Modules/readline.c |
diffstat | 2 files changed, 25 insertions(+), 12 deletions(-)[+] [-] Misc/NEWS 3 Modules/readline.c 34 |
line wrap: on
line diff
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -56,6 +56,9 @@ Core and Builtins Library ------- +- Issue #18458: Prevent crashes with newer versions of libedit. Its readline
- Issue #18852: Handle case of
readline.__doc__
beingNone
in the new readline activation code insite.py
.
--- a/Modules/readline.c +++ b/Modules/readline.c @@ -63,6 +63,8 @@ extern char **completion_matches(char *, / static int using_libedit_emulation = 0; static const char libedit_version_tag[] = "EditLine wrapper"; + +static int libedit_history_start = 0; #endif / APPLE */ #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK @@ -627,21 +629,21 @@ get_history_item(PyObject *self, PyObjec return NULL; #ifdef APPLE if (using_libedit_emulation) {
/* Libedit emulation uses 0-based indexes,[](#l2.16)
* the real one uses 1-based indexes,[](#l2.17)
* adjust the index to ensure that Python[](#l2.18)
* code doesn't have to worry about the[](#l2.19)
* difference.[](#l2.20)
/* Older versions of libedit's readline emulation[](#l2.21)
* use 0-based indexes, while readline and newer[](#l2.22)
* versions of libedit use 1-based indexes.[](#l2.23) */[](#l2.24) int length = _py_get_history_length();[](#l2.25)
idx --;[](#l2.26)
idx = idx - 1 + libedit_history_start;[](#l2.28)
/* * Apple's readline emulation crashes when * the index is out of range, therefore * test for that and fail gracefully. */
if (idx < 0 || idx >= length) {[](#l2.35)
if (idx < (0 + libedit_history_start)[](#l2.36)
} @@ -974,6 +976,17 @@ setup_readline(readlinestate *mod_state) */ if (using_libedit_emulation) rl_initialize();|| idx >= (length + libedit_history_start)) {[](#l2.37) Py_RETURN_NONE;[](#l2.38) }[](#l2.39)
- /* Detect if libedit's readline emulation uses 0-based
* indexing or 1-based indexing.[](#l2.47)
*/[](#l2.48)
- add_history("1");
- if (history_get(1) == NULL) {
libedit_history_start = 0;[](#l2.51)
- } else {
libedit_history_start = 1;[](#l2.53)
- }
- clear_history();
#endif /* APPLE */ using_history(); @@ -1178,11 +1191,8 @@ call_readline(FILE *sys_stdin, FILE *sys if (length > 0) #ifdef APPLE if (using_libedit_emulation) {
/*[](#l2.63)
* Libedit's emulation uses 0-based indexes,[](#l2.64)
* the real readline uses 1-based indexes.[](#l2.65)
*/[](#l2.66)
line = (const char *)history_get(length - 1)->line;[](#l2.67)
/* handle older 0-based or newer 1-based indexing */[](#l2.68)
line = (const char *)history_get(length + libedit_history_start - 1)->line;[](#l2.69) } else[](#l2.70)
#endif /* APPLE */ line = (const char *)history_get(length)->line;