From 7d98e97190b8ada8290865e8551b07a568c588cc Mon Sep 17 00:00:00 2001 From: Pieter Pas Date: Mon, 13 Nov 2023 20:53:00 +0100 Subject: [PATCH] [Test] test bare C module (generated) --- noxfile.py | 2 +- test-packages/bare-c-module/CMakeLists.txt | 14 +++++ test-packages/bare-c-module/LICENSE | 0 test-packages/bare-c-module/README.md | 3 ++ test-packages/bare-c-module/add_module.c | 51 +++++++++++++++++++ test-packages/bare-c-module/pyproject.toml | 43 ++++++++++++++++ .../bare-c-module/tests/test_add_module.py | 5 ++ .../expected_contents/bare-c-module/sdist.txt | 6 +++ tests/expected_contents/bare-c-module/whl.txt | 6 +++ 9 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 test-packages/bare-c-module/CMakeLists.txt create mode 100644 test-packages/bare-c-module/LICENSE create mode 100644 test-packages/bare-c-module/README.md create mode 100644 test-packages/bare-c-module/add_module.c create mode 100644 test-packages/bare-c-module/pyproject.toml create mode 100644 test-packages/bare-c-module/tests/test_add_module.py create mode 100644 tests/expected_contents/bare-c-module/sdist.txt create mode 100644 tests/expected_contents/bare-c-module/whl.txt diff --git a/noxfile.py b/noxfile.py index ff93d73..9abd146 100644 --- a/noxfile.py +++ b/noxfile.py @@ -35,7 +35,7 @@ project_dir = Path(__file__).resolve().parent examples = "minimal-program", "pybind11-project", "nanobind-project", "minimal" -test_packages = "namespace-project-a", "namespace-project-b" +test_packages = "namespace-project-a", "namespace-project-b", "bare-c-module" purity = {"namespace-project-b": True} diff --git a/test-packages/bare-c-module/CMakeLists.txt b/test-packages/bare-c-module/CMakeLists.txt new file mode 100644 index 0000000..caf5bbb --- /dev/null +++ b/test-packages/bare-c-module/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.17) +project(bare-c-module) + +# Find the Python development files +find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module) + +# Add the module to compile +Python3_add_library(bare_c_module MODULE "add_module.c" WITH_SOABI) + +# Install the module +install(TARGETS bare_c_module + EXCLUDE_FROM_ALL + COMPONENT python_modules + DESTINATION .) diff --git a/test-packages/bare-c-module/LICENSE b/test-packages/bare-c-module/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/test-packages/bare-c-module/README.md b/test-packages/bare-c-module/README.md new file mode 100644 index 0000000..ca5f7cb --- /dev/null +++ b/test-packages/bare-c-module/README.md @@ -0,0 +1,3 @@ +# Bare C module + +No subdirectories or `__init__.py` files. \ No newline at end of file diff --git a/test-packages/bare-c-module/add_module.c b/test-packages/bare-c-module/add_module.c new file mode 100644 index 0000000..df90b12 --- /dev/null +++ b/test-packages/bare-c-module/add_module.c @@ -0,0 +1,51 @@ +/* Example based on https://docs.python.org/3/extending/extending.html */ + +#define PY_SSIZE_T_CLEAN +#include + +/* This is the addition function we wish to expose to Python. */ +long add(long a, long b) { + return a + b; +} + +/* Docstring for our Python module. */ +PyDoc_STRVAR( + docstring, + "Simple module that adds integers. " + "Based loosely on https://docs.python.org/3/extending/extending.html"); + +/* Wrapper for our 'add' function, using the Python C API to get the function + arguments as integers, and to return the result as a Python object. */ +static PyObject *add_module_add(PyObject *self, PyObject *args) { + (void)self; + long a, b; + if (!PyArg_ParseTuple(args, "ll", &a, &b)) + return NULL; + long result = add(a, b); + return PyLong_FromLong(result); +} + +/* Define the functions/methods that this module exports. */ +static PyMethodDef AddModuleMethods[] = { + {"add", add_module_add, METH_VARARGS, "Add two integers."}, + {NULL, NULL, 0, NULL}, /* Sentinel */ +}; + +/* Define the actual module. */ +static struct PyModuleDef add_module = { + PyModuleDef_HEAD_INIT, + "bare_c_module", /* name of module */ + docstring, /* module documentation, may be NULL */ + -1, /* size of per-interpreter state of the module, or -1 if + the module keeps state in global variables. */ + AddModuleMethods, + NULL, + NULL, + NULL, + NULL, +}; + +/* The main entry point that is called by Python when our module is imported. */ +PyMODINIT_FUNC PyInit_bare_c_module(void) { + return PyModule_Create(&add_module); +} diff --git a/test-packages/bare-c-module/pyproject.toml b/test-packages/bare-c-module/pyproject.toml new file mode 100644 index 0000000..a62a1c4 --- /dev/null +++ b/test-packages/bare-c-module/pyproject.toml @@ -0,0 +1,43 @@ +[project] +name = "bare_c_module" +version = "0.2.0a9.dev0" +description = "Single extension module, without any folders or __init__.py" +readme = "README.md" +requires-python = ">=3.7" +license = { "file" = "LICENSE" } +authors = [{ "name" = "Pieter P", "email" = "pieter.p.dev@outlook.com" }] +keywords = ["example", "addition", "subtraction"] +classifiers = [ + "Development Status :: 3 - Alpha", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Operating System :: POSIX :: Linux", + "Operating System :: Microsoft :: Windows", + "Operating System :: MacOS", +] +urls = { "Documentation" = "https://tttapa.github.io/" } +dependencies = [] + +[build-system] +requires = ["py-build-cmake~=0.2.0a9.dev0"] +build-backend = "py_build_cmake.build" + +[tool.py-build-cmake.module] +generated = "module" + +[tool.py-build-cmake.sdist] +include = ["CMakeLists.txt", "add_module.c"] + +[tool.py-build-cmake.cmake] +minimum_version = "3.17" +build_type = "RelWithDebInfo" +build_args = ["-j"] +install_components = ["python_modules"] + +[tool.pytest.ini_options] +testpaths = ["tests"] diff --git a/test-packages/bare-c-module/tests/test_add_module.py b/test-packages/bare-c-module/tests/test_add_module.py new file mode 100644 index 0000000..f616470 --- /dev/null +++ b/test-packages/bare-c-module/tests/test_add_module.py @@ -0,0 +1,5 @@ +from bare_c_module import add + + +def test_add(): + assert add(1, 2) == 3 diff --git a/tests/expected_contents/bare-c-module/sdist.txt b/tests/expected_contents/bare-c-module/sdist.txt new file mode 100644 index 0000000..af1f5a5 --- /dev/null +++ b/tests/expected_contents/bare-c-module/sdist.txt @@ -0,0 +1,6 @@ +bare_c_module-{{version}}/CMakeLists.txt +bare_c_module-{{version}}/LICENSE +bare_c_module-{{version}}/PKG-INFO +bare_c_module-{{version}}/README.md +bare_c_module-{{version}}/pyproject.toml +bare_c_module-{{version}}/add_module.c \ No newline at end of file diff --git a/tests/expected_contents/bare-c-module/whl.txt b/tests/expected_contents/bare-c-module/whl.txt new file mode 100644 index 0000000..ed539ef --- /dev/null +++ b/tests/expected_contents/bare-c-module/whl.txt @@ -0,0 +1,6 @@ +bare_c_module-{{version}}.dist-info/LICENSE +bare_c_module-{{version}}.dist-info/METADATA +bare_c_module-{{version}}.dist-info/RECORD +bare_c_module-{{version}}.dist-info/WHEEL +bare_c_module-{{version}}.dist-info/entry_points.txt +bare_c_module{{ext_suffix}} \ No newline at end of file