Patchwork [2,of,4] dirs: convert PyString to PyBytes

login
register
mail settings
Submitter Gregory Szorc
Date Oct. 8, 2016, 3 p.m.
Message ID <3904cbad91663a7ae74c.1475938846@gps-mbp.local>
Download mbox | patch
Permalink /patch/16926/
State Accepted
Headers show

Comments

Gregory Szorc - Oct. 8, 2016, 3 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1475929919 -7200
#      Sat Oct 08 14:31:59 2016 +0200
# Node ID 3904cbad91663a7ae74c117a21879220b3e851b6
# Parent  33589809a2a64ceeb2828bbfe6e9126d6997bf94
dirs: convert PyString to PyBytes

PyStringObject was renamed to PyBytes in Python 3 along with the
corresponding PyString* functions and macros. PyString* doesn't
exist in Python 3. But PyBytes* is an alias to PyString in Python 2.
So rewrite PyString* to PyBytes* for Python 2/3 dual compatibility.

Patch

diff --git a/mercurial/dirs.c b/mercurial/dirs.c
--- a/mercurial/dirs.c
+++ b/mercurial/dirs.c
@@ -40,10 +40,10 @@  static inline Py_ssize_t _finddir(const 
 }
 
 static int _addpath(PyObject *dirs, PyObject *path)
 {
-	const char *cpath = PyString_AS_STRING(path);
-	Py_ssize_t pos = PyString_GET_SIZE(path);
+	const char *cpath = PyBytes_AS_STRING(path);
+	Py_ssize_t pos = PyBytes_GET_SIZE(path);
 	PyObject *key = NULL;
 	int ret = -1;
 
 	while ((pos = _finddir(cpath, pos - 1)) != -1) {
@@ -52,18 +52,18 @@  static int _addpath(PyObject *dirs, PyOb
 		/* It's likely that every prefix already has an entry
 		   in our dict. Try to avoid allocating and
 		   deallocating a string for each prefix we check. */
 		if (key != NULL)
-			((PyStringObject *)key)->ob_shash = -1;
+			((PyBytesObject *)key)->ob_shash = -1;
 		else {
 			/* Force Python to not reuse a small shared string. */
-			key = PyString_FromStringAndSize(cpath,
+			key = PyBytes_FromStringAndSize(cpath,
 							 pos < 2 ? 2 : pos);
 			if (key == NULL)
 				goto bail;
 		}
 		Py_SIZE(key) = pos;
-		((PyStringObject *)key)->ob_sval[pos] = '\0';
+		((PyBytesObject *)key)->ob_sval[pos] = '\0';
 
 		val = PyDict_GetItem(dirs, key);
 		if (val != NULL) {
 			PyInt_AS_LONG(val) += 1;
@@ -92,17 +92,17 @@  bail:
 }
 
 static int _delpath(PyObject *dirs, PyObject *path)
 {
-	char *cpath = PyString_AS_STRING(path);
-	Py_ssize_t pos = PyString_GET_SIZE(path);
+	char *cpath = PyBytes_AS_STRING(path);
+	Py_ssize_t pos = PyBytes_GET_SIZE(path);
 	PyObject *key = NULL;
 	int ret = -1;
 
 	while ((pos = _finddir(cpath, pos - 1)) != -1) {
 		PyObject *val;
 
-		key = PyString_FromStringAndSize(cpath, pos);
+		key = PyBytes_FromStringAndSize(cpath, pos);
 
 		if (key == NULL)
 			goto bail;
 
@@ -133,9 +133,9 @@  static int dirs_fromdict(PyObject *dirs,
 	PyObject *key, *value;
 	Py_ssize_t pos = 0;
 
 	while (PyDict_Next(source, &pos, &key, &value)) {
-		if (!PyString_Check(key)) {
+		if (!PyBytes_Check(key)) {
 			PyErr_SetString(PyExc_TypeError, "expected string key");
 			return -1;
 		}
 		if (skipchar) {
@@ -164,9 +164,9 @@  static int dirs_fromiter(PyObject *dirs,
 	if (iter == NULL)
 		return -1;
 
 	while ((item = PyIter_Next(iter)) != NULL) {
-		if (!PyString_Check(item)) {
+		if (!PyBytes_Check(item)) {
 			PyErr_SetString(PyExc_TypeError, "expected string");
 			break;
 		}
 
@@ -223,9 +223,9 @@  static int dirs_init(dirsObject *self, P
 PyObject *dirs_addpath(dirsObject *self, PyObject *args)
 {
 	PyObject *path;
 
-	if (!PyArg_ParseTuple(args, "O!:addpath", &PyString_Type, &path))
+	if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path))
 		return NULL;
 
 	if (_addpath(self->dict, path) == -1)
 		return NULL;
@@ -236,9 +236,9 @@  PyObject *dirs_addpath(dirsObject *self,
 static PyObject *dirs_delpath(dirsObject *self, PyObject *args)
 {
 	PyObject *path;
 
-	if (!PyArg_ParseTuple(args, "O!:delpath", &PyString_Type, &path))
+	if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path))
 		return NULL;
 
 	if (_delpath(self->dict, path) == -1)
 		return NULL;
@@ -247,9 +247,9 @@  static PyObject *dirs_delpath(dirsObject
 }
 
 static int dirs_contains(dirsObject *self, PyObject *value)
 {
-	return PyString_Check(value) ? PyDict_Contains(self->dict, value) : 0;
+	return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0;
 }
 
 static void dirs_dealloc(dirsObject *self)
 {