Skip to content
Prev Previous commit
Next Next commit
Add TIOCGSIZE support to termios.tcgetwinsize()
Signed-off-by: Soumendra Ganguly <soumendraganguly@gmail.com>
  • Loading branch information
8vasu committed Dec 14, 2020
commit 628b7dcc32efe8d1bc5a52bf167547f4092412a0
3 changes: 2 additions & 1 deletion Doc/library/termios.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ The module defines the following functions:
.. function:: tcgetwinsize(fd)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one follow up PR as I forgot to fix it up here, these need versionadded tags. I'll take care of it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gpshead Thank you for making the improvements!


Return a list ``[ws_row, ws_col]`` containing the tty window size for file
descriptor *fd*. Requires :const:`termios.TIOCGWINSZ`.
descriptor *fd*. Requires :const:`termios.TIOCGWINSZ` or
:const:`termios.TIOCGSIZE`.


.. function:: tcsetwinsize(fd, winsize)
Expand Down
25 changes: 24 additions & 1 deletion Modules/termios.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,28 @@ termios_tcgetwinsize_impl(PyObject *module, int fd)
return NULL;
}
return v;
#elif defined(TIOCGSIZE)
termiosmodulestate *state = PyModule_GetState(module);
struct ttysize s;
if (ioctl(fd, TIOCGSIZE, &s) == -1) {
return PyErr_SetFromErrno(state->TermiosError);
}

PyObject *v;
if (!(v = PyList_New(2))) {
return NULL;
}

PyList_SetItem(v, 0, PyLong_FromLong((long)s.ts_lines));
PyList_SetItem(v, 1, PyLong_FromLong((long)s.ts_cols));
if (PyErr_Occurred()) {
Py_DECREF(v);
return NULL;
}
return v;
#else
PyErr_SetString(PyExc_NotImplementedError, "termios.TIOCGWINSZ undefined");
PyErr_SetString(PyExc_NotImplementedError,
"termios.TIOCGWINSZ and termios.TIOCGSIZE undefined");
return NULL;
#endif /* defined(TIOCGWINSZ) */
}
Expand Down Expand Up @@ -933,6 +953,9 @@ static struct constant {
#ifdef TIOCGSERIAL
{"TIOCGSERIAL", TIOCGSERIAL},
#endif
#ifdef TIOCGSIZE
{"TIOCGSIZE", TIOCGSIZE},
#endif
#ifdef TIOCGSOFTCAR
{"TIOCGSOFTCAR", TIOCGSOFTCAR},
#endif
Expand Down