Skip to content

Commit 6deba19

Browse files
committedOct 29, 2017
moved excepthook to cs50.py
1 parent c2eb5e5 commit 6deba19

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed
 

‎src/cs50/__init__.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
23
from os.path import abspath, join
34
from site import getsitepackages, getusersitepackages
45
from termcolor import cprint
@@ -30,22 +31,3 @@ def load_module(self, name):
3031

3132

3233
sys.meta_path.append(CustomImporter())
33-
34-
35-
def excepthook(type, value, tb):
36-
"""
37-
Format traceback, darkening entries from global site-packages and user-specific site-packages directory.
38-
39-
https://stackoverflow.com/a/33042323/5156190
40-
"""
41-
packages = tuple(join(abspath(p), "") for p in getsitepackages() + [getusersitepackages()])
42-
for entry in extract_tb(tb):
43-
fmt = format_list((entry,))
44-
if (entry.filename.startswith(packages)):
45-
cprint("".join(fmt), attrs=["dark"], end="", file=sys.stderr)
46-
else:
47-
cprint("".join(fmt), end="", file=sys.stderr)
48-
cprint("".join(format_exception_only(type, value)), end="")
49-
50-
51-
sys.excepthook = excepthook

‎src/cs50/cs50.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
from __future__ import print_function
2+
23
import inspect
34
import re
45
import sys
56

7+
from os.path import abspath, join
8+
from site import getsitepackages, getusersitepackages
9+
from termcolor import cprint
10+
from traceback import extract_tb, format_list, format_exception_only
11+
12+
13+
def excepthook(type, value, tb):
14+
"""
15+
Format traceback, darkening entries from global site-packages and user-specific site-packages directory.
16+
17+
https://stackoverflow.com/a/33042323/5156190
18+
"""
19+
packages = tuple(join(abspath(p), "") for p in getsitepackages() + [getusersitepackages()])
20+
for entry in extract_tb(tb):
21+
fmt = format_list((entry,))
22+
if (entry.filename.startswith(packages)):
23+
cprint("".join(fmt), attrs=["dark"], end="", file=sys.stderr)
24+
else:
25+
cprint("".join(fmt), end="", file=sys.stderr)
26+
cprint("".join(format_exception_only(type, value)), end="")
27+
28+
29+
sys.excepthook = excepthook
30+
31+
632
class flushfile():
733
"""
834
Disable buffering for standard output and standard error.
935
1036
http://stackoverflow.com/a/231216
1137
"""
38+
1239
def __init__(self, f):
1340
self.f = f
1441

@@ -18,9 +45,12 @@ def __getattr__(self, name):
1845
def write(self, x):
1946
self.f.write(x)
2047
self.f.flush()
48+
49+
2150
sys.stderr = flushfile(sys.stderr)
2251
sys.stdout = flushfile(sys.stdout)
2352

53+
2454
def eprint(*args, **kwargs):
2555
"""
2656
Print an error message to standard error, prefixing it with
@@ -32,6 +62,7 @@ def eprint(*args, **kwargs):
3262
print("{}:{}: ".format(filename, lineno), end="")
3363
print(*args, end=end, file=sys.stderr, sep=sep)
3464

65+
3566
def get_char(prompt=None):
3667
"""
3768
Read a line of text from standard input and return the equivalent char;
@@ -49,6 +80,7 @@ def get_char(prompt=None):
4980
if prompt is None:
5081
print("Retry: ", end="")
5182

83+
5284
def get_float(prompt=None):
5385
"""
5486
Read a line of text from standard input and return the equivalent float
@@ -69,20 +101,21 @@ def get_float(prompt=None):
69101
if prompt is None:
70102
print("Retry: ", end="")
71103

104+
72105
def get_int(prompt=None):
73106
"""
74107
Read a line of text from standard input and return the equivalent int;
75108
if text does not represent an int, user is prompted to retry. If line
76109
can't be read, return None.
77110
"""
78111
while True:
79-
s = get_string(prompt);
112+
s = get_string(prompt)
80113
if s is None:
81114
return None
82115
if re.search(r"^[+-]?\d+$", s):
83116
try:
84117
i = int(s, 10)
85-
if type(i) is int: # could become long in Python 2
118+
if type(i) is int: # could become long in Python 2
86119
return i
87120
except ValueError:
88121
pass
@@ -91,6 +124,7 @@ def get_int(prompt=None):
91124
if prompt is None:
92125
print("Retry: ", end="")
93126

127+
94128
if sys.version_info.major != 3:
95129
def get_long(prompt=None):
96130
"""
@@ -112,6 +146,7 @@ def get_long(prompt=None):
112146
if prompt is None:
113147
print("Retry: ", end="")
114148

149+
115150
def get_string(prompt=None):
116151
"""
117152
Read a line of text from standard input and return it as a string,

0 commit comments

Comments
 (0)
Please sign in to comment.