Skip to content

Commit 442e0d0

Browse files
committed
Add __repr__ of slice
Issue: RustPython#1431
1 parent 557d3aa commit 442e0d0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

vm/src/obj/objslice.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ impl PySlice {
4949
get_property_value(vm, &self.step)
5050
}
5151

52+
#[pymethod(name = "__repr__")]
53+
fn repr(&self, _vm: &VirtualMachine) -> PyResult<String> {
54+
let start = self.start(_vm);
55+
let stop = self.stop(_vm);
56+
let step = self.step(_vm);
57+
58+
let start_repr = _vm.to_repr(&start)?;
59+
let stop_repr = _vm.to_repr(&stop)?;
60+
let step_repr = _vm.to_repr(&step)?;
61+
62+
Ok(format!(
63+
"slice({}, {}, {})",
64+
start_repr.as_str(),
65+
stop_repr.as_str(),
66+
step_repr.as_str()
67+
))
68+
}
69+
5270
pub fn start_index(&self, vm: &VirtualMachine) -> PyResult<Option<BigInt>> {
5371
if let Some(obj) = &self.start {
5472
to_index_value(vm, obj)

0 commit comments

Comments
 (0)