Skip to content

Commit 60f0904

Browse files
test: dis colorization, DisColored test case
1 parent 19436ee commit 60f0904

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

Lib/_colorize.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ class Dis(ThemeSection):
205205
label_bg: str = ANSIColors.BACKGROUND_CYAN
206206
label_fg: str = ANSIColors.BLACK
207207

208-
L:str = ANSIColors.BOLD_RED
209208
exception_label: str = ANSIColors.CYAN
210209
argument_detail: str = ANSIColors.CYAN
211210

Lib/test/test_dis.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
import types
1515
import unittest
1616
from test.support import (captured_stdout, force_not_colorized_test_class,
17-
requires_debug_ranges, requires_specialization,
18-
cpython_only, os_helper, import_helper, reset_code)
17+
force_colorized_test_class, requires_debug_ranges,
18+
requires_specialization, cpython_only, os_helper,
19+
import_helper, reset_code)
1920
from test.support.bytecode_helper import BytecodeTestCase
2021

2122

@@ -36,6 +37,12 @@ def _error():
3637

3738
TRACEBACK_CODE = get_tb().tb_frame.f_code
3839

40+
def _get_dis_theme():
41+
from _colorize import get_theme
42+
return get_theme().dis
43+
44+
theme = _get_dis_theme()
45+
3946
class _C:
4047
def __init__(self, x):
4148
self.x = x == 1
@@ -2633,6 +2640,65 @@ def test_specialized_code(self):
26332640
for flag in ['-S', '--specialized']:
26342641
self.check_output(source, expect, flag)
26352642

2643+
@force_colorized_test_class
2644+
class DisColored(unittest.TestCase):
2645+
def get_colored_output(self, func):
2646+
output = io.StringIO()
2647+
2648+
with contextlib.redirect_stdout(output):
2649+
dis.dis(func)
2650+
2651+
return output.getvalue()
2652+
2653+
def assertOpColored(self, output, opname, color):
2654+
self.assertIn(
2655+
f"{color}{opname}", output,
2656+
f"{opname} should be colored with {color!r}"
2657+
)
2658+
2659+
def test_load_ops_colored(self):
2660+
def f(a):
2661+
return a
2662+
out = self.get_colored_output(f)
2663+
self.assertOpColored(out, "LOAD_FAST", theme.op_load)
2664+
2665+
def test_call_return_ops_colored(self):
2666+
def f():
2667+
return 1
2668+
out = self.get_colored_output(f)
2669+
self.assertOpColored(out, "RETURN_VALUE", theme.op_call_return)
2670+
self.assertOpColored(out, "RESUME", theme.op_call_return)
2671+
2672+
def test_pop_ops_colored(self):
2673+
def f(a):
2674+
print(a)
2675+
out = self.get_colored_output(f)
2676+
self.assertOpColored(out, "POP_TOP", theme.op_pop)
2677+
2678+
def test_control_flow_ops_colored(self):
2679+
def f(a):
2680+
for _ in a:
2681+
pass
2682+
out = self.get_colored_output(f)
2683+
self.assertOpColored(out, "FOR_ITER", theme.op_control_flow)
2684+
self.assertOpColored(out, "JUMP_BACKWARD", theme.op_control_flow)
2685+
2686+
def test_argrepr_colored(self):
2687+
def f(a):
2688+
print(a)
2689+
out = self.get_colored_output(f)
2690+
self.assertIn(f"{theme.argument_detail}(", out)
2691+
2692+
def test_color_by_opname_coverage(self):
2693+
self.assertEqual(theme.color_by_opname("LOAD_FAST"), theme.op_load)
2694+
self.assertEqual(theme.color_by_opname("LOAD_GLOBAL"), theme.op_load)
2695+
self.assertEqual(theme.color_by_opname("POP_TOP"), theme.op_pop)
2696+
self.assertEqual(theme.color_by_opname("CALL"), theme.op_call_return)
2697+
self.assertEqual(theme.color_by_opname("RETURN_VALUE"), theme.op_call_return)
2698+
self.assertEqual(theme.color_by_opname("RESUME"), theme.op_call_return)
2699+
self.assertEqual(theme.color_by_opname("FOR_ITER"), theme.op_control_flow)
2700+
self.assertEqual(theme.color_by_opname("JUMP_BACKWARD"), theme.op_control_flow)
2701+
self.assertEqual(theme.color_by_opname("BINARY_OP"), theme.reset) # uncolored
26362702

26372703
if __name__ == "__main__":
26382704
unittest.main()

0 commit comments

Comments
 (0)