Skip to content
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

bpo-46376: Change PySequence_Check and PyMapping_Check to use tpflags #30600

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 7 additions & 5 deletions Doc/c-api/mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and

.. c:function:: int PyMapping_Check(PyObject *o)

Return ``1`` if the object provides mapping protocol or supports slicing,
and ``0`` otherwise. Note that it returns ``1`` for Python classes with
a :meth:`__getitem__` method since in general case it is impossible to
determine what type of keys it supports. This function always succeeds.

Return ``1`` if the object provides mapping protocol
and ``0`` otherwise. This checks if :const:`Py_TPFLAGS_MAPPING` flag
set in its :c:member:`~PyTypeObject.tp_flags`). This function always succeeds.

.. versionchanged:: 3.11
Previously, the function only checked for the existence of :meth:`__getitem__`
leading to uncertainity between sequences and mappings.

.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
Py_ssize_t PyMapping_Length(PyObject *o)
Expand Down
12 changes: 7 additions & 5 deletions Doc/c-api/sequence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ Sequence Protocol

.. c:function:: int PySequence_Check(PyObject *o)

Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
Note that it returns ``1`` for Python classes with a :meth:`__getitem__`
method unless they are :class:`dict` subclasses since in general case it
is impossible to determine what the type of keys it supports. This
function always succeeds.
Return ``1`` if the object provides sequence protocol
and ``0`` otherwise. This checks if :const:`Py_TPFLAGS_SEQUENCE` flag
set in its :c:member:`~PyTypeObject.tp_flags`). This function always succeeds.

.. versionchanged:: 3.11
Previously, the function only checked for the existence of :meth:`__getitem__`
leading to uncertainity between sequences and mappings.


.. c:function:: Py_ssize_t PySequence_Size(PyObject *o)
Expand Down
4 changes: 2 additions & 2 deletions Lib/sre_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""Internal support module for sre"""

# XXX: show string offset and offending character for all errors

import collections.abc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may hurt startup performance, since collections.abc is a heavy module and re currently does not depend on it.

from sre_constants import *

SPECIAL_CHARS = ".\\[{()*+?^$|"
Expand Down Expand Up @@ -106,7 +106,7 @@ def checklookbehindgroup(self, gid, source):
raise source.error('cannot refer to group defined in the same '
'lookbehind subpattern')

class SubPattern:
class SubPattern(collections.abc.Sequence):
# a subpattern, in intermediate form
def __init__(self, state, data=None):
self.state = state
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ Stephen Hansen
Barry Hantman
Lynda Hardman
Bar Harel
Aviram Hassan
Derek Harland
Jason Harper
David Harrigan
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Changed `PySequence_Check` and `PyMapping_Check` to use `Py_TPFLAGS_SEQUENCE` and `Py_TPFLAGS_MAPPING`.
This fixes possible scenarios where an object is both sequence and mapping at the same time when checked using those functions.
It changes behavior for objects not being iterable/sequences if not inheriting from `abc.collections.Sequence`.
8 changes: 2 additions & 6 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1708,10 +1708,7 @@ PyNumber_ToBase(PyObject *n, int base)
int
PySequence_Check(PyObject *s)
{
if (PyDict_Check(s))
return 0;
return Py_TYPE(s)->tp_as_sequence &&
Py_TYPE(s)->tp_as_sequence->sq_item != NULL;
return s && (Py_TYPE(s)->tp_flags & Py_TPFLAGS_SEQUENCE);
}

Py_ssize_t
Expand Down Expand Up @@ -2297,8 +2294,7 @@ PySequence_Index(PyObject *s, PyObject *o)
int
PyMapping_Check(PyObject *o)
{
return o && Py_TYPE(o)->tp_as_mapping &&
Py_TYPE(o)->tp_as_mapping->mp_subscript;
return o && (Py_TYPE(o)->tp_flags & Py_TPFLAGS_MAPPING);
}

Py_ssize_t
Expand Down