Add NULL checks to the initializer of the locale module

The _locale module was using old-style APIs to set numeric module
constants from macros. The new way requires less code and properly
checks for NULL.

CID 1295027
This commit is contained in:
Christian Heimes 2016-09-09 00:24:12 +02:00
parent 45af0c83da
commit ff4fddde57
1 changed files with 20 additions and 34 deletions

View File

@ -621,7 +621,7 @@ static struct PyModuleDef _localemodule = {
PyMODINIT_FUNC PyMODINIT_FUNC
PyInit__locale(void) PyInit__locale(void)
{ {
PyObject *m, *d, *x; PyObject *m;
#ifdef HAVE_LANGINFO_H #ifdef HAVE_LANGINFO_H
int i; int i;
#endif #endif
@ -630,44 +630,25 @@ PyInit__locale(void)
if (m == NULL) if (m == NULL)
return NULL; return NULL;
d = PyModule_GetDict(m); PyModule_AddIntMacro(m, LC_CTYPE);
PyModule_AddIntMacro(m, LC_TIME);
x = PyLong_FromLong(LC_CTYPE); PyModule_AddIntMacro(m, LC_COLLATE);
PyDict_SetItemString(d, "LC_CTYPE", x); PyModule_AddIntMacro(m, LC_MONETARY);
Py_XDECREF(x);
x = PyLong_FromLong(LC_TIME);
PyDict_SetItemString(d, "LC_TIME", x);
Py_XDECREF(x);
x = PyLong_FromLong(LC_COLLATE);
PyDict_SetItemString(d, "LC_COLLATE", x);
Py_XDECREF(x);
x = PyLong_FromLong(LC_MONETARY);
PyDict_SetItemString(d, "LC_MONETARY", x);
Py_XDECREF(x);
#ifdef LC_MESSAGES #ifdef LC_MESSAGES
x = PyLong_FromLong(LC_MESSAGES); PyModule_AddIntMacro(m, LC_MESSAGES);
PyDict_SetItemString(d, "LC_MESSAGES", x);
Py_XDECREF(x);
#endif /* LC_MESSAGES */ #endif /* LC_MESSAGES */
x = PyLong_FromLong(LC_NUMERIC); PyModule_AddIntMacro(m, LC_NUMERIC);
PyDict_SetItemString(d, "LC_NUMERIC", x); PyModule_AddIntMacro(m, LC_ALL);
Py_XDECREF(x); PyModule_AddIntMacro(m, CHAR_MAX);
x = PyLong_FromLong(LC_ALL);
PyDict_SetItemString(d, "LC_ALL", x);
Py_XDECREF(x);
x = PyLong_FromLong(CHAR_MAX);
PyDict_SetItemString(d, "CHAR_MAX", x);
Py_XDECREF(x);
Error = PyErr_NewException("locale.Error", NULL, NULL); Error = PyErr_NewException("locale.Error", NULL, NULL);
PyDict_SetItemString(d, "Error", Error); if (Error == NULL) {
Py_DECREF(m);
return NULL;
}
PyModule_AddObject(m, "Error", Error);
#ifdef HAVE_LANGINFO_H #ifdef HAVE_LANGINFO_H
for (i = 0; langinfo_constants[i].name; i++) { for (i = 0; langinfo_constants[i].name; i++) {
@ -675,6 +656,11 @@ PyInit__locale(void)
langinfo_constants[i].value); langinfo_constants[i].value);
} }
#endif #endif
if (PyErr_Occurred()) {
Py_DECREF(m);
return NULL;
}
return m; return m;
} }