Patchwork [v2] parsers: use PyTuple_Pack instead of manual list-filling

login
register
mail settings
Submitter Bryan O'Sullivan
Date Dec. 14, 2015, 7:03 p.m.
Message ID <15c01045d64513f9ff70.1450119781@bryano-mbp.local>
Download mbox | patch
Permalink /patch/12036/
State Superseded
Commit 41127e875758315cb716ef922cee20060f9f5a03
Delegated to: Yuya Nishihara
Headers show

Comments

Bryan O'Sullivan - Dec. 14, 2015, 7:03 p.m.
# HG changeset patch
# User Bryan O'Sullivan <bos@serpentine.com>
# Date 1450119772 28800
#      Mon Dec 14 11:02:52 2015 -0800
# Node ID 15c01045d64513f9ff7063953da8cccf42852cb1
# Parent  13a42f5695ac05f1a684070d5e6516286dc4ce8d
parsers: use PyTuple_Pack instead of manual list-filling

Suggested by Yuya.
Yuya Nishihara - Dec. 15, 2015, 3:37 p.m.
On Mon, 14 Dec 2015 11:03:01 -0800, Bryan O'Sullivan wrote:
> # HG changeset patch
> # User Bryan O'Sullivan <bos@serpentine.com>
> # Date 1450119772 28800
> #      Mon Dec 14 11:02:52 2015 -0800
> # Node ID 15c01045d64513f9ff7063953da8cccf42852cb1
> # Parent  13a42f5695ac05f1a684070d5e6516286dc4ce8d
> parsers: use PyTuple_Pack instead of manual list-filling
> 
> Suggested by Yuya.
> 
> diff --git a/mercurial/parsers.c b/mercurial/parsers.c
> --- a/mercurial/parsers.c
> +++ b/mercurial/parsers.c
> @@ -1351,15 +1351,12 @@ static PyObject *compute_phases_map_sets
>  			goto release;
>  		PyList_SET_ITEM(phaseslist, i, phaseval);
>  	}
> -	ret = PyList_New(2);
> -	if (ret == NULL)
> -		goto release;
> -
> -	PyList_SET_ITEM(ret, 0, phaseslist);
> -	PyList_SET_ITEM(ret, 1, phasessetlist);
> -	/* We don't release phaseslist and phasessetlist as we return them to
> -	 * python */
> -	goto done;
> +	ret = PyTuple_Pack(2, phaseslist, phasessetlist);
> +	if (ret != NULL) {
> +		/* We don't release phaseslist and phasessetlist as we
> +		 * return them to python */
> +		goto done;
> +	}

PyTuple_Pack() increfs arguments, so every exit path can go through "release".
We can remove "done".

Patch

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -1351,15 +1351,12 @@  static PyObject *compute_phases_map_sets
 			goto release;
 		PyList_SET_ITEM(phaseslist, i, phaseval);
 	}
-	ret = PyList_New(2);
-	if (ret == NULL)
-		goto release;
-
-	PyList_SET_ITEM(ret, 0, phaseslist);
-	PyList_SET_ITEM(ret, 1, phasessetlist);
-	/* We don't release phaseslist and phasessetlist as we return them to
-	 * python */
-	goto done;
+	ret = PyTuple_Pack(2, phaseslist, phasessetlist);
+	if (ret != NULL) {
+		/* We don't release phaseslist and phasessetlist as we
+		 * return them to python */
+		goto done;
+	}
 
 release:
 	Py_XDECREF(phaseslist);