From edc77e8a245fc4545c98a07b762e96a913520fbd Mon Sep 17 00:00:00 2001 From: Bojan Serafimov Date: Tue, 30 Aug 2022 11:28:22 -0400 Subject: [PATCH] Add pyo3 prototype --- .gitignore | 1 + Cargo.toml | 4 + bindings/python/neon-dev-utils/Cargo.lock | 264 ++++++++++++++++++ bindings/python/neon-dev-utils/Cargo.toml | 16 ++ bindings/python/neon-dev-utils/poetry.lock | 31 ++ bindings/python/neon-dev-utils/pyproject.toml | 15 + bindings/python/neon-dev-utils/src/lib.rs | 17 ++ pyproject.toml | 1 + test_runner/batch_others/test_bindings.py | 5 + 9 files changed, 354 insertions(+) create mode 100644 bindings/python/neon-dev-utils/Cargo.lock create mode 100644 bindings/python/neon-dev-utils/Cargo.toml create mode 100644 bindings/python/neon-dev-utils/poetry.lock create mode 100644 bindings/python/neon-dev-utils/pyproject.toml create mode 100644 bindings/python/neon-dev-utils/src/lib.rs create mode 100644 test_runner/batch_others/test_bindings.py diff --git a/.gitignore b/.gitignore index ed718c8c79..cdb302b2cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target +/bindings/python/neon-dev-utils/target /tmp_check /tmp_install /tmp_check_cli diff --git a/Cargo.toml b/Cargo.toml index 18407a27e3..04659b59ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,10 @@ members = [ "integration_tests", "libs/*", ] +exclude = [ + "bindings/python/neon-dev-utils", +] + [profile.release] # This is useful for profiling and, to some extent, debug. diff --git a/bindings/python/neon-dev-utils/Cargo.lock b/bindings/python/neon-dev-utils/Cargo.lock new file mode 100644 index 0000000000..5a5ba398b8 --- /dev/null +++ b/bindings/python/neon-dev-utils/Cargo.lock @@ -0,0 +1,264 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "indoc" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8" +dependencies = [ + "indoc-impl", + "proc-macro-hack", +] + +[[package]] +name = "indoc-impl" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", + "unindent", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "libc" +version = "0.2.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" + +[[package]] +name = "lock_api" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f80bf5aacaf25cbfc8210d1cfb718f2bf3b11c4c54e5afe36c236853a8ec390" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "neon-dev-utils" +version = "0.1.0" +dependencies = [ + "pyo3", +] + +[[package]] +name = "once_cell" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" + +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", +] + +[[package]] +name = "paste" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" +dependencies = [ + "paste-impl", + "proc-macro-hack", +] + +[[package]] +name = "paste-impl" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" +dependencies = [ + "proc-macro-hack", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pyo3" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d41d50a7271e08c7c8a54cd24af5d62f73ee3a6f6a314215281ebdec421d5752" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "parking_lot", + "paste", + "pyo3-build-config", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "779239fc40b8e18bc8416d3a37d280ca9b9fb04bda54b98037bb6748595c2410" +dependencies = [ + "once_cell", +] + +[[package]] +name = "pyo3-macros" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b247e8c664be87998d8628e86f282c25066165f1f8dda66100c48202fdb93a" +dependencies = [ + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a8c2812c412e00e641d99eeb79dd478317d981d938aa60325dfa7157b607095" +dependencies = [ + "proc-macro2", + "pyo3-build-config", + "quote", + "syn", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "smallvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + +[[package]] +name = "unindent" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58ee9362deb4a96cef4d437d1ad49cffc9b9e92d202b6995674e928ce684f112" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/bindings/python/neon-dev-utils/Cargo.toml b/bindings/python/neon-dev-utils/Cargo.toml new file mode 100644 index 0000000000..9ad725400b --- /dev/null +++ b/bindings/python/neon-dev-utils/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "neon-dev-utils" +version = "0.1.0" +edition = "2021" + +[lib] +name = "neon_dev_utils" +# "cdylib" is necessary to produce a shared library for Python to import from. +# +# Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able +# to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.: +# crate-type = ["cdylib", "rlib"] +crate-type = ["cdylib"] + +[dependencies] +pyo3 = { version = "0.15.1", features = ["extension-module"] } diff --git a/bindings/python/neon-dev-utils/poetry.lock b/bindings/python/neon-dev-utils/poetry.lock new file mode 100644 index 0000000000..f6cb84e7db --- /dev/null +++ b/bindings/python/neon-dev-utils/poetry.lock @@ -0,0 +1,31 @@ +[[package]] +name = "maturin" +version = "0.13.2" +description = "Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages" +category = "dev" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} + +[package.extras] +zig = ["ziglang (>=0.9.0,<0.10.0)"] +patchelf = ["patchelf"] + +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +category = "dev" +optional = false +python-versions = ">=3.7" + +[metadata] +lock-version = "1.1" +python-versions = "^3.10" +content-hash = "4e177514d6cf74b58bcd8ca30ef300c10a833b3e6b1d809aa57337ee20efeb47" + +[metadata.files] +maturin = [] +tomli = [] diff --git a/bindings/python/neon-dev-utils/pyproject.toml b/bindings/python/neon-dev-utils/pyproject.toml new file mode 100644 index 0000000000..b4282db679 --- /dev/null +++ b/bindings/python/neon-dev-utils/pyproject.toml @@ -0,0 +1,15 @@ +[tool.poetry] +name = "neon-dev-utils" +version = "0.1.0" +description = "Python bindings for common neon development utils" +authors = ["Your Name "] + +[tool.poetry.dependencies] +python = "^3.10" + +[tool.poetry.dev-dependencies] +maturin = "^0.13.2" + +[build-system] +requires = ["maturin>=0.13.2", "poetry-core>=1.0.0"] +build-backend = "maturin" diff --git a/bindings/python/neon-dev-utils/src/lib.rs b/bindings/python/neon-dev-utils/src/lib.rs new file mode 100644 index 0000000000..ced1d66d53 --- /dev/null +++ b/bindings/python/neon-dev-utils/src/lib.rs @@ -0,0 +1,17 @@ +use pyo3::prelude::*; + +/// Formats the sum of two numbers as string. +#[pyfunction] +fn sum_as_string(a: usize, b: usize) -> PyResult { + Ok((a + b).to_string()) +} + +/// A Python module implemented in Rust. The name of this function must match +/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to +/// import the module. +#[pymodule] +fn neon_dev_utils(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + + Ok(()) +} diff --git a/pyproject.toml b/pyproject.toml index 8a3d22f088..b4a73136d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ prometheus-client = "^0.14.1" pytest-timeout = "^2.1.0" Werkzeug = "2.1.2" pytest-order = "^1.0.1" +neon-dev-utils = {path = "./bindings/python/neon-dev-utils"} [tool.poetry.dev-dependencies] yapf = "==0.31.0" diff --git a/test_runner/batch_others/test_bindings.py b/test_runner/batch_others/test_bindings.py new file mode 100644 index 0000000000..78d3844d2b --- /dev/null +++ b/test_runner/batch_others/test_bindings.py @@ -0,0 +1,5 @@ +from neon_dev_utils import sum_as_string + + +def test_neon_dev_utils(): + assert sum_as_string(2, 3) == "5"