Skip to content

Commit c118c24

Browse files
authored
bpo-46161: Fix bug in starunpack_helper in compile.c (GH-30235)
1 parent 62a6594 commit c118c24

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Lib/test/test_class.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,5 +666,23 @@ def __init__(self, *args, **kwargs):
666666
with self.assertRaisesRegex(TypeError, error_msg):
667667
object.__init__(E(), 42)
668668

669+
def testClassWithExtCall(self):
670+
class Meta(int):
671+
def __init__(*args, **kwargs):
672+
pass
673+
674+
def __new__(cls, name, bases, attrs, **kwargs):
675+
return bases, kwargs
676+
677+
d = {'metaclass': Meta}
678+
679+
class A(**d): pass
680+
self.assertEqual(A, ((), {}))
681+
class A(0, 1, 2, 3, 4, 5, 6, 7, **d): pass
682+
self.assertEqual(A, (tuple(range(8)), {}))
683+
class A(0, *range(1, 8), **d, foo='bar'): pass
684+
self.assertEqual(A, (tuple(range(8)), {'foo': 'bar'}))
685+
686+
669687
if __name__ == '__main__':
670688
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the class building error when the arguments are constants and CALL_FUNCTION_EX is used.

Python/compile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4233,7 +4233,7 @@ starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
42334233
Py_INCREF(val);
42344234
PyTuple_SET_ITEM(folded, i, val);
42354235
}
4236-
if (tuple) {
4236+
if (tuple && !pushed) {
42374237
ADDOP_LOAD_CONST_NEW(c, folded);
42384238
} else {
42394239
if (add == SET_ADD) {
@@ -4245,6 +4245,9 @@ starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
42454245
ADDOP_I(c, build, pushed);
42464246
ADDOP_LOAD_CONST_NEW(c, folded);
42474247
ADDOP_I(c, extend, 1);
4248+
if (tuple) {
4249+
ADDOP(c, LIST_TO_TUPLE);
4250+
}
42484251
}
42494252
return 1;
42504253
}

0 commit comments

Comments
 (0)