[pypy-svn] r73267 - in pypy/branch/cpython-extension/pypy/module/cpyext: . include test

fijal at codespeak.net fijal at codespeak.net
Fri Apr 2 01:41:44 CEST 2010


Author: fijal
Date: Fri Apr  2 01:41:43 2010
New Revision: 73267

Added:
   pypy/branch/cpython-extension/pypy/module/cpyext/getargs.py   (contents, props changed)
   pypy/branch/cpython-extension/pypy/module/cpyext/include/getargs.c
Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py
   pypy/branch/cpython-extension/pypy/module/cpyext/api.py
   pypy/branch/cpython-extension/pypy/module/cpyext/include/modsupport.h
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_cpyext.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_getargs.py
Log:
Add support for PyArg_Parse. Right now it happily segfaults


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py	Fri Apr  2 01:41:43 2010
@@ -45,5 +45,7 @@
 import pypy.module.cpyext.listobject
 import pypy.module.cpyext.sequence
 import pypy.module.cpyext.eval
+import pypy.module.cpyext.getargs
+
 # now that all rffi_platform.Struct types are registered, configure them
 api.configure_types()

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/api.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/api.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/api.py	Fri Apr  2 01:41:43 2010
@@ -38,12 +38,14 @@
 class CConfig:
     _compilation_info_ = ExternalCompilationInfo(
         include_dirs=include_dirs,
-        includes=['Python.h']
+        includes=['Python.h', 'stdarg.h']
         )
 
 class CConfig_constants:
     _compilation_info_ = CConfig._compilation_info_
 
+va_list = rffi.COpaque('va_list')
+
 constant_names = """
 Py_TPFLAGS_READY Py_TPFLAGS_READYING
 METH_COEXIST METH_STATIC METH_CLASS METH_NOARGS
@@ -202,7 +204,10 @@
 
 def cpython_api_c():
     def decorate(func):
+        def uncallable(*args, **kwds):
+            raise Exception("Uncallable")
         FUNCTIONS_C[func.func_name] = None
+        return uncallable
     return decorate
 
 def cpython_struct(name, fields, forward=None):
@@ -255,6 +260,9 @@
     (("buffer", rffi.CCHARP), ("size", Py_ssize_t))
 cpython_struct("PyStringObject", PyStringObjectFields, PyStringObjectStruct)
 
+VA_TP_LIST = {'int': rffi.INT,
+              'PyObject*': PyObject}
+
 def configure_types():
     for name, TYPE in rffi_platform.configure(CConfig).iteritems():
         if name in TYPES:
@@ -434,6 +442,13 @@
             ll2ctypes.lltype2ctypes(func.get_llhelper(space)),
             ctypes.c_void_p)
 
+    for name, TP in VA_TP_LIST.iteritems():
+        name_no_star = name.strip('*')
+        ARGS = [lltype.Ptr(va_list)]
+        func = rffi.llexternal('pypy_va_get_%s' % name_no_star, ARGS,
+                               TP, compilation_info=eci)
+        globals()['va_get_%s' % name_no_star] = func
+
     return modulename.new(ext='')
 
 def generate_macros(export_symbols, rename=True, do_deref=True):
@@ -482,6 +497,13 @@
     pypy_decls.append("#ifndef PYPY_STANDALONE\n")
     for name, (typ, expr) in GLOBALS.iteritems():
         pypy_decls.append('PyAPI_DATA(%s) %s;' % (typ, name.replace("#", "")))
+    for name in VA_TP_LIST:
+        name_no_star = name.strip('*')
+        header = ('%s pypy_va_get_%s(va_list* vp)' %
+                  (name, name_no_star))
+        pypy_decls.append(header + ';')
+        functions.append(header + '\n{return va_arg(*vp, %s);}\n' % name)
+    
     pypy_decls.append("#endif\n")
 
     pypy_decl_h = udir.join('pypy_decl.h')
@@ -510,7 +532,8 @@
         include_dirs=include_dirs,
         separate_module_files=[include_dir / "varargwrapper.c",
                                include_dir / "pyerrors.c",
-                               include_dir / "modsupport.c"],
+                               include_dir / "modsupport.c",
+                               include_dir / "getargs.c"],
         export_symbols=export_symbols_eci,
         **kwds
         )

Added: pypy/branch/cpython-extension/pypy/module/cpyext/getargs.py
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/getargs.py	Fri Apr  2 01:41:43 2010
@@ -0,0 +1,24 @@
+
+from pypy.module.cpyext.api import cpython_api, PyObject, CANNOT_FAIL, va_list,\
+     cpython_api_c
+from pypy.module.cpyext import api
+from pypy.rpython.lltypesystem import lltype, rffi
+
+ at cpython_api_c()
+def PyArg_Parse():
+    pass
+
+ at cpython_api([PyObject, rffi.CCHARP, lltype.Ptr(va_list), rffi.INT_real],
+             rffi.INT_real, error=-1)
+def pypy_vgetargs1(space, w_obj, fmt, va_list_p, lgt):
+    i = 0
+    import pdb
+    pdb.set_trace()
+    while True:
+        c = fmt[i]
+        if c == "\x00":
+            return 0
+        if c == "i":
+            pyobj = api.va_get_PyObject(va_list_p);
+        i += 1
+    return 0

Added: pypy/branch/cpython-extension/pypy/module/cpyext/include/getargs.c
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/include/getargs.c	Fri Apr  2 01:41:43 2010
@@ -0,0 +1,141 @@
+
+/* New getargs implementation */
+
+#include "Python.h"
+
+#include <ctype.h>
+
+
+#ifdef __cplusplus
+extern "C" { 
+#endif
+
+int PyArg_Parse(PyObject *, const char *, ...);
+int PyArg_ParseTuple(PyObject *, const char *, ...);
+int PyArg_VaParse(PyObject *, const char *, va_list);
+
+int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
+				const char *, char **, ...);
+int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
+				const char *, char **, va_list);
+
+#ifdef HAVE_DECLSPEC_DLL
+/* Export functions */
+/*PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, char *, ...);
+PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, char *, ...);
+PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
+                                                  const char *, char **, ...);
+PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
+PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, char *, va_list);
+PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
+const char *, char **, va_list);*/
+#endif
+
+#define FLAG_COMPAT 1
+#define FLAG_SIZE_T 2
+
+
+/* Forward */
+int pypy_vgetargs1(struct _object *, char *, va_list *, int);
+  //static void seterror(int, const char *, int *, const char *, const char *);
+  //static char *convertitem(PyObject *, const char **, va_list *, int, int *, 
+  //                       char *, size_t, PyObject **);
+//static char *converttuple(PyObject *, const char **, va_list *, int,
+//			  int *, char *, size_t, int, PyObject **);
+//static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
+//			   size_t, PyObject **);
+//static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
+//static int getbuffer(PyObject *, Py_buffer *, char**);
+
+//static int vgetargskeywords(PyObject *, PyObject *,
+//			    const char *, char **, va_list *, int);
+//static char *skipitem(const char **, va_list *, int);
+
+int
+PyArg_Parse(PyObject *args, const char *format, ...)
+{
+	int retval;
+	va_list va;
+	
+	va_start(va, format);
+	retval = pypy_vgetargs1(args, format, &va, FLAG_COMPAT);
+	va_end(va);
+	return retval;
+}
+
+int
+_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
+{
+	int retval;
+	va_list va;
+	
+	va_start(va, format);
+	retval = pypy_vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
+	va_end(va);
+	return retval;
+}
+
+
+int
+PyArg_ParseTuple(PyObject *args, const char *format, ...)
+{
+	int retval;
+	va_list va;
+	
+	va_start(va, format);
+	retval = pypy_vgetargs1(args, format, &va, 0);
+	va_end(va);
+	return retval;
+}
+
+int
+_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
+{
+	int retval;
+	va_list va;
+	
+	va_start(va, format);
+	retval = pypy_vgetargs1(args, format, &va, FLAG_SIZE_T);
+	va_end(va);
+	return retval;
+}
+
+
+int
+PyArg_VaParse(PyObject *args, const char *format, va_list va)
+{
+	va_list lva;
+
+#ifdef VA_LIST_IS_ARRAY
+	memcpy(lva, va, sizeof(va_list));
+#else
+#ifdef __va_copy
+	__va_copy(lva, va);
+#else
+	lva = va;
+#endif
+#endif
+
+	return pypy_vgetargs1(args, format, &lva, 0);
+}
+
+int
+_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
+{
+	va_list lva;
+
+#ifdef VA_LIST_IS_ARRAY
+	memcpy(lva, va, sizeof(va_list));
+#else
+#ifdef __va_copy
+	__va_copy(lva, va);
+#else
+	lva = va;
+#endif
+#endif
+
+	return pypy_vgetargs1(args, format, &lva, FLAG_SIZE_T);
+}
+
+
+// REST IS NOT COPIED FROM CPYTHON

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/include/modsupport.h
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/include/modsupport.h	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/include/modsupport.h	Fri Apr  2 01:41:43 2010
@@ -10,6 +10,15 @@
 #define PYTHON_API_VERSION 1013
 #define PYTHON_API_STRING "1013"
 
+int PyArg_Parse(PyObject *, const char *, ...);
+int PyArg_ParseTuple(PyObject *, const char *, ...);
+int PyArg_VaParse(PyObject *, const char *, va_list);
+
+int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
+				const char *, char **, ...);
+int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
+				const char *, char **, va_list);
+
 #define Py_InitModule(name, methods) \
 	Py_InitModule4(name, methods, (char *)NULL, (PyObject *)NULL, \
 		       PYTHON_API_VERSION)
@@ -20,6 +29,8 @@
 
 int PyModule_AddObject(PyObject *m, const char *name, PyObject *o);
 
+
+
 #ifdef __cplusplus
 }
 #endif

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_cpyext.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_cpyext.py	Fri Apr  2 01:41:43 2010
@@ -437,7 +437,7 @@
     def test_new_exception(self):
         skip("not working yet")
         mod = self.import_extension('foo', [
-            ('newexc', 'METHOD_VARARGS',
+            ('newexc', 'METH_VARARGS',
              '''
              char *name = PyString_AsString(PyTuple_GetItem(args, 0));
              return PyExc_NewException(name, PyTuple_GetItem(args, 1),

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_getargs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_getargs.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_getargs.py	Fri Apr  2 01:41:43 2010
@@ -8,9 +8,9 @@
             ('oneargint', 'METH_VARARGS',
              '''
              int l;
-             //if (!PyArg_Parse(args, "i", &l)) {
-             //    return NULL;
-             //}
+             if (!PyArg_Parse(args, "i", &l)) {
+                 return NULL;
+             }
              return PyInt_FromLong(l);
              '''
              )])



More information about the Pypy-commit mailing list