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

Method repr

crates/vm/src/builtins/list.rs:590–631  ·  view source on GitHub ↗
(zelf: &Py<Self>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

588impl Representable for PyList {
589 #[inline]
590 fn repr(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> {
591 if zelf.__len__() == 0 {
592 return Ok(vm.ctx.intern_str("[]").to_owned());
593 }
594
595 if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
596 // Clone elements before calling repr to release the read lock.
597 // Element repr may mutate the list (e.g., list.clear()), which
598 // needs a write lock and would deadlock if read lock is held.
599 let mut writer = Wtf8Buf::new();
600 writer.push_char('[');
601
602 let mut elements = zelf.borrow_vec().to_vec();
603 let mut size = zelf.__len__();
604 let mut first = true;
605 let mut i = 0;
606 while i < size {
607 if elements.len() != size {
608 // `repr` mutated the list. refetch it.
609 elements = zelf.borrow_vec().to_vec();
610 }
611
612 let item = &elements[i];
613
614 if first {
615 first = false;
616 } else {
617 writer.push_str(", ");
618 }
619
620 writer.push_wtf8(item.repr(vm)?.as_wtf8());
621
622 size = zelf.__len__(); // Refetch list size as `repr` may mutate the list.
623 i += 1;
624 }
625
626 writer.push_char(']');
627 Ok(vm.ctx.new_str(writer))
628 } else {
629 Ok(vm.ctx.intern_str("[...]").to_owned())
630 }
631 }
632
633 fn repr_str(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
634 unreachable!("repr() is overridden directly")

Callers

nothing calls this directly

Calls 13

newFunction · 0.85
intern_strMethod · 0.80
to_vecMethod · 0.80
borrow_vecMethod · 0.80
push_wtf8Method · 0.80
__len__Method · 0.45
to_ownedMethod · 0.45
as_objectMethod · 0.45
push_charMethod · 0.45
lenMethod · 0.45
push_strMethod · 0.45
as_wtf8Method · 0.45

Tested by

no test coverage detected