| 1 |
""" |
| 2 |
Test the hpy+distutils integration. Most of the relevant code is in |
| 3 |
hpy/devel/__init__.py. |
| 4 |
|
| 5 |
Note that this is a different kind of test than the majority of the other |
| 6 |
files in this directory, which all inherit from HPyTest and test the API |
| 7 |
itself. |
| 8 |
""" |
| 9 |
|
| 10 |
import sys |
| 11 |
import os |
| 12 |
import textwrap |
| 13 |
import subprocess |
| 14 |
import shutil |
| 15 |
import venv |
| 16 |
import py |
| 17 |
import pytest |
| 18 |
|
| 19 |
from ..support import atomic_run, HPY_ROOT |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
def print_CalledProcessError(p): |
| 28 |
""" |
| 29 |
Print all information about a CalledProcessError |
| 30 |
""" |
| 31 |
print('========== subprocess failed ==========') |
| 32 |
print('command:', ' '.join(p.cmd)) |
| 33 |
print('argv: ', p.cmd) |
| 34 |
print('return code:', p.returncode) |
| 35 |
print() |
| 36 |
print('---------- <stdout> ----------') |
| 37 |
print(p.stdout.decode('latin-1')) |
| 38 |
print('---------- </stdout> ---------') |
| 39 |
print() |
| 40 |
print('---------- <stderr> ----------') |
| 41 |
print(p.stderr.decode('latin-1')) |
| 42 |
print('---------- </stderr> ---------') |
| 43 |
|
| 44 |
@pytest.fixture(scope='session') |
| 45 |
def venv_template(request, tmpdir_factory): |
| 46 |
if getattr(request.config.option, "reuse_venv", False): |
| 47 |
d = py.path.local('/tmp/venv-for-hpytest') |
| 48 |
if d.check(dir=True): |
| 49 |
|
| 50 |
|
| 51 |
return d |
| 52 |
else: |
| 53 |
d = tmpdir_factory.mktemp('venv') |
| 54 |
|
| 55 |
venv.create(d, with_pip=True) |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
attach_python_to_venv(d) |
| 61 |
keep = ['python', 'pypy', 'lib'] |
| 62 |
for script in d.bin.listdir(): |
| 63 |
if any([script.basename.startswith(k) for k in keep]): |
| 64 |
continue |
| 65 |
script.remove() |
| 66 |
|
| 67 |
try: |
| 68 |
atomic_run( |
| 69 |
[str(d.python), '-m', 'pip', 'install', '-U', 'pip', 'wheel', 'setuptools<72'], |
| 70 |
check=True, |
| 71 |
capture_output=True, |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
except subprocess.CalledProcessError as cpe: |
| 79 |
print_CalledProcessError(cpe) |
| 80 |
raise |
| 81 |
return d |
| 82 |
|
| 83 |
def attach_python_to_venv(d): |
| 84 |
if os.name == 'nt': |
| 85 |
d.bin = d.join('Scripts') |
| 86 |
else: |
| 87 |
d.bin = d.join('bin') |
| 88 |
d.python = d.bin.join('python') |
| 89 |
|
| 90 |
modes = ['hybrid', 'universal'] |
| 91 |
if sys.implementation.name == 'cpython': |
| 92 |
modes += ['cpython'] |
| 93 |
|
| 94 |
@pytest.mark.usefixtures('initargs') |
| 95 |
class TestDistutils: |
| 96 |
|
| 97 |
@pytest.fixture() |
| 98 |
def initargs(self, pytestconfig, tmpdir, venv_template): |
| 99 |
self.tmpdir = tmpdir |
| 100 |
|
| 101 |
self.venv = tmpdir.join('venv') |
| 102 |
shutil.copytree(venv_template, self.venv, symlinks=True) |
| 103 |
attach_python_to_venv(self.venv) |
| 104 |
|
| 105 |
self.hpy_test_project = tmpdir.join('hpy_test_project').ensure(dir=True) |
| 106 |
self.gen_project() |
| 107 |
self.hpy_test_project.chdir() |
| 108 |
|
| 109 |
@pytest.fixture(params=modes) |
| 110 |
def hpy_abi(self, request): |
| 111 |
return request.param |
| 112 |
|
| 113 |
def python(self, *args, capture=False): |
| 114 |
""" |
| 115 |
Run python inside the venv; if capture==True, return stdout |
| 116 |
""" |
| 117 |
cmd = [str(self.venv.python)] + list(args) |
| 118 |
print('[RUN]', ' '.join(cmd)) |
| 119 |
if capture: |
| 120 |
proc = atomic_run(cmd, capture_output=True) |
| 121 |
out = proc.stdout.decode('latin-1').strip() |
| 122 |
else: |
| 123 |
proc = atomic_run(cmd) |
| 124 |
out = None |
| 125 |
proc.check_returncode() |
| 126 |
return out |
| 127 |
|
| 128 |
|
| 129 |
def writefile(self, fname, content): |
| 130 |
""" |
| 131 |
Write a file inside hpy_test_project |
| 132 |
""" |
| 133 |
f = self.hpy_test_project.join(fname) |
| 134 |
content = textwrap.dedent(content) |
| 135 |
f.write(content) |
| 136 |
|
| 137 |
def gen_project(self): |
| 138 |
""" |
| 139 |
Generate the files needed to build the project, except setup.py |
| 140 |
""" |
| 141 |
self.writefile('cpymod.c', """ |
| 142 |
// the simplest possible Python/C module |
| 143 |
#include <Python.h> |
| 144 |
static PyModuleDef moduledef = { |
| 145 |
PyModuleDef_HEAD_INIT, |
| 146 |
"cpymod", |
| 147 |
"cpymod docstring" |
| 148 |
}; |
| 149 |
|
| 150 |
PyMODINIT_FUNC |
| 151 |
PyInit_cpymod(void) |
| 152 |
{ |
| 153 |
return PyModule_Create(&moduledef); |
| 154 |
} |
| 155 |
""") |
| 156 |
|
| 157 |
self.writefile('hpymod.c', """ |
| 158 |
// the simplest possible HPy module |
| 159 |
#include <hpy.h> |
| 160 |
static HPyModuleDef moduledef = { |
| 161 |
.doc = "hpymod with HPy ABI: " HPY_ABI, |
| 162 |
}; |
| 163 |
|
| 164 |
HPy_MODINIT(hpymod, moduledef) |
| 165 |
""") |
| 166 |
|
| 167 |
self.writefile('hpymod_legacy.c', """ |
| 168 |
// the simplest possible HPy+legacy module |
| 169 |
#include <hpy.h> |
| 170 |
#include <Python.h> |
| 171 |
|
| 172 |
static PyObject *f(PyObject *self, PyObject *args) |
| 173 |
{ |
| 174 |
return PyLong_FromLong(1234); |
| 175 |
} |
| 176 |
static PyMethodDef my_legacy_methods[] = { |
| 177 |
{"f", (PyCFunction)f, METH_NOARGS}, |
| 178 |
{NULL} |
| 179 |
}; |
| 180 |
|
| 181 |
static HPyModuleDef moduledef = { |
| 182 |
.doc = "hpymod_legacy with HPy ABI: " HPY_ABI, |
| 183 |
.legacy_methods = my_legacy_methods, |
| 184 |
}; |
| 185 |
|
| 186 |
HPy_MODINIT(hpymod_legacy, moduledef) |
| 187 |
""") |
| 188 |
|
| 189 |
def gen_setup_py(self, src): |
| 190 |
preamble = textwrap.dedent(""" |
| 191 |
from setuptools import setup, Extension |
| 192 |
cpymod = Extension("cpymod", ["cpymod.c"]) |
| 193 |
hpymod = Extension("hpymod", ["hpymod.c"]) |
| 194 |
hpymod_legacy = Extension("hpymod_legacy", ["hpymod_legacy.c"]) |
| 195 |
""") |
| 196 |
src = preamble + textwrap.dedent(src) |
| 197 |
f = self.hpy_test_project.join('setup.py') |
| 198 |
f.write(src) |
| 199 |
|
| 200 |
def get_docstring(self, modname): |
| 201 |
cmd = f'import {modname}; print({modname}.__doc__)' |
| 202 |
return self.python('-c', cmd, capture=True) |
| 203 |
|
| 204 |
def test_cpymod_setup_install(self): |
| 205 |
|
| 206 |
|
| 207 |
self.gen_setup_py(""" |
| 208 |
setup(name = "hpy_test_project", |
| 209 |
ext_modules = [cpymod], |
| 210 |
) |
| 211 |
""") |
| 212 |
self.python('setup.py', 'install') |
| 213 |
doc = self.get_docstring('cpymod') |
| 214 |
assert doc == 'cpymod docstring' |
| 215 |
|
| 216 |
def test_cpymod_with_empty_hpy_ext_modules_setup_install(self): |
| 217 |
|
| 218 |
|
| 219 |
self.gen_setup_py(""" |
| 220 |
setup(name = "hpy_test_project", |
| 221 |
ext_modules = [cpymod], |
| 222 |
hpy_ext_modules = [] |
| 223 |
) |
| 224 |
""") |
| 225 |
self.python('setup.py', 'install') |
| 226 |
doc = self.get_docstring('cpymod') |
| 227 |
assert doc == 'cpymod docstring' |
| 228 |
|
| 229 |
def test_hpymod_py_stub(self): |
| 230 |
|
| 231 |
self.gen_setup_py(""" |
| 232 |
setup(name = "hpy_test_project", |
| 233 |
hpy_ext_modules = [hpymod], |
| 234 |
) |
| 235 |
""") |
| 236 |
self.python('setup.py', '--hpy-abi=universal', 'build') |
| 237 |
build = self.hpy_test_project.join('build') |
| 238 |
lib = build.listdir('lib*')[0] |
| 239 |
hpymod_py = lib.join('hpymod.py') |
| 240 |
assert hpymod_py.check(exists=True) |
| 241 |
assert 'This file is automatically generated by hpy' in hpymod_py.read() |
| 242 |
|
| 243 |
def test_hpymod_build_platlib(self): |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
self.gen_setup_py(""" |
| 249 |
setup(name = "hpy_test_project", |
| 250 |
hpy_ext_modules = [hpymod], |
| 251 |
) |
| 252 |
""") |
| 253 |
self.python('setup.py', 'build') |
| 254 |
build = self.hpy_test_project.join('build') |
| 255 |
libs = build.listdir('lib*') |
| 256 |
assert len(libs) == 1 |
| 257 |
libdir = libs[0] |
| 258 |
|
| 259 |
assert libdir.basename != 'lib' |
| 260 |
|
| 261 |
def test_hpymod_build_ext_inplace(self, hpy_abi): |
| 262 |
|
| 263 |
self.gen_setup_py(""" |
| 264 |
setup(name = "hpy_test_project", |
| 265 |
hpy_ext_modules = [hpymod], |
| 266 |
) |
| 267 |
""") |
| 268 |
self.python('setup.py', f'--hpy-abi={hpy_abi}', 'build_ext', '--inplace') |
| 269 |
doc = self.get_docstring('hpymod') |
| 270 |
assert doc == f'hpymod with HPy ABI: {hpy_abi}' |
| 271 |
|
| 272 |
def test_hpymod_setup_install(self, hpy_abi): |
| 273 |
|
| 274 |
self.gen_setup_py(""" |
| 275 |
setup(name = "hpy_test_project", |
| 276 |
hpy_ext_modules = [hpymod], |
| 277 |
) |
| 278 |
""") |
| 279 |
self.python('setup.py', f'--hpy-abi={hpy_abi}', 'install') |
| 280 |
doc = self.get_docstring('hpymod') |
| 281 |
assert doc == f'hpymod with HPy ABI: {hpy_abi}' |
| 282 |
|
| 283 |
def test_hpymod_wheel(self, hpy_abi): |
| 284 |
|
| 285 |
self.gen_setup_py(""" |
| 286 |
setup(name = "hpy_test_project", |
| 287 |
hpy_ext_modules = [hpymod], |
| 288 |
) |
| 289 |
""") |
| 290 |
self.python('setup.py', f'--hpy-abi={hpy_abi}', 'bdist_wheel') |
| 291 |
dist = self.hpy_test_project.join('dist') |
| 292 |
whl = dist.listdir('*.whl')[0] |
| 293 |
self.python('-m', 'pip', 'install', str(whl)) |
| 294 |
doc = self.get_docstring('hpymod') |
| 295 |
assert doc == f'hpymod with HPy ABI: {hpy_abi}' |
| 296 |
|
| 297 |
@pytest.mark.skipif(sys.implementation.name != "cpython", |
| 298 |
reason="cpython only") |
| 299 |
def test_dont_mix_cpython_and_universal_abis(self): |
| 300 |
""" |
| 301 |
See issue #322 |
| 302 |
""" |
| 303 |
|
| 304 |
|
| 305 |
self.gen_setup_py(""" |
| 306 |
setup(name = "hpy_test_project", |
| 307 |
hpy_ext_modules = [hpymod], |
| 308 |
install_requires = [], |
| 309 |
) |
| 310 |
""") |
| 311 |
self.python('setup.py', 'install') |
| 312 |
|
| 313 |
build = self.hpy_test_project.join('build') |
| 314 |
temps = build.listdir('temp*') |
| 315 |
libs = build.listdir('lib*') |
| 316 |
assert len(temps) == 1 |
| 317 |
assert len(libs) == 1 |
| 318 |
|
| 319 |
doc = self.get_docstring('hpymod') |
| 320 |
assert doc == 'hpymod with HPy ABI: cpython' |
| 321 |
|
| 322 |
|
| 323 |
self.python('setup.py', '--hpy-abi=universal', 'install') |
| 324 |
|
| 325 |
build = self.hpy_test_project.join('build') |
| 326 |
temps = build.listdir('temp*') |
| 327 |
libs = build.listdir('lib*') |
| 328 |
assert len(temps) == 2 |
| 329 |
assert len(libs) == 2 |
| 330 |
|
| 331 |
doc = self.get_docstring('hpymod') |
| 332 |
assert doc == 'hpymod with HPy ABI: universal' |
| 333 |
|
| 334 |
def test_hpymod_legacy(self, hpy_abi): |
| 335 |
if hpy_abi == 'universal': |
| 336 |
pytest.skip('only for cpython and hybrid ABIs') |
| 337 |
self.gen_setup_py(""" |
| 338 |
setup(name = "hpy_test_project", |
| 339 |
hpy_ext_modules = [hpymod_legacy], |
| 340 |
install_requires = [], |
| 341 |
) |
| 342 |
""") |
| 343 |
self.python('setup.py', f"--hpy-abi={hpy_abi}", 'install') |
| 344 |
src = 'import hpymod_legacy; print(hpymod_legacy.f())' |
| 345 |
out = self.python('-c', src, capture=True) |
| 346 |
assert out == '1234' |
| 347 |
|
| 348 |
def test_hpymod_legacy_fails_with_universal(self): |
| 349 |
self.gen_setup_py(""" |
| 350 |
setup(name = "hpy_test_project", |
| 351 |
hpy_ext_modules = [hpymod_legacy], |
| 352 |
install_requires = [], |
| 353 |
) |
| 354 |
""") |
| 355 |
with pytest.raises(subprocess.CalledProcessError) as exc: |
| 356 |
self.python('setup.py', '--hpy-abi=universal', 'install', capture=True) |
| 357 |
expected_msg = ("It is forbidden to #include <Python.h> when " |
| 358 |
"targeting the HPy Universal ABI") |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
out = exc.value.stdout + b'\n' + exc.value.stderr |
| 364 |
out = out.decode('latin-1') |
| 365 |
if expected_msg not in out: |
| 366 |
print_CalledProcessError(exc.value) |
| 367 |
assert expected_msg in out |