Skip to content
21 changes: 11 additions & 10 deletions Lib/test/test_getpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
except ImportError:
pwd = None

try:
import msvcrt

except ImportError:
mscvrt = None

@mock.patch('os.environ')
class GetpassGetuserTest(unittest.TestCase):

Expand Down Expand Up @@ -162,15 +168,8 @@ def test_falls_back_to_stdin(self):
self.assertIn('Password:', stderr.getvalue())


try:
import msvcrt
except ImportError:
msvcrt_available = False
else:
msvcrt_available = True

@unittest.skipIf(msvcrt is None, 'tests require system with msvcrt (Windows)')
@unittest.skipUnless(support.MS_WINDOWS, "Windows-specific tests")
@unittest.skipUnless(msvcrt_available, 'tests require system with msvcrt (Windows)')
class WinGetpassTest(unittest.TestCase):

def test_uses_msvcrt_directly(self):
Expand All @@ -187,8 +186,10 @@ def test_uses_msvcrt_directly(self):
mock_stream.flush.assert_called()

def test_handles_backspace(self):
with mock.patch('msvcrt.getch') as getch, \
mock.patch('msvcrt.putch') as putch:
with (
mock.patch('msvcrt.getch') as getch,
mock.patch('msvcrt.putch') as putch,
):
getch.side_effect = [b'a', b'b', b'\b', b'c', b'\r']
result = getpass.win_getpass()
self.assertEqual(result, 'ac')
Expand Down
Loading