Skip to content

Commit

Permalink
Merge pull request #2439 from davidhewitt/ffi-descrobject
Browse files Browse the repository at this point in the history
ffi: tidy descrobject.rs
  • Loading branch information
davidhewitt committed Jun 7, 2022
2 parents 330fccc + 866ddac commit 5603fa0
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 87 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix FFI definition `_inittab` field `initfunc` typo'd as `initfun`. [#2431](https://github.com/PyO3/pyo3/pull/2431)
- Fix FFI definitions `_PyDateTime_BaseTime` and `_PyDateTime_BaseDateTime` incorrectly having `fold` member. [#2432](https://github.com/PyO3/pyo3/pull/2432)
- Fix FFI definitions `PyTypeObject`. `PyHeapTypeObject`, and `PyCFunctionObject` having incorrect members on PyPy 3.9. [#2428](https://github.com/PyO3/pyo3/pull/2428)
- Fix FFI definition `PyGetSetDef` to have `*const c_char` for `doc` member (not `*mut c_char`). [#2439](https://github.com/PyO3/pyo3/pull/2439)

## [0.16.5] - 2022-05-15

Expand Down
78 changes: 78 additions & 0 deletions pyo3-ffi/src/cpython/descrobject.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use crate::{PyGetSetDef, PyMethodDef, PyObject, PyTypeObject};
use std::os::raw::{c_char, c_int, c_void};

pub type wrapperfunc = Option<
unsafe extern "C" fn(
slf: *mut PyObject,
args: *mut PyObject,
wrapped: *mut c_void,
) -> *mut PyObject,
>;

pub type wrapperfunc_kwds = Option<
unsafe extern "C" fn(
slf: *mut PyObject,
args: *mut PyObject,
wrapped: *mut c_void,
kwds: *mut PyObject,
) -> *mut PyObject,
>;

#[repr(C)]
pub struct wrapperbase {
pub name: *const c_char,
pub offset: c_int,
pub function: *mut c_void,
pub wrapper: wrapperfunc,
pub doc: *const c_char,
pub flags: c_int,
pub name_strobj: *mut PyObject,
}

pub const PyWrapperFlag_KEYWORDS: c_int = 1;

#[repr(C)]
pub struct PyDescrObject {
pub ob_base: PyObject,
pub d_type: *mut PyTypeObject,
pub d_name: *mut PyObject,
pub d_qualname: *mut PyObject,
}

// skipped non-limited PyDescr_TYPE
// skipped non-limited PyDescr_NAME

#[repr(C)]
pub struct PyMethodDescrObject {
pub d_common: PyDescrObject,
pub d_method: *mut PyMethodDef,
#[cfg(all(not(PyPy), Py_3_8))]
pub vectorcall: Option<crate::vectorcallfunc>,
}

#[repr(C)]
pub struct PyMemberDescrObject {
pub d_common: PyDescrObject,
pub d_member: *mut PyGetSetDef,
}

#[repr(C)]
pub struct PyGetSetDescrObject {
pub d_common: PyDescrObject,
pub d_getset: *mut PyGetSetDef,
}

#[repr(C)]
pub struct PyWrapperDescrObject {
pub d_common: PyDescrObject,
pub d_base: *mut wrapperbase,
pub d_wrapped: *mut c_void,
}

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
pub static mut _PyMethodWrapper_Type: PyTypeObject;
}

// skipped non-limited PyDescr_NewWrapper
// skipped non-limited PyDescr_IsData
2 changes: 2 additions & 0 deletions pyo3-ffi/src/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod bytesobject;
pub(crate) mod ceval;
pub(crate) mod code;
pub(crate) mod compile;
pub(crate) mod descrobject;
#[cfg(not(PyPy))]
pub(crate) mod dictobject;
// skipped fileobject.h
Expand Down Expand Up @@ -39,6 +40,7 @@ pub use self::bytesobject::*;
pub use self::ceval::*;
pub use self::code::*;
pub use self::compile::*;
pub use self::descrobject::*;
#[cfg(not(PyPy))]
pub use self::dictobject::*;
pub use self::frameobject::*;
Expand Down
92 changes: 7 additions & 85 deletions pyo3-ffi/src/descrobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct PyGetSetDef {
pub name: *mut c_char,
pub get: Option<getter>,
pub set: Option<setter>,
pub doc: *mut c_char,
pub doc: *const c_char,
pub closure: *mut c_void,
}

Expand All @@ -24,89 +24,12 @@ impl Default for PyGetSetDef {
name: ptr::null_mut(),
get: None,
set: None,
doc: ptr::null_mut(),
doc: ptr::null(),
closure: ptr::null_mut(),
}
}
}

#[cfg(not(Py_LIMITED_API))]
pub type wrapperfunc = Option<
unsafe extern "C" fn(
slf: *mut PyObject,
args: *mut PyObject,
wrapped: *mut c_void,
) -> *mut PyObject,
>;

#[cfg(not(Py_LIMITED_API))]
pub type wrapperfunc_kwds = Option<
unsafe extern "C" fn(
slf: *mut PyObject,
args: *mut PyObject,
wrapped: *mut c_void,
kwds: *mut PyObject,
) -> *mut PyObject,
>;

#[cfg(not(Py_LIMITED_API))]
#[repr(C)]
pub struct wrapperbase {
pub name: *const c_char,
pub offset: c_int,
pub function: *mut c_void,
pub wrapper: wrapperfunc,
pub doc: *const c_char,
pub flags: c_int,
pub name_strobj: *mut PyObject,
}

#[cfg(not(Py_LIMITED_API))]
pub const PyWrapperFlag_KEYWORDS: c_int = 1;

#[cfg(not(Py_LIMITED_API))]
#[repr(C)]
pub struct PyDescrObject {
pub ob_base: PyObject,
pub d_type: *mut PyTypeObject,
pub d_name: *mut PyObject,
pub d_qualname: *mut PyObject,
}

// skipped non-limited PyDescr_TYPE
// skipped non-limited PyDescr_NAME

#[cfg(not(Py_LIMITED_API))]
#[repr(C)]
pub struct PyMethodDescrObject {
pub d_common: PyDescrObject,
pub d_method: *mut PyMethodDef,
#[cfg(all(not(PyPy), Py_3_8))]
pub vectorcall: Option<crate::vectorcallfunc>,
}

#[cfg(not(Py_LIMITED_API))]
#[repr(C)]
pub struct PyMemberDescrObject {
pub d_common: PyDescrObject,
pub d_member: *mut PyGetSetDef,
}

#[cfg(not(Py_LIMITED_API))]
#[repr(C)]
pub struct PyGetSetDescrObject {
pub d_common: PyDescrObject,
pub d_getset: *mut PyGetSetDef,
}

#[cfg(not(Py_LIMITED_API))]
#[repr(C)]
pub struct PyWrapperDescrObject {
pub d_common: PyDescrObject,
pub d_base: *mut wrapperbase,
pub d_wrapped: *mut c_void,
}

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyClassMethodDescr_Type")]
Expand All @@ -121,22 +44,21 @@ extern "C" {
pub static mut PyWrapperDescr_Type: PyTypeObject;
#[cfg_attr(PyPy, link_name = "PyPyDictProxy_Type")]
pub static mut PyDictProxy_Type: PyTypeObject;
// skipped non-limited _PyMethodWrapper_Type
#[cfg_attr(PyPy, link_name = "PyPyProperty_Type")]
pub static mut PyProperty_Type: PyTypeObject;
}

extern "C" {
pub fn PyDescr_NewMethod(arg1: *mut PyTypeObject, arg2: *mut PyMethodDef) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyDescr_NewClassMethod")]
pub fn PyDescr_NewClassMethod(arg1: *mut PyTypeObject, arg2: *mut PyMethodDef)
-> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyDescr_NewMember")]
pub fn PyDescr_NewMember(arg1: *mut PyTypeObject, arg2: *mut PyMemberDef) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyDescr_NewGetSet")]
pub fn PyDescr_NewGetSet(arg1: *mut PyTypeObject, arg2: *mut PyGetSetDef) -> *mut PyObject;
// skipped non-limited PyDescr_NewWrapper
// skipped non-limited PyDescr_IsData

#[cfg_attr(PyPy, link_name = "PyPyDictProxy_New")]
pub fn PyDictProxy_New(arg1: *mut PyObject) -> *mut PyObject;
pub fn PyWrapper_New(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;

#[cfg_attr(PyPy, link_name = "PyPyProperty_Type")]
pub static mut PyProperty_Type: PyTypeObject;
}
4 changes: 2 additions & 2 deletions src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ fn method_defs_to_pyclass_info(
name: "__dict__\0".as_ptr() as *mut c_char,
get: Some(ffi::PyObject_GenericGetDict),
set: Some(ffi::PyObject_GenericSetDict),
doc: ptr::null_mut(),
doc: ptr::null(),
closure: ptr::null_mut(),
});
}
Expand Down Expand Up @@ -435,7 +435,7 @@ const PY_GET_SET_DEF_INIT: ffi::PyGetSetDef = ffi::PyGetSetDef {
name: ptr::null_mut(),
get: None,
set: None,
doc: ptr::null_mut(),
doc: ptr::null(),
closure: ptr::null_mut(),
};

Expand Down

0 comments on commit 5603fa0

Please sign in to comment.