From 03805287e519b5f327239bf0b36e70200b01f6ba Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 10 Dec 2018 11:05:48 -0800 Subject: modules/python: Don't pass instance variable to instance method --- mesonbuild/modules/python.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 621d104..6af3374 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -301,7 +301,7 @@ class PythonInstallation(ExternalProgramHolder, InterpreterObject): self.link_libpython = info['link_libpython'] @permittedKwargs(mod_kwargs) - def extension_module(self, interpreter, state, args, kwargs): + def extension_module(self, state, args, kwargs): if 'subdir' in kwargs and 'install_dir' in kwargs: raise InvalidArguments('"subdir" and "install_dir" are mutually exclusive') @@ -334,14 +334,14 @@ class PythonInstallation(ExternalProgramHolder, InterpreterObject): kwargs['name_prefix'] = '' kwargs['name_suffix'] = suffix - return interpreter.func_shared_module(None, args, kwargs) + return self.interpreter.func_shared_module(None, args, kwargs) - def dependency(self, interpreter, state, args, kwargs): - dep = PythonDependency(self, interpreter.environment, kwargs) - return interpreter.holderify(dep) + def dependency(self, state, args, kwargs): + dep = PythonDependency(self, self.interpreter.environment, kwargs) + return self.interpreter.holderify(dep) @permittedKwargs(['pure', 'subdir']) - def install_sources(self, interpreter, state, args, kwargs): + def install_sources(self, state, args, kwargs): pure = kwargs.pop('pure', False) if not isinstance(pure, bool): raise InvalidArguments('"pure" argument must be a boolean.') @@ -355,7 +355,7 @@ class PythonInstallation(ExternalProgramHolder, InterpreterObject): else: kwargs['install_dir'] = os.path.join(self.platlib_install_path, subdir) - return interpreter.func_install_data(None, args, kwargs) + return self.interpreter.func_install_data(None, args, kwargs) @noPosargs @permittedKwargs(['pure', 'subdir']) @@ -450,11 +450,11 @@ class PythonInstallation(ExternalProgramHolder, InterpreterObject): if not getattr(fn, 'no-args-flattening', False): args = flatten(args) + value = fn(None, args, kwargs) + if method_name in ['extension_module', 'dependency', 'install_sources']: - value = fn(self.interpreter, None, args, kwargs) return self.interpreter.holderify(value) elif method_name in ['has_variable', 'get_variable', 'has_path', 'get_path', 'found', 'language_version', 'get_install_dir']: - value = fn(None, args, kwargs) return self.interpreter.module_method_callback(value) else: raise InvalidArguments('Python object does not have method %s.' % method_name) -- cgit v1.1 From 3d322b3381c0ba2ea71792d5c6c5ad32e205d65a Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 10 Dec 2018 11:12:53 -0800 Subject: modules/python: don't inherit from InterpreterObject Because ExternalProjectHolder already does --- mesonbuild/modules/python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 6af3374..0707ed1 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -284,9 +284,9 @@ print (json.dumps ({ })) ''' -class PythonInstallation(ExternalProgramHolder, InterpreterObject): + +class PythonInstallation(ExternalProgramHolder): def __init__(self, interpreter, python, info): - InterpreterObject.__init__(self) ExternalProgramHolder.__init__(self, python) self.interpreter = interpreter prefix = self.interpreter.environment.coredata.get_builtin_option('prefix') -- cgit v1.1 From d536a6f01896ce12d9729f1060f9744753a8e140 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 10 Dec 2018 11:34:36 -0800 Subject: modules/python: Refactor the PythonInstallation object Into a generic interpreter object. This isn't a module, it's an object returned by a module, it also happens to be a special case of an ExternalProgram, which is a normal interpreter object. Let's treat it like one. --- mesonbuild/modules/python.py | 73 ++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 40 deletions(-) diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 0707ed1..e408a71 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -289,6 +289,7 @@ class PythonInstallation(ExternalProgramHolder): def __init__(self, interpreter, python, info): ExternalProgramHolder.__init__(self, python) self.interpreter = interpreter + self.subproject = self.interpreter.subproject prefix = self.interpreter.environment.coredata.get_builtin_option('prefix') self.variables = info['variables'] self.paths = info['paths'] @@ -299,9 +300,24 @@ class PythonInstallation(ExternalProgramHolder): self.platform = info['platform'] self.is_pypy = info['is_pypy'] self.link_libpython = info['link_libpython'] + self.methods.update({ + 'extension_module': self.extension_module_method, + 'dependency': self.dependency_method, + 'install_sources': self.install_sources_method, + 'get_install_dir': self.get_install_dir_method, + 'language_version': self.language_version_method, + 'found': self.found_method, + 'has_path': self.has_path_method, + 'get_path': self.get_path_method, + 'has_variable': self.has_variable_method, + 'get_variable': self.get_variable_method, + }) + + # Remove this so that an API change is not part of a refactor + del self.methods['path_method'] @permittedKwargs(mod_kwargs) - def extension_module(self, state, args, kwargs): + def extension_module_method(self, args, kwargs): if 'subdir' in kwargs and 'install_dir' in kwargs: raise InvalidArguments('"subdir" and "install_dir" are mutually exclusive') @@ -320,7 +336,7 @@ class PythonInstallation(ExternalProgramHolder): for holder in mesonlib.extract_as_list(kwargs, 'dependencies'): dep = holder.held_object if isinstance(dep, PythonDependency): - holder = interpreter.holderify(dep.get_partial_dependency(compile_args=True)) + holder = self.interpreter.holderify(dep.get_partial_dependency(compile_args=True)) new_deps.append(holder) kwargs['dependencies'] = new_deps @@ -336,12 +352,12 @@ class PythonInstallation(ExternalProgramHolder): return self.interpreter.func_shared_module(None, args, kwargs) - def dependency(self, state, args, kwargs): + def dependency_method(self, args, kwargs): dep = PythonDependency(self, self.interpreter.environment, kwargs) return self.interpreter.holderify(dep) @permittedKwargs(['pure', 'subdir']) - def install_sources(self, state, args, kwargs): + def install_sources_method(self, args, kwargs): pure = kwargs.pop('pure', False) if not isinstance(pure, bool): raise InvalidArguments('"pure" argument must be a boolean.') @@ -355,11 +371,11 @@ class PythonInstallation(ExternalProgramHolder): else: kwargs['install_dir'] = os.path.join(self.platlib_install_path, subdir) - return self.interpreter.func_install_data(None, args, kwargs) + return self.interpreter.holderify(self.interpreter.func_install_data(None, args, kwargs)) @noPosargs @permittedKwargs(['pure', 'subdir']) - def get_install_dir(self, node, args, kwargs): + def get_install_dir_method(self, args, kwargs): pure = kwargs.pop('pure', True) if not isinstance(pure, bool): raise InvalidArguments('"pure" argument must be a boolean.') @@ -373,30 +389,25 @@ class PythonInstallation(ExternalProgramHolder): else: res = os.path.join(self.platlib_install_path, subdir) - return ModuleReturnValue(res, []) + return self.interpreter.module_method_callback(ModuleReturnValue(res, [])) @noPosargs @noKwargs - def language_version(self, node, args, kwargs): - return ModuleReturnValue(self.version, []) - - @noPosargs - @noKwargs - def found(self, node, args, kwargs): - return ModuleReturnValue(True, []) + def language_version_method(self, args, kwargs): + return self.interpreter.module_method_callback(ModuleReturnValue(self.version, [])) @noKwargs - def has_path(self, node, args, kwargs): + def has_path_method(self, args, kwargs): if len(args) != 1: raise InvalidArguments('has_path takes exactly one positional argument.') path_name = args[0] if not isinstance(path_name, str): raise InvalidArguments('has_path argument must be a string.') - return ModuleReturnValue(path_name in self.paths, []) + return self.interpreter.module_method_callback(ModuleReturnValue(path_name in self.paths, [])) @noKwargs - def get_path(self, node, args, kwargs): + def get_path_method(self, args, kwargs): if len(args) not in (1, 2): raise InvalidArguments('get_path must have one or two arguments.') path_name = args[0] @@ -411,20 +422,20 @@ class PythonInstallation(ExternalProgramHolder): else: raise InvalidArguments('{} is not a valid path name'.format(path_name)) - return ModuleReturnValue(path, []) + return self.interpreter.module_method_callback(ModuleReturnValue(path, [])) @noKwargs - def has_variable(self, node, args, kwargs): + def has_variable_method(self, args, kwargs): if len(args) != 1: raise InvalidArguments('has_variable takes exactly one positional argument.') var_name = args[0] if not isinstance(var_name, str): raise InvalidArguments('has_variable argument must be a string.') - return ModuleReturnValue(var_name in self.variables, []) + return self.interpreter.module_method_callback(ModuleReturnValue(var_name in self.variables, [])) @noKwargs - def get_variable(self, node, args, kwargs): + def get_variable_method(self, args, kwargs): if len(args) not in (1, 2): raise InvalidArguments('get_variable must have one or two arguments.') var_name = args[0] @@ -439,25 +450,7 @@ class PythonInstallation(ExternalProgramHolder): else: raise InvalidArguments('{} is not a valid variable name'.format(var_name)) - return ModuleReturnValue(var, []) - - def method_call(self, method_name, args, kwargs): - try: - fn = getattr(self, method_name) - except AttributeError: - raise InvalidArguments('Python object does not have method %s.' % method_name) - - if not getattr(fn, 'no-args-flattening', False): - args = flatten(args) - - value = fn(None, args, kwargs) - - if method_name in ['extension_module', 'dependency', 'install_sources']: - return self.interpreter.holderify(value) - elif method_name in ['has_variable', 'get_variable', 'has_path', 'get_path', 'found', 'language_version', 'get_install_dir']: - return self.interpreter.module_method_callback(value) - else: - raise InvalidArguments('Python object does not have method %s.' % method_name) + return self.interpreter.module_method_callback(ModuleReturnValue(var, [])) class PythonModule(ExtensionModule): -- cgit v1.1 From f6405adc2de0c6cc494b4957df9d796b76195ec4 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 10 Dec 2018 13:57:41 -0800 Subject: modules/python: Add path method This would normally be exposed by the ExternalProgramHolder, but wasn't due to the implementation of the PythonInstallation module. Because of that I've duplicated the method so that we can add the FeatureNew decorator. Fixes #4070 --- mesonbuild/modules/python.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index e408a71..c51b412 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -311,11 +311,9 @@ class PythonInstallation(ExternalProgramHolder): 'get_path': self.get_path_method, 'has_variable': self.has_variable_method, 'get_variable': self.get_variable_method, + 'path': self.path_method, }) - # Remove this so that an API change is not part of a refactor - del self.methods['path_method'] - @permittedKwargs(mod_kwargs) def extension_module_method(self, args, kwargs): if 'subdir' in kwargs and 'install_dir' in kwargs: @@ -452,6 +450,12 @@ class PythonInstallation(ExternalProgramHolder): return self.interpreter.module_method_callback(ModuleReturnValue(var, [])) + @noPosargs + @noKwargs + @FeatureNew('Python module path method', '0.50.0') + def path_method(self, args, kwargs): + return super().path_method(args, kwargs) + class PythonModule(ExtensionModule): -- cgit v1.1 From 978e6fb88f926da14314a3563de13197a092cc9f Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 10 Dec 2018 14:33:58 -0800 Subject: tests: Add tests for the python module This doesn't touch everything as it's just based on the python3 module tests, ported to the python module. It's still better than the one very basic test in the unit test module. --- run_project_tests.py | 1 + test cases/python/1 basic/gluon/__init__.py | 0 test cases/python/1 basic/gluon/gluonator.py | 2 + test cases/python/1 basic/meson.build | 26 +++++++++++ test cases/python/1 basic/prog.py | 9 ++++ test cases/python/1 basic/subdir/meson.build | 4 ++ test cases/python/1 basic/subdir/subprog.py | 12 +++++ test cases/python/2 extmodule/blaster.py | 14 ++++++ test cases/python/2 extmodule/ext/meson.build | 6 +++ test cases/python/2 extmodule/ext/tachyon_module.c | 49 +++++++++++++++++++++ test cases/python/2 extmodule/meson.build | 28 ++++++++++++ test cases/python/3 cython/cytest.py | 23 ++++++++++ test cases/python/3 cython/libdir/cstorer.pxd | 9 ++++ test cases/python/3 cython/libdir/meson.build | 11 +++++ test cases/python/3 cython/libdir/storer.c | 24 ++++++++++ test cases/python/3 cython/libdir/storer.h | 8 ++++ test cases/python/3 cython/libdir/storer.pyx | 16 +++++++ test cases/python/3 cython/meson.build | 20 +++++++++ .../4 custom target depends extmodule/blaster.py | 32 ++++++++++++++ .../ext/lib/meson-tachyonlib.c | 8 ++++ .../ext/lib/meson-tachyonlib.h | 6 +++ .../ext/lib/meson.build | 4 ++ .../ext/meson.build | 6 +++ .../ext/tachyon_module.c | 51 ++++++++++++++++++++++ .../4 custom target depends extmodule/meson.build | 35 +++++++++++++++ 25 files changed, 404 insertions(+) create mode 100644 test cases/python/1 basic/gluon/__init__.py create mode 100644 test cases/python/1 basic/gluon/gluonator.py create mode 100644 test cases/python/1 basic/meson.build create mode 100755 test cases/python/1 basic/prog.py create mode 100644 test cases/python/1 basic/subdir/meson.build create mode 100755 test cases/python/1 basic/subdir/subprog.py create mode 100755 test cases/python/2 extmodule/blaster.py create mode 100644 test cases/python/2 extmodule/ext/meson.build create mode 100644 test cases/python/2 extmodule/ext/tachyon_module.c create mode 100644 test cases/python/2 extmodule/meson.build create mode 100755 test cases/python/3 cython/cytest.py create mode 100644 test cases/python/3 cython/libdir/cstorer.pxd create mode 100644 test cases/python/3 cython/libdir/meson.build create mode 100644 test cases/python/3 cython/libdir/storer.c create mode 100644 test cases/python/3 cython/libdir/storer.h create mode 100644 test cases/python/3 cython/libdir/storer.pyx create mode 100644 test cases/python/3 cython/meson.build create mode 100644 test cases/python/4 custom target depends extmodule/blaster.py create mode 100644 test cases/python/4 custom target depends extmodule/ext/lib/meson-tachyonlib.c create mode 100644 test cases/python/4 custom target depends extmodule/ext/lib/meson-tachyonlib.h create mode 100644 test cases/python/4 custom target depends extmodule/ext/lib/meson.build create mode 100644 test cases/python/4 custom target depends extmodule/ext/meson.build create mode 100644 test cases/python/4 custom target depends extmodule/ext/tachyon_module.c create mode 100644 test cases/python/4 custom target depends extmodule/meson.build diff --git a/run_project_tests.py b/run_project_tests.py index 0d64f47..02897ce 100755 --- a/run_project_tests.py +++ b/run_project_tests.py @@ -543,6 +543,7 @@ def detect_tests_to_run(): ('fortran', 'fortran', backend is not Backend.ninja or not shutil.which('gfortran')), ('swift', 'swift', backend not in (Backend.ninja, Backend.xcode) or not shutil.which('swiftc')), ('python3', 'python3', backend is not Backend.ninja), + ('python', 'python', backend is not Backend.ninja), ('fpga', 'fpga', shutil.which('yosys') is None), ('frameworks', 'frameworks', False), ('nasm', 'nasm', False), diff --git a/test cases/python/1 basic/gluon/__init__.py b/test cases/python/1 basic/gluon/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test cases/python/1 basic/gluon/gluonator.py b/test cases/python/1 basic/gluon/gluonator.py new file mode 100644 index 0000000..b53d6de --- /dev/null +++ b/test cases/python/1 basic/gluon/gluonator.py @@ -0,0 +1,2 @@ +def gluoninate(): + return 42 diff --git a/test cases/python/1 basic/meson.build b/test cases/python/1 basic/meson.build new file mode 100644 index 0000000..f9a7433 --- /dev/null +++ b/test cases/python/1 basic/meson.build @@ -0,0 +1,26 @@ +project('python sample', 'c') + +py_mod = import('python') +py = py_mod.find_installation() + +py_version = py.language_version() +if py_version.version_compare('< 3.2') + error('MESON_SKIP_TEST python 3 required for tests') +endif + +py_purelib = py.get_path('purelib') +if not py_purelib.endswith('site-packages') + error('Python3 purelib path seems invalid? ' + py_purelib) +endif + +# could be 'lib64' or 'Lib' on some systems +py_platlib = py.get_path('platlib') +if not py_platlib.endswith('site-packages') + error('Python3 platlib path seems invalid? ' + py_platlib) +endif + +main = files('prog.py') + +test('toplevel', py, args : main) + +subdir('subdir') diff --git a/test cases/python/1 basic/prog.py b/test cases/python/1 basic/prog.py new file mode 100755 index 0000000..9d95aea --- /dev/null +++ b/test cases/python/1 basic/prog.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 + +from gluon import gluonator +import sys + +print('Running mainprog from root dir.') + +if gluonator.gluoninate() != 42: + sys.exit(1) diff --git a/test cases/python/1 basic/subdir/meson.build b/test cases/python/1 basic/subdir/meson.build new file mode 100644 index 0000000..66957c1 --- /dev/null +++ b/test cases/python/1 basic/subdir/meson.build @@ -0,0 +1,4 @@ +test('subdir', + py, + args : files('subprog.py'), + env : 'PYTHONPATH=' + meson.source_root()) diff --git a/test cases/python/1 basic/subdir/subprog.py b/test cases/python/1 basic/subdir/subprog.py new file mode 100755 index 0000000..08652f0 --- /dev/null +++ b/test cases/python/1 basic/subdir/subprog.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 + +# In order to run this program, PYTHONPATH must be set to +# point to source root. + +from gluon import gluonator +import sys + +print('Running mainprog from subdir.') + +if gluonator.gluoninate() != 42: + sys.exit(1) diff --git a/test cases/python/2 extmodule/blaster.py b/test cases/python/2 extmodule/blaster.py new file mode 100755 index 0000000..7e1eae6 --- /dev/null +++ b/test cases/python/2 extmodule/blaster.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +import tachyon +import sys + +result = tachyon.phaserize('shoot') + +if not isinstance(result, int): + print('Returned result not an integer.') + sys.exit(1) + +if result != 1: + print('Returned result {} is not 1.'.format(result)) + sys.exit(1) diff --git a/test cases/python/2 extmodule/ext/meson.build b/test cases/python/2 extmodule/ext/meson.build new file mode 100644 index 0000000..b13bb32 --- /dev/null +++ b/test cases/python/2 extmodule/ext/meson.build @@ -0,0 +1,6 @@ +pylib = py.extension_module('tachyon', + 'tachyon_module.c', + dependencies : py_dep, +) + +pypathdir = meson.current_build_dir() diff --git a/test cases/python/2 extmodule/ext/tachyon_module.c b/test cases/python/2 extmodule/ext/tachyon_module.c new file mode 100644 index 0000000..b2592e4 --- /dev/null +++ b/test cases/python/2 extmodule/ext/tachyon_module.c @@ -0,0 +1,49 @@ +/* + Copyright 2016 The Meson development team + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/* A very simple Python extension module. */ + +#include +#include + +static PyObject* phaserize(PyObject *self, PyObject *args) { + const char *message; + int result; + + if(!PyArg_ParseTuple(args, "s", &message)) + return NULL; + + result = strcmp(message, "shoot") ? 0 : 1; + return PyLong_FromLong(result); +} + +static PyMethodDef TachyonMethods[] = { + {"phaserize", phaserize, METH_VARARGS, + "Shoot tachyon cannons."}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef tachyonmodule = { + PyModuleDef_HEAD_INIT, + "tachyon", + NULL, + -1, + TachyonMethods +}; + +PyMODINIT_FUNC PyInit_tachyon(void) { + return PyModule_Create(&tachyonmodule); +} diff --git a/test cases/python/2 extmodule/meson.build b/test cases/python/2 extmodule/meson.build new file mode 100644 index 0000000..b4eb960 --- /dev/null +++ b/test cases/python/2 extmodule/meson.build @@ -0,0 +1,28 @@ +project('Python extension module', 'c', + default_options : ['buildtype=release']) +# Because Windows Python ships only with optimized libs, +# we must build this project the same way. + +py_mod = import('python') +py = py_mod.find_installation() +py_dep = py.dependency() + +if py_dep.found() + subdir('ext') + + test('extmod', + py, + args : files('blaster.py'), + env : ['PYTHONPATH=' + pypathdir]) + + # Check we can apply a version constraint + dependency('python3', version: '>=@0@'.format(py_dep.version())) + +else + error('MESON_SKIP_TEST: Python3 libraries not found, skipping test.') +endif + +py3_pkg_dep = dependency('python3', method: 'pkg-config', required : false) +if py3_pkg_dep.found() + python_lib_dir = py3_pkg_dep.get_pkgconfig_variable('libdir') +endif diff --git a/test cases/python/3 cython/cytest.py b/test cases/python/3 cython/cytest.py new file mode 100755 index 0000000..43443dc --- /dev/null +++ b/test cases/python/3 cython/cytest.py @@ -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 diff --git a/test cases/python/3 cython/libdir/cstorer.pxd b/test cases/python/3 cython/libdir/cstorer.pxd new file mode 100644 index 0000000..7b730fc --- /dev/null +++ b/test cases/python/3 cython/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/python/3 cython/libdir/meson.build b/test cases/python/3 cython/libdir/meson.build new file mode 100644 index 0000000..2b6ebc7 --- /dev/null +++ b/test cases/python/3 cython/libdir/meson.build @@ -0,0 +1,11 @@ +pyx_c = custom_target('storer_pyx', + output : 'storer_pyx.c', + input : 'storer.pyx', + command : [cython, '@INPUT@', '-o', '@OUTPUT@'], +) + +slib = py3.extension_module('storer', + 'storer.c', pyx_c, + dependencies : py3_dep) + +pydir = meson.current_build_dir() diff --git a/test cases/python/3 cython/libdir/storer.c b/test cases/python/3 cython/libdir/storer.c new file mode 100644 index 0000000..0199bb8 --- /dev/null +++ b/test cases/python/3 cython/libdir/storer.c @@ -0,0 +1,24 @@ +#include"storer.h" +#include + +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/python/3 cython/libdir/storer.h b/test cases/python/3 cython/libdir/storer.h new file mode 100644 index 0000000..4f71917 --- /dev/null +++ b/test cases/python/3 cython/libdir/storer.h @@ -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); diff --git a/test cases/python/3 cython/libdir/storer.pyx b/test cases/python/3 cython/libdir/storer.pyx new file mode 100644 index 0000000..ed551dc --- /dev/null +++ b/test cases/python/3 cython/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/python/3 cython/meson.build b/test cases/python/3 cython/meson.build new file mode 100644 index 0000000..194920b --- /dev/null +++ b/test cases/python/3 cython/meson.build @@ -0,0 +1,20 @@ +project('cython', 'c', + default_options : ['warning_level=3']) + +cython = find_program('cython3', required : false) +py3_dep = dependency('python3', required : false) + +if cython.found() and py3_dep.found() + py_mod = import('python') + py3 = py_mod.find_installation() + py3_dep = py3.dependency() + subdir('libdir') + + test('cython tester', + py3, + args : files('cytest.py'), + env : ['PYTHONPATH=' + pydir] + ) +else + error('MESON_SKIP_TEST: Cython3 or Python3 libraries not found, skipping test.') +endif diff --git a/test cases/python/4 custom target depends extmodule/blaster.py b/test cases/python/4 custom target depends extmodule/blaster.py new file mode 100644 index 0000000..6106f6b --- /dev/null +++ b/test cases/python/4 custom target depends extmodule/blaster.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +import os +import sys +import argparse + +from pathlib import Path + +filedir = Path(os.path.dirname(__file__)).resolve() +if list(filedir.glob('ext/*tachyon*')): + sys.path.insert(0, (filedir / 'ext').as_posix()) + +import tachyon + +parser = argparse.ArgumentParser() +parser.add_argument('-o', dest='output', default=None) + +options = parser.parse_args(sys.argv[1:]) + +result = tachyon.phaserize('shoot') + +if options.output: + with open(options.output, 'w') as f: + f.write('success') + +if not isinstance(result, int): + print('Returned result not an integer.') + sys.exit(1) + +if result != 1: + print('Returned result {} is not 1.'.format(result)) + sys.exit(1) diff --git a/test cases/python/4 custom target depends extmodule/ext/lib/meson-tachyonlib.c b/test cases/python/4 custom target depends extmodule/ext/lib/meson-tachyonlib.c new file mode 100644 index 0000000..aeff296 --- /dev/null +++ b/test cases/python/4 custom target depends extmodule/ext/lib/meson-tachyonlib.c @@ -0,0 +1,8 @@ +#ifdef _MSC_VER +__declspec(dllexport) +#endif +const char* +tachyon_phaser_command (void) +{ + return "shoot"; +} diff --git a/test cases/python/4 custom target depends extmodule/ext/lib/meson-tachyonlib.h b/test cases/python/4 custom target depends extmodule/ext/lib/meson-tachyonlib.h new file mode 100644 index 0000000..dca71d3 --- /dev/null +++ b/test cases/python/4 custom target depends extmodule/ext/lib/meson-tachyonlib.h @@ -0,0 +1,6 @@ +#pragma once + +#ifdef _MSC_VER +__declspec(dllimport) +#endif +const char* tachyon_phaser_command (void); diff --git a/test cases/python/4 custom target depends extmodule/ext/lib/meson.build b/test cases/python/4 custom target depends extmodule/ext/lib/meson.build new file mode 100644 index 0000000..b1f8938 --- /dev/null +++ b/test cases/python/4 custom target depends extmodule/ext/lib/meson.build @@ -0,0 +1,4 @@ +libtachyon = shared_library('tachyonlib', 'meson-tachyonlib.c') + +libtachyon_dep = declare_dependency(link_with : libtachyon, + include_directories : include_directories('.')) diff --git a/test cases/python/4 custom target depends extmodule/ext/meson.build b/test cases/python/4 custom target depends extmodule/ext/meson.build new file mode 100644 index 0000000..1bb275d --- /dev/null +++ b/test cases/python/4 custom target depends extmodule/ext/meson.build @@ -0,0 +1,6 @@ +subdir('lib') + +pylib = py3.extension_module('tachyon', + 'tachyon_module.c', + dependencies : [libtachyon_dep, py3_dep], +) diff --git a/test cases/python/4 custom target depends extmodule/ext/tachyon_module.c b/test cases/python/4 custom target depends extmodule/ext/tachyon_module.c new file mode 100644 index 0000000..b48032b --- /dev/null +++ b/test cases/python/4 custom target depends extmodule/ext/tachyon_module.c @@ -0,0 +1,51 @@ +/* + Copyright 2016 The Meson development team + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/* A very simple Python extension module. */ + +#include +#include + +#include "meson-tachyonlib.h" + +static PyObject* phaserize(PyObject *self, PyObject *args) { + const char *message; + int result; + + if(!PyArg_ParseTuple(args, "s", &message)) + return NULL; + + result = strcmp(message, tachyon_phaser_command()) ? 0 : 1; + return PyLong_FromLong(result); +} + +static PyMethodDef TachyonMethods[] = { + {"phaserize", phaserize, METH_VARARGS, + "Shoot tachyon cannons."}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef tachyonmodule = { + PyModuleDef_HEAD_INIT, + "tachyon", + NULL, + -1, + TachyonMethods +}; + +PyMODINIT_FUNC PyInit_tachyon(void) { + return PyModule_Create(&tachyonmodule); +} diff --git a/test cases/python/4 custom target depends extmodule/meson.build b/test cases/python/4 custom target depends extmodule/meson.build new file mode 100644 index 0000000..4e2aff0 --- /dev/null +++ b/test cases/python/4 custom target depends extmodule/meson.build @@ -0,0 +1,35 @@ +project('Python extension module', 'c', + default_options : ['buildtype=release']) +# Because Windows Python ships only with optimized libs, +# we must build this project the same way. + +py_mod = import('python') +py3 = py_mod.find_installation() +py3_dep = py3.dependency(required : false) + +# Copy to the builddir so that blaster.py can find the built tachyon module +# FIXME: We should automatically detect this case and append the correct paths +# to PYTHONLIBDIR +blaster_py = configure_file(input : 'blaster.py', + output : 'blaster.py', + copy : true) + +check_exists = ''' +import os, sys +with open(sys.argv[1], 'rb') as f: + assert(f.read() == b'success') +''' +if py3_dep.found() + subdir('ext') + + out_txt = custom_target('tachyon flux', + input : blaster_py, + output : 'out.txt', + command : [py3, '@INPUT@', '-o', '@OUTPUT@'], + depends : pylib, + build_by_default: true) + + test('flux', py3, args : ['-c', check_exists, out_txt]) +else + error('MESON_SKIP_TEST: Python3 libraries not found, skipping test.') +endif -- cgit v1.1