comparison mercurial/pathencode.c @ 30102:a8c948ee3668

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.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 08 Oct 2016 22:21:22 +0200
parents e60de7fcad29
children f5607b6253da
comparison
equal deleted inserted replaced
30101:b6f78a72c4a4 30102:a8c948ee3668
169 } 169 }
170 170
171 newobj = PyBytes_FromStringAndSize(NULL, newlen); 171 newobj = PyBytes_FromStringAndSize(NULL, newlen);
172 172
173 if (newobj) { 173 if (newobj) {
174 PyBytes_GET_SIZE(newobj)--; 174 assert(PyBytes_Check(newobj));
175 Py_SIZE(newobj)--;
175 _encodedir(PyBytes_AS_STRING(newobj), newlen, path, 176 _encodedir(PyBytes_AS_STRING(newobj), newlen, path,
176 len + 1); 177 len + 1);
177 } 178 }
178 179
179 return newobj; 180 return newobj;
636 637
637 if (lastdot >= 0) 638 if (lastdot >= 0)
638 memcopy(dest, &destlen, destsize, &src[lastdot], 639 memcopy(dest, &destlen, destsize, &src[lastdot],
639 len - lastdot - 1); 640 len - lastdot - 1);
640 641
641 PyBytes_GET_SIZE(ret) = destlen; 642 PyBytes_Check(ret);
643 Py_SIZE(ret) = destlen;
642 644
643 return ret; 645 return ret;
644 } 646 }
645 647
646 /* 648 /*
748 } 750 }
749 751
750 newobj = PyBytes_FromStringAndSize(NULL, newlen); 752 newobj = PyBytes_FromStringAndSize(NULL, newlen);
751 753
752 if (newobj) { 754 if (newobj) {
753 PyBytes_GET_SIZE(newobj)--; 755 PyBytes_Check(newobj);
756 Py_SIZE(newobj)--;
754 basicencode(PyBytes_AS_STRING(newobj), newlen, path, 757 basicencode(PyBytes_AS_STRING(newobj), newlen, path,
755 len + 1); 758 len + 1);
756 } 759 }
757 } 760 }
758 else 761 else