(zelf: &Py<Self>, vm: &VirtualMachine)
| 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") |
no test coverage detected