[Python-checkins] r43495 - in python/trunk: Doc/lib/libgetpass.tex Lib/getpass.py Misc/NEWS

georg.brandl python-checkins at python.org
Fri Mar 31 20:42:17 CEST 2006


Author: georg.brandl
Date: Fri Mar 31 20:42:16 2006
New Revision: 43495

Modified:
   python/trunk/Doc/lib/libgetpass.tex
   python/trunk/Lib/getpass.py
   python/trunk/Misc/NEWS
Log:
Bug #1445068: getpass.getpass() can now be given an explicit stream
argument to specify where to write the prompt.


Modified: python/trunk/Doc/lib/libgetpass.tex
==============================================================================
--- python/trunk/Doc/lib/libgetpass.tex	(original)
+++ python/trunk/Doc/lib/libgetpass.tex	Fri Mar 31 20:42:16 2006
@@ -11,11 +11,15 @@
 The \module{getpass} module provides two functions:
 
 
-\begin{funcdesc}{getpass}{\optional{prompt}}
+\begin{funcdesc}{getpass}{\optional{prompt\optional{, stream}}}
   Prompt the user for a password without echoing.  The user is
   prompted using the string \var{prompt}, which defaults to
-  \code{'Password: '}.
+  \code{'Password: '}. On \UNIX, the prompt is written to the
+  file-like object \var{stream}, which defaults to
+  \code{sys.stdout} (this argument is ignored on Windows).
+
   Availability: Macintosh, \UNIX, Windows.
+  \versionadded[The \var{stream} parameter]{2.5}
 \end{funcdesc}
 
 

Modified: python/trunk/Lib/getpass.py
==============================================================================
--- python/trunk/Lib/getpass.py	(original)
+++ python/trunk/Lib/getpass.py	Fri Mar 31 20:42:16 2006
@@ -15,11 +15,14 @@
 
 __all__ = ["getpass","getuser"]
 
-def unix_getpass(prompt='Password: '):
+def unix_getpass(prompt='Password: ', stream=None):
     """Prompt for a password, with echo turned off.
+    The prompt is written on stream, by default stdout.
 
     Restore terminal settings at end.
     """
+    if stream is None:
+        stream = sys.stdout
 
     try:
         fd = sys.stdin.fileno()
@@ -32,18 +35,18 @@
     new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
     try:
         termios.tcsetattr(fd, termios.TCSADRAIN, new)
-        passwd = _raw_input(prompt)
+        passwd = _raw_input(prompt, stream)
     finally:
         termios.tcsetattr(fd, termios.TCSADRAIN, old)
 
-    sys.stdout.write('\n')
+    stream.write('\n')
     return passwd
 
 
-def win_getpass(prompt='Password: '):
+def win_getpass(prompt='Password: ', stream=None):
     """Prompt for password with echo off, using Windows getch()."""
     if sys.stdin is not sys.__stdin__:
-        return default_getpass(prompt)
+        return default_getpass(prompt, stream)
     import msvcrt
     for c in prompt:
         msvcrt.putch(c)
@@ -63,17 +66,19 @@
     return pw
 
 
-def default_getpass(prompt='Password: '):
-    print "Warning: Problem with getpass. Passwords may be echoed."
-    return _raw_input(prompt)
+def default_getpass(prompt='Password: ', stream=None):
+    print >>sys.stderr, "Warning: Problem with getpass. Passwords may be echoed."
+    return _raw_input(prompt, stream)
 
 
-def _raw_input(prompt=""):
+def _raw_input(prompt="", stream=None):
     # A raw_input() replacement that doesn't save the string in the
     # GNU readline history.
+    if stream is None:
+        stream = sys.stdout
     prompt = str(prompt)
     if prompt:
-        sys.stdout.write(prompt)
+        stream.write(prompt)
     line = sys.stdin.readline()
     if not line:
         raise EOFError

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Fri Mar 31 20:42:16 2006
@@ -487,6 +487,9 @@
 Library
 -------
 
+- Bug #1445068: getpass.getpass() can now be given an explicit stream
+  argument to specify where to write the prompt.
+
 - Patch #1462313, bug #1443328: the pickle modules now can handle classes
   that have __private names in their __slots__.
 


More information about the Python-checkins mailing list