Skip to content

Replace PyDict_GetItem() with PyDict_GetItemWithError() #64814

Closed

Description

@vstinner
BPO 20615
Nosy @rhettinger, @vstinner, @serhiy-storchaka

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 = None
closed_at = <Date 2017-06-28.01:33:58.858>
created_at = <Date 2014-02-13.10:44:13.118>
labels = []
title = 'Replace PyDict_GetItem() with PyDict_GetItemWithError()'
updated_at = <Date 2017-06-28.01:33:58.857>
user = 'https://github.com/vstinner'

bugs.python.org fields:

activity = <Date 2017-06-28.01:33:58.857>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2017-06-28.01:33:58.858>
closer = 'vstinner'
components = []
creation = <Date 2014-02-13.10:44:13.118>
creator = 'vstinner'
dependencies = []
files = []
hgrepos = []
issue_num = 20615
keywords = []
message_count = 3.0
messages = ['211142', '211167', '297122']
nosy_count = 3.0
nosy_names = ['rhettinger', 'vstinner', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue20615'
versions = ['Python 3.3', 'Python 3.4', 'Python 3.5']

Activity

  1. vstinner commented on Feb 13, 2014

    @vstinner
    MemberAuthor

    See this huge warning in the definition of PyDict_GetItem:

    /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors

    • that may occur (originally dicts supported only string keys, and exceptions
    • weren't possible). So, while the original intent was that a NULL return
    • meant the key wasn't present, in reality it can mean that, or that an error
    • (suppressed) occurred while computing the key's hash, or that some error
    • (suppressed) occurred when comparing keys in the dict's internal probe
    • sequence. A nasty example of the latter is when a Python-coded comparison
    • function hits a stack-depth error, which can cause this to return NULL
    • even if the key is present.
      */
      PyObject *
      PyDict_GetItem(PyObject *op, PyObject *key)
      { ... }

    PyDict_GetItem() should avoided because it may hide important exceptions like KeyboardInterrupt or MemoryError.

    See for example bpo-14537 for a specific error.

    By the way, the PyDict_GetItem() documentation should contain a big red warning and suggest to use PyDict_GetItemWithError() instead.

  2. rhettinger commented on Feb 13, 2014

    @rhettinger
    Contributor

    Please, no more big red warnings.

    Keep the docs factual and straight-forward. In the docs for PyDict_GetItem(), note that errors will get suppressed. To
    get error reporting use PyDict_GetItemWithError() instead.

  3. vstinner commented on Jun 28, 2017

    @vstinner
    MemberAuthor

    Sorry, I lost track of this issue, so I just close it.

  4. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions