Skip to content

Commit 010b59e

Browse files
authored
Merge pull request #79 from cs50/deprecations
deprecated eprint, get_char, and prompts
2 parents 12747c7 + 12ea7b4 commit 010b59e

File tree

4 files changed

+31
-83
lines changed

4 files changed

+31
-83
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ pip install cs50
1010

1111
## Usage
1212

13-
import cs50
13+
```
14+
import cs50
1415
15-
...
16+
...
1617
17-
c = cs50.get_char();
18-
f = cs50.get_float();
19-
i = cs50.get_int();
20-
s = cs50.get_string();
18+
f = cs50.get_float();
19+
i = cs50.get_int();
20+
s = cs50.get_string();
21+
```

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
package_dir={"": "src"},
1717
packages=["cs50"],
1818
url="https://github.com/cs50/python-cs50",
19-
version="3.2.0"
19+
version="4.0.0"
2020
)

src/cs50/cs50.py

+17-70
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from __future__ import print_function
22

33
import inspect
4+
import os
45
import re
56
import sys
67

78
from distutils.sysconfig import get_python_lib
89
from os.path import abspath, join
910
from termcolor import colored
10-
from traceback import extract_tb, format_list, format_exception_only, format_exception
11+
from traceback import format_exception
1112

1213

13-
class flushfile():
14+
class _flushfile():
1415
"""
1516
Disable buffering for standard output and standard error.
1617
@@ -28,23 +29,11 @@ def write(self, x):
2829
self.f.flush()
2930

3031

31-
sys.stderr = flushfile(sys.stderr)
32-
sys.stdout = flushfile(sys.stdout)
32+
sys.stderr = _flushfile(sys.stderr)
33+
sys.stdout = _flushfile(sys.stdout)
3334

3435

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):
4837
"""
4938
Format traceback, darkening entries from global site-packages directories
5039
and user-specific site-packages directory.
@@ -67,28 +56,18 @@ def formatException(type, value, tb):
6756
return "".join(lines).rstrip()
6857

6958

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)
7160

7261

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!")
8564

86-
# Temporarily here for backwards compatibility
87-
if prompt is None:
88-
print("Retry: ", end="")
8965

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!")
9068

91-
def get_float(prompt=None):
69+
70+
def get_float(prompt):
9271
"""
9372
Read a line of text from standard input and return the equivalent float
9473
as precisely as possible; if text does not represent a double, user is
@@ -104,12 +83,8 @@ def get_float(prompt=None):
10483
except ValueError:
10584
pass
10685

107-
# Temporarily here for backwards compatibility
108-
if prompt is None:
109-
print("Retry: ", end="")
110-
11186

112-
def get_int(prompt=None):
87+
def get_int(prompt):
11388
"""
11489
Read a line of text from standard input and return the equivalent int;
11590
if text does not represent an int, user is prompted to retry. If line
@@ -121,40 +96,12 @@ def get_int(prompt=None):
12196
return None
12297
if re.search(r"^[+-]?\d+$", s):
12398
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)
127100
except ValueError:
128101
pass
129102

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):
158105
"""
159106
Read a line of text from standard input and return it as a string,
160107
sans trailing line ending. Supports CR (\r), LF (\n), and CRLF (\r\n)

src/cs50/flask.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from os import getenv
55
from pkg_resources import get_distribution
66

7-
from .cs50 import formatException
7+
from .cs50 import _formatException
88

99
# Try to monkey-patch Flask, if installed
1010
try:
@@ -18,7 +18,7 @@
1818
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
1919
try:
2020
import flask.logging
21-
flask.logging.default_handler.formatter.formatException = lambda exc_info: formatException(*exc_info)
21+
flask.logging.default_handler.formatter.formatException = lambda exc_info: _formatException(*exc_info)
2222
except Exception:
2323
pass
2424

@@ -42,16 +42,16 @@ def _after(*args, **kwargs):
4242
logging.getLogger("cs50").disabled = disabled
4343
SQL.execute = _after
4444

45-
# Add support for Cloud9 proxy so that flask.redirect doesn't redirect from HTTPS to HTTP
46-
# http://stackoverflow.com/a/23504684/5156190
45+
# When behind CS50 IDE's proxy, ensure that flask.redirect doesn't redirect from HTTPS to HTTP
46+
# https://werkzeug.palletsprojects.com/en/0.15.x/middleware/proxy_fix/#module-werkzeug.middleware.proxy_fix
4747
if getenv("C9_HOSTNAME") and not getenv("IDE_OFFLINE"):
4848
try:
4949
import flask
50-
from werkzeug.contrib.fixers import ProxyFix
50+
from werkzeug.middleware.proxy_fix import ProxyFix
5151
_before = flask.Flask.__init__
5252
def _after(*args, **kwargs):
5353
_before(*args, **kwargs)
54-
self.wsgi_app = ProxyFix(self.wsgi_app)
54+
self.wsgi_app = ProxyFix(self.wsgi_app, x_proto=1)
5555
flask.Flask.__init__ = _after
5656
except:
5757
pass

0 commit comments

Comments
 (0)