summaryrefslogtreecommitdiff
path: root/Python/codecs.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-01-27 23:23:12 +0100
committerGitHub <noreply@github.com>2020-01-27 23:23:12 +0100
commitd3a1de22705cc79d7e8a0f44c4f00255e58c8b20 (patch)
treebaf6080ff2c67dde5c37aa790bc29e497ac825f5 /Python/codecs.c
parentdd023ad1619b6f1ab313986e8953eea32c18f50c (diff)
downloadcpython-git-d3a1de22705cc79d7e8a0f44c4f00255e58c8b20.tar.gz
bpo-38631: Avoid Py_FatalError() in _PyCodecRegistry_Init() (GH-18217)
_PyCodecRegistry_Init() now reports exceptions to the caller, rather than calling Py_FatalError().
Diffstat (limited to 'Python/codecs.c')
-rw-r--r--Python/codecs.c37
1 files changed, 21 insertions, 16 deletions
diff --git a/Python/codecs.c b/Python/codecs.c
index 08e9b916f2..10d76969a5 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -1494,32 +1494,37 @@ static int _PyCodecRegistry_Init(void)
PyInterpreterState *interp = _PyInterpreterState_Get();
PyObject *mod;
- unsigned i;
if (interp->codec_search_path != NULL)
return 0;
interp->codec_search_path = PyList_New(0);
+ if (interp->codec_search_path == NULL) {
+ return -1;
+ }
+
interp->codec_search_cache = PyDict_New();
+ if (interp->codec_search_cache == NULL) {
+ return -1;
+ }
+
interp->codec_error_registry = PyDict_New();
+ if (interp->codec_error_registry == NULL) {
+ return -1;
+ }
- if (interp->codec_error_registry) {
- for (i = 0; i < Py_ARRAY_LENGTH(methods); ++i) {
- PyObject *func = PyCFunction_NewEx(&methods[i].def, NULL, NULL);
- int res;
- if (!func)
- Py_FatalError("can't initialize codec error registry");
- res = PyCodec_RegisterError(methods[i].name, func);
- Py_DECREF(func);
- if (res)
- Py_FatalError("can't initialize codec error registry");
+ for (size_t i = 0; i < Py_ARRAY_LENGTH(methods); ++i) {
+ PyObject *func = PyCFunction_NewEx(&methods[i].def, NULL, NULL);
+ if (!func) {
+ return -1;
}
- }
- if (interp->codec_search_path == NULL ||
- interp->codec_search_cache == NULL ||
- interp->codec_error_registry == NULL)
- Py_FatalError("can't initialize codec registry");
+ int res = PyCodec_RegisterError(methods[i].name, func);
+ Py_DECREF(func);
+ if (res) {
+ return -1;
+ }
+ }
mod = PyImport_ImportModuleNoBlock("encodings");
if (mod == NULL) {