1
1
from __future__ import print_function
2
2
3
3
import inspect
4
+ import os
4
5
import re
5
6
import sys
6
7
7
8
from distutils .sysconfig import get_python_lib
8
9
from os .path import abspath , join
9
10
from termcolor import colored
10
- from traceback import extract_tb , format_list , format_exception_only , format_exception
11
+ from traceback import format_exception
11
12
12
13
13
- class flushfile ():
14
+ class _flushfile ():
14
15
"""
15
16
Disable buffering for standard output and standard error.
16
17
@@ -28,23 +29,11 @@ def write(self, x):
28
29
self .f .flush ()
29
30
30
31
31
- sys .stderr = flushfile (sys .stderr )
32
- sys .stdout = flushfile (sys .stdout )
32
+ sys .stderr = _flushfile (sys .stderr )
33
+ sys .stdout = _flushfile (sys .stdout )
33
34
34
35
35
- def eprint (* args , ** kwargs ):
36
- """
37
- Print an error message to standard error, prefixing it with
38
- file name and line number from which method was called.
39
- """
40
- end = kwargs .get ("end" , "\n " )
41
- sep = kwargs .get ("sep" , " " )
42
- (filename , lineno ) = inspect .stack ()[1 ][1 :3 ]
43
- print ("{}:{}: " .format (filename , lineno ), end = "" )
44
- print (* args , end = end , file = sys .stderr , sep = sep )
45
-
46
-
47
- def formatException (type , value , tb ):
36
+ def _formatException (type , value , tb ):
48
37
"""
49
38
Format traceback, darkening entries from global site-packages directories
50
39
and user-specific site-packages directory.
@@ -67,28 +56,18 @@ def formatException(type, value, tb):
67
56
return "" .join (lines ).rstrip ()
68
57
69
58
70
- sys .excepthook = lambda type , value , tb : print (formatException (type , value , tb ), file = sys .stderr )
59
+ sys .excepthook = lambda type , value , tb : print (_formatException (type , value , tb ), file = sys .stderr )
71
60
72
61
73
- def get_char (prompt = None ):
74
- """
75
- Read a line of text from standard input and return the equivalent char;
76
- if text is not a single char, user is prompted to retry. If line can't
77
- be read, return None.
78
- """
79
- while True :
80
- s = get_string (prompt )
81
- if s is None :
82
- return None
83
- if len (s ) == 1 :
84
- return s [0 ]
62
+ def eprint (* args , ** kwargs ):
63
+ raise RuntimeError ("The CS50 Library for Python no longer supports eprint, but you can use print instead!" )
85
64
86
- # Temporarily here for backwards compatibility
87
- if prompt is None :
88
- print ("Retry: " , end = "" )
89
65
66
+ def get_char (prompt ):
67
+ raise RuntimeError ("The CS50 Library for Python no longer supports get_char, but you can use get_string instead!" )
90
68
91
- def get_float (prompt = None ):
69
+
70
+ def get_float (prompt ):
92
71
"""
93
72
Read a line of text from standard input and return the equivalent float
94
73
as precisely as possible; if text does not represent a double, user is
@@ -104,12 +83,8 @@ def get_float(prompt=None):
104
83
except ValueError :
105
84
pass
106
85
107
- # Temporarily here for backwards compatibility
108
- if prompt is None :
109
- print ("Retry: " , end = "" )
110
-
111
86
112
- def get_int (prompt = None ):
87
+ def get_int (prompt ):
113
88
"""
114
89
Read a line of text from standard input and return the equivalent int;
115
90
if text does not represent an int, user is prompted to retry. If line
@@ -121,40 +96,12 @@ def get_int(prompt=None):
121
96
return None
122
97
if re .search (r"^[+-]?\d+$" , s ):
123
98
try :
124
- i = int (s , 10 )
125
- if type (i ) is int : # Could become long in Python 2
126
- return i
99
+ return int (s , 10 )
127
100
except ValueError :
128
101
pass
129
102
130
- # Temporarily here for backwards compatibility
131
- if prompt is None :
132
- print ("Retry: " , end = "" )
133
-
134
-
135
- if sys .version_info .major != 3 :
136
- def get_long (prompt = None ):
137
- """
138
- Read a line of text from standard input and return the equivalent long;
139
- if text does not represent a long, user is prompted to retry. If line
140
- can't be read, return None.
141
- """
142
- while True :
143
- s = get_string (prompt )
144
- if s is None :
145
- return None
146
- if re .search (r"^[+-]?\d+$" , s ):
147
- try :
148
- return long (s , 10 )
149
- except ValueError :
150
- pass
151
-
152
- # Temporarily here for backwards compatibility
153
- if prompt is None :
154
- print ("Retry: " , end = "" )
155
-
156
-
157
- def get_string (prompt = None ):
103
+
104
+ def get_string (prompt ):
158
105
"""
159
106
Read a line of text from standard input and return it as a string,
160
107
sans trailing line ending. Supports CR (\r ), LF (\n ), and CRLF (\r \n )
0 commit comments