Skip to content

Commit a9bd787

Browse files
committed
bpo-43277: Add PySet_CheckExact to the C-API
1 parent 46496f9 commit a9bd787

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

Doc/c-api/set.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ the constructor functions work with any iterable Python object.
6565
Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
6666
instance of a subtype. This function always succeeds.
6767
68+
.. c:function:: int PySet_CheckExact(PyObject *p)
69+
70+
Return true if *p* is a :class:`set` object but not an instance of a
71+
subtype. This function always succeeds.
72+
73+
.. versionadded:: 3.10
6874
6975
.. c:function:: int PyAnySet_CheckExact(PyObject *p)
7076

Doc/whatsnew/3.10.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,9 @@ New Features
893893
* The :c:func:`PyType_GetSlot` function can accept static types.
894894
(Contributed by Hai Shi and Petr Viktorin in :issue:`41073`.)
895895
896+
* Add a new :c:func:`PySet_CheckExact` function to the C-API to check if an
897+
object is an instance of :class:`set` but not an instance of a subtype.
898+
(Contributed by Pablo Galindo in :issue:`43277`.)
896899
897900
Porting to Python 3.10
898901
----------------------

Include/setobject.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,21 @@ PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
8888
PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
8989

9090
#define PyFrozenSet_CheckExact(ob) Py_IS_TYPE(ob, &PyFrozenSet_Type)
91+
#define PyFrozenSet_Check(ob) \
92+
(Py_IS_TYPE(ob, &PyFrozenSet_Type) || \
93+
PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
94+
9195
#define PyAnySet_CheckExact(ob) \
9296
(Py_IS_TYPE(ob, &PySet_Type) || Py_IS_TYPE(ob, &PyFrozenSet_Type))
9397
#define PyAnySet_Check(ob) \
9498
(Py_IS_TYPE(ob, &PySet_Type) || Py_IS_TYPE(ob, &PyFrozenSet_Type) || \
9599
PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
96100
PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
101+
102+
#define PySet_CheckExact(op) Py_IS_TYPE(op, &PySet_Type)
97103
#define PySet_Check(ob) \
98104
(Py_IS_TYPE(ob, &PySet_Type) || \
99105
PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
100-
#define PyFrozenSet_Check(ob) \
101-
(Py_IS_TYPE(ob, &PyFrozenSet_Type) || \
102-
PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
103106

104107
#ifdef __cplusplus
105108
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add a new :c:func:`PySet_CheckExact` function to the C-API to check if an
2+
object is an instance of :class:`set` but not an instance of a subtype.
3+
Patch by Pablo Galindo.

Objects/dictobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4482,7 +4482,7 @@ _PyDictView_Intersect(PyObject* self, PyObject *other)
44824482

44834483
/* if other is a set and self is smaller than other,
44844484
reuse set intersection logic */
4485-
if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
4485+
if (PySet_CheckExact(other) && len_self <= PyObject_Size(other)) {
44864486
_Py_IDENTIFIER(intersection);
44874487
return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
44884488
}

Objects/setobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ set_repr(PySetObject *so)
522522
goto done;
523523
listrepr = tmp;
524524

525-
if (!Py_IS_TYPE(so, &PySet_Type))
525+
if (!PySet_CheckExact(so))
526526
result = PyUnicode_FromFormat("%s({%U})",
527527
Py_TYPE(so)->tp_name,
528528
listrepr);

0 commit comments

Comments
 (0)