Skip to content

Commit 8fe7cfa

Browse files
Docs: correct API guidance and examples (GH-160907)
(cherry picked from commit bd3be61) Co-authored-by: Inada Naoki <songofacandy@gmail.com>
1 parent da6c5d9 commit 8fe7cfa

16 files changed

Lines changed: 42 additions & 39 deletions

‎Doc/builtins/functions.rst‎

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

17941792
This class has a custom representation that can be evaluated::
17951793

‎Doc/builtins/stdtypes.rst‎

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

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

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

37863786

37873787
This is equivalent to:
@@ -5593,9 +5593,8 @@ Note, the non-operator versions of the :meth:`~set.update`,
55935593
argument.
55945594

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

56005599
Sets and frozensets are :ref:`generic <generics>` over the type of their elements.
56015600

‎Doc/c-api/code.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ may change without deprecation warnings.
315315
316316
Return a new opaque index value used to adding data to code objects.
317317
318-
You generally call this function once (per interpreter) and use the result
319-
with ``PyCode_GetExtra`` and ``PyCode_SetExtra`` to manipulate
320-
data on individual code objects.
318+
You generally call this function once (per interpreter). Use the result with
319+
:c:func:`PyUnstable_Code_GetExtra` and :c:func:`PyUnstable_Code_SetExtra`
320+
to manipulate data on individual code objects.
321321
322322
If *free* is not ``NULL``: when a code object is deallocated,
323323
*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/extension-modules.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ For example, a module called ``spam`` would be defined like this::
8686
static PySlot spam_slots[] = {
8787
PySlot_STATIC_DATA(Py_mod_abi, &abi_info),
8888
PySlot_STATIC_DATA(Py_mod_name, "spam"),
89-
PySlot_FUNC(Py_mod_init, spam_init_function),
89+
PySlot_FUNC(Py_mod_exec, spam_init_function),
9090
...
9191
PySlot_END
9292
};

‎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
@@ -334,6 +334,8 @@ event code, you might use the following code::
334334
PyObject *arglist;
335335
...
336336
arglist = Py_BuildValue("(l)", eventcode);
337+
if (arglist == NULL)
338+
return NULL;
337339
result = PyObject_CallObject(my_callback, arglist);
338340
Py_DECREF(arglist);
339341
if (result == NULL)
@@ -342,17 +344,24 @@ event code, you might use the following code::
342344
Py_DECREF(result);
343345

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

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

352-
PyObject *dict;
353+
PyObject *arglist, *dict;
353354
...
355+
arglist = PyTuple_New(0);
356+
if (arglist == NULL)
357+
return NULL;
354358
dict = Py_BuildValue("{s:i}", "name", val);
355-
result = PyObject_Call(my_callback, NULL, dict);
359+
if (dict == NULL) {
360+
Py_DECREF(arglist);
361+
return NULL;
362+
}
363+
result = PyObject_Call(my_callback, arglist, dict);
364+
Py_DECREF(arglist);
356365
Py_DECREF(dict);
357366
if (result == NULL)
358367
return NULL; /* Pass error back */

‎Doc/extending/first-extension-module.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ Add this array just below the ``spam_system`` function:
378378
As with module slots, a zero-filled sentinel marks the end of the array.
379379

380380
Next, we'll add the method to the module.
381-
Add a :c:data:`Py_mod_methods` slot to your :c:type:`PyMethodDef` array:
381+
Add a :c:data:`Py_mod_methods` slot to your :c:type:`PySlot` array:
382382

383383
.. literalinclude:: ../includes/capi-extension/spammodule-01.c
384384
:start-after: /// Module slot table
@@ -518,7 +518,7 @@ Add an ``if`` block for this:
518518
static PyObject *
519519
spam_system(PyObject *self, PyObject *arg)
520520
{
521-
const char *command = PyUnicode_AsUTF8AndSize(arg);
521+
const char *command = PyUnicode_AsUTF8AndSize(arg, NULL);
522522
if (command == NULL) {
523523
return NULL;
524524
}
@@ -548,7 +548,7 @@ the ``char *`` buffer, and using its result instead of the ``3``:
548548
static PyObject *
549549
spam_system(PyObject *self, PyObject *arg)
550550
{
551-
const char *command = PyUnicode_AsUTF8AndSize(arg);
551+
const char *command = PyUnicode_AsUTF8AndSize(arg, NULL);
552552
if (command == NULL) {
553553
return NULL;
554554
}

‎Doc/extending/newtypes_tutorial.rst‎

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

899899
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
@@ -777,7 +777,7 @@ returned::
777777
import urllib.request
778778
from http.cookiejar import CookieJar, DefaultCookiePolicy
779779
policy = DefaultCookiePolicy(
780-
rfc2965=True, strict_ns_domain=Policy.DomainStrict,
780+
rfc2965=True, strict_ns_domain=DefaultCookiePolicy.DomainStrict,
781781
blocked_domains=["ads.net", ".ads.net"])
782782
cj = CookieJar(policy)
783783
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

0 commit comments

Comments
 (0)