Skip to content

Commit 2ab0453

Browse files
committed
Merge branch 'main' into code-version
2 parents ba0b8f4 + 748c6c0 commit 2ab0453

File tree

315 files changed

+8491
-3985
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

315 files changed

+8491
-3985
lines changed

Doc/_static/og-image.png

14.2 KB
Loading

Doc/c-api/code.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,51 @@ bound into a function.
115115
the free variables. On error, ``NULL`` is returned and an exception is raised.
116116
117117
.. versionadded:: 3.11
118+
119+
.. c:function:: int PyCode_AddWatcher(PyCode_WatchCallback callback)
120+
121+
Register *callback* as a code object watcher for the current interpreter.
122+
Return an ID which may be passed to :c:func:`PyCode_ClearWatcher`.
123+
In case of error (e.g. no more watcher IDs available),
124+
return ``-1`` and set an exception.
125+
126+
.. versionadded:: 3.12
127+
128+
.. c:function:: int PyCode_ClearWatcher(int watcher_id)
129+
130+
Clear watcher identified by *watcher_id* previously returned from
131+
:c:func:`PyCode_AddWatcher` for the current interpreter.
132+
Return ``0`` on success, or ``-1`` and set an exception on error
133+
(e.g. if the given *watcher_id* was never registered.)
134+
135+
.. versionadded:: 3.12
136+
137+
.. c:type:: PyCodeEvent
138+
139+
Enumeration of possible code object watcher events:
140+
- ``PY_CODE_EVENT_CREATE``
141+
- ``PY_CODE_EVENT_DESTROY``
142+
143+
.. versionadded:: 3.12
144+
145+
.. c:type:: int (*PyCode_WatchCallback)(PyCodeEvent event, PyCodeObject* co)
146+
147+
Type of a code object watcher callback function.
148+
149+
If *event* is ``PY_CODE_EVENT_CREATE``, then the callback is invoked
150+
after `co` has been fully initialized. Otherwise, the callback is invoked
151+
before the destruction of *co* takes place, so the prior state of *co*
152+
can be inspected.
153+
154+
Users of this API should not rely on internal runtime implementation
155+
details. Such details may include, but are not limited to, the exact
156+
order and timing of creation and destruction of code objects. While
157+
changes in these details may result in differences observable by watchers
158+
(including whether a callback is invoked or not), it does not change
159+
the semantics of the Python code being executed.
160+
161+
If the callback returns with an exception set, it must return ``-1``; this
162+
exception will be printed as an unraisable exception using
163+
:c:func:`PyErr_WriteUnraisable`. Otherwise it should return ``0``.
164+
165+
.. versionadded:: 3.12

Doc/c-api/refcounting.rst

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
Reference Counting
88
******************
99

10-
The macros in this section are used for managing reference counts of Python
11-
objects.
10+
The functions and macros in this section are used for managing reference counts
11+
of Python objects.
1212

1313

1414
.. c:function:: Py_ssize_t Py_REFCNT(PyObject *o)
@@ -129,6 +129,11 @@ objects.
129129
It is a good idea to use this macro whenever decrementing the reference
130130
count of an object that might be traversed during garbage collection.
131131
132+
.. versionchanged:: 3.12
133+
The macro argument is now only evaluated once. If the argument has side
134+
effects, these are no longer duplicated.
135+
136+
132137
.. c:function:: void Py_IncRef(PyObject *o)
133138
134139
Increment the reference count for object *o*. A function version of :c:func:`Py_XINCREF`.
@@ -139,3 +144,40 @@ objects.
139144
140145
Decrement the reference count for object *o*. A function version of :c:func:`Py_XDECREF`.
141146
It can be used for runtime dynamic embedding of Python.
147+
148+
149+
.. c:macro:: Py_SETREF(dst, src)
150+
151+
Macro safely decrementing the `dst` reference count and setting `dst` to
152+
`src`.
153+
154+
As in case of :c:func:`Py_CLEAR`, "the obvious" code can be deadly::
155+
156+
Py_DECREF(dst);
157+
dst = src;
158+
159+
The safe way is::
160+
161+
Py_SETREF(dst, src);
162+
163+
That arranges to set `dst` to `src` _before_ decrementing reference count of
164+
*dst* old value, so that any code triggered as a side-effect of `dst`
165+
getting torn down no longer believes `dst` points to a valid object.
166+
167+
.. versionadded:: 3.6
168+
169+
.. versionchanged:: 3.12
170+
The macro arguments are now only evaluated once. If an argument has side
171+
effects, these are no longer duplicated.
172+
173+
174+
.. c:macro:: Py_XSETREF(dst, src)
175+
176+
Variant of :c:macro:`Py_SETREF` macro that uses :c:func:`Py_XDECREF` instead
177+
of :c:func:`Py_DECREF`.
178+
179+
.. versionadded:: 3.6
180+
181+
.. versionchanged:: 3.12
182+
The macro arguments are now only evaluated once. If an argument has side
183+
effects, these are no longer duplicated.

Doc/conf.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@
1313
# General configuration
1414
# ---------------------
1515

16-
extensions = ['sphinx.ext.coverage', 'sphinx.ext.doctest',
17-
'pyspecific', 'c_annotations', 'escape4chm',
18-
'asdl_highlight', 'peg_highlight', 'glossary_search']
16+
extensions = [
17+
'asdl_highlight',
18+
'c_annotations',
19+
'escape4chm',
20+
'glossary_search',
21+
'peg_highlight',
22+
'pyspecific',
23+
'sphinx.ext.coverage',
24+
'sphinx.ext.doctest',
25+
]
26+
27+
# Skip if downstream redistributors haven't installed it
28+
try:
29+
import sphinxext.opengraph
30+
except ImportError:
31+
pass
32+
else:
33+
extensions.append('sphinxext.opengraph')
34+
1935

2036
doctest_global_setup = '''
2137
try:
@@ -89,6 +105,14 @@
89105
# Short title used e.g. for <title> HTML tags.
90106
html_short_title = '%s Documentation' % release
91107

108+
# Deployment preview information, from Netlify
109+
# (See netlify.toml and https://docs.netlify.com/configure-builds/environment-variables/#git-metadata)
110+
html_context = {
111+
"is_deployment_preview": os.getenv("IS_DEPLOYMENT_PREVIEW"),
112+
"repository_url": os.getenv("REPOSITORY_URL"),
113+
"pr_id": os.getenv("REVIEW_ID")
114+
}
115+
92116
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
93117
# using the given strftime format.
94118
html_last_updated_fmt = '%b %d, %Y'
@@ -114,7 +138,7 @@
114138
html_use_opensearch = 'https://docs.python.org/' + version
115139

116140
# Additional static files.
117-
html_static_path = ['tools/static']
141+
html_static_path = ['_static', 'tools/static']
118142

119143
# Output file base name for HTML help builder.
120144
htmlhelp_basename = 'python' + release.replace('.', '')
@@ -238,3 +262,13 @@
238262
# Relative filename of the data files
239263
refcount_file = 'data/refcounts.dat'
240264
stable_abi_file = 'data/stable_abi.dat'
265+
266+
# sphinxext-opengraph config
267+
ogp_site_url = 'https://docs.python.org/3/'
268+
ogp_site_name = 'Python documentation'
269+
ogp_image = '_static/og-image.png'
270+
ogp_custom_meta_tags = [
271+
'<meta property="og:image:width" content="200">',
272+
'<meta property="og:image:height" content="200">',
273+
'<meta name="theme-color" content="#3776ab">',
274+
]

Doc/howto/enum.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ And a function to display the chores for a given day::
158158
... for chore, days in chores.items():
159159
... if day in days:
160160
... print(chore)
161+
...
161162
>>> show_chores(chores_for_ethan, Weekday.SATURDAY)
162163
answer SO questions
163164

@@ -459,6 +460,31 @@ sense to allow sharing some common behavior between a group of enumerations.
459460
(See `OrderedEnum`_ for an example.)
460461

461462

463+
.. _enum-dataclass-support:
464+
465+
Dataclass support
466+
-----------------
467+
468+
When inheriting from a :class:`~dataclasses.dataclass`,
469+
the :meth:`~Enum.__repr__` omits the inherited class' name. For example::
470+
471+
>>> @dataclass
472+
... class CreatureDataMixin:
473+
... size: str
474+
... legs: int
475+
... tail: bool = field(repr=False, default=True)
476+
...
477+
>>> class Creature(CreatureDataMixin, Enum):
478+
... BEETLE = 'small', 6
479+
... DOG = 'medium', 4
480+
...
481+
>>> Creature.DOG
482+
<Creature.DOG: size='medium', legs=4>
483+
484+
Use the :func:`!dataclass` argument ``repr=False``
485+
to use the standard :func:`repr`.
486+
487+
462488
Pickling
463489
--------
464490

@@ -687,6 +713,7 @@ It is also possible to name the combinations::
687713
... W = 2
688714
... X = 1
689715
... RWX = 7
716+
...
690717
>>> Perm.RWX
691718
<Perm.RWX: 7>
692719
>>> ~Perm.RWX

Doc/library/argparse.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ arguments they contain. For example::
565565

566566
>>> with open('args.txt', 'w', encoding=sys.getfilesystemencoding()) as fp:
567567
... fp.write('-f\nbar')
568+
...
568569
>>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
569570
>>> parser.add_argument('-f')
570571
>>> parser.parse_args(['-f', 'foo', '@args.txt'])

Doc/library/asyncio-eventloop.rst

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ an event loop:
3333

3434
Return the running event loop in the current OS thread.
3535

36-
If there is no running event loop a :exc:`RuntimeError` is raised.
36+
Raise a :exc:`RuntimeError` if there is no running event loop.
37+
3738
This function can only be called from a coroutine or a callback.
3839

3940
.. versionadded:: 3.7
@@ -42,27 +43,31 @@ an event loop:
4243

4344
Get the current event loop.
4445

45-
If there is no current event loop set in the current OS thread,
46-
the OS thread is main, and :func:`set_event_loop` has not yet
47-
been called, asyncio will create a new event loop and set it as the
48-
current one.
46+
When called from a coroutine or a callback (e.g. scheduled with
47+
call_soon or similar API), this function will always return the
48+
running event loop.
49+
50+
If there is no running event loop set, the function will return
51+
the result of calling ``get_event_loop_policy().get_event_loop()``.
4952

5053
Because this function has rather complex behavior (especially
5154
when custom event loop policies are in use), using the
5255
:func:`get_running_loop` function is preferred to :func:`get_event_loop`
5356
in coroutines and callbacks.
5457

55-
Consider also using the :func:`asyncio.run` function instead of using
56-
lower level functions to manually create and close an event loop.
58+
As noted above, consider using the higher-level :func:`asyncio.run` function,
59+
instead of using these lower level functions to manually create and close an
60+
event loop.
5761

58-
.. deprecated:: 3.10
59-
Deprecation warning is emitted if there is no running event loop.
60-
In future Python releases, this function will be an alias of
61-
:func:`get_running_loop`.
62+
.. note::
63+
In Python versions 3.10.0--3.10.8 and 3.11.0 this function
64+
(and other functions which used it implicitly) emitted a
65+
:exc:`DeprecationWarning` if there was no running event loop, even if
66+
the current loop was set.
6267

6368
.. function:: set_event_loop(loop)
6469

65-
Set *loop* as a current event loop for the current OS thread.
70+
Set *loop* as the current event loop for the current OS thread.
6671

6772
.. function:: new_event_loop()
6873

Doc/library/asyncio-llapi-index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Obtaining the Event Loop
1919
- The **preferred** function to get the running event loop.
2020

2121
* - :func:`asyncio.get_event_loop`
22-
- Get an event loop instance (current or via the policy).
22+
- Get an event loop instance (running or current via the current policy).
2323

2424
* - :func:`asyncio.set_event_loop`
2525
- Set the event loop as current via the current policy.

Doc/library/asyncio-policy.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ asyncio ships with the following built-in policies:
116116

117117
On Windows, :class:`ProactorEventLoop` is now used by default.
118118

119+
.. versionchanged:: 3.12
120+
:meth:`get_event_loop` now raises a :exc:`RuntimeError` if there is no
121+
current event loop set.
122+
119123

120124
.. class:: WindowsSelectorEventLoopPolicy
121125

Doc/library/base64.rst

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,23 @@ The modern interface provides:
5353
Encode the :term:`bytes-like object` *s* using Base64 and return the encoded
5454
:class:`bytes`.
5555

56-
Optional *altchars* must be a :term:`bytes-like object` of at least
57-
length 2 (additional characters are ignored) which specifies an alternative
58-
alphabet for the ``+`` and ``/`` characters. This allows an application to e.g.
59-
generate URL or filesystem safe Base64 strings. The default is ``None``, for
60-
which the standard Base64 alphabet is used.
56+
Optional *altchars* must be a :term:`bytes-like object` of length 2 which
57+
specifies an alternative alphabet for the ``+`` and ``/`` characters.
58+
This allows an application to e.g. generate URL or filesystem safe Base64
59+
strings. The default is ``None``, for which the standard Base64 alphabet is used.
60+
61+
May assert or raise a a :exc:`ValueError` if the length of *altchars* is not 2. Raises a
62+
:exc:`TypeError` if *altchars* is not a :term:`bytes-like object`.
6163

6264

6365
.. function:: b64decode(s, altchars=None, validate=False)
6466

6567
Decode the Base64 encoded :term:`bytes-like object` or ASCII string
6668
*s* and return the decoded :class:`bytes`.
6769

68-
Optional *altchars* must be a :term:`bytes-like object` or ASCII string of
69-
at least length 2 (additional characters are ignored) which specifies the
70-
alternative alphabet used instead of the ``+`` and ``/`` characters.
70+
Optional *altchars* must be a :term:`bytes-like object` or ASCII string
71+
of length 2 which specifies the alternative alphabet used instead of the
72+
``+`` and ``/`` characters.
7173

7274
A :exc:`binascii.Error` exception is raised
7375
if *s* is incorrectly padded.
@@ -80,6 +82,7 @@ The modern interface provides:
8082

8183
For more information about the strict base64 check, see :func:`binascii.a2b_base64`
8284

85+
May assert or raise a :exc:`ValueError` if the length of *altchars* is not 2.
8386

8487
.. function:: standard_b64encode(s)
8588

Doc/library/bz2.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,11 @@ Writing and reading a bzip2-compressed file in binary mode:
320320
>>> with bz2.open("myfile.bz2", "wb") as f:
321321
... # Write compressed data to file
322322
... unused = f.write(data)
323+
...
323324
>>> with bz2.open("myfile.bz2", "rb") as f:
324325
... # Decompress data from file
325326
... content = f.read()
327+
...
326328
>>> content == data # Check equality to original object after round-trip
327329
True
328330

Doc/library/collections.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ For example::
229229
>>> cnt = Counter()
230230
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
231231
... cnt[word] += 1
232+
...
232233
>>> cnt
233234
Counter({'blue': 3, 'red': 2, 'green': 1})
234235

@@ -818,6 +819,7 @@ zero):
818819

819820
>>> def constant_factory(value):
820821
... return lambda: value
822+
...
821823
>>> d = defaultdict(constant_factory('<missing>'))
822824
>>> d.update(name='John', action='ran')
823825
>>> '%(name)s %(action)s to %(object)s' % d

Doc/library/dataclasses.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ Module contents
7979
class C:
8080
...
8181

82-
@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)
82+
@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False,
83+
match_args=True, kw_only=False, slots=False, weakref_slot=False)
8384
class C:
8485
...
8586

Doc/library/datetime.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ Example of counting days to an event::
765765
>>> my_birthday = date(today.year, 6, 24)
766766
>>> if my_birthday < today:
767767
... my_birthday = my_birthday.replace(year=today.year + 1)
768+
...
768769
>>> my_birthday
769770
datetime.date(2008, 6, 24)
770771
>>> time_to_birthday = abs(my_birthday - today)

0 commit comments

Comments
 (0)