Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffi cleanup: abstract.h #1436

Merged
merged 6 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Remove FFI definition `PyCFunction_ClearFreeList` for Python 3.9 and later. [#1425](https://github.com/PyO3/pyo3/pull/1425)
- `PYO3_CROSS_LIB_DIR` enviroment variable no long required when compiling for x86-64 Python from macOS arm64 and reverse. [#1428](https://github.com/PyO3/pyo3/pull/1428)
- Fix FFI definition `_PyEval_RequestCodeExtraIndex` which took an argument of the wrong type. [#1429](https://github.com/PyO3/pyo3/pull/1429)
- Fix FFI definition `PyIndex_Check` missing with the `abi3` feature. [#1436](https://github.com/PyO3/pyo3/pull/1436)

## [0.13.2] - 2021-02-12
### Packaging
Expand Down
39 changes: 30 additions & 9 deletions src/ffi/objectabstract.rs → src/ffi/abstract_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ extern "C" {
...
) -> *mut PyObject;

// skipped _PyObject_CallFunction_SizeT
// skipped _PyObject_CallMethod_SizeT

#[cfg_attr(PyPy, link_name = "PyPyObject_CallFunctionObjArgs")]
pub fn PyObject_CallFunctionObjArgs(callable: *mut PyObject, ...) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyObject_CallMethodObjArgs")]
Expand Down Expand Up @@ -84,16 +87,30 @@ extern "C" {
pub fn PyObject_Format(obj: *mut PyObject, format_spec: *mut PyObject) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyObject_GetIter")]
pub fn PyObject_GetIter(arg1: *mut PyObject) -> *mut PyObject;
}

// PyIter_Check for unlimited API is in cpython/abstract_.rs
#[cfg(any(all(Py_LIMITED_API, Py_3_8), PyPy))]
#[cfg_attr(PyPy, link_name = "PyPyIter_Check")]
pub fn PyIter_Check(obj: *mut PyObject) -> c_int;
// Defined as this macro in Python 3.6, 3.7 limited API, but relies on
// non-limited PyTypeObject. Don't expose this since it cannot be used.
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
#[inline]
pub unsafe fn PyIter_Check(o: *mut PyObject) -> c_int {
Comment on lines +92 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

(match (*crate::ffi::Py_TYPE(o)).tp_iternext {
Some(tp_iternext) => {
tp_iternext as *const std::os::raw::c_void
!= crate::ffi::_PyObject_NextNotImplemented as _
}
None => false,
}) as c_int
}

extern "C" {
#[cfg(any(all(Py_3_8, Py_LIMITED_API), PyPy))]
#[cfg_attr(PyPy, link_name = "PyPyIter_Check")]
pub fn PyIter_Check(obj: *mut PyObject) -> c_int;

#[cfg_attr(PyPy, link_name = "PyPyIter_Next")]
pub fn PyIter_Next(arg1: *mut PyObject) -> *mut PyObject;
// skipped non-limited / 3.10 PyIter_Send

#[cfg_attr(PyPy, link_name = "PyPyNumber_Check")]
pub fn PyNumber_Check(o: *mut PyObject) -> c_int;
Expand Down Expand Up @@ -134,12 +151,10 @@ extern "C" {
pub fn PyNumber_Xor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyNumber_Or")]
pub fn PyNumber_Or(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject;

#[cfg(PyPy)]
#[link_name = "PyPyIndex_Check"]
pub fn PyIndex_Check(o: *mut PyObject) -> c_int;
}

// Defined as this macro in Python 3.6, 3.7 limited API, but relies on
// non-limited PyTypeObject. Don't expose this since it cannot be used.
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
#[inline]
pub unsafe fn PyIndex_Check(o: *mut PyObject) -> c_int {
Expand All @@ -148,6 +163,10 @@ pub unsafe fn PyIndex_Check(o: *mut PyObject) -> c_int {
}

extern "C" {
#[cfg(any(all(Py_3_8, Py_LIMITED_API), PyPy))]
#[link_name = "PyPyIndex_Check"]
pub fn PyIndex_Check(o: *mut PyObject) -> c_int;

#[cfg_attr(PyPy, link_name = "PyPyNumber_Index")]
pub fn PyNumber_Index(o: *mut PyObject) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyNumber_AsSsize_t")]
Expand Down Expand Up @@ -232,7 +251,9 @@ extern "C" {
pub fn PySequence_List(o: *mut PyObject) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPySequence_Fast")]
pub fn PySequence_Fast(o: *mut PyObject, m: *const c_char) -> *mut PyObject;
// TODO: PySequence_Fast macros
// skipped PySequenc_Fast_GET_SIZE
// skipped PySequenc_Fast_GET_ITEM
// skipped PySequenc_Fast_GET_ITEMS
pub fn PySequence_Count(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t;
#[cfg_attr(PyPy, link_name = "PyPySequence_Contains")]
pub fn PySequence_Contains(seq: *mut PyObject, ob: *mut PyObject) -> c_int;
Expand Down
15 changes: 5 additions & 10 deletions src/ffi/cpython/abstract_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,11 @@ extern "C" {
pub fn PyBuffer_Release(view: *mut Py_buffer);
}

#[inline]
#[cfg(not(any(all(Py_3_8, Py_LIMITED_API), PyPy)))]
pub unsafe fn PyIter_Check(o: *mut PyObject) -> c_int {
(match (*crate::ffi::Py_TYPE(o)).tp_iternext {
Some(tp_iternext) => {
tp_iternext as *const c_void != crate::ffi::_PyObject_NextNotImplemented as _
}
None => false,
}) as c_int
}
// PyIter_Check defined in ffi/abstract_.rs
// PyIndex_Check defined in ffi/abstract_.rs
// Not defined here because this file is not compiled under the
// limited API, but the macros need to be defined for 3.6, 3.7 which
// predate the limited API changes.

// skipped PySequence_ITEM

Expand Down
7 changes: 2 additions & 5 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ macro_rules! opaque_struct {
};
}

pub use self::abstract_::*;
pub use self::bltinmodule::*;
pub use self::boolobject::*;
pub use self::bytearrayobject::*;
Expand Down Expand Up @@ -47,7 +48,6 @@ pub use self::methodobject::*;
pub use self::modsupport::*;
pub use self::moduleobject::*;
pub use self::object::*;
pub use self::objectabstract::*; // FIXME: no matching objectabstract.h in cpython master
pub use self::objimpl::*;
pub use self::osmodule::*;
pub use self::pyarena::*;
Expand Down Expand Up @@ -76,8 +76,7 @@ pub use self::weakrefobject::*;

#[cfg(not(Py_LIMITED_API))]
pub use self::cpython::*;

// skipped abstract.h
mod abstract_;
// skipped asdl.h
// skipped ast.h
mod bltinmodule;
Expand Down Expand Up @@ -200,8 +199,6 @@ mod pythonrun; // TODO some functions need to be moved to pylifecycle
mod osmodule;
mod sysmodule; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5

mod objectabstract; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5

// mod pyctype; TODO excluded by PEP-384
mod pystrtod; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
// mod pystrcmp; TODO nothing interesting for Rust?
Expand Down
10 changes: 4 additions & 6 deletions src/ffi/pyerrors.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::ffi::object::*;
#[cfg(PyPy)]
use crate::ffi::objectabstract::PyObject_CallFunction;
use crate::ffi::pyport::Py_ssize_t;
#[cfg(PyPy)]
use std::ffi::CStr;
use std::os::raw::{c_char, c_int};

#[repr(C)]
Expand Down Expand Up @@ -162,9 +158,11 @@ pub unsafe fn PyUnicodeDecodeError_Create(
end: Py_ssize_t,
_reason: *const c_char,
) -> *mut PyObject {
return PyObject_CallFunction(
return crate::ffi::PyObject_CallFunction(
PyExc_UnicodeDecodeError,
CStr::from_bytes_with_nul(b"sy#nns\0").unwrap().as_ptr(),
std::ffi::CStr::from_bytes_with_nul(b"sy#nns\0")
.unwrap()
.as_ptr(),
encoding,
object,
length,
Expand Down