Check return result of PyModule_GetDict().

Fix a bunch of refleaks in the init of the module.  This would only be found
when running python -v.
This commit is contained in:
Neal Norwitz 2006-08-13 18:11:27 +00:00
parent 56423e5762
commit e9ac0bb169
1 changed files with 24 additions and 22 deletions

View File

@ -1126,6 +1126,15 @@ static struct PyMethodDef mmap_functions[] = {
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
static void
setint(PyObject *d, const char *name, long value)
{
PyObject *o = PyInt_FromLong(value);
if (o)
if (PyDict_SetItemString(d, name, o) == 0)
Py_DECREF(o);
}
PyMODINIT_FUNC PyMODINIT_FUNC
initmmap(void) initmmap(void)
{ {
@ -1138,47 +1147,40 @@ PyMODINIT_FUNC
if (module == NULL) if (module == NULL)
return; return;
dict = PyModule_GetDict(module); dict = PyModule_GetDict(module);
if (!dict)
return;
mmap_module_error = PyExc_EnvironmentError; mmap_module_error = PyExc_EnvironmentError;
Py_INCREF(mmap_module_error);
PyDict_SetItemString(dict, "error", mmap_module_error); PyDict_SetItemString(dict, "error", mmap_module_error);
#ifdef PROT_EXEC #ifdef PROT_EXEC
PyDict_SetItemString(dict, "PROT_EXEC", PyInt_FromLong(PROT_EXEC) ); setint(dict, "PROT_EXEC", PROT_EXEC);
#endif #endif
#ifdef PROT_READ #ifdef PROT_READ
PyDict_SetItemString(dict, "PROT_READ", PyInt_FromLong(PROT_READ) ); setint(dict, "PROT_READ", PROT_READ);
#endif #endif
#ifdef PROT_WRITE #ifdef PROT_WRITE
PyDict_SetItemString(dict, "PROT_WRITE", PyInt_FromLong(PROT_WRITE) ); setint(dict, "PROT_WRITE", PROT_WRITE);
#endif #endif
#ifdef MAP_SHARED #ifdef MAP_SHARED
PyDict_SetItemString(dict, "MAP_SHARED", PyInt_FromLong(MAP_SHARED) ); setint(dict, "MAP_SHARED", MAP_SHARED);
#endif #endif
#ifdef MAP_PRIVATE #ifdef MAP_PRIVATE
PyDict_SetItemString(dict, "MAP_PRIVATE", setint(dict, "MAP_PRIVATE", MAP_PRIVATE);
PyInt_FromLong(MAP_PRIVATE) );
#endif #endif
#ifdef MAP_DENYWRITE #ifdef MAP_DENYWRITE
PyDict_SetItemString(dict, "MAP_DENYWRITE", setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE);
PyInt_FromLong(MAP_DENYWRITE) );
#endif #endif
#ifdef MAP_EXECUTABLE #ifdef MAP_EXECUTABLE
PyDict_SetItemString(dict, "MAP_EXECUTABLE", setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE);
PyInt_FromLong(MAP_EXECUTABLE) );
#endif #endif
#ifdef MAP_ANONYMOUS #ifdef MAP_ANONYMOUS
PyDict_SetItemString(dict, "MAP_ANON", PyInt_FromLong(MAP_ANONYMOUS) ); setint(dict, "MAP_ANON", MAP_ANONYMOUS);
PyDict_SetItemString(dict, "MAP_ANONYMOUS", setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);
PyInt_FromLong(MAP_ANONYMOUS) );
#endif #endif
PyDict_SetItemString(dict, "PAGESIZE", setint(dict, "PAGESIZE", (long)my_getpagesize());
PyInt_FromLong((long)my_getpagesize()));
PyDict_SetItemString(dict, "ACCESS_READ", setint(dict, "ACCESS_READ", ACCESS_READ);
PyInt_FromLong(ACCESS_READ)); setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
PyDict_SetItemString(dict, "ACCESS_WRITE", setint(dict, "ACCESS_COPY", ACCESS_COPY);
PyInt_FromLong(ACCESS_WRITE));
PyDict_SetItemString(dict, "ACCESS_COPY",
PyInt_FromLong(ACCESS_COPY));
} }