(
vm: &VirtualMachine,
obj: &PyObject,
type_name: &str,
)
| 259 | |
| 260 | impl SequenceIndex { |
| 261 | pub fn try_from_borrowed_object( |
| 262 | vm: &VirtualMachine, |
| 263 | obj: &PyObject, |
| 264 | type_name: &str, |
| 265 | ) -> PyResult<Self> { |
| 266 | if let Some(i) = obj.downcast_ref::<PyInt>() { |
| 267 | // TODO: number protocol |
| 268 | i.try_to_primitive(vm) |
| 269 | .map_err(|_| vm.new_index_error("cannot fit 'int' into an index-sized integer")) |
| 270 | .map(Self::Int) |
| 271 | } else if let Some(slice) = obj.downcast_ref::<PySlice>() { |
| 272 | slice.to_saturated(vm).map(Self::Slice) |
| 273 | } else if let Some(i) = obj.try_index_opt(vm) { |
| 274 | // TODO: __index__ for indices is no more supported? |
| 275 | i?.try_to_primitive(vm) |
| 276 | .map_err(|_| vm.new_index_error("cannot fit 'int' into an index-sized integer")) |
| 277 | .map(Self::Int) |
| 278 | } else { |
| 279 | Err(vm.new_type_error(format!( |
| 280 | "{} indices must be integers or slices or classes that override __index__ operator, not '{}'", |
| 281 | type_name, |
| 282 | obj.class() |
| 283 | ))) |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | pub trait SequenceIndexOp { |
nothing calls this directly
no test coverage detected