[pypy-svn] r74986 - in pypy/trunk/pypy/module/cpyext: . include test

afa at codespeak.net afa at codespeak.net
Tue Jun 1 00:07:25 CEST 2010


Author: afa
Date: Tue Jun  1 00:07:24 2010
New Revision: 74986

Modified:
   pypy/trunk/pypy/module/cpyext/api.py
   pypy/trunk/pypy/module/cpyext/include/Python.h
   pypy/trunk/pypy/module/cpyext/sliceobject.py
   pypy/trunk/pypy/module/cpyext/test/comparisons.c
   pypy/trunk/pypy/module/cpyext/test/foo.c
   pypy/trunk/pypy/module/cpyext/test/test_object.py
Log:
Kill most compilation warnings in cpyext tests


Modified: pypy/trunk/pypy/module/cpyext/api.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/api.py	(original)
+++ pypy/trunk/pypy/module/cpyext/api.py	Tue Jun  1 00:07:24 2010
@@ -224,7 +224,8 @@
                         elif isinstance(input_arg, W_Root):
                             arg = input_arg
                         else:
-                            arg = from_ref(space, input_arg)
+                            arg = from_ref(space,
+                                           rffi.cast(PyObject, input_arg))
                     else:
                         arg = input_arg
                     newargs += (arg, )
@@ -314,7 +315,7 @@
     # Standard exceptions
     for exc_name in exceptions.Module.interpleveldefs.keys():
         GLOBALS['PyExc_' + exc_name] = (
-            'PyTypeObject*',
+            'PyObject*',
             'space.gettypeobject(interp_exceptions.W_%s.typedef)'% (exc_name, ))
 
     # Common types with their own struct
@@ -568,8 +569,19 @@
     members = []
     structindex = {}
     for name, func in FUNCTIONS.iteritems():
-        cdecl = db.gettype(func.functype)
-        members.append(cdecl.replace('@', name) + ';')
+        restype = db.gettype(func.restype).replace('@', '').strip()
+        args = []
+        for i, argtype in enumerate(func.argtypes):
+            if argtype is CONST_STRING:
+                arg = 'const char *@'
+            elif argtype is CONST_WSTRING:
+                arg = 'const wchar_t *@'
+            else:
+                arg = db.gettype(argtype)
+            arg = arg.replace('@', 'arg%d' % (i,)).strip()
+            args.append(arg)
+        args = ', '.join(args) or "void"
+        members.append('%s (*%s)(%s);' % (restype, name, args))
         structindex[name] = len(structindex)
     structmembers = '\n'.join(members)
     struct_declaration_code = """\
@@ -583,6 +595,8 @@
 
     global_objects = []
     for name, (type, expr) in GLOBALS.iteritems():
+        if "#" in name:
+            continue
         global_objects.append('%s %s = NULL;' % (type, name.replace("#", "")))
     global_code = '\n'.join(global_objects)
 
@@ -606,16 +620,35 @@
     pypyAPI = ctypes.POINTER(ctypes.c_void_p).in_dll(bridge, 'pypyAPI')
 
     # populate static data
-    for name, (type, expr) in GLOBALS.iteritems():
+    for name, (typ, expr) in GLOBALS.iteritems():
         from pypy.module import cpyext
         w_obj = eval(expr)
-        name = name.replace("#", "")
+        if name.endswith('#'):
+            name = name[:-1]
+            isptr = False
+        else:
+            isptr = True
         INTERPLEVEL_API[name] = w_obj
 
         name = name.replace('Py', 'PyPy')
-        ptr = ctypes.c_void_p.in_dll(bridge, name)
-        ptr.value = ctypes.cast(ll2ctypes.lltype2ctypes(make_ref(space, w_obj)),
-            ctypes.c_void_p).value
+        if isptr:
+            ptr = ctypes.c_void_p.in_dll(bridge, name)
+            ptr.value = ctypes.cast(ll2ctypes.lltype2ctypes(make_ref(space, w_obj)),
+                                    ctypes.c_void_p).value
+        elif typ in ('PyObject*', 'PyTypeObject*'):
+            in_dll = ll2ctypes.get_ctypes_type(PyObject.TO).in_dll(bridge,name)
+            py_obj = ll2ctypes.ctypes2lltype(PyObject, ctypes.pointer(in_dll))
+            from pypy.module.cpyext.pyobject import (
+                track_reference, get_typedescr)
+            w_type = space.type(w_obj)
+            typedescr = get_typedescr(w_type.instancetypedef)
+            py_obj.c_ob_refcnt = 1
+            py_obj.c_ob_type = rffi.cast(PyTypeObjectPtr,
+                                         make_ref(space, w_type))
+            typedescr.attach(space, py_obj, w_obj)
+            track_reference(space, py_obj, w_obj)
+        else:
+            assert False, "Unknown static object: %s %s" % (typ, name)
 
     # implement structure initialization code
     for name, func in FUNCTIONS.iteritems():
@@ -635,17 +668,13 @@
         if name.startswith("PyPy"):
             renamed_symbols.append(name)
             continue
-        if "#" in name:
-            deref = "*"
-            if not do_deref and not rename: continue
-        else:
-            deref = ""
-            if not rename: continue
+        if not rename:
+            continue
         name = name.replace("#", "")
         newname = name.replace('Py', 'PyPy')
         if not rename:
             newname = name
-        pypy_macros.append('#define %s %s%s' % (name, deref, newname))
+        pypy_macros.append('#define %s %s' % (name, newname))
         renamed_symbols.append(newname)
     if rename:
         export_symbols[:] = renamed_symbols
@@ -666,7 +695,7 @@
     pypy_macros_h = udir.join('pypy_macros.h')
     pypy_macros_h.write('\n'.join(pypy_macros))
 
-def generate_decls_and_callbacks(db, export_symbols, api_struct=True, globals_are_pointers=True):
+def generate_decls_and_callbacks(db, export_symbols, api_struct=True):
     # implement function callbacks and generate function decls
     functions = []
     pypy_decls = []
@@ -695,7 +724,10 @@
         if api_struct:
             callargs = ', '.join('arg%d' % (i,)
                                  for i in range(len(func.argtypes)))
-            body = "{ return _pypyAPI.%s(%s); }" % (name, callargs)
+            if func.restype is lltype.Void:
+                body = "{ _pypyAPI.%s(%s); }" % (name, callargs)
+            else:
+                body = "{ return _pypyAPI.%s(%s); }" % (name, callargs)
             functions.append('%s %s(%s)\n%s' % (restype, name, args, body))
     for name in VA_TP_LIST:
         name_no_star = process_va_name(name)
@@ -706,12 +738,10 @@
         export_symbols.append('pypy_va_get_%s' % (name_no_star,))
 
     for name, (typ, expr) in GLOBALS.iteritems():
-        name_clean = name.replace("#", "")
-        if not globals_are_pointers:
+        if name.endswith('#'):
+            name = name.replace("#", "")
             typ = typ.replace("*", "")
-        pypy_decls.append('PyAPI_DATA(%s) %s;' % (typ, name_clean))
-        if not globals_are_pointers and "#" not in name:
-            pypy_decls.append("#define %s (PyObject*)&%s" % (name, name,))
+        pypy_decls.append('PyAPI_DATA(%s) %s;' % (typ, name))
 
     pypy_decls.append("#ifdef __cplusplus")
     pypy_decls.append("}")
@@ -739,6 +769,14 @@
     else:
         kwds["includes"] = ['Python.h'] # this is our Python.h
 
+    # Generate definitions for global structures
+    struct_file = udir.join('pypy_structs.c')
+    structs = ["#include <Python.h>"]
+    for name, (type, expr) in GLOBALS.iteritems():
+        if name.endswith('#'):
+            structs.append('%s %s;' % (type[:-1], name[:-1]))
+    struct_file.write('\n'.join(structs))
+
     eci = ExternalCompilationInfo(
         include_dirs=include_dirs,
         separate_module_files=[source_dir / "varargwrapper.c",
@@ -751,6 +789,7 @@
                                source_dir / "bufferobject.c",
                                source_dir / "object.c",
                                source_dir / "cobject.c",
+                               struct_file,
                                ],
         separate_module_sources = [code],
         export_symbols=export_symbols_eci,
@@ -769,7 +808,7 @@
 
     generate_macros(export_symbols, rename=False, do_deref=False)
 
-    functions = generate_decls_and_callbacks(db, [], api_struct=False, globals_are_pointers=False)
+    functions = generate_decls_and_callbacks(db, [], api_struct=False)
     code = "#include <Python.h>\n" + "\n".join(functions)
 
     eci = build_eci(False, export_symbols, code)

Modified: pypy/trunk/pypy/module/cpyext/include/Python.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/Python.h	(original)
+++ pypy/trunk/pypy/module/cpyext/include/Python.h	Tue Jun  1 00:07:24 2010
@@ -28,15 +28,19 @@
 # define Py_LOCAL_INLINE(type) static __inline type __fastcall
 #endif
 
-#define Py_ssize_t long
+#include <stdlib.h>
+
+#ifndef _WIN32
+typedef intptr_t Py_ssize_t;
+#else
+typedef long Py_ssize_t;
+#endif
 #define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
 #define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX-1)
 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
 
 #define Py_USING_UNICODE
 
-#include <stdlib.h>
-
 /* Convert a possibly signed character to a nonnegative int */
 /* XXX This assumes characters are 8 bits wide */
 #ifdef __CHAR_UNSIGNED__

Modified: pypy/trunk/pypy/module/cpyext/sliceobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/sliceobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/sliceobject.py	Tue Jun  1 00:07:24 2010
@@ -58,10 +58,10 @@
     the new object could not be allocated."""
     return W_SliceObject(w_start, w_stop, w_step)
 
- at cpython_api([PyObject, Py_ssize_t, Py_ssize_tP, Py_ssize_tP, Py_ssize_tP, 
+ at cpython_api([PySliceObject, Py_ssize_t, Py_ssize_tP, Py_ssize_tP, Py_ssize_tP,
                 Py_ssize_tP], rffi.INT_real, error=-1)
-def PySlice_GetIndicesEx(space, w_slice, length, start_p, stop_p, 
-        step_p, slicelength_p):
+def PySlice_GetIndicesEx(space, w_slice, length, start_p, stop_p, step_p,
+                         slicelength_p):
     """Usable replacement for PySlice_GetIndices().  Retrieve the start,
     stop, and step indices from the slice object slice assuming a sequence of
     length length, and store the length of the slice in slicelength.  Out

Modified: pypy/trunk/pypy/module/cpyext/test/comparisons.c
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/comparisons.c	(original)
+++ pypy/trunk/pypy/module/cpyext/test/comparisons.c	Tue Jun  1 00:07:24 2010
@@ -18,6 +18,7 @@
     else if (opid == Py_NE) {
         return PyBool_FromLong(val != 3);
     }
+    return Py_NotImplemented;
 }
 
 static long cmp_hashfunc(PyObject *self) {

Modified: pypy/trunk/pypy/module/cpyext/test/foo.c
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/foo.c	(original)
+++ pypy/trunk/pypy/module/cpyext/test/foo.c	Tue Jun  1 00:07:24 2010
@@ -55,7 +55,7 @@
 static PyObject *
 foo_create(fooobject *self)
 {
-    return newfooobject();
+    return (PyObject*)newfooobject();
 }
 
 static PyObject *
@@ -130,7 +130,7 @@
             return -1;
         self->foo = v;
     }
-    return PyObject_GenericSetAttr(self, name, value);
+    return PyObject_GenericSetAttr((PyObject *)self, name, value);
 }
 
 static PyMemberDef foo_members[] = {
@@ -170,7 +170,7 @@
     foo_call,                /*tp_call*/
     0,                       /*tp_str*/
     0,                       /*tp_getattro*/
-    foo_setattro,            /*tp_setattro*/
+    (setattrofunc)foo_setattro, /*tp_setattro*/
     0,                       /*tp_as_buffer*/
     Py_TPFLAGS_DEFAULT,      /*tp_flags*/
     0,                       /*tp_doc*/
@@ -262,7 +262,7 @@
     0,          /*tp_descr_set*/
     0,          /*tp_dictoffset*/
 
-    Fuu_init,          /*tp_init*/
+    (initproc) Fuu_init, /*tp_init*/
     0,          /*tp_alloc  will be set to PyType_GenericAlloc in module init*/
     0,          /*tp_new*/
     0,          /*tp_free  Low-level free-memory routine */

Modified: pypy/trunk/pypy/module/cpyext/test/test_object.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_object.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_object.py	Tue Jun  1 00:07:24 2010
@@ -202,7 +202,7 @@
             ("typecheck", "METH_VARARGS",
              """
                  PyObject *obj = PyTuple_GET_ITEM(args, 0);
-                 PyObject *type = PyTuple_GET_ITEM(args, 1);
+                 PyTypeObject *type = (PyTypeObject *)PyTuple_GET_ITEM(args, 1);
                  return PyBool_FromLong(PyObject_TypeCheck(obj, type));
              """)])
         assert module.typecheck(1, int)



More information about the Pypy-commit mailing list