diff --git a/examples/extension-hatch/README b/examples/extension-hatch/README new file mode 100644 index 000000000..6ddb727e3 --- /dev/null +++ b/examples/extension-hatch/README @@ -0,0 +1,3 @@ +An example Python package used to support Python packaging tutorials + +This project demonstrates mixing C and Python code by using the frontend hatch with the backend mesonpy diff --git a/examples/extension-hatch/examplePy/__init__.py b/examples/extension-hatch/examplePy/__init__.py new file mode 100644 index 000000000..6b5b4cb19 --- /dev/null +++ b/examples/extension-hatch/examplePy/__init__.py @@ -0,0 +1,4 @@ +from .temperature import celsius_to_fahrenheit, fahrenheit_to_celsius + +__all__ = ["celsius_to_fahrenheit", "fahrenheit_to_celsius"] +__version__ = "0.1.0dev0" diff --git a/examples/extension-hatch/examplePy/meson.build b/examples/extension-hatch/examplePy/meson.build new file mode 100644 index 000000000..745aeb875 --- /dev/null +++ b/examples/extension-hatch/examplePy/meson.build @@ -0,0 +1,15 @@ +py.extension_module( + 'temperature', + 'temperature.c', + install: true, + subdir: 'examplePy' +) + +python_sources = [ + '__init__.py' +] + +py.install_sources( + python_sources, + subdir: 'examplePy' +) diff --git a/examples/extension-hatch/examplePy/temperature.c b/examples/extension-hatch/examplePy/temperature.c new file mode 100644 index 000000000..2317da313 --- /dev/null +++ b/examples/extension-hatch/examplePy/temperature.c @@ -0,0 +1,63 @@ +#define PY_SSIZE_T_CLEAN +#include + +static PyObject * +temperature_celsius_to_fahrenheit(PyObject *self, PyObject *args) +{ + long celsius; + long fahrenheit; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "l", &celsius)) + return NULL; + + fahrenheit = (celsius * 9/5) + 32; + + ret = PyLong_FromLong(fahrenheit); + Py_INCREF(ret); + return ret; +} + +static PyObject * +temperature_fahrenheit_to_celsius(PyObject *self, PyObject *args) +{ + long fahrenheit; + long celsius; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "l", &fahrenheit)) + return NULL; + + celsius = (fahrenheit - 32) * 9/5; + + ret = PyLong_FromLong(celsius); + Py_INCREF(ret); + return ret; +} + +static PyMethodDef CoreMethods[] = { + {"celsius_to_fahrenheit", temperature_celsius_to_fahrenheit, METH_VARARGS, "Convert temperature from Celsius to Fahrenheit"}, + {"fahrenheit_to_celsius", temperature_fahrenheit_to_celsius, METH_VARARGS, "Convert temperature from Fahrenheit to Celsius"}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +static struct PyModuleDef temperaturemodule = { + PyModuleDef_HEAD_INIT, + "temperature", /* name of module */ + NULL, /* module documentation, may be NULL */ + -1, /* size of per-interpreter state of the module, + or -1 if the module keeps state in global variables. */ + CoreMethods +}; + +PyMODINIT_FUNC +PyInit_temperature(void) +{ + PyObject *m; + + m = PyModule_Create(&temperaturemodule); + if (m == NULL) + return NULL; + + return m; +} diff --git a/examples/extension-hatch/meson.build b/examples/extension-hatch/meson.build new file mode 100644 index 000000000..dca69047d --- /dev/null +++ b/examples/extension-hatch/meson.build @@ -0,0 +1,20 @@ +project( + 'examplePy', + 'c', + version: '0.1.dev0', + license: 'MIT', + meson_version: '>= 0.64.0', + default_options: [ + 'buildtype=debugoptimized', + 'c_std=c99', + 'cpp_std=c++14', + ], +) + +cc = meson.get_compiler('c') + +py_mod = import('python') +py = py_mod.find_installation(pure: false) +py_dep = py.dependency() + +subdir('examplePy') diff --git a/examples/extension-hatch/pyproject.toml b/examples/extension-hatch/pyproject.toml new file mode 100644 index 000000000..d8c9092fb --- /dev/null +++ b/examples/extension-hatch/pyproject.toml @@ -0,0 +1,23 @@ +[build-system] +build-backend = "mesonpy" +requires = [ + "meson-python>=0.13.0rc0", +] + +[project] +name = "examplePy" +version = "0.1" +authors = [ + {name = "Some Maintainer", email = "some-email@pyopensci.org"}, +] +maintainers = [ + {name = "All the contributors"}, +] +description = "An example Python package used to support Python packaging tutorials" +keywords = ["pyOpenSci", "python packaging"] +readme = "README" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: BSD License", + "Operating System :: OS Independent", +]