@@ -37,7 +37,8 @@ Object Protocol
3737
3838 Exceptions that occur when this calls :meth: `~object.__getattr__ ` and
3939 :meth: `~object.__getattribute__ ` methods are silently ignored.
40- For proper error handling, use :c:func: `PyObject_GetAttr ` instead.
40+ For proper error handling, use :c:func: `PyObject_GetOptionalAttr ` or
41+ :c:func: `PyObject_GetAttr ` instead.
4142
4243
4344.. c :function :: int PyObject_HasAttrString (PyObject *o, const char *attr_name)
@@ -51,7 +52,8 @@ Object Protocol
5152 Exceptions that occur when this calls :meth: `~object.__getattr__ ` and
5253 :meth: `~object.__getattribute__ ` methods or while creating the temporary :class: `str `
5354 object are silently ignored.
54- For proper error handling, use :c:func: `PyObject_GetAttrString ` instead.
55+ For proper error handling, use :c:func: `PyObject_GetOptionalAttrString `
56+ or :c:func: `PyObject_GetAttrString ` instead.
5557
5658
5759.. c :function :: PyObject* PyObject_GetAttr (PyObject *o, PyObject *attr_name)
@@ -60,13 +62,48 @@ Object Protocol
6062 value on success, or ``NULL `` on failure. This is the equivalent of the Python
6163 expression ``o.attr_name ``.
6264
65+ If the missing attribute should not be treated as a failure, you can use
66+ :c:func: `PyObject_GetOptionalAttr ` instead.
67+
6368
6469.. c :function :: PyObject* PyObject_GetAttrString (PyObject *o, const char *attr_name)
6570
6671 Retrieve an attribute named *attr_name * from object *o *. Returns the attribute
6772 value on success, or ``NULL `` on failure. This is the equivalent of the Python
6873 expression ``o.attr_name ``.
6974
75+ If the missing attribute should not be treated as a failure, you can use
76+ :c:func: `PyObject_GetOptionalAttrString ` instead.
77+
78+
79+ .. c :function :: int PyObject_GetOptionalAttr (PyObject *obj, PyObject *attr_name, PyObject **result);
80+
81+ Variant of :c:func: `PyObject_GetAttr ` which doesn't raise
82+ :exc: `AttributeError ` if the attribute is not found.
83+
84+ If the attribute is found, return ``1 `` and set *\* result * to a new
85+ :term: `strong reference ` to the attribute.
86+ If the attribute is not found, return ``0 `` and set *\* result * to ``NULL ``;
87+ the :exc: `AttributeError ` is silenced.
88+ If an error other than :exc: `AttributeError ` is raised, return ``-1 `` and
89+ set *\* result * to ``NULL ``.
90+
91+ .. versionadded :: 3.13
92+
93+
94+ .. c :function :: int PyObject_GetOptionalAttrString (PyObject *obj, const char *attr_name, PyObject **result);
95+
96+ Variant of :c:func: `PyObject_GetAttrString ` which doesn't raise
97+ :exc: `AttributeError ` if the attribute is not found.
98+
99+ If the attribute is found, return ``1 `` and set *\* result * to a new
100+ :term: `strong reference ` to the attribute.
101+ If the attribute is not found, return ``0 `` and set *\* result * to ``NULL ``;
102+ the :exc: `AttributeError ` is silenced.
103+ If an error other than :exc: `AttributeError ` is raised, return ``-1 `` and
104+ set *\* result * to ``NULL ``.
105+
106+ .. versionadded :: 3.13
70107
71108.. c :function :: PyObject* PyObject_GenericGetAttr (PyObject *o, PyObject *name)
72109
0 commit comments