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)
| 16 | /// 1. Has a function set in the type's slots |
| 17 | /// 2. Doesn't already have an attribute in the type's dict |
| 18 | pub 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 | |
| 66 | pub trait StaticType { |
| 67 | // Ideally, saving PyType is better than PyTypeRef |
no test coverage detected