Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-40521: Empty frozensets are no longer singletons
  • Loading branch information
rhettinger committed Jun 23, 2020
commit da8e997a082ffea1c0109d2b0d850ae23da77d32
7 changes: 0 additions & 7 deletions Lib/test/test_marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ def test_sets(self):
for constructor in (set, frozenset):
self.helper(constructor(self.d.keys()))

@support.cpython_only
def test_empty_frozenset_singleton(self):
# marshal.loads() must reuse the empty frozenset singleton
obj = frozenset()
obj2 = marshal.loads(marshal.dumps(obj))
self.assertIs(obj2, obj)


class BufferTestCase(unittest.TestCase, HelperMixin):

Expand Down
9 changes: 0 additions & 9 deletions Lib/test/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,6 @@ def test_init(self):
s.__init__(self.otherword)
self.assertEqual(s, set(self.word))

def test_singleton_empty_frozenset(self):
f = frozenset()
efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''),
frozenset(), frozenset([]), frozenset(()), frozenset(''),
frozenset(range(0)), frozenset(frozenset()),
frozenset(f), f]
# All of the empty frozensets should have just one id()
self.assertEqual(len(set(map(id, efs))), 1)

def test_constructor_identity(self):
s = self.thetype(range(3))
t = self.thetype(s)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Empty frozensets are no longer singletons.
27 changes: 5 additions & 22 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,36 +975,19 @@ make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
return make_new_set(type, iterable);
}

/* The empty frozenset is a singleton */
static PyObject *emptyfrozenset = NULL;

static PyObject *
make_new_frozenset(PyTypeObject *type, PyObject *iterable)
{
if (type != &PyFrozenSet_Type) {
return make_new_set(type, iterable);
}

if (iterable != NULL) {
if (PyFrozenSet_CheckExact(iterable)) {
/* frozenset(f) is idempotent */
Py_INCREF(iterable);
return iterable;
}
PyObject *res = make_new_set((PyTypeObject *)type, iterable);
if (res == NULL || PySet_GET_SIZE(res) != 0) {
return res;
if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
/* frozenset(f) is idempotent */
Py_INCREF(iterable);
return iterable;
}
/* If the created frozenset is empty, return the empty frozenset singleton instead */
Py_DECREF(res);
}

// The empty frozenset is a singleton
if (emptyfrozenset == NULL) {
emptyfrozenset = make_new_set((PyTypeObject *)type, NULL);
}
Py_XINCREF(emptyfrozenset);
return emptyfrozenset;
return make_new_set((PyTypeObject *)type, iterable);
}

static PyObject *
Expand Down