Added Cython sample project.

pull/383/head
Jussi Pakkanen 9 years ago
parent 98d00386b1
commit 738f7f860c
  1. 23
      test cases/python3/3 cython/cytest.py
  2. 9
      test cases/python3/3 cython/libdir/cstorer.pxd
  3. 18
      test cases/python3/3 cython/libdir/meson.build
  4. 24
      test cases/python3/3 cython/libdir/storer.c
  5. 8
      test cases/python3/3 cython/libdir/storer.h
  6. 16
      test cases/python3/3 cython/libdir/storer.pyx
  7. 12
      test cases/python3/3 cython/meson.build

@ -0,0 +1,23 @@
#!/usr/bin/env python3
from storer import Storer
import sys
s = Storer()
if s.get_value() != 0:
print('Initial value incorrect.')
sys.exit(1)
s.set_value(42)
if s.get_value() != 42:
print('Setting value failed.')
sys.exit(1)
try:
s.set_value('not a number')
print('Using wrong argument type did not fail.')
sys.exit(1)
except TypeError:
pass

@ -0,0 +1,9 @@
cdef extern from "storer.h":
ctypedef struct Storer:
pass
Storer* storer_new();
void storer_destroy(Storer *s);
int storer_get_value(Storer *s);
void storer_set_value(Storer *s, int v);

@ -0,0 +1,18 @@
pxd_c = custom_target('cstorer_pxd',
output : 'cstorer_pxd.c',
input : 'cstorer.pxd',
command : [cython, '@INPUT@', '-o', '@OUTPUT@'],
)
pyx_c = custom_target('storer_pyx',
output : 'storer_pyx.c',
input : 'storer.pyx',
command : [cython, '@INPUT@', '-o', '@OUTPUT@'],
)
slib = shared_library('storer',
'storer.c', pxd_c, pyx_c,
name_prefix : '',
dependencies : py3_dep)
pydir = meson.current_build_dir()

@ -0,0 +1,24 @@
#include"storer.h"
#include<stdlib.h>
struct _Storer {
int value;
};
Storer* storer_new() {
Storer *s = malloc(sizeof(struct _Storer));
s->value = 0;
return s;
}
void storer_destroy(Storer *s) {
free(s);
}
int storer_get_value(Storer *s) {
return s->value;
}
void storer_set_value(Storer *s, int v) {
s->value = v;
}

@ -0,0 +1,8 @@
#pragma once
typedef struct _Storer Storer;
Storer* storer_new();
void storer_destroy(Storer *s);
int storer_get_value(Storer *s);
void storer_set_value(Storer *s, int v);

@ -0,0 +1,16 @@
cimport cstorer
cdef class Storer:
cdef cstorer.Storer* _c_storer
def __cinit__(self):
self._c_storer = cstorer.storer_new()
def __dealloc__(self):
cstorer.storer_destroy(self._c_storer)
cpdef int get_value(self):
return cstorer.storer_get_value(self._c_storer)
cpdef void set_value(self, int value):
cstorer.storer_set_value(self._c_storer, value)

@ -0,0 +1,12 @@
project('cython', 'c',
default_options : ['warning_level=3'])
cython = find_program('cython3')
py3_dep = dependency('python3')
subdir('libdir')
test('cython tester',
find_program('cytest.py'),
env : ['PYTHONPATH=' + pydir]
)
Loading…
Cancel
Save