Skip to content

Commit 836f267

Browse files
panics
1 parent fc84fe8 commit 836f267

File tree

1 file changed

+51
-66
lines changed

1 file changed

+51
-66
lines changed

vm/src/obj/objrange.rs

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ impl PyRange {
4040
}
4141
}
4242

43-
#[inline]
44-
pub fn contains(&self, value: &BigInt) -> bool {
45-
match self.offset(value) {
46-
Some(ref offset) => offset.is_multiple_of(&self.step),
47-
None => false,
48-
}
49-
}
50-
5143
#[inline]
5244
pub fn index_of(&self, value: &BigInt) -> Option<BigInt> {
5345
match self.offset(value) {
@@ -112,16 +104,16 @@ pub fn init(context: &PyContext) {
112104

113105
extend_class!(context, range_type, {
114106
"__bool__" => context.new_rustfunc(PyRangeRef::bool),
115-
"__contains__" => context.new_rustfunc(range_contains),
107+
"__contains__" => context.new_rustfunc(PyRangeRef::contains),
116108
"__doc__" => context.new_str(range_doc.to_string()),
117109
"__getitem__" => context.new_rustfunc(PyRangeRef::getitem),
118110
"__iter__" => context.new_rustfunc(PyRangeRef::iter),
119111
"__len__" => context.new_rustfunc(PyRangeRef::len),
120112
"__new__" => context.new_rustfunc(range_new),
121113
"__repr__" => context.new_rustfunc(PyRangeRef::repr),
122114
"__reversed__" => context.new_rustfunc(PyRangeRef::reversed),
123-
"count" => context.new_rustfunc(range_count),
124-
"index" => context.new_rustfunc(range_index),
115+
"count" => context.new_rustfunc(PyRangeRef::count),
116+
"index" => context.new_rustfunc(PyRangeRef::index),
125117
"start" => context.new_property(PyRangeRef::start),
126118
"stop" => context.new_property(PyRangeRef::stop),
127119
"step" => context.new_property(PyRangeRef::step),
@@ -231,6 +223,54 @@ impl PyRangeRef {
231223
self.stop != self.start
232224
}
233225

226+
fn contains(self, needle: PyObjectRef, vm: &VirtualMachine) -> bool {
227+
if let Ok(int) = needle.downcast::<PyInt>() {
228+
match self.offset(&int.value) {
229+
Some(ref offset) => offset.is_multiple_of(&self.step),
230+
None => false,
231+
}
232+
} else {
233+
false
234+
}
235+
}
236+
237+
fn index(self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyInt> {
238+
arg_check!(
239+
vm,
240+
args,
241+
required = [(zelf, Some(vm.ctx.range_type())), (needle, None)]
242+
);
243+
244+
let range = get_value(zelf);
245+
246+
if objtype::isinstance(needle, &vm.ctx.int_type()) {
247+
let needle = objint::get_value(needle);
248+
249+
match range.index_of(&needle) {
250+
Some(idx) => Ok(vm.ctx.new_int(idx)),
251+
None => Err(vm.new_value_error(format!("{} is not in range", needle))),
252+
}
253+
} else {
254+
Err(vm.new_value_error("sequence.index(x): x not in sequence".to_string()))
255+
}
256+
}
257+
258+
fn range_count(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
259+
arg_check!(
260+
vm,
261+
args,
262+
required = [(zelf, Some(vm.ctx.range_type())), (item, None)]
263+
);
264+
265+
let range = get_value(zelf);
266+
267+
if objtype::isinstance(item, &vm.ctx.int_type()) {
268+
Ok(vm.ctx.new_int(range.count(&objint::get_value(item))))
269+
} else {
270+
Ok(vm.ctx.new_int(0))
271+
}
272+
}
273+
234274
fn getitem(self, subscript: Either<PyIntRef, PySliceRef>, vm: &VirtualMachine) -> PyResult {
235275
match subscript {
236276
Either::A(index) => {
@@ -290,58 +330,3 @@ fn range_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
290330

291331
Ok(range.into_object())
292332
}
293-
294-
fn range_contains(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
295-
arg_check!(
296-
vm,
297-
args,
298-
required = [(zelf, Some(vm.ctx.range_type())), (needle, None)]
299-
);
300-
301-
let range = get_value(zelf);
302-
303-
let result = if objtype::isinstance(needle, &vm.ctx.int_type()) {
304-
range.contains(&objint::get_value(needle))
305-
} else {
306-
false
307-
};
308-
309-
Ok(vm.ctx.new_bool(result))
310-
}
311-
312-
fn range_index(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
313-
arg_check!(
314-
vm,
315-
args,
316-
required = [(zelf, Some(vm.ctx.range_type())), (needle, None)]
317-
);
318-
319-
let range = get_value(zelf);
320-
321-
if objtype::isinstance(needle, &vm.ctx.int_type()) {
322-
let needle = objint::get_value(needle);
323-
324-
match range.index_of(&needle) {
325-
Some(idx) => Ok(vm.ctx.new_int(idx)),
326-
None => Err(vm.new_value_error(format!("{} is not in range", needle))),
327-
}
328-
} else {
329-
Err(vm.new_value_error("sequence.index(x): x not in sequence".to_string()))
330-
}
331-
}
332-
333-
fn range_count(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
334-
arg_check!(
335-
vm,
336-
args,
337-
required = [(zelf, Some(vm.ctx.range_type())), (item, None)]
338-
);
339-
340-
let range = get_value(zelf);
341-
342-
if objtype::isinstance(item, &vm.ctx.int_type()) {
343-
Ok(vm.ctx.new_int(range.count(&objint::get_value(item))))
344-
} else {
345-
Ok(vm.ctx.new_int(0))
346-
}
347-
}

0 commit comments

Comments
 (0)