diff -r a8fb2c787e61 Modules/termios.c --- a/Modules/termios.c Thu Aug 16 23:27:43 2012 +0200 +++ b/Modules/termios.c Thu Aug 16 23:31:53 2012 +0200 @@ -33,7 +33,36 @@ argument. This can be an integer file descriptor, such as returned by\n\ sys.stdin.fileno(), or a file object, such as sys.stdin itself."); -static PyObject *TermiosError; +typedef struct { + PyObject *TermiosError; +} termiosstate; + +#define termios_state(o) ((termiosstate *)PyModule_GetState(o)) + +static int +termios_clear(PyObject *m) +{ + Py_CLEAR(termios_state(m)->TermiosError); + return 0; +} + +static int +termios_traverse(PyObject *m, visitproc visit, void *arg) +{ + Py_VISIT(termios_state(m)->TermiosError); + return 0; +} + +static void +termios_free(void *m) +{ + termios_clear((PyObject *)m); +} + +static PyModuleDef termiosmodule; + +#define termiosstate_global ((termiosstate *)PyModule_GetState(PyState_FindModule(&termiosmodule))) + static int fdconv(PyObject* obj, void* p) { @@ -74,7 +103,7 @@ return NULL; if (tcgetattr(fd, &mode) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); ispeed = cfgetispeed(&mode); ospeed = cfgetospeed(&mode); @@ -155,7 +184,7 @@ /* Get the old mode, in case there are any hidden fields... */ if (tcgetattr(fd, &mode) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); mode.c_iflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 0)); mode.c_oflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 1)); mode.c_cflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 2)); @@ -188,11 +217,11 @@ } if (cfsetispeed(&mode, (speed_t) ispeed) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); if (cfsetospeed(&mode, (speed_t) ospeed) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); if (tcsetattr(fd, when, &mode) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); Py_INCREF(Py_None); return Py_None; @@ -214,7 +243,7 @@ fdconv, &fd, &duration)) return NULL; if (tcsendbreak(fd, duration) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); Py_INCREF(Py_None); return Py_None; @@ -234,7 +263,7 @@ fdconv, &fd)) return NULL; if (tcdrain(fd) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); Py_INCREF(Py_None); return Py_None; @@ -257,7 +286,7 @@ fdconv, &fd, &queue)) return NULL; if (tcflush(fd, queue) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); Py_INCREF(Py_None); return Py_None; @@ -280,7 +309,7 @@ fdconv, &fd, &action)) return NULL; if (tcflow(fd, action) == -1) - return PyErr_SetFromErrno(TermiosError); + return PyErr_SetFromErrno(termiosstate_global->TermiosError); Py_INCREF(Py_None); return Py_None; @@ -939,12 +968,12 @@ PyModuleDef_HEAD_INIT, "termios", termios__doc__, - -1, + sizeof(termiosstate), termios_methods, NULL, - NULL, - NULL, - NULL + termios_traverse, + termios_clear, + termios_free }; PyMODINIT_FUNC @@ -957,11 +986,11 @@ if (m == NULL) return NULL; - if (TermiosError == NULL) { - TermiosError = PyErr_NewException("termios.error", NULL, NULL); + if (termios_state(m)->TermiosError == NULL) { + termios_state(m)->TermiosError = PyErr_NewException("termios.error", NULL, NULL); } - Py_INCREF(TermiosError); - PyModule_AddObject(m, "error", TermiosError); + Py_INCREF(termios_state(m)->TermiosError); + PyModule_AddObject(m, "error", termios_state(m)->TermiosError); while (constant->name != NULL) { PyModule_AddIntConstant(m, constant->name, constant->value);