Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grays out site packages in tracebacks #33

Merged
merged 13 commits into from
Nov 1, 2017
20 changes: 1 addition & 19 deletions src/cs50/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys

from os.path import abspath, join
from site import getsitepackages, getusersitepackages
from termcolor import cprint
@@ -30,22 +31,3 @@ def load_module(self, name):


sys.meta_path.append(CustomImporter())


def excepthook(type, value, tb):
"""
Format traceback, darkening entries from global site-packages and user-specific site-packages directory.
https://stackoverflow.com/a/33042323/5156190
"""
packages = tuple(join(abspath(p), "") for p in getsitepackages() + [getusersitepackages()])
for entry in extract_tb(tb):
fmt = format_list((entry,))
if (entry.filename.startswith(packages)):
cprint("".join(fmt), attrs=["dark"], end="", file=sys.stderr)
else:
cprint("".join(fmt), end="", file=sys.stderr)
cprint("".join(format_exception_only(type, value)), end="")


sys.excepthook = excepthook
39 changes: 37 additions & 2 deletions src/cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
from __future__ import print_function

import inspect
import re
import sys

from os.path import abspath, join
from site import getsitepackages, getusersitepackages
from termcolor import cprint
from traceback import extract_tb, format_list, format_exception_only


def excepthook(type, value, tb):
"""
Format traceback, darkening entries from global site-packages and user-specific site-packages directory.
https://stackoverflow.com/a/33042323/5156190
"""
packages = tuple(join(abspath(p), "") for p in getsitepackages() + [getusersitepackages()])
for entry in extract_tb(tb):
fmt = format_list((entry,))
if (entry.filename.startswith(packages)):
cprint("".join(fmt), attrs=["dark"], end="", file=sys.stderr)
else:
cprint("".join(fmt), end="", file=sys.stderr)
cprint("".join(format_exception_only(type, value)), end="")


sys.excepthook = excepthook


class flushfile():
"""
Disable buffering for standard output and standard error.
http://stackoverflow.com/a/231216
"""

def __init__(self, f):
self.f = f

@@ -18,9 +45,12 @@ def __getattr__(self, name):
def write(self, x):
self.f.write(x)
self.f.flush()


sys.stderr = flushfile(sys.stderr)
sys.stdout = flushfile(sys.stdout)


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


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


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


def get_int(prompt=None):
"""
Read a line of text from standard input and return the equivalent int;
if text does not represent an int, user is prompted to retry. If line
can't be read, return None.
"""
while True:
s = get_string(prompt);
s = get_string(prompt)
if s is None:
return None
if re.search(r"^[+-]?\d+$", s):
try:
i = int(s, 10)
if type(i) is int: # could become long in Python 2
if type(i) is int: # could become long in Python 2
return i
except ValueError:
pass
@@ -91,6 +124,7 @@ def get_int(prompt=None):
if prompt is None:
print("Retry: ", end="")


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


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