Mailing List Archive

bpo-46161: Fix bug in starunpack_helper in compile.c (GH-30235)
https://github.com/python/cpython/commit/c118c2455c95baea08045dc64963600b7a56b6fd
commit: c118c2455c95baea08045dc64963600b7a56b6fd
branch: main
author: zq1997 <email2zq@qq.com>
committer: markshannon <mark@hotpy.org>
date: 2022-01-17T17:45:44Z
summary:

bpo-46161: Fix bug in starunpack_helper in compile.c (GH-30235)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-12-23-12-32-45.bpo-46161.EljBmu.rst
M Lib/test/test_class.py
M Python/compile.c

diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py
index 7524f58a3ce73..7cf5e06d59e20 100644
--- a/Lib/test/test_class.py
+++ b/Lib/test/test_class.py
@@ -666,5 +666,23 @@ def __init__(self, *args, **kwargs):
with self.assertRaisesRegex(TypeError, error_msg):
object.__init__(E(), 42)

+ def testClassWithExtCall(self):
+ class Meta(int):
+ def __init__(*args, **kwargs):
+ pass
+
+ def __new__(cls, name, bases, attrs, **kwargs):
+ return bases, kwargs
+
+ d = {'metaclass': Meta}
+
+ class A(**d): pass
+ self.assertEqual(A, ((), {}))
+ class A(0, 1, 2, 3, 4, 5, 6, 7, **d): pass
+ self.assertEqual(A, (tuple(range(8)), {}))
+ class A(0, *range(1, 8), **d, foo='bar'): pass
+ self.assertEqual(A, (tuple(range(8)), {'foo': 'bar'}))
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-12-23-12-32-45.bpo-46161.EljBmu.rst b/Misc/NEWS.d/next/Core and Builtins/2021-12-23-12-32-45.bpo-46161.EljBmu.rst
new file mode 100644
index 0000000000000..3eeb358c52080
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-12-23-12-32-45.bpo-46161.EljBmu.rst
@@ -0,0 +1 @@
+Fix the class building error when the arguments are constants and CALL_FUNCTION_EX is used.
\ No newline at end of file
diff --git a/Python/compile.c b/Python/compile.c
index b2702da8707f3..86f888ef8a394 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -4233,7 +4233,7 @@ starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Py_INCREF(val);
PyTuple_SET_ITEM(folded, i, val);
}
- if (tuple) {
+ if (tuple && !pushed) {
ADDOP_LOAD_CONST_NEW(c, folded);
} else {
if (add == SET_ADD) {
@@ -4245,6 +4245,9 @@ starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
ADDOP_I(c, build, pushed);
ADDOP_LOAD_CONST_NEW(c, folded);
ADDOP_I(c, extend, 1);
+ if (tuple) {
+ ADDOP(c, LIST_TO_TUPLE);
+ }
}
return 1;
}

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins