Skip to content

Commit

Permalink
Cython: test
Browse files Browse the repository at this point in the history
  • Loading branch information
amcn committed Feb 19, 2024
1 parent b454af7 commit 4c2294d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test cases/cython/4 limited api/limited.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This is taken from Cython's limited API tests and
# as a result is under Apache 2.0

import cython

@cython.binding(False)
def fib(int n):
cdef int a, b
a, b = 0, 1
while b < n:
a, b = b, a + b
return b

def lsum(values):
cdef long result = 0
for value in values:
result += value
if type(values) is list:
for value in reversed(<list>values):
result += value
elif type(values) is tuple:
for value in reversed(<tuple>values):
result += value
return result

@cython.binding(False)
def raises():
raise RuntimeError()

def decode(bytes b, bytearray ba):
return b.decode("utf-8") + ba.decode("utf-8")

def cast_float(object o):
return float(o)

class C:
pass

cdef class D:
pass

cdef class E(D):
pass
19 changes: 19 additions & 0 deletions test cases/cython/4 limited api/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
project('cython limited api', ['cython', 'c'])

cythonc = meson.get_compiler('cython')
if cythonc.version() < '3.0'
error('MESON_SKIP_TEST: Cython compiler version does not support limited API')
endif

py3 = import('python').find_installation()

ext_mod = py3.extension_module('limited',
'limited.pyx',
limited_api: '3.10'
)

test('limited_api runner',
py3,
args : files('run.py'),
env : ['PYTHONPATH=' + meson.current_build_dir()]
)
25 changes: 25 additions & 0 deletions test cases/cython/4 limited api/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This is taken from Cython's limited API tests and
# as a result is under Apache 2.0

import limited

limited.fib(11)

assert limited.lsum(list(range(10))) == 90
assert limited.lsum(tuple(range(10))) == 90
assert limited.lsum(iter(range(10))) == 45

try:
limited.raises()
except RuntimeError:
pass

limited.C()
limited.D()
limited.E()

assert limited.decode(b'a', bytearray(b'b')) == "ab"

assert limited.cast_float(1) == 1.0
assert limited.cast_float("2.0") == 2.0
assert limited.cast_float(bytearray(b"3")) == 3.0
10 changes: 10 additions & 0 deletions test cases/cython/4 limited api/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"matrix": {
"options": {
"cython_language": [
{ "val": "c" },
{ "val": "cpp" }
]
}
}
}

0 comments on commit 4c2294d

Please sign in to comment.