Skip to content
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Victor Stinner <vstinner@python.org>
  • Loading branch information
3 people authored Jul 8, 2023
commit 4b3fc37d06d03940dad37cf4a9a80d5d44d388ec
16 changes: 8 additions & 8 deletions Doc/c-api/mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and

.. c:function:: int PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)

Replacement of :c:func:`PyObject_GetItem` which doesn't raise :exc:`KeyError`.
Variant of :c:func:`PyObject_GetItem` which doesn't raise :exc:`KeyError`.

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

Expand All @@ -48,11 +48,11 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and

.. c:function:: int PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)

Replacement of :c:func:`PyMapping_GetItemString` which doesn't raise :exc:`KeyError`.
Variant of :c:func:`PyMapping_GetItemString` which doesn't raise :exc:`KeyError`.

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

Expand Down
4 changes: 2 additions & 2 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,10 @@ New Features
(Contributed by Victor Stinner in :gh:`105927`.)

* Add :c:func:`PyMapping_GetOptionalItem` and
:c:func:`PyMapping_GetOptionalItemString` function: replacements of
:c:func:`PyMapping_GetOptionalItemString`: variants of
:c:func:`PyObject_GetAttr` and :c:func:`PyMapping_GetItemString` which don't
raise :exc:`KeyError`.
It is convenient and faster if the missing key should not be treated
These variants are more convenient and faster if the missing key should not be treated
as a failure.
(Contributed by Serhiy Storchaka in :gh:`106307`.)

Expand Down
2 changes: 1 addition & 1 deletion Include/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
const char *key);

/* Replacements of PyObject_GetItem() and PyMapping_GetItemString() which don't
/* Variants of PyObject_GetItem() and PyMapping_GetItemString() which don't
raise KeyError.

Return 1 and set *result != NULL if a key is found.
Expand Down
2 changes: 1 addition & 1 deletion Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -4439,7 +4439,7 @@ save(PickleState *st, PicklerObject *self, PyObject *obj, int pers_save)
}
} else {
if (PyMapping_GetOptionalItem(self->dispatch_table, (PyObject *)type,
&reduce_func) < 0) {
&reduce_func) < 0) {
goto error;
}
}
Expand Down