Skip to content

Commit

Permalink
pyo3-ffi-pure
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Feb 8, 2022
1 parent aa5685b commit d873dd5
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test-crates/pyo3-ffi-pure/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test-crates/pyo3-ffi-pure/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
25 changes: 25 additions & 0 deletions test-crates/pyo3-ffi-pure/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 18 additions & 0 deletions test-crates/pyo3-ffi-pure/Readme.md
Original file line number Diff line number Diff line change
@@ -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
```
15 changes: 15 additions & 0 deletions test-crates/pyo3-ffi-pure/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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" }
53 changes: 53 additions & 0 deletions test-crates/pyo3-ffi-pure/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::<PyCFunctionWithKeywords, PyCFunction>(
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)
}
6 changes: 6 additions & 0 deletions test-crates/pyo3-ffi-pure/test_pyo3_ffi_pure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python3

import pyo3_ffi_pure

def test_static():
assert pyo3_ffi_pure.sum(2, 40) == 42

0 comments on commit d873dd5

Please sign in to comment.