Skip to content

Commit

Permalink
[Test] test bare C module (generated)
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Nov 13, 2023
1 parent 8202449 commit 7d98e97
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 1 deletion.
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
14 changes: 14 additions & 0 deletions test-packages/bare-c-module/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 .)
Empty file.
3 changes: 3 additions & 0 deletions test-packages/bare-c-module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Bare C module

No subdirectories or `__init__.py` files.
51 changes: 51 additions & 0 deletions test-packages/bare-c-module/add_module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Example based on https://docs.python.org/3/extending/extending.html */

#define PY_SSIZE_T_CLEAN
#include <Python.h>

/* 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);
}
43 changes: 43 additions & 0 deletions test-packages/bare-c-module/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"]
5 changes: 5 additions & 0 deletions test-packages/bare-c-module/tests/test_add_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from bare_c_module import add


def test_add():
assert add(1, 2) == 3
6 changes: 6 additions & 0 deletions tests/expected_contents/bare-c-module/sdist.txt
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions tests/expected_contents/bare-c-module/whl.txt
Original file line number Diff line number Diff line change
@@ -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}}

0 comments on commit 7d98e97

Please sign in to comment.