8000 bpo-43277: Add PySet_CheckExact to the C-API by pablogsal · Pull Request #24598 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-43277: Add PySet_CheckExact to the C-API #24598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions Doc/c-api/set.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ the constructor functions work with any iterable Python object.
Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
instance of a subtype. This fu 8000 nction 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)

Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,9 @@ New Features
* 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
----------------------
Expand Down
9 changes: 6 additions & 3 deletions Include/setobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,21 @@ PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.
Patch by Pablo Galindo.
2 changes: 1 addition & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4482,7 +4482,7 @@ _PyDictView_Intersect(PyObject* self, PyObject *other)

/* 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);
}
Expand Down
2 changes: 1 addition & 1 deletion Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ set_repr(PySetObject *so)
goto done;
listrepr = tmp;

if (!Py_IS_TYPE(so, &PySet_Type))
if (!PySet_CheckExact(so))
result = PyUnicode_FromFormat("%s({%U})",
Py_TYPE(so)->tp_name,
listrepr);
Expand Down
0