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

Method repr

crates/vm/src/stdlib/_io.rs:4010–4054  ·  view source on GitHub ↗
(zelf: &Py<Self>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

4008 impl Representable for TextIOWrapper {
4009 #[inline]
4010 fn repr(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> {
4011 let type_name = zelf.class().slot_name();
4012 let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) else {
4013 return Err(
4014 vm.new_runtime_error(format!("reentrant call inside {type_name}.__repr__"))
4015 );
4016 };
4017 let Some(data) = zelf.data.lock() else {
4018 // Reentrant call
4019 return Ok(vm.ctx.new_str(Wtf8Buf::from(format!("<{type_name}>"))));
4020 };
4021 let Some(data) = data.as_ref() else {
4022 return Err(vm.new_value_error("I/O operation on uninitialized object"));
4023 };
4024
4025 let mut result = Wtf8Buf::from(format!("<{type_name}"));
4026
4027 // Add name if present
4028 if let Ok(Some(name)) = vm.get_attribute_opt(data.buffer.clone(), "name") {
4029 let name_repr = name.repr(vm)?;
4030 result.push_wtf8(" name=".as_ref());
4031 result.push_wtf8(name_repr.as_wtf8());
4032 }
4033
4034 // Add mode if present (prefer the wrapper's attribute)
4035 let mode_obj = match vm.get_attribute_opt(zelf.as_object().to_owned(), "mode") {
4036 Ok(Some(mode)) => Some(mode),
4037 Ok(None) | Err(_) => match vm.get_attribute_opt(data.buffer.clone(), "mode") {
4038 Ok(Some(mode)) => Some(mode),
4039 _ => None,
4040 },
4041 };
4042 if let Some(mode) = mode_obj {
4043 let mode_repr = mode.repr(vm)?;
4044 result.push_wtf8(" mode=".as_ref());
4045 result.push_wtf8(mode_repr.as_wtf8());
4046 }
4047
4048 // Add encoding (always valid UTF-8)
4049 result.push_wtf8(" encoding='".as_ref());
4050 result.push_wtf8(data.encoding.as_str().as_ref());
4051 result.push_wtf8("'>".as_ref());
4052
4053 Ok(vm.ctx.new_str(result))
4054 }
4055
4056 fn repr_str(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
4057 unreachable!("repr() is overridden directly")

Callers 15

repr_wtf8Method · 0.45
displayhookFunction · 0.45
_unraisablehookFunction · 0.45
reprFunction · 0.45
repr_wtf8Method · 0.45
repr_file_obj_nameFunction · 0.45
try_from_objectMethod · 0.45
dealloc_warnMethod · 0.45
repr_wtf8Method · 0.45
_run_exitfuncsFunction · 0.45
repr_wtf8Method · 0.45
typing_type_reprFunction · 0.45

Calls 14

get_attribute_optMethod · 0.80
push_wtf8Method · 0.80
ErrClass · 0.50
SomeClass · 0.50
slot_nameMethod · 0.45
classMethod · 0.45
as_objectMethod · 0.45
lockMethod · 0.45
new_strMethod · 0.45
as_refMethod · 0.45
cloneMethod · 0.45
as_wtf8Method · 0.45

Tested by

no test coverage detected