Skip to content

Commit

Permalink
test: Use meson of setuptools consistently
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Górny <mgorny@gentoo.org>
  • Loading branch information
mgorny committed Jul 17, 2024
1 parent d76f17e commit 13cf94d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ include = [
test = [
"meson",
"ninja",
"setuptools",
]

[project.urls]
Expand Down
75 changes: 39 additions & 36 deletions test/test_c_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,43 @@ def inner(code=""):
return PyModule_Create(&testmodule);
}}
""")
pytester.makepyfile(setup=f"""
from setuptools import setup, Extension
setup(name="test",
version="0",
ext_modules=[
Extension(name="test",
sources=["test.c"],
py_limited_api={py_limited_api}),
])
pytester.makefile(".build", meson="""
project('test', 'c')
pymod = import('python')
python = pymod.find_installation(get_option('python'))
python.extension_module('test', ['test.c'], limited_api : '3.9')
""")
pytester.makefile(".options", meson="""
option('python', type : 'string')
""")
subprocess.run([sys.executable, "setup.py", "build_ext", "-i"],
check=True)
build_dir = pytester.mkdir("build")
subprocess.run(["meson", "setup", build_dir,
f"-Dpython={sys.executable}",
f"-Dpython.allow_limited_api={py_limited_api}",
], check=True)
subprocess.run(["meson", "compile", "-C", build_dir], check=True)
return build_dir
yield inner


def test_c_ext(run, build_c_ext):
build_c_ext()
result = run("--ignore=setup.py")
build_dir = build_c_ext()
result = run(build_dir)
result.assert_outcomes(passed=1)
result.stdout.fnmatch_lines(["test.*::import-check*PASSED*"])
result.stdout.fnmatch_lines(["build/test.*::import-check*PASSED*"])


@pytest.mark.skipif(os.name == "nt",
reason="Python on Windows crashes on loading an extension "
"with undefined symbols (!)")
def test_c_ext_undefined_symbol(run, build_c_ext):
build_c_ext(code="this_function_does_not_exist();")
result = run("--ignore=setup.py")
build_dir = build_c_ext(code="this_function_does_not_exist();")
result = run(build_dir)
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines([
"test.*::import-check*FAILED*",
"E*ImportError*test.*this_function_does_not_exist*",
"build/test.*::import-check*FAILED*",
"E*ImportError*build/test.*this_function_does_not_exist*",
])
# check whether we got nicely stripped traceback
result.stdout.no_fnmatch_line("*/_pytest/*")
Expand All @@ -78,24 +82,23 @@ def test_c_ext_undefined_symbol(run, build_c_ext):

def test_c_ext_import_py(pytester, run, build_c_ext):
pytester.makepyfile(foo="")
build_c_ext(code='if (!PyImport_ImportModule("foo")) return NULL;')
result = run("--ignore=setup.py")
result.assert_outcomes(passed=2)
build_dir = build_c_ext(code='if (!PyImport_ImportModule("foo")) return NULL;')
result = run(build_dir)
result.assert_outcomes(passed=1)
result.stdout.fnmatch_lines([
"foo.py::import-check*PASSED*",
"test.*::import-check*PASSED*",
"build/test.*::import-check*PASSED*",
])


def test_c_ext_import_nonexisting(pytester, run, build_c_ext):
build_c_ext(code="""
build_dir = build_c_ext(code="""
if (!PyImport_ImportModule("this_package_really_shouldnt_exist"))
return NULL;
""")
result = run("--ignore=setup.py")
result = run(build_dir)
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines([
"test.*::import-check*FAILED*",
"build/test.*::import-check*FAILED*",
"E*ModuleNotFoundError*this_package_really_shouldnt_exist*",
])
# check whether we got nicely stripped traceback
Expand All @@ -105,14 +108,14 @@ def test_c_ext_import_nonexisting(pytester, run, build_c_ext):

def test_c_ext_import_syntax_error(pytester, run, build_c_ext):
pytester.makepyfile(bad="/ / /")
build_c_ext(code="""
build_dir = build_c_ext(code="""
if (!PyImport_ImportModule("bad"))
return NULL;
""")
result = run("--ignore=setup.py", "--ignore=bad.py")
result = run(build_dir)
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines([
"test.*::import-check*FAILED*",
"build/test.*::import-check*FAILED*",
"E*File*/bad.py*line 1",
"*/ / /",
"*SyntaxError:*",
Expand All @@ -123,14 +126,14 @@ def test_c_ext_import_syntax_error(pytester, run, build_c_ext):


def test_c_ext_exception(pytester, run, build_c_ext):
build_c_ext(code="""
build_dir = build_c_ext(code="""
PyErr_SetString(PyExc_ValueError, "Imma failing");
return NULL;
""")
result = run("--ignore=setup.py")
result = run(build_dir)
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines([
"test.*::import-check*FAILED*",
"build/test.*::import-check*FAILED*",
"E*ValueError*Imma failing*",
])
# check whether we got nicely stripped traceback
Expand All @@ -140,14 +143,14 @@ def test_c_ext_exception(pytester, run, build_c_ext):

def test_c_ext_import_indirect_nonexisting(pytester, run, build_c_ext):
pytester.makepyfile(bad="import this_package_really_shouldnt_exist")
build_c_ext(code="""
build_dir = build_c_ext(code="""
if (!PyImport_ImportModule("bad"))
return NULL;
""")
result = run("--ignore=setup.py", "--ignore=bad.py")
result = run(build_dir)
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines([
"test.*::import-check*FAILED*",
"build/test.*::import-check*FAILED*",
">*import this_package_really_shouldnt_exist",
"E*ModuleNotFoundError:*",
"bad.py:1: ModuleNotFoundError",
Expand Down

0 comments on commit 13cf94d

Please sign in to comment.