Skip to content

Commit f4acc36

Browse files
Tweak _PyTuple_Resize().
1 parent fa2edad commit f4acc36

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Objects/tupleobject.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -920,14 +920,22 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
920920
PyErr_BadInternalCall();
921921
return -1;
922922
}
923+
923924
oldsize = Py_SIZE(v);
924-
if (oldsize == newsize)
925+
if (oldsize == newsize) {
925926
return 0;
926-
927+
}
928+
if (newsize == 0) {
929+
Py_DECREF(v);
930+
*pv = tuple_get_empty();
931+
return 0;
932+
}
927933
if (oldsize == 0) {
928-
/* Empty tuples are often shared, so we should never
929-
resize them in-place even if we do own the only
930-
(current) reference */
934+
#ifdef Py_DEBUG
935+
assert(v == &_Py_SINGLETON(tuple_empty));
936+
#endif
937+
/* The empty tuple is statically allocated so we never
938+
resize it in-place. */
931939
Py_DECREF(v);
932940
*pv = PyTuple_New(newsize);
933941
return *pv == NULL ? -1 : 0;
@@ -947,10 +955,6 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
947955
for (i = newsize; i < oldsize; i++) {
948956
Py_CLEAR(v->ob_item[i]);
949957
}
950-
if (newsize == 0) {
951-
*pv = tuple_get_empty();
952-
return 0;
953-
}
954958
sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
955959
if (sv == NULL) {
956960
*pv = NULL;

0 commit comments

Comments
 (0)