# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 2016-10-08 20:21:22
# Node ID a8c948ee3668e8fb3d17a995e220e8e22d1e8c04
# Parent  b6f78a72c4a4e786c6881d02fc9273dd14f29446

pathencode: use Py_SIZE directly

On Python 2, PyBytes_GET_SIZE is the same as PyString_GET_SIZE which
is the same as Py_SIZE which resolves to a struct member.

On Python 3, PyBytes_GET_SIZE is
"(assert(PyBytes_Check(op)),Py_SIZE(op))". The compiler barfs when
assigning to this version.

This patch simply changes PyBytes_GET_SIZE to Py_SIZE. On Python 2,
there is no effective change in behavior. On Python 3, we drop the
PyBytes_Check(). However, in all cases we have explicitly created
a PyBytesObject in the same function, so the PyBytes_Check() is
guaranteed to be true. Despite this, code changes over time, so
I've added added assert() in all callers so we can catch this in
debug builds.

With this patch, all mercurial.* C extensions now compile on Python 3
on my OS X machine. There are several compiler warnings and I'm sure
there are incompatibilities with Python 3, including possibly
segfaults. But it is a milestone.

diff --git a/mercurial/pathencode.c b/mercurial/pathencode.c
--- a/mercurial/pathencode.c
+++ b/mercurial/pathencode.c
@@ -171,7 +171,8 @@ PyObject *encodedir(PyObject *self, PyOb
 	newobj = PyBytes_FromStringAndSize(NULL, newlen);
 
 	if (newobj) {
-		PyBytes_GET_SIZE(newobj)--;
+		assert(PyBytes_Check(newobj));
+		Py_SIZE(newobj)--;
 		_encodedir(PyBytes_AS_STRING(newobj), newlen, path,
 			   len + 1);
 	}
@@ -638,7 +639,8 @@ static PyObject *hashmangle(const char *
 		memcopy(dest, &destlen, destsize, &src[lastdot],
 			len - lastdot - 1);
 
-	PyBytes_GET_SIZE(ret) = destlen;
+	PyBytes_Check(ret);
+	Py_SIZE(ret) = destlen;
 
 	return ret;
 }
@@ -750,7 +752,8 @@ PyObject *pathencode(PyObject *self, PyO
 		newobj = PyBytes_FromStringAndSize(NULL, newlen);
 
 		if (newobj) {
-			PyBytes_GET_SIZE(newobj)--;
+			PyBytes_Check(newobj);
+			Py_SIZE(newobj)--;
 			basicencode(PyBytes_AS_STRING(newobj), newlen, path,
 				    len + 1);
 		}