File: extern_python.py

package info (click to toggle)
python-cffi 2.0.0~b1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,796 kB
  • sloc: python: 28,456; ansic: 15,272; asm: 116; makefile: 97; sh: 14
file content (26 lines) | stat: -rw-r--r-- 460 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cffi

ffi = cffi.FFI()

ffi.cdef("""int my_algo(int); extern "Python" int f(int);""")

ffi.set_source("_extern_python_cffi", """
    static int f(int);
    static int my_algo(int n) {
        int i, sum = 0;
        for (i = 0; i < n; i++)
            sum += f(i);
        return sum;
    }
""")

ffi.compile()


from _extern_python_cffi import ffi, lib

@ffi.def_extern()
def f(n):
    return n * n

assert lib.my_algo(10) == 0+1+4+9+16+25+36+49+64+81