MCPcopy Index your code
hub / github.com/RustPython/RustPython / add_operators

Function add_operators

crates/vm/src/class.rs:18–64  ·  view source on GitHub ↗

Add slot wrapper descriptors to a type's dict Iterates SLOT_DEFS and creates a PyWrapper for each slot that: 1. Has a function set in the type's slots 2. Doesn't already have an attribute in the type's dict

(class: &'static Py<PyType>, ctx: &Context)

Source from the content-addressed store, hash-verified

16/// 1. Has a function set in the type's slots
17/// 2. Doesn't already have an attribute in the type's dict
18pub fn add_operators(class: &'static Py<PyType>, ctx: &Context) {
19 for def in SLOT_DEFS.iter() {
20 // Skip __new__ - it has special handling
21 if def.name == "__new__" {
22 continue;
23 }
24
25 // Special handling for __hash__ = None
26 if def.name == "__hash__"
27 && class
28 .slots
29 .hash
30 .load()
31 .is_some_and(|h| h as usize == hash_not_implemented as *const () as usize)
32 {
33 class.set_attr(ctx.names.__hash__, ctx.none.clone().into());
34 continue;
35 }
36
37 // __getattr__ should only have a wrapper if the type explicitly defines it.
38 // Unlike __getattribute__, __getattr__ is not present on object by default.
39 // Both map to TpGetattro, but only __getattribute__ gets a wrapper from the slot.
40 if def.name == "__getattr__" {
41 continue;
42 }
43
44 // Get the slot function wrapped in SlotFunc
45 let Some(slot_func) = def.accessor.get_slot_func_with_op(&class.slots, def.op) else {
46 continue;
47 };
48
49 // Check if attribute already exists in dict
50 let attr_name = ctx.intern_str(def.name);
51 if class.attributes.read().contains_key(attr_name) {
52 continue;
53 }
54
55 // Create and add the wrapper
56 let wrapper = PyWrapper {
57 typ: class,
58 name: attr_name,
59 wrapped: slot_func,
60 doc: Some(def.doc),
61 };
62 class.set_attr(attr_name, wrapper.into_ref(ctx).into());
63 }
64}
65
66pub trait StaticType {
67 // Ideally, saving PyType is better than PyTypeRef

Callers 1

extend_classMethod · 0.85

Calls 10

get_slot_func_with_opMethod · 0.80
intern_strMethod · 0.80
contains_keyMethod · 0.80
SomeClass · 0.50
iterMethod · 0.45
loadMethod · 0.45
set_attrMethod · 0.45
cloneMethod · 0.45
readMethod · 0.45
into_refMethod · 0.45

Tested by

no test coverage detected