Skip to content

Commit 4782565

Browse files
authored
Doc: correct API guidance and examples (GH-160907)
(cherry picked from commit bd3be61)
1 parent 29f7b69 commit 4782565

14 files changed

Lines changed: 38 additions & 35 deletions

File tree

‎Doc/builtins/functions.rst‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,8 +1775,6 @@ are always available. They are listed here in alphabetical order.
17751775
including the name and address of the object. A class can control what this
17761776
function returns for its instances
17771777
by defining a :meth:`~object.__repr__` method.
1778-
If :func:`sys.displayhook` is not accessible, this function will raise
1779-
:exc:`RuntimeError`.
17801778

17811779
This class has a custom representation that can be evaluated::
17821780

‎Doc/builtins/stdtypes.rst‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,8 +3497,8 @@ objects.
34973497

34983498
If the :class:`bytearray` needs to shrink, bytes beyond *size* are truncated.
34993499

3500-
If the :class:`bytearray` needs to grow, all new bytes, those beyond *size*,
3501-
will be set to null bytes.
3500+
If the :class:`bytearray` needs to grow, all new bytes will be set to null
3501+
bytes.
35023502

35033503

35043504
This is equivalent to:
@@ -5282,9 +5282,8 @@ Note, the non-operator versions of the :meth:`~set.update`,
52825282
argument.
52835283

52845284
Note, the *elem* argument to the :meth:`~object.__contains__`,
5285-
:meth:`~set.remove`, and
5286-
:meth:`~set.discard` methods may be a set. To support searching for an equivalent
5287-
frozenset, a temporary one is created from *elem*.
5285+
:meth:`~set.remove`, and :meth:`~set.discard` methods may be a set. In this case,
5286+
an equivalent frozenset is searched for.
52885287

52895288
.. seealso::
52905289

‎Doc/c-api/code.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ may change without deprecation warnings.
317317
318318
Return a new opaque index value used to adding data to code objects.
319319
320-
You generally call this function once (per interpreter) and use the result
321-
with ``PyCode_GetExtra`` and ``PyCode_SetExtra`` to manipulate
322-
data on individual code objects.
320+
You generally call this function once (per interpreter). Use the result with
321+
:c:func:`PyUnstable_Code_GetExtra` and :c:func:`PyUnstable_Code_SetExtra`
322+
to manipulate data on individual code objects.
323323
324324
If *free* is not ``NULL``: when a code object is deallocated,
325325
*free* will be called on non-``NULL`` data stored under the new index.

‎Doc/c-api/codec.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Codec lookup API
4747
4848
In the following functions, the *encoding* string is looked up converted to all
4949
lower-case characters, which makes encodings looked up through this mechanism
50-
effectively case-insensitive. If no codec is found, a :exc:`KeyError` is set
50+
effectively case-insensitive. If no codec is found, a :exc:`LookupError` is set
5151
and ``NULL`` returned.
5252
5353
.. c:function:: PyObject* PyCodec_Encoder(const char *encoding)

‎Doc/c-api/gcsupport.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ rules:
127127
128128
.. c:macro:: PyObject_GC_Resize(TYPE, op, newsize)
129129
130-
Resize an object allocated by :c:macro:`PyObject_NewVar`.
130+
Resize an object allocated by :c:macro:`PyObject_GC_NewVar`.
131131
Returns the resized object of type ``TYPE*`` (refers to any C type)
132132
or ``NULL`` on failure.
133133

‎Doc/extending/extending.rst‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ event code, you might use the following code::
648648
PyObject *arglist;
649649
...
650650
arglist = Py_BuildValue("(l)", eventcode);
651+
if (arglist == NULL)
652+
return NULL;
651653
result = PyObject_CallObject(my_callback, arglist);
652654
Py_DECREF(arglist);
653655
if (result == NULL)
@@ -656,17 +658,24 @@ event code, you might use the following code::
656658
Py_DECREF(result);
657659

658660
Note the placement of ``Py_DECREF(arglist)`` immediately after the call, before
659-
the error check! Also note that strictly speaking this code is not complete:
660-
:c:func:`Py_BuildValue` may run out of memory, and this should be checked.
661+
the error check!
661662

662663
You may also call a function with keyword arguments by using
663664
:c:func:`PyObject_Call`, which supports arguments and keyword arguments. As in
664665
the above example, we use :c:func:`Py_BuildValue` to construct the dictionary. ::
665666

666-
PyObject *dict;
667+
PyObject *arglist, *dict;
667668
...
669+
arglist = PyTuple_New(0);
670+
if (arglist == NULL)
671+
return NULL;
668672
dict = Py_BuildValue("{s:i}", "name", val);
669-
result = PyObject_Call(my_callback, NULL, dict);
673+
if (dict == NULL) {
674+
Py_DECREF(arglist);
675+
return NULL;
676+
}
677+
result = PyObject_Call(my_callback, arglist, dict);
678+
Py_DECREF(arglist);
670679
Py_DECREF(dict);
671680
if (result == NULL)
672681
return NULL; /* Pass error back */

‎Doc/extending/newtypes_tutorial.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ function::
898898
Before calling :c:func:`PyType_Ready`, the type structure must have the
899899
:c:member:`~PyTypeObject.tp_base` slot filled in. When we are deriving an
900900
existing type, it is not necessary to fill out the :c:member:`~PyTypeObject.tp_alloc`
901-
slot with :c:func:`PyType_GenericNew` -- the allocation function from the base
901+
slot with :c:func:`PyType_GenericAlloc` -- the allocation function from the base
902902
type will be inherited.
903903

904904
After that, calling :c:func:`PyType_Ready` and adding the type object to the

‎Doc/library/http.cookiejar.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ returned::
780780
import urllib.request
781781
from http.cookiejar import CookieJar, DefaultCookiePolicy
782782
policy = DefaultCookiePolicy(
783-
rfc2965=True, strict_ns_domain=Policy.DomainStrict,
783+
rfc2965=True, strict_ns_domain=DefaultCookiePolicy.DomainStrict,
784784
blocked_domains=["ads.net", ".ads.net"])
785785
cj = CookieJar(policy)
786786
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

‎Doc/library/mailbox.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,6 @@ due to malformed messages in the mailbox::
17421742
inbox.unlock()
17431743
break # Found destination, so stop looking.
17441744

1745-
for box in boxes.itervalues():
1745+
for box in boxes.values():
17461746
box.close()
17471747

‎Doc/library/os.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,8 @@ as internal buffering of data.
12441244
* :const:`SEEK_SET` or ``0`` -- set *pos* relative to the beginning of the file
12451245
* :const:`SEEK_CUR` or ``1`` -- set *pos* relative to the current file position
12461246
* :const:`SEEK_END` or ``2`` -- set *pos* relative to the end of the file
1247-
* :const:`SEEK_HOLE` -- set *pos* to the next data location, relative to *pos*
1248-
* :const:`SEEK_DATA` -- set *pos* to the next data hole, relative to *pos*
1247+
* :const:`SEEK_DATA` -- set *pos* to the next data location, relative to *pos*
1248+
* :const:`SEEK_HOLE` -- set *pos* to the next data hole, relative to *pos*
12491249

12501250
.. versionchanged:: 3.3
12511251

0 commit comments

Comments
 (0)