summaryrefslogtreecommitdiffstats
path: root/test cases/cython
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-29 04:41:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-29 04:41:38 +0000
commit7b6e527f440cd7e6f8be2b07cee320ee6ca18786 (patch)
tree4a2738d69fa2814659fdadddf5826282e73d81f4 /test cases/cython
parentInitial commit. (diff)
downloadmeson-7b6e527f440cd7e6f8be2b07cee320ee6ca18786.tar.xz
meson-7b6e527f440cd7e6f8be2b07cee320ee6ca18786.zip
Adding upstream version 1.0.1.upstream/1.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test cases/cython')
-rwxr-xr-xtest cases/cython/1 basic/cytest.py19
-rw-r--r--test cases/cython/1 basic/libdir/cstorer.pxd9
-rw-r--r--test cases/cython/1 basic/libdir/meson.build8
-rw-r--r--test cases/cython/1 basic/libdir/storer.c24
-rw-r--r--test cases/cython/1 basic/libdir/storer.h16
-rw-r--r--test cases/cython/1 basic/libdir/storer.pyx16
-rw-r--r--test cases/cython/1 basic/meson.build20
-rw-r--r--test cases/cython/1 basic/test.json10
-rw-r--r--test cases/cython/2 generated sources/configure.pyx.in2
-rw-r--r--test cases/cython/2 generated sources/g.in2
-rw-r--r--test cases/cython/2 generated sources/gen.py14
-rwxr-xr-xtest cases/cython/2 generated sources/generator.py12
-rw-r--r--test cases/cython/2 generated sources/includestuff.pyx1
-rw-r--r--test cases/cython/2 generated sources/libdir/gen.py14
-rw-r--r--test cases/cython/2 generated sources/libdir/meson.build10
-rw-r--r--test cases/cython/2 generated sources/meson.build105
-rw-r--r--test cases/cython/2 generated sources/simpleinclude.pyx1
-rw-r--r--test cases/cython/2 generated sources/simplestuff.pxi2
-rw-r--r--test cases/cython/2 generated sources/stuff.pxi.in2
-rw-r--r--test cases/cython/2 generated sources/test.py13
-rw-r--r--test cases/cython/3 cython_args/cythonargs.pyx5
-rw-r--r--test cases/cython/3 cython_args/meson.build30
-rw-r--r--test cases/cython/3 cython_args/test.py3
23 files changed, 338 insertions, 0 deletions
diff --git a/test cases/cython/1 basic/cytest.py b/test cases/cython/1 basic/cytest.py
new file mode 100755
index 0000000..c08ffee
--- /dev/null
+++ b/test cases/cython/1 basic/cytest.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+
+from storer import Storer
+
+s = Storer()
+
+if s.get_value() != 0:
+ raise SystemExit('Initial value incorrect.')
+
+s.set_value(42)
+
+if s.get_value() != 42:
+ raise SystemExit('Setting value failed.')
+
+try:
+ s.set_value('not a number')
+ raise SystemExit('Using wrong argument type did not fail.')
+except TypeError:
+ pass
diff --git a/test cases/cython/1 basic/libdir/cstorer.pxd b/test cases/cython/1 basic/libdir/cstorer.pxd
new file mode 100644
index 0000000..7b730fc
--- /dev/null
+++ b/test cases/cython/1 basic/libdir/cstorer.pxd
@@ -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);
diff --git a/test cases/cython/1 basic/libdir/meson.build b/test cases/cython/1 basic/libdir/meson.build
new file mode 100644
index 0000000..144bb1f
--- /dev/null
+++ b/test cases/cython/1 basic/libdir/meson.build
@@ -0,0 +1,8 @@
+slib = py3.extension_module(
+ 'storer',
+ 'storer.pyx',
+ 'storer.c',
+ dependencies : py3_dep
+)
+
+pydir = meson.current_build_dir()
diff --git a/test cases/cython/1 basic/libdir/storer.c b/test cases/cython/1 basic/libdir/storer.c
new file mode 100644
index 0000000..0199bb8
--- /dev/null
+++ b/test cases/cython/1 basic/libdir/storer.c
@@ -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;
+}
diff --git a/test cases/cython/1 basic/libdir/storer.h b/test cases/cython/1 basic/libdir/storer.h
new file mode 100644
index 0000000..6f5bc6f
--- /dev/null
+++ b/test cases/cython/1 basic/libdir/storer.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+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);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/test cases/cython/1 basic/libdir/storer.pyx b/test cases/cython/1 basic/libdir/storer.pyx
new file mode 100644
index 0000000..ed551dc
--- /dev/null
+++ b/test cases/cython/1 basic/libdir/storer.pyx
@@ -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 set_value(self, int value):
+ cstorer.storer_set_value(self._c_storer, value)
diff --git a/test cases/cython/1 basic/meson.build b/test cases/cython/1 basic/meson.build
new file mode 100644
index 0000000..8c24e23
--- /dev/null
+++ b/test cases/cython/1 basic/meson.build
@@ -0,0 +1,20 @@
+project(
+ 'basic cython project',
+ ['cython', 'c'],
+ default_options : ['warning_level=3']
+)
+
+py_mod = import('python')
+py3 = py_mod.find_installation()
+py3_dep = py3.dependency(required : false)
+if not py3_dep.found()
+ error('MESON_SKIP_TEST: Python library not found.')
+endif
+
+subdir('libdir')
+
+test('cython tester',
+ py3,
+ args : files('cytest.py'),
+ env : ['PYTHONPATH=' + pydir]
+)
diff --git a/test cases/cython/1 basic/test.json b/test cases/cython/1 basic/test.json
new file mode 100644
index 0000000..ed7ac2b
--- /dev/null
+++ b/test cases/cython/1 basic/test.json
@@ -0,0 +1,10 @@
+{
+ "matrix": {
+ "options": {
+ "cython_language": [
+ { "val": "c" },
+ { "val": "cpp" }
+ ]
+ }
+ }
+}
diff --git a/test cases/cython/2 generated sources/configure.pyx.in b/test cases/cython/2 generated sources/configure.pyx.in
new file mode 100644
index 0000000..1c44f6d
--- /dev/null
+++ b/test cases/cython/2 generated sources/configure.pyx.in
@@ -0,0 +1,2 @@
+cpdef func():
+ return "Hello, World!"
diff --git a/test cases/cython/2 generated sources/g.in b/test cases/cython/2 generated sources/g.in
new file mode 100644
index 0000000..1c44f6d
--- /dev/null
+++ b/test cases/cython/2 generated sources/g.in
@@ -0,0 +1,2 @@
+cpdef func():
+ return "Hello, World!"
diff --git a/test cases/cython/2 generated sources/gen.py b/test cases/cython/2 generated sources/gen.py
new file mode 100644
index 0000000..5c0a82d
--- /dev/null
+++ b/test cases/cython/2 generated sources/gen.py
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: Apache-2.0
+
+import argparse
+import textwrap
+
+parser = argparse.ArgumentParser()
+parser.add_argument('output')
+args = parser.parse_args()
+
+with open(args.output, 'w') as f:
+ f.write(textwrap.dedent('''\
+ cpdef func():
+ return "Hello, World!"
+ '''))
diff --git a/test cases/cython/2 generated sources/generator.py b/test cases/cython/2 generated sources/generator.py
new file mode 100755
index 0000000..9b0d94a
--- /dev/null
+++ b/test cases/cython/2 generated sources/generator.py
@@ -0,0 +1,12 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: Apache-2.0
+
+import argparse
+
+parser = argparse.ArgumentParser()
+parser.add_argument('input')
+parser.add_argument('output')
+args = parser.parse_args()
+
+with open(args.input) as i, open(args.output, 'w') as o:
+ o.write(i.read())
diff --git a/test cases/cython/2 generated sources/includestuff.pyx b/test cases/cython/2 generated sources/includestuff.pyx
new file mode 100644
index 0000000..83f881b
--- /dev/null
+++ b/test cases/cython/2 generated sources/includestuff.pyx
@@ -0,0 +1 @@
+include "stuff.pxi"
diff --git a/test cases/cython/2 generated sources/libdir/gen.py b/test cases/cython/2 generated sources/libdir/gen.py
new file mode 100644
index 0000000..5c0a82d
--- /dev/null
+++ b/test cases/cython/2 generated sources/libdir/gen.py
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: Apache-2.0
+
+import argparse
+import textwrap
+
+parser = argparse.ArgumentParser()
+parser.add_argument('output')
+args = parser.parse_args()
+
+with open(args.output, 'w') as f:
+ f.write(textwrap.dedent('''\
+ cpdef func():
+ return "Hello, World!"
+ '''))
diff --git a/test cases/cython/2 generated sources/libdir/meson.build b/test cases/cython/2 generated sources/libdir/meson.build
new file mode 100644
index 0000000..e9259bd
--- /dev/null
+++ b/test cases/cython/2 generated sources/libdir/meson.build
@@ -0,0 +1,10 @@
+ct2 = custom_target(
+ 'ct2',
+ input : 'gen.py',
+ output : 'ct2.pyx',
+ command : [py3, '@INPUT@', '@OUTPUT@'],
+)
+
+ct2_ext = py3.extension_module('ct2', ct2, dependencies : py3_dep)
+
+pydir = meson.current_build_dir()
diff --git a/test cases/cython/2 generated sources/meson.build b/test cases/cython/2 generated sources/meson.build
new file mode 100644
index 0000000..498e319
--- /dev/null
+++ b/test cases/cython/2 generated sources/meson.build
@@ -0,0 +1,105 @@
+project(
+ 'generated cython sources',
+ ['cython'],
+)
+
+py_mod = import('python')
+py3 = py_mod.find_installation('python3')
+py3_dep = py3.dependency(required : false)
+fs = import('fs')
+if not py3_dep.found()
+ error('MESON_SKIP_TEST: Python library not found.')
+endif
+
+ct = custom_target(
+ 'ct',
+ input : 'gen.py',
+ output : 'ct.pyx',
+ command : [py3, '@INPUT@', '@OUTPUT@'],
+)
+
+ct_ext = py3.extension_module('ct', ct, dependencies : py3_dep)
+
+test(
+ 'custom target',
+ py3,
+ args : [files('test.py'), 'ct'],
+ env : ['PYTHONPATH=' + meson.current_build_dir()]
+)
+
+# Test a CustomTargetIndex
+cti = custom_target(
+ 'cti',
+ input : 'gen.py',
+ output : 'cti.pyx',
+ command : [py3, '@INPUT@', '@OUTPUT@'],
+)
+
+cti_ext = py3.extension_module('cti', cti[0], dependencies : py3_dep)
+
+cf = configure_file(
+ input : 'configure.pyx.in',
+ output : 'cf.pyx',
+ copy : true,
+)
+
+cf_ext = py3.extension_module('cf', cf, dependencies : py3_dep)
+
+test(
+ 'configure file',
+ py3,
+ args : [files('test.py'), 'cf'],
+ env : ['PYTHONPATH=' + meson.current_build_dir()]
+)
+
+gen = generator(
+ find_program('generator.py'),
+ arguments : ['@INPUT@', '@OUTPUT@'],
+ output : '@BASENAME@.pyx',
+)
+
+g_ext = py3.extension_module(
+ 'g',
+ gen.process('g.in'),
+ dependencies : py3_dep,
+)
+
+test(
+ 'generator',
+ py3,
+ args : [files('test.py'), 'g'],
+ env : ['PYTHONPATH=' + meson.current_build_dir()]
+)
+
+stuff_pxi = fs.copyfile(
+ 'stuff.pxi.in',
+ 'stuff.pxi'
+)
+
+# Need to copy the cython source to the build directory
+# since meson can only generate the .pxi there
+includestuff_pyx = fs.copyfile(
+ 'includestuff.pyx'
+)
+
+stuff_pxi_dep = declare_dependency(sources: stuff_pxi)
+
+includestuff_ext = py3.extension_module(
+ 'includestuff',
+ includestuff_pyx,
+ dependencies: stuff_pxi_dep
+)
+
+simpleinclude_ext = py3.extension_module(
+ 'simpleinclude',
+ 'simpleinclude.pyx',
+)
+
+subdir('libdir')
+
+test(
+ 'custom target in subdir',
+ py3,
+ args : [files('test.py'), 'ct2'],
+ env : ['PYTHONPATH=' + pydir]
+)
diff --git a/test cases/cython/2 generated sources/simpleinclude.pyx b/test cases/cython/2 generated sources/simpleinclude.pyx
new file mode 100644
index 0000000..c110f75
--- /dev/null
+++ b/test cases/cython/2 generated sources/simpleinclude.pyx
@@ -0,0 +1 @@
+include "simplestuff.pxi"
diff --git a/test cases/cython/2 generated sources/simplestuff.pxi b/test cases/cython/2 generated sources/simplestuff.pxi
new file mode 100644
index 0000000..2645216
--- /dev/null
+++ b/test cases/cython/2 generated sources/simplestuff.pxi
@@ -0,0 +1,2 @@
+def func2():
+ print("Hello world")
diff --git a/test cases/cython/2 generated sources/stuff.pxi.in b/test cases/cython/2 generated sources/stuff.pxi.in
new file mode 100644
index 0000000..6417cbc
--- /dev/null
+++ b/test cases/cython/2 generated sources/stuff.pxi.in
@@ -0,0 +1,2 @@
+def func():
+ print("Hello world")
diff --git a/test cases/cython/2 generated sources/test.py b/test cases/cython/2 generated sources/test.py
new file mode 100644
index 0000000..307283f
--- /dev/null
+++ b/test cases/cython/2 generated sources/test.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: Apache-2.0
+
+import argparse
+import importlib
+
+parser = argparse.ArgumentParser()
+parser.add_argument('mod')
+args = parser.parse_args()
+
+mod = importlib.import_module(args.mod)
+
+assert mod.func() == 'Hello, World!'
diff --git a/test cases/cython/3 cython_args/cythonargs.pyx b/test cases/cython/3 cython_args/cythonargs.pyx
new file mode 100644
index 0000000..2b220a7
--- /dev/null
+++ b/test cases/cython/3 cython_args/cythonargs.pyx
@@ -0,0 +1,5 @@
+def test():
+ IF VALUE:
+ return 1
+ ELSE:
+ return 0
diff --git a/test cases/cython/3 cython_args/meson.build b/test cases/cython/3 cython_args/meson.build
new file mode 100644
index 0000000..e41d1b7
--- /dev/null
+++ b/test cases/cython/3 cython_args/meson.build
@@ -0,0 +1,30 @@
+project('cython_args', ['cython', 'c'])
+
+pymod = import('python')
+python = pymod.find_installation('python3')
+python_dep = python.dependency()
+if not python_dep.found()
+ error('MESON_SKIP_TEST: Python library not found.')
+endif
+
+mod = python.extension_module(
+ 'cythonargs',
+ files('cythonargs.pyx'),
+ cython_args: [
+ '--compile-time-env',
+ 'VALUE=1'
+ ],
+ dependencies: [python_dep]
+)
+
+test(
+ 'test',
+ python,
+ args: [
+ 'test.py'
+ ],
+ workdir: meson.current_source_dir(),
+ env: environment({
+ 'PYTHONPATH': meson.current_build_dir(),
+ })
+)
diff --git a/test cases/cython/3 cython_args/test.py b/test cases/cython/3 cython_args/test.py
new file mode 100644
index 0000000..bd8a152
--- /dev/null
+++ b/test cases/cython/3 cython_args/test.py
@@ -0,0 +1,3 @@
+import cythonargs
+
+assert cythonargs.test() == 1