Skip to content

Commit

Permalink
Remove symbols not available in abi3
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Sep 23, 2020
1 parent d8c8c17 commit 381904e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/ffi/funcobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crate::ffi::object::{PyObject, PyTypeObject, Py_TYPE};

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg(not(Py_LIMITED_API))]
#[cfg_attr(PyPy, link_name = "PyPyFunction_Type")]
pub static mut PyFunction_Type: PyTypeObject;
}

#[cfg(not(Py_LIMITED_API))]
#[inline]
pub unsafe fn PyFunction_Check(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyFunction_Type) as c_int
Expand Down
1 change: 1 addition & 0 deletions src/ffi/marshal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::PyObject;
use std::os::raw::{c_char, c_int};

#[cfg(not(Py_LIMITED_API))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyMarshal_WriteObjectToString")]
pub fn PyMarshal_WriteObjectToString(object: *mut PyObject, version: c_int) -> *mut PyObject;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ mod gil;
mod instance;
#[macro_use]
mod internal_tricks;
#[cfg(not(Py_LIMITED_API))]
pub mod marshal;
pub mod once_cell;
pub mod panic;
Expand Down
2 changes: 2 additions & 0 deletions src/marshal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(not(Py_LIMITED_API))]

use crate::ffi;
use crate::types::{PyAny, PyBytes};
use crate::{AsPyPointer, FromPyPointer, PyResult, Python};
Expand Down
1 change: 1 addition & 0 deletions src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ impl PyCFunction {
#[repr(transparent)]
pub struct PyFunction(PyAny);

#[cfg(not(Py_LIMITED_API))]
pyobject_native_var_type!(PyFunction, ffi::PyFunction_Type, ffi::PyFunction_Check);
28 changes: 21 additions & 7 deletions tests/test_pyfunction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#[cfg(not(Py_LIMITED_API))]
use pyo3::buffer::PyBuffer;
use pyo3::prelude::*;
use pyo3::types::{PyCFunction, PyFunction};
use pyo3::types::PyCFunction;
#[cfg(not(Py_LIMITED_API))]
use pyo3::types::PyFunction;
use pyo3::{raw_pycfunction, wrap_pyfunction};

mod common;
Expand Down Expand Up @@ -67,6 +69,7 @@ assert a, array.array("i", [2, 4, 6, 8])
);
}

#[cfg(not(Py_LIMITED_API))]
#[pyfunction]
fn function_with_pyfunction_arg(fun: &PyFunction) -> PyResult<&PyAny> {
fun.call((), None)
Expand All @@ -81,21 +84,32 @@ fn function_with_pycfunction_arg(fun: &PyCFunction) -> PyResult<&PyAny> {
fn test_functions_with_function_args() {
let gil = Python::acquire_gil();
let py = gil.python();
let py_func_arg = wrap_pyfunction!(function_with_pyfunction_arg)(py).unwrap();
let py_cfunc_arg = wrap_pyfunction!(function_with_pycfunction_arg)(py).unwrap();
let bool_to_string = wrap_pyfunction!(optional_bool)(py).unwrap();

pyo3::py_run!(
py,
py_func_arg
py_cfunc_arg
bool_to_string,
py_cfunc_arg
bool_to_string,
r#"
def foo(): return "bar"
assert py_func_arg(foo) == "bar"
assert py_cfunc_arg(bool_to_string) == "Some(true)"
"#
)
);

#[cfg(not(Py_LIMITED_API))]
{
let py_func_arg = wrap_pyfunction!(function_with_pyfunction_arg)(py).unwrap();

pyo3::py_run!(
py,
py_func_arg,
r#"
def foo(): return "bar"
assert py_func_arg(foo) == "bar"
"#
);
}
}

#[test]
Expand Down

0 comments on commit 381904e

Please sign in to comment.