[PATCH 4 of 7] parsers: convert PyString* to PyBytes*

Gregory Szorc gregory.szorc at gmail.com
Sat Oct 8 16:48:10 EDT 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1475956949 -7200
#      Sat Oct 08 22:02:29 2016 +0200
# Node ID a91e68ef608ec814bd853f31920fe1f75993dd40
# Parent  5ce286c00dcefb7c432265417eb3d5d1fe368296
parsers: convert PyString* to PyBytes*

With this change, we no longer have any occurrences of "PyString" in
our C extensions.

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -609,39 +609,39 @@ static PyObject *pack_dirstate(PyObject 
 
 	/* Figure out how much we need to allocate. */
 	for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
 		PyObject *c;
-		if (!PyString_Check(k)) {
+		if (!PyBytes_Check(k)) {
 			PyErr_SetString(PyExc_TypeError, "expected string key");
 			goto bail;
 		}
-		nbytes += PyString_GET_SIZE(k) + 17;
+		nbytes += PyBytes_GET_SIZE(k) + 17;
 		c = PyDict_GetItem(copymap, k);
 		if (c) {
-			if (!PyString_Check(c)) {
+			if (!PyBytes_Check(c)) {
 				PyErr_SetString(PyExc_TypeError,
 						"expected string key");
 				goto bail;
 			}
-			nbytes += PyString_GET_SIZE(c) + 1;
+			nbytes += PyBytes_GET_SIZE(c) + 1;
 		}
 	}
 
-	packobj = PyString_FromStringAndSize(NULL, nbytes);
+	packobj = PyBytes_FromStringAndSize(NULL, nbytes);
 	if (packobj == NULL)
 		goto bail;
 
-	p = PyString_AS_STRING(packobj);
+	p = PyBytes_AS_STRING(packobj);
 
 	pn = PySequence_ITEM(pl, 0);
-	if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
+	if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
 		PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
 		goto bail;
 	}
 	memcpy(p, s, l);
 	p += 20;
 	pn = PySequence_ITEM(pl, 1);
-	if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
+	if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
 		PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
 		goto bail;
 	}
 	memcpy(p, s, l);
@@ -684,23 +684,23 @@ static PyObject *pack_dirstate(PyObject 
 		putbe32((uint32_t)size, p + 4);
 		putbe32((uint32_t)mtime, p + 8);
 		t = p + 12;
 		p += 16;
-		len = PyString_GET_SIZE(k);
-		memcpy(p, PyString_AS_STRING(k), len);
+		len = PyBytes_GET_SIZE(k);
+		memcpy(p, PyBytes_AS_STRING(k), len);
 		p += len;
 		o = PyDict_GetItem(copymap, k);
 		if (o) {
 			*p++ = '\0';
-			l = PyString_GET_SIZE(o);
-			memcpy(p, PyString_AS_STRING(o), l);
+			l = PyBytes_GET_SIZE(o);
+			memcpy(p, PyBytes_AS_STRING(o), l);
 			p += l;
 			len += l + 1;
 		}
 		putbe32((uint32_t)len, t);
 	}
 
-	pos = p - PyString_AS_STRING(packobj);
+	pos = p - PyBytes_AS_STRING(packobj);
 	if (pos != nbytes) {
 		PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
                              (long)pos, (long)nbytes);
 		goto bail;
@@ -795,9 +795,9 @@ static const char *index_deref(indexObje
 		}
 		return self->offsets[pos];
 	}
 
-	return PyString_AS_STRING(self->data) + pos * v1_hdrsize;
+	return PyBytes_AS_STRING(self->data) + pos * v1_hdrsize;
 }
 
 static inline int index_get_parents(indexObject *self, Py_ssize_t rev,
 				    int *ps, int maxrev)
@@ -925,9 +925,9 @@ static const char *index_node(indexObjec
 	if (pos >= self->length - 1) {
 		PyObject *tuple, *str;
 		tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
 		str = PyTuple_GetItem(tuple, 7);
-		return str ? PyString_AS_STRING(str) : NULL;
+		return str ? PyBytes_AS_STRING(str) : NULL;
 	}
 
 	data = index_deref(self, pos);
 	return data ? data + 32 : NULL;
@@ -936,9 +936,9 @@ static const char *index_node(indexObjec
 static int nt_insert(indexObject *self, const char *node, int rev);
 
 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
 {
-	if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
+	if (PyBytes_AsStringAndSize(obj, node, nodelen) == -1)
 		return -1;
 	if (*nodelen == 20)
 		return 0;
 	PyErr_SetString(PyExc_ValueError, "20-byte hash required");
@@ -1824,18 +1824,18 @@ static PyObject *index_partialmatch(inde
 		return NULL;
 	case -2:
 		Py_RETURN_NONE;
 	case -1:
-		return PyString_FromStringAndSize(nullid, 20);
+		return PyBytes_FromStringAndSize(nullid, 20);
 	}
 
 	fullnode = index_node(self, rev);
 	if (fullnode == NULL) {
 		PyErr_Format(PyExc_IndexError,
 			     "could not access rev %d", rev);
 		return NULL;
 	}
-	return PyString_FromStringAndSize(fullnode, 20);
+	return PyBytes_FromStringAndSize(fullnode, 20);
 }
 
 static PyObject *index_m_get(indexObject *self, PyObject *args)
 {
@@ -2246,9 +2246,9 @@ static void nt_invalidate_added(indexObj
 	for (i = start; i < len; i++) {
 		PyObject *tuple = PyList_GET_ITEM(self->added, i);
 		PyObject *node = PyTuple_GET_ITEM(tuple, 7);
 
-		nt_insert(self, PyString_AS_STRING(node), -1);
+		nt_insert(self, PyBytes_AS_STRING(node), -1);
 	}
 
 	if (start == 0)
 		Py_CLEAR(self->added);
@@ -2371,11 +2371,11 @@ static int index_assign_subscript(indexO
  * the optional "offsets" table with those entries.
  */
 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
 {
-	const char *data = PyString_AS_STRING(self->data);
+	const char *data = PyBytes_AS_STRING(self->data);
 	Py_ssize_t pos = 0;
-	Py_ssize_t end = PyString_GET_SIZE(self->data);
+	Py_ssize_t end = PyBytes_GET_SIZE(self->data);
 	long incr = v1_hdrsize;
 	Py_ssize_t len = 0;
 
 	while (pos + v1_hdrsize <= end && pos >= 0) {
@@ -2415,13 +2415,13 @@ static int index_init(indexObject *self,
 	self->offsets = NULL;
 
 	if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
 		return -1;
-	if (!PyString_Check(data_obj)) {
+	if (!PyBytes_Check(data_obj)) {
 		PyErr_SetString(PyExc_TypeError, "data is not a string");
 		return -1;
 	}
-	size = PyString_GET_SIZE(data_obj);
+	size = PyBytes_GET_SIZE(data_obj);
 
 	self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
 	self->data = data_obj;
 
@@ -2612,9 +2612,9 @@ static PyObject *readshas(
 	if (list == NULL) {
 		return NULL;
 	}
 	for (i = 0; i < num; i++) {
-		PyObject *hash = PyString_FromStringAndSize(source, hashwidth);
+		PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth);
 		if (hash == NULL) {
 			Py_DECREF(list);
 			return NULL;
 		}
@@ -2668,9 +2668,9 @@ static PyObject *fm1readmarker(const cha
 
 	if (data + hashwidth > dataend) {
 		goto overflow;
 	}
-	prec = PyString_FromStringAndSize(data, hashwidth);
+	prec = PyBytes_FromStringAndSize(data, hashwidth);
 	data += hashwidth;
 	if (prec == NULL) {
 		goto bail;
 	}
@@ -2711,11 +2711,11 @@ static PyObject *fm1readmarker(const cha
 		Py_ssize_t rightsize = (unsigned char)(*data++);
 		if (meta + leftsize + rightsize > dataend) {
 			goto overflow;
 		}
-		left = PyString_FromStringAndSize(meta, leftsize);
+		left = PyBytes_FromStringAndSize(meta, leftsize);
 		meta += leftsize;
-		right = PyString_FromStringAndSize(meta, rightsize);
+		right = PyBytes_FromStringAndSize(meta, rightsize);
 		meta += rightsize;
 		tmp = PyTuple_New(2);
 		if (!left || !right || !tmp) {
 			Py_XDECREF(left);


More information about the Mercurial-devel mailing list