[pypy-commit] cffi default: Ask for the length of the list from C code, too.

arigo noreply at buildbot.pypy.org
Fri Aug 31 15:24:30 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r907:8dadf841d6f3
Date: 2012-08-31 15:24 +0200
http://bitbucket.org/cffi/cffi/changeset/8dadf841d6f3/

Log:	Ask for the length of the list from C code, too.

diff --git a/demo/pyobj.py b/demo/pyobj.py
--- a/demo/pyobj.py
+++ b/demo/pyobj.py
@@ -33,9 +33,14 @@
 
 ffi.cdef("""
     typedef int pyobj_t;
-    int sum(pyobj_t oblist, int count);
+    int sum(pyobj_t oblist);
 """)
 
+ at ffi.pyexport("int(pyobj_t)")
+def length(oblist):
+    list = referents[oblist]
+    return len(list)
+
 @ffi.pyexport("int(pyobj_t, int)")
 def getitem(oblist, index):
     list = referents[oblist]
@@ -44,8 +49,9 @@
 lib = ffi.verify("""
     typedef int pyobj_t;
 
-    int sum(pyobj_t oblist, int count) {
+    int sum(pyobj_t oblist) {
         int i, result = 0;
+        int count = length(oblist);
         for (i=0; i<count; i++) {
             int n = getitem(oblist, i);
             result += n;
@@ -55,4 +61,4 @@
 """)
 
 with Ref([10, 20, 30, 40]) as oblist:
-    print lib.sum(oblist, 4)
+    print lib.sum(oblist)


More information about the pypy-commit mailing list