Use PyModule_AddIntConstant() instead of creating a private helper function.

This also avoids directly accessing the module'd __dict__.
This commit is contained in:
Fred Drake 2002-02-14 06:59:26 +00:00
parent 8301256a44
commit cca657b8fe
1 changed files with 22 additions and 30 deletions

View File

@ -157,88 +157,80 @@ resource_methods[] = {
/* Module initialization */
static void
ins(PyObject *dict, char *name, int value)
{
PyObject *v = PyInt_FromLong((long) value);
if (v) {
PyDict_SetItemString(dict, name, v);
Py_DECREF(v);
}
/* errors will be checked by initresource() */
}
DL_EXPORT(void)
initresource(void)
{
PyObject *m, *d;
PyObject *m;
/* Create the module and add the functions */
m = Py_InitModule("resource", resource_methods);
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
ResourceError = PyErr_NewException("resource.error", NULL, NULL);
PyDict_SetItemString(d, "error", ResourceError);
if (ResourceError == NULL) {
ResourceError = PyErr_NewException("resource.error",
NULL, NULL);
}
Py_INCREF(ResourceError);
PyModule_AddObject(m, "error", ResourceError);
/* insert constants */
#ifdef RLIMIT_CPU
ins(d, "RLIMIT_CPU", RLIMIT_CPU);
PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
#endif
#ifdef RLIMIT_FSIZE
ins(d, "RLIMIT_FSIZE", RLIMIT_FSIZE);
PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
#endif
#ifdef RLIMIT_DATA
ins(d, "RLIMIT_DATA", RLIMIT_DATA);
PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
#endif
#ifdef RLIMIT_STACK
ins(d, "RLIMIT_STACK", RLIMIT_STACK);
PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
#endif
#ifdef RLIMIT_CORE
ins(d, "RLIMIT_CORE", RLIMIT_CORE);
PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
#endif
#ifdef RLIMIT_NOFILE
ins(d, "RLIMIT_NOFILE", RLIMIT_NOFILE);
PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
#endif
#ifdef RLIMIT_OFILE
ins(d, "RLIMIT_OFILE", RLIMIT_OFILE);
PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
#endif
#ifdef RLIMIT_VMEM
ins(d, "RLIMIT_VMEM", RLIMIT_VMEM);
PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
#endif
#ifdef RLIMIT_AS
ins(d, "RLIMIT_AS", RLIMIT_AS);
PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
#endif
#ifdef RLIMIT_RSS
ins(d, "RLIMIT_RSS", RLIMIT_RSS);
PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
#endif
#ifdef RLIMIT_NPROC
ins(d, "RLIMIT_NPROC", RLIMIT_NPROC);
PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
#endif
#ifdef RLIMIT_MEMLOCK
ins(d, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
#endif
#ifdef RUSAGE_SELF
ins(d, "RUSAGE_SELF", RUSAGE_SELF);
PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
#endif
#ifdef RUSAGE_CHILDREN
ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
#endif
#ifdef RUSAGE_BOTH
ins(d, "RUSAGE_BOTH", RUSAGE_BOTH);
PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
#endif
}