[Python-checkins] python/dist/src/Python modsupport.c,2.70,2.71

mwh at users.sourceforge.net mwh at users.sourceforge.net
Wed Jul 14 13:28:08 CEST 2004


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9485
Modified Files:
	modsupport.c 
Log Message:
This is Pete Shinners' patch from his bug report
[ 984722 ] Py_BuildValue loses reference counts on error
I'm ever-so-slightly uneasy at the amount of work this can do with an
exception pending, but I don't think that this can result in anything
more serious than a strange error message.
Index: modsupport.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/modsupport.c,v
retrieving revision 2.70
retrieving revision 2.71
diff -C2 -d -r2.70 -r2.71
*** modsupport.c	19 Nov 2003 15:24:47 -0000	2.70
--- modsupport.c	14 Jul 2004 11:28:06 -0000	2.71
***************
*** 153,160 ****
--- 153,163 ----
 	PyObject *d;
 	int i;
+ 	int itemfailed = 0;
 	if (n < 0)
 		return NULL;
 	if ((d = PyDict_New()) == NULL)
 		return NULL;
+ 	/* Note that we can't bail immediately on error as this will leak
+ 	 refcounts on any 'N' arguments. */
 	for (i = 0; i < n; i+= 2) {
 		PyObject *k, *v;
***************
*** 162,178 ****
 		k = do_mkvalue(p_format, p_va);
 		if (k == NULL) {
! 			Py_DECREF(d);
! 			return NULL;
 		}
 		v = do_mkvalue(p_format, p_va);
 		if (v == NULL) {
! 			Py_DECREF(k);
! 			Py_DECREF(d);
! 			return NULL;
 		}
 		err = PyDict_SetItem(d, k, v);
 		Py_DECREF(k);
 		Py_DECREF(v);
! 		if (err < 0) {
 			Py_DECREF(d);
 			return NULL;
--- 165,182 ----
 		k = do_mkvalue(p_format, p_va);
 		if (k == NULL) {
! 			itemfailed = 1;
! 			Py_INCREF(Py_None);
! 			k = Py_None;
 		}
 		v = do_mkvalue(p_format, p_va);
 		if (v == NULL) {
! 			itemfailed = 1;
! 			Py_INCREF(Py_None);
! 			v = Py_None;
 		}
 		err = PyDict_SetItem(d, k, v);
 		Py_DECREF(k);
 		Py_DECREF(v);
! 		if (err < 0 || itemfailed) {
 			Py_DECREF(d);
 			return NULL;
***************
*** 195,207 ****
 	PyObject *v;
 	int i;
 	if (n < 0)
 		return NULL;
 	if ((v = PyList_New(n)) == NULL)
 		return NULL;
 	for (i = 0; i < n; i++) {
 		PyObject *w = do_mkvalue(p_format, p_va);
 		if (w == NULL) {
! 			Py_DECREF(v);
! 			return NULL;
 		}
 		PyList_SetItem(v, i, w);
--- 199,215 ----
 	PyObject *v;
 	int i;
+ 	int itemfailed = 0;
 	if (n < 0)
 		return NULL;
 	if ((v = PyList_New(n)) == NULL)
 		return NULL;
+ 	/* Note that we can't bail immediately on error as this will leak
+ 	 refcounts on any 'N' arguments. */
 	for (i = 0; i < n; i++) {
 		PyObject *w = do_mkvalue(p_format, p_va);
 		if (w == NULL) {
! 			itemfailed = 1;
! 			Py_INCREF(Py_None);
! 			w = Py_None;
 		}
 		PyList_SetItem(v, i, w);
***************
*** 215,218 ****
--- 223,230 ----
 	else if (endchar)
 		++*p_format;
+ 	if (itemfailed) {
+ 		Py_DECREF(v);
+ 		v = NULL;
+ 	}
 	return v;
 }
***************
*** 234,246 ****
 	PyObject *v;
 	int i;
 	if (n < 0)
 		return NULL;
 	if ((v = PyTuple_New(n)) == NULL)
 		return NULL;
 	for (i = 0; i < n; i++) {
 		PyObject *w = do_mkvalue(p_format, p_va);
 		if (w == NULL) {
! 			Py_DECREF(v);
! 			return NULL;
 		}
 		PyTuple_SetItem(v, i, w);
--- 246,262 ----
 	PyObject *v;
 	int i;
+ 	int itemfailed = 0;
 	if (n < 0)
 		return NULL;
 	if ((v = PyTuple_New(n)) == NULL)
 		return NULL;
+ 	/* Note that we can't bail immediately on error as this will leak
+ 	 refcounts on any 'N' arguments. */
 	for (i = 0; i < n; i++) {
 		PyObject *w = do_mkvalue(p_format, p_va);
 		if (w == NULL) {
! 			itemfailed = 1;
! 			Py_INCREF(Py_None);
! 			w = Py_None;
 		}
 		PyTuple_SetItem(v, i, w);
***************
*** 254,257 ****
--- 270,277 ----
 	else if (endchar)
 		++*p_format;
+ 	if (itemfailed) {
+ 		Py_DECREF(v);
+ 		v = NULL;
+ 	}
 	return v;
 }


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /