Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __iter__(self) -> Iterator[str]:
return iter(self.__dataclass_fields__)


@dataclass(frozen=True)
@dataclass(frozen=True, kw_only=True)
class Argparse(ThemeSection):
usage: str = ANSIColors.BOLD_BLUE
prog: str = ANSIColors.BOLD_MAGENTA
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test__colorize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import dataclasses
import io
import sys
import unittest
Expand All @@ -21,6 +22,42 @@ def supports_virtual_terminal():
return contextlib.nullcontext()


class TestTheme(unittest.TestCase):

def test_attributes(self):
# only theme configurations attributes by default
for field in dataclasses.fields(_colorize.Theme):
with self.subTest(field.name):
self.assertIsSubclass(field.type, _colorize.ThemeSection)
self.assertIsNotNone(field.default_factory)

def test_copy_with(self):
theme = _colorize.Theme()

copy = theme.copy_with()
self.assertEqual(theme, copy)

unittest_no_colors = _colorize.Unittest.no_colors()
copy = theme.copy_with(unittest=unittest_no_colors)
self.assertEqual(copy.argparse, theme.argparse)
self.assertEqual(copy.difflib, theme.difflib)
self.assertEqual(copy.syntax, theme.syntax)
self.assertEqual(copy.traceback, theme.traceback)
self.assertEqual(copy.unittest, unittest_no_colors)

def test_no_colors(self):
# idempotence test
theme_no_colors = _colorize.Theme().no_colors()
theme_no_colors_no_colors = theme_no_colors.no_colors()
self.assertEqual(theme_no_colors, theme_no_colors_no_colors)

# attributes check
for section in dataclasses.fields(_colorize.Theme):
with self.subTest(section.name):
section_theme = getattr(theme_no_colors, section.name)
self.assertEqual(section_theme, section.type.no_colors())


class TestColorizeFunction(unittest.TestCase):
def test_colorized_detection_checks_for_environment_variables(self):
def check(env, fallback, expected):
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_pyexpat.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,23 @@ def test_change_size_2(self):
parser.Parse(xml2, True)
self.assertEqual(self.n, 4)

class ElementDeclHandlerTest(unittest.TestCase):
def test_trigger_leak(self):
# Unfixed, this test would leak the memory of the so-called
# "content model" in function ``my_ElementDeclHandler`` of pyexpat.
# See https://github.com/python/cpython/issues/140593.
data = textwrap.dedent('''\
<!DOCTYPE quotations SYSTEM "quotations.dtd" [
<!ELEMENT root ANY>
]>
<root/>
''').encode('UTF-8')

parser = expat.ParserCreate()
parser.NotStandaloneHandler = lambda: 1.234 # arbitrary float
parser.ElementDeclHandler = lambda _1, _2: None
self.assertRaises(TypeError, parser.Parse, data, True)

class MalformedInputTest(unittest.TestCase):
def test1(self):
xml = b"\0\r\n"
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,9 @@ class TestDumbTerminal(ReplTestCase):
def test_dumb_terminal_exits_cleanly(self):
env = os.environ.copy()
env.pop('PYTHON_BASIC_REPL', None)
# Ignore PYTHONSTARTUP to not pollute the output
# with an unrelated traceback. See GH-137568.
env.pop('PYTHONSTARTUP', None)
env.update({"TERM": "dumb"})
output, exit_code = self.run_repl("exit()\n", env=env)
self.assertEqual(exit_code, 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`xml.parsers.expat`: Fix a memory leak that could affect users with
:meth:`~xml.parsers.expat.xmlparser.ElementDeclHandler` set to a custom
element declaration handler. Patch by Sebastian Pipping.
2 changes: 1 addition & 1 deletion Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ my_ElementDeclHandler(void *userData,
PyObject *modelobj, *nameobj;

if (PyErr_Occurred())
return;
goto finally;

if (flush_character_buffer(self) < 0)
goto finally;
Expand Down
Loading