Skip to content

gh-85283: Build termios extension with the limited C API #116928

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 1 commit into from
Mar 17, 2024
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
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,8 @@ Build Changes
* Building CPython now requires a compiler with support for the C11 atomic
library, GCC built-in atomic functions, or MSVC interlocked intrinsics.

* The ``errno``, ``fcntl``, ``grp``, ``md5``, ``pwd``, ``resource``, ``winsound``,
* The ``errno``, ``fcntl``, ``grp``, ``md5``, ``pwd``, ``resource``,
``termios``, ``winsound``,
``_ctypes_test``, ``_multiprocessing.posixshmem``, ``_scproxy``, ``_stat``,
``_testimportmultiple`` and ``_uuid`` C extensions are now built with the
:ref:`limited C API <limited-c-api>`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
The ``fcntl``, ``grp`` and ``pwd`` C extensions are now built with the :ref:`limited
C API <limited-c-api>`. (Contributed by Victor Stinner in :gh:`85283`.)
The ``fcntl``, ``grp``, ``pwd`` and ``termios`` C extensions are now
built with the :ref:`limited C API <limited-c-api>`. Patch by Victor Stinner.
29 changes: 16 additions & 13 deletions Modules/clinic/termios.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 22 additions & 16 deletions Modules/termios.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/* termios.c -- POSIX terminal I/O module implementation. */

#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
// Need limited C API version 3.13 for PyLong_AsInt()
// in code generated by Argument Clinic.
#include "pyconfig.h" // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
# define Py_LIMITED_API 0x030d0000
#endif

#include "Python.h"

#include <string.h> // memset()
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h> // _POSIX_VDISABLE

// On QNX 6, struct termio must be declared by including sys/termio.h
// if TCGETA, TCSETA, TCSETAW, or TCSETAF are used. sys/termio.h must
// be included before termios.h or it will generate an error.
Expand All @@ -19,28 +27,26 @@
# define CTRL(c) ((c)&037)
#endif

// We could do better. Check bpo-32660
#if defined(__sun)
/* We could do better. Check issue-32660 */
#include <sys/filio.h>
#include <sys/sockio.h>
# include <sys/filio.h>
# include <sys/sockio.h>
#endif

#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h> // _POSIX_VDISABLE

/* HP-UX requires that this be included to pick up MDCD, MCTS, MDSR,
* MDTR, MRI, and MRTS (apparently used internally by some things
* defined as macros; these are not used here directly).
*/
#ifdef HAVE_SYS_MODEM_H
#include <sys/modem.h>
# include <sys/modem.h>
#endif

/* HP-UX requires that this be included to pick up TIOCGPGRP and friends */
#ifdef HAVE_SYS_BSDTTY_H
#include <sys/bsdtty.h>
# include <sys/bsdtty.h>
#endif


/*[clinic input]
module termios
[clinic start generated code]*/
Expand Down Expand Up @@ -120,7 +126,7 @@ termios_tcgetattr_impl(PyObject *module, int fd)
v = PyBytes_FromStringAndSize(&ch, 1);
if (v == NULL)
goto err;
PyList_SET_ITEM(cc, i, v);
PyList_SetItem(cc, i, v);
}

/* Convert the MIN and TIME slots to integer. On some systems, the
Expand Down Expand Up @@ -154,7 +160,7 @@ termios_tcgetattr_impl(PyObject *module, int fd)
Py_DECREF(v); \
goto err; \
} \
PyList_SET_ITEM(v, index, l); \
PyList_SetItem(v, index, l); \
} while (0)

ADD_LONG_ITEM(0, mode.c_iflag);
Expand All @@ -165,7 +171,7 @@ termios_tcgetattr_impl(PyObject *module, int fd)
ADD_LONG_ITEM(5, ospeed);
#undef ADD_LONG_ITEM

PyList_SET_ITEM(v, 6, cc);
PyList_SetItem(v, 6, cc);
return v;
err:
Py_DECREF(cc);
Expand Down Expand Up @@ -214,7 +220,7 @@ termios_tcsetattr_impl(PyObject *module, int fd, int when, PyObject *term)

speed_t ispeed, ospeed;
#define SET_FROM_LIST(TYPE, VAR, LIST, N) do { \
PyObject *item = PyList_GET_ITEM(LIST, N); \
PyObject *item = PyList_GetItem(LIST, N); \
long num = PyLong_AsLong(item); \
if (num == -1 && PyErr_Occurred()) { \
return NULL; \
Expand All @@ -230,7 +236,7 @@ termios_tcsetattr_impl(PyObject *module, int fd, int when, PyObject *term)
SET_FROM_LIST(speed_t, ospeed, term, 5);
#undef SET_FROM_LIST

PyObject *cc = PyList_GET_ITEM(term, 6);
PyObject *cc = PyList_GetItem(term, 6);
if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) {
PyErr_Format(PyExc_TypeError,
"tcsetattr: attributes[6] must be %d element list",
Expand Down