changeset 40509:88702fd208ce

py3: convert revlog stats to a dict of (bytes, int) pairs Py_DECREF(t) is replaced with Py_CLEAR(t) so that t is set to NULL once decrefed. Otherwise, it would be excessively decrefed if a subsequent PyBytes_FromString() failed.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 24 Oct 2018 21:28:03 +0900
parents 909c31805f54
children 24cb8a6d4c20
files mercurial/cext/revlog.c
diffstat 1 files changed, 12 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cext/revlog.c	Thu Oct 25 22:13:22 2018 -0400
+++ b/mercurial/cext/revlog.c	Wed Oct 24 21:28:03 2018 +0900
@@ -348,6 +348,7 @@
 static PyObject *index_stats(indexObject *self)
 {
 	PyObject *obj = PyDict_New();
+	PyObject *s = NULL;
 	PyObject *t = NULL;
 
 	if (obj == NULL)
@@ -355,22 +356,26 @@
 
 #define istat(__n, __d) \
 	do { \
+		s = PyBytes_FromString(__d); \
 		t = PyInt_FromSsize_t(self->__n); \
-		if (!t) \
+		if (!s || !t) \
 			goto bail; \
-		if (PyDict_SetItemString(obj, __d, t) == -1) \
+		if (PyDict_SetItem(obj, s, t) == -1) \
 			goto bail; \
-		Py_DECREF(t); \
+		Py_CLEAR(s); \
+		Py_CLEAR(t); \
 	} while (0)
 
 	if (self->added) {
 		Py_ssize_t len = PyList_GET_SIZE(self->added);
+		s = PyBytes_FromString("index entries added");
 		t = PyInt_FromSsize_t(len);
-		if (!t)
+		if (!s || !t)
 			goto bail;
-		if (PyDict_SetItemString(obj, "index entries added", t) == -1)
+		if (PyDict_SetItem(obj, s, t) == -1)
 			goto bail;
-		Py_DECREF(t);
+		Py_CLEAR(s);
+		Py_CLEAR(t);
 	}
 
 	if (self->raw_length != self->length)
@@ -392,6 +397,7 @@
 
 bail:
 	Py_XDECREF(obj);
+	Py_XDECREF(s);
 	Py_XDECREF(t);
 	return NULL;
 }