From d873dd5642c9eba5b46ee4c3b76d0543e30c9d57 Mon Sep 17 00:00:00 2001 From: ijl Date: Tue, 8 Feb 2022 23:45:12 +0000 Subject: [PATCH] pyo3-ffi-pure --- test-crates/pyo3-ffi-pure/Cargo.lock | 37 +++++++++++++ test-crates/pyo3-ffi-pure/Cargo.toml | 11 ++++ test-crates/pyo3-ffi-pure/LICENSE | 25 +++++++++ test-crates/pyo3-ffi-pure/Readme.md | 18 +++++++ test-crates/pyo3-ffi-pure/pyproject.toml | 15 ++++++ test-crates/pyo3-ffi-pure/src/lib.rs | 53 +++++++++++++++++++ .../pyo3-ffi-pure/test_pyo3_ffi_pure.py | 6 +++ 7 files changed, 165 insertions(+) create mode 100644 test-crates/pyo3-ffi-pure/Cargo.lock create mode 100644 test-crates/pyo3-ffi-pure/Cargo.toml create mode 100644 test-crates/pyo3-ffi-pure/LICENSE create mode 100644 test-crates/pyo3-ffi-pure/Readme.md create mode 100644 test-crates/pyo3-ffi-pure/pyproject.toml create mode 100644 test-crates/pyo3-ffi-pure/src/lib.rs create mode 100644 test-crates/pyo3-ffi-pure/test_pyo3_ffi_pure.py diff --git a/test-crates/pyo3-ffi-pure/Cargo.lock b/test-crates/pyo3-ffi-pure/Cargo.lock new file mode 100644 index 000000000..fea09b4df --- /dev/null +++ b/test-crates/pyo3-ffi-pure/Cargo.lock @@ -0,0 +1,37 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "libc" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" + +[[package]] +name = "once_cell" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" + +[[package]] +name = "pyo3-build-config" +version = "0.15.1" +dependencies = [ + "once_cell", +] + +[[package]] +name = "pyo3-ffi" +version = "0.15.1" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-ffi-pure" +version = "1.0.0" +dependencies = [ + "pyo3-ffi", +] diff --git a/test-crates/pyo3-ffi-pure/Cargo.toml b/test-crates/pyo3-ffi-pure/Cargo.toml new file mode 100644 index 000000000..ea2846e4e --- /dev/null +++ b/test-crates/pyo3-ffi-pure/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pyo3-ffi-pure" +version = "1.0.0" +edition = "2018" + +[dependencies] +pyo3-ffi = { path = "../../../pyo3/pyo3-ffi", features = ["extension-module"] } + +[lib] +name = "pyo3_ffi_pure" +crate-type = ["cdylib"] diff --git a/test-crates/pyo3-ffi-pure/LICENSE b/test-crates/pyo3-ffi-pure/LICENSE new file mode 100644 index 000000000..3e349a199 --- /dev/null +++ b/test-crates/pyo3-ffi-pure/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2022-present maturin contributors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/test-crates/pyo3-ffi-pure/Readme.md b/test-crates/pyo3-ffi-pure/Readme.md new file mode 100644 index 000000000..2224361b2 --- /dev/null +++ b/test-crates/pyo3-ffi-pure/Readme.md @@ -0,0 +1,18 @@ +# pyo3-ffi-pure + +A package with pyo3-ffi bindings for testing maturin. + +## Usage + +```python +import pyo3_ffi_pure +assert pyo3_ffi_pure.sum(2, 40) == 42 +``` + +## Testing + +Install `pytest` and run: + +```bash +pytest -v test_pyo3_ffi_pure.py +``` diff --git a/test-crates/pyo3-ffi-pure/pyproject.toml b/test-crates/pyo3-ffi-pure/pyproject.toml new file mode 100644 index 000000000..83403ba31 --- /dev/null +++ b/test-crates/pyo3-ffi-pure/pyproject.toml @@ -0,0 +1,15 @@ +[build-system] +requires = ["maturin>=0.12,<0.13"] +build-backend = "maturin" + +[project] +name = "pyo3-ffi-pure" +classifiers = [ + "Programming Language :: Rust" +] +description = "Tests compilation of packages using pyo3-ffi bindings" +readme = "Readme.md" +maintainers = [ + {name = "messense", email = "messense@icloud.com"} +] +license = { file = "LICENSE" } diff --git a/test-crates/pyo3-ffi-pure/src/lib.rs b/test-crates/pyo3-ffi-pure/src/lib.rs new file mode 100644 index 000000000..6348cbe47 --- /dev/null +++ b/test-crates/pyo3-ffi-pure/src/lib.rs @@ -0,0 +1,53 @@ +use pyo3_ffi::*; +use std::os::raw::c_char; + +#[allow(non_snake_case)] +#[no_mangle] +pub unsafe extern "C" fn PyInit_pyo3_ffi_pure() -> *mut PyObject { + let module_name = "pyo3_ffi_pure\0".as_ptr() as *const c_char; + let init = PyModuleDef { + m_base: PyModuleDef_HEAD_INIT, + m_name: module_name, + m_doc: std::ptr::null(), + m_size: 0, + m_methods: std::ptr::null_mut(), + m_slots: std::ptr::null_mut(), + m_traverse: None, + m_clear: None, + m_free: None, + }; + let mptr = PyModule_Create(Box::into_raw(Box::new(init))); + + let wrapped_sum = PyMethodDef { + ml_name: "sum\0".as_ptr() as *const c_char, + ml_meth: Some(std::mem::transmute::( + sum, + )), + ml_flags: METH_VARARGS, + ml_doc: std::ptr::null_mut(), + }; + PyModule_AddObject( + mptr, + "sum\0".as_ptr() as *const c_char, + PyCFunction_NewEx( + Box::into_raw(Box::new(wrapped_sum)), + std::ptr::null_mut(), + PyUnicode_InternFromString(module_name), + ), + ); + + mptr +} + +#[no_mangle] +pub unsafe extern "C" fn sum( + _self: *mut PyObject, + args: *mut PyObject, + _kwds: *mut PyObject, +) -> *mut PyObject { + // this is a minimal test of compilation, not good example code + let val_a = PyTuple_GET_ITEM(args, 0); + let val_b = PyTuple_GET_ITEM(args, 1); + let res: i64 = PyLong_AsLongLong(val_a) + PyLong_AsLongLong(val_b); + PyLong_FromLongLong(res) +} diff --git a/test-crates/pyo3-ffi-pure/test_pyo3_ffi_pure.py b/test-crates/pyo3-ffi-pure/test_pyo3_ffi_pure.py new file mode 100644 index 000000000..77020d70e --- /dev/null +++ b/test-crates/pyo3-ffi-pure/test_pyo3_ffi_pure.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 + +import pyo3_ffi_pure + +def test_static(): + assert pyo3_ffi_pure.sum(2, 40) == 42