File: conftest.py

package info (click to toggle)
sip6 6.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,888 kB
  • sloc: ansic: 192,450; python: 20,951; makefile: 25; cpp: 20
file content (425 lines) | stat: -rw-r--r-- 12,338 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# SPDX-License-Identifier: BSD-2-Clause

# Copyright (c) 2026 Phil Thompson <phil@riverbankcomputing.com>


import glob
import importlib
import os
import shutil
import subprocess
import sys
import tarfile

import pytest


# The different ABI versions.
ABI_VERSIONS = (12, 13)


def pytest_addoption(parser):
    """ Add the required command line option to specify the ABI version to
    test.
    """

    parser.addoption("--sip-abi-version",
            help="Specify the sip module ABI version", type=int,
            choices=ABI_VERSIONS, default=ABI_VERSIONS[-1])


@pytest.fixture
def abi_version(module):
    """ This fixture extracts the ABI version used to build a test module.  The
    value is the ABI major version.
    """

    return module.SIP_ABI_VERSION >> 16


@pytest.fixture(scope='module')
def code(request):
    """ This fixture generates the C/C++ code of a test module but does not try
    to compile it.  The value is True if the code was successfully generated
    and False otherwise.
    """

    # Get the configuration details.
    abi_version, exceptions, package, sip_module_configuration, tags, test_dir = _get_configuration_details(request)

    # Try to generate the code of the test module.
    try:
        _build_test_module(_get_only_sip_file(test_dir), test_dir, abi_version,
                None, exceptions, tags, no_compile=True)
    except subprocess.CalledProcessError:
        return False

    return True


@pytest.fixture(scope='module')
def module(request):
    """ The fixture is an appropriately built and imported wrapped module. """

    # Get the configuration details.
    abi_version, exceptions, package, sip_module_configuration, tags, test_dir = _get_configuration_details(request)

    # Build the test module.
    module_name = _build_test_module(_get_only_sip_file(test_dir), test_dir,
            abi_version, package, exceptions, tags)

    # Build a sip module if required.
    sip_module_name = _build_sip_module(test_dir, abi_version, package,
            sip_module_configuration) if package else None

    # Import the module.
    importlib.import_module(module_name)

    # The fixture is the module object.
    yield sys.modules[module_name]

    # Unload each module.
    del sys.modules[module_name]

    if sip_module_name is not None:
        try:
            del sys.modules[sip_module_name]
        except KeyError:
            pass


@pytest.fixture(scope='module')
def package(request):
    """ The fixture is a package containing one or more appropriately built
    wrapped modules and a separate sip module.
    """

    # Get the configuration details.
    abi_version, exceptions, package, sip_module_configuration, tags, test_dir = _get_configuration_details(request)

    if not package:
        raise ValueError("a value for 'cfg_package' must be specified")

    module_names = [package]

    # Remove any previous package directory.
    shutil.rmtree(os.path.join(test_dir, package), ignore_errors=True)

    # Build each module in the package.
    for sip_file in _get_sip_files(test_dir):
        module_name = _build_test_module(sip_file, test_dir, abi_version,
                package, exceptions, tags)
        module_names.append(module_name)

    # Build the sip module.
    sip_module_name = _build_sip_module(test_dir, abi_version, package,
            sip_module_configuration)

    # Import the modules.
    for module_name in module_names:
        importlib.import_module(module_name)

    # The fixture is the package object.
    yield sys.modules[package]

    # Unload each module.
    for module_name in module_names:
        del sys.modules[module_name]

    del sys.modules[sip_module_name]


class VirtualHook:
    """ The implementation of a virtual hook. """

    def __init__(self):
        """ Initialise the hook. """

        self._exc = None

    def catch(self, xtype, xvalue, xtb):
        """ The replacement exceptionhook. """

        # Save the exception for later.
        self._exc = xvalue

    def reraise(self):
        """ Restore the original exception hook and re-raise any exception
        raised by a virtual reimplementation.
        """

        if self._exc is not None:
            raise self._exc


@pytest.fixture
def virtual_hook():
    """ This fixture installs an exception hook that will catch any exceptions
    raised in a Python reimplementation of a C++ virtual.  The value is an
    object that has a reraise() method.  When it is called it will re-raise any
    any caught exception.
    """

    hook = VirtualHook()

    # Save the old hook and install the new one.
    old_hook = sys.excepthook
    sys.excepthook = hook.catch

    yield hook

    # Restore the original hook.
    sys.excepthook = old_hook


# The prototype pyproject.toml file.
_PYPROJECT_TOML = """
[build-system]
requires = ["sip >=6"]
build-backend = "sipbuild.api"

[project]
name = "{module_name}"

[tool.sip.project]
abi-version = "{abi_version}"
minimum-macos-version = "10.9"
sip-files-dir = ".."
"""

_SIP_MODULE = 'sip-module = "{sip_module}"\n'


def _build_module(module_name, package, build_args, src_dir, test_dir,
        impl_subdirs=None, no_compile=False):
    """ Build a module and move any implementation to the test directory. """

    os.chdir(src_dir)

    pkg_dir = test_dir

    # Do the build.
    args = [sys.executable] + build_args

    if no_compile:
        args.append('--no-compile')

    subprocess.run(args).check_returncode()

    if no_compile:
        return

    # Find the implementation.  Note that we only support setuptools and not
    # distutils.
    impl_pattern = ['build']

    if impl_subdirs is not None:
        impl_pattern.extend(impl_subdirs)

    impl_pattern.append('lib*')

    if package:
        pkg_subdirs = package.split('.')
        impl_pattern.extend(pkg_subdirs)

        pkg_dir = os.path.join(pkg_dir, os.path.join(*pkg_subdirs))
        os.makedirs(pkg_dir, exist_ok=True)

    impl_pattern.append(
            module_name + '*.pyd' if sys.platform == 'win32' else '*.so')

    impl_paths = glob.glob(os.path.join(*impl_pattern))
    if len(impl_paths) == 0:
        raise Exception(
                "no '{0}' extension module was built".format(module_name))

    if len(impl_paths) != 1:
        raise Exception(
                "unable to determine file name of the '{0}' extension module".format(module_name))

    impl_path = os.path.abspath(impl_paths[0])
    impl = os.path.basename(impl_path)

    os.chdir(pkg_dir)

    try:
        os.remove(impl)
    except:
        pass

    os.rename(impl_path, impl)


def _build_test_module(sip_file, test_dir, abi_version, package, exceptions,
        tags, no_compile=False):
    """ Build a test extension module and return its name. """

    build_dir = sip_file[:-4]
    module_name = os.path.basename(build_dir)

    # Remove any previous build directory.
    shutil.rmtree(build_dir, ignore_errors=True)

    os.mkdir(build_dir)

    # See if there are any .h files.
    has_h_files = (len(glob.glob(os.path.join(test_dir, '*.h'))) > 0)

    # Create a pyproject.toml file.
    pyproject_toml = os.path.join(build_dir, 'pyproject.toml')

    with open(pyproject_toml, 'w') as f:
        f.write(
                _PYPROJECT_TOML.format(module_name=module_name,
                        abi_version=abi_version))

        if package:
            f.write(
                    _SIP_MODULE.format(
                            sip_module=_get_full_module_name(package)))

        if exceptions or has_h_files or tags is not None:
            f.write(f'\n[tool.sip.bindings.{module_name}]\n')

            if exceptions:
                f.write('exceptions = true\n')

            if has_h_files:
                f.write('include-dirs = [".."]\n')

            if tags is not None:
                tags_s = ', '.join([f'"{t}"' for t in tags])
                f.write(f'tags = [{tags_s}]\n')

    # Configure the C++11 support.
    cxxflags = os.environ.get('CXXFLAGS', '')
    if '-std=c++11' not in cxxflags:
        os.environ['CXXFLAGS'] = f'{cxxflags} -std=c++11'

    # Build and move the test module.
    _build_module(module_name, package,
            ['-m', 'sipbuild.tools.build', '--verbose'], build_dir, test_dir,
            impl_subdirs=[module_name, 'build'], no_compile=no_compile)

    return _get_full_module_name(package, module_name)


def _build_sip_module(test_dir, abi_version, package,
        sip_module_configuration):
    """ Build the sip module and return its name. """

    sip_module_name = _get_full_module_name(package)

    sdist_glob = os.path.join(test_dir,
            sip_module_name.replace('.', '_') + '-*.tar.gz')

    # Remove any existing sdists.
    for old in glob.glob(sdist_glob):
        os.remove(old)

    # Create the sdist.
    args = [sys.executable, '-m', 'sipbuild.tools.module', '--sdist',
        '--target-dir', test_dir, '--abi-version', str(abi_version)
    ]

    if sip_module_configuration is not None:
        for option in sip_module_configuration:
            args.append('--option')
            args.append(option)

    args.append(sip_module_name)

    subprocess.run(args).check_returncode()

    # Find the sdist and unpack it.
    sdists = glob.glob(sdist_glob)

    if len(sdists) != 1:
        raise Exception(
            "unable to determine the name of the sip module sdist file")

    sdist = sdists[0]

    with tarfile.open(sdist) as tf:
        tf.extractall(path=test_dir, filter='data')

    # Build and move the module.
    src_dir = os.path.join(test_dir, os.path.basename(sdist)[:-7])

    _build_module(sip_module_name, package, ['setup.py', 'build'], src_dir,
            test_dir)

    return sip_module_name


def _get_configuration_details(request):
    """ Return a 6-tuple of the ABI version, the exception support,
    the package, the sip module configuration, the tags and the test directory.
    """

    # Get the ABI version.
    abi_version = request.config.getoption('sip_abi_version')

    # Determine if the tests should be skipped depending on the ABI version.
    if hasattr(request.module, 'cfg_enabled_for'):
        # If the value is a non-empty sequence then it defines those ABIs for
        # which the tests are enabled.
        enabled_for = getattr(request.module, 'cfg_enabled_for')

        if enabled_for:
            if abi_version not in enabled_for:
                pytest.skip(f"Skipping non-enabled test for ABI v{abi_version}")
        else:
            pytest.skip("Skipping disabled test")

    elif hasattr(request.module, 'cfg_disabled_for'):
        # If the value is a non-empty sequence then it defines those ABIs for
        # which the tests are disabled.
        disabled_for = getattr(request.module, 'cfg_disabled_for')

        if disabled_for:
            if abi_version in disabled_for:
                pytest.skip(f"Skipping disabled test for ABI v{abi_version}")

    # See if exception support should be enabled.
    exceptions = getattr(request.module, 'cfg_exceptions', False)

    # The optional package in which generated modules will be placed.  This
    # will also cause a seperate sip module to be built.
    package = getattr(request.module, 'cfg_package', None)

    # The optional configuration of a sip module.
    sip_module_configuration = getattr(request.module,
            'cfg_sip_module_configuration', None)

    # Get the optional list of tags to be used to configure the test module.
    tags = getattr(request.module, 'cfg_tags', None)

    # Get the directory containing the particular test.
    test_dir = request.path.parent

    return abi_version, exceptions, package, sip_module_configuration, tags, test_dir


def _get_full_module_name(package, module_name='sip'):
    """ Return the full name of module taking an optional package into account.
    """

    return package + '.' + module_name if package else module_name


def _get_only_sip_file(test_dir):
    """ Return the name of the .sip file in a test directory. """

    sip_files = _get_sip_files(test_dir)

    if len(sip_files) != 1:
        raise ValueError("Exactly one .sip file expected")

    return sip_files[0]


def _get_sip_files(test_dir):
    """ Return the names of the .sip files in a test directory. """

    return glob.glob(os.path.join(test_dir, '*.sip'))