2007-08-15 11:28:22 -03:00
|
|
|
:mod:`termios` --- POSIX style tty control
|
|
|
|
==========================================
|
|
|
|
|
|
|
|
.. module:: termios
|
|
|
|
:platform: Unix
|
|
|
|
:synopsis: POSIX style tty control.
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: POSIX; I/O control
|
|
|
|
pair: tty; I/O control
|
|
|
|
|
|
|
|
This module provides an interface to the POSIX calls for tty I/O control. For a
|
|
|
|
complete description of these calls, see the POSIX or Unix manual pages. It is
|
|
|
|
only available for those Unix versions that support POSIX *termios* style tty
|
|
|
|
I/O control (and then only if configured at installation time).
|
|
|
|
|
|
|
|
All functions in this module take a file descriptor *fd* as their first
|
|
|
|
argument. This can be an integer file descriptor, such as returned by
|
2010-09-15 08:25:11 -03:00
|
|
|
``sys.stdin.fileno()``, or a :term:`file object`, such as ``sys.stdin`` itself.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This module also defines all the constants needed to work with the functions
|
|
|
|
provided here; these have the same name as their counterparts in C. Please
|
|
|
|
refer to your system documentation for more information on using these terminal
|
|
|
|
control interfaces.
|
|
|
|
|
|
|
|
The module defines the following functions:
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: tcgetattr(fd)
|
|
|
|
|
|
|
|
Return a list containing the tty attributes for file descriptor *fd*, as
|
|
|
|
follows: ``[iflag, oflag, cflag, lflag, ispeed, ospeed, cc]`` where *cc* is a
|
|
|
|
list of the tty special characters (each a string of length 1, except the
|
|
|
|
items with indices :const:`VMIN` and :const:`VTIME`, which are integers when
|
|
|
|
these fields are defined). The interpretation of the flags and the speeds as
|
|
|
|
well as the indexing in the *cc* array must be done using the symbolic
|
|
|
|
constants defined in the :mod:`termios` module.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: tcsetattr(fd, when, attributes)
|
|
|
|
|
|
|
|
Set the tty attributes for file descriptor *fd* from the *attributes*, which is
|
|
|
|
a list like the one returned by :func:`tcgetattr`. The *when* argument
|
|
|
|
determines when the attributes are changed: :const:`TCSANOW` to change
|
|
|
|
immediately, :const:`TCSADRAIN` to change after transmitting all queued output,
|
|
|
|
or :const:`TCSAFLUSH` to change after transmitting all queued output and
|
|
|
|
discarding all queued input.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: tcsendbreak(fd, duration)
|
|
|
|
|
|
|
|
Send a break on file descriptor *fd*. A zero *duration* sends a break for 0.25
|
|
|
|
--0.5 seconds; a nonzero *duration* has a system dependent meaning.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: tcdrain(fd)
|
|
|
|
|
|
|
|
Wait until all output written to file descriptor *fd* has been transmitted.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: tcflush(fd, queue)
|
|
|
|
|
|
|
|
Discard queued data on file descriptor *fd*. The *queue* selector specifies
|
|
|
|
which queue: :const:`TCIFLUSH` for the input queue, :const:`TCOFLUSH` for the
|
|
|
|
output queue, or :const:`TCIOFLUSH` for both queues.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: tcflow(fd, action)
|
|
|
|
|
|
|
|
Suspend or resume input or output on file descriptor *fd*. The *action*
|
|
|
|
argument can be :const:`TCOOFF` to suspend output, :const:`TCOON` to restart
|
|
|
|
output, :const:`TCIOFF` to suspend input, or :const:`TCION` to restart input.
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Module :mod:`tty`
|
|
|
|
Convenience functions for common terminal control operations.
|
|
|
|
|
|
|
|
|
Merged revisions 74821,74828-74831,74833,74835 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k
................
r74821 | georg.brandl | 2009-09-16 11:42:19 +0200 (Mi, 16 Sep 2009) | 1 line
#6885: run python 3 as python3.
................
r74828 | georg.brandl | 2009-09-16 16:23:20 +0200 (Mi, 16 Sep 2009) | 1 line
Use true booleans.
................
r74829 | georg.brandl | 2009-09-16 16:24:29 +0200 (Mi, 16 Sep 2009) | 1 line
Small PEP8 correction.
................
r74830 | georg.brandl | 2009-09-16 16:36:22 +0200 (Mi, 16 Sep 2009) | 1 line
Use true booleans.
................
r74831 | georg.brandl | 2009-09-16 17:54:04 +0200 (Mi, 16 Sep 2009) | 1 line
Use true booleans and PEP8 for argdefaults.
................
r74833 | georg.brandl | 2009-09-16 17:58:14 +0200 (Mi, 16 Sep 2009) | 1 line
Last round of adapting style of documenting argument default values.
................
r74835 | georg.brandl | 2009-09-16 18:00:31 +0200 (Mi, 16 Sep 2009) | 33 lines
Merged revisions 74817-74820,74822-74824 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74817 | georg.brandl | 2009-09-16 11:05:11 +0200 (Mi, 16 Sep 2009) | 1 line
Make deprecation notices as visible as warnings are right now.
........
r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line
#6880: add reference to classes section in exceptions section, which comes earlier.
........
r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line
#6876: fix base class constructor invocation in example.
........
r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line
#6891: comment out dead link to Unicode article.
........
r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line
#5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign.
........
r74823 | georg.brandl | 2009-09-16 15:06:22 +0200 (Mi, 16 Sep 2009) | 1 line
Remove strange trailing commas.
........
r74824 | georg.brandl | 2009-09-16 15:11:06 +0200 (Mi, 16 Sep 2009) | 1 line
#6892: fix optparse example involving help option.
........
................
2009-09-16 13:05:59 -03:00
|
|
|
.. _termios-example:
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
Here's a function that prompts for a password with echoing turned off. Note the
|
|
|
|
technique using a separate :func:`tcgetattr` call and a :keyword:`try` ...
|
|
|
|
:keyword:`finally` statement to ensure that the old tty attributes are restored
|
|
|
|
exactly no matter what happens::
|
|
|
|
|
Merged revisions 75231 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r75231 | benjamin.peterson | 2009-10-04 09:49:41 -0500 (Sun, 04 Oct 2009) | 166 lines
Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line
Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567.
........
r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line
unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866.
........
r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line
Typo fix.
........
r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line
Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568.
........
r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line
Remove an extraneous space in unittest documentation.
........
r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line
Py_SetPythonHome uses static storage #6913
........
r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line
Add Armin Ronacher.
........
r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line
kill bare except
........
r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line
pep 8 defaults
........
r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line
rationalize a bit
........
r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line
#6932: remove paragraph that advises relying on __del__ being called.
........
r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line
use macros
........
r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6936: for interactive use, quit() is just fine.
........
r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line
#6905: use better exception messages in inspect when the argument is of the wrong type.
........
r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line
#6938: "ident" is always a string, so use a format code which works.
........
r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line
Use str.format() to fix beginner's mistake with %-style string formatting.
........
r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line
Optimize optimization and fix method name in docstring.
........
r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6925: rewrite docs for locals() and vars() a bit.
........
r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line
#6930: clarify description about byteorder handling in UTF decoder routines.
........
r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line
#6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple.
........
r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line
Update bug tracker reference.
........
r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line
#6946: fix duplicate index entries for datetime classes.
........
r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line
Fix references to threading.enumerate().
........
r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line
Add Doug.
........
r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line
Add Mark Summerfield.
........
r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line
Fix encoding name.
........
r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line
Fixed a typo, and added sections on optimization and using arbitrary objects as messages.
........
r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line
fix typos/rephrase
........
r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line
comment out ugly xxx
........
r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line
Tidied up name of parameter in StreamHandler
........
r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line
Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956.
........
r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line
Documentation improvement for load_tests protocol in unittest. Issue 6515.
........
r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines
Patch from Thomas Barr so that csv.Sniffer will set doublequote property.
Closes issue 6606.
........
r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line
Issue #7014: logging: Improved IronPython 2.6 compatibility.
........
r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines
Issue 7008: Better document str.title and show how to work around the apostrophe problem.
........
r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line
test logging
........
................
2009-10-04 11:54:52 -03:00
|
|
|
def getpass(prompt="Password: "):
|
2007-08-15 11:28:22 -03:00
|
|
|
import termios, sys
|
|
|
|
fd = sys.stdin.fileno()
|
|
|
|
old = termios.tcgetattr(fd)
|
|
|
|
new = termios.tcgetattr(fd)
|
|
|
|
new[3] = new[3] & ~termios.ECHO # lflags
|
|
|
|
try:
|
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, new)
|
2007-12-02 18:48:17 -04:00
|
|
|
passwd = input(prompt)
|
2007-08-15 11:28:22 -03:00
|
|
|
finally:
|
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
|
|
|
return passwd
|
|
|
|
|