-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "[email protected]"} | ||
] | ||
license = { file = "LICENSE" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |