From a8ec4664e4dec00f9e31b71a1c206f56da6225c6 Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Wed, 15 Nov 2023 17:11:39 +0100 Subject: [PATCH] Initial commit. --- .gitignore | 3 +++ Cargo.lock | 7 +++++++ Cargo.toml | 11 +++++++++++ README.md | 19 +++++++++++++++++++ python_add.c | 39 +++++++++++++++++++++++++++++++++++++++ setup.py | 7 +++++++ src/lib.rs | 5 +++++ 7 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 python_add.c create mode 100644 setup.py create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cd5f4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/Cargo.lock +build/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..421fa90 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust_add_lib" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2691e2b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rust_add_lib" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[lib] +crate-type = ["cdylib"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..700a62b --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Calling a rust function from Python + +- `cargo build` builds a `./target/debug/librust_add_lib.so` with a `rust_add` function in it. +- `LDFLAGS='-L target/debug/' python setup.py build` builds a Python module using `rust_add_lib`. + +Then the module can be imported and used from Python: + +```bash +$ LD_LIBRARY_PATH=target/debug PYTHONPATH='./build/lib.linux-x86_64-cpython-311/' python +Python 3.11.6 (main, Oct 8 2023, 05:06:43) [GCC 13.2.0] on linux +Type "help", "copyright", "credits" or "license" for more information. +>>> import add +>>> add.add(.1, .2) +[src/lib.rs:3] left = 0.1 +[src/lib.rs:3] right = 0.2 +[src/lib.rs:4] left + right = 0.30000000000000004 +0.30000000000000004 +>>> +``` diff --git a/python_add.c b/python_add.c new file mode 100644 index 0000000..96aa837 --- /dev/null +++ b/python_add.c @@ -0,0 +1,39 @@ +#define PY_SSIZE_T_CLEAN +#include + +double rust_add(double, double); // from rust, from librust_lib.so + + +static PyObject *python_add(PyObject *self, PyObject *args) +{ + double float_left; + double float_right; + double result; + + if (!PyArg_ParseTuple(args, "dd", &float_left, &float_right)) + return NULL; + + result = rust_add(float_left, float_right); + + return PyFloat_FromDouble(result); +} + +static PyMethodDef AddMethods[] = { + {"add", python_add, METH_VARARGS, + "Just add from rust."}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +static struct PyModuleDef addmodule = { + PyModuleDef_HEAD_INIT, + "add", /* 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. */ + AddMethods +}; + +PyMODINIT_FUNC PyInit_add(void) +{ + return PyModule_Create(&addmodule); +} diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..429f90d --- /dev/null +++ b/setup.py @@ -0,0 +1,7 @@ +from distutils.core import setup +from distutils.extension import Extension + +setup(name='add', + version='1.0', + ext_modules=[Extension('add', ['python_add.c'], libraries=['rust_add_lib'])], + ) diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f31908a --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +#[no_mangle] +pub extern "C" fn rust_add(left: f64, right: f64) -> f64 { + dbg!(left, right); + dbg!(left + right) +}