@@ -37,7 +37,8 @@ Object Protocol
37
37
38
38
Exceptions that occur when this calls :meth: `~object.__getattr__ ` and
39
39
: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.
41
42
42
43
43
44
.. c :function :: int PyObject_HasAttrString (PyObject *o, const char *attr_name)
@@ -51,7 +52,8 @@ Object Protocol
51
52
Exceptions that occur when this calls :meth: `~object.__getattr__ ` and
52
53
:meth: `~object.__getattribute__ ` methods or while creating the temporary :class: `str `
53
54
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.
55
57
56
58
57
59
.. c :function :: PyObject* PyObject_GetAttr (PyObject *o, PyObject *attr_name)
@@ -60,13 +62,48 @@ Object Protocol
60
62
value on success, or ``NULL `` on failure. This is the equivalent of the Python
61
63
expression ``o.attr_name ``.
62
64
65
+ If the missing attribute should not be treated as a failure, you can use
66
+ :c:func: `PyObject_GetOptionalAttr ` instead.
67
+
63
68
64
69
.. c :function :: PyObject* PyObject_GetAttrString (PyObject *o, const char *attr_name)
65
70
66
71
Retrieve an attribute named *attr_name * from object *o *. Returns the attribute
67
72
value on success, or ``NULL `` on failure. This is the equivalent of the Python
68
73
expression ``o.attr_name ``.
69
74
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
70
107
71
108
.. c :function :: PyObject* PyObject_GenericGetAttr (PyObject *o, PyObject *name)
72
109
0 commit comments