Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65eb913

Browse files
committed
Add POSIX pseudo-terminal functions.
Signed-off-by: Soumendra Ganguly <[email protected]>
1 parent e94edab commit 65eb913

File tree

6 files changed

+343
-9
lines changed

6 files changed

+343
-9
lines changed

Doc/library/os.rst

+43
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,17 @@ as internal buffering of data.
11081108
.. versionchanged:: 3.12
11091109
Added support for pipes on Windows.
11101110

1111+
1112+
.. function:: grantpt(fd, /)
1113+
1114+
Grant access to the slave pseudo-terminal device associated with the
1115+
master pseudo-terminal device to which the file descriptor *fd* refers.
1116+
1117+
Calls the C standard library function :c:func:`grantpt`.
1118+
1119+
.. availability:: Unix, not Emscripten, not WASI.
1120+
1121+
11111122
.. function:: isatty(fd, /)
11121123

11131124
Return ``True`` if the file descriptor *fd* is open and connected to a
@@ -1375,6 +1386,17 @@ or `the MSDN <https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windo
13751386
.. versionadded:: 3.3
13761387

13771388

1389+
.. function:: posix_openpt(oflag, /)
1390+
1391+
Open and return a file descriptor for a master pseudo-terminal device.
1392+
1393+
Calls the C standard library function :c:func:`posix_openpt`. The *oflag*
1394+
argument is used to set file status flags and file access modes as
1395+
specified in the manual page of :c:func:`posix_openpt` of your system.
1396+
1397+
.. availability:: Unix, not Emscripten, not WASI.
1398+
1399+
13781400
.. function:: preadv(fd, buffers, offset, flags=0, /)
13791401

13801402
Read from a file descriptor *fd* at a position of *offset* into mutable
@@ -1432,6 +1454,17 @@ or `the MSDN <https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windo
14321454
.. versionadded:: 3.7
14331455

14341456

1457+
.. function:: ptsname(fd, /)
1458+
1459+
Return the name of the slave pseudo-terminal device associated with the
1460+
master pseudo-terminal device to which the file descriptor *fd* refers.
1461+
1462+
Calls the C standard library function :c:func:`ptsname`, which is not
1463+
guaranteed to be thread-safe.
1464+
1465+
.. availability:: Unix, not Emscripten, not WASI.
1466+
1467+
14351468
.. function:: pwrite(fd, str, offset, /)
14361469

14371470
Write the bytestring in *str* to file descriptor *fd* at position of
@@ -1683,6 +1716,16 @@ or `the MSDN <https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windo
16831716
.. availability:: Unix.
16841717

16851718

1719+
.. function:: unlockpt(fd, /)
1720+
1721+
Unlock the slave pseudo-terminal device associated with the master
1722+
pseudo-terminal device to which the file descriptor *fd* refers.
1723+
1724+
Calls the C standard library function :c:func:`unlockpt`.
1725+
1726+
.. availability:: Unix, not Emscripten, not WASI.
1727+
1728+
16861729
.. function:: write(fd, str, /)
16871730

16881731
Write the bytestring in *str* to file descriptor *fd*.

Modules/clinic/posixmodule.c.h

+166-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

+114
Original file line numberDiff line numberDiff line change
@@ -7858,6 +7858,116 @@ os_sched_getaffinity_impl(PyObject *module, pid_t pid)
78587858
#endif /* HAVE_SCHED_H */
78597859

78607860

7861+
#ifdef HAVE_POSIX_OPENPT
7862+
/*[clinic input]
7863+
os.posix_openpt -> int
7864+
7865+
oflag: int
7866+
/
7867+
7868+
Open and return a file descriptor for a master pseudo-terminal device.
7869+
7870+
Performs a posix_openpt() C function call. The oflag argument is used to
7871+
set file status flags and file access modes as specified in the manual page
7872+
of posix_openpt() of your system.
7873+
[clinic start generated code]*/
7874+
7875+
static int
7876+
os_posix_openpt_impl(PyObject *module, int oflag)
7877+
/*[clinic end generated code: output=ee0bc2624305fc79 input=0de33d0e29693caa]*/
7878+
{
7879+
int fd;
7880+
7881+
fd = posix_openpt(oflag);
7882+
if (fd == -1)
7883+
posix_error();
7884+
7885+
return fd;
7886+
}
7887+
#endif /* HAVE_POSIX_OPENPT */
7888+
7889+
#ifdef HAVE_GRANTPT
7890+
/*[clinic input]
7891+
os.grantpt
7892+
7893+
fd: fildes
7894+
File descriptor of a master pseudo-terminal device.
7895+
/
7896+
7897+
Grant access to the slave pseudo-terminal device.
7898+
7899+
Performs a grantpt() C function call.
7900+
[clinic start generated code]*/
7901+
7902+
static PyObject *
7903+
os_grantpt_impl(PyObject *module, int fd)
7904+
/*[clinic end generated code: output=dfd580015cf548ab input=0668e3b96760e849]*/
7905+
{
7906+
int ret;
7907+
PyOS_sighandler_t sig_saved;
7908+
7909+
sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
7910+
ret = grantpt(fd);
7911+
PyOS_setsig(SIGCHLD, sig_saved);
7912+
7913+
if (ret == -1)
7914+
return posix_error();
7915+
7916+
Py_RETURN_NONE;
7917+
}
7918+
#endif /* HAVE_GRANTPT */
7919+
7920+
#ifdef HAVE_UNLOCKPT
7921+
/*[clinic input]
7922+
os.unlockpt
7923+
7924+
fd: fildes
7925+
File descriptor of a master pseudo-terminal device.
7926+
/
7927+
7928+
Unlock a pseudo-terminal master/slave pair.
7929+
7930+
Performs an unlockpt() C function call.
7931+
[clinic start generated code]*/
7932+
7933+
static PyObject *
7934+
os_unlockpt_impl(PyObject *module, int fd)
7935+
/*[clinic end generated code: output=e08d354dec12d30c input=de7ab1f59f69a2b4]*/
7936+
{
7937+
if (unlockpt(fd) == -1)
7938+
return posix_error();
7939+
7940+
Py_RETURN_NONE;
7941+
}
7942+
#endif /* HAVE_UNLOCKPT */
7943+
7944+
#ifdef HAVE_PTSNAME
7945+
/*[clinic input]
7946+
os.ptsname
7947+
7948+
fd: fildes
7949+
File descriptor of a master pseudo-terminal device.
7950+
/
7951+
7952+
Return the name of the slave pseudo-terminal device.
7953+
7954+
Performs a ptsname() C function call.
7955+
[clinic start generated code]*/
7956+
7957+
static PyObject *
7958+
os_ptsname_impl(PyObject *module, int fd)
7959+
/*[clinic end generated code: output=ef300fadc5675872 input=a00d870c51570c2a]*/
7960+
{
7961+
char *name;
7962+
7963+
name = ptsname(fd);
7964+
if (name == NULL)
7965+
return posix_error();
7966+
7967+
return PyUnicode_DecodeFSDefault(name);
7968+
}
7969+
#endif /* HAVE_PTSNAME */
7970+
78617971
/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
78627972
#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
78637973
# define DEV_PTY_FILE "/dev/ptc"
@@ -15486,6 +15596,10 @@ static PyMethodDef posix_methods[] = {
1548615596
OS_SCHED_YIELD_METHODDEF
1548715597
OS_SCHED_SETAFFINITY_METHODDEF
1548815598
OS_SCHED_GETAFFINITY_METHODDEF
15599+
OS_POSIX_OPENPT_METHODDEF
15600+
OS_GRANTPT_METHODDEF
15601+
OS_UNLOCKPT_METHODDEF
15602+
OS_PTSNAME_METHODDEF
1548915603
OS_OPENPTY_METHODDEF
1549015604
OS_LOGIN_TTY_METHODDEF
1549115605
OS_FORKPTY_METHODDEF

0 commit comments

Comments
 (0)