From d30b514507260a47714e51fcc00487596bae2c9a Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Fri, 13 Mar 2026 18:42:36 +0500 Subject: [PATCH 1/3] Fill newly created tuple with Py_None instead of NULL --- Objects/tupleobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 01afa53e15cd5d..b443525cf027e5 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -83,7 +83,7 @@ PyTuple_New(Py_ssize_t size) return NULL; } for (Py_ssize_t i = 0; i < size; i++) { - op->ob_item[i] = NULL; + op->ob_item[i] = Py_None; } _PyObject_GC_TRACK(op); return (PyObject *) op; From bb8a69db6827df1450549d2fb43c608b1ea26d41 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Fri, 13 Mar 2026 20:39:13 +0500 Subject: [PATCH 2/3] Fix resize --- Lib/test/test_capi/test_tuple.py | 12 +++++------- Objects/tupleobject.c | 8 ++++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_capi/test_tuple.py b/Lib/test/test_capi/test_tuple.py index 0c27e81168ff77..c29c67b0b57391 100644 --- a/Lib/test/test_capi/test_tuple.py +++ b/Lib/test/test_capi/test_tuple.py @@ -54,7 +54,6 @@ def test_tuple_new(self): # Test PyTuple_New() tuple_new = _testlimitedcapi.tuple_new size = _testlimitedcapi.tuple_size - checknull = _testcapi._check_tuple_item_is_NULL tup1 = tuple_new(0) self.assertEqual(tup1, ()) @@ -66,7 +65,7 @@ def test_tuple_new(self): self.assertIs(type(tup2), tuple) self.assertEqual(size(tup2), 1) self.assertIsNot(tup2, tup1) - self.assertTrue(checknull(tup2, 0)) + self.assertIsNone(tup2[0]) self._tracked(tup2) self.assertRaises(SystemError, tuple_new, -1) @@ -299,7 +298,6 @@ def test_tuple_set_item(self): def test__tuple_resize(self): # Test _PyTuple_Resize() resize = _testcapi._tuple_resize - checknull = _testcapi._check_tuple_item_is_NULL a = () b = resize(a, 0, False) @@ -308,8 +306,8 @@ def test__tuple_resize(self): b = resize(a, 2, False) self.assertEqual(len(a), 0) self.assertEqual(len(b), 2) - self.assertTrue(checknull(b, 0)) - self.assertTrue(checknull(b, 1)) + self.assertIsNone(b[0]) + self.assertIsNone(b[1]) a = ([1], [2], [3]) b = resize(a, 3) @@ -319,8 +317,8 @@ def test__tuple_resize(self): b = resize(a, 5) self.assertEqual(len(b), 5) self.assertEqual(b[:3], a) - self.assertTrue(checknull(b, 3)) - self.assertTrue(checknull(b, 4)) + self.assertIsNone(b[3]) + self.assertIsNone(b[4]) a = () self.assertRaises(MemoryError, resize, a, PY_SSIZE_T_MAX) diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index b443525cf027e5..7c762c9d20f727 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -1050,10 +1050,10 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) return -1; } _Py_NewReferenceNoTotal((PyObject *) sv); - /* Zero out items added by growing */ - if (newsize > oldsize) - memset(&sv->ob_item[oldsize], 0, - sizeof(*sv->ob_item) * (newsize - oldsize)); + /* Set items added by growing to Py_None */ + for(i = oldsize; i < newsize;i++) { + sv->ob_item[i] = Py_None; + } *pv = (PyObject *) sv; _PyObject_GC_TRACK(sv); return 0; From 16de50a4c065976c56caa75aab5f43191e734fcb Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Fri, 13 Mar 2026 20:39:55 +0500 Subject: [PATCH 3/3] Fix asserts --- Python/ceval.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index 950050a6027116..51422752d82935 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -604,7 +604,7 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, } if (match_self) { // Easy. Copy the subject itself, and move on to kwargs. - assert(PyTuple_GET_ITEM(attrs, 0) == NULL); + assert(PyTuple_GET_ITEM(attrs, 0) == Py_None); PyTuple_SET_ITEM(attrs, 0, Py_NewRef(subject)); } else { @@ -621,7 +621,7 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, if (attr == NULL) { goto fail; } - assert(PyTuple_GET_ITEM(attrs, i) == NULL); + assert(PyTuple_GET_ITEM(attrs, i) == Py_None); PyTuple_SET_ITEM(attrs, i, attr); } } @@ -634,7 +634,7 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, if (attr == NULL) { goto fail; } - assert(PyTuple_GET_ITEM(attrs, nargs + i) == NULL); + assert(PyTuple_GET_ITEM(attrs, nargs + i) == Py_None); PyTuple_SET_ITEM(attrs, nargs + i, attr); } Py_XDECREF(seen);