Skip to content

bpo-1635741: Port the termios to multi-phase init (PEP 489) #22139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Port the :mod:`termios` extension module to multi-phase initialization
(:pep:`489`).
182 changes: 101 additions & 81 deletions Modules/termios.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -79,31 +77,32 @@ 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;
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(module);
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;
Expand Down Expand Up @@ -156,36 +155,38 @@ 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;
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");
return NULL;
}

/* Get the old mode, in case there are any hidden fields... */
termiosmodulestate *state = modulestate_global;
if (tcgetattr(fd, &mode) == -1)
termiosmodulestate *state = PyModule_GetState(module);
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,
Expand All @@ -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);

Expand Down Expand Up @@ -226,15 +229,18 @@ 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",
fdconv, &fd, &duration))
fdconv, &fd, &duration)) {
return NULL;
if (tcsendbreak(fd, duration) == -1)
return PyErr_SetFromErrno(modulestate_global->TermiosError);
}

termiosmodulestate *state = PyModule_GetState(module);
if (tcsendbreak(fd, duration) == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}

Py_RETURN_NONE;
}
Expand All @@ -245,15 +251,18 @@ 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",
fdconv, &fd))
fdconv, &fd)) {
return NULL;
if (tcdrain(fd) == -1)
return PyErr_SetFromErrno(modulestate_global->TermiosError);
}

termiosmodulestate *state = PyModule_GetState(module);
if (tcdrain(fd) == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}

Py_RETURN_NONE;
}
Expand All @@ -267,15 +276,18 @@ 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",
fdconv, &fd, &queue))
fdconv, &fd, &queue)) {
return NULL;
if (tcflush(fd, queue) == -1)
return PyErr_SetFromErrno(modulestate_global->TermiosError);
}

termiosmodulestate *state = PyModule_GetState(module);
if (tcflush(fd, queue) == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}

Py_RETURN_NONE;
}
Expand All @@ -289,15 +301,18 @@ 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",
fdconv, &fd, &action))
fdconv, &fd, &action)) {
return NULL;
if (tcflow(fd, action) == -1)
return PyErr_SetFromErrno(modulestate_global->TermiosError);
}

termiosmodulestate *state = PyModule_GetState(module);
if (tcflow(fd, action) == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}

Py_RETURN_NONE;
}
Expand Down Expand Up @@ -997,44 +1012,49 @@ 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);
if (PyModule_AddObject(mod, "error", state->TermiosError) < 0) {
Py_DECREF(state->TermiosError);
return -1;
}

while (constant->name != NULL) {
PyModule_AddIntConstant(m, constant->name, constant->value);
if (PyModule_AddIntConstant(
mod, constant->name, constant->value) < 0) {
return -1;
}
++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);
}