-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Test] test bare C module (generated)
- Loading branch information
Showing
9 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Bare C module | ||
|
||
No subdirectories or `__init__.py` files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |