Skip to content

bpo-38631: Avoid Py_FatalError() in readline #16998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Modules/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,15 +1066,16 @@ flex_complete(const char *text, int start, int end)
}


/* Helper to initialize GNU readline properly. */

static void
/* Helper to initialize GNU readline properly.
Return -1 on memory allocation failure, return 0 on success. */
static int
setup_readline(readlinestate *mod_state)
{
#ifdef SAVE_LOCALE
char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
if (!saved_locale)
Py_FatalError("not enough memory to save locale");
if (!saved_locale) {
return -1;
}
#endif

/* The name must be defined before initialization */
Expand Down Expand Up @@ -1156,6 +1157,7 @@ setup_readline(readlinestate *mod_state)
rl_initialize();

RESTORE_LOCALE(saved_locale)
return 0;
}

/* Wrapper around GNU readline that handles signals differently. */
Expand Down Expand Up @@ -1369,7 +1371,10 @@ PyInit_readline(void)

mod_state = (readlinestate *) PyModule_GetState(m);
PyOS_ReadlineFunctionPointer = call_readline;
setup_readline(mod_state);
if (setup_readline(mod_state) < 0) {
PyErr_NoMemory();
goto error;
}

return m;

Expand Down