Merge pull request #1346 from PyO3/py39-dict-and-weakref-tweaks

Use inline fn instead of macro_rules to construct PyMemberDef
This commit is contained in:
David Hewitt 2020-12-28 15:55:58 +00:00 committed by GitHub
commit acbfaf4a06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 12 deletions

View File

@ -354,28 +354,27 @@ fn py_class_method_defs<T: PyMethods>() -> (
/// Only works on Python 3.9 and up.
#[cfg(Py_3_9)]
fn py_class_members<T: PyClass>() -> Vec<ffi::structmember::PyMemberDef> {
macro_rules! offset_def {
($name:literal, $offset:expr) => {
ffi::structmember::PyMemberDef {
name: $name.as_ptr() as _,
type_code: ffi::structmember::T_PYSSIZET,
offset: $offset,
flags: ffi::structmember::READONLY,
doc: std::ptr::null_mut(),
}
};
#[inline(always)]
fn offset_def(name: &'static str, offset: usize) -> ffi::structmember::PyMemberDef {
ffi::structmember::PyMemberDef {
name: name.as_ptr() as _,
type_code: ffi::structmember::T_PYSSIZET,
offset: offset as _,
flags: ffi::structmember::READONLY,
doc: std::ptr::null_mut(),
}
}
let mut members = Vec::new();
// __dict__ support
if let Some(dict_offset) = PyCell::<T>::dict_offset() {
members.push(offset_def!("__dictoffset__\0", dict_offset as _));
members.push(offset_def("__dictoffset__\0", dict_offset));
}
// weakref support
if let Some(weakref_offset) = PyCell::<T>::weakref_offset() {
members.push(offset_def!("__weaklistoffset__\0", weakref_offset as _));
members.push(offset_def("__weaklistoffset__\0", weakref_offset));
}
if !members.is_empty() {