From 7927a1e23a10580060cc76bdf0e43979040db8ae Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Wed, 14 Feb 2024 02:13:12 +0800 Subject: [PATCH 1/3] Fix flaky globals to constant test --- Lib/test/test_capi/test_opt.py | 47 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index b64aed10d2d653..578e718ab800de 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -7,6 +7,8 @@ import _testinternalcapi +from test.support import script_helper + @contextlib.contextmanager def temporary_optimizer(opt): @@ -659,7 +661,7 @@ def dummy(x): opt = _testinternalcapi.get_uop_optimizer() with temporary_optimizer(opt): - testfunc(20) + testfunc(32) ex = get_first_executor(testfunc) self.assertIsNotNone(ex) @@ -677,10 +679,10 @@ def testfunc(n): opt = _testinternalcapi.get_uop_optimizer() with temporary_optimizer(opt): - res = testfunc(20) + res = testfunc(32) ex = get_first_executor(testfunc) - self.assertEqual(res, 19 * 2) + self.assertEqual(res, 62) self.assertIsNotNone(ex) uops = {opname for opname, _, _ in ex} self.assertNotIn("_GUARD_BOTH_INT", uops) @@ -699,7 +701,7 @@ def testfunc(n): opt = _testinternalcapi.get_uop_optimizer() with temporary_optimizer(opt): - res = testfunc(20) + res = testfunc(32) ex = get_first_executor(testfunc) self.assertEqual(res, 4) @@ -716,7 +718,7 @@ def testfunc(n): opt = _testinternalcapi.get_uop_optimizer() with temporary_optimizer(opt): - testfunc(20) + testfunc(32) ex = get_first_executor(testfunc) self.assertIsNotNone(ex) @@ -740,7 +742,7 @@ def testfunc(n): def dummy(x): return x + 2 - testfunc(10) + testfunc(32) ex = get_first_executor(testfunc) # Honestly as long as it doesn't crash it's fine. @@ -749,20 +751,39 @@ def dummy(x): # This test is a little implementation specific. def test_promote_globals_to_constants(self): + + result = script_helper.run_python_until_end('-c', textwrap.dedent(""" + import _testinternalcapi + import opcode + + def get_first_executor(func): + code = func.__code__ + co_code = code.co_code + JUMP_BACKWARD = opcode.opmap["JUMP_BACKWARD"] + for i in range(0, len(co_code), 2): + if co_code[i] == JUMP_BACKWARD: + try: + return _testinternalcapi.get_executor(code, i) + except ValueError: + pass + return None + def testfunc(n): for i in range(n): x = range(i) return x - + opt = _testinternalcapi.get_uop_optimizer() - with temporary_optimizer(opt): - testfunc(20) - + _testinternalcapi.set_optimizer(opt) + testfunc(64) + ex = get_first_executor(testfunc) - self.assertIsNotNone(ex) + assert ex is not None uops = {opname for opname, _, _ in ex} - self.assertNotIn("_LOAD_GLOBAL_BUILTIN", uops) - self.assertIn("_LOAD_CONST_INLINE_BORROW_WITH_NULL", uops) + assert "_LOAD_GLOBAL_BUILTINS" not in uops + assert "_LOAD_CONST_INLINE_BORROW_WITH_NULL" in uops + """)) + self.assertEqual(result[0].rc, 0) From bc52c0e8233f338151397adfa3a20d90e5e8ebd4 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Wed, 14 Feb 2024 02:16:15 +0800 Subject: [PATCH 2/3] fix whitespace --- Lib/test/test_capi/test_opt.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 578e718ab800de..9b78a6e6b4db1f 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -755,7 +755,7 @@ def test_promote_globals_to_constants(self): result = script_helper.run_python_until_end('-c', textwrap.dedent(""" import _testinternalcapi import opcode - + def get_first_executor(func): code = func.__code__ co_code = code.co_code @@ -766,17 +766,17 @@ def get_first_executor(func): return _testinternalcapi.get_executor(code, i) except ValueError: pass - return None - + return None + def testfunc(n): for i in range(n): x = range(i) return x - + opt = _testinternalcapi.get_uop_optimizer() _testinternalcapi.set_optimizer(opt) testfunc(64) - + ex = get_first_executor(testfunc) assert ex is not None uops = {opname for opname, _, _ in ex} From 9eb786829d36b7e022a08282066b22a78605d06e Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Wed, 14 Feb 2024 08:41:17 +0800 Subject: [PATCH 3/3] Display stdout and stderr on error Co-authored-by: Victor Stinner --- Lib/test/test_capi/test_opt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 9b78a6e6b4db1f..1a8ed3441fa855 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -783,7 +783,7 @@ def testfunc(n): assert "_LOAD_GLOBAL_BUILTINS" not in uops assert "_LOAD_CONST_INLINE_BORROW_WITH_NULL" in uops """)) - self.assertEqual(result[0].rc, 0) + self.assertEqual(result[0].rc, 0, result)