From 96ebd50d064605f8501cd741fe40dc7f915c08fd Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 13 Jul 2023 11:37:08 +0300 Subject: [PATCH 1/7] gh-105540: Convert `pytest` tests of `cases_generator` to regular tests --- .../test/test_generated_cases.py | 325 ++++++++++-------- 1 file changed, 178 insertions(+), 147 deletions(-) rename Tools/cases_generator/test_generator.py => Lib/test/test_generated_cases.py (61%) diff --git a/Tools/cases_generator/test_generator.py b/Lib/test/test_generated_cases.py similarity index 61% rename from Tools/cases_generator/test_generator.py rename to Lib/test/test_generated_cases.py index e374ac41e6a94d..e4fe11a517a935 100644 --- a/Tools/cases_generator/test_generator.py +++ b/Lib/test/test_generated_cases.py @@ -1,97 +1,128 @@ -# Sorry for using pytest, these tests are mostly just for me. -# Use pytest -vv for best results. - import tempfile +import unittest + +from test import test_tools + +test_tools.skip_if_missing('cases_generator') +with test_tools.imports_under_tool('cases_generator'): + import generate_cases + from parser import StackEffect + + +class TestEffects(unittest.TestCase): + def test_effect_sizes(self): + input_effects = [ + x := StackEffect("x", "", "", ""), + y := StackEffect("y", "", "", "oparg"), + z := StackEffect("z", "", "", "oparg*2"), + ] + output_effects = [ + StackEffect("a", "", "", ""), + StackEffect("b", "", "", "oparg*4"), + StackEffect("c", "", "", ""), + ] + other_effects = [ + StackEffect("p", "", "", "oparg<<1"), + StackEffect("q", "", "", ""), + StackEffect("r", "", "", ""), + ] + self.assertEqual(generate_cases.effect_size(x), (1, "")) + self.assertEqual(generate_cases.effect_size(y), (0, "oparg")) + self.assertEqual(generate_cases.effect_size(z), (0, "oparg*2")) + + self.assertEqual( + generate_cases.list_effect_size(input_effects), + (1, "oparg + oparg*2"), + ) + self.assertEqual( + generate_cases.list_effect_size(output_effects), + (2, "oparg*4"), + ) + self.assertEqual( + generate_cases.list_effect_size(other_effects), + (2, "(oparg<<1)"), + ) + + self.assertEqual( + generate_cases.string_effect_size( + generate_cases.list_effect_size(input_effects), + ), "1 + oparg + oparg*2", + ) + self.assertEqual( + generate_cases.string_effect_size( + generate_cases.list_effect_size(output_effects), + ), + "2 + oparg*4", + ) + self.assertEqual( + generate_cases.string_effect_size( + generate_cases.list_effect_size(other_effects), + ), + "2 + (oparg<<1)", + ) + + +class TestGeneratedCases(unittest.TestCase): + def run_cases_test(self, input: str, expected: str): + with ( + tempfile.NamedTemporaryFile("w+") as temp_input, + tempfile.NamedTemporaryFile("w+") as temp_output, + tempfile.NamedTemporaryFile("w+") as temp_metadata, + tempfile.NamedTemporaryFile("w+") as temp_pymetadata, + tempfile.NamedTemporaryFile("w+") as temp_executor, + ): + temp_input.write(generate_cases.BEGIN_MARKER) + temp_input.write(input) + temp_input.write(generate_cases.END_MARKER) + temp_input.flush() + + a = generate_cases.Analyzer( + [temp_input.name], + temp_output.name, + temp_metadata.name, + temp_pymetadata.name, + temp_executor.name, + ) + a.parse() + a.analyze() + if a.errors: + raise RuntimeError(f"Found {a.errors} errors") + a.write_instructions() + temp_output.seek(0) + lines = temp_output.readlines() + while lines and lines[0].startswith("// "): + lines.pop(0) + actual = "".join(lines) + # if actual.rstrip() != expected.rstrip(): + # print("Actual:") + # print(actual) + # print("Expected:") + # print(expected) + # print("End") + + self.assertEqual(actual.rstrip(), expected.rstrip()) -import generate_cases -from parser import StackEffect - - -def test_effect_sizes(): - input_effects = [ - x := StackEffect("x", "", "", ""), - y := StackEffect("y", "", "", "oparg"), - z := StackEffect("z", "", "", "oparg*2"), - ] - output_effects = [ - StackEffect("a", "", "", ""), - StackEffect("b", "", "", "oparg*4"), - StackEffect("c", "", "", ""), - ] - other_effects = [ - StackEffect("p", "", "", "oparg<<1"), - StackEffect("q", "", "", ""), - StackEffect("r", "", "", ""), - ] - assert generate_cases.effect_size(x) == (1, "") - assert generate_cases.effect_size(y) == (0, "oparg") - assert generate_cases.effect_size(z) == (0, "oparg*2") - - assert generate_cases.list_effect_size(input_effects) == (1, "oparg + oparg*2") - assert generate_cases.list_effect_size(output_effects) == (2, "oparg*4") - assert generate_cases.list_effect_size(other_effects) == (2, "(oparg<<1)") - - assert generate_cases.string_effect_size(generate_cases.list_effect_size(input_effects)) == "1 + oparg + oparg*2" - assert generate_cases.string_effect_size(generate_cases.list_effect_size(output_effects)) == "2 + oparg*4" - assert generate_cases.string_effect_size(generate_cases.list_effect_size(other_effects)) == "2 + (oparg<<1)" - - -def run_cases_test(input: str, expected: str): - temp_input = tempfile.NamedTemporaryFile("w+") - temp_input.write(generate_cases.BEGIN_MARKER) - temp_input.write(input) - temp_input.write(generate_cases.END_MARKER) - temp_input.flush() - temp_output = tempfile.NamedTemporaryFile("w+") - temp_metadata = tempfile.NamedTemporaryFile("w+") - temp_pymetadata = tempfile.NamedTemporaryFile("w+") - temp_executor = tempfile.NamedTemporaryFile("w+") - a = generate_cases.Analyzer( - [temp_input.name], - temp_output.name, - temp_metadata.name, - temp_pymetadata.name, - temp_executor.name, - ) - a.parse() - a.analyze() - if a.errors: - raise RuntimeError(f"Found {a.errors} errors") - a.write_instructions() - temp_output.seek(0) - lines = temp_output.readlines() - while lines and lines[0].startswith("// "): - lines.pop(0) - actual = "".join(lines) - # if actual.rstrip() != expected.rstrip(): - # print("Actual:") - # print(actual) - # print("Expected:") - # print(expected) - # print("End") - assert actual.rstrip() == expected.rstrip() - -def test_inst_no_args(): - input = """ + def test_inst_no_args(self): + input = """ inst(OP, (--)) { spam(); } """ - output = """ + output = """ TARGET(OP) { spam(); DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_inst_one_pop(): - input = """ + def test_inst_one_pop(self): + input = """ inst(OP, (value --)) { spam(); } """ - output = """ + output = """ TARGET(OP) { PyObject *value = stack_pointer[-1]; spam(); @@ -99,15 +130,15 @@ def test_inst_one_pop(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_inst_one_push(): - input = """ + def test_inst_one_push(self): + input = """ inst(OP, (-- res)) { spam(); } """ - output = """ + output = """ TARGET(OP) { PyObject *res; spam(); @@ -116,15 +147,15 @@ def test_inst_one_push(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_inst_one_push_one_pop(): - input = """ + def test_inst_one_push_one_pop(self): + input = """ inst(OP, (value -- res)) { spam(); } """ - output = """ + output = """ TARGET(OP) { PyObject *value = stack_pointer[-1]; PyObject *res; @@ -133,15 +164,15 @@ def test_inst_one_push_one_pop(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_binary_op(): - input = """ + def test_binary_op(self): + input = """ inst(OP, (left, right -- res)) { spam(); } """ - output = """ + output = """ TARGET(OP) { PyObject *right = stack_pointer[-1]; PyObject *left = stack_pointer[-2]; @@ -152,15 +183,15 @@ def test_binary_op(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_overlap(): - input = """ + def test_overlap(self): + input = """ inst(OP, (left, right -- left, result)) { spam(); } """ - output = """ + output = """ TARGET(OP) { PyObject *right = stack_pointer[-1]; PyObject *left = stack_pointer[-2]; @@ -170,10 +201,10 @@ def test_overlap(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_predictions_and_eval_breaker(): - input = """ + def test_predictions_and_eval_breaker(self): + input = """ inst(OP1, (--)) { } inst(OP3, (arg -- res)) { @@ -181,7 +212,7 @@ def test_predictions_and_eval_breaker(): CHECK_EVAL_BREAKER(); } """ - output = """ + output = """ TARGET(OP1) { PREDICTED(OP1); DISPATCH(); @@ -196,43 +227,43 @@ def test_predictions_and_eval_breaker(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_error_if_plain(): - input = """ + def test_error_if_plain(self): + input = """ inst(OP, (--)) { ERROR_IF(cond, label); } """ - output = """ + output = """ TARGET(OP) { if (cond) goto label; DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_error_if_plain_with_comment(): - input = """ + def test_error_if_plain_with_comment(self): + input = """ inst(OP, (--)) { ERROR_IF(cond, label); // Comment is ok } """ - output = """ + output = """ TARGET(OP) { if (cond) goto label; DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_error_if_pop(): - input = """ + def test_error_if_pop(self): + input = """ inst(OP, (left, right -- res)) { ERROR_IF(cond, label); } """ - output = """ + output = """ TARGET(OP) { PyObject *right = stack_pointer[-1]; PyObject *left = stack_pointer[-2]; @@ -243,14 +274,14 @@ def test_error_if_pop(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_cache_effect(): - input = """ + def test_cache_effect(self): + input = """ inst(OP, (counter/1, extra/2, value --)) { } """ - output = """ + output = """ TARGET(OP) { PyObject *value = stack_pointer[-1]; uint16_t counter = read_u16(&next_instr[0].cache); @@ -260,23 +291,23 @@ def test_cache_effect(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_suppress_dispatch(): - input = """ + def test_suppress_dispatch(self): + input = """ inst(OP, (--)) { goto somewhere; } """ - output = """ + output = """ TARGET(OP) { goto somewhere; } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_macro_instruction(): - input = """ + def test_macro_instruction(self): + input = """ inst(OP1, (counter/1, left, right -- left, right)) { op1(left, right); } @@ -289,7 +320,7 @@ def test_macro_instruction(): } family(op, INLINE_CACHE_ENTRIES_OP) = { OP, OP3 }; """ - output = """ + output = """ TARGET(OP1) { PyObject *right = stack_pointer[-1]; PyObject *left = stack_pointer[-2]; @@ -339,15 +370,15 @@ def test_macro_instruction(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_array_input(): - input = """ + def test_array_input(self): + input = """ inst(OP, (below, values[oparg*2], above --)) { spam(); } """ - output = """ + output = """ TARGET(OP) { PyObject *above = stack_pointer[-1]; PyObject **values = (stack_pointer - (1 + oparg*2)); @@ -358,15 +389,15 @@ def test_array_input(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_array_output(): - input = """ + def test_array_output(self): + input = """ inst(OP, (unused, unused -- below, values[oparg*3], above)) { spam(values, oparg); } """ - output = """ + output = """ TARGET(OP) { PyObject *below; PyObject **values = stack_pointer - (2) + 1; @@ -378,15 +409,15 @@ def test_array_output(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_array_input_output(): - input = """ + def test_array_input_output(self): + input = """ inst(OP, (values[oparg] -- values[oparg], above)) { spam(values, oparg); } """ - output = """ + output = """ TARGET(OP) { PyObject **values = (stack_pointer - oparg); PyObject *above; @@ -396,15 +427,15 @@ def test_array_input_output(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_array_error_if(): - input = """ + def test_array_error_if(self): + input = """ inst(OP, (extra, values[oparg] --)) { ERROR_IF(oparg == 0, somewhere); } """ - output = """ + output = """ TARGET(OP) { PyObject **values = (stack_pointer - oparg); PyObject *extra = stack_pointer[-(1 + oparg)]; @@ -414,15 +445,15 @@ def test_array_error_if(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_cond_effect(): - input = """ + def test_cond_effect(self): + input = """ inst(OP, (aa, input if ((oparg & 1) == 1), cc -- xx, output if (oparg & 2), zz)) { output = spam(oparg, input); } """ - output = """ + output = """ TARGET(OP) { PyObject *cc = stack_pointer[-1]; PyObject *input = ((oparg & 1) == 1) ? stack_pointer[-(1 + (((oparg & 1) == 1) ? 1 : 0))] : NULL; @@ -439,10 +470,10 @@ def test_cond_effect(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) -def test_macro_cond_effect(): - input = """ + def test_macro_cond_effect(self): + input = """ op(A, (left, middle, right --)) { # Body of A } @@ -451,7 +482,7 @@ def test_macro_cond_effect(): } macro(M) = A + B; """ - output = """ + output = """ TARGET(M) { PyObject *_tmp_1 = stack_pointer[-1]; PyObject *_tmp_2 = stack_pointer[-2]; @@ -479,4 +510,4 @@ def test_macro_cond_effect(): DISPATCH(); } """ - run_cases_test(input, output) + self.run_cases_test(input, output) From b7ea63759a100827ba843f4e97eb5006a77064fd Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 13 Jul 2023 12:24:19 +0300 Subject: [PATCH 2/7] Fix test run on Windows --- Lib/test/test_generated_cases.py | 56 ++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/Lib/test/test_generated_cases.py b/Lib/test/test_generated_cases.py index e4fe11a517a935..18edaea2323ec4 100644 --- a/Lib/test/test_generated_cases.py +++ b/Lib/test/test_generated_cases.py @@ -1,5 +1,7 @@ import tempfile import unittest +import os +import shutil from test import test_tools @@ -63,36 +65,48 @@ def test_effect_sizes(self): class TestGeneratedCases(unittest.TestCase): + def setUp(self) -> None: + super().setUp() + + self.temp_dir = tempfile.gettempdir() + self.temp_input_filename = os.path.join(self.temp_dir, "input.txt") + self.temp_output_filename = os.path.join(self.temp_dir, "output.txt") + self.temp_metadata_filename = os.path.join(self.temp_dir, "metadata.txt") + self.temp_pymetadata_filename = os.path.join(self.temp_dir, "pymetadata.txt") + self.temp_executor_filename = os.path.join(self.temp_dir, "executor.txt") + + def tearDown(self) -> None: + try: + shutil.rmtree(self.temp_dir) + except: + pass + super().tearDown() + def run_cases_test(self, input: str, expected: str): - with ( - tempfile.NamedTemporaryFile("w+") as temp_input, - tempfile.NamedTemporaryFile("w+") as temp_output, - tempfile.NamedTemporaryFile("w+") as temp_metadata, - tempfile.NamedTemporaryFile("w+") as temp_pymetadata, - tempfile.NamedTemporaryFile("w+") as temp_executor, - ): + with open(self.temp_input_filename, "w+") as temp_input: temp_input.write(generate_cases.BEGIN_MARKER) temp_input.write(input) temp_input.write(generate_cases.END_MARKER) temp_input.flush() - a = generate_cases.Analyzer( - [temp_input.name], - temp_output.name, - temp_metadata.name, - temp_pymetadata.name, - temp_executor.name, - ) - a.parse() - a.analyze() - if a.errors: - raise RuntimeError(f"Found {a.errors} errors") - a.write_instructions() - temp_output.seek(0) + a = generate_cases.Analyzer( + [self.temp_input_filename], + self.temp_output_filename, + self.temp_metadata_filename, + self.temp_pymetadata_filename, + self.temp_executor_filename, + ) + a.parse() + a.analyze() + if a.errors: + raise RuntimeError(f"Found {a.errors} errors") + a.write_instructions() + + with open(self.temp_output_filename) as temp_output: lines = temp_output.readlines() while lines and lines[0].startswith("// "): lines.pop(0) - actual = "".join(lines) + actual = "".join(lines) # if actual.rstrip() != expected.rstrip(): # print("Actual:") # print(actual) From f22fd88d15483849cec32e123458cb5f20762c3f Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 13 Jul 2023 12:33:06 +0300 Subject: [PATCH 3/7] Fix Linux build --- Lib/test/test_generated_cases.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_generated_cases.py b/Lib/test/test_generated_cases.py index 18edaea2323ec4..06e4cd634645be 100644 --- a/Lib/test/test_generated_cases.py +++ b/Lib/test/test_generated_cases.py @@ -1,7 +1,6 @@ import tempfile import unittest import os -import shutil from test import test_tools @@ -76,10 +75,17 @@ def setUp(self) -> None: self.temp_executor_filename = os.path.join(self.temp_dir, "executor.txt") def tearDown(self) -> None: - try: - shutil.rmtree(self.temp_dir) - except: - pass + for filename in [ + self.temp_input_filename, + self.temp_output_filename, + self.temp_metadata_filename, + self.temp_pymetadata_filename, + self.temp_executor_filename, + ]: + try: + os.remove(filename) + except: + pass super().tearDown() def run_cases_test(self, input: str, expected: str): From 0bfb1e16bcbbfc4e3f03407a4a6839b3055106b2 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 13 Jul 2023 15:12:52 +0300 Subject: [PATCH 4/7] Fix Windows build --- Tools/cases_generator/generate_cases.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py index 14269ca8cbe750..fca249502f2548 100644 --- a/Tools/cases_generator/generate_cases.py +++ b/Tools/cases_generator/generate_cases.py @@ -735,8 +735,8 @@ def parse_file(self, filename: str, instrs_idx: dict[str, int]) -> None: with open(filename) as file: src = file.read() - filename = os.path.relpath(filename, ROOT) - # Make filename more user-friendly and less platform-specific + # Make filename more user-friendly and less platform-specific, + # it is only used for error reporting at this point. filename = filename.replace("\\", "/") if filename.startswith("./"): filename = filename[2:] From 79f01b89831480bb30eada54b19af2da993ad306 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 13 Jul 2023 16:03:23 +0300 Subject: [PATCH 5/7] Fix Windows build --- Tools/cases_generator/generate_cases.py | 30 ++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py index fca249502f2548..5aa60bad0209d3 100644 --- a/Tools/cases_generator/generate_cases.py +++ b/Tools/cases_generator/generate_cases.py @@ -156,16 +156,9 @@ def __init__( self.emit_line_directives = emit_line_directives self.comment = comment self.lineno = 1 - filename = os.path.relpath(self.stream.name, ROOT) - # Make filename more user-friendly and less platform-specific - filename = filename.replace("\\", "/") - if filename.startswith("./"): - filename = filename[2:] - if filename.endswith(".new"): - filename = filename[:-4] - self.filename = filename + self.filename = prettify_filename(self.stream.name) self.nominal_lineno = 1 - self.nominal_filename = filename + self.nominal_filename = self.filename def write_raw(self, s: str) -> None: self.stream.write(s) @@ -735,12 +728,8 @@ def parse_file(self, filename: str, instrs_idx: dict[str, int]) -> None: with open(filename) as file: src = file.read() - # Make filename more user-friendly and less platform-specific, - # it is only used for error reporting at this point. - filename = filename.replace("\\", "/") - if filename.startswith("./"): - filename = filename[2:] - psr = parser.Parser(src, filename=filename) + + psr = parser.Parser(src, filename=prettify_filename(filename)) # Skip until begin marker while tkn := psr.next(raw=True): @@ -1584,6 +1573,17 @@ def wrap_macro(self, mac: MacroInstruction): self.out.emit(f"DISPATCH();") +def prettify_filename(filename: str) -> str: + # Make filename more user-friendly and less platform-specific, + # it is only used for error reporting at this point. + filename = filename.replace("\\", "/") + if filename.startswith("./"): + filename = filename[2:] + if filename.endswith(".new"): + filename = filename[:-4] + return filename + + def extract_block_text(block: parser.Block) -> tuple[list[str], bool, int]: # Get lines of text with proper dedent blocklines = block.text.splitlines(True) From 2286f99f00fee0d999e9eb9924cb1645edc740bf Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 13 Jul 2023 16:37:08 +0300 Subject: [PATCH 6/7] Fix Windows build --- Tools/cases_generator/generate_cases.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py index 5aa60bad0209d3..3768b408d3e649 100644 --- a/Tools/cases_generator/generate_cases.py +++ b/Tools/cases_generator/generate_cases.py @@ -1147,7 +1147,7 @@ def write_function( def from_source_files(self) -> str: paths = f"\n{self.out.comment} ".join( - os.path.relpath(filename, ROOT).replace(os.path.sep, posixpath.sep) + prettify_filename(filename) for filename in self.input_filenames ) return f"{self.out.comment} from:\n{self.out.comment} {paths}\n" From 58ebcc78e0d87fee3e03e5515c5df6ef85d1c705 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 14 Jul 2023 09:04:15 +0300 Subject: [PATCH 7/7] Add `unittest.main` --- Lib/test/test_generated_cases.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_generated_cases.py b/Lib/test/test_generated_cases.py index 06e4cd634645be..a68d15d47ab5f3 100644 --- a/Lib/test/test_generated_cases.py +++ b/Lib/test/test_generated_cases.py @@ -531,3 +531,7 @@ def test_macro_cond_effect(self): } """ self.run_cases_test(input, output) + + +if __name__ == "__main__": + unittest.main()