Skip to content

Commit 4103e4d

Browse files
committed
Issue #28071: Add early-out for differencing from an empty set.
1 parent 34b74ff commit 4103e4d

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Core and Builtins
3333

3434
- Issue #28046: Remove platform-specific directories from sys.path.
3535

36+
- Issue #28071: Add early-out for differencing from an empty set.
37+
3638
- Issue #25758: Prevents zipimport from unnecessarily encoding a filename
3739
(patch by Eryk Sun)
3840

Objects/setobject.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,10 @@ PyDoc_STRVAR(isdisjoint_doc,
14761476
static int
14771477
set_difference_update_internal(PySetObject *so, PyObject *other)
14781478
{
1479+
if (PySet_GET_SIZE(so) == 0) {
1480+
return 0;
1481+
}
1482+
14791483
if ((PyObject *)so == other)
14801484
return set_clear_internal(so);
14811485

@@ -1550,6 +1554,10 @@ set_difference(PySetObject *so, PyObject *other)
15501554
Py_ssize_t pos = 0;
15511555
int rv;
15521556

1557+
if (PySet_GET_SIZE(so) == 0) {
1558+
return set_copy(so);
1559+
}
1560+
15531561
if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
15541562
return set_copy_and_difference(so, other);
15551563
}

0 commit comments

Comments
 (0)