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

gh-117225: Move colorize functionality to own internal module #118283

Merged
merged 5 commits into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import io
import os
import sys

COLORIZE = True


class ANSIColors:
BOLD_GREEN = "\x1b[1;32m"
BOLD_MAGENTA = "\x1b[1;35m"
BOLD_RED = "\x1b[1;31m"
GREEN = "\x1b[32m"
GREY = "\x1b[90m"
MAGENTA = "\x1b[35m"
RED = "\x1b[31m"
RESET = "\x1b[0m"
YELLOW = "\x1b[33m"


NoColors = ANSIColors()

for attr in dir(NoColors):
if not attr.startswith("__"):
setattr(NoColors, attr, "")


def get_colors(colorize: bool = False) -> ANSIColors:
if colorize or can_colorize():
return ANSIColors()
else:
return NoColors


def can_colorize() -> bool:
if sys.platform == "win32":
try:
import nt

if not nt._supports_virtual_terminal():
return False
except (ImportError, AttributeError):
return False
if not sys.flags.ignore_environment:
if os.environ.get("PYTHON_COLORS") == "0":
return False
if os.environ.get("PYTHON_COLORS") == "1":
return True
if "NO_COLOR" in os.environ:
return False
if not COLORIZE:
return False
if not sys.flags.ignore_environment:
if "FORCE_COLOR" in os.environ:
return True
if os.environ.get("TERM") == "dumb":
return False

if not hasattr(sys.stderr, "fileno"):
return False

try:
return os.isatty(sys.stderr.fileno())
except io.UnsupportedOperation:
return sys.stderr.isatty()
38 changes: 16 additions & 22 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
@@ -104,7 +104,8 @@ def _test():
import unittest
from io import StringIO, IncrementalNewlineDecoder
from collections import namedtuple
from traceback import _ANSIColors, _can_colorize
import _colorize # Used in doctests
from _colorize import ANSIColors, can_colorize


class TestResults(namedtuple('TestResults', 'failed attempted')):
@@ -1180,8 +1181,8 @@ class DocTestRunner:
The `run` method is used to process a single DocTest case. It
returns a TestResults instance.
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> tests = DocTestFinder().find(_TestClass)
>>> runner = DocTestRunner(verbose=False)
@@ -1234,7 +1235,7 @@ class DocTestRunner:
overriding the methods `report_start`, `report_success`,
`report_unexpected_exception`, and `report_failure`.
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""
# This divider string is used to separate failure messages, and to
# separate sections of the summary.
@@ -1314,7 +1315,7 @@ def report_unexpected_exception(self, out, test, example, exc_info):

def _failure_header(self, test, example):
red, reset = (
(_ANSIColors.RED, _ANSIColors.RESET) if _can_colorize() else ("", "")
(ANSIColors.RED, ANSIColors.RESET) if can_colorize() else ("", "")
)
out = [f"{red}{self.DIVIDER}{reset}"]
if test.filename:
@@ -1556,8 +1557,8 @@ def out(s):
# Make sure sys.displayhook just prints the value to stdout
save_displayhook = sys.displayhook
sys.displayhook = sys.__displayhook__
saved_can_colorize = traceback._can_colorize
traceback._can_colorize = lambda: False
saved_can_colorize = _colorize.can_colorize
_colorize.can_colorize = lambda: False
color_variables = {"PYTHON_COLORS": None, "FORCE_COLOR": None}
for key in color_variables:
color_variables[key] = os.environ.pop(key, None)
@@ -1569,7 +1570,7 @@ def out(s):
sys.settrace(save_trace)
linecache.getlines = self.save_linecache_getlines
sys.displayhook = save_displayhook
traceback._can_colorize = saved_can_colorize
_colorize.can_colorize = saved_can_colorize
for key, value in color_variables.items():
if value is not None:
os.environ[key] = value
@@ -1609,20 +1610,13 @@ def summarize(self, verbose=None):
else:
failed.append((name, (failures, tries, skips)))

if _can_colorize():
bold_green = _ANSIColors.BOLD_GREEN
bold_red = _ANSIColors.BOLD_RED
green = _ANSIColors.GREEN
red = _ANSIColors.RED
reset = _ANSIColors.RESET
yellow = _ANSIColors.YELLOW
else:
bold_green = ""
bold_red = ""
green = ""
red = ""
reset = ""
yellow = ""
ansi = _colorize.get_colors()
bold_green = ansi.BOLD_GREEN
bold_red = ansi.BOLD_RED
green = ansi.GREEN
red = ansi.RED
reset = ansi.RESET
yellow = ansi.YELLOW

if verbose:
if notests:
9 changes: 5 additions & 4 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
@@ -2579,20 +2579,21 @@ def copy_python_src_ignore(path, names):
}
return ignored


def force_not_colorized(func):
"""Force the terminal not to be colorized."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
import traceback
original_fn = traceback._can_colorize
import _colorize
original_fn = _colorize.can_colorize
variables = {"PYTHON_COLORS": None, "FORCE_COLOR": None}
try:
for key in variables:
variables[key] = os.environ.pop(key, None)
traceback._can_colorize = lambda: False
_colorize.can_colorize = lambda: False
return func(*args, **kwargs)
finally:
traceback._can_colorize = original_fn
_colorize.can_colorize = original_fn
for key, value in variables.items():
if value is not None:
os.environ[key] = value
59 changes: 59 additions & 0 deletions Lib/test/test__colorize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import contextlib
import sys
import unittest
import unittest.mock
import _colorize
from test.support import force_not_colorized

ORIGINAL_CAN_COLORIZE = _colorize.can_colorize


def setUpModule():
_colorize.can_colorize = lambda: False


def tearDownModule():
_colorize.can_colorize = ORIGINAL_CAN_COLORIZE


class TestColorizeFunction(unittest.TestCase):
@force_not_colorized
def test_colorized_detection_checks_for_environment_variables(self):
if sys.platform == "win32":
virtual_patching = unittest.mock.patch("nt._supports_virtual_terminal",
return_value=True)
else:
virtual_patching = contextlib.nullcontext()
with virtual_patching:

flags = unittest.mock.MagicMock(ignore_environment=False)
with (unittest.mock.patch("os.isatty") as isatty_mock,
unittest.mock.patch("sys.flags", flags),
unittest.mock.patch("_colorize.can_colorize", ORIGINAL_CAN_COLORIZE)):
isatty_mock.return_value = True
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '0'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ",
{'NO_COLOR': '1', "PYTHON_COLORS": '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), True)
with unittest.mock.patch("os.environ",
{'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
self.assertEqual(_colorize.can_colorize(), False)
with unittest.mock.patch("os.environ",
{'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}):
self.assertEqual(_colorize.can_colorize(), False)
isatty_mock.return_value = False
with unittest.mock.patch("os.environ", {}):
self.assertEqual(_colorize.can_colorize(), False)


if __name__ == "__main__":
unittest.main()
56 changes: 28 additions & 28 deletions Lib/test/test_doctest/test_doctest.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
import tempfile
import types
import contextlib
import traceback
import _colorize


def doctest_skip_if(condition):
@@ -893,8 +893,8 @@ def basics(): r"""
DocTestRunner is used to run DocTest test cases, and to accumulate
statistics. Here's a simple DocTest case we can use:
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> def f(x):
... '''
@@ -951,7 +951,7 @@ def basics(): r"""
ok
TestResults(failed=1, attempted=3)
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""
def verbose_flag(): r"""
The `verbose` flag makes the test runner generate more detailed
@@ -1027,8 +1027,8 @@ def exceptions(): r"""
lines between the first line and the type/value may be omitted or
replaced with any other string:
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> def f(x):
... '''
@@ -1261,7 +1261,7 @@ def exceptions(): r"""
ZeroDivisionError: integer division or modulo by zero
TestResults(failed=1, attempted=1)
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""
def displayhook(): r"""
Test that changing sys.displayhook doesn't matter for doctest.
@@ -1303,8 +1303,8 @@ def optionflags(): r"""
The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False
and 1/0:
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> def f(x):
... '>>> True\n1\n'
@@ -1725,7 +1725,7 @@ def optionflags(): r"""
Clean up.
>>> del doctest.OPTIONFLAGS_BY_NAME[unlikely]
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""

@@ -1736,8 +1736,8 @@ def option_directives(): r"""
single example. To turn an option on for an example, follow that
example with a comment of the form ``# doctest: +OPTION``:
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> def f(x): r'''
... >>> print(list(range(10))) # should fail: no ellipsis
@@ -1947,7 +1947,7 @@ def option_directives(): r"""
Traceback (most recent call last):
ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""

def test_testsource(): r"""
@@ -2031,8 +2031,8 @@ def test_pdb_set_trace():
with a version that restores stdout. This is necessary for you to
see debugger output.
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> doc = '''
... >>> x = 42
@@ -2157,7 +2157,7 @@ def test_pdb_set_trace():
9
TestResults(failed=1, attempted=3)
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""

def test_pdb_set_trace_nested():
@@ -2694,8 +2694,8 @@ def test_testfile(): r"""
We don't want color or `-v` in sys.argv for these tests.
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> save_argv = sys.argv
>>> if '-v' in sys.argv:
@@ -2863,7 +2863,7 @@ def test_testfile(): r"""
TestResults(failed=0, attempted=2)
>>> doctest.master = None # Reset master.
>>> sys.argv = save_argv
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""

class TestImporter(importlib.abc.MetaPathFinder, importlib.abc.ResourceLoader):
@@ -3001,8 +3001,8 @@ def test_testmod(): r"""
def test_unicode(): """
Check doctest with a non-ascii filename:
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> doc = '''
... >>> raise Exception('clé')
@@ -3030,7 +3030,7 @@ def test_unicode(): """
Exception: clé
TestResults(failed=1, attempted=1)
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""


@@ -3325,8 +3325,8 @@ def test_run_doctestsuite_multiple_times():

def test_exception_with_note(note):
"""
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> test_exception_with_note('Note')
Traceback (most recent call last):
@@ -3378,7 +3378,7 @@ def test_exception_with_note(note):
note
TestResults(failed=1, attempted=...)
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""
exc = ValueError('Text')
exc.add_note(note)
@@ -3459,8 +3459,8 @@ def test_syntax_error_subclass_from_stdlib():

def test_syntax_error_with_incorrect_expected_note():
"""
>>> save_colorize = traceback._COLORIZE
>>> traceback._COLORIZE = False
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False
>>> def f(x):
... r'''
@@ -3491,7 +3491,7 @@ def test_syntax_error_with_incorrect_expected_note():
note2
TestResults(failed=1, attempted=...)
>>> traceback._COLORIZE = save_colorize
>>> _colorize.COLORIZE = save_colorize
"""


75 changes: 18 additions & 57 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
@@ -26,9 +26,9 @@
import json
import textwrap
import traceback
import contextlib
from functools import partial
from pathlib import Path
import _colorize

MODULE_PREFIX = f'{__name__}.' if __name__ == '__main__' else ''

@@ -40,25 +40,18 @@

LEVENSHTEIN_DATA_FILE = Path(__file__).parent / 'levenshtein_examples.json'

ORIGINAL_CAN_COLORIZE = traceback._can_colorize

def setUpModule():
traceback._can_colorize = lambda: False

def tearDownModule():
traceback._can_colorize = ORIGINAL_CAN_COLORIZE

class TracebackCases(unittest.TestCase):
# For now, a very minimal set of tests. I want to be sure that
# formatting of SyntaxErrors works based on changes for 2.1.
def setUp(self):
super().setUp()
self.colorize = traceback._COLORIZE
traceback._COLORIZE = False
self.colorize = _colorize.COLORIZE
_colorize.COLORIZE = False

def tearDown(self):
super().tearDown()
traceback._COLORIZE = self.colorize
_colorize.COLORIZE = self.colorize

def get_exception_format(self, func, exc):
try:
@@ -4301,9 +4294,9 @@ def bar():
e, capture_locals=True
)
lines = "".join(exc.format(colorize=True))
red = traceback._ANSIColors.RED
boldr = traceback._ANSIColors.BOLD_RED
reset = traceback._ANSIColors.RESET
red = _colorize.ANSIColors.RED
boldr = _colorize.ANSIColors.BOLD_RED
reset = _colorize.ANSIColors.RESET
self.assertIn("y = " + red + "x['a']['b']" + reset + boldr + "['c']" + reset, lines)
self.assertIn("return " + red + "foo" + reset + boldr + "(1,2,3,4)" + reset, lines)
self.assertIn("return " + red + "baz" + reset + boldr + "(1," + reset, lines)
@@ -4319,11 +4312,11 @@ def test_colorized_syntax_error(self):
e, capture_locals=True
)
actual = "".join(exc.format(colorize=True))
red = traceback._ANSIColors.RED
magenta = traceback._ANSIColors.MAGENTA
boldm = traceback._ANSIColors.BOLD_MAGENTA
boldr = traceback._ANSIColors.BOLD_RED
reset = traceback._ANSIColors.RESET
red = _colorize.ANSIColors.RED
magenta = _colorize.ANSIColors.MAGENTA
boldm = _colorize.ANSIColors.BOLD_MAGENTA
boldr = _colorize.ANSIColors.BOLD_RED
reset = _colorize.ANSIColors.RESET
expected = "".join([
f' File {magenta}"<string>"{reset}, line {magenta}1{reset}\n',
f' a {boldr}${reset} b\n',
@@ -4342,15 +4335,15 @@ def foo():
self.fail("No exception thrown.")
except Exception as e:
with captured_output("stderr") as tbstderr:
with unittest.mock.patch('traceback._can_colorize', return_value=True):
with unittest.mock.patch('_colorize.can_colorize', return_value=True):
exception_print(e)
actual = tbstderr.getvalue().splitlines()

red = traceback._ANSIColors.RED
boldr = traceback._ANSIColors.BOLD_RED
magenta = traceback._ANSIColors.MAGENTA
boldm = traceback._ANSIColors.BOLD_MAGENTA
reset = traceback._ANSIColors.RESET
red = _colorize.ANSIColors.RED
boldr = _colorize.ANSIColors.BOLD_RED
magenta = _colorize.ANSIColors.MAGENTA
boldm = _colorize.ANSIColors.BOLD_MAGENTA
reset = _colorize.ANSIColors.RESET
lno_foo = foo.__code__.co_firstlineno
expected = ['Traceback (most recent call last):',
f' File {magenta}"{__file__}"{reset}, '
@@ -4364,38 +4357,6 @@ def foo():
f'{boldm}ZeroDivisionError{reset}: {magenta}division by zero{reset}']
self.assertEqual(actual, expected)

@force_not_colorized
def test_colorized_detection_checks_for_environment_variables(self):
if sys.platform == "win32":
virtual_patching = unittest.mock.patch("nt._supports_virtual_terminal", return_value=True)
else:
virtual_patching = contextlib.nullcontext()
with virtual_patching:

flags = unittest.mock.MagicMock(ignore_environment=False)
with (unittest.mock.patch("os.isatty") as isatty_mock,
unittest.mock.patch("sys.flags", flags),
unittest.mock.patch("traceback._can_colorize", ORIGINAL_CAN_COLORIZE)):
isatty_mock.return_value = True
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
self.assertEqual(traceback._can_colorize(), False)
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '1'}):
self.assertEqual(traceback._can_colorize(), True)
with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '0'}):
self.assertEqual(traceback._can_colorize(), False)
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
self.assertEqual(traceback._can_colorize(), False)
with unittest.mock.patch("os.environ", {'NO_COLOR': '1', "PYTHON_COLORS": '1'}):
self.assertEqual(traceback._can_colorize(), True)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
self.assertEqual(traceback._can_colorize(), True)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
self.assertEqual(traceback._can_colorize(), False)
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}):
self.assertEqual(traceback._can_colorize(), False)
isatty_mock.return_value = False
with unittest.mock.patch("os.environ", {}):
self.assertEqual(traceback._can_colorize(), False)

if __name__ == "__main__":
unittest.main()
96 changes: 27 additions & 69 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Extract, format and print information about Python stack traces."""

import os
import io
import collections.abc
import itertools
import linecache
import sys
import textwrap
import warnings
from contextlib import suppress
import _colorize
from _colorize import ANSIColors

__all__ = ['extract_stack', 'extract_tb', 'format_exception',
'format_exception_only', 'format_list', 'format_stack',
@@ -21,7 +21,6 @@
# Formatting and printing lists of traceback lines.
#

_COLORIZE = True

def print_list(extracted_list, file=None):
"""Print the list of tuples as returned by extract_tb() or
@@ -133,41 +132,10 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \

BUILTIN_EXCEPTION_LIMIT = object()

def _can_colorize():
if sys.platform == "win32":
try:
import nt
if not nt._supports_virtual_terminal():
return False
except (ImportError, AttributeError):
return False
if not sys.flags.ignore_environment:
if os.environ.get("PYTHON_COLORS") == "0":
return False
if os.environ.get("PYTHON_COLORS") == "1":
return True
if "NO_COLOR" in os.environ:
return False
if not _COLORIZE:
return False
if not sys.flags.ignore_environment:
if "FORCE_COLOR" in os.environ:
return True
if os.environ.get("TERM") == "dumb":
return False

if not hasattr(sys.stderr, "fileno"):
return False

try:
return os.isatty(sys.stderr.fileno())
except io.UnsupportedOperation:
return sys.stderr.isatty()


def _print_exception_bltin(exc, /):
file = sys.stderr if sys.stderr is not None else sys.__stderr__
colorize = _can_colorize()
colorize = _colorize.can_colorize()
return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)


@@ -214,16 +182,17 @@ def _format_final_exc_line(etype, value, *, insert_final_newline=True, colorize=
end_char = "\n" if insert_final_newline else ""
if colorize:
if value is None or not valuestr:
line = f"{_ANSIColors.BOLD_MAGENTA}{etype}{_ANSIColors.RESET}{end_char}"
line = f"{ANSIColors.BOLD_MAGENTA}{etype}{ANSIColors.RESET}{end_char}"
else:
line = f"{_ANSIColors.BOLD_MAGENTA}{etype}{_ANSIColors.RESET}: {_ANSIColors.MAGENTA}{valuestr}{_ANSIColors.RESET}{end_char}"
line = f"{ANSIColors.BOLD_MAGENTA}{etype}{ANSIColors.RESET}: {ANSIColors.MAGENTA}{valuestr}{ANSIColors.RESET}{end_char}"
else:
if value is None or not valuestr:
line = f"{etype}{end_char}"
else:
line = f"{etype}: {valuestr}{end_char}"
return line


def _safe_string(value, what, func=str):
try:
return func(value)
@@ -449,17 +418,6 @@ def _get_code_position(code, instruction_index):

_RECURSIVE_CUTOFF = 3 # Also hardcoded in traceback.c.

class _ANSIColors:
RED = '\x1b[31m'
BOLD_RED = '\x1b[1;31m'
MAGENTA = '\x1b[35m'
BOLD_MAGENTA = '\x1b[1;35m'
GREEN = "\x1b[32m"
BOLD_GREEN = "\x1b[1;32m"
GREY = '\x1b[90m'
RESET = '\x1b[0m'
YELLOW = "\x1b[33m"


class StackSummary(list):
"""A list of FrameSummary objects, representing a stack of frames."""
@@ -564,15 +522,15 @@ def format_frame_summary(self, frame_summary, **kwargs):
filename = "<stdin>"
if colorize:
row.append(' File {}"{}"{}, line {}{}{}, in {}{}{}\n'.format(
_ANSIColors.MAGENTA,
ANSIColors.MAGENTA,
filename,
_ANSIColors.RESET,
_ANSIColors.MAGENTA,
ANSIColors.RESET,
ANSIColors.MAGENTA,
frame_summary.lineno,
_ANSIColors.RESET,
_ANSIColors.MAGENTA,
ANSIColors.RESET,
ANSIColors.MAGENTA,
frame_summary.name,
_ANSIColors.RESET,
ANSIColors.RESET,
)
)
else:
@@ -699,11 +657,11 @@ def output_line(lineno):
for color, group in itertools.groupby(itertools.zip_longest(line, carets, fillvalue=""), key=lambda x: x[1]):
caret_group = list(group)
if color == "^":
colorized_line_parts.append(_ANSIColors.BOLD_RED + "".join(char for char, _ in caret_group) + _ANSIColors.RESET)
colorized_carets_parts.append(_ANSIColors.BOLD_RED + "".join(caret for _, caret in caret_group) + _ANSIColors.RESET)
colorized_line_parts.append(ANSIColors.BOLD_RED + "".join(char for char, _ in caret_group) + ANSIColors.RESET)
colorized_carets_parts.append(ANSIColors.BOLD_RED + "".join(caret for _, caret in caret_group) + ANSIColors.RESET)
elif color == "~":
colorized_line_parts.append(_ANSIColors.RED + "".join(char for char, _ in caret_group) + _ANSIColors.RESET)
colorized_carets_parts.append(_ANSIColors.RED + "".join(caret for _, caret in caret_group) + _ANSIColors.RESET)
colorized_line_parts.append(ANSIColors.RED + "".join(char for char, _ in caret_group) + ANSIColors.RESET)
colorized_carets_parts.append(ANSIColors.RED + "".join(caret for _, caret in caret_group) + ANSIColors.RESET)
else:
colorized_line_parts.append("".join(char for char, _ in caret_group))
colorized_carets_parts.append("".join(caret for _, caret in caret_group))
@@ -1279,12 +1237,12 @@ def _format_syntax_error(self, stype, **kwargs):
if self.lineno is not None:
if colorize:
yield ' File {}"{}"{}, line {}{}{}\n'.format(
_ANSIColors.MAGENTA,
ANSIColors.MAGENTA,
self.filename or "<string>",
_ANSIColors.RESET,
_ANSIColors.MAGENTA,
ANSIColors.RESET,
ANSIColors.MAGENTA,
self.lineno,
_ANSIColors.RESET,
ANSIColors.RESET,
)
else:
yield ' File "{}", line {}\n'.format(
@@ -1324,11 +1282,11 @@ def _format_syntax_error(self, stype, **kwargs):
# colorize from colno to end_colno
ltext = (
ltext[:colno] +
_ANSIColors.BOLD_RED + ltext[colno:end_colno] + _ANSIColors.RESET +
ANSIColors.BOLD_RED + ltext[colno:end_colno] + ANSIColors.RESET +
ltext[end_colno:]
)
start_color = _ANSIColors.BOLD_RED
end_color = _ANSIColors.RESET
start_color = ANSIColors.BOLD_RED
end_color = ANSIColors.RESET
yield ' {}\n'.format(ltext)
yield ' {}{}{}{}\n'.format(
"".join(caretspace),
@@ -1341,12 +1299,12 @@ def _format_syntax_error(self, stype, **kwargs):
msg = self.msg or "<no detail available>"
if colorize:
yield "{}{}{}: {}{}{}{}\n".format(
_ANSIColors.BOLD_MAGENTA,
ANSIColors.BOLD_MAGENTA,
stype,
_ANSIColors.RESET,
_ANSIColors.MAGENTA,
ANSIColors.RESET,
ANSIColors.MAGENTA,
msg,
_ANSIColors.RESET,
ANSIColors.RESET,
filename_suffix)
else:
yield "{}: {}{}\n".format(stype, msg, filename_suffix)
1 change: 1 addition & 0 deletions Python/stdlib_module_names.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.


Unchanged files with check annotations Beta

case _FOR_ITER_GEN_FRAME: {
_PyInterpreterFrame *gen_frame;
gen_frame = sym_new_not_null(ctx);

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

assignment to ‘_PyInterpreterFrame *’ {aka ‘struct _PyInterpreterFrame *’} from incompatible pointer type ‘_Py_UopsSymbol *’ {aka ‘struct _Py_UopsSymbol *’} [-Wincompatible-pointer-types]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.13)

assignment to ‘_PyInterpreterFrame *’ {aka ‘struct _PyInterpreterFrame *’} from incompatible pointer type ‘_Py_UopsSymbol *’ {aka ‘struct _Py_UopsSymbol *’} [-Wincompatible-pointer-types]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.5)

assignment to ‘_PyInterpreterFrame *’ {aka ‘struct _PyInterpreterFrame *’} from incompatible pointer type ‘_Py_UopsSymbol *’ {aka ‘struct _Py_UopsSymbol *’} [-Wincompatible-pointer-types]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.2.1)

assignment to ‘_PyInterpreterFrame *’ {aka ‘struct _PyInterpreterFrame *’} from incompatible pointer type ‘_Py_UopsSymbol *’ {aka ‘struct _Py_UopsSymbol *’} [-Wincompatible-pointer-types]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Address sanitizer

assignment to ‘_PyInterpreterFrame *’ from incompatible pointer type ‘_Py_UopsSymbol *’ [-Wincompatible-pointer-types]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Hypothesis tests on Ubuntu

assignment to ‘_PyInterpreterFrame *’ {aka ‘struct _PyInterpreterFrame *’} from incompatible pointer type ‘_Py_UopsSymbol *’ {aka ‘struct _Py_UopsSymbol *’} [-Wincompatible-pointer-types]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Windows / build and test (x64)

'=': incompatible types - from '_Py_UopsSymbol *' to '_PyInterpreterFrame *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Windows / build and test (x64)

'=': incompatible types - from '_Py_UopsSymbol *' to '_PyInterpreterFrame *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Windows / build (arm64)

'=': incompatible types - from '_Py_UopsSymbol *' to '_PyInterpreterFrame *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Windows / build (arm64)

'=': incompatible types - from '_Py_UopsSymbol *' to '_PyInterpreterFrame *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Ubuntu / build and test

assignment to ‘_PyInterpreterFrame *’ {aka ‘struct _PyInterpreterFrame *’} from incompatible pointer type ‘_Py_UopsSymbol *’ {aka ‘struct _Py_UopsSymbol *’} [-Wincompatible-pointer-types]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Windows (free-threading) / build and test (x64)

'=': incompatible types - from '_Py_UopsSymbol *' to '_PyInterpreterFrame *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Windows (free-threading) / build and test (x64)

'=': incompatible types - from '_Py_UopsSymbol *' to '_PyInterpreterFrame *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Ubuntu (free-threading) / build and test

assignment to ‘_PyInterpreterFrame *’ {aka ‘struct _PyInterpreterFrame *’} from incompatible pointer type ‘_Py_UopsSymbol *’ {aka ‘struct _Py_UopsSymbol *’} [-Wincompatible-pointer-types]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Windows (free-threading) / build (arm64)

'=': incompatible types - from '_Py_UopsSymbol *' to '_PyInterpreterFrame *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 1444 in Python/optimizer_cases.c.h

GitHub Actions / Windows (free-threading) / build (arm64)

'=': incompatible types - from '_Py_UopsSymbol *' to '_PyInterpreterFrame *' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
if (gen_frame == NULL) goto out_of_space;
stack_pointer[0] = (_Py_UopsSymbol *)gen_frame;
stack_pointer += 1;