Patchwork [03,of,11] py3: bulk-replace bytes format specifier passed to Py_BuildValue()

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

Comments

Yuya Nishihara - March 3, 2018, 1:27 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1520075302 18000
#      Sat Mar 03 06:08:22 2018 -0500
# Node ID cab34bb9893c9d275cb5a79cfeee931bda7ada88
# Parent  42b8b2c1eb3b5c70e50e68c68e0c8dc9776f14de
py3: bulk-replace bytes format specifier passed to Py_BuildValue()

On Python 3, "s" means a utf-8 string. We have to use "y" for bytes, sigh.

https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue

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

Patch

diff --git a/mercurial/cext/manifest.c b/mercurial/cext/manifest.c
--- a/mercurial/cext/manifest.c
+++ b/mercurial/cext/manifest.c
@@ -718,7 +718,8 @@  static lazymanifest *lazymanifest_filter
 	Py_INCREF(self->pydata);
 	for (i = 0; i < self->numlines; i++) {
 		PyObject *arglist = NULL, *result = NULL;
-		arglist = Py_BuildValue("(s)", self->lines[i].start);
+		arglist = Py_BuildValue(PY23("(s)", "(y)"),
+					self->lines[i].start);
 		if (!arglist) {
 			return NULL;
 		}
diff --git a/mercurial/cext/osutil.c b/mercurial/cext/osutil.c
--- a/mercurial/cext/osutil.c
+++ b/mercurial/cext/osutil.c
@@ -184,7 +184,7 @@  static PyObject *make_item(const WIN32_F
 		? _S_IFDIR : _S_IFREG;
 
 	if (!wantstat)
-		return Py_BuildValue("si", fd->cFileName, kind);
+		return Py_BuildValue(PY23("si", "yi"), fd->cFileName, kind);
 
 	py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
 	if (!py_st)
@@ -202,7 +202,7 @@  static PyObject *make_item(const WIN32_F
 	if (kind == _S_IFREG)
 		stp->st_size = ((__int64)fd->nFileSizeHigh << 32)
 				+ fd->nFileSizeLow;
-	return Py_BuildValue("siN", fd->cFileName,
+	return Py_BuildValue(PY23("siN", "yiN"), fd->cFileName,
 		kind, py_st);
 }
 
@@ -390,9 +390,11 @@  static PyObject *_listdir_stat(char *pat
 			stat = makestat(&st);
 			if (!stat)
 				goto error;
-			elem = Py_BuildValue("siN", ent->d_name, kind, stat);
+			elem = Py_BuildValue(PY23("siN", "yiN"), ent->d_name,
+					     kind, stat);
 		} else
-			elem = Py_BuildValue("si", ent->d_name, kind);
+			elem = Py_BuildValue(PY23("si", "yi"), ent->d_name,
+					     kind);
 		if (!elem)
 			goto error;
 		stat = NULL;
@@ -570,9 +572,11 @@  static PyObject *_listdir_batch(char *pa
 				stat = makestat(&st);
 				if (!stat)
 					goto error;
-				elem = Py_BuildValue("siN", filename, kind, stat);
+				elem = Py_BuildValue(PY23("siN", "yiN"),
+						     filename, kind, stat);
 			} else
-				elem = Py_BuildValue("si", filename, kind);
+				elem = Py_BuildValue(PY23("si", "yi"),
+						     filename, kind);
 			if (!elem)
 				goto error;
 			stat = NULL;
@@ -1108,7 +1112,7 @@  static PyObject *getfstype(PyObject *sel
 	r = statfs(path, &buf);
 	if (r != 0)
 		return PyErr_SetFromErrno(PyExc_OSError);
-	return Py_BuildValue("s", describefstype(&buf));
+	return Py_BuildValue(PY23("s", "y"), describefstype(&buf));
 }
 #endif /* defined(HAVE_LINUX_STATFS) || defined(HAVE_BSD_STATFS) */
 
@@ -1126,7 +1130,7 @@  static PyObject *getfsmountpoint(PyObjec
 	r = statfs(path, &buf);
 	if (r != 0)
 		return PyErr_SetFromErrno(PyExc_OSError);
-	return Py_BuildValue("s", buf.f_mntonname);
+	return Py_BuildValue(PY23("s", "y"), buf.f_mntonname);
 }
 #endif /* defined(HAVE_BSD_STATFS) */
 
diff --git a/mercurial/cext/parsers.c b/mercurial/cext/parsers.c
--- a/mercurial/cext/parsers.c
+++ b/mercurial/cext/parsers.c
@@ -254,7 +254,7 @@  static PyObject *parse_dirstate(PyObject
 		goto quit;
 	}
 
-	parents = Py_BuildValue("s#s#", str, 20, str + 20, 20);
+	parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, 20, str + 20, 20);
 	if (!parents)
 		goto quit;
 
diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -87,9 +87,9 @@  static const char nullid[20];
 static Py_ssize_t inline_scan(indexObject *self, const char **offsets);
 
 #if LONG_MAX == 0x7fffffffL
-static char *tuple_format = "Kiiiiiis#";
+static char *tuple_format = PY23("Kiiiiiis#", "Kiiiiiiy#");
 #else
-static char *tuple_format = "kiiiiiis#";
+static char *tuple_format = PY23("kiiiiiis#", "kiiiiiiy#");
 #endif
 
 /* A RevlogNG v1 index entry is 64 bytes long. */
@@ -2077,7 +2077,7 @@  void revlog_module_init(PyObject *mod)
 	Py_INCREF(&indexType);
 	PyModule_AddObject(mod, "index", (PyObject *)&indexType);
 
-	nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
+	nullentry = Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0, 0,
 				  -1, -1, -1, -1, nullid, 20);
 	if (nullentry)
 		PyObject_GC_UnTrack(nullentry);