@@ -21,7 +21,11 @@ def write(self, x):
21
21
sys .stdout = flushfile (sys .stdout )
22
22
23
23
def get_char (prompt = None ):
24
- """Read a line of text from standard input and return the equivalent char."""
24
+ """
25
+ Read a line of text from standard input and return the equivalent char;
26
+ if text is not a single char, user is prompted to retry. If line can't
27
+ be read, return None.
28
+ """
25
29
while True :
26
30
s = get_string (prompt )
27
31
if s is None :
@@ -34,7 +38,11 @@ def get_char(prompt=None):
34
38
print ("Retry: " , end = "" )
35
39
36
40
def get_float (prompt = None ):
37
- """Read a line of text from standard input and return the equivalent float."""
41
+ """
42
+ Read a line of text from standard input and return the equivalent float
43
+ as precisely as possible; if text does not represent a double, user is
44
+ prompted to retry. If line can't be read, return None.
45
+ """
38
46
while True :
39
47
s = get_string (prompt )
40
48
if s is None :
@@ -50,7 +58,11 @@ def get_float(prompt=None):
50
58
print ("Retry: " , end = "" )
51
59
52
60
def get_int (prompt = None ):
53
- """Read a line of text from standard input and return the equivalent int."""
61
+ """
62
+ Read a line of text from standard input and return the equivalent int;
63
+ if text does not represent an int, user is prompted to retry. If line
64
+ can't be read, return None.
65
+ """
54
66
while True :
55
67
s = get_string (prompt );
56
68
if s is None :
@@ -69,7 +81,11 @@ def get_int(prompt=None):
69
81
70
82
if sys .version_info .major != 3 :
71
83
def get_long (prompt = None ):
72
- """Read a line of text from standard input and return the equivalent long."""
84
+ """
85
+ Read a line of text from standard input and return the equivalent long;
86
+ if text does not represent a long, user is prompted to retry. If line
87
+ can't be read, return None.
88
+ """
73
89
while True :
74
90
s = get_string (prompt )
75
91
if s is None :
@@ -85,11 +101,18 @@ def get_long(prompt=None):
85
101
print ("Retry: " , end = "" )
86
102
87
103
def get_string (prompt = None ):
88
- """Read a line of text from standard input and return it as a string."""
104
+ """
105
+ Read a line of text from standard input and return it as a string,
106
+ sans trailing line ending. Supports CR (\r ), LF (\n ), and CRLF (\r \n )
107
+ as line endings. If user inputs only a line ending, returns "", not None.
108
+ Returns None upon error or no input whatsoever (i.e., just EOF).
109
+ """
89
110
try :
90
111
if prompt is not None :
91
112
print (prompt , end = "" )
92
113
s = sys .stdin .readline ()
114
+ if not s :
115
+ return None
93
116
return re .sub (r"(?:\r|\r\n|\n)$" , "" , s )
94
117
except ValueError :
95
118
return None
0 commit comments