From a3faea94da2bef2456fcf7c55353b052dd001ad7 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Mon, 10 Aug 2020 19:15:19 -0500 Subject: [PATCH 1/3] port termios to multi-phase --- Modules/termios.c | 164 +++++++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 75 deletions(-) diff --git a/Modules/termios.c b/Modules/termios.c index 178ae4ee6e41dd..77700bd9263770 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -51,8 +51,6 @@ get_termios_state(PyObject *module) return (termiosmodulestate *)state; } -#define modulestate_global get_termios_state(PyState_FindModule(&termiosmodule)) - static int fdconv(PyObject* obj, void* p) { int fd; @@ -82,28 +80,29 @@ static PyObject * termios_tcgetattr(PyObject *self, PyObject *args) { int fd; - struct termios mode; - PyObject *cc; - speed_t ispeed, ospeed; - PyObject *v; - int i; - char ch; - if (!PyArg_ParseTuple(args, "O&:tcgetattr", - fdconv, (void*)&fd)) + fdconv, (void*)&fd)) { return NULL; + } - if (tcgetattr(fd, &mode) == -1) - return PyErr_SetFromErrno(modulestate_global->TermiosError); + termiosmodulestate *state = PyModule_GetState(self); + struct termios mode; + if (tcgetattr(fd, &mode) == -1) { + return PyErr_SetFromErrno(state->TermiosError); + } - ispeed = cfgetispeed(&mode); - ospeed = cfgetospeed(&mode); + speed_t ispeed = cfgetispeed(&mode); + speed_t ospeed = cfgetospeed(&mode); - cc = PyList_New(NCCS); - if (cc == NULL) + PyObject *cc = PyList_New(NCCS); + if (cc == NULL) { return NULL; + } + + PyObject *v; + int i; for (i = 0; i < NCCS; i++) { - ch = (char)mode.c_cc[i]; + char ch = (char)mode.c_cc[i]; v = PyBytes_FromStringAndSize(&ch, 1); if (v == NULL) goto err; @@ -159,14 +158,12 @@ static PyObject * termios_tcsetattr(PyObject *self, PyObject *args) { int fd, when; - struct termios mode; - speed_t ispeed, ospeed; - PyObject *term, *cc, *v; - int i; - + PyObject *term; if (!PyArg_ParseTuple(args, "O&iO:tcsetattr", - fdconv, &fd, &when, &term)) + fdconv, &fd, &when, &term)) { return NULL; + } + if (!PyList_Check(term) || PyList_Size(term) != 7) { PyErr_SetString(PyExc_TypeError, "tcsetattr, arg 3: must be 7 element list"); @@ -174,18 +171,22 @@ termios_tcsetattr(PyObject *self, PyObject *args) } /* Get the old mode, in case there are any hidden fields... */ - termiosmodulestate *state = modulestate_global; - if (tcgetattr(fd, &mode) == -1) + termiosmodulestate *state = PyModule_GetState(self); + struct termios mode; + if (tcgetattr(fd, &mode) == -1) { return PyErr_SetFromErrno(state->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)); mode.c_lflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 3)); - ispeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 4)); - ospeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 5)); - cc = PyList_GetItem(term, 6); - if (PyErr_Occurred()) + speed_t ispeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 4)); + speed_t ospeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 5)); + PyObject *cc = PyList_GetItem(term, 6); + if (PyErr_Occurred()) { return NULL; + } if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) { PyErr_Format(PyExc_TypeError, @@ -194,6 +195,8 @@ termios_tcsetattr(PyObject *self, PyObject *args) return NULL; } + int i; + PyObject *v; for (i = 0; i < NCCS; i++) { v = PyList_GetItem(cc, i); @@ -229,12 +232,15 @@ static PyObject * termios_tcsendbreak(PyObject *self, PyObject *args) { int fd, duration; - if (!PyArg_ParseTuple(args, "O&i:tcsendbreak", - fdconv, &fd, &duration)) + fdconv, &fd, &duration)) { return NULL; - if (tcsendbreak(fd, duration) == -1) - return PyErr_SetFromErrno(modulestate_global->TermiosError); + } + + termiosmodulestate *state = PyModule_GetState(self); + if (tcsendbreak(fd, duration) == -1) { + return PyErr_SetFromErrno(state->TermiosError); + } Py_RETURN_NONE; } @@ -248,12 +254,15 @@ static PyObject * termios_tcdrain(PyObject *self, PyObject *args) { int fd; - if (!PyArg_ParseTuple(args, "O&:tcdrain", - fdconv, &fd)) + fdconv, &fd)) { return NULL; - if (tcdrain(fd) == -1) - return PyErr_SetFromErrno(modulestate_global->TermiosError); + } + + termiosmodulestate *state = PyModule_GetState(self); + if (tcdrain(fd) == -1) { + return PyErr_SetFromErrno(state->TermiosError); + } Py_RETURN_NONE; } @@ -270,12 +279,15 @@ static PyObject * termios_tcflush(PyObject *self, PyObject *args) { int fd, queue; - if (!PyArg_ParseTuple(args, "O&i:tcflush", - fdconv, &fd, &queue)) + fdconv, &fd, &queue)) { return NULL; - if (tcflush(fd, queue) == -1) - return PyErr_SetFromErrno(modulestate_global->TermiosError); + } + + termiosmodulestate *state = PyModule_GetState(self); + if (tcflush(fd, queue) == -1) { + return PyErr_SetFromErrno(state->TermiosError); + } Py_RETURN_NONE; } @@ -292,12 +304,15 @@ static PyObject * termios_tcflow(PyObject *self, PyObject *args) { int fd, action; - if (!PyArg_ParseTuple(args, "O&i:tcflow", - fdconv, &fd, &action)) + fdconv, &fd, &action)) { return NULL; - if (tcflow(fd, action) == -1) - return PyErr_SetFromErrno(modulestate_global->TermiosError); + } + + termiosmodulestate *state = PyModule_GetState(self); + if (tcflow(fd, action) == -1) { + return PyErr_SetFromErrno(state->TermiosError); + } Py_RETURN_NONE; } @@ -997,44 +1012,43 @@ static void termiosmodule_free(void *m) { termiosmodule_clear((PyObject *)m); } -static struct PyModuleDef termiosmodule = { - PyModuleDef_HEAD_INIT, - "termios", - termios__doc__, - sizeof(termiosmodulestate), - termios_methods, - NULL, - termiosmodule_traverse, - termiosmodule_clear, - termiosmodule_free, -}; - -PyMODINIT_FUNC -PyInit_termios(void) +static int +termios_exec(PyObject *mod) { - PyObject *m; struct constant *constant = termios_constants; - - if ((m = PyState_FindModule(&termiosmodule)) != NULL) { - Py_INCREF(m); - return m; - } - - if ((m = PyModule_Create(&termiosmodule)) == NULL) { - return NULL; - } - - termiosmodulestate *state = get_termios_state(m); + termiosmodulestate *state = get_termios_state(mod); state->TermiosError = PyErr_NewException("termios.error", NULL, NULL); if (state->TermiosError == NULL) { - return NULL; + return -1; } Py_INCREF(state->TermiosError); - PyModule_AddObject(m, "error", state->TermiosError); + PyModule_AddObject(mod, "error", state->TermiosError); while (constant->name != NULL) { - PyModule_AddIntConstant(m, constant->name, constant->value); + PyModule_AddIntConstant(mod, constant->name, constant->value); ++constant; } - return m; + return 0; +} + +static PyModuleDef_Slot termios_slots[] = { + {Py_mod_exec, termios_exec}, + {0, NULL} +}; + +static struct PyModuleDef termiosmodule = { + PyModuleDef_HEAD_INIT, + .m_name = "termios", + .m_doc = termios__doc__, + .m_size = sizeof(termiosmodulestate), + .m_methods = termios_methods, + .m_slots = termios_slots, + .m_traverse = termiosmodule_traverse, + .m_clear = termiosmodule_clear, + .m_free = termiosmodule_free, +}; + +PyMODINIT_FUNC PyInit_termios(void) +{ + return PyModuleDef_Init(&termiosmodule); } From a4bbec3c770eeaf0a2d1328b5c5b6b9d28e79cea Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Mon, 7 Sep 2020 11:35:07 -0500 Subject: [PATCH 2/3] blurb --- .../2020-09-07-11-35-02.bpo-1635741.rvIexb.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-09-07-11-35-02.bpo-1635741.rvIexb.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-09-07-11-35-02.bpo-1635741.rvIexb.rst b/Misc/NEWS.d/next/Core and Builtins/2020-09-07-11-35-02.bpo-1635741.rvIexb.rst new file mode 100644 index 00000000000000..1e19b34b372d89 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-09-07-11-35-02.bpo-1635741.rvIexb.rst @@ -0,0 +1,2 @@ +Port the :mod:`termios` extension module to multi-phase initialization +(:pep:`489`). From 9d593d7f9a4cd8671f9128a10f09a4507c0be812 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Mon, 7 Sep 2020 20:54:47 -0500 Subject: [PATCH 3/3] minor changes --- Modules/termios.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/Modules/termios.c b/Modules/termios.c index 77700bd9263770..cc0d5853f85e35 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -77,7 +77,7 @@ indexing in the cc array must be done using the symbolic constants defined\n\ in this module."); static PyObject * -termios_tcgetattr(PyObject *self, PyObject *args) +termios_tcgetattr(PyObject *module, PyObject *args) { int fd; if (!PyArg_ParseTuple(args, "O&:tcgetattr", @@ -85,7 +85,7 @@ termios_tcgetattr(PyObject *self, PyObject *args) return NULL; } - termiosmodulestate *state = PyModule_GetState(self); + termiosmodulestate *state = PyModule_GetState(module); struct termios mode; if (tcgetattr(fd, &mode) == -1) { return PyErr_SetFromErrno(state->TermiosError); @@ -155,7 +155,7 @@ queued output, or termios.TCSAFLUSH to change after transmitting all\n\ queued output and discarding all queued input. "); static PyObject * -termios_tcsetattr(PyObject *self, PyObject *args) +termios_tcsetattr(PyObject *module, PyObject *args) { int fd, when; PyObject *term; @@ -171,7 +171,7 @@ termios_tcsetattr(PyObject *self, PyObject *args) } /* Get the old mode, in case there are any hidden fields... */ - termiosmodulestate *state = PyModule_GetState(self); + termiosmodulestate *state = PyModule_GetState(module); struct termios mode; if (tcgetattr(fd, &mode) == -1) { return PyErr_SetFromErrno(state->TermiosError); @@ -229,7 +229,7 @@ A zero duration sends a break for 0.25-0.5 seconds; a nonzero duration\n\ has a system dependent meaning."); static PyObject * -termios_tcsendbreak(PyObject *self, PyObject *args) +termios_tcsendbreak(PyObject *module, PyObject *args) { int fd, duration; if (!PyArg_ParseTuple(args, "O&i:tcsendbreak", @@ -237,7 +237,7 @@ termios_tcsendbreak(PyObject *self, PyObject *args) return NULL; } - termiosmodulestate *state = PyModule_GetState(self); + termiosmodulestate *state = PyModule_GetState(module); if (tcsendbreak(fd, duration) == -1) { return PyErr_SetFromErrno(state->TermiosError); } @@ -251,7 +251,7 @@ PyDoc_STRVAR(termios_tcdrain__doc__, Wait until all output written to file descriptor fd has been transmitted."); static PyObject * -termios_tcdrain(PyObject *self, PyObject *args) +termios_tcdrain(PyObject *module, PyObject *args) { int fd; if (!PyArg_ParseTuple(args, "O&:tcdrain", @@ -259,7 +259,7 @@ termios_tcdrain(PyObject *self, PyObject *args) return NULL; } - termiosmodulestate *state = PyModule_GetState(self); + termiosmodulestate *state = PyModule_GetState(module); if (tcdrain(fd) == -1) { return PyErr_SetFromErrno(state->TermiosError); } @@ -276,7 +276,7 @@ queue, termios.TCOFLUSH for the output queue, or termios.TCIOFLUSH for\n\ both queues. "); static PyObject * -termios_tcflush(PyObject *self, PyObject *args) +termios_tcflush(PyObject *module, PyObject *args) { int fd, queue; if (!PyArg_ParseTuple(args, "O&i:tcflush", @@ -284,7 +284,7 @@ termios_tcflush(PyObject *self, PyObject *args) return NULL; } - termiosmodulestate *state = PyModule_GetState(self); + termiosmodulestate *state = PyModule_GetState(module); if (tcflush(fd, queue) == -1) { return PyErr_SetFromErrno(state->TermiosError); } @@ -301,7 +301,7 @@ termios.TCOON to restart output, termios.TCIOFF to suspend input,\n\ or termios.TCION to restart input."); static PyObject * -termios_tcflow(PyObject *self, PyObject *args) +termios_tcflow(PyObject *module, PyObject *args) { int fd, action; if (!PyArg_ParseTuple(args, "O&i:tcflow", @@ -309,7 +309,7 @@ termios_tcflow(PyObject *self, PyObject *args) return NULL; } - termiosmodulestate *state = PyModule_GetState(self); + termiosmodulestate *state = PyModule_GetState(module); if (tcflow(fd, action) == -1) { return PyErr_SetFromErrno(state->TermiosError); } @@ -1022,10 +1022,16 @@ termios_exec(PyObject *mod) return -1; } Py_INCREF(state->TermiosError); - PyModule_AddObject(mod, "error", state->TermiosError); + if (PyModule_AddObject(mod, "error", state->TermiosError) < 0) { + Py_DECREF(state->TermiosError); + return -1; + } while (constant->name != NULL) { - PyModule_AddIntConstant(mod, constant->name, constant->value); + if (PyModule_AddIntConstant( + mod, constant->name, constant->value) < 0) { + return -1; + } ++constant; } return 0; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy