Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-133346: Make theming support in _colorize extensible#133347
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
3344008
68e2385
252dfa0
b29c9b9
ca34939
5c5c3e1
f449e7b
8da314f
65d3a78
0866fd8
965c7cf
230d658
c4808c8
fd9c85c
664ef14
e67fda9
70ca161
3166d63
5c45ddb
68a5c44
9f51769
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -122,18 +122,38 @@ class REPL(ThemeSection): | ||
reset: str = ANSIColors.RESET | ||
@dataclass(frozen=True) | ||
class Traceback(ThemeSection): | ||
type: str = ANSIColors.BOLD_MAGENTA | ||
message: str = ANSIColors.MAGENTA | ||
filename: str = ANSIColors.MAGENTA | ||
line_no: str = ANSIColors.MAGENTA | ||
frame: str = ANSIColors.MAGENTA | ||
error_highlight: str = ANSIColors.BOLD_RED | ||
error_range: str = ANSIColors.RED | ||
reset: str = ANSIColors.RESET | ||
@dataclass(frozen=True) | ||
class Theme: | ||
repl: REPL = field(default_factory=REPL) | ||
traceback: Traceback = field(default_factory=Traceback) | ||
def copy_with( | ||
self, | ||
*, | ||
repl: REPL | None = None, | ||
traceback: Traceback | None = None, | ||
) -> Self: | ||
return type(self)( | ||
repl=repl or self.repl, | ||
traceback=traceback or self.traceback, | ||
) | ||
def no_colors(self) -> Self: | ||
ambv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return type(self)( | ||
repl=self.repl.no_colors(), | ||
traceback=self.traceback.no_colors(), | ||
) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -37,6 +37,12 @@ | ||
test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals']) | ||
test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next', 'tb_lasti']) | ||
color_overrides = {"reset": "z", "filename": "fn", "error_highlight": "E"} | ||
colors = { | ||
color_overrides.get(k, k[0].lower()): v | ||
for k, v in _colorize.default_theme.traceback.items() | ||
} | ||
LEVENSHTEIN_DATA_FILE = Path(__file__).parent / 'levenshtein_examples.json' | ||
@@ -4721,6 +4727,8 @@ class MyList(list): | ||
class TestColorizedTraceback(unittest.TestCase): | ||
maxDiff = None | ||
def test_colorized_traceback(self): | ||
def foo(*args): | ||
x = {'a':{'b': None}} | ||
@@ -4743,9 +4751,9 @@ def bar(): | ||
e, capture_locals=True | ||
) | ||
lines = "".join(exc.format(colorize=True)) | ||
red =colors["e"] | ||
boldr =colors["E"] | ||
reset =colors["z"] | ||
ambv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
self.assertIn("y = " + red + "x['a']['b']" + reset + boldr + "['c']" + reset, lines) | ||
self.assertIn("return " + red + "(lambda *args: foo(*args))" + reset + boldr + "(1,2,3,4)" + reset, lines) | ||
self.assertIn("return (lambda *args: " + red + "foo" + reset + boldr + "(*args)" + reset + ")(1,2,3,4)", lines) | ||
@@ -4761,18 +4769,16 @@ def test_colorized_syntax_error(self): | ||
e, capture_locals=True | ||
) | ||
actual = "".join(exc.format(colorize=True)) | ||
def expected(t, m, fn, l, f, E, e, z): | ||
return "".join( | ||
[ | ||
f' File {fn}"<string>"{z}, line {l}1{z}\n', | ||
f' a {E}${z} b\n', | ||
f' {E}^{z}\n', | ||
f'{t}SyntaxError{z}: {m}invalid syntax{z}\n' | ||
] | ||
) | ||
self.assertIn(expected(**colors), actual) | ||
def test_colorized_traceback_is_the_default(self): | ||
def foo(): | ||
@@ -4788,23 +4794,21 @@ def foo(): | ||
exception_print(e) | ||
actual = tbstderr.getvalue().splitlines() | ||
lno_foo = foo.__code__.co_firstlineno | ||
def expected(t, m, fn, l, f, E, e, z): | ||
return [ | ||
'Traceback (most recent call last):', | ||
f' File {fn}"{__file__}"{z}, ' | ||
f'line {l}{lno_foo+5}{z}, in {f}test_colorized_traceback_is_the_default{z}', | ||
f' {e}foo{z}{E}(){z}', | ||
f' {e}~~~{z}{E}^^{z}', | ||
f' File {fn}"{__file__}"{z}, ' | ||
f'line {l}{lno_foo+1}{z}, in {f}foo{z}', | ||
f' {e}1{z}{E}/{z}{e}0{z}', | ||
f' {e}~{z}{E}^{z}{e}~{z}', | ||
f'{t}ZeroDivisionError{z}: {m}division by zero{z}', | ||
] | ||
self.assertEqual(actual, expected(**colors)) | ||
def test_colorized_traceback_from_exception_group(self): | ||
def foo(): | ||
@@ -4822,33 +4826,31 @@ def foo(): | ||
e, capture_locals=True | ||
) | ||
lno_foo = foo.__code__.co_firstlineno | ||
actual = "".join(exc.format(colorize=True)).splitlines() | ||
def expected(t, m, fn, l, f, E, e, z): | ||
ambv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return [ | ||
f" + Exception Group Traceback (most recent call last):", | ||
f' | File {fn}"{__file__}"{z}, line {l}{lno_foo+9}{z}, in {f}test_colorized_traceback_from_exception_group{z}', | ||
f' | {e}foo{z}{E}(){z}', | ||
f' | {e}~~~{z}{E}^^{z}', | ||
f" | e = ExceptionGroup('test', [ZeroDivisionError('division by zero')])", | ||
f" | foo = {foo}", | ||
f' | self = <{__name__}.TestColorizedTraceback testMethod=test_colorized_traceback_from_exception_group>', | ||
f' | File {fn}"{__file__}"{z}, line {l}{lno_foo+6}{z}, in {f}foo{z}', | ||
f' | raise ExceptionGroup("test", exceptions)', | ||
f" | exceptions = [ZeroDivisionError('division by zero')]", | ||
f' | {t}ExceptionGroup{z}: {m}test (1 sub-exception){z}', | ||
f' +-+---------------- 1 ----------------', | ||
f' | Traceback (most recent call last):', | ||
f' | File {fn}"{__file__}"{z}, line {l}{lno_foo+3}{z}, in {f}foo{z}', | ||
f' | {e}1 {z}{E}/{z}{e} 0{z}', | ||
f' | {e}~~{z}{E}^{z}{e}~~{z}', | ||
f" | exceptions = [ZeroDivisionError('division by zero')]", | ||
f' | {t}ZeroDivisionError{z}: {m}division by zero{z}', | ||
f' +------------------------------------', | ||
] | ||
self.assertEqual(actual, expected(**colors)) | ||
if __name__ == "__main__": | ||
unittest.main() |
Uh oh!
There was an error while loading.Please reload this page.