Skip to content

Commit 8fba904

Browse files
committed
Merge remote-tracking branch 'upstream/main' into pr-148445
Co-authored-by: Muse Code (Meta Muse Spark)
2 parents b1157ba + 81518ce commit 8fba904

91 files changed

Lines changed: 2199 additions & 349 deletions

File tree

Some content is hidden

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

‎Doc/library/ast.rst‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ Node classes
4848
module and re-exported in :mod:`!ast`.
4949

5050
There is one class defined for each left-hand side symbol in the abstract
51-
grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition,
51+
grammar (for example, :class:`!ast.stmt` or :class:`!ast.expr`). In addition,
5252
there is one class defined for each constructor on the right-hand side; these
5353
classes inherit from the classes for the left-hand side trees. For example,
54-
:class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules
54+
:class:`ast.BinOp` inherits from :class:`!ast.expr`. For production rules
5555
with alternatives (aka "sums"), the left-hand side class is abstract: only
5656
instances of specific constructor nodes are ever created.
5757

@@ -65,7 +65,7 @@ Node classes
6565

6666
Each instance of a concrete class has one attribute for each child node,
6767
of the type as defined in the grammar. For example, :class:`ast.BinOp`
68-
instances have an attribute :attr:`left` of type :class:`ast.expr`.
68+
instances have an attribute :attr:`!left` of type :class:`!ast.expr`.
6969

7070
If these attributes are marked as optional in the grammar (using a
7171
question mark), the value might be ``None``. If the attributes can have
@@ -104,10 +104,11 @@ Node classes
104104
one can get the source segment of a one-line expression node using
105105
``source_line[node.col_offset : node.end_col_offset]``.
106106

107-
The constructor of a class :class:`ast.T` parses its arguments as follows:
107+
The constructors of AST node classes parse their arguments as follows:
108108

109109
* If there are positional arguments, there must be as many as there are items
110-
in :attr:`T._fields`; they will be assigned as attributes of these names.
110+
in :attr:`!_fields`; they will be assigned to attributes with
111+
the corresponding names.
111112
* If there are keyword arguments, they will set the attributes of the same
112113
names to the given values.
113114

@@ -2223,8 +2224,8 @@ Async and await
22232224

22242225
.. note::
22252226
When a string is parsed by :func:`ast.parse`, operator nodes (subclasses
2226-
of :class:`ast.operator`, :class:`ast.unaryop`, :class:`ast.cmpop`,
2227-
:class:`ast.boolop` and :class:`ast.expr_context`) on the returned tree
2227+
of :class:`!ast.operator`, :class:`!ast.unaryop`, :class:`!ast.cmpop`,
2228+
:class:`!ast.boolop` and :class:`!ast.expr_context`) on the returned tree
22282229
will be singletons. Changes to one will be reflected in all other
22292230
occurrences of the same value (for example, :class:`ast.Add`).
22302231

‎Doc/library/datetime.rst‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,10 @@ convenience.
26992699
| | number, zero-padded to 6 | 999999 | |
27002700
| | digits. | | |
27012701
+-----------+--------------------------------+------------------------+-------+
2702+
| ``%Nf`` | Fraction of a second truncated | 000, 001, ..., 999 | \(5) |
2703+
| | to *N* digits, where *N* is | (``%3f``) | |
2704+
| | from 1 to 6. | | |
2705+
+-----------+--------------------------------+------------------------+-------+
27022706
| ``%:z`` | UTC offset in the form | (empty), +00:00, | \(6) |
27032707
| | ``±HH:MM[:SS[.ffffff]]`` | -04:00, +10:30, | |
27042708
| | (empty string if the object is | +06:34:15, | |
@@ -2721,6 +2725,9 @@ differences between platforms in handling of unsupported format specifiers.
27212725
``%D``, ``%F``, ``%n``, ``%t``, and ``%:z`` were added for
27222726
:meth:`~.datetime.strptime`.
27232727

2728+
.. versionadded:: next
2729+
``%Nf`` was added.
2730+
27242731

27252732
Technical detail
27262733
^^^^^^^^^^^^^^^^
@@ -2833,7 +2840,8 @@ Notes:
28332840

28342841
(5)
28352842
When used with the :meth:`~.datetime.strptime` method, the ``%f`` directive
2836-
accepts from one to six digits and zero pads on the right. ``%f`` is
2843+
accepts from one to six digits and zero pads on the right, while ``%Nf``
2844+
accepts exactly *N* digits. ``%f`` is
28372845
an extension to the set of format characters in the C standard (but
28382846
implemented separately in datetime objects, and therefore always
28392847
available).

‎Doc/library/pickle.rst‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,11 @@ The following types can be pickled:
517517
* classes accessible from the top level of a module;
518518

519519
* instances of such classes for which the result of calling :meth:`~object.__getstate__`
520-
is picklable (see section :ref:`pickle-inst` for details).
520+
is picklable (see section :ref:`pickle-inst` for details);
521+
522+
* weak references and the weak containers from the :mod:`weakref` module,
523+
provided that the weakly referenced objects are strongly referenced
524+
elsewhere in the same pickle (see :class:`weakref.ref`).
521525

522526
Attempts to pickle unpicklable objects will raise the :exc:`PicklingError`
523527
exception; when this happens, an unspecified number of bytes may have already

‎Doc/library/weakref.rst‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ See :ref:`__slots__ documentation <slots>` for details.
126126
Weak references are :ref:`generic <generics>` over the type of the object they
127127
reference.
128128

129+
A weak reference can be pickled
130+
if the referent is strongly referenced elsewhere in the same pickle;
131+
otherwise pickling raises :exc:`pickle.PicklingError`.
132+
The unpickled weak reference refers to the unpickled referent.
133+
A reference to a dead object unpickles as a reference to a dead object.
134+
The *callback* is not preserved.
135+
136+
Weak references are atomic for the :mod:`copy` module:
137+
:func:`copy.copy` and :func:`copy.deepcopy` return
138+
the original weak reference without copying the referent.
139+
129140
.. attribute:: __callback__
130141

131142
This read-only attribute returns the callback currently associated to the
@@ -138,6 +149,9 @@ See :ref:`__slots__ documentation <slots>` for details.
138149
.. versionchanged:: 3.16
139150
Raise :exc:`!TypeError` if *callback* is not callable or ``None``.
140151

152+
.. versionchanged:: next
153+
Weak references can now be pickled.
154+
141155

142156
.. function:: proxy(object[, callback])
143157

@@ -203,9 +217,16 @@ See :ref:`__slots__ documentation <slots>` for details.
203217
>>> d[k2] = 2 # d = {k2: 2}
204218
>>> del k1 # d = {k2: 2}
205219

220+
A :class:`!WeakKeyDictionary` can be copied,
221+
and pickled if the keys are strongly referenced
222+
elsewhere in the same pickle (see :class:`ref`).
223+
206224
.. versionchanged:: 3.9
207225
Added support for ``|`` and ``|=`` operators, as specified in :pep:`584`.
208226

227+
.. versionchanged:: next
228+
:class:`!WeakKeyDictionary` can now be pickled.
229+
209230
:class:`WeakKeyDictionary` objects have an additional method that
210231
exposes the internal references directly. The references are not guaranteed to
211232
be "live" at the time they are used, so the result of calling the references
@@ -224,9 +245,16 @@ than needed.
224245
Mapping class that references values weakly. Entries in the dictionary will be
225246
discarded when no strong reference to the value exists any more.
226247

248+
A :class:`!WeakValueDictionary` can be copied,
249+
and pickled if the values are strongly referenced
250+
elsewhere in the same pickle (see :class:`ref`).
251+
227252
.. versionchanged:: 3.9
228253
Added support for ``|`` and ``|=`` operators, as specified in :pep:`584`.
229254

255+
.. versionchanged:: next
256+
:class:`!WeakValueDictionary` can now be pickled.
257+
230258
:class:`WeakValueDictionary` objects have the following additional methods.
231259
These have the same issues as the :meth:`WeakKeyDictionary.keyrefs` method.
232260

@@ -246,6 +274,14 @@ These have the same issues as the :meth:`WeakKeyDictionary.keyrefs` method.
246274
Set class that keeps weak references to its elements. An element will be
247275
discarded when no strong reference to it exists any more.
248276

277+
A :class:`!WeakSet` can be pickled if the elements are strongly
278+
referenced elsewhere in the same pickle (see :class:`ref`).
279+
A copy or a deep copy shares the elements with the original;
280+
user-defined attributes are copied or deep-copied.
281+
282+
.. versionchanged:: next
283+
:class:`!WeakSet` can now be pickled and copied.
284+
249285

250286
.. class:: WeakMethod(method[, callback])
251287

@@ -275,8 +311,16 @@ These have the same issues as the :meth:`WeakKeyDictionary.keyrefs` method.
275311

276312
*callback* is the same as the parameter of the same name to the :func:`ref` function.
277313

314+
A :class:`!WeakMethod` can be copied,
315+
and pickled if the object the method is bound to is strongly referenced
316+
elsewhere in the same pickle (see :class:`ref`).
317+
A copy or a deep copy is an equal weak method to the same object.
318+
278319
.. versionadded:: 3.4
279320

321+
.. versionchanged:: next
322+
:class:`!WeakMethod` can now be pickled and copied.
323+
280324
.. class:: finalize(obj, func, /, *args, **kwargs)
281325

282326
Return a callable finalizer object which will be called when *obj*

‎Doc/whatsnew/3.16.rst‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,16 @@ curses
362362
(Contributed by Serhiy Storchaka in :gh:`133031`.)
363363

364364

365+
datetime
366+
--------
367+
368+
* Add the ``%Nf`` format code (where *N* is from 1 to 6) to
369+
:meth:`datetime.datetime.strftime`, :meth:`datetime.time.strftime`,
370+
:meth:`datetime.date.strftime`, :meth:`datetime.datetime.strptime` and
371+
:meth:`datetime.time.strptime`.
372+
(Contributed by Stan Ulbrych and Karoline Pauls in :gh:`157290`.)
373+
374+
365375
dbm.gnu
366376
-------
367377

@@ -576,6 +586,17 @@ pydoc
576586
(Contributed by Serhiy Storchaka in :gh:`153906`.)
577587

578588

589+
pickle
590+
------
591+
592+
* Weak references (:class:`weakref.ref`), :class:`weakref.WeakMethod`,
593+
:class:`weakref.WeakSet`, :class:`weakref.WeakValueDictionary` and
594+
:class:`weakref.WeakKeyDictionary` can now be pickled if the weakly
595+
referenced objects are strongly referenced elsewhere in the same pickle;
596+
otherwise pickling raises :exc:`pickle.PicklingError`.
597+
:class:`!WeakMethod` and :class:`!WeakSet` can now also be copied.
598+
(Contributed by Serhiy Storchaka in :gh:`74876`.)
599+
579600
re
580601
--
581602

‎Lib/_pydatetime.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ def _wrap_strftime(object, format, timetuple):
237237
freplace = '%06d' % getattr(object,
238238
'microsecond', 0)
239239
newformat.append(freplace)
240+
elif ch in '123456' and format[i:i+1] == 'f':
241+
i += 1
242+
if freplace is None:
243+
freplace = '%06d' % getattr(object,
244+
'microsecond', 0)
245+
precision = int(ch)
246+
newformat.append(freplace[:precision])
240247
elif ch == 'z':
241248
if zreplace is None:
242249
if hasattr(object, "utcoffset"):

‎Lib/_pyrepl/_module_completer.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ def get_completions(
8383
if not result:
8484
return None
8585
try:
86-
return self.complete(*result, include_values=include_values)
86+
names, values, action = self.complete(
87+
*result, include_values=include_values
88+
)
89+
names = [self._remove_separated_words(line, c) for c in names]
90+
return names, values, action
8791
except Exception:
8892
# Some unexpected error occurred, make it look like
8993
# no completions are available
@@ -343,6 +347,26 @@ def _do_import() -> str | None:
343347

344348
return (prompt, _do_import)
345349

350+
def _remove_separated_words(self, line: str, completion: str) -> str:
351+
"""Remove completion parts imputed as separate words.
352+
353+
Needed because the completer insert any completion provided
354+
by replacing the stem (eg. word) currently imputed, if any.
355+
356+
Examples:
357+
- 'import foo.', 'foo.bar' -> 'foo.bar'
358+
- 'import foo .', 'foo.bar' -> '.bar'
359+
- 'from foo import ', 'bar' -> 'bar'
360+
- 'import x.x .x.', 'x.x.x.x' -> '.x.x'
361+
"""
362+
last_word = line.split(" ")[-1]
363+
if not last_word:
364+
return completion
365+
index = completion.rfind(last_word)
366+
if index > 0:
367+
return completion[index:]
368+
return completion
369+
346370

347371
class ImportParser:
348372
"""

‎Lib/_strptime.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ def __init__(self, locale_time=None):
386386
't': r'\s*',
387387
'%': '%',
388388
}
389+
for n in range(1, 7):
390+
mapping[f'{n}f'] = rf"(?P<f>[0-9]{{{n}}})"
389391
if self.locale_time.LC_alt_digits is None:
390392
for d in 'dmyCHIMS':
391393
mapping['O' + d] = r'(?P<%s>\d\d|\d| \d)' % d

‎Lib/_weakrefset.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010

1111
class WeakSet:
12+
# Slots give __getstate__() a uniform (dict, slots) shape for __reduce__.
13+
# __dict__ carries user attributes; add() needs __weakref__.
14+
__slots__ = '__dict__', '__weakref__', 'data', '_remove'
15+
1216
def __init__(self, data=None):
1317
self.data = set()
1418

@@ -40,7 +44,14 @@ def __contains__(self, item):
4044
return wr in self.data
4145

4246
def __reduce__(self):
43-
return self.__class__, (list(self),), self.__getstate__()
47+
# Pickle the elements as weak references; data and _remove are
48+
# rebuilt by the reconstructor. Copying shares the elements.
49+
dict, slots = self.__getstate__()
50+
for name in ('data', '_remove'):
51+
slots.pop(name, None)
52+
state = (dict, slots) if dict or slots else None
53+
return (_restore_weakset, (self.__class__, [ref(item) for item in self]),
54+
state)
4455

4556
def add(self, item):
4657
self.data.add(ref(item, self._remove))
@@ -145,3 +156,10 @@ def __repr__(self):
145156
return repr(self.data)
146157

147158
__class_getitem__ = classmethod(GenericAlias)
159+
160+
161+
def _restore_weakset(cls, refs):
162+
self = cls.__new__(cls)
163+
WeakSet.__init__(self)
164+
self.update(item for r in refs for item in [r()] if item is not None)
165+
return self

‎Lib/bdb.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Debugger basics"""
22

3-
import fnmatch
4-
import sys
5-
import threading
63
import os
7-
import weakref
4+
import sys
85
from contextlib import contextmanager
96
from inspect import CO_GENERATOR, CO_COROUTINE, CO_ASYNC_GENERATOR
7+
lazy import fnmatch
8+
lazy import threading
9+
lazy import weakref
1010

1111
__all__ = ["BdbQuit", "Bdb", "Breakpoint"]
1212

@@ -601,6 +601,13 @@ def set_trace(self, frame=None):
601601
602602
If frame is not specified, debugging starts from caller's frame.
603603
"""
604+
605+
if self.enterframe is not None:
606+
# If self.enterframe is not None, it means we are already in
607+
# dispatched functions. Tracing is disabled when calling a
608+
# trace function, so we just skip this set_trace() call.
609+
return
610+
604611
self.stop_trace()
605612
if frame is None:
606613
frame = sys._getframe().f_back

0 commit comments

Comments
 (0)