Skip to content

Commit d3a1de2

Browse files
authored
bpo-38631: Avoid Py_FatalError() in _PyCodecRegistry_Init() (pythonGH-18217)
_PyCodecRegistry_Init() now reports exceptions to the caller, rather than calling Py_FatalError().
1 parent dd023ad commit d3a1de2

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

Python/codecs.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,32 +1494,37 @@ static int _PyCodecRegistry_Init(void)
14941494

14951495
PyInterpreterState *interp = _PyInterpreterState_Get();
14961496
PyObject *mod;
1497-
unsigned i;
14981497

14991498
if (interp->codec_search_path != NULL)
15001499
return 0;
15011500

15021501
interp->codec_search_path = PyList_New(0);
1502+
if (interp->codec_search_path == NULL) {
1503+
return -1;
1504+
}
1505+
15031506
interp->codec_search_cache = PyDict_New();
1507+
if (interp->codec_search_cache == NULL) {
1508+
return -1;
1509+
}
1510+
15041511
interp->codec_error_registry = PyDict_New();
1512+
if (interp->codec_error_registry == NULL) {
1513+
return -1;
1514+
}
15051515

1506-
if (interp->codec_error_registry) {
1507-
for (i = 0; i < Py_ARRAY_LENGTH(methods); ++i) {
1508-
PyObject *func = PyCFunction_NewEx(&methods[i].def, NULL, NULL);
1509-
int res;
1510-
if (!func)
1511-
Py_FatalError("can't initialize codec error registry");
1512-
res = PyCodec_RegisterError(methods[i].name, func);
1513-
Py_DECREF(func);
1514-
if (res)
1515-
Py_FatalError("can't initialize codec error registry");
1516+
for (size_t i = 0; i < Py_ARRAY_LENGTH(methods); ++i) {
1517+
PyObject *func = PyCFunction_NewEx(&methods[i].def, NULL, NULL);
1518+
if (!func) {
1519+
return -1;
15161520
}
1517-
}
15181521

1519-
if (interp->codec_search_path == NULL ||
1520-
interp->codec_search_cache == NULL ||
1521-
interp->codec_error_registry == NULL)
1522-
Py_FatalError("can't initialize codec registry");
1522+
int res = PyCodec_RegisterError(methods[i].name, func);
1523+
Py_DECREF(func);
1524+
if (res) {
1525+
return -1;
1526+
}
1527+
}
15231528

15241529
mod = PyImport_ImportModuleNoBlock("encodings");
15251530
if (mod == NULL) {

0 commit comments

Comments
 (0)