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

login
register
mail settings
Submitter Siddharth Agarwal
Date April 1, 2015, 8:59 p.m.
Message ID <d624f49c6f5f4741b8cd.1427921999@devbig136.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8426/
State Accepted
Commit e97a00bf18ae4156ba1590ae08bf204db29ca132
Headers show

Comments

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

We're going to reuse this in upcoming patches.

The change to Py_ssize_t is necessary because parsers.c doesn't define
PY_SSIZE_T_CLEAN. That macro changes the behavior of PyArg_ParseTuple but not
PyInt_AS_LONG.
Siddharth Agarwal - April 1, 2015, 9:03 p.m.
On 04/01/2015 01:59 PM, Siddharth Agarwal wrote:
> The change to Py_ssize_t is necessary because parsers.c doesn't define
> PY_SSIZE_T_CLEAN. That macro changes the behavior of PyArg_ParseTuple but not
> PyInt_AS_LONG.

Ugh, lots of wires crossed here. I meant PyBytes_GET_SIZE. Please fix
inflight if possible.

Patch

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -93,14 +93,14 @@ 
 	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;
 
-	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)
@@ -127,6 +127,14 @@ 
 	return NULL;
 }
 
+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);
+}
+
 /*
  * This code assumes that a manifest is stitched together with newline
  * ('\n') characters.