diff -r 6dcc9628065c Lib/getpass.py --- a/Lib/getpass.py Tue Mar 19 15:03:26 2013 -0700 +++ b/Lib/getpass.py Tue Mar 19 15:30:40 2013 -0700 @@ -40,6 +40,7 @@ """ fd = None tty = None + passwd = None try: # Always try reading and writing directly on the tty first. fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY) diff -r 6dcc9628065c Lib/test/test_getpass.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/test/test_getpass.py Tue Mar 19 15:30:40 2013 -0700 @@ -0,0 +1,126 @@ +import getpass +import unittest +from unittest import mock +from test.support import run_unittest +from io import StringIO +import os + + +@mock.patch('os.environ') +class GetpassGetuserTest(unittest.TestCase): + + def test_username_takes_username_from_env(self, environ): + expected_name = 'some_name' + environ.get.return_value = expected_name + self.assertEqual(expected_name, getpass.getuser()) + + def test_username_priorities_of_env_values(self, environ): + environ.get.return_value = None + getpass.getuser() + self.assertEqual( + environ.get.call_args_list, + [mock.call(x) for x in ('LOGNAME', 'USER', 'LNAME', 'USERNAME')]) + + def test_username_falls_back_to_pwd(self, environ): + expected_name = 'some_name' + environ.get.return_value = None + with mock.patch('os.getuid') as uid,\ + mock.patch('pwd.getpwuid') as getpw: + uid.return_value = 42 + getpw.return_value = [expected_name] + self.assertEqual(expected_name, + getpass.getuser()) + getpw.assert_called_once_with(42) + + +class GetpassRawinputTest(unittest.TestCase): + def test_flushes_stream_after_prompt(self): + # see issue 1703 + stream = mock.Mock(spec=StringIO) + mock_input = mock.Mock(spec=StringIO) + mock_input.readline.return_value = 'input_string' + getpass._raw_input('some_prompt', stream, input=mock_input) + stream.flush.assert_called_once_with() + + def test_uses_stderr_as_default(self): + mock_input = mock.Mock(spec=StringIO) + mock_input.readline.return_value = 'input_string' + prompt = 'some_prompt' + with mock.patch('sys.stderr') as stderr: + getpass._raw_input(prompt, input=mock_input) + stderr.write.assert_called_once_with(prompt) + + @mock.patch('sys.stdin') + def test_uses_stdin_as_default_input(self, mock_input): + mock_input.readline.return_value = 'input_string' + getpass._raw_input(stream=StringIO()) + mock_input.readline.assert_called_once_with() + + def test_raises_on_empty_input(self): + mock_input = mock.Mock(spec=StringIO) + mock_input.readline.return_value = '' + self.assertRaises(EOFError, getpass._raw_input, input=mock_input) + + def test_trims_trailing_newline(self): + mock_input = mock.Mock(spec=StringIO) + mock_input.readline.return_value = 'test\n' + self.assertEqual('test', getpass._raw_input(input=mock_input)) + + +@unittest.skipUnless(os.name == 'posix', 'tests are for the unix version of getpass') +class UnixGetpassTest(unittest.TestCase): + def test_uses_tty_directly(self): + with mock.patch('os.open') as open,\ + mock.patch('os.fdopen'): + open.return_value = None + getpass.unix_getpass() + open.assert_called_once_with('/dev/tty', os.O_RDWR | os.O_NOCTTY) + + def test_resets_termios(self): + with mock.patch('os.open') as open, \ + mock.patch('os.fdopen'),\ + mock.patch('termios.tcgetattr') as tcgetattr, \ + mock.patch('termios.tcsetattr') as tcsetattr: + open.return_value = 3 + fake_attrs = [255, 255, 255, 255, 255] + tcgetattr.return_value = list(fake_attrs) + getpass.unix_getpass() + tcsetattr.assert_called_with(3, mock.ANY, fake_attrs) + + def test_flushes_stream_after_input(self): + # issue 7208 + with mock.patch('os.open') as open, \ + mock.patch('os.fdopen'), \ + mock.patch('termios.tcgetattr') as tcgetattr, \ + mock.patch('termios.tcsetattr') as tcsetattr: + open.return_value = 3 + mock_stream = mock.Mock(spec=StringIO) + getpass.unix_getpass(stream=mock_stream) + mock_stream.flush.assert_called_with() + + def test_falls_back_to_stdin(self): + with mock.patch('os.open') as os_open, \ + mock.patch('sys.stdin', callable=StringIO) as stdin: + os_open.side_effect = IOError + stdin.fileno.side_effect = AttributeError + getpass.unix_getpass() + stdin.readline.assert_called_once_with() + + def test_warns_about_echoed_password(self): + with mock.patch('warnings.warn') as warn, \ + mock.patch('sys.stdin', callable=StringIO) as stdin: + stdin.readline.return_value = 'a' + getpass.fallback_getpass() + warn.assert_called_once_with(mock.ANY, getpass.GetPassWarning, + stacklevel=mock.ANY) + + +def test_main(): + + run_unittest(GetpassGetuserTest, + GetpassRawinputTest, + UnixGetpassTest) + + +if __name__ == "__main__": + test_main() diff -r 6dcc9628065c Lib/test/test_sundry.py --- a/Lib/test/test_sundry.py Tue Mar 19 15:03:26 2013 -0700 +++ b/Lib/test/test_sundry.py Tue Mar 19 15:30:40 2013 -0700 @@ -41,7 +41,6 @@ import encodings import formatter - import getpass import html.entities import imghdr import keyword