File: test_distutils.py

package info (click to toggle)
pypy3 7.3.20%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212,628 kB
  • sloc: python: 2,101,020; ansic: 540,684; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (367 lines) | stat: -rw-r--r-- 12,929 bytes parent folder | download | duplicates (2)
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
"""
Test the hpy+distutils integration. Most of the relevant code is in
hpy/devel/__init__.py.

Note that this is a different kind of test than the majority of the other
files in this directory, which all inherit from HPyTest and test the API
itself.
"""

import sys
import os
import textwrap
import subprocess
import shutil
import venv
import py
import pytest

from ..support import atomic_run, HPY_ROOT

# ====== IMPORTANT DEVELOPMENT TIP =====
# You can use py.test --reuse-venv to speed up local testing.
#
# The env is created once in /tmp/venv-for-hpytest and reused among tests and
# sessions. If you want to recreate it, simply rm -r /tmp/venv-for-hpytest

def print_CalledProcessError(p):
    """
    Print all information about a CalledProcessError
    """
    print('========== subprocess failed ==========')
    print('command:', ' '.join(p.cmd))
    print('argv:   ', p.cmd)
    print('return code:', p.returncode)
    print()
    print('---------- <stdout> ----------')
    print(p.stdout.decode('latin-1'))
    print('---------- </stdout> ---------')
    print()
    print('---------- <stderr> ----------')
    print(p.stderr.decode('latin-1'))
    print('---------- </stderr> ---------')

@pytest.fixture(scope='session')
def venv_template(request, tmpdir_factory):
    if getattr(request.config.option, "reuse_venv", False):
        d = py.path.local('/tmp/venv-for-hpytest')
        if d.check(dir=True):
            # if it exists, we assume it's correct. If you want to recreate,
            # just manually delete /tmp/venv-for-hpytest
            return d
    else:
        d = tmpdir_factory.mktemp('venv')

    venv.create(d, with_pip=True)

    # remove the scripts: they contains a shebang and it will fail subtly
    # after we clone the template. Yes, we could try to fix the shebangs, but
    # it's just easier to use e.g. python -m pip
    attach_python_to_venv(d)
    keep = ['python', 'pypy', 'lib']
    for script in d.bin.listdir():
        if any([script.basename.startswith(k) for k in keep]):
            continue
        script.remove()
    #
    try:
        atomic_run(
            [str(d.python), '-m', 'pip', 'install', '-U', 'pip', 'wheel', 'setuptools'],
            check=True,
            capture_output=True,
        )
        # atomic_run(
        #     [str(d.python), '-m', 'pip', 'install', str(HPY_ROOT)],
        #     check=True,
        #     capture_output=True,
        # )
    except subprocess.CalledProcessError as cpe:
        print_CalledProcessError(cpe)
        raise
    return d

def attach_python_to_venv(d):
    if os.name == 'nt':
        d.bin = d.join('Scripts')
    else:
        d.bin = d.join('bin')
    d.python = d.bin.join('python')

modes = ['hybrid', 'universal']
if sys.implementation.name == 'cpython':
    modes += ['cpython']

@pytest.mark.usefixtures('initargs')
class TestDistutils:

    @pytest.fixture()
    def initargs(self, pytestconfig, tmpdir, venv_template):
        self.tmpdir = tmpdir
        # create a fresh venv by copying the template
        self.venv = tmpdir.join('venv')
        shutil.copytree(venv_template, self.venv, symlinks=True)
        attach_python_to_venv(self.venv)
        # create the files for our test project
        self.hpy_test_project = tmpdir.join('hpy_test_project').ensure(dir=True)
        self.gen_project()
        self.hpy_test_project.chdir()

    @pytest.fixture(params=modes)
    def hpy_abi(self, request):
        return request.param

    def python(self, *args, capture=False):
        """
        Run python inside the venv; if capture==True, return stdout
        """
        cmd = [str(self.venv.python)] + list(args)
        print('[RUN]', ' '.join(cmd))
        if capture:
            proc = atomic_run(cmd, capture_output=True)
            out = proc.stdout.decode('latin-1').strip()
        else:
            proc = atomic_run(cmd)
            out = None
        proc.check_returncode()
        return out


    def writefile(self, fname, content):
        """
        Write a file inside hpy_test_project
        """
        f = self.hpy_test_project.join(fname)
        content = textwrap.dedent(content)
        f.write(content)

    def gen_project(self):
        """
        Generate the files needed to build the project, except setup.py
        """
        self.writefile('cpymod.c', """
            // the simplest possible Python/C module
            #include <Python.h>
            static PyModuleDef moduledef = {
                PyModuleDef_HEAD_INIT,
                "cpymod",
                "cpymod docstring"
            };

            PyMODINIT_FUNC
            PyInit_cpymod(void)
            {
                return PyModule_Create(&moduledef);
            }
        """)

        self.writefile('hpymod.c', """
            // the simplest possible HPy module
            #include <hpy.h>
            static HPyModuleDef moduledef = {
                .doc = "hpymod with HPy ABI: " HPY_ABI,
            };

            HPy_MODINIT(hpymod, moduledef)
        """)

        self.writefile('hpymod_legacy.c', """
            // the simplest possible HPy+legacy module
            #include <hpy.h>
            #include <Python.h>

            static PyObject *f(PyObject *self, PyObject *args)
            {
                return PyLong_FromLong(1234);
            }
            static PyMethodDef my_legacy_methods[] = {
                {"f", (PyCFunction)f, METH_NOARGS},
                {NULL}
            };

            static HPyModuleDef moduledef = {
                .doc = "hpymod_legacy with HPy ABI: " HPY_ABI,
                .legacy_methods = my_legacy_methods,
            };

            HPy_MODINIT(hpymod_legacy, moduledef)
        """)

    def gen_setup_py(self, src):
        preamble = textwrap.dedent("""
            from setuptools import setup, Extension
            cpymod = Extension("cpymod", ["cpymod.c"])
            hpymod = Extension("hpymod", ["hpymod.c"])
            hpymod_legacy = Extension("hpymod_legacy", ["hpymod_legacy.c"])
        """)
        src = preamble + textwrap.dedent(src)
        f = self.hpy_test_project.join('setup.py')
        f.write(src)

    def get_docstring(self, modname):
        cmd = f'import {modname}; print({modname}.__doc__)'
        return self.python('-c', cmd, capture=True)

    def test_cpymod_setup_install(self):
        # CPython-only project, no hpy at all. This is a baseline to check
        # that everything works even without hpy.
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  ext_modules = [cpymod],
            )
        """)
        self.python('setup.py', 'install')
        doc = self.get_docstring('cpymod')
        assert doc == 'cpymod docstring'

    def test_cpymod_with_empty_hpy_ext_modules_setup_install(self):
        # if we have hpy_ext_modules=[] we trigger the hpy.devel monkey
        # patch. This checks that we don't ext_modules still works after that.
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  ext_modules = [cpymod],
                  hpy_ext_modules = []
            )
        """)
        self.python('setup.py', 'install')
        doc = self.get_docstring('cpymod')
        assert doc == 'cpymod docstring'

    def test_hpymod_py_stub(self):
        # check that that we generated the .py stub for universal
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  hpy_ext_modules = [hpymod],
            )
        """)
        self.python('setup.py', '--hpy-abi=universal', 'build')
        build = self.hpy_test_project.join('build')
        lib = build.listdir('lib*')[0]
        hpymod_py = lib.join('hpymod.py')
        assert hpymod_py.check(exists=True)
        assert 'This file is automatically generated by hpy' in hpymod_py.read()

    def test_hpymod_build_platlib(self):
        # check that if we have only hpy_ext_modules, the distribution is
        # detected as "platform-specific" and not "platform-neutral". In
        # particular, we want the end result to be in
        # e.g. build/lib.linux-x86_64-3.8 and NOT in build/lib.
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  hpy_ext_modules = [hpymod],
            )
        """)
        self.python('setup.py', 'build')
        build = self.hpy_test_project.join('build')
        libs = build.listdir('lib*')
        assert len(libs) == 1
        libdir = libs[0]
        # this is something like lib.linux-x86_64-cpython-38
        assert libdir.basename != 'lib'

    def test_hpymod_build_ext_inplace(self, hpy_abi):
        # check that we can install hpy modules with setup.py build_ext -i
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  hpy_ext_modules = [hpymod],
            )
        """)
        self.python('setup.py', f'--hpy-abi={hpy_abi}', 'build_ext', '--inplace')
        doc = self.get_docstring('hpymod')
        assert doc == f'hpymod with HPy ABI: {hpy_abi}'

    def test_hpymod_setup_install(self, hpy_abi):
        # check that we can install hpy modules with setup.py install
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  hpy_ext_modules = [hpymod],
            )
        """)
        self.python('setup.py', f'--hpy-abi={hpy_abi}', 'install')
        doc = self.get_docstring('hpymod')
        assert doc == f'hpymod with HPy ABI: {hpy_abi}'

    def test_hpymod_wheel(self, hpy_abi):
        # check that we can build and install wheels
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  hpy_ext_modules = [hpymod],
            )
        """)
        self.python('setup.py', f'--hpy-abi={hpy_abi}', 'bdist_wheel')
        dist = self.hpy_test_project.join('dist')
        whl = dist.listdir('*.whl')[0]
        self.python('-m', 'pip', 'install', str(whl))
        doc = self.get_docstring('hpymod')
        assert doc == f'hpymod with HPy ABI: {hpy_abi}'

    @pytest.mark.skipif(sys.implementation.name != "cpython",
                        reason="cpython only")
    def test_dont_mix_cpython_and_universal_abis(self):
        """
        See issue #322
        """
        # make sure that the build dirs for cpython and universal ABIs are
        # distinct
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  hpy_ext_modules = [hpymod],
                  install_requires = [],
            )
        """)
        self.python('setup.py', 'install')
        # in the build/ dir, we should have 2 directories: temp and lib
        build = self.hpy_test_project.join('build')
        temps = build.listdir('temp*')
        libs = build.listdir('lib*')
        assert len(temps) == 1
        assert len(libs) == 1
        #
        doc = self.get_docstring('hpymod')
        assert doc == 'hpymod with HPy ABI: cpython'

        # now recompile with universal *without* cleaning the build
        self.python('setup.py', '--hpy-abi=universal', 'install')
        # in the build/ dir, we should have 4 directories: 2 temp*, and 2 lib*
        build = self.hpy_test_project.join('build')
        temps = build.listdir('temp*')
        libs = build.listdir('lib*')
        assert len(temps) == 2
        assert len(libs) == 2
        #
        doc = self.get_docstring('hpymod')
        assert doc == 'hpymod with HPy ABI: universal'

    def test_hpymod_legacy(self, hpy_abi):
        if hpy_abi == 'universal':
            pytest.skip('only for cpython and hybrid ABIs')
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  hpy_ext_modules = [hpymod_legacy],
                  install_requires = [],
            )
        """)
        self.python('setup.py', f"--hpy-abi={hpy_abi}", 'install')
        src = 'import hpymod_legacy; print(hpymod_legacy.f())'
        out = self.python('-c', src, capture=True)
        assert out == '1234'

    def test_hpymod_legacy_fails_with_universal(self):
        self.gen_setup_py("""
            setup(name = "hpy_test_project",
                  hpy_ext_modules = [hpymod_legacy],
                  install_requires = [],
            )
        """)
        with pytest.raises(subprocess.CalledProcessError) as exc:
            self.python('setup.py', '--hpy-abi=universal', 'install', capture=True)
        expected_msg = ("It is forbidden to #include <Python.h> when "
                        "targeting the HPy Universal ABI")

        # gcc/clang prints the #error on stderr, MSVC prints it on
        # stdout. Here we check that the error is printed "somewhere", we
        # don't care exactly where.
        out = exc.value.stdout + b'\n' + exc.value.stderr
        out = out.decode('latin-1')
        if expected_msg not in out:
            print_CalledProcessError(exc.value)
        assert expected_msg in out