Kaydet (Commit) 4baedc1d authored tarafından Fred Drake's avatar Fred Drake

Use the PyModule_Add*() APIs instead of manipulating the module dict

directly.
üst 9bb74321
...@@ -657,34 +657,35 @@ insint(PyObject *d, char *name, int value) ...@@ -657,34 +657,35 @@ insint(PyObject *d, char *name, int value)
DL_EXPORT(void) DL_EXPORT(void)
initselect(void) initselect(void)
{ {
PyObject *m, *d; PyObject *m;
m = Py_InitModule3("select", select_methods, module_doc); m = Py_InitModule3("select", select_methods, module_doc);
d = PyModule_GetDict(m);
SelectError = PyErr_NewException("select.error", NULL, NULL); SelectError = PyErr_NewException("select.error", NULL, NULL);
PyDict_SetItemString(d, "error", SelectError); Py_INCREF(SelectError);
PyModule_AddObject(m, "error", SelectError);
#ifdef HAVE_POLL #ifdef HAVE_POLL
poll_Type.ob_type = &PyType_Type; poll_Type.ob_type = &PyType_Type;
insint(d, "POLLIN", POLLIN); PyModule_AddIntConstant(m, "POLLIN", POLLIN);
insint(d, "POLLPRI", POLLPRI); PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
insint(d, "POLLOUT", POLLOUT); PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
insint(d, "POLLERR", POLLERR); PyModule_AddIntConstant(m, "POLLERR", POLLERR);
insint(d, "POLLHUP", POLLHUP); PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
insint(d, "POLLNVAL", POLLNVAL); PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
#ifdef POLLRDNORM #ifdef POLLRDNORM
insint(d, "POLLRDNORM", POLLRDNORM); PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
#endif #endif
#ifdef POLLRDBAND #ifdef POLLRDBAND
insint(d, "POLLRDBAND", POLLRDBAND); PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
#endif #endif
#ifdef POLLWRNORM #ifdef POLLWRNORM
insint(d, "POLLWRNORM", POLLWRNORM); PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
#endif #endif
#ifdef POLLWRBAND #ifdef POLLWRBAND
insint(d, "POLLWRBAND", POLLWRBAND); PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
#endif #endif
#ifdef POLLMSG #ifdef POLLMSG
insint(d, "POLLMSG", POLLMSG); PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
#endif #endif
#endif /* HAVE_POLL */ #endif /* HAVE_POLL */
} }
This diff is collapsed.
...@@ -1215,12 +1215,11 @@ strop_methods[] = { ...@@ -1215,12 +1215,11 @@ strop_methods[] = {
DL_EXPORT(void) DL_EXPORT(void)
initstrop(void) initstrop(void)
{ {
PyObject *m, *d, *s; PyObject *m, *s;
char buf[256]; char buf[256];
int c, n; int c, n;
m = Py_InitModule4("strop", strop_methods, strop_module__doc__, m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
(PyObject*)NULL, PYTHON_API_VERSION); (PyObject*)NULL, PYTHON_API_VERSION);
d = PyModule_GetDict(m);
/* Create 'whitespace' object */ /* Create 'whitespace' object */
n = 0; n = 0;
...@@ -1229,10 +1228,9 @@ initstrop(void) ...@@ -1229,10 +1228,9 @@ initstrop(void)
buf[n++] = c; buf[n++] = c;
} }
s = PyString_FromStringAndSize(buf, n); s = PyString_FromStringAndSize(buf, n);
if (s) { if (s)
PyDict_SetItemString(d, "whitespace", s); PyModule_AddObject(m, "whitespace", s);
Py_DECREF(s);
}
/* Create 'lowercase' object */ /* Create 'lowercase' object */
n = 0; n = 0;
for (c = 0; c < 256; c++) { for (c = 0; c < 256; c++) {
...@@ -1240,10 +1238,8 @@ initstrop(void) ...@@ -1240,10 +1238,8 @@ initstrop(void)
buf[n++] = c; buf[n++] = c;
} }
s = PyString_FromStringAndSize(buf, n); s = PyString_FromStringAndSize(buf, n);
if (s) { if (s)
PyDict_SetItemString(d, "lowercase", s); PyModule_AddObject(m, "lowercase", s);
Py_DECREF(s);
}
/* Create 'uppercase' object */ /* Create 'uppercase' object */
n = 0; n = 0;
...@@ -1252,8 +1248,6 @@ initstrop(void) ...@@ -1252,8 +1248,6 @@ initstrop(void)
buf[n++] = c; buf[n++] = c;
} }
s = PyString_FromStringAndSize(buf, n); s = PyString_FromStringAndSize(buf, n);
if (s) { if (s)
PyDict_SetItemString(d, "uppercase", s); PyModule_AddObject(m, "uppercase", s);
Py_DECREF(s);
}
} }
...@@ -153,67 +153,54 @@ static PyMethodDef syslog_methods[] = { ...@@ -153,67 +153,54 @@ static PyMethodDef syslog_methods[] = {
{NULL, NULL, 0} {NULL, NULL, 0}
}; };
/* helper function for initialization function */
static void
ins(PyObject *d, char *s, long x)
{
PyObject *v = PyInt_FromLong(x);
if (v) {
PyDict_SetItemString(d, s, v);
Py_DECREF(v);
}
}
/* Initialization function for the module */ /* Initialization function for the module */
DL_EXPORT(void) DL_EXPORT(void)
initsyslog(void) initsyslog(void)
{ {
PyObject *m, *d; PyObject *m;
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule("syslog", syslog_methods); m = Py_InitModule("syslog", syslog_methods);
/* Add some symbolic constants to the module */ /* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
/* Priorities */ /* Priorities */
ins(d, "LOG_EMERG", LOG_EMERG); PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
ins(d, "LOG_ALERT", LOG_ALERT); PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
ins(d, "LOG_CRIT", LOG_CRIT); PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
ins(d, "LOG_ERR", LOG_ERR); PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
ins(d, "LOG_WARNING", LOG_WARNING); PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
ins(d, "LOG_NOTICE", LOG_NOTICE); PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
ins(d, "LOG_INFO", LOG_INFO); PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
ins(d, "LOG_DEBUG", LOG_DEBUG); PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
/* openlog() option flags */ /* openlog() option flags */
ins(d, "LOG_PID", LOG_PID); PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
ins(d, "LOG_CONS", LOG_CONS); PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
ins(d, "LOG_NDELAY", LOG_NDELAY); PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
#ifdef LOG_NOWAIT #ifdef LOG_NOWAIT
ins(d, "LOG_NOWAIT", LOG_NOWAIT); PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
#endif #endif
#ifdef LOG_PERROR #ifdef LOG_PERROR
ins(d, "LOG_PERROR", LOG_PERROR); PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
#endif #endif
/* Facilities */ /* Facilities */
ins(d, "LOG_KERN", LOG_KERN); PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
ins(d, "LOG_USER", LOG_USER); PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
ins(d, "LOG_MAIL", LOG_MAIL); PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
ins(d, "LOG_DAEMON", LOG_DAEMON); PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
ins(d, "LOG_AUTH", LOG_AUTH); PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
ins(d, "LOG_LPR", LOG_LPR); PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
ins(d, "LOG_LOCAL0", LOG_LOCAL0); PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
ins(d, "LOG_LOCAL1", LOG_LOCAL1); PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
ins(d, "LOG_LOCAL2", LOG_LOCAL2); PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
ins(d, "LOG_LOCAL3", LOG_LOCAL3); PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
ins(d, "LOG_LOCAL4", LOG_LOCAL4); PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
ins(d, "LOG_LOCAL5", LOG_LOCAL5); PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
ins(d, "LOG_LOCAL6", LOG_LOCAL6); PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
ins(d, "LOG_LOCAL7", LOG_LOCAL7); PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
#ifndef LOG_SYSLOG #ifndef LOG_SYSLOG
#define LOG_SYSLOG LOG_DAEMON #define LOG_SYSLOG LOG_DAEMON
...@@ -228,8 +215,8 @@ initsyslog(void) ...@@ -228,8 +215,8 @@ initsyslog(void)
#define LOG_CRON LOG_DAEMON #define LOG_CRON LOG_DAEMON
#endif #endif
ins(d, "LOG_SYSLOG", LOG_SYSLOG); PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
ins(d, "LOG_CRON", LOG_CRON); PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
ins(d, "LOG_UUCP", LOG_UUCP); PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
ins(d, "LOG_NEWS", LOG_NEWS); PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
} }
...@@ -847,17 +847,18 @@ static char zlib_module_documentation[]= ...@@ -847,17 +847,18 @@ static char zlib_module_documentation[]=
DL_EXPORT(void) DL_EXPORT(void)
PyInit_zlib(void) PyInit_zlib(void)
{ {
PyObject *m, *d, *ver; PyObject *m, *ver;
Comptype.ob_type = &PyType_Type; Comptype.ob_type = &PyType_Type;
Decomptype.ob_type = &PyType_Type; Decomptype.ob_type = &PyType_Type;
m = Py_InitModule4("zlib", zlib_methods, m = Py_InitModule4("zlib", zlib_methods,
zlib_module_documentation, zlib_module_documentation,
(PyObject*)NULL,PYTHON_API_VERSION); (PyObject*)NULL,PYTHON_API_VERSION);
d = PyModule_GetDict(m);
ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
if (ZlibError != NULL)
PyDict_SetItemString(d, "error", ZlibError);
ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
if (ZlibError != NULL) {
Py_INCREF(ZlibError);
PyModule_AddObject(m, "error", ZlibError);
}
PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS); PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
PyModule_AddIntConstant(m, "DEFLATED", DEFLATED); PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL); PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
...@@ -874,10 +875,8 @@ PyInit_zlib(void) ...@@ -874,10 +875,8 @@ PyInit_zlib(void)
PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH); PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
ver = PyString_FromString(ZLIB_VERSION); ver = PyString_FromString(ZLIB_VERSION);
if (ver != NULL) { if (ver != NULL)
PyDict_SetItemString(d, "ZLIB_VERSION", ver); PyModule_AddObject(m, "ZLIB_VERSION", ver);
Py_DECREF(ver);
}
#ifdef WITH_THREAD #ifdef WITH_THREAD
zlib_lock = PyThread_allocate_lock(); zlib_lock = PyThread_allocate_lock();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment