Patchwork [1,of,4,foldmap-in-C] parsers: factor out most of asciilower into an internal function

login
register
mail settings
Submitter Siddharth Agarwal
Date April 1, 2015, 5:28 p.m.
Message ID <719010b9a74187eb3139.1427909284@devbig136.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8415/
State Superseded
Commit e97a00bf18ae4156ba1590ae08bf204db29ca132
Headers show

Comments

Siddharth Agarwal - April 1, 2015, 5:28 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1427822729 25200
#      Tue Mar 31 10:25:29 2015 -0700
# Node ID 719010b9a74187eb3139b4e57562ecefde288cc4
# Parent  9a023f039ed86ed4694174223b5f6c3965e99723
parsers: factor out most of asciilower into an internal function

We're going to reuse this in upcoming patches.
Siddharth Agarwal - April 1, 2015, 8:53 p.m.
On 04/01/2015 10:28 AM, Siddharth Agarwal wrote:
> -static PyObject *asciilower(PyObject *self, PyObject *args)
> +static inline PyObject *_asciilower(PyObject *str_obj)
>  {
>  	char *str, *newstr;
> -	int i, len;
> +	Py_ssize_t i, len;
>  	PyObject *newobj = NULL;
> +	PyObject *ret = NULL;
>  
> -	if (!PyArg_ParseTuple(args, "s#", &str, &len))
> -		goto quit;
> +	str = PyBytes_AS_STRING(str_obj);
> +	len = PyBytes_GET_SIZE(str_obj);
>  
>  	newobj = PyBytes_FromStringAndSize(NULL, len);
>  	if (!newobj)
> @@ -121,10 +122,19 @@
>  		newstr[i] = lowertable[(unsigned char)c];
>  	}
>  
> -	return newobj;
> +	ret = newobj;
> +	Py_INCREF(ret);
>  quit:
>  	Py_XDECREF(newobj);
> -	return NULL;
> +	return ret;
> +}

Huh, I meant to break this up into multiple patches before sending. I'll
fix up and resend.

Patch

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -93,14 +93,15 @@ 
 	return ret;
 }
 
-static PyObject *asciilower(PyObject *self, PyObject *args)
+static inline PyObject *_asciilower(PyObject *str_obj)
 {
 	char *str, *newstr;
-	int i, len;
+	Py_ssize_t i, len;
 	PyObject *newobj = NULL;
+	PyObject *ret = NULL;
 
-	if (!PyArg_ParseTuple(args, "s#", &str, &len))
-		goto quit;
+	str = PyBytes_AS_STRING(str_obj);
+	len = PyBytes_GET_SIZE(str_obj);
 
 	newobj = PyBytes_FromStringAndSize(NULL, len);
 	if (!newobj)
@@ -121,10 +122,19 @@ 
 		newstr[i] = lowertable[(unsigned char)c];
 	}
 
-	return newobj;
+	ret = newobj;
+	Py_INCREF(ret);
 quit:
 	Py_XDECREF(newobj);
-	return NULL;
+	return ret;
+}
+
+static PyObject *asciilower(PyObject *self, PyObject *args)
+{
+	PyObject *str_obj;
+	if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj))
+		return NULL;
+	return _asciilower(str_obj);
 }
 
 /*