Skip to content
Prev Previous commit
Next Next commit
Improve termios.tcsetwinsize().
Signed-off-by: Soumendra Ganguly <soumendraganguly@gmail.com>
  • Loading branch information
8vasu committed Dec 10, 2020
commit 0d2d35bd8e5d094881505d5b68f460831d113bac
2 changes: 1 addition & 1 deletion Doc/library/termios.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ The module defines the following functions:

Set the tty window size for file descriptor *fd* from *winsize*, which is
a list like the one returned by :func:`tcgetwinsize`. Requires
:const:`termios.TIOCSWINSZ`.
:const:`termios.TIOCGWINSZ` and :const:`termios.TIOCSWINSZ`.


.. seealso::
Expand Down
14 changes: 10 additions & 4 deletions Modules/termios.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ termios_tcgetwinsize_impl(PyObject *module, int fd)
#else
PyErr_SetString(PyExc_NotImplementedError, "termios.TIOCGWINSZ undefined");
return NULL;
#endif /* TIOCGWINSZ */
#endif /* defined(TIOCGWINSZ) */
}

/*[clinic input]
Expand All @@ -372,7 +372,7 @@ static PyObject *
termios_tcsetwinsize_impl(PyObject *module, int fd, PyObject *winsz)
/*[clinic end generated code: output=2ac3c9bb6eda83e1 input=c495180b2b932a30]*/
{
#if defined(TIOCSWINSZ)
#if defined(TIOCGWINSZ) && defined(TIOCSWINSZ)
if (!PyList_Check(winsz) || PyList_Size(winsz) != 2) {
PyErr_SetString(PyExc_TypeError,
"tcsetwinsize, arg 2: must be 2 element list");
Expand All @@ -381,6 +381,11 @@ termios_tcsetwinsize_impl(PyObject *module, int fd, PyObject *winsz)

termiosmodulestate *state = PyModule_GetState(module);
struct winsize w;
/* Get the old winsize, in case there are
more fields such as xpixel, ypixel */
if (ioctl(fd, TIOCGWINSZ, &w) == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}

w.ws_row = (unsigned short) PyLong_AsLong(PyList_GetItem(winsz, 0));
w.ws_col = (unsigned short) PyLong_AsLong(PyList_GetItem(winsz, 1));
Expand All @@ -394,9 +399,10 @@ termios_tcsetwinsize_impl(PyObject *module, int fd, PyObject *winsz)

Py_RETURN_NONE;
#else
PyErr_SetString(PyExc_NotImplementedError, "termios.TIOCSWINSZ undefined");
PyErr_SetString(PyExc_NotImplementedError,
"termios.TIOCGWINSZ and/or termios.TIOCSWINSZ undefined");
return NULL;
#endif /* TIOCSWINSZ */
#endif /* defined(TIOCGWINSZ) && defined(TIOCSWINSZ) */
}

static PyMethodDef termios_methods[] =
Expand Down