(&self, vm: &VirtualMachine)
| 385 | self.str(vm)?.try_into_utf8(vm) |
| 386 | } |
| 387 | pub fn str(&self, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> { |
| 388 | let obj = match self.to_owned().downcast_exact::<PyStr>(vm) { |
| 389 | Ok(s) => return Ok(s.into_pyref()), |
| 390 | Err(obj) => obj, |
| 391 | }; |
| 392 | // Fast path for exact int: skip __str__ method resolution |
| 393 | let obj = match obj.downcast_exact::<PyInt>(vm) { |
| 394 | Ok(int) => { |
| 395 | return Ok(vm.ctx.new_str(int.to_str_radix_10())); |
| 396 | } |
| 397 | Err(obj) => obj, |
| 398 | }; |
| 399 | // TODO: replace to obj.class().slots.str |
| 400 | let str_method = match vm.get_special_method(&obj, identifier!(vm, __str__))? { |
| 401 | Some(str_method) => str_method, |
| 402 | None => return obj.repr(vm), |
| 403 | }; |
| 404 | let s = str_method.invoke((), vm)?; |
| 405 | s.downcast::<PyStr>().map_err(|obj| { |
| 406 | vm.new_type_error(format!( |
| 407 | "__str__ returned non-string (type {})", |
| 408 | obj.class().name() |
| 409 | )) |
| 410 | }) |
| 411 | } |
| 412 | |
| 413 | // Equivalent to CPython's check_class. Returns Ok(()) if cls is a valid class, |
| 414 | // Err with TypeError if not. Uses abstract_get_bases internally. |
no test coverage detected