bpo-40334: Rewrite test_c_parser to avoid memory leaks (GH-19694)

Previously every test was building an extension module and
loading it into sys.modules. The tearDown function was thus
not able to clean up correctly, resulting in memory leaks.

With this commit, every test function now builds the extension
module and runs the actual test code in a new process
(using assert_python_ok), so that sys.modules stays intact
and no memory gets leaked.
This commit is contained in:
Lysandros Nikolaou 2020-04-24 16:51:09 +03:00 committed by GitHub
parent e6f8abd500
commit 24ffe705c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 146 additions and 83 deletions

View file

@ -83,6 +83,7 @@ def compile_c_extension(
cmd.inplace = True
if build_dir:
cmd.build_temp = build_dir
cmd.build_lib = build_dir
cmd.ensure_finalized()
cmd.run()

View file

@ -92,9 +92,7 @@ def generate_parser_c_extension(
with open(source, "w") as file:
genr = CParserGenerator(grammar, file, debug=debug)
genr.generate("parse.c")
extension_path = compile_c_extension(str(source), build_dir=str(path / "build"))
extension = import_file("parse", extension_path)
return extension
compile_c_extension(str(source), build_dir=str(path))
def print_memstats() -> bool: