summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz@archlinux.org>2023-03-06 21:37:29 -0500
committerEli Schwartz <eschwartz@archlinux.org>2023-05-02 19:28:35 -0400
commit6a1427401c76db73081e478c4ff49fcc75420de6 (patch)
treebb7790eb92ef6da31ffcff79da0fadb8d4c796c2
parent0e7fb07f915b7a2b04df209fbacd92aca19c87af (diff)
downloadmeson-6a1427401c76db73081e478c4ff49fcc75420de6.tar.gz
tests: add a python test for bytecode compilation
Some tweaks are added to the test case so that it supports python2 as well.
-rwxr-xr-xtest cases/python/2 extmodule/blaster.py.in2
-rw-r--r--test cases/python/2 extmodule/ext/meson.build6
-rw-r--r--test cases/python/2 extmodule/ext/nested/meson.build17
-rw-r--r--test cases/python/2 extmodule/ext/tachyon_module.c14
-rw-r--r--test cases/python/2 extmodule/ext/wrongdir/meson.build6
-rw-r--r--test cases/python/2 extmodule/meson.build7
-rw-r--r--test cases/python/2 extmodule/meson_options.txt1
-rwxr-xr-xtest cases/python/2 extmodule/subinst/printer.py3
-rwxr-xr-xtest cases/python/2 extmodule/subinst/submod/printer.py3
-rw-r--r--test cases/python/2 extmodule/test.json2
-rw-r--r--unittests/pythontests.py38
11 files changed, 91 insertions, 8 deletions
diff --git a/test cases/python/2 extmodule/blaster.py.in b/test cases/python/2 extmodule/blaster.py.in
index b690b4092..c93026f4f 100755
--- a/test cases/python/2 extmodule/blaster.py.in
+++ b/test cases/python/2 extmodule/blaster.py.in
@@ -8,4 +8,4 @@ if not isinstance(result, int):
raise SystemExit('Returned result not an integer.')
if result != 1:
- raise SystemExit(f'Returned result {result} is not 1.')
+ raise SystemExit('Returned result {} is not 1.'.format(result))
diff --git a/test cases/python/2 extmodule/ext/meson.build b/test cases/python/2 extmodule/ext/meson.build
index 14fa94abf..0fba9f582 100644
--- a/test cases/python/2 extmodule/ext/meson.build
+++ b/test cases/python/2 extmodule/ext/meson.build
@@ -4,6 +4,12 @@ pylib = py.extension_module('tachyon',
install: true,
)
+pylib2 = py2.extension_module('tachyon',
+ 'tachyon_module.c',
+ c_args: '-DMESON_MODULENAME="tachyon"',
+ install: true,
+)
+
subdir('nested')
subdir('wrongdir')
pypathdir = meson.current_build_dir()
diff --git a/test cases/python/2 extmodule/ext/nested/meson.build b/test cases/python/2 extmodule/ext/nested/meson.build
index 34c119273..4a7ec7675 100644
--- a/test cases/python/2 extmodule/ext/nested/meson.build
+++ b/test cases/python/2 extmodule/ext/nested/meson.build
@@ -13,3 +13,20 @@ py.install_sources(
pure: false,
subdir: 'nested',
)
+
+
+py2.extension_module('tachyon',
+ '../tachyon_module.c',
+ c_args: '-DMESON_MODULENAME="nested.tachyon"',
+ install: true,
+ subdir: 'nested'
+)
+py2.install_sources(
+ configure_file(
+ input: '../../blaster.py.in',
+ output: 'blaster.py',
+ configuration: {'tachyon_module': 'nested.tachyon'}
+ ),
+ pure: false,
+ subdir: 'nested',
+)
diff --git a/test cases/python/2 extmodule/ext/tachyon_module.c b/test cases/python/2 extmodule/ext/tachyon_module.c
index a5d7cdc98..68eda5380 100644
--- a/test cases/python/2 extmodule/ext/tachyon_module.c
+++ b/test cases/python/2 extmodule/ext/tachyon_module.c
@@ -1,5 +1,5 @@
/*
- Copyright 2016 The Meson development team
+ Copyright 2018 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.
@@ -27,7 +27,11 @@ static PyObject* phaserize(PyObject *self, PyObject *args) {
return NULL;
result = strcmp(message, "shoot") ? 0 : 1;
+#if PY_VERSION_HEX < 0x03000000
+ return PyInt_FromLong(result);
+#else
return PyLong_FromLong(result);
+#endif
}
static PyMethodDef TachyonMethods[] = {
@@ -36,9 +40,14 @@ static PyMethodDef TachyonMethods[] = {
{NULL, NULL, 0, NULL}
};
+#if PY_VERSION_HEX < 0x03000000
+PyMODINIT_FUNC inittachyon(void) {
+ Py_InitModule("tachyon", TachyonMethods);
+}
+#else
static struct PyModuleDef tachyonmodule = {
PyModuleDef_HEAD_INIT,
- MESON_MODULENAME,
+ "tachyon",
NULL,
-1,
TachyonMethods
@@ -47,3 +56,4 @@ static struct PyModuleDef tachyonmodule = {
PyMODINIT_FUNC PyInit_tachyon(void) {
return PyModule_Create(&tachyonmodule);
}
+#endif
diff --git a/test cases/python/2 extmodule/ext/wrongdir/meson.build b/test cases/python/2 extmodule/ext/wrongdir/meson.build
index 5074701ea..79b13eb77 100644
--- a/test cases/python/2 extmodule/ext/wrongdir/meson.build
+++ b/test cases/python/2 extmodule/ext/wrongdir/meson.build
@@ -4,3 +4,9 @@ py.extension_module('tachyon',
install: true,
install_dir: get_option('libdir')
)
+py2.extension_module('tachyon',
+ '../tachyon_module.c',
+ c_args: '-DMESON_MODULENAME="tachyon"',
+ install: true,
+ install_dir: get_option('libdir')
+)
diff --git a/test cases/python/2 extmodule/meson.build b/test cases/python/2 extmodule/meson.build
index 079463160..65f9177c0 100644
--- a/test cases/python/2 extmodule/meson.build
+++ b/test cases/python/2 extmodule/meson.build
@@ -10,6 +10,7 @@ endif
py_mod = import('python')
py = py_mod.find_installation()
+py2 = py_mod.find_installation('python2', required: get_option('python2'), disabler: true)
py_dep = py.dependency(required: false)
if not py_dep.found()
@@ -31,6 +32,12 @@ test('extmod',
py.install_sources(blaster, pure: false)
py.install_sources(blaster, subdir: 'pure')
+install_subdir('subinst', install_dir: py.get_install_dir(pure: false))
+
+py2.install_sources(blaster, pure: false)
+py2.install_sources(blaster, subdir: 'pure')
+install_subdir('subinst', install_dir: py2.get_install_dir(pure: false))
+
py3_pkg_dep = dependency('python3', method: 'pkg-config', required : false)
if py3_pkg_dep.found()
diff --git a/test cases/python/2 extmodule/meson_options.txt b/test cases/python/2 extmodule/meson_options.txt
new file mode 100644
index 000000000..76d3b6788
--- /dev/null
+++ b/test cases/python/2 extmodule/meson_options.txt
@@ -0,0 +1 @@
+option('python2', type: 'feature', value: 'disabled')
diff --git a/test cases/python/2 extmodule/subinst/printer.py b/test cases/python/2 extmodule/subinst/printer.py
new file mode 100755
index 000000000..8bc528dfe
--- /dev/null
+++ b/test cases/python/2 extmodule/subinst/printer.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python3
+
+print('subinst')
diff --git a/test cases/python/2 extmodule/subinst/submod/printer.py b/test cases/python/2 extmodule/subinst/submod/printer.py
new file mode 100755
index 000000000..2a4a61bc9
--- /dev/null
+++ b/test cases/python/2 extmodule/subinst/submod/printer.py
@@ -0,0 +1,3 @@
+#!/usr/bin/env python3
+
+print('subinst.submod')
diff --git a/test cases/python/2 extmodule/test.json b/test cases/python/2 extmodule/test.json
index 6bd119592..fcb3975a1 100644
--- a/test cases/python/2 extmodule/test.json
+++ b/test cases/python/2 extmodule/test.json
@@ -7,6 +7,8 @@
{ "type": "python_file", "file": "usr/@PYTHON_PLATLIB@/nested/blaster.py" },
{ "type": "python_lib", "file": "usr/@PYTHON_PLATLIB@/nested/tachyon" },
{ "type": "py_implib", "file": "usr/@PYTHON_PLATLIB@/nested/tachyon" },
+ { "type": "python_file", "file": "usr/@PYTHON_PLATLIB@/subinst/printer.py" },
+ { "type": "python_file", "file": "usr/@PYTHON_PLATLIB@/subinst/submod/printer.py" },
{ "type": "python_lib", "file": "usr/lib/tachyon" },
{ "type": "py_implib", "file": "usr/lib/tachyon" }
]
diff --git a/unittests/pythontests.py b/unittests/pythontests.py
index d49107f6d..6f3f07506 100644
--- a/unittests/pythontests.py
+++ b/unittests/pythontests.py
@@ -12,18 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-import os
-import unittest
-import pathlib
-import subprocess
+import glob, os, pathlib, shutil, subprocess, unittest
from run_tests import (
Backend
)
from .allplatformstests import git_init
-
from .baseplatformtests import BasePlatformTests
+
from mesonbuild.mesonlib import TemporaryDirectoryWinProof
class PythonTests(BasePlatformTests):
@@ -60,3 +57,34 @@ python = pymod.find_installation('python3', required: true)
git_init(dirstr)
self.init(dirstr)
subprocess.check_call(self.meson_command + ['dist', '-C', self.builddir], stdout=subprocess.DEVNULL)
+
+ def _test_bytecompile(self, py2=False):
+ testdir = os.path.join(self.src_root, 'test cases', 'python', '2 extmodule')
+
+ self.init(testdir, extra_args=['-Dpython2=auto', '-Dpython.bytecompile=1'])
+ self.build()
+ self.install()
+
+ count = 0
+ for root, dirs, files in os.walk(self.installdir):
+ for file in files:
+ realfile = os.path.join(root, file)
+ if file.endswith('.py'):
+ cached = glob.glob(realfile+'?') + glob.glob(os.path.join(root, '__pycache__', os.path.splitext(file)[0] + '*.pyc'))
+ self.assertEqual(len(cached), 2)
+ count += 1
+ # there are 5 files x 2 installations
+ if py2:
+ self.assertEqual(count, 10)
+ else:
+ self.assertEqual(count, 5)
+
+ def test_bytecompile_multi(self):
+ if not shutil.which('python2'):
+ raise self.skipTest('python2 not installed')
+ self._test_bytecompile(True)
+
+ def test_bytecompile_single(self):
+ if shutil.which('python2'):
+ raise self.skipTest('python2 installed, already tested')
+ self._test_bytecompile()