Skip to content

Commit 6484df3

Browse files
committed
Make frozenset idempotent and unskip some set tests
1 parent c5fca81 commit 6484df3

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

Lib/test/test_set.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def test_badcmp(self):
315315
self.assertRaises(RuntimeError, s.discard, BadCmp())
316316
self.assertRaises(RuntimeError, s.remove, BadCmp())
317317

318+
@unittest.skip("TODO: RUSTPYTHON")
318319
def test_cyclical_repr(self):
319320
w = ReprWrapper()
320321
s = self.thetype([w])
@@ -676,7 +677,6 @@ def test_c_api(self):
676677
class SetSubclass(set):
677678
pass
678679

679-
@unittest.skip("TODO: RUSTPYTHON")
680680
class TestSetSubclass(TestSet):
681681
thetype = SetSubclass
682682
basetype = set
@@ -685,9 +685,9 @@ class SetSubclassWithKeywordArgs(set):
685685
def __init__(self, iterable=[], newarg=None):
686686
set.__init__(self, iterable)
687687

688-
@unittest.skip("TODO: RUSTPYTHON")
689688
class TestSetSubclassWithKeywordArgs(TestSet):
690-
689+
# TODO: RUSTPYTHON
690+
@unittest.expectedFailure
691691
def test_keywords_in_subclass(self):
692692
'SF bug #1486663 -- this used to erroneously raise a TypeError'
693693
SetSubclassWithKeywordArgs(newarg=1)
@@ -712,8 +712,6 @@ def test_singleton_empty_frozenset(self):
712712
# All of the empty frozensets should have just one id()
713713
self.assertEqual(len(set(map(id, efs))), 1)
714714

715-
# TODO: RUSTPYTHON
716-
@unittest.expectedFailure
717715
def test_constructor_identity(self):
718716
s = self.thetype(range(3))
719717
t = self.thetype(s)

vm/src/builtins/set.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use super::pytype::PyTypeRef;
55
use crate::common::hash::PyHash;
66
use crate::common::rc::PyRc;
77
use crate::dictdatatype;
8+
use crate::function::OptionalArg::{Missing, Present};
89
use crate::function::{Args, OptionalArg};
910
use crate::pyobject::{
10-
self, BorrowValue, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef,
11-
PyResult, PyValue, TryFromObject, TypeProtocol,
11+
self, BorrowValue, IdProtocol, PyClassImpl, PyComparisonValue, PyContext, PyIterable,
12+
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
1213
};
1314
use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhashable};
1415
use crate::vm::{ReprGuard, VirtualMachine};
@@ -569,9 +570,22 @@ impl PyFrozenSet {
569570
#[pyslot]
570571
fn tp_new(
571572
cls: PyTypeRef,
572-
iterable: OptionalArg<PyIterable>,
573+
iterable: OptionalArg<PyObjectRef>,
573574
vm: &VirtualMachine,
574575
) -> PyResult<PyRef<Self>> {
576+
let iterable = if let Present(iterable) = iterable {
577+
if cls.is(&vm.ctx.types.frozenset_type) {
578+
match iterable.downcast_exact::<PyFrozenSet>(vm) {
579+
Ok(iter) => return Ok(iter),
580+
Err(iterable) => Present(PyIterable::try_from_object(vm, iterable)?),
581+
}
582+
} else {
583+
Present(PyIterable::try_from_object(vm, iterable)?)
584+
}
585+
} else {
586+
Missing
587+
};
588+
575589
Self {
576590
inner: PySetInner::from_arg(iterable, vm)?,
577591
}

0 commit comments

Comments
 (0)