Skip to content

Commit 1d8da61

Browse files
authored
bpo-38631: Avoid Py_FatalError() in readline (GH-16998)
readline now calls PyErr_NoMemory() rather than Py_FatalError() on memory allocation failure, when importing the module.
1 parent a4ed6ed commit 1d8da61

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Modules/readline.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,15 +1066,16 @@ flex_complete(const char *text, int start, int end)
10661066
}
10671067

10681068

1069-
/* Helper to initialize GNU readline properly. */
1070-
1071-
static void
1069+
/* Helper to initialize GNU readline properly.
1070+
Return -1 on memory allocation failure, return 0 on success. */
1071+
static int
10721072
setup_readline(readlinestate *mod_state)
10731073
{
10741074
#ifdef SAVE_LOCALE
10751075
char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1076-
if (!saved_locale)
1077-
Py_FatalError("not enough memory to save locale");
1076+
if (!saved_locale) {
1077+
return -1;
1078+
}
10781079
#endif
10791080

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

11581159
RESTORE_LOCALE(saved_locale)
1160+
return 0;
11591161
}
11601162

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

13701372
mod_state = (readlinestate *) PyModule_GetState(m);
13711373
PyOS_ReadlineFunctionPointer = call_readline;
1372-
setup_readline(mod_state);
1374+
if (setup_readline(mod_state) < 0) {
1375+
PyErr_NoMemory();
1376+
goto error;
1377+
}
13731378

13741379
return m;
13751380

0 commit comments

Comments
 (0)