[issue11466] getpass.getpass doesn't close tty file

Steffen Daode Nurpmeso report at bugs.python.org
Fri Mar 11 14:16:35 CET 2011


Steffen Daode Nurpmeso <sdaoden at googlemail.com> added the comment:

This new patch (11466.2.patch) also includes the #11236 patch, 
because if ^C would work as expected then that would not close the 
fd either. 
It's identical to the first patch beside that.

----------
Added file: http://bugs.python.org/file21081/11466.2.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11466>
_______________________________________
-------------- next part --------------
diff --git a/Lib/getpass.py b/Lib/getpass.py
--- a/Lib/getpass.py
+++ b/Lib/getpass.py
@@ -62,20 +62,30 @@
         try:
             old = termios.tcgetattr(fd)     # a copy to save
             new = old[:]
-            new[3] &= ~(termios.ECHO|termios.ISIG)  # 3 == 'lflags'
+            new[3] &= ~termios.ECHO  # 3 == 'lflags'
             tcsetattr_flags = termios.TCSAFLUSH
+            do_close = False
             if hasattr(termios, 'TCSASOFT'):
                 tcsetattr_flags |= termios.TCSASOFT
             try:
                 termios.tcsetattr(fd, tcsetattr_flags, new)
                 passwd = _raw_input(prompt, stream, input=input)
+            except (EOFError, KeyboardInterrupt):
+                do_close = True
+                raise
             finally:
                 termios.tcsetattr(fd, tcsetattr_flags, old)
-                stream.flush()  # issue7208
+                if do_close:
+                    stream.write('\n')
+                    stream.close()
+                else:
+                    stream.flush()  # issue7208
         except termios.error as e:
             if passwd is not None:
                 # _raw_input succeeded.  The final tcsetattr failed.  Reraise
                 # instead of leaving the terminal in an unknown state.
+                stream.write('\n')
+                stream.close()
                 raise
             # We can't control the tty or stdin.  Give up and use normal IO.
             # fallback_getpass() raises an appropriate warning.
@@ -83,6 +93,8 @@
             passwd = fallback_getpass(prompt, stream)
 
     stream.write('\n')
+    if tty is not None:
+        stream.close()
     return passwd
 
 


More information about the Python-bugs-list mailing list