Skip to content

Commit 70b576e

Browse files
committed
temporary fill up missing number protocols
1 parent f00e6b6 commit 70b576e

File tree

5 files changed

+121
-31
lines changed

5 files changed

+121
-31
lines changed

derive-impl/src/pyclass.rs

+21-8
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

+1
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

+86-22
Original file line numberDiff line numberDiff line change
@@ -187,25 +187,6 @@ impl PyType {
187187

188188
*slots.name.get_mut() = Some(String::from(name));
189189

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

223-
for attr_name in slot_name_set {
224-
new_type.update_slot::<true>(attr_name, ctx);
225-
}
204+
new_type.init_slots(ctx);
226205

227206
let weakref_type = super::PyWeak::static_type();
228207
for base in &new_type.bases {
@@ -280,6 +259,30 @@ impl PyType {
280259
Ok(new_type)
281260
}
282261

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

vm/src/stdlib/builtins.rs

+5
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,11 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
957957
crate::protocol::VecBuffer::make_class(&vm.ctx);
958958

959959
builtins::extend_module(vm, &module);
960+
use crate::AsObject;
961+
ctx.types
962+
.generic_alias_type
963+
.as_object()
964+
.init_builtin_number_slots(&vm.ctx);
960965

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

vm/src/vm/context.rs

+8-1
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)