mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #2792 from DimitrisJim/init_bltins
Initialize with init for mutable builtins.
This commit is contained in:
@@ -12,8 +12,6 @@ from test import support, seq_tests
|
||||
|
||||
class CommonTest(seq_tests.CommonTest):
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_init(self):
|
||||
# Iterable arg is optional
|
||||
self.assertEqual(self.type2test([]), self.type2test())
|
||||
|
||||
@@ -47,8 +47,6 @@ class TestJointOps:
|
||||
self.s = self.thetype(word)
|
||||
self.d = dict.fromkeys(word)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_new_or_init(self):
|
||||
self.assertRaises(TypeError, self.thetype, [], 2)
|
||||
self.assertRaises(TypeError, set().__init__, a=1)
|
||||
@@ -386,8 +384,6 @@ class TestSet(TestJointOps, unittest.TestCase):
|
||||
def test_contains(self):
|
||||
super().test_contains()
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_init(self):
|
||||
s = self.thetype()
|
||||
s.__init__(self.word)
|
||||
|
||||
@@ -54,10 +54,7 @@ impl PyValue for PyDict {
|
||||
impl PyDict {
|
||||
#[pyslot]
|
||||
fn tp_new(class: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
PyDict {
|
||||
entries: DictContentType::default(),
|
||||
}
|
||||
.into_ref_with_type(vm, class)
|
||||
PyDict::default().into_ref_with_type(vm, class)
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
|
||||
@@ -391,16 +391,21 @@ impl PyList {
|
||||
#[pyslot]
|
||||
fn tp_new(
|
||||
cls: PyTypeRef,
|
||||
iterable: OptionalArg<PyObjectRef>,
|
||||
_iterable: OptionalArg<PyObjectRef>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyRef<Self>> {
|
||||
let elements = if let OptionalArg::Present(iterable) = iterable {
|
||||
PyList::default().into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__init__")]
|
||||
fn init(&self, iterable: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let mut elements = if let OptionalArg::Present(iterable) = iterable {
|
||||
vm.extract_elements(&iterable)?
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
PyList::from(elements).into_ref_with_type(vm, cls)
|
||||
std::mem::swap(self.borrow_vec_mut().deref_mut(), &mut elements);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,14 +78,6 @@ impl PySetInner {
|
||||
Ok(set)
|
||||
}
|
||||
|
||||
fn from_arg(iterable: OptionalArg<PyIterable>, vm: &VirtualMachine) -> PyResult<PySetInner> {
|
||||
if let OptionalArg::Present(iterable) = iterable {
|
||||
Self::new(iterable, vm)
|
||||
} else {
|
||||
Ok(PySetInner::default())
|
||||
}
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.content.len()
|
||||
}
|
||||
@@ -318,13 +310,21 @@ impl PySet {
|
||||
#[pyslot]
|
||||
fn tp_new(
|
||||
cls: PyTypeRef,
|
||||
iterable: OptionalArg<PyIterable>,
|
||||
_iterable: OptionalArg<PyIterable>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyRef<Self>> {
|
||||
Self {
|
||||
inner: PySetInner::from_arg(iterable, vm)?,
|
||||
PySet::default().into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn init(&self, iterable: OptionalArg<PyIterable>, vm: &VirtualMachine) -> PyResult<()> {
|
||||
if self.len() > 0 {
|
||||
self.clear();
|
||||
}
|
||||
.into_ref_with_type(vm, cls)
|
||||
if let OptionalArg::Present(it) = iterable {
|
||||
self.update(Args::new(vec![it]), vm)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
|
||||
Reference in New Issue
Block a user