Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
instance of a subtype. This function always succeeds.
+.. c:function:: int PySet_CheckExact(PyObject *p)
+
+ Return true if *p* is a :class:`set` object but not an instance of a
+ subtype. This function always succeeds.
+
+ .. versionadded:: 3.10
.. c:function:: int PyAnySet_CheckExact(PyObject *p)
* The :c:func:`PyType_GetSlot` function can accept static types.
(Contributed by Hai Shi and Petr Viktorin in :issue:`41073`.)
+* Add a new :c:func:`PySet_CheckExact` function to the C-API to check if an
+ object is an instance of :class:`set` but not an instance of a subtype.
+ (Contributed by Pablo Galindo in :issue:`43277`.)
Porting to Python 3.10
----------------------
PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
#define PyFrozenSet_CheckExact(ob) Py_IS_TYPE(ob, &PyFrozenSet_Type)
+#define PyFrozenSet_Check(ob) \
+ (Py_IS_TYPE(ob, &PyFrozenSet_Type) || \
+ PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
+
#define PyAnySet_CheckExact(ob) \
(Py_IS_TYPE(ob, &PySet_Type) || Py_IS_TYPE(ob, &PyFrozenSet_Type))
#define PyAnySet_Check(ob) \
(Py_IS_TYPE(ob, &PySet_Type) || Py_IS_TYPE(ob, &PyFrozenSet_Type) || \
PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
+
+#define PySet_CheckExact(op) Py_IS_TYPE(op, &PySet_Type)
#define PySet_Check(ob) \
(Py_IS_TYPE(ob, &PySet_Type) || \
PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
-#define PyFrozenSet_Check(ob) \
- (Py_IS_TYPE(ob, &PyFrozenSet_Type) || \
- PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
#ifdef __cplusplus
}
/* if other is a set and self is smaller than other,
reuse set intersection logic */
- if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
+ if (PySet_CheckExact(other) && len_self <= PyObject_Size(other)) {
_Py_IDENTIFIER(intersection);
return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
}