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

Replace PyDict_GetItem() with PyDict_GetItemWithError() #64814

Closed
vstinner opened this issue Feb 13, 2014 · 3 comments
Closed

Replace PyDict_GetItem() with PyDict_GetItemWithError() #64814

vstinner opened this issue Feb 13, 2014 · 3 comments

Comments

@vstinner
Copy link
Member

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']

@vstinner
Copy link
Member Author

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.

@rhettinger
Copy link
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.

@vstinner
Copy link
Member Author

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

@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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants