Patchwork [1,of,7] manifest: convert PyString* to PyBytes*

login
register
mail settings
Submitter Gregory Szorc
Date Oct. 8, 2016, 8:48 p.m.
Message ID <d9914b172e706be9b208.1475959687@gps-mbp.local>
Download mbox | patch
Permalink /patch/16960/
State Accepted
Headers show

Comments

Gregory Szorc - Oct. 8, 2016, 8:48 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1475956675 -7200
#      Sat Oct 08 21:57:55 2016 +0200
# Node ID d9914b172e706be9b208e84977160cdc3b3d5266
# Parent  d15d8ac73cfd2d1ddbd443262ccad9c68ee69406
manifest: convert PyString* to PyBytes*

Python 2.6 introduced PyBytesObject and PyBytes* as aliases for
PyStringObject and PyString*. So on Python 2.6+, PyBytes* and PyString*
are identical and this patch should be a no-op.

On Python 3, PyStringObject is effectively renamed to PyUnicodeObject
and PyBytesObject becomes the main type for byte strings.

This patch begins the process of mass converting PyString* to PyBytes*
so the C extensions use the correct type on Python 3.

Patch

diff --git a/mercurial/manifest.c b/mercurial/manifest.c
--- a/mercurial/manifest.c
+++ b/mercurial/manifest.c
@@ -55,12 +55,12 @@  static PyObject *nodeof(line *l) {
 		return NULL;
 	}
 	if (l->hash_suffix != '\0') {
 		char newhash[21];
-		memcpy(newhash, PyString_AsString(hash), 20);
+		memcpy(newhash, PyBytes_AsString(hash), 20);
 		Py_DECREF(hash);
 		newhash[20] = l->hash_suffix;
-		hash = PyString_FromStringAndSize(newhash, 21);
+		hash = PyBytes_FromStringAndSize(newhash, 21);
 	}
 	return hash;
 }
 
@@ -78,9 +78,9 @@  static PyObject *hashflags(line *l)
 	PyObject *tup;
 
 	if (!hash)
 		return NULL;
-	flags = PyString_FromStringAndSize(s + hplen - 1, flen);
+	flags = PyBytes_FromStringAndSize(s + hplen - 1, flen);
 	if (!flags) {
 		Py_DECREF(hash);
 		return NULL;
 	}
@@ -143,9 +143,9 @@  static int lazymanifest_init(lazymanifes
 	PyObject *pydata;
 	if (!PyArg_ParseTuple(args, "S", &pydata)) {
 		return -1;
 	}
-	err = PyString_AsStringAndSize(pydata, &data, &len);
+	err = PyBytes_AsStringAndSize(pydata, &data, &len);
 
 	self->dirty = false;
 	if (err == -1)
 		return -1;
@@ -237,12 +237,12 @@  static PyObject *lmiter_iterentriesnext(
 	if (!l) {
 		goto done;
 	}
 	pl = pathlen(l);
-	path = PyString_FromStringAndSize(l->start, pl);
+	path = PyBytes_FromStringAndSize(l->start, pl);
 	hash = nodeof(l);
 	consumed = pl + 41;
-	flags = PyString_FromStringAndSize(l->start + consumed,
+	flags = PyBytes_FromStringAndSize(l->start + consumed,
 					   l->len - consumed - 1);
 	if (!path || !hash || !flags) {
 		goto done;
 	}
@@ -299,9 +299,9 @@  static PyObject *lmiter_iterkeysnext(PyO
 	if (!l) {
 		return NULL;
 	}
 	pl = pathlen(l);
-	return PyString_FromStringAndSize(l->start, pl);
+	return PyBytes_FromStringAndSize(l->start, pl);
 }
 
 #ifdef IS_PY3K
 #define LAZYMANIFESTKEYSITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT
@@ -397,14 +397,14 @@  static int linecmp(const void *left, con
 static PyObject *lazymanifest_getitem(lazymanifest *self, PyObject *key)
 {
 	line needle;
 	line *hit;
-	if (!PyString_Check(key)) {
+	if (!PyBytes_Check(key)) {
 		PyErr_Format(PyExc_TypeError,
 			     "getitem: manifest keys must be a string.");
 		return NULL;
 	}
-	needle.start = PyString_AsString(key);
+	needle.start = PyBytes_AsString(key);
 	hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
 		      &linecmp);
 	if (!hit || hit->deleted) {
 		PyErr_Format(PyExc_KeyError, "No such manifest entry.");
@@ -416,14 +416,14 @@  static PyObject *lazymanifest_getitem(la
 static int lazymanifest_delitem(lazymanifest *self, PyObject *key)
 {
 	line needle;
 	line *hit;
-	if (!PyString_Check(key)) {
+	if (!PyBytes_Check(key)) {
 		PyErr_Format(PyExc_TypeError,
 			     "delitem: manifest keys must be a string.");
 		return -1;
 	}
-	needle.start = PyString_AsString(key);
+	needle.start = PyBytes_AsString(key);
 	hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
 		      &linecmp);
 	if (!hit || hit->deleted) {
 		PyErr_Format(PyExc_KeyError,
@@ -485,9 +485,9 @@  static int lazymanifest_setitem(
 	size_t dlen;
 	char *dest;
 	int i;
 	line new;
-	if (!PyString_Check(key)) {
+	if (!PyBytes_Check(key)) {
 		PyErr_Format(PyExc_TypeError,
 			     "setitem: manifest keys must be a string.");
 		return -1;
 	}
@@ -498,19 +498,19 @@  static int lazymanifest_setitem(
 		PyErr_Format(PyExc_TypeError,
 			     "Manifest values must be a tuple of (node, flags).");
 		return -1;
 	}
-	if (PyString_AsStringAndSize(key, &path, &plen) == -1) {
+	if (PyBytes_AsStringAndSize(key, &path, &plen) == -1) {
 		return -1;
 	}
 
 	pyhash = PyTuple_GetItem(value, 0);
-	if (!PyString_Check(pyhash)) {
+	if (!PyBytes_Check(pyhash)) {
 		PyErr_Format(PyExc_TypeError,
 			     "node must be a 20-byte string");
 		return -1;
 	}
-	hlen = PyString_Size(pyhash);
+	hlen = PyBytes_Size(pyhash);
 	/* Some parts of the codebase try and set 21 or 22
 	 * byte "hash" values in order to perturb things for
 	 * status. We have to preserve at least the 21st
 	 * byte. Sigh. If there's a 22nd byte, we drop it on
@@ -520,17 +520,17 @@  static int lazymanifest_setitem(
 		PyErr_Format(PyExc_TypeError,
 			     "node must be a 20-byte string");
 		return -1;
 	}
-	hash = PyString_AsString(pyhash);
+	hash = PyBytes_AsString(pyhash);
 
 	pyflags = PyTuple_GetItem(value, 1);
-	if (!PyString_Check(pyflags) || PyString_Size(pyflags) > 1) {
+	if (!PyBytes_Check(pyflags) || PyBytes_Size(pyflags) > 1) {
 		PyErr_Format(PyExc_TypeError,
 			     "flags must a 0 or 1 byte string");
 		return -1;
 	}
-	if (PyString_AsStringAndSize(pyflags, &flags, &flen) == -1) {
+	if (PyBytes_AsStringAndSize(pyflags, &flags, &flen) == -1) {
 		return -1;
 	}
 	/* one null byte and one newline */
 	dlen = plen + 41 + flen + 1;
@@ -573,14 +573,14 @@  static PyMappingMethods lazymanifest_map
 static int lazymanifest_contains(lazymanifest *self, PyObject *key)
 {
 	line needle;
 	line *hit;
-	if (!PyString_Check(key)) {
+	if (!PyBytes_Check(key)) {
 		/* Our keys are always strings, so if the contains
 		 * check is for a non-string, just return false. */
 		return 0;
 	}
-	needle.start = PyString_AsString(key);
+	needle.start = PyBytes_AsString(key);
 	hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
 		      &linecmp);
 	if (!hit || hit->deleted) {
 		return 0;
@@ -618,12 +618,12 @@  static int compact(lazymanifest *self) {
 		if (!self->lines[i].deleted) {
 			need += self->lines[i].len;
 		}
 	}
-	pydata = PyString_FromStringAndSize(NULL, need);
+	pydata = PyBytes_FromStringAndSize(NULL, need);
 	if (!pydata)
 		return -1;
-	data = PyString_AsString(pydata);
+	data = PyBytes_AsString(pydata);
 	if (!data) {
 		return -1;
 	}
 	src = self->lines;
@@ -756,9 +756,9 @@  static PyObject *lazymanifest_diff(lazym
 	if (!PyArg_ParseTuple(args, "O!|O", &lazymanifestType, &other, &pyclean)) {
 		return NULL;
 	}
 	listclean = (!pyclean) ? false : PyObject_IsTrue(pyclean);
-	es = PyString_FromString("");
+	es = PyBytes_FromString("");
 	if (!es) {
 		goto nomem;
 	}
 	emptyTup = PyTuple_Pack(2, Py_None, es);
@@ -796,10 +796,10 @@  static PyObject *lazymanifest_diff(lazym
 		} else {
 			result = linecmp(left, right);
 		}
 		key = result <= 0 ?
-			PyString_FromString(left->start) :
-			PyString_FromString(right->start);
+			PyBytes_FromString(left->start) :
+			PyBytes_FromString(right->start);
 		if (!key)
 			goto nomem;
 		if (result < 0) {
 			PyObject *l = hashflags(left);