Skip to content

Commit

Permalink
Restructure protcol-table initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Sep 5, 2020
1 parent d2a10b6 commit c2f10e2
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 570 deletions.
10 changes: 5 additions & 5 deletions pyo3-derive-backend/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub const OBJECT: Proto = Proto {

pub const ASYNC: Proto = Proto {
name: "Async",
slot_table: "pyo3::ffi::PyAsyncMethods",
slot_table: "pyo3::class::pyasync::PyAsyncMethods",
set_slot_table: "set_async_methods",
methods: &[
MethodProto::UnaryS {
Expand Down Expand Up @@ -220,7 +220,7 @@ pub const ASYNC: Proto = Proto {

pub const BUFFER: Proto = Proto {
name: "Buffer",
slot_table: "pyo3::ffi::PyBufferProcs",
slot_table: "pyo3::class::buffer::PyBufferProcs",
set_slot_table: "set_buffer_methods",
methods: &[
MethodProto::Unary {
Expand Down Expand Up @@ -358,7 +358,7 @@ pub const ITER: Proto = Proto {

pub const MAPPING: Proto = Proto {
name: "Mapping",
slot_table: "pyo3::ffi::PyMappingMethods",
slot_table: "pyo3::class::mapping::PyMappingMethods",
set_slot_table: "set_mapping_methods",
methods: &[
MethodProto::Unary {
Expand Down Expand Up @@ -401,7 +401,7 @@ pub const MAPPING: Proto = Proto {

pub const SEQ: Proto = Proto {
name: "Sequence",
slot_table: "pyo3::ffi::PySequenceMethods",
slot_table: "pyo3::class::sequence::PySequenceMethods",
set_slot_table: "set_sequence_methods",
methods: &[
MethodProto::Unary {
Expand Down Expand Up @@ -467,7 +467,7 @@ pub const SEQ: Proto = Proto {

pub const NUM: Proto = Proto {
name: "Number",
slot_table: "pyo3::ffi::PyNumberMethods",
slot_table: "pyo3::class::number::PyNumberMethods",
set_slot_table: "set_number_methods",
methods: &[
MethodProto::BinaryS {
Expand Down
46 changes: 11 additions & 35 deletions src/class/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
//! [typeobj docs](https://docs.python.org/3/c-api/typeobj.html)
use crate::callback::{HashCallbackOutput, IntoPyCallbackOutput};
use crate::pyclass::maybe_push_slot;
use crate::{exceptions, ffi, FromPyObject, PyAny, PyCell, PyClass, PyErr, PyObject, PyResult};
use std::os::raw::{c_int, c_void};
use std::os::raw::c_int;

/// Operators for the __richcmp__ method
#[derive(Debug)]
Expand Down Expand Up @@ -148,39 +147,6 @@ pub struct PyObjectMethods {

#[doc(hidden)]
impl PyObjectMethods {
pub(crate) fn update_slots(&self, slots: &mut Vec<ffi::PyType_Slot>) {
maybe_push_slot(slots, ffi::Py_tp_str, self.tp_str.map(|v| v as *mut c_void));
maybe_push_slot(
slots,
ffi::Py_tp_repr,
self.tp_repr.map(|v| v as *mut c_void),
);
maybe_push_slot(
slots,
ffi::Py_tp_hash,
self.tp_hash.map(|v| v as *mut c_void),
);
maybe_push_slot(
slots,
ffi::Py_tp_getattro,
self.tp_getattro.map(|v| v as *mut c_void),
);
maybe_push_slot(
slots,
ffi::Py_tp_richcompare,
self.tp_richcompare.map(|v| v as *mut c_void),
);
maybe_push_slot(
slots,
ffi::Py_tp_setattro,
self.tp_setattro.map(|v| v as *mut c_void),
);
maybe_push_slot(
slots,
ffi::Py_nb_bool,
self.nb_bool.map(|v| v as *mut c_void),
);
}
// Set functions used by `#[pyproto]`.
pub fn set_str<T>(&mut self)
where
Expand Down Expand Up @@ -242,6 +208,16 @@ impl PyObjectMethods {
{
self.nb_bool = py_unary_func!(PyObjectBoolProtocol, T::__bool__, c_int);
}

pub(crate) fn update_slots(&self, slots: &mut crate::pyclass::TypeSlots) {
slots.maybe_push(ffi::Py_tp_str, self.tp_str.map(|v| v as _));
slots.maybe_push(ffi::Py_tp_repr, self.tp_repr.map(|v| v as _));
slots.maybe_push(ffi::Py_tp_hash, self.tp_hash.map(|v| v as _));
slots.maybe_push(ffi::Py_tp_getattro, self.tp_getattro.map(|v| v as _));
slots.maybe_push(ffi::Py_tp_richcompare, self.tp_richcompare.map(|v| v as _));
slots.maybe_push(ffi::Py_tp_setattro, self.tp_setattro.map(|v| v as _));
slots.maybe_push(ffi::Py_nb_bool, self.nb_bool.map(|v| v as _));
}
}

fn tp_getattro<T>() -> Option<ffi::binaryfunc>
Expand Down
15 changes: 11 additions & 4 deletions src/class/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
//! For more information check [buffer protocol](https://docs.python.org/3/c-api/buffer.html)
//! c-api
use crate::callback::IntoPyCallbackOutput;
use crate::{
ffi::{self, PyBufferProcs},
PyCell, PyClass, PyRefMut,
};
use crate::{ffi, PyCell, PyClass, PyRefMut};
use std::os::raw::c_int;

#[cfg(Py_LIMITED_API)]
#[derive(Clone, Default)]
pub struct PyBufferProcs {
pub bf_getbuffer: Option<ffi::getbufferproc>,
pub bf_releasebuffer: Option<ffi::releasebufferproc>,
}

#[cfg(not(Py_LIMITED_API))]
pub use ffi::PyBufferProcs;

/// Buffer protocol interface
///
/// For more information check [buffer protocol](https://docs.python.org/3/c-api/buffer.html)
Expand Down
19 changes: 5 additions & 14 deletions src/class/descr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
//! https://docs.python.org/3/reference/datamodel.html#implementing-descriptors)
use crate::callback::IntoPyCallbackOutput;
use crate::pyclass::maybe_push_slot;
use crate::types::PyAny;
use crate::{ffi, FromPyObject, PyClass, PyObject};
use std::os::raw::{c_int, c_void};
use std::os::raw::c_int;

/// Descriptor interface
#[allow(unused_variables)]
Expand Down Expand Up @@ -80,18 +79,6 @@ pub struct PyDescrMethods {

#[doc(hidden)]
impl PyDescrMethods {
pub(crate) fn update_slots(&self, slots: &mut Vec<ffi::PyType_Slot>) {
maybe_push_slot(
slots,
ffi::Py_tp_descr_get,
self.tp_descr_get.map(|v| v as *mut c_void),
);
maybe_push_slot(
slots,
ffi::Py_tp_descr_set,
self.tp_descr_set.map(|v| v as *mut c_void),
);
}
pub fn set_descr_get<T>(&mut self)
where
T: for<'p> PyDescrGetProtocol<'p>,
Expand All @@ -104,4 +91,8 @@ impl PyDescrMethods {
{
self.tp_descr_set = py_ternarys_func!(PyDescrSetProtocol, T::__set__, c_int);
}
pub(crate) fn update_slots(&self, slots: &mut crate::pyclass::TypeSlots) {
slots.maybe_push(ffi::Py_tp_descr_get, self.tp_descr_get.map(|v| v as _));
slots.maybe_push(ffi::Py_tp_descr_set, self.tp_descr_set.map(|v| v as _));
}
}
17 changes: 4 additions & 13 deletions src/class/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! Python GC support
//!
use crate::pyclass::maybe_push_slot;
use crate::{ffi, AsPyPointer, PyCell, PyClass, Python};
use std::os::raw::{c_int, c_void};

Expand All @@ -28,17 +27,9 @@ pub struct PyGCMethods {

#[doc(hidden)]
impl PyGCMethods {
pub(crate) fn update_slots(&self, slots: &mut Vec<ffi::PyType_Slot>) {
maybe_push_slot(
slots,
ffi::Py_tp_traverse,
self.tp_traverse.map(|v| v as *mut c_void),
);
maybe_push_slot(
slots,
ffi::Py_tp_clear,
self.tp_clear.map(|v| v as *mut c_void),
);
pub(crate) fn update_slots(&self, slots: &mut crate::pyclass::TypeSlots) {
slots.maybe_push(ffi::Py_tp_traverse, self.tp_traverse.map(|v| v as _));
slots.maybe_push(ffi::Py_tp_clear, self.tp_clear.map(|v| v as _));
}

pub fn set_traverse<T>(&mut self)
Expand All @@ -57,7 +48,7 @@ impl PyGCMethods {
}

/// Object visitor for GC.
#[derive(Copy, Clone)]
#[derive(Clone)]
pub struct PyVisit<'p> {
visit: ffi::visitproc,
arg: *mut c_void,
Expand Down
18 changes: 4 additions & 14 deletions src/class/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use crate::callback::IntoPyCallbackOutput;
use crate::derive_utils::TryFromPyCell;
use crate::err::PyResult;
use crate::pyclass::maybe_push_slot;
use crate::{ffi, IntoPy, IntoPyPointer, PyClass, PyObject, Python};
use std::os::raw::c_void;

/// Python Iterator Interface.
///
Expand Down Expand Up @@ -81,18 +79,6 @@ pub struct PyIterMethods {

#[doc(hidden)]
impl PyIterMethods {
pub(crate) fn update_slots(&self, slots: &mut Vec<ffi::PyType_Slot>) {
maybe_push_slot(
slots,
ffi::Py_tp_iter,
self.tp_iter.map(|v| v as *mut c_void),
);
maybe_push_slot(
slots,
ffi::Py_tp_iternext,
self.tp_iternext.map(|v| v as *mut c_void),
);
}
pub fn set_iter<T>(&mut self)
where
T: for<'p> PyIterIterProtocol<'p>,
Expand All @@ -105,6 +91,10 @@ impl PyIterMethods {
{
self.tp_iternext = py_unarys_func!(PyIterNextProtocol, T::__next__);
}
pub(crate) fn update_slots(&self, slots: &mut crate::pyclass::TypeSlots) {
slots.maybe_push(ffi::Py_tp_iter, self.tp_iter.map(|v| v as _));
slots.maybe_push(ffi::Py_tp_iternext, self.tp_iternext.map(|v| v as _));
}
}

/// Output of `__next__` which can either `yield` the next value in the iteration, or
Expand Down
21 changes: 20 additions & 1 deletion src/class/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ use crate::callback::IntoPyCallbackOutput;
use crate::err::PyErr;
use crate::{exceptions, ffi, FromPyObject, PyClass, PyObject};

#[cfg(Py_LIMITED_API)]
#[derive(Clone, Default)]
pub struct PyMappingMethods {
pub mp_length: Option<ffi::lenfunc>,
pub mp_subscript: Option<ffi::binaryfunc>,
pub mp_ass_subscript: Option<ffi::objobjargproc>,
}

#[cfg(not(Py_LIMITED_API))]
pub use ffi::PyMappingMethods;

/// Mapping interface
#[allow(unused_variables)]
pub trait PyMappingProtocol<'p>: PyClass {
Expand Down Expand Up @@ -74,7 +85,7 @@ pub trait PyMappingReversedProtocol<'p>: PyMappingProtocol<'p> {
}

#[doc(hidden)]
impl ffi::PyMappingMethods {
impl PyMappingMethods {
pub fn set_length<T>(&mut self)
where
T: for<'p> PyMappingLenProtocol<'p>,
Expand Down Expand Up @@ -111,4 +122,12 @@ impl ffi::PyMappingMethods {
__delitem__
);
}
pub(crate) fn update_slots(&self, slots: &mut crate::pyclass::TypeSlots) {
slots.maybe_push(ffi::Py_mp_length, self.mp_length.map(|v| v as _));
slots.maybe_push(ffi::Py_mp_subscript, self.mp_subscript.map(|v| v as _));
slots.maybe_push(
ffi::Py_mp_ass_subscript,
self.mp_ass_subscript.map(|v| v as _),
);
}
}
Loading

0 comments on commit c2f10e2

Please sign in to comment.