Skip to content

Commit a59643b

Browse files
authored
Merge pull request #274 from ucodery/meson
Add: hatch plus meson-python python package example to guidebook
2 parents 2d7402b + 996de5a commit a59643b

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

Diff for: examples/extension-hatch/README

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
An example Python package used to support Python packaging tutorials
2+
3+
This project demonstrates mixing C and Python code by using the frontend hatch with the backend mesonpy

Diff for: examples/extension-hatch/examplePy/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .temperature import celsius_to_fahrenheit, fahrenheit_to_celsius
2+
3+
__all__ = ["celsius_to_fahrenheit", "fahrenheit_to_celsius"]
4+
__version__ = "0.1.0dev0"

Diff for: examples/extension-hatch/examplePy/meson.build

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
py.extension_module(
2+
'temperature',
3+
'temperature.c',
4+
install: true,
5+
subdir: 'examplePy'
6+
)
7+
8+
python_sources = [
9+
'__init__.py'
10+
]
11+
12+
py.install_sources(
13+
python_sources,
14+
subdir: 'examplePy'
15+
)

Diff for: examples/extension-hatch/examplePy/temperature.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python.h>
3+
4+
static PyObject *
5+
temperature_celsius_to_fahrenheit(PyObject *self, PyObject *args)
6+
{
7+
long celsius;
8+
long fahrenheit;
9+
PyObject *ret;
10+
11+
if (!PyArg_ParseTuple(args, "l", &celsius))
12+
return NULL;
13+
14+
fahrenheit = (celsius * 9/5) + 32;
15+
16+
ret = PyLong_FromLong(fahrenheit);
17+
Py_INCREF(ret);
18+
return ret;
19+
}
20+
21+
static PyObject *
22+
temperature_fahrenheit_to_celsius(PyObject *self, PyObject *args)
23+
{
24+
long fahrenheit;
25+
long celsius;
26+
PyObject *ret;
27+
28+
if (!PyArg_ParseTuple(args, "l", &fahrenheit))
29+
return NULL;
30+
31+
celsius = (fahrenheit - 32) * 9/5;
32+
33+
ret = PyLong_FromLong(celsius);
34+
Py_INCREF(ret);
35+
return ret;
36+
}
37+
38+
static PyMethodDef CoreMethods[] = {
39+
{"celsius_to_fahrenheit", temperature_celsius_to_fahrenheit, METH_VARARGS, "Convert temperature from Celsius to Fahrenheit"},
40+
{"fahrenheit_to_celsius", temperature_fahrenheit_to_celsius, METH_VARARGS, "Convert temperature from Fahrenheit to Celsius"},
41+
{NULL, NULL, 0, NULL} /* Sentinel */
42+
};
43+
44+
static struct PyModuleDef temperaturemodule = {
45+
PyModuleDef_HEAD_INIT,
46+
"temperature", /* name of module */
47+
NULL, /* module documentation, may be NULL */
48+
-1, /* size of per-interpreter state of the module,
49+
or -1 if the module keeps state in global variables. */
50+
CoreMethods
51+
};
52+
53+
PyMODINIT_FUNC
54+
PyInit_temperature(void)
55+
{
56+
PyObject *m;
57+
58+
m = PyModule_Create(&temperaturemodule);
59+
if (m == NULL)
60+
return NULL;
61+
62+
return m;
63+
}

Diff for: examples/extension-hatch/meson.build

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
project(
2+
'examplePy',
3+
'c',
4+
version: '0.1.dev0',
5+
license: 'MIT',
6+
meson_version: '>= 0.64.0',
7+
default_options: [
8+
'buildtype=debugoptimized',
9+
'c_std=c99',
10+
'cpp_std=c++14',
11+
],
12+
)
13+
14+
cc = meson.get_compiler('c')
15+
16+
py_mod = import('python')
17+
py = py_mod.find_installation(pure: false)
18+
py_dep = py.dependency()
19+
20+
subdir('examplePy')

Diff for: examples/extension-hatch/pyproject.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[build-system]
2+
build-backend = "mesonpy"
3+
requires = [
4+
"meson-python>=0.13.0rc0",
5+
]
6+
7+
[project]
8+
name = "examplePy"
9+
version = "0.1"
10+
authors = [
11+
{name = "Some Maintainer", email = "some-email@pyopensci.org"},
12+
]
13+
maintainers = [
14+
{name = "All the contributors"},
15+
]
16+
description = "An example Python package used to support Python packaging tutorials"
17+
keywords = ["pyOpenSci", "python packaging"]
18+
readme = "README"
19+
classifiers = [
20+
"Programming Language :: Python :: 3",
21+
"License :: OSI Approved :: BSD License",
22+
"Operating System :: OS Independent",
23+
]

0 commit comments

Comments
 (0)