Patchwork [1,of,2] py3: convert revlog stats to a dict of (bytes, int) pairs

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 24, 2018, 12:50 p.m.
Message ID <491701afb3ec768f82d7.1540385459@mimosa>
Download mbox | patch
Permalink /patch/36264/
State New
Headers show

Comments

Yuya Nishihara - Oct. 24, 2018, 12:50 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1540384083 -32400
#      Wed Oct 24 21:28:03 2018 +0900
# Node ID 491701afb3ec768f82d7985410fcb259a3a4375e
# Parent  005bc856e9192b2dad6b9d382af9fbd5ce0b6a9a
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.

Patch

diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -348,6 +348,7 @@  static PyObject *index_append(indexObjec
 static PyObject *index_stats(indexObject *self)
 {
 	PyObject *obj = PyDict_New();
+	PyObject *s = NULL;
 	PyObject *t = NULL;
 
 	if (obj == NULL)
@@ -355,22 +356,26 @@  static PyObject *index_stats(indexObject
 
 #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 @@  static PyObject *index_stats(indexObject
 
 bail:
 	Py_XDECREF(obj);
+	Py_XDECREF(s);
 	Py_XDECREF(t);
 	return NULL;
 }