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

Method init

crates/vm/src/builtins/property.rs:351–400  ·  view source on GitHub ↗
(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

349 type Args = PropertyArgs;
350
351 fn init(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
352 // Set doc and getter_doc flag
353 let mut getter_doc = false;
354
355 // Helper to get doc from getter
356 let get_getter_doc = |fget: &PyObject| -> Option<PyObjectRef> {
357 fget.get_attr("__doc__", vm)
358 .ok()
359 .filter(|doc| !vm.is_none(doc))
360 };
361
362 let doc = match args.doc {
363 Some(doc) if !vm.is_none(&doc) => Some(doc),
364 _ => {
365 // No explicit doc or doc is None, try to get from getter
366 args.fget.as_ref().and_then(|fget| {
367 get_getter_doc(fget).inspect(|_| {
368 getter_doc = true;
369 })
370 })
371 }
372 };
373
374 // Check if this is a property subclass
375 let is_exact_property = zelf.class().is(vm.ctx.types.property_type);
376
377 if is_exact_property {
378 // For exact property type, store doc in the field
379 *zelf.doc.write() = doc;
380 } else {
381 // For property subclass, set __doc__ as an attribute
382 let doc_to_set = doc.unwrap_or_else(|| vm.ctx.none());
383 match zelf.as_object().set_attr("__doc__", doc_to_set, vm) {
384 Ok(()) => {}
385 Err(e) if !getter_doc && e.class().is(vm.ctx.exceptions.attribute_error) => {
386 // Silently ignore AttributeError for backwards compatibility
387 // (only when not using getter_doc)
388 }
389 Err(e) => return Err(e),
390 }
391 }
392
393 *zelf.getter.write() = args.fget;
394 *zelf.setter.write() = args.fset;
395 *zelf.deleter.write() = args.fdel;
396 *zelf.name.write() = args.name.map(|a| a.as_object().to_owned());
397 zelf.getter_doc.store(getter_doc, Ordering::Relaxed);
398
399 Ok(())
400 }
401}
402
403pub(crate) fn init(context: &'static Context) {

Callers

nothing calls this directly

Calls 15

okMethod · 0.80
isMethod · 0.80
noneMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50
filterMethod · 0.45
get_attrMethod · 0.45
is_noneMethod · 0.45
as_refMethod · 0.45
classMethod · 0.45
writeMethod · 0.45
set_attrMethod · 0.45

Tested by

no test coverage detected