• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`!tty` --- Terminal control functions
2==========================================
3
4.. module:: tty
5   :platform: Unix
6   :synopsis: Utility functions that perform common terminal control operations.
7
8.. moduleauthor:: Steen Lumholt
9.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
10
11**Source code:** :source:`Lib/tty.py`
12
13--------------
14
15The :mod:`tty` module defines functions for putting the tty into cbreak and raw
16modes.
17
18.. availability:: Unix.
19
20Because it requires the :mod:`termios` module, it will work only on Unix.
21
22The :mod:`tty` module defines the following functions:
23
24
25.. function:: cfmakeraw(mode)
26
27   Convert the tty attribute list *mode*, which is a list like the one returned
28   by :func:`termios.tcgetattr`, to that of a tty in raw mode.
29
30   .. versionadded:: 3.12
31
32
33.. function:: cfmakecbreak(mode)
34
35   Convert the tty attribute list *mode*, which is a list like the one returned
36   by :func:`termios.tcgetattr`, to that of a tty in cbreak mode.
37
38   This clears the ``ECHO`` and ``ICANON`` local mode flags in *mode* as well
39   as setting the minimum input to 1 byte with no delay.
40
41   .. versionadded:: 3.12
42
43   .. versionchanged:: 3.12.2
44      The ``ICRNL`` flag is no longer cleared. This matches Linux and macOS
45      ``stty cbreak`` behavior and what :func:`setcbreak` historically did.
46
47
48.. function:: setraw(fd, when=termios.TCSAFLUSH)
49
50   Change the mode of the file descriptor *fd* to raw. If *when* is omitted, it
51   defaults to :const:`termios.TCSAFLUSH`, and is passed to
52   :func:`termios.tcsetattr`. The return value of :func:`termios.tcgetattr`
53   is saved before setting *fd* to raw mode; this value is returned.
54
55   .. versionchanged:: 3.12
56      The return value is now the original tty attributes, instead of ``None``.
57
58
59.. function:: setcbreak(fd, when=termios.TCSAFLUSH)
60
61   Change the mode of file descriptor *fd* to cbreak. If *when* is omitted, it
62   defaults to :const:`termios.TCSAFLUSH`, and is passed to
63   :func:`termios.tcsetattr`. The return value of :func:`termios.tcgetattr`
64   is saved before setting *fd* to cbreak mode; this value is returned.
65
66   This clears the ``ECHO`` and ``ICANON`` local mode flags as well as setting
67   the minimum input to 1 byte with no delay.
68
69   .. versionchanged:: 3.12
70      The return value is now the original tty attributes, instead of ``None``.
71
72   .. versionchanged:: 3.12.2
73      The ``ICRNL`` flag is no longer cleared. This restores the behavior
74      of Python 3.11 and earlier as well as matching what Linux, macOS, & BSDs
75      describe in their ``stty(1)`` man pages regarding cbreak mode.
76
77
78.. seealso::
79
80   Module :mod:`termios`
81      Low-level terminal control interface.
82
83