From a40b364c98c98fba1b41d5992dc751d2d89affc4 Mon Sep 17 00:00:00 2001 From: Jeremiah Paige Date: Mon, 20 May 2024 11:36:14 -0700 Subject: [PATCH 1/5] create meson example --- examples/extension-hatch/README.md | 3 + .../extension-hatch/examplePy/__init__.py | 4 ++ .../extension-hatch/examplePy/meson.build | 15 +++++ .../extension-hatch/examplePy/temperature.c | 61 +++++++++++++++++++ examples/extension-hatch/meson.build | 20 ++++++ examples/extension-hatch/pyproject.toml | 23 +++++++ 6 files changed, 126 insertions(+) create mode 100644 examples/extension-hatch/README.md create mode 100644 examples/extension-hatch/examplePy/__init__.py create mode 100644 examples/extension-hatch/examplePy/meson.build create mode 100644 examples/extension-hatch/examplePy/temperature.c create mode 100644 examples/extension-hatch/meson.build create mode 100644 examples/extension-hatch/pyproject.toml diff --git a/examples/extension-hatch/README.md b/examples/extension-hatch/README.md new file mode 100644 index 000000000..6ddb727e3 --- /dev/null +++ b/examples/extension-hatch/README.md @@ -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..f1de1ebb6 --- /dev/null +++ b/examples/extension-hatch/examplePy/temperature.c @@ -0,0 +1,61 @@ +#define PY_SSIZE_T_CLEAN +#include + +static PyObject * +temperature_celsius_to_fahrenheit(PyObject *self, PyObject *args) +{ + long celsius; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "l", celsius)) + return NULL; + + celsius = (celsius * 9/5) + 32; + + ret = PyLong_FromLong(celsius); + Py_INCREF(ret); + return ret; +} + +static PyObject * +temperature_fahrenheit_to_celsius(PyObject *self, PyObject *args) +{ + long fahrenheit; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "l", fahrenheit)) + return NULL; + + fahrenheit = (fahrenheit - 32) * 9/5; + + ret = PyLong_FromLong(fahrenheit); + 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..8037e4426 --- /dev/null +++ b/examples/extension-hatch/meson.build @@ -0,0 +1,20 @@ +project( + 'examplePy', + 'c', + version: '0.1.dev0', + license: 'BSD-3', + 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..4402fc74a --- /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.md" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: BSD License", + "Operating System :: OS Independent", +] From 11e3521013a732b3891b7916636ff87cdf233e57 Mon Sep 17 00:00:00 2001 From: Jeremy Paige Date: Mon, 20 May 2024 20:41:10 -0700 Subject: [PATCH 2/5] fix indirection bug Co-authored-by: Ken Seehart --- examples/extension-hatch/examplePy/temperature.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/extension-hatch/examplePy/temperature.c b/examples/extension-hatch/examplePy/temperature.c index f1de1ebb6..86bb0bcea 100644 --- a/examples/extension-hatch/examplePy/temperature.c +++ b/examples/extension-hatch/examplePy/temperature.c @@ -7,7 +7,7 @@ temperature_celsius_to_fahrenheit(PyObject *self, PyObject *args) long celsius; PyObject *ret; - if (!PyArg_ParseTuple(args, "l", celsius)) + if (!PyArg_ParseTuple(args, "l", &celsius)) return NULL; celsius = (celsius * 9/5) + 32; @@ -23,7 +23,7 @@ temperature_fahrenheit_to_celsius(PyObject *self, PyObject *args) long fahrenheit; PyObject *ret; - if (!PyArg_ParseTuple(args, "l", fahrenheit)) + if (!PyArg_ParseTuple(args, "l", &fahrenheit)) return NULL; fahrenheit = (fahrenheit - 32) * 9/5; From 565f8d492136986ce3425c97aa2c80ab4ffeadac Mon Sep 17 00:00:00 2001 From: Jeremiah Paige Date: Mon, 20 May 2024 20:46:48 -0700 Subject: [PATCH 3/5] use explicit return names --- examples/extension-hatch/examplePy/temperature.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/extension-hatch/examplePy/temperature.c b/examples/extension-hatch/examplePy/temperature.c index 86bb0bcea..2317da313 100644 --- a/examples/extension-hatch/examplePy/temperature.c +++ b/examples/extension-hatch/examplePy/temperature.c @@ -5,14 +5,15 @@ static PyObject * temperature_celsius_to_fahrenheit(PyObject *self, PyObject *args) { long celsius; + long fahrenheit; PyObject *ret; if (!PyArg_ParseTuple(args, "l", &celsius)) return NULL; - celsius = (celsius * 9/5) + 32; + fahrenheit = (celsius * 9/5) + 32; - ret = PyLong_FromLong(celsius); + ret = PyLong_FromLong(fahrenheit); Py_INCREF(ret); return ret; } @@ -21,14 +22,15 @@ static PyObject * temperature_fahrenheit_to_celsius(PyObject *self, PyObject *args) { long fahrenheit; + long celsius; PyObject *ret; if (!PyArg_ParseTuple(args, "l", &fahrenheit)) return NULL; - fahrenheit = (fahrenheit - 32) * 9/5; + celsius = (fahrenheit - 32) * 9/5; - ret = PyLong_FromLong(fahrenheit); + ret = PyLong_FromLong(celsius); Py_INCREF(ret); return ret; } From 7e2313e87b392a56b7fc53835856ffa90038bbb7 Mon Sep 17 00:00:00 2001 From: Jeremiah Paige Date: Thu, 23 May 2024 11:44:02 -0700 Subject: [PATCH 4/5] use plaintext README --- examples/extension-hatch/{README.md => README} | 0 examples/extension-hatch/pyproject.toml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename examples/extension-hatch/{README.md => README} (100%) diff --git a/examples/extension-hatch/README.md b/examples/extension-hatch/README similarity index 100% rename from examples/extension-hatch/README.md rename to examples/extension-hatch/README diff --git a/examples/extension-hatch/pyproject.toml b/examples/extension-hatch/pyproject.toml index 4402fc74a..d8c9092fb 100644 --- a/examples/extension-hatch/pyproject.toml +++ b/examples/extension-hatch/pyproject.toml @@ -15,7 +15,7 @@ maintainers = [ ] description = "An example Python package used to support Python packaging tutorials" keywords = ["pyOpenSci", "python packaging"] -readme = "README.md" +readme = "README" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: BSD License", From 996de5a7379d0e6222dc204b6486de328c0bdeb8 Mon Sep 17 00:00:00 2001 From: Jeremy Paige Date: Fri, 31 May 2024 12:56:42 -0700 Subject: [PATCH 5/5] Update examples/extension-hatch/meson.build --- examples/extension-hatch/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/extension-hatch/meson.build b/examples/extension-hatch/meson.build index 8037e4426..dca69047d 100644 --- a/examples/extension-hatch/meson.build +++ b/examples/extension-hatch/meson.build @@ -2,7 +2,7 @@ project( 'examplePy', 'c', version: '0.1.dev0', - license: 'BSD-3', + license: 'MIT', meson_version: '>= 0.64.0', default_options: [ 'buildtype=debugoptimized',