Skip to content

Commit 255b3e7

Browse files
Improve some C API documentation
* Express functions which take argument as a C string in terms of functions which take Python object. * Use "note" directive for PyMapping_HasKey() and PyMapping_HasKeyString() notes.
1 parent 23f54c1 commit 255b3e7

File tree

3 files changed

+54
-65
lines changed

3 files changed

+54
-65
lines changed

Doc/c-api/dict.rst

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,9 @@ Dictionary Objects
7979
8080
.. c:function:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
8181
82-
.. index:: single: PyUnicode_FromString()
83-
84-
Insert *val* into the dictionary *p* using *key* as a key. *key* should
85-
be a :c:expr:`const char*` UTF-8 encoded bytes string. The key object is created using
86-
``PyUnicode_FromString(key)``. Return ``0`` on success or ``-1`` on
87-
failure. This function *does not* steal a reference to *val*.
82+
This is the same as :c:func:`PyDict_SetItem`, but *key* is
83+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
84+
rather than a :c:expr:`PyObject*`.
8885
8986
9087
.. c:function:: int PyDict_DelItem(PyObject *p, PyObject *key)
@@ -97,10 +94,9 @@ Dictionary Objects
9794
9895
.. c:function:: int PyDict_DelItemString(PyObject *p, const char *key)
9996
100-
Remove the entry in dictionary *p* which has a key specified by the UTF-8
101-
encoded bytes string *key*.
102-
If *key* is not in the dictionary, :exc:`KeyError` is raised.
103-
Return ``0`` on success or ``-1`` on failure.
97+
This is the same as :c:func:`PyDict_DelItem`, but *key* is
98+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
99+
rather than a :c:expr:`PyObject*`.
104100
105101
106102
.. c:function:: int PyDict_GetItemRef(PyObject *p, PyObject *key, PyObject **result)

Doc/c-api/mapping.rst

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
2828
2929
.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
3030
31-
Return element of *o* corresponding to the string *key* or ``NULL`` on failure.
32-
This is the equivalent of the Python expression ``o[key]``.
33-
See also :c:func:`PyObject_GetItem`.
31+
This is the same as :c:func:`PyObject_GetItem`, but *key* is
32+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
33+
rather than a :c:expr:`PyObject*`.
3434
3535
3636
.. c:function:: int PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
@@ -50,38 +50,30 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
5050
5151
.. c:function:: int PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)
5252
53-
Variant of :c:func:`PyMapping_GetItemString` which doesn't raise
54-
:exc:`KeyError` if the key is not found.
55-
56-
If the key is found, return ``1`` and set *\*result* to a new
57-
:term:`strong reference` to the corresponding value.
58-
If the key is not found, return ``0`` and set *\*result* to ``NULL``;
59-
the :exc:`KeyError` is silenced.
60-
If an error other than :exc:`KeyError` is raised, return ``-1`` and
61-
set *\*result* to ``NULL``.
53+
This is the same as :c:func:`PyMapping_GetOptionalItem`, but *key* is
54+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
55+
rather than a :c:expr:`PyObject*`.
6256
6357
.. versionadded:: 3.13
6458
6559
6660
.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
6761
68-
Map the string *key* to the value *v* in object *o*. Returns ``-1`` on
69-
failure. This is the equivalent of the Python statement ``o[key] = v``.
70-
See also :c:func:`PyObject_SetItem`. This function *does not* steal a
71-
reference to *v*.
62+
This is the same as :c:func:`PyObject_SetItem`, but *key* is
63+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
64+
rather than a :c:expr:`PyObject*`.
7265
7366
7467
.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
7568
76-
Remove the mapping for the object *key* from the object *o*. Return ``-1``
77-
on failure. This is equivalent to the Python statement ``del o[key]``.
7869
This is an alias of :c:func:`PyObject_DelItem`.
7970
8071
8172
.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
8273
83-
Remove the mapping for the string *key* from the object *o*. Return ``-1``
84-
on failure. This is equivalent to the Python statement ``del o[key]``.
74+
This is the same as :c:func:`PyObject_DelItem`, but *key* is
75+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
76+
rather than a :c:expr:`PyObject*`.
8577
8678
8779
.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
@@ -90,20 +82,27 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
9082
This is equivalent to the Python expression ``key in o``.
9183
This function always succeeds.
9284
93-
Note that exceptions which occur while calling the :meth:`~object.__getitem__`
94-
method will get suppressed.
95-
To get error reporting use :c:func:`PyObject_GetItem()` instead.
85+
.. note::
86+
87+
Exceptions which occur when this calls :meth:`~object.__getitem__`
88+
method are silently ignored.
89+
For proper error handling, use :c:func:`PyMapping_GetOptionalItem` or
90+
:c:func:`PyObject_GetItem()` instead.
9691
9792
9893
.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
9994
100-
Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
101-
This is equivalent to the Python expression ``key in o``.
102-
This function always succeeds.
95+
This is the same as :c:func:`PyMapping_HasKey`, but *key* is
96+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
97+
rather than a :c:expr:`PyObject*`.
98+
99+
.. note::
103100
104-
Note that exceptions which occur while calling the :meth:`~object.__getitem__`
105-
method and creating a temporary string object will get suppressed.
106-
To get error reporting use :c:func:`PyMapping_GetItemString()` instead.
101+
Exceptions that occur when this calls :meth:`~object.__getitem__`
102+
method or while creating the temporary :class:`str`
103+
object are silently ignored.
104+
For proper error handling, use :c:func:`PyMapping_GetOptionalItemString` or
105+
:c:func:`PyMapping_GetItemString` instead.
107106
108107
109108
.. c:function:: PyObject* PyMapping_Keys(PyObject *o)

Doc/c-api/object.rst

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ Object Protocol
4343
4444
.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
4545
46-
Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
47-
is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
48-
always succeeds.
46+
This is the same as :c:func:`PyObject_HasAttr`, but *attr_name* is
47+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
48+
rather than a :c:expr:`PyObject*`.
4949
5050
.. note::
5151
5252
Exceptions that occur when this calls :meth:`~object.__getattr__` and
53-
:meth:`~object.__getattribute__` methods or while creating the temporary :class:`str`
54-
object are silently ignored.
55-
For proper error handling, use :c:func:`PyObject_GetOptionalAttrString`
56-
or :c:func:`PyObject_GetAttrString` instead.
53+
:meth:`~object.__getattribute__` methods or while creating the temporary
54+
:class:`str` object are silently ignored.
55+
For proper error handling, use :c:func:`PyObject_GetOptionalAttrString` or
56+
:c:func:`PyObject_GetAttrString` instead.
5757
5858
5959
.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
@@ -68,9 +68,9 @@ Object Protocol
6868
6969
.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
7070
71-
Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
72-
value on success, or ``NULL`` on failure. This is the equivalent of the Python
73-
expression ``o.attr_name``.
71+
This is the same as :c:func:`PyObject_GetAttr`, but *attr_name* is
72+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
73+
rather than a :c:expr:`PyObject*`.
7474
7575
If the missing attribute should not be treated as a failure, you can use
7676
:c:func:`PyObject_GetOptionalAttrString` instead.
@@ -93,15 +93,9 @@ Object Protocol
9393
9494
.. c:function:: int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result);
9595
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``.
96+
This is the same as :c:func:`PyObject_GetOptionalAttr`, but *attr_name* is
97+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
98+
rather than a :c:expr:`PyObject*`.
10599
106100
.. versionadded:: 3.13
107101
@@ -127,12 +121,11 @@ Object Protocol
127121
plans to remove it.
128122
129123
130-
.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
124+
.. c:function:: int PyObject_SetAttr(PyObject *o, const char *attr_name, PyObject *v)
131125
132-
Set the value of the attribute named *attr_name*, for object *o*, to the value
133-
*v*. Raise an exception and return ``-1`` on failure;
134-
return ``0`` on success. This is the equivalent of the Python statement
135-
``o.attr_name = v``.
126+
This is the same as :c:func:`PyObject_SetAttr`, but *attr_name* is
127+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
128+
rather than a :c:expr:`PyObject*`.
136129
137130
If *v* is ``NULL``, the attribute is deleted, but this feature is
138131
deprecated in favour of using :c:func:`PyObject_DelAttrString`.
@@ -158,8 +151,9 @@ Object Protocol
158151
159152
.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
160153
161-
Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
162-
This is the equivalent of the Python statement ``del o.attr_name``.
154+
This is the same as :c:func:`PyObject_DelAttr`, but *attr_name* is
155+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
156+
rather than a :c:expr:`PyObject*`.
163157
164158
165159
.. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context)

0 commit comments

Comments
 (0)