[pypy-commit] pypy default: PY_SSIZE_T_CLEAN support, second and hopefully final part

arigo noreply at buildbot.pypy.org
Mon Nov 11 10:46:28 CET 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r67949:0611a3ab0561
Date: 2013-11-11 10:45 +0100
http://bitbucket.org/pypy/pypy/changeset/0611a3ab0561/

Log:	PY_SSIZE_T_CLEAN support, second and hopefully final part

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -386,6 +386,9 @@
     'PyString_FromFormat', 'PyString_FromFormatV',
     'PyModule_AddObject', 'PyModule_AddIntConstant', 'PyModule_AddStringConstant',
     'Py_BuildValue', 'Py_VaBuildValue', 'PyTuple_Pack',
+    '_PyArg_Parse_SizeT', '_PyArg_ParseTuple_SizeT',
+    '_PyArg_ParseTupleAndKeywords_SizeT', '_PyArg_VaParse_SizeT',
+    '_PyArg_VaParseTupleAndKeywords_SizeT',
     '_Py_BuildValue_SizeT', '_Py_VaBuildValue_SizeT',
 
     'PyErr_Format', 'PyErr_NewException', 'PyErr_NewExceptionWithDoc',
diff --git a/pypy/module/cpyext/include/eval.h b/pypy/module/cpyext/include/eval.h
--- a/pypy/module/cpyext/include/eval.h
+++ b/pypy/module/cpyext/include/eval.h
@@ -10,8 +10,10 @@
 #include "Python.h"
 
 #ifdef PY_SSIZE_T_CLEAN
-#define PyPyObject_CallFunction _PyPyObject_CallFunction_SizeT
-#define PyPyObject_CallMethod _PyPyObject_CallMethod_SizeT
+#undef PyObject_CallFunction
+#undef PyObject_CallMethod
+#define PyObject_CallFunction _PyObject_CallFunction_SizeT
+#define PyObject_CallMethod _PyObject_CallMethod_SizeT
 #endif
 
 #define PyEval_CallObject(func,arg) \
diff --git a/pypy/module/cpyext/include/modsupport.h b/pypy/module/cpyext/include/modsupport.h
--- a/pypy/module/cpyext/include/modsupport.h
+++ b/pypy/module/cpyext/include/modsupport.h
@@ -10,9 +10,20 @@
 /* If PY_SSIZE_T_CLEAN is defined, each functions treats #-specifier
    to mean Py_ssize_t */
 #ifdef PY_SSIZE_T_CLEAN
-#define PyPy_BuildValue           _PyPy_BuildValue_SizeT
-#define PyPy_VaBuildValue         _PyPy_VaBuildValue_SizeT
-    /*XXX more*/
+#undef PyArg_Parse
+#undef PyArg_ParseTuple
+#undef PyArg_ParseTupleAndKeywords
+#undef PyArg_VaParse
+#undef PyArg_VaParseTupleAndKeywords
+#undef Py_BuildValue
+#undef Py_VaBuildValue
+#define PyArg_Parse         _PyArg_Parse_SizeT
+#define PyArg_ParseTuple        _PyArg_ParseTuple_SizeT
+#define PyArg_ParseTupleAndKeywords _PyArg_ParseTupleAndKeywords_SizeT
+#define PyArg_VaParse           _PyArg_VaParse_SizeT
+#define PyArg_VaParseTupleAndKeywords   _PyArg_VaParseTupleAndKeywords_SizeT
+#define Py_BuildValue           _Py_BuildValue_SizeT
+#define Py_VaBuildValue         _Py_VaBuildValue_SizeT
 #endif
 
 #define PYTHON_API_VERSION 1013
@@ -26,6 +37,15 @@
 				const char *, char **, ...);
 int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
 				const char *, char **, va_list);
+
+int _PyArg_Parse_SizeT(PyObject *, const char *, ...);
+int _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
+int _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
+
+int _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
+				const char *, char **, ...);
+int _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
+				const char *, char **, va_list);
   
 /* to make sure that modules compiled with CPython's or PyPy's Python.h
    are not importable on the other interpreter, use a #define to expect a
diff --git a/pypy/module/cpyext/src/getargs.c b/pypy/module/cpyext/src/getargs.c
--- a/pypy/module/cpyext/src/getargs.c
+++ b/pypy/module/cpyext/src/getargs.c
@@ -53,7 +53,7 @@
 }
 
 int
-_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
+_PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
 {
     int retval;
     va_list va;
@@ -78,7 +78,7 @@
 }
 
 int
-_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
+_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
 {
     int retval;
     va_list va;
@@ -109,7 +109,7 @@
 }
 
 int
-_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
+_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
 {
     va_list lva;
 
diff --git a/pypy/module/cpyext/test/test_getargs.py b/pypy/module/cpyext/test/test_getargs.py
--- a/pypy/module/cpyext/test/test_getargs.py
+++ b/pypy/module/cpyext/test/test_getargs.py
@@ -3,9 +3,11 @@
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 
 class AppTestGetargs(AppTestCpythonExtensionBase):
-    def w_import_parser(self, implementation, argstyle='METH_VARARGS'):
+    def w_import_parser(self, implementation, argstyle='METH_VARARGS',
+                        PY_SSIZE_T_CLEAN=False):
         mod = self.import_extension(
-            'modname', [('funcname', argstyle, implementation)])
+            'modname', [('funcname', argstyle, implementation)],
+            PY_SSIZE_T_CLEAN=PY_SSIZE_T_CLEAN)
         return mod.funcname
 
     def test_pyarg_parse_int(self):
@@ -179,3 +181,34 @@
             ''')
         raises(TypeError, "charbuf(10)")
         assert 'foo\0bar\0baz' == charbuf('foo\0bar\0baz')
+
+    def test_pyarg_parse_without_py_ssize_t(self):
+        import sys
+        charbuf = self.import_parser(
+            '''
+            char *buf;
+            Py_ssize_t y = -1;
+            if (!PyArg_ParseTuple(args, "s#", &buf, &y)) {
+                return NULL;
+            }
+            return PyInt_FromSsize_t(y);
+            ''')
+        if sys.maxsize < 2**32:
+            expected = 5
+        elif sys.byteorder == 'little':
+            expected = -0xfffffffb
+        else:
+            expected = 0x5ffffffff
+        assert charbuf('12345') == expected
+
+    def test_pyarg_parse_with_py_ssize_t(self):
+        charbuf = self.import_parser(
+            '''
+            char *buf;
+            Py_ssize_t y = -1;
+            if (!PyArg_ParseTuple(args, "s#", &buf, &y)) {
+                return NULL;
+            }
+            return PyInt_FromSsize_t(y);
+            ''', PY_SSIZE_T_CLEAN=True)
+        assert charbuf('12345') == 5


More information about the pypy-commit mailing list