Debian Codegraph @

unittests/pythontests.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2016-2021 The Meson development team
3
4 import glob, os, pathlib, shutil, subprocess, sys, unittest
5
6 from run_tests import (
7 Backend
8 )
9
10 from .allplatformstests import git_init
11 from .baseplatformtests import BasePlatformTests
12 from .helpers import *
13
14 from mesonbuild.compilers.detect import detect_c_compiler
15 from mesonbuild.mesonlib import MachineChoice, TemporaryDirectoryWinProof, is_windows
16 from mesonbuild.modules.python import PythonModule
17
18 class PythonTests(BasePlatformTests):
19 '''
20 Tests that verify compilation of python extension modules
21 '''
22
23 def test_bad_versions(self):
24 if self.backend is not Backend.ninja:
25 raise unittest.SkipTest(f'Skipping python tests with {self.backend.name} backend')
26
27 testdir = os.path.join(self.src_root, 'test cases', 'python', '8 different python versions')
28
29 # The test is configured to error out with MESON_SKIP_TEST
30 # in case it could not find python
31 with self.assertRaises(unittest.SkipTest):
32 self.init(testdir, extra_args=['-Dpython=not-python'])
33 self.wipe()
34
35 # While dir is an external command on both Windows and Linux,
36 # it certainly isn't python
37 with self.assertRaises(unittest.SkipTest):
38 self.init(testdir, extra_args=['-Dpython=dir'])
39 self.wipe()
40
41 def test_dist(self):
42 with TemporaryDirectoryWinProof() as dirstr:
43 dirobj = pathlib.Path(dirstr)
44 mesonfile = dirobj / 'meson.build'
45 mesonfile.write_text('''project('test', 'c', version: '1')
46 pymod = import('python')
47 python = pymod.find_installation('python3', required: true)
48 ''', encoding='utf-8')
49 git_init(dirstr)
50 self.init(dirstr)
51 subprocess.check_call(self.meson_command + ['dist', '-C', self.builddir], stdout=subprocess.DEVNULL)
52
53 def _test_bytecompile(self, py2=False):
54 testdir = os.path.join(self.src_root, 'test cases', 'python', '2 extmodule')
55
56 env = get_fake_env(testdir, self.builddir, self.prefix)
57 cc = detect_c_compiler(env, MachineChoice.HOST)
58
59 self.init(testdir, extra_args=['-Dpython2=auto', '-Dpython.bytecompile=1'])
60 self.build()
61 self.install()
62
63 count = 0
64 for root, dirs, files in os.walk(self.installdir):
65 for file in files:
66 realfile = os.path.join(root, file)
67 if file.endswith('.py'):
68 # FIXME: relpath must be adjusted for windows path behaviour
69 if getattr(sys, "pycache_prefix", None) is not None:
70 root = os.path.join(sys.pycache_prefix, os.path.relpath(root, '/'))
71 else:
72 root = os.path.join(root, '__pycache__')
73 cached = glob.glob(realfile+'?') + glob.glob(os.path.join(root, os.path.splitext(file)[0] + '*.pyc'))
74 if py2 and cc.get_id() == 'msvc':
75 # MSVC python installs python2/python3 into the same directory
76 self.assertLength(cached, 4)
77 else:
78 self.assertLength(cached, 2)
79 count += 1
80 # there are 5 files x 2 installations
81 if py2 and not cc.get_id() == 'msvc':
82 self.assertEqual(count, 10)
83 else:
84 self.assertEqual(count, 5)
85
86 def test_bytecompile_multi(self):
87 if not shutil.which('python2') and not PythonModule._get_win_pythonpath('python2'):
88 raise self.skipTest('python2 not installed')
89 self._test_bytecompile(True)
90
91 def test_bytecompile_single(self):
92 if shutil.which('python2') or PythonModule._get_win_pythonpath('python2'):
93 raise self.skipTest('python2 installed, already tested')
94 self._test_bytecompile()
95
96 def test_limited_api_linked_correct_lib(self):
97 if not is_windows():
98 return self.skipTest('Test only run on Windows.')
99
100 testdir = os.path.join(self.src_root, 'test cases', 'python', '9 extmodule limited api')
101
102 self.init(testdir)
103 self.build()
104
105 from importlib.machinery import EXTENSION_SUFFIXES
106 limited_suffix = EXTENSION_SUFFIXES[1]
107
108 limited_library_path = os.path.join(self.builddir, f'limited{limited_suffix}')
109 self.assertPathExists(limited_library_path)
110
111 limited_dep_name = 'python3.dll'
112 if shutil.which('dumpbin'):
113 # MSVC
114 output = subprocess.check_output(['dumpbin', '/DEPENDENTS', limited_library_path],
115 stderr=subprocess.STDOUT)
116 self.assertIn(limited_dep_name, output.decode())
117 elif shutil.which('objdump'):
118 # mingw
119 output = subprocess.check_output(['objdump', '-p', limited_library_path],
120 stderr=subprocess.STDOUT)
121 self.assertIn(limited_dep_name, output.decode())
122 else:
123 raise self.skipTest('Test needs either dumpbin(MSVC) or objdump(mingw).')