@@ -20,33 +20,51 @@ def write(self, x):
20
20
sys .stderr = flushfile (sys .stderr )
21
21
sys .stdout = flushfile (sys .stdout )
22
22
23
- def get_char ():
24
- """Read a line of text from standard input and return the equivalent char."""
23
+ def get_char (prompt = None ):
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
- s = get_string ()
30
+ s = get_string (prompt )
27
31
if s is None :
28
32
return None
29
33
if len (s ) == 1 :
30
34
return s [0 ]
31
- print ("Retry: " , end = "" )
32
35
33
- def get_float ():
34
- """Read a line of text from standard input and return the equivalent float."""
36
+ # temporarily here for backwards compatibility
37
+ if prompt is None :
38
+ print ("Retry: " , end = "" )
39
+
40
+ def get_float (prompt = None ):
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
+ """
35
46
while True :
36
- s = get_string ()
47
+ s = get_string (prompt )
37
48
if s is None :
38
49
return None
39
50
if len (s ) > 0 and re .search (r"^[+-]?\d*(?:\.\d*)?$" , s ):
40
51
try :
41
52
return float (s )
42
53
except ValueError :
43
54
pass
44
- print ("Retry: " , end = "" )
45
55
46
- def get_int ():
47
- """Read a line of text from standard input and return the equivalent int."""
56
+ # temporarily here for backwards compatibility
57
+ if prompt is None :
58
+ print ("Retry: " , end = "" )
59
+
60
+ def get_int (prompt = None ):
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
+ """
48
66
while True :
49
- s = get_string ();
67
+ s = get_string (prompt );
50
68
if s is None :
51
69
return None
52
70
if re .search (r"^[+-]?\d+$" , s ):
@@ -56,26 +74,45 @@ def get_int():
56
74
return i
57
75
except ValueError :
58
76
pass
59
- print ("Retry: " , end = "" )
77
+
78
+ # temporarily here for backwards compatibility
79
+ if prompt is None :
80
+ print ("Retry: " , end = "" )
60
81
61
82
if sys .version_info .major != 3 :
62
- def get_long ():
63
- """Read a line of text from standard input and return the equivalent long."""
83
+ def get_long (prompt = None ):
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
+ """
64
89
while True :
65
- s = get_string ();
90
+ s = get_string (prompt )
66
91
if s is None :
67
92
return None
68
93
if re .search (r"^[+-]?\d+$" , s ):
69
94
try :
70
95
return long (s , 10 )
71
96
except ValueError :
72
97
pass
73
- print ("Retry: " , end = "" )
74
98
75
- def get_string ():
76
- """Read a line of text from standard input and return it as a string."""
99
+ # temporarily here for backwards compatibility
100
+ if prompt is None :
101
+ print ("Retry: " , end = "" )
102
+
103
+ def get_string (prompt = None ):
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
+ """
77
110
try :
111
+ if prompt is not None :
112
+ print (prompt , end = "" )
78
113
s = sys .stdin .readline ()
114
+ if not s :
115
+ return None
79
116
return re .sub (r"(?:\r|\r\n|\n)$" , "" , s )
80
117
except ValueError :
81
118
return None
0 commit comments