diff --git a/Lib/getpass.py b/Lib/getpass.py --- a/Lib/getpass.py +++ b/Lib/getpass.py @@ -64,18 +64,28 @@ new = old[:] new[3] &= ~(termios.ECHO|termios.ISIG) # 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: + 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