Skip to content

Commit fc3fa70

Browse files
committed
Add more comments for #[pyproto] related parts
1 parent 2a69367 commit fc3fa70

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

pyo3-derive-backend/src/defs.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
// Copyright (c) 2017-present PyO3 Project and Contributors
22
use crate::func::MethodProto;
33

4+
/// Predicates for `#[pyproto]`.
45
pub struct Proto {
6+
/// The name of this protocol. E.g., Iter.
57
pub name: &'static str,
8+
/// The name of slot table. E.g., PyIterMethods.
69
pub slot_table: &'static str,
10+
/// The name of the setter used to set the table to `PyProtoRegistry`.
711
pub set_slot_table: &'static str,
12+
/// All methods.
813
pub methods: &'static [MethodProto],
14+
/// All methods registered as normal methods like `#[pymethods]`.
915
pub py_methods: &'static [PyMethod],
16+
/// All methods registered to the slot table.
1017
pub slot_setters: &'static [SlotSetter],
1118
}
1219

@@ -25,6 +32,7 @@ impl Proto {
2532
}
2633
}
2734

35+
/// Represents a method registered as a normal method like `#[pymethods]`.
2836
// TODO(kngwyu): Currently only __radd__-like methods use METH_COEXIST to prevent
2937
// __add__-like methods from overriding them.
3038
pub struct PyMethod {
@@ -50,9 +58,15 @@ impl PyMethod {
5058
}
5159
}
5260

61+
/// Represents a setter used to register a method to the method table.
5362
pub struct SlotSetter {
63+
/// Protocols necessary for invoking this setter.
64+
/// E.g., we need `__setattr__` and `__delattr__` for invoking `set_setdelitem`.
5465
pub proto_names: &'static [&'static str],
66+
/// The name of the setter called to the method table.
5567
pub set_function: &'static str,
68+
/// Represents a set of setters disabled by this setter.
69+
/// E.g., `set_setdelitem` have to disable `set_setitem` and `set_delitem`.
5670
pub skipped_setters: &'static [&'static str],
5771
}
5872

pyo3-derive-backend/src/pyclass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn impl_methods_inventory(cls: &syn::Ident) -> TokenStream {
236236
}
237237
}
238238

239-
/// TODO(kngwyu): doc
239+
/// Implement `HasProtoRegistry` for the class for lazy protocol initialization.
240240
fn impl_proto_registry(cls: &syn::Ident) -> TokenStream {
241241
quote! {
242242
impl pyo3::class::proto_methods::HasProtoRegistry for #cls {

pyo3-derive-backend/src/pyproto.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn impl_proto_impl(
7171
// Insert the method to the HashSet
7272
method_names.insert(met.sig.ident.to_string());
7373
}
74-
// Add non-slot methods to inventory slots
74+
// Add non-slot methods to inventory like `#[pymethods]`
7575
if let Some(m) = proto.get_method(&met.sig.ident) {
7676
let name = &met.sig.ident;
7777
let fn_spec = FnSpec::parse(&met.sig, &mut met.attrs, false)?;
@@ -82,7 +82,7 @@ fn impl_proto_impl(
8282
} else {
8383
quote!(0)
8484
};
85-
// TODO(kngwyu): ml_doc
85+
// TODO(kngwyu): Set ml_doc
8686
py_methods.push(quote! {
8787
pyo3::class::PyMethodDefType::Method({
8888
#method
@@ -125,24 +125,23 @@ fn slot_initialization(
125125
ty: &syn::Type,
126126
proto: &defs::Proto,
127127
) -> syn::Result<TokenStream> {
128-
let mut initializers: Vec<TokenStream> = vec![];
129128
// Some setters cannot coexist.
130129
// E.g., if we have `__add__`, we need to skip `set_radd`.
131-
let mut skipped_setters = HashSet::new();
132-
'setter_loop: for m in proto.slot_setters {
133-
if skipped_setters.contains(m.set_function) {
130+
let mut skipped_setters = Vec::new();
131+
// Collect initializers
132+
let mut initializers: Vec<TokenStream> = vec![];
133+
'outer_loop: for m in proto.slot_setters {
134+
if skipped_setters.contains(&m.set_function) {
134135
continue;
135136
}
136137
for name in m.proto_names {
138+
// If this `#[pyproto]` block doesn't provide all required methods,
139+
// let's skip implementing this method.
137140
if !method_names.contains(*name) {
138-
// If this `#[pyproto]` block doesn't provide all required methods,
139-
// let's skip implementing this method.
140-
continue 'setter_loop;
141+
continue 'outer_loop;
141142
}
142143
}
143-
for s in m.skipped_setters {
144-
skipped_setters.insert(s.to_string());
145-
}
144+
skipped_setters.extend_from_slice(m.skipped_setters);
146145
// Add slot methods to PyProtoRegistry
147146
let set = syn::Ident::new(m.set_function, Span::call_site());
148147
initializers.push(quote! { table.#set::<#ty>(); });

src/class/proto_methods.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::{
99
sync::atomic::{AtomicPtr, Ordering},
1010
};
1111

12-
/// Defines all methods for object protocols.
13-
// Note: stub implementations are for rust-numpy. Please remove them.
12+
/// Defines all method tables we need for object protocols.
13+
// Note(kngwyu): default implementations are for rust-numpy. Please don't remove them.
1414
pub trait PyProtoMethods {
1515
fn async_methods() -> Option<NonNull<PyAsyncMethods>> {
1616
None

0 commit comments

Comments
 (0)