Skip to content

Commit 1f00aa0

Browse files
authored
Merge pull request RustPython#3969 from daeun503/fix-range-attribute
Fix range attributes type
2 parents 1daf2f1 + 97d8529 commit 1f00aa0

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

Lib/test/test_copy.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from operator import le, lt, ge, gt, eq, ne
88

99
import unittest
10+
from test import support
1011

1112
order_comparisons = le, lt, ge, gt
1213
equality_comparisons = eq, ne
@@ -99,7 +100,7 @@ class WithMetaclass(metaclass=abc.ABCMeta):
99100
42, 2**100, 3.14, True, False, 1j,
100101
"hello", "hello\u1234", f.__code__,
101102
b"world", bytes(range(256)), range(10), slice(1, 10, 2),
102-
NewStyle, Classic, max, WithMetaclass]
103+
NewStyle, Classic, max, WithMetaclass, property()]
103104
for x in tests:
104105
self.assertIs(copy.copy(x), x)
105106

@@ -350,6 +351,8 @@ def __getattribute__(self, name):
350351

351352
# Type-specific _deepcopy_xxx() methods
352353

354+
# TODO: RUSTPYTHON
355+
@unittest.expectedFailure
353356
def test_deepcopy_atomic(self):
354357
class Classic:
355358
pass
@@ -359,7 +362,7 @@ def f():
359362
pass
360363
tests = [None, 42, 2**100, 3.14, True, False, 1j,
361364
"hello", "hello\u1234", f.__code__,
362-
NewStyle, Classic, max]
365+
NewStyle, range(10), Classic, max, property()]
363366
for x in tests:
364367
self.assertIs(copy.deepcopy(x), x)
365368

@@ -583,17 +586,6 @@ class C:
583586
self.assertIsNot(y, x)
584587
self.assertIs(y.foo, y)
585588

586-
def test_deepcopy_range(self):
587-
class I(int):
588-
pass
589-
x = range(I(10))
590-
y = copy.deepcopy(x)
591-
self.assertIsNot(y, x)
592-
self.assertEqual(y, x)
593-
self.assertIsNot(y.stop, x.stop)
594-
self.assertEqual(y.stop, x.stop)
595-
self.assertIsInstance(y.stop, I)
596-
597589
# _reconstruct()
598590

599591
def test_reconstruct_string(self):
@@ -820,6 +812,7 @@ class C(object):
820812
self.assertEqual(v[c], d)
821813
self.assertEqual(len(v), 2)
822814
del c, d
815+
support.gc_collect() # For PyPy or other GCs.
823816
self.assertEqual(len(v), 1)
824817
x, y = C(), C()
825818
# The underlying containers are decoupled
@@ -849,6 +842,7 @@ def __init__(self, i):
849842
self.assertEqual(v[a].i, b.i)
850843
self.assertEqual(v[c].i, d.i)
851844
del c
845+
support.gc_collect() # For PyPy or other GCs.
852846
self.assertEqual(len(v), 1)
853847

854848
def test_deepcopy_weakvaluedict(self):
@@ -872,6 +866,7 @@ def __init__(self, i):
872866
self.assertIs(t, d)
873867
del x, y, z, t
874868
del d
869+
support.gc_collect() # For PyPy or other GCs.
875870
self.assertEqual(len(v), 1)
876871

877872
def test_deepcopy_bound_method(self):

Lib/test/test_range.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,6 @@ def test_comparison(self):
661661
with self.assertRaises(TypeError):
662662
range(0) >= range(0)
663663

664-
665-
# TODO: RUSTPYTHON
666-
@unittest.expectedFailure
667664
def test_attributes(self):
668665
# test the start, stop and step attributes of range objects
669666
self.assert_attrs(range(0), 0, 0, 1)

vm/src/builtins/range.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,32 @@ pub fn init(context: &Context) {
181181

182182
#[pyimpl(with(AsMapping, AsSequence, Hashable, Comparable, Iterable))]
183183
impl PyRange {
184-
fn new(cls: PyTypeRef, stop: PyIntRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
184+
fn new(cls: PyTypeRef, stop: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
185185
PyRange {
186186
start: vm.new_pyref(0),
187-
stop,
187+
stop: stop.try_index(vm)?,
188188
step: vm.new_pyref(1),
189189
}
190190
.into_ref_with_type(vm, cls)
191191
}
192192

193193
fn new_from(
194194
cls: PyTypeRef,
195-
start: PyIntRef,
196-
stop: PyIntRef,
197-
step: OptionalArg<PyIntRef>,
195+
start: PyObjectRef,
196+
stop: PyObjectRef,
197+
step: OptionalArg<PyObjectRef>,
198198
vm: &VirtualMachine,
199199
) -> PyResult<PyRef<Self>> {
200-
let step = step.unwrap_or_else(|| vm.new_pyref(1));
200+
let step = step.unwrap_or_else(|| vm.new_pyobj(1)).try_index(vm)?;
201201
if step.as_bigint().is_zero() {
202202
return Err(vm.new_value_error("range() arg 3 must not be zero".to_owned()));
203203
}
204-
PyRange { start, stop, step }.into_ref_with_type(vm, cls)
204+
PyRange {
205+
start: start.try_index(vm)?,
206+
stop: stop.try_index(vm)?,
207+
step,
208+
}
209+
.into_ref_with_type(vm, cls)
205210
}
206211

207212
#[pyproperty]

0 commit comments

Comments
 (0)