Skip to content

Commit 05317f1

Browse files
youknowonexiaozhiyan
authored andcommitted
temporary fill up missing number protocols
1 parent 8a42a68 commit 05317f1

File tree

5 files changed

+121
-31
lines changed

5 files changed

+121
-31
lines changed

derive-impl/src/pyclass.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -548,14 +548,27 @@ where
548548
other
549549
),
550550
};
551-
quote_spanned! { ident.span() =>
552-
class.set_str_attr(
553-
#py_name,
554-
ctx.make_funcdef(#py_name, Self::#ident)
555-
#doc
556-
#build_func,
557-
ctx,
558-
);
551+
if py_name.starts_with("__") && py_name.ends_with("__") {
552+
let name_ident = Ident::new(&py_name, ident.span());
553+
quote_spanned! { ident.span() =>
554+
class.set_attr(
555+
ctx.names.#name_ident,
556+
ctx.make_funcdef(#py_name, Self::#ident)
557+
#doc
558+
#build_func
559+
.into(),
560+
);
561+
}
562+
} else {
563+
quote_spanned! { ident.span() =>
564+
class.set_str_attr(
565+
#py_name,
566+
ctx.make_funcdef(#py_name, Self::#ident)
567+
#doc
568+
#build_func,
569+
ctx,
570+
);
571+
}
559572
}
560573
};
561574

stdlib/src/array.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
66
let array = module
77
.get_attr("array", vm)
88
.expect("Expect array has array type.");
9+
array.init_builtin_number_slots(&vm.ctx);
910

1011
let collections_abc = vm
1112
.import("collections.abc", None, 0)

vm/src/builtins/type.rs

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -185,25 +185,6 @@ impl PyType {
185185

186186
*slots.name.get_mut() = Some(String::from(name));
187187

188-
#[allow(clippy::mutable_key_type)]
189-
let mut slot_name_set = HashSet::new();
190-
191-
for cls in mro.iter() {
192-
for &name in cls.attributes.read().keys() {
193-
if name != identifier!(ctx, __new__)
194-
&& name.as_str().starts_with("__")
195-
&& name.as_str().ends_with("__")
196-
{
197-
slot_name_set.insert(name);
198-
}
199-
}
200-
}
201-
for &name in attrs.keys() {
202-
if name.as_str().starts_with("__") && name.as_str().ends_with("__") {
203-
slot_name_set.insert(name);
204-
}
205-
}
206-
207188
let new_type = PyRef::new_ref(
208189
PyType {
209190
base: Some(base),
@@ -218,9 +199,7 @@ impl PyType {
218199
None,
219200
);
220201

221-
for attr_name in slot_name_set {
222-
new_type.update_slot::<true>(attr_name, ctx);
223-
}
202+
new_type.init_slots(ctx);
224203

225204
let weakref_type = super::PyWeak::static_type();
226205
for base in &new_type.bases {
@@ -278,6 +257,30 @@ impl PyType {
278257
Ok(new_type)
279258
}
280259

260+
pub(crate) fn init_slots(&self, ctx: &Context) {
261+
#[allow(clippy::mutable_key_type)]
262+
let mut slot_name_set = std::collections::HashSet::new();
263+
264+
for cls in self.mro.iter() {
265+
for &name in cls.attributes.read().keys() {
266+
if name == identifier!(ctx, __new__) {
267+
continue;
268+
}
269+
if name.as_str().starts_with("__") && name.as_str().ends_with("__") {
270+
slot_name_set.insert(name);
271+
}
272+
}
273+
}
274+
for &name in self.attributes.read().keys() {
275+
if name.as_str().starts_with("__") && name.as_str().ends_with("__") {
276+
slot_name_set.insert(name);
277+
}
278+
}
279+
for attr_name in slot_name_set {
280+
self.update_slot::<true>(attr_name, ctx);
281+
}
282+
}
283+
281284
pub fn slot_name(&self) -> String {
282285
self.slots.name.read().as_ref().unwrap().to_string()
283286
}
@@ -1326,3 +1329,64 @@ mod tests {
13261329
);
13271330
}
13281331
}
1332+
1333+
impl crate::PyObject {
1334+
// temporary tool to fill missing number protocols for builtin types
1335+
pub fn init_builtin_number_slots(&self, ctx: &Context) {
1336+
let typ = self
1337+
.downcast_ref::<PyType>()
1338+
.expect("not called from a type");
1339+
macro_rules! call_update_slot {
1340+
($name:ident) => {
1341+
let id = identifier!(ctx, $name);
1342+
if typ.has_attr(id) {
1343+
typ.update_slot::<true>(identifier!(ctx, $name), ctx);
1344+
}
1345+
};
1346+
}
1347+
call_update_slot!(__add__);
1348+
call_update_slot!(__radd__);
1349+
call_update_slot!(__iadd__);
1350+
call_update_slot!(__sub__);
1351+
call_update_slot!(__rsub__);
1352+
call_update_slot!(__isub__);
1353+
call_update_slot!(__mul__);
1354+
call_update_slot!(__rmul__);
1355+
call_update_slot!(__imul__);
1356+
call_update_slot!(__mod__);
1357+
call_update_slot!(__rmod__);
1358+
call_update_slot!(__imod__);
1359+
call_update_slot!(__div__);
1360+
call_update_slot!(__rdiv__);
1361+
call_update_slot!(__idiv__);
1362+
call_update_slot!(__divmod__);
1363+
call_update_slot!(__rdivmod__);
1364+
call_update_slot!(__pow__);
1365+
call_update_slot!(__rpow__);
1366+
call_update_slot!(__ipow__);
1367+
call_update_slot!(__lshift__);
1368+
call_update_slot!(__rlshift__);
1369+
call_update_slot!(__ilshift__);
1370+
call_update_slot!(__rshift__);
1371+
call_update_slot!(__rrshift__);
1372+
call_update_slot!(__irshift__);
1373+
call_update_slot!(__and__);
1374+
call_update_slot!(__rand__);
1375+
call_update_slot!(__iand__);
1376+
call_update_slot!(__xor__);
1377+
call_update_slot!(__rxor__);
1378+
call_update_slot!(__ixor__);
1379+
call_update_slot!(__or__);
1380+
call_update_slot!(__ror__);
1381+
call_update_slot!(__ior__);
1382+
call_update_slot!(__floordiv__);
1383+
call_update_slot!(__rfloordiv__);
1384+
call_update_slot!(__ifloordiv__);
1385+
call_update_slot!(__truediv__);
1386+
call_update_slot!(__rtruediv__);
1387+
call_update_slot!(__itruediv__);
1388+
call_update_slot!(__matmul__);
1389+
call_update_slot!(__rmatmul__);
1390+
call_update_slot!(__imatmul__);
1391+
}
1392+
}

vm/src/stdlib/builtins.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,11 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
948948
crate::protocol::VecBuffer::make_class(&vm.ctx);
949949

950950
builtins::extend_module(vm, &module);
951+
use crate::AsObject;
952+
ctx.types
953+
.generic_alias_type
954+
.as_object()
955+
.init_builtin_number_slots(&vm.ctx);
951956

952957
let debug_mode: bool = vm.state.settings.optimize == 0;
953958
extend_module!(vm, module, {

vm/src/vm/context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ declare_const_name! {
7878
__aenter__,
7979
__aexit__,
8080
__aiter__,
81+
__alloc__,
8182
__all__,
8283
__and__,
8384
__anext__,
@@ -121,7 +122,9 @@ declare_const_name! {
121122
__get__,
122123
__getattr__,
123124
__getattribute__,
125+
__getformat__,
124126
__getitem__,
127+
__getnewargs__,
125128
__gt__,
126129
__hash__,
127130
__iadd__,
@@ -146,6 +149,7 @@ declare_const_name! {
146149
__iter__,
147150
__itruediv__,
148151
__ixor__,
152+
__jit__, // RustPython dialect
149153
__le__,
150154
__len__,
151155
__length_hint__,
@@ -195,13 +199,16 @@ declare_const_name! {
195199
__rtruediv__,
196200
__rxor__,
197201
__set__,
198-
__set_name__,
199202
__setattr__,
200203
__setitem__,
204+
__setstate__,
205+
__set_name__,
201206
__slots__,
202207
__str__,
203208
__sub__,
204209
__subclasscheck__,
210+
__subclasshook__,
211+
__subclasses__,
205212
__sizeof__,
206213
__truediv__,
207214
__trunc__,

0 commit comments

Comments
 (0)