◐ Shell
reader mode source ↗
Skip to content

gh-106307: Add PyMapping_GetOptionalItem()#106308

Merged
serhiy-storchaka merged 10 commits into
python:mainfrom
serhiy-storchaka:_PyMapping_LookupItem
Jul 11, 2023
Merged

gh-106307: Add PyMapping_GetOptionalItem()#106308
serhiy-storchaka merged 10 commits into
python:mainfrom
serhiy-storchaka:_PyMapping_LookupItem

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jul 1, 2023

Copy link
Copy Markdown
Member

Replacement of PyObject_GetItem() which doesn't raise KeyError.

int _PyMapping_LookupItem(PyObject *obj, PyObject *key, PyObject **result)

Return 1 and set *result != NULL if a key is found.
Return 0 and set *result == NULL if a key is not found; a KeyError is silenced.
Return -1 and set *result == NULL if an error other than KeyError is raised.

Replacement of PyObject_GetItem() which doesn't raise KeyError.

  int _PyMapping_LookupItem(PyObject *obj, PyObject *key, PyObject **result)

Return 1 and set *result != NULL if a key is found.
Return 0 and set *result == NULL if a key is not found;
a KeyError is silenced.
Return -1 and set *result == NULL if an error other than KeyError
is raised.
@serhiy-storchaka serhiy-storchaka force-pushed the _PyMapping_LookupItem branch from cfc5851 to 078ea48 Compare July 1, 2023 20:46
@serhiy-storchaka serhiy-storchaka requested review from a team and encukou as code owners July 7, 2023 17:15
@serhiy-storchaka serhiy-storchaka changed the title gh-106307: Add _PyMapping_LookupItem() Jul 7, 2023

@carljm carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

The bytecodes.c cleanups alone (i.e. the existing cases where we cared enough about perf to special-case the exact-dict fast path) clearly show the value of this API! Now all call sites can get the same perf benefit, with much less verbosity.

@brettcannon brettcannon removed their request for review July 7, 2023 22:01
serhiy-storchaka and others added 3 commits July 8, 2023 11:45
Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Carl Meyer <carl@oddbird.net>

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

LGTM

@serhiy-storchaka serhiy-storchaka merged commit 4bf4371 into python:main Jul 11, 2023
@serhiy-storchaka serhiy-storchaka deleted the _PyMapping_LookupItem branch July 11, 2023 20:04
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

Successfully merging this pull request may close these issues.

6 participants