Skip to content

Commit d700cad

Browse files
Minimize refcount changes.
1 parent 82ee6d9 commit d700cad

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Python/getargs.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
466466
const char *format = *p_format;
467467
int i;
468468
Py_ssize_t len;
469-
int mustbetuple = PyTuple_Check(arg);
469+
int istuple = PyTuple_Check(arg);
470+
int mustbetuple = istuple;
470471

471472
for (;;) {
472473
int c = *format++;
@@ -514,8 +515,8 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
514515
}
515516
}
516517

517-
if (PyTuple_Check(arg)) {
518-
Py_INCREF(arg);
518+
if (istuple) {
519+
/* fallthrough */
519520
}
520521
else if (!PySequence_Check(arg) ||
521522
PyUnicode_Check(arg) || PyBytes_Check(arg) || PyByteArray_Check(arg))
@@ -555,7 +556,9 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
555556
PyOS_snprintf(msgbuf, bufsize,
556557
"must be tuple of length %d, not %zd",
557558
n, len);
558-
Py_DECREF(arg);
559+
if (!istuple) {
560+
Py_DECREF(arg);
561+
}
559562
return msgbuf;
560563
}
561564

@@ -567,13 +570,17 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
567570
msgbuf, bufsize, freelist);
568571
if (msg != NULL) {
569572
levels[0] = i+1;
570-
Py_DECREF(arg);
573+
if (!istuple) {
574+
Py_DECREF(arg);
575+
}
571576
return msg;
572577
}
573578
}
574579

575580
*p_format = format;
576-
Py_DECREF(arg);
581+
if (!istuple) {
582+
Py_DECREF(arg);
583+
}
577584
return NULL;
578585
}
579586

0 commit comments

Comments
 (0)