[Python-Dev] warning in initpyexpat (original) (raw)

Jeremy Hylton jeremy@beopen.com
Fri, 25 Aug 2000 13:25:13 -0400 (EDT)


gcc -Wall is complaining about possible use of errors_module without initialization in the initpyexpat function. Here's the offending code:

sys_modules = PySys_GetObject("modules");
{
    PyObject *errmod_name = PyString_FromString("pyexpat.errors");

    if (errmod_name != NULL) {
        errors_module = PyDict_GetItem(d, errmod_name);
        if (errors_module == NULL) {
            errors_module = PyModule_New("pyexpat.errors");
            if (errors_module != NULL) {
                PyDict_SetItemString(d, "errors", errors_module);
                PyDict_SetItem(sys_modules, errmod_name, errors_module);
            }
        }
        Py_DECREF(errmod_name);
        if (errors_module == NULL)
            /* Don't code dump later! */
            return;
    }
}
errors_dict = PyModule_GetDict(errors_module);

It is indeed the case that errors_module can be used without initialization. If PyString_FromString("pyexpat.errors") fails, you ignore the error and will immediately call PyModule_GetDict with an uninitialized variable.

You ought to check for the error condition and bail cleanly, rather than ignoring it and failing somewhere else.

I also wonder why the code that does this check is in its own set of curly braces; thus, the post to python-dev to discuss the style issue. Why did you do this? Is it approved Python style? It looks cluttered to me.

Jeremy