Skip to content

Commit 19d6511

Browse files
authored
gh-106033: [docs] Improve C API GetItem & HasAttr notes. (#106047)
Use a note:: tag so that these dict and object API deficiencies show up clearly. A caution:: tag was considered, but our current python docs rendering doesn't do much with that (no box or color change). warning:: seemed too extreme. note looks good.
1 parent 4a6c84f commit 19d6511

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

Doc/c-api/dict.rst

+12-7
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ Dictionary Objects
9898
Return the object from dictionary *p* which has a key *key*. Return ``NULL``
9999
if the key *key* is not present, but *without* setting an exception.
100100
101-
Note that exceptions which occur while calling :meth:`__hash__` and
102-
:meth:`__eq__` methods will get suppressed.
103-
To get error reporting use :c:func:`PyDict_GetItemWithError()` instead.
101+
.. note::
102+
103+
Exceptions that occur while this calls :meth:`~object.__hash__` and
104+
:meth:`~object.__eq__` methods are silently ignored.
105+
Prefer the :c:func:`PyDict_GetItemWithError` function instead.
104106
105107
.. versionchanged:: 3.10
106108
Calling this API without :term:`GIL` held had been allowed for historical
@@ -120,10 +122,13 @@ Dictionary Objects
120122
This is the same as :c:func:`PyDict_GetItem`, but *key* is specified as a
121123
:c:expr:`const char*`, rather than a :c:expr:`PyObject*`.
122124
123-
Note that exceptions which occur while calling :meth:`__hash__` and
124-
:meth:`__eq__` methods and creating a temporary string object
125-
will get suppressed.
126-
To get error reporting use :c:func:`PyDict_GetItemWithError()` instead.
125+
.. note::
126+
127+
Exceptions that occur while this calls :meth:`~object.__hash__` and
128+
:meth:`~object.__eq__` methods or while creating the temporary :class:`str`
129+
object are silently ignored.
130+
Prefer using the :c:func:`PyDict_GetItemWithError` function with your own
131+
:c:func:`PyUnicode_FromString` *key* instead.
127132
128133
129134
.. c:function:: PyObject* PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *defaultobj)

Doc/c-api/object.rst

+11-7
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ Object Protocol
3333
is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
3434
always succeeds.
3535
36-
Note that exceptions which occur while calling :meth:`__getattr__` and
37-
:meth:`__getattribute__` methods will get suppressed.
38-
To get error reporting use :c:func:`PyObject_GetAttr()` instead.
36+
.. note::
37+
38+
Exceptions that occur when this calls :meth:`~object.__getattr__` and
39+
:meth:`~object.__getattribute__` methods are silently ignored.
40+
For proper error handling, use :c:func:`PyObject_GetAttr` instead.
3941
4042
4143
.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
@@ -44,10 +46,12 @@ Object Protocol
4446
is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
4547
always succeeds.
4648
47-
Note that exceptions which occur while calling :meth:`__getattr__` and
48-
:meth:`__getattribute__` methods and creating a temporary string object
49-
will get suppressed.
50-
To get error reporting use :c:func:`PyObject_GetAttrString()` instead.
49+
.. note::
50+
51+
Exceptions that occur when this calls :meth:`~object.__getattr__` and
52+
:meth:`~object.__getattribute__` methods or while creating the temporary :class:`str`
53+
object are silently ignored.
54+
For proper error handling, use :c:func:`PyObject_GetAttrString` instead.
5155
5256
5357
.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)

0 commit comments

Comments
 (0)