[pypy-commit] pypy cpyext-refactor-methodobject: add a test for METH_KEYWORDS; the test passes with -A, but is currently broken in pypy

antocuni pypy.commits at gmail.com
Sat Oct 21 19:59:20 EDT 2017


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: cpyext-refactor-methodobject
Changeset: r92818:094b65ff1d96
Date: 2017-10-21 15:46 +0200
http://bitbucket.org/pypy/pypy/changeset/094b65ff1d96/

Log:	add a test for METH_KEYWORDS; the test passes with -A, but is
	currently broken in pypy

diff --git a/pypy/module/cpyext/test/test_methodobject.py b/pypy/module/cpyext/test/test_methodobject.py
--- a/pypy/module/cpyext/test/test_methodobject.py
+++ b/pypy/module/cpyext/test/test_methodobject.py
@@ -92,6 +92,21 @@
         #
         raises(TypeError, mod.getarg_VARARGS, k=1)
 
+    def test_call_METH_KEYWORDS(self):
+        mod = self.import_extension('MyModule', [
+            ('getarg_KW', 'METH_VARARGS | METH_KEYWORDS',
+             '''
+             if (!kwargs) kwargs = Py_None;
+             return Py_BuildValue("OO", args, kwargs);
+             '''
+             ),
+            ])
+        assert mod.getarg_KW(1) == ((1,), None)
+        assert mod.getarg_KW(1, 2) == ((1, 2), None)
+        assert mod.getarg_KW(a=3, b=4) == ((), {'a': 3, 'b': 4})
+        assert mod.getarg_KW(1, 2, a=3, b=4) == ((1, 2), {'a': 3, 'b': 4})
+        assert mod.getarg_KW.__name__ == "getarg_KW"
+
     def test_func_attributes(self):
         mod = self.import_extension('MyModule', [
             ('isCFunction', 'METH_O',
diff --git a/pypy/tool/cpyext/extbuild.py b/pypy/tool/cpyext/extbuild.py
--- a/pypy/tool/cpyext/extbuild.py
+++ b/pypy/tool/cpyext/extbuild.py
@@ -114,14 +114,18 @@
     codes = []
     for funcname, flags, code in functions:
         cfuncname = "%s_%s" % (modname, funcname)
+        if 'METH_KEYWORDS' in flags:
+            signature = '(PyObject *self, PyObject *args, PyObject *kwargs)'
+        else:
+            signature = '(PyObject *self, PyObject *args)'
         methods_table.append(
-            "{\"%s\", %s, %s}," % (funcname, cfuncname, flags))
+            "{\"%s\", (PyCFunction)%s, %s}," % (funcname, cfuncname, flags))
         func_code = """
-        static PyObject* %s(PyObject* self, PyObject* args)
-        {
-        %s
-        }
-        """ % (cfuncname, code)
+        static PyObject* {cfuncname}{signature}
+        {{
+        {code}
+        }}
+        """.format(cfuncname=cfuncname, signature=signature, code=code)
         codes.append(func_code)
 
     body = "\n".join(codes) + """


More information about the pypy-commit mailing list