Skip to content

Commit 7a9bd6d

Browse files
committed
create meson example
1 parent a9fc8c1 commit 7a9bd6d

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

examples/extension-hatch/README.md

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

examples/extension-hatch/meson.build

Lines changed: 20 additions & 0 deletions
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: 'BSD-3',
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')
Lines changed: 23 additions & 0 deletions
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.md"
19+
classifiers = [
20+
"Programming Language :: Python :: 3",
21+
"License :: OSI Approved :: BSD License",
22+
"Operating System :: OS Independent",
23+
]

0 commit comments

Comments
 (0)