Skip to content

Commit

Permalink
Restore Python 3.x compatibility (raw_input() -> input())
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jun 29, 2014
1 parent 9f31767 commit dad593c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
11 changes: 9 additions & 2 deletions humanfriendly.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# URL: https://humanfriendly.readthedocs.org

# Semi-standard module versioning.
__version__ = '1.9.3'
__version__ = '1.9.4'

# Standard library modules.
import math
Expand All @@ -15,6 +15,13 @@
import sys
import time

try:
# Python 2.x.
interactive_prompt = raw_input
except NameError:
# Python 3.x.
interactive_prompt = input

# Common disk size units, used for formatting and parsing.
disk_size_units = (dict(prefix='b', divider=1, singular='byte', plural='bytes'),
dict(prefix='k', divider=1024**1, singular='KB', plural='KB'),
Expand Down Expand Up @@ -294,7 +301,7 @@ def prompt_for_choice(choices, default=None):
# Loop until a valid choice is made.
prompt = "Enter your choice as a number or unique substring (Ctrl-C aborts): "
while True:
input = raw_input(prompt).strip()
input = interactive_prompt(prompt).strip()
# Make sure the user entered something.
if not input:
if default is not None:
Expand Down
17 changes: 9 additions & 8 deletions humanfriendly_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,34 @@ def test_spinner(self):
self.assertEqual(sorted(set(lines)), sorted(lines))

def test_prompt_for_choice(self):
interactive_prompt = humanfriendly.interactive_prompt
try:
# Choice selection by full string match.
humanfriendly.raw_input = lambda prompt: 'foo'
humanfriendly.interactive_prompt = lambda prompt: 'foo'
self.assertEqual(humanfriendly.prompt_for_choice(['foo', 'bar']), 'foo')
# Choice selection by substring input.
humanfriendly.raw_input = lambda prompt: 'f'
humanfriendly.interactive_prompt = lambda prompt: 'f'
self.assertEqual(humanfriendly.prompt_for_choice(['foo', 'bar']), 'foo')
# Choice selection by number.
humanfriendly.raw_input = lambda prompt: '2'
humanfriendly.interactive_prompt = lambda prompt: '2'
self.assertEqual(humanfriendly.prompt_for_choice(['foo', 'bar']), 'bar')
# Choice selection by going with the default.
humanfriendly.raw_input = lambda prompt: ''
humanfriendly.interactive_prompt = lambda prompt: ''
self.assertEqual(humanfriendly.prompt_for_choice(['foo', 'bar'], default='bar'), 'bar')
# Invalid substrings are refused.
responses = ['', 'q', 'z']
humanfriendly.raw_input = lambda prompt: responses.pop(0)
humanfriendly.interactive_prompt = lambda prompt: responses.pop(0)
self.assertEqual(humanfriendly.prompt_for_choice(['foo', 'bar', 'baz']), 'baz')
# Choice selection by substring input requires an unambiguous substring match.
responses = ['a', 'q']
humanfriendly.raw_input = lambda prompt: responses.pop(0)
humanfriendly.interactive_prompt = lambda prompt: responses.pop(0)
self.assertEqual(humanfriendly.prompt_for_choice(['foo', 'bar', 'baz', 'qux']), 'qux')
# Invalid numbers are refused.
responses = ['42', '2']
humanfriendly.raw_input = lambda prompt: responses.pop(0)
humanfriendly.interactive_prompt = lambda prompt: responses.pop(0)
self.assertEqual(humanfriendly.prompt_for_choice(['foo', 'bar', 'baz']), 'bar')
finally:
humanfriendly.raw_input = raw_input
humanfriendly.interactive_prompt = interactive_prompt

if __name__ == '__main__':
unittest.main()

0 comments on commit dad593c

Please sign in to comment.