Skip to content

Commit cf17d92

Browse files
committed
fix review idea
Signed-off-by: Manjusaka <[email protected]>
1 parent b1640ed commit cf17d92

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,19 +1926,32 @@ def testfunc(n):
19261926
self.assertNotIn("_GUARD_TOS_INT", uops)
19271927

19281928
def test_get_len(self):
1929-
def testfunc(n):
1929+
def testfunc1(n):
19301930
x = 0
1931-
a = [1, 2, 3, 4]
19321931
for _ in range(n):
1933-
match a:
1932+
match (1, 2, 3, 4):
19341933
case [_, _, _, _]:
19351934
x += 1
19361935
return x
1937-
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1936+
res, ex = self._run_with_optimizer(testfunc1, TIER2_THRESHOLD)
1937+
self.assertEqual(res, TIER2_THRESHOLD)
1938+
uops = get_opnames(ex)
1939+
self.assertNotIn("_GUARD_TOS_INT", uops)
1940+
self.assertIn("_GET_LEN", uops)
1941+
self.assertIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
1942+
def testfunc1(n):
1943+
x = 0
1944+
for _ in range(n):
1945+
match [1, 2, 3, 4]:
1946+
case [_, _, _, _]:
1947+
x += 1
1948+
return x
1949+
res, ex = self._run_with_optimizer(testfunc1, TIER2_THRESHOLD)
19381950
self.assertEqual(res, TIER2_THRESHOLD)
19391951
uops = get_opnames(ex)
19401952
self.assertNotIn("_GUARD_TOS_INT", uops)
19411953
self.assertIn("_GET_LEN", uops)
1954+
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
19421955

19431956
def test_binary_op_subscr_tuple_int(self):
19441957
def testfunc(n):

Python/optimizer_bytecodes.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "pycore_uops.h"
44
#include "pycore_uop_ids.h"
55
#include "internal/pycore_moduleobject.h"
6+
#include "tupleobject.h"
67

78
#define op(name, ...) /* NAME is ignored */
89

@@ -1095,7 +1096,12 @@ dummy_func(void) {
10951096
}
10961097
else {
10971098
assert(tuple_length >= 0);
1098-
len = sym_new_const(ctx, PyLong_FromLong(tuple_length));
1099+
PyObject *temp = PyLong_FromLong(tuple_length);
1100+
if (temp == NULL) {
1101+
goto error;
1102+
}
1103+
len = sym_new_const(ctx, temp);
1104+
Py_DECREF(temp);
10991105
}
11001106
}
11011107

Python/optimizer_cases.c.h

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)