Skip to content

Commit 6950ad6

Browse files
committed
Initialize with init for mutable builtins.
1 parent 8f39794 commit 6950ad6

File tree

5 files changed

+22
-26
lines changed

5 files changed

+22
-26
lines changed

Lib/test/list_tests.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
class CommonTest(seq_tests.CommonTest):
1414

15-
# TODO: RUSTPYTHON
16-
@unittest.expectedFailure
1715
def test_init(self):
1816
# Iterable arg is optional
1917
self.assertEqual(self.type2test([]), self.type2test())

Lib/test/test_set.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def setUp(self):
4747
self.s = self.thetype(word)
4848
self.d = dict.fromkeys(word)
4949

50-
# TODO: RUSTPYTHON
51-
@unittest.expectedFailure
5250
def test_new_or_init(self):
5351
self.assertRaises(TypeError, self.thetype, [], 2)
5452
self.assertRaises(TypeError, set().__init__, a=1)
@@ -386,8 +384,6 @@ class TestSet(TestJointOps, unittest.TestCase):
386384
def test_contains(self):
387385
super().test_contains()
388386

389-
# TODO: RUSTPYTHON
390-
@unittest.expectedFailure
391387
def test_init(self):
392388
s = self.thetype()
393389
s.__init__(self.word)

vm/src/builtins/dict.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ impl PyValue for PyDict {
5454
impl PyDict {
5555
#[pyslot]
5656
fn tp_new(class: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
57-
PyDict {
58-
entries: DictContentType::default(),
59-
}
60-
.into_ref_with_type(vm, class)
57+
PyDict::default().into_ref_with_type(vm, class)
6158
}
6259

6360
#[pymethod(magic)]

vm/src/builtins/list.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,21 @@ impl PyList {
391391
#[pyslot]
392392
fn tp_new(
393393
cls: PyTypeRef,
394-
iterable: OptionalArg<PyObjectRef>,
394+
_iterable: OptionalArg<PyObjectRef>,
395395
vm: &VirtualMachine,
396396
) -> PyResult<PyRef<Self>> {
397-
let elements = if let OptionalArg::Present(iterable) = iterable {
397+
PyList::default().into_ref_with_type(vm, cls)
398+
}
399+
400+
#[pymethod(name = "__init__")]
401+
fn init(&self, iterable: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<()> {
402+
let mut elements = if let OptionalArg::Present(iterable) = iterable {
398403
vm.extract_elements(&iterable)?
399404
} else {
400405
vec![]
401406
};
402-
403-
PyList::from(elements).into_ref_with_type(vm, cls)
407+
std::mem::swap(self.borrow_vec_mut().deref_mut(), &mut elements);
408+
Ok(())
404409
}
405410
}
406411

vm/src/builtins/set.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ impl PySetInner {
7878
Ok(set)
7979
}
8080

81-
fn from_arg(iterable: OptionalArg<PyIterable>, vm: &VirtualMachine) -> PyResult<PySetInner> {
82-
if let OptionalArg::Present(iterable) = iterable {
83-
Self::new(iterable, vm)
84-
} else {
85-
Ok(PySetInner::default())
86-
}
87-
}
88-
8981
fn len(&self) -> usize {
9082
self.content.len()
9183
}
@@ -318,13 +310,21 @@ impl PySet {
318310
#[pyslot]
319311
fn tp_new(
320312
cls: PyTypeRef,
321-
iterable: OptionalArg<PyIterable>,
313+
_iterable: OptionalArg<PyIterable>,
322314
vm: &VirtualMachine,
323315
) -> PyResult<PyRef<Self>> {
324-
Self {
325-
inner: PySetInner::from_arg(iterable, vm)?,
316+
PySet::default().into_ref_with_type(vm, cls)
317+
}
318+
319+
#[pymethod(magic)]
320+
fn init(&self, iterable: OptionalArg<PyIterable>, vm: &VirtualMachine) -> PyResult<()> {
321+
if self.len() > 0 {
322+
self.clear();
326323
}
327-
.into_ref_with_type(vm, cls)
324+
if let OptionalArg::Present(it) = iterable {
325+
self.update(Args::new(vec![it]), vm)?;
326+
}
327+
Ok(())
328328
}
329329

330330
#[pymethod(name = "__len__")]

0 commit comments

Comments
 (0)