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

SystemError in _collections._count_element() in case of a bad mapping argument #75767

Closed
orenmn mannequin opened this issue Sep 26, 2017 · 5 comments
Closed

SystemError in _collections._count_element() in case of a bad mapping argument #75767

orenmn mannequin opened this issue Sep 26, 2017 · 5 comments
Assignees
Labels
3.7 (EOL) end of life extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@orenmn
Copy link
Mannequin

orenmn mannequin commented Sep 26, 2017

BPO 31586
Nosy @rhettinger, @serhiy-storchaka, @orenmn
PRs
  • bpo-31586: Fix a SystemError in _collections._count_element() in case of a bad mapping arg #3763
  • [3.6] bpo-31586: Use _count_element fast path for real dicts. #3776
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2017-09-27.03:46:36.120>
    created_at = <Date 2017-09-26.06:32:28.949>
    labels = ['extension-modules', 'type-bug', '3.7']
    title = 'SystemError in _collections._count_element() in case of a bad mapping argument'
    updated_at = <Date 2017-09-27.03:46:36.119>
    user = 'https://github.com/orenmn'

    bugs.python.org fields:

    activity = <Date 2017-09-27.03:46:36.119>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2017-09-27.03:46:36.120>
    closer = 'rhettinger'
    components = ['Extension Modules']
    creation = <Date 2017-09-26.06:32:28.949>
    creator = 'Oren Milman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31586
    keywords = ['patch']
    message_count = 5.0
    messages = ['303014', '303016', '303077', '303088', '303089']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'serhiy.storchaka', 'Oren Milman']
    pr_nums = ['3763', '3776']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31586'
    versions = ['Python 3.6', 'Python 3.7']

    @orenmn
    Copy link
    Mannequin Author

    orenmn mannequin commented Sep 26, 2017

    The following code causes a SystemError:
    class BadMapping:
    get = dict.get
    __setitem__ = dict.__setitem__

    import _collections
    _collections._count_elements(BadMapping(), [42])

    This is because _count_elements() (in Modules/_collectionsmodule.c) assumes
    that the mapping argument is a dictionary in case it has the same get() and
    __setitem__() methods as dict. And so, _count_elements() passes the mapping
    argument to _PyDict_GetItem_KnownHash(), which raises the SystemError.

    ISTM it is a very unlikely corner case, so that adding a test (as well as
    a NEWS.d item) for it is unnecessary.
    What do you think?

    @orenmn orenmn mannequin added 3.8 only security fixes extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error labels Sep 26, 2017
    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life and removed 3.8 only security fixes labels Sep 26, 2017
    @serhiy-storchaka
    Copy link
    Member

    I agree. _collections._count_elements() is an internal function. It should be called only with Counter (which is a dict subclass) or its subclass.

    But I think it is worth to make the code more reliable.

    @rhettinger
    Copy link
    Contributor

    Oren, thank you for the work you've been doing.

    @rhettinger rhettinger self-assigned this Sep 26, 2017
    @rhettinger
    Copy link
    Contributor

    New changeset 31aca4b by Raymond Hettinger (Oren Milman) in branch 'master':
    bpo-31586: Use _count_element fast path for real dicts.
    31aca4b

    @rhettinger
    Copy link
    Contributor

    New changeset a1c49f6 by Raymond Hettinger (Miss Islington (bot)) in branch '3.6':
    [3.6] bpo-31586: Use _count_element fast path for real dicts. (bpo-3776)
    a1c49f6

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants