Skip to content

Commit

Permalink
Proof of concept of using PEP384s PyType_Spec
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Sep 1, 2020
1 parent 4a05f27 commit 95975b5
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 129 deletions.
42 changes: 34 additions & 8 deletions src/class/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
//! [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;
use std::os::raw::{c_int, c_void};

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

#[doc(hidden)]
impl PyObjectMethods {
pub(crate) fn update_typeobj(&self, type_object: &mut ffi::PyTypeObject) {
type_object.tp_str = self.tp_str;
type_object.tp_repr = self.tp_repr;
type_object.tp_hash = self.tp_hash;
type_object.tp_getattro = self.tp_getattro;
type_object.tp_richcompare = self.tp_richcompare;
type_object.tp_setattro = self.tp_setattro;
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)
Expand Down
17 changes: 13 additions & 4 deletions src/class/descr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//! 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;
use std::os::raw::{c_int, c_void};

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

#[doc(hidden)]
impl PyDescrMethods {
pub(crate) fn update_typeobj(&self, type_object: &mut ffi::PyTypeObject) {
type_object.tp_descr_get = self.tp_descr_get;
type_object.tp_descr_set = self.tp_descr_set;
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
Expand Down
16 changes: 13 additions & 3 deletions src/class/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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 @@ -79,9 +81,17 @@ pub struct PyIterMethods {

#[doc(hidden)]
impl PyIterMethods {
pub(crate) fn update_typeobj(&self, type_object: &mut ffi::PyTypeObject) {
type_object.tp_iter = self.tp_iter;
type_object.tp_iternext = self.tp_iternext;
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
Expand Down
Loading

0 comments on commit 95975b5

Please sign in to comment.