From d0afc4c3bc381894131132be629b726a7487d120 Mon Sep 17 00:00:00 2001 From: snowapril Date: Thu, 26 Aug 2021 16:15:28 +0900 Subject: [PATCH] set range iter type not creatable by calling type This commit fix `RangeTest.test_range_iterators_invocation` in [`test_range.py`](https://github.com/RustPython/RustPython/blob/main/Lib/test/test_range.py#L507). This is last `TODO` in `test_range.py`. By add slot tp_new to both types and return new_type_error, prevent creation of range_iterator and longrange_iterator by calling its type. Signed-off-by: snowapril --- Lib/test/test_range.py | 2 -- vm/src/builtins/range.rs | 10 ++++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py index 33388e599..94c96a941 100644 --- a/Lib/test/test_range.py +++ b/Lib/test/test_range.py @@ -502,8 +502,6 @@ class RangeTest(unittest.TestCase): test_id = "reversed(range({}, {}, {}))".format(start, end, step) self.assert_iterators_equal(iter1, iter2, test_id, limit=100) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_range_iterators_invocation(self): # verify range iterators instances cannot be created by # calling their type diff --git a/vm/src/builtins/range.rs b/vm/src/builtins/range.rs index 3e708299e..7017f120a 100644 --- a/vm/src/builtins/range.rs +++ b/vm/src/builtins/range.rs @@ -483,6 +483,11 @@ impl PyValue for PyLongRangeIterator { #[pyimpl(with(PyIter))] impl PyLongRangeIterator { + #[pyslot] + fn tp_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { + Err(vm.new_type_error("cannot create 'longrange_iterator' instances".to_owned())) + } + #[pymethod(magic)] fn length_hint(&self) -> BigInt { let index = BigInt::from(self.index.load()); @@ -546,6 +551,11 @@ impl PyValue for PyRangeIterator { #[pyimpl(with(PyIter))] impl PyRangeIterator { + #[pyslot] + fn tp_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { + Err(vm.new_type_error("cannot create 'range_iterator' instances".to_owned())) + } + #[pymethod(magic)] fn length_hint(&self) -> usize { let index = self.index.load();