Patchwork [04,of,11] py3: bulk-replace 'const char*' format specifier passed to PyArg_ParseTuple*()

login
register
mail settings
Submitter Yuya Nishihara
Date March 3, 2018, 1:27 p.m.
Message ID <fd5992aca7c15c920be1.1520083657@mimosa>
Download mbox | patch
Permalink /patch/28769/
State Accepted
Headers show

Comments

Yuya Nishihara - March 3, 2018, 1:27 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1520075927 18000
#      Sat Mar 03 06:18:47 2018 -0500
# Node ID fd5992aca7c15c920be1b513ee3717c0f5a970a2
# Parent  cab34bb9893c9d275cb5a79cfeee931bda7ada88
py3: bulk-replace 'const char*' format specifier passed to PyArg_ParseTuple*()

Perhaps we need this because 's' accepts a unicode string.

https://docs.python.org/3/c-api/arg.html#strings-and-buffers

Substituted using the following pattern with some manual fixes:
  '\b(PyArg_ParseTuple)\((\s*\w+,\s*)"([^"]+)"'
  '\b(PyArg_ParseTupleAndKeywords)\((\s*\w+,\s*\w+,\s*)"([^"]+)"'

Patch

diff --git a/mercurial/cext/base85.c b/mercurial/cext/base85.c
--- a/mercurial/cext/base85.c
+++ b/mercurial/cext/base85.c
@@ -37,7 +37,7 @@  static PyObject *b85encode(PyObject *sel
 	unsigned int acc, val, ch;
 	int pad = 0;
 
-	if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
+	if (!PyArg_ParseTuple(args, PY23("s#|i", "y#|i"), &text, &len, &pad))
 		return NULL;
 
 	if (pad)
@@ -84,7 +84,7 @@  static PyObject *b85decode(PyObject *sel
 	int c;
 	unsigned int acc;
 
-	if (!PyArg_ParseTuple(args, "s#", &text, &len))
+	if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &text, &len))
 		return NULL;
 
 	olen = len / 5 * 4;
diff --git a/mercurial/cext/bdiff.c b/mercurial/cext/bdiff.c
--- a/mercurial/cext/bdiff.c
+++ b/mercurial/cext/bdiff.c
@@ -70,7 +70,8 @@  static PyObject *bdiff(PyObject *self, P
 
 	l.next = NULL;
 
-	if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
+	if (!PyArg_ParseTuple(args, PY23("s#s#:bdiff", "y#y#:bdiff"), &sa, &la,
+	                      &sb, &lb))
 		return NULL;
 
 	if (la > UINT_MAX || lb > UINT_MAX) {
@@ -196,7 +197,7 @@  static PyObject *splitnewlines(PyObject 
 	Py_ssize_t nelts = 0, size, i, start = 0;
 	PyObject *result = NULL;
 
-	if (!PyArg_ParseTuple(args, "s#", &text, &size)) {
+	if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &text, &size)) {
 		goto abort;
 	}
 	if (!size) {
diff --git a/mercurial/cext/charencode.c b/mercurial/cext/charencode.c
--- a/mercurial/cext/charencode.c
+++ b/mercurial/cext/charencode.c
@@ -132,7 +132,8 @@  PyObject *isasciistr(PyObject *self, PyO
 {
 	const char *buf;
 	Py_ssize_t i, len;
-	if (!PyArg_ParseTuple(args, "s#:isasciistr", &buf, &len))
+	if (!PyArg_ParseTuple(args, PY23("s#:isasciistr", "y#:isasciistr"),
+	                      &buf, &len))
 		return NULL;
 	i = 0;
 	/* char array in PyStringObject should be at least 4-byte aligned */
diff --git a/mercurial/cext/mpatch.c b/mercurial/cext/mpatch.c
--- a/mercurial/cext/mpatch.c
+++ b/mercurial/cext/mpatch.c
@@ -134,7 +134,7 @@  static PyObject *patchedsize(PyObject *s
 	Py_ssize_t patchlen;
 	char *bin;
 
-	if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen))
+	if (!PyArg_ParseTuple(args, PY23("ls#", "ly#"), &orig, &bin, &patchlen))
 		return NULL;
 
 	while (pos >= 0 && pos < patchlen) {
diff --git a/mercurial/cext/osutil.c b/mercurial/cext/osutil.c
--- a/mercurial/cext/osutil.c
+++ b/mercurial/cext/osutil.c
@@ -758,7 +758,7 @@  bail:
 static PyObject *setprocname(PyObject *self, PyObject *args)
 {
 	const char *name = NULL;
-	if (!PyArg_ParseTuple(args, "s", &name))
+	if (!PyArg_ParseTuple(args, PY23("s", "y"), &name))
 		return NULL;
 
 #if defined(SETPROCNAME_USE_SETPROCTITLE)
@@ -1105,7 +1105,7 @@  static PyObject *getfstype(PyObject *sel
 	const char *path = NULL;
 	struct statfs buf;
 	int r;
-	if (!PyArg_ParseTuple(args, "s", &path))
+	if (!PyArg_ParseTuple(args, PY23("s", "y"), &path))
 		return NULL;
 
 	memset(&buf, 0, sizeof(buf));
@@ -1123,7 +1123,7 @@  static PyObject *getfsmountpoint(PyObjec
 	const char *path = NULL;
 	struct statfs buf;
 	int r;
-	if (!PyArg_ParseTuple(args, "s", &path))
+	if (!PyArg_ParseTuple(args, PY23("s", "y"), &path))
 		return NULL;
 
 	memset(&buf, 0, sizeof(buf));
@@ -1164,7 +1164,8 @@  static PyObject *listdir(PyObject *self,
 
 	static char *kwlist[] = {"path", "stat", "skip", NULL};
 
-	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|OO:listdir",
+	if (!PyArg_ParseTupleAndKeywords(args, kwargs, PY23("s#|OO:listdir",
+							    "y#|OO:listdir"),
 			kwlist, &path, &plen, &statobj, &skipobj))
 		return NULL;
 
@@ -1197,7 +1198,9 @@  static PyObject *posixfile(PyObject *sel
 	int plus;
 	FILE *fp;
 
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:posixfile", kwlist,
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, PY23("et|si:posixfile",
+							  "et|yi:posixfile"),
+					 kwlist,
 					 Py_FileSystemDefaultEncoding,
 					 &name, &mode, &bufsize))
 		return NULL;
diff --git a/mercurial/cext/parsers.c b/mercurial/cext/parsers.c
--- a/mercurial/cext/parsers.c
+++ b/mercurial/cext/parsers.c
@@ -48,8 +48,9 @@  static PyObject *parse_manifest(PyObject
 	char *str, *start, *end;
 	int len;
 
-	if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", &PyDict_Type,
-	                      &mfdict, &PyDict_Type, &fdict, &str, &len))
+	if (!PyArg_ParseTuple(
+	        args, PY23("O!O!s#:parse_manifest", "O!O!y#:parse_manifest"),
+	        &PyDict_Type, &mfdict, &PyDict_Type, &fdict, &str, &len))
 		goto quit;
 
 	start = str;
@@ -241,8 +242,9 @@  static PyObject *parse_dirstate(PyObject
 	unsigned int flen, len, pos = 40;
 	int readlen;
 
-	if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", &PyDict_Type,
-	                      &dmap, &PyDict_Type, &cmap, &str, &readlen))
+	if (!PyArg_ParseTuple(
+	        args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"),
+	        &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen))
 		goto quit;
 
 	len = readlen;
@@ -645,7 +647,8 @@  static PyObject *fm1readmarkers(PyObject
 	Py_ssize_t offset, stop;
 	PyObject *markers = NULL;
 
-	if (!PyArg_ParseTuple(args, "s#nn", &data, &datalen, &offset, &stop)) {
+	if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen,
+	                      &offset, &stop)) {
 		return NULL;
 	}
 	dataend = data + datalen;
diff --git a/mercurial/cext/pathencode.c b/mercurial/cext/pathencode.c
--- a/mercurial/cext/pathencode.c
+++ b/mercurial/cext/pathencode.c
@@ -512,7 +512,8 @@  PyObject *lowerencode(PyObject *self, Py
 	Py_ssize_t len, newlen;
 	PyObject *ret;
 
-	if (!PyArg_ParseTuple(args, "s#:lowerencode", &path, &len))
+	if (!PyArg_ParseTuple(args, PY23("s#:lowerencode", "y#:lowerencode"),
+	                      &path, &len))
 		return NULL;
 
 	newlen = _lowerencode(NULL, 0, path, len);
diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -1243,7 +1243,7 @@  static PyObject *index_partialmatch(inde
 	char *node;
 	int rev, i;
 
-	if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
+	if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &node, &nodelen))
 		return NULL;
 
 	if (nodelen < 4) {