@@ -355,8 +355,8 @@ It is meant to formalize existing optimizations which were already done
355
355
for various classes.
356
356
Any extension type implementing a callable can use this protocol.
357
357
358
- This is currently provisional,
359
- the aim is to make it fully public in Python 3.9.
358
+ This is currently provisional.
359
+ The aim is to make it fully public in Python 3.9.
360
360
361
361
See :pep: `590 ` for a full description.
362
362
@@ -445,7 +445,7 @@ Other Language Changes
445
445
an instance of the subclass, rather than the base class. This also affects
446
446
the return type of operations whose implementation (directly or indirectly)
447
447
uses :class: `datetime.timedelta ` arithmetic, such as
448
- :meth: `datetime.datetime.astimezone `.
448
+ :meth: `~ datetime.datetime.astimezone `.
449
449
(Contributed by Paul Ganssle in :issue: `32417 `.)
450
450
451
451
* When the Python interpreter is interrupted by Ctrl-C (SIGINT) and the
@@ -529,6 +529,13 @@ Other Language Changes
529
529
530
530
(Contributed by Jörn Heissler in :issue: `35224 `.)
531
531
532
+ * The :meth: `object.__reduce__ ` method can now return a tuple from two to
533
+ six elements long. Formerly, five was the limit. The new, optional sixth
534
+ element is a callable with a ``(obj, state) `` signature. This allows the
535
+ direct control over the state-updating behavior of a specific object. If
536
+ not *None *, this callable will have priority over the object's
537
+ :meth: `~__setstate__ ` method.
538
+ (Contributed by Pierre Glaser and Olivier Grisel in :issue: `35900 `.)
532
539
533
540
New Modules
534
541
===========
@@ -579,8 +586,8 @@ The :func:`ast.parse` function has some new flags:
579
586
comments" (returned for function definition AST nodes);
580
587
581
588
* ``feature_version=(3, N) `` allows specifying an earlier Python 3
582
- version. ( For example, ``feature_version=(3, 4) `` will treat
583
- `` async `` and `` await `` as non-reserved words.)
589
+ version. For example, ``feature_version=(3, 4) `` will treat
590
+ :keyword: ` async ` and :keyword: ` await ` as non-reserved words.
584
591
585
592
(Contributed by Guido van Rossum in :issue: `35766 `.)
586
593
@@ -630,14 +637,39 @@ marked with the ``CO_COROUTINE`` flag may then be returned.
630
637
collections
631
638
-----------
632
639
633
- The :meth: `_asdict() ` method for :func: `collections.namedtuple ` now returns
634
- a :class: `dict ` instead of a :class: `collections.OrderedDict `. This works because
635
- regular dicts have guaranteed ordering since Python 3.7. If the extra
636
- features of :class: `OrderedDict ` are required, the suggested remediation is
637
- to cast the result to the desired type: ``OrderedDict(nt._asdict()) ``.
640
+ The :meth: `~collections.somenamedtuple._asdict ` method for
641
+ :func: `collections.namedtuple ` now returns a :class: `dict ` instead of a
642
+ :class: `collections.OrderedDict `. This works because regular dicts have
643
+ guaranteed ordering since Python 3.7. If the extra features of
644
+ :class: `OrderedDict ` are required, the suggested remediation is to cast the
645
+ result to the desired type: ``OrderedDict(nt._asdict()) ``.
638
646
(Contributed by Raymond Hettinger in :issue: `35864 `.)
639
647
640
648
649
+ cProfile
650
+ --------
651
+
652
+ The :class: `cProfile.Profile <profile.Profile> ` class can now be used as a context manager.
653
+ Profile a block of code by running::
654
+
655
+ import cProfile
656
+
657
+ with cProfile.Profile() as profiler:
658
+ # code to be profiled
659
+ ...
660
+
661
+ (Contributed by Scott Sanderson in :issue: `29235 `.)
662
+
663
+
664
+ csv
665
+ ---
666
+
667
+ The :class: `csv.DictReader ` now returns instances of :class: `dict ` instead of
668
+ a :class: `collections.OrderedDict `. The tool is now faster and uses less
669
+ memory while still preserving the field order.
670
+ (Contributed by Michael Seek in :issue: `34003 `.)
671
+
672
+
641
673
curses
642
674
-------
643
675
@@ -700,6 +732,30 @@ cached for the life of the instance. ::
700
732
(Contributed by Carl Meyer in :issue: `21145 `)
701
733
702
734
735
+ Added a new :func: `functools.singledispatchmethod ` decorator that converts
736
+ methods into :term: `generic functions <generic function> ` using
737
+ :term: `single dispatch `::
738
+
739
+ from functools import singledispatchmethod
740
+ from contextlib import suppress
741
+
742
+ class TaskManager:
743
+
744
+ def __init__(self, tasks):
745
+ self.tasks = list(tasks)
746
+
747
+ @singledispatchmethod
748
+ def discard(self, value):
749
+ with suppress(ValueError):
750
+ self.tasks.remove(value)
751
+
752
+ @discard.register(list)
753
+ def _(self, tasks):
754
+ targets = set(tasks)
755
+ self.tasks = [x for x in self.tasks if x not in targets]
756
+
757
+ (Contributed by Ethan Smith in :issue: `32380 `)
758
+
703
759
gc
704
760
--
705
761
@@ -727,7 +783,7 @@ for certain types of invalid or corrupt gzip files.
727
783
:issue: `6584 `.)
728
784
729
785
730
- idlelib and IDLE
786
+ IDLE and idlelib
731
787
----------------
732
788
733
789
Output over N lines (50 by default) is squeezed down to a button.
@@ -743,12 +799,19 @@ They also re-appear in the box for the next customized run. One can also
743
799
suppress the normal Shell main module restart. (Contributed by Cheryl
744
800
Sabella, Terry Jan Reedy, and others in :issue: `5680 ` and :issue: `37627 `.)
745
801
746
- Add optional line numbers for IDLE editor windows. Windows
802
+ Added optional line numbers for IDLE editor windows. Windows
747
803
open without line numbers unless set otherwise in the General
748
804
tab of the configuration dialog. Line numbers for an existing
749
805
window are shown and hidden in the Options menu.
750
806
(Contributed by Tal Einat and Saimadhav Heblikar in :issue: `17535 `.)
751
807
808
+ OS native encoding is now used for converting between Python strings and Tcl
809
+ objects. This allows IDLE to work with emoji and other non-BMP characters.
810
+ These characters can be displayed or copied and pasted to or from the
811
+ clipboard. Converting strings from Tcl to Python and back now never fails.
812
+ (Many people worked on this for eight years but the problem was finally
813
+ solved by Serhiy Storchaka in :issue: `13153 `.)
814
+
752
815
The changes above have been backported to 3.7 maintenance releases.
753
816
754
817
@@ -779,13 +842,44 @@ fails. The exception is ignored silently by default in release build.
779
842
(Contributed by Victor Stinner in :issue: `18748 `.)
780
843
781
844
845
+ itertools
846
+ ---------
847
+
848
+ The :func: `itertools.accumulate ` function added an option *initial * keyword
849
+ argument to specify an initial value::
850
+
851
+ >>> from itertools import accumulate
852
+ >>> list(accumulate([10, 5, 30, 15], initial=1000))
853
+ [1000, 1010, 1015, 1045, 1060]
854
+
855
+ (Contributed by Lisa Roach in :issue: `34659 `.)
856
+
857
+
782
858
json.tool
783
859
---------
784
860
785
- Add option ``--json-lines `` to parse every input line as separate JSON object.
861
+ Add option ``--json-lines `` to parse every input line as a separate JSON object.
786
862
(Contributed by Weipeng Hong in :issue: `31553 `.)
787
863
788
864
865
+ logging
866
+ -------
867
+
868
+ Added a *force * keyword argument to :func: `logging.basicConfig() `
869
+ When set to *True *, any existing handlers attached
870
+ to the root logger are removed and closed before carrying out the
871
+ configuration specified by the other arguments.
872
+
873
+ This solves a long-standing problem. Once a logger or *basicConfig() * had
874
+ been called, subsequent calls to *basicConfig() * were silently ignored.
875
+ This made it difficult to update, experiment with, or teach the various
876
+ logging configuration options using the interactive prompt or a Jupyter
877
+ notebook.
878
+
879
+ (Suggested by Raymond Hettinger, implemented by Dong-hee Na, and
880
+ reviewed by Vinay Sajip in :issue: `33897 `.)
881
+
882
+
789
883
math
790
884
----
791
885
@@ -807,7 +901,28 @@ numbers::
807
901
808
902
(Contributed by Pablo Galindo in :issue: `35606 `.)
809
903
810
- Added new function :func: `math.isqrt ` for computing integer square roots.
904
+ Added two new combinatoric functions :func: `math.perm ` and :func: `math.comb `::
905
+
906
+ >>> math.perm(10, 3) # Permutations of 10 things taken 3 at a time
907
+ 720
908
+ >>> math.comb(10, 3) # Combinations of 10 things taken 3 at a time
909
+ 120
910
+
911
+ (Contributed by Yash Aggarwal, Keller Fuchs, Serhiy Storchaka, and Raymond
912
+ Hettinger in :issue: `37128 `, :issue: `37178 `, and :issue: `35431 `.)
913
+
914
+ Added a new function :func: `math.isqrt ` for computing accurate integer square
915
+ roots without conversion to floating point. The new function supports
916
+ arbitrarily large integers. It is faster than ``floor(sqrt(n)) `` but slower
917
+ than :func: `math.sqrt `::
918
+
919
+ >>> r = 650320427
920
+ >>> s = r ** 2
921
+ >>> isqrt(s - 1) # correct
922
+ 650320426
923
+ >>> floor(sqrt(s - 1)) # incorrect
924
+ 650320427
925
+
811
926
(Contributed by Mark Dickinson in :issue: `36887 `.)
812
927
813
928
The function :func: `math.factorial ` no longer accepts arguments that are not
@@ -908,11 +1023,6 @@ to a path.
908
1023
pickle
909
1024
------
910
1025
911
- Reduction methods can now include a 6th item in the tuple they return. This
912
- item should specify a custom state-setting method that's called instead of the
913
- regular ``__setstate__ `` method.
914
- (Contributed by Pierre Glaser and Olivier Grisel in :issue: `35900 `.)
915
-
916
1026
:mod: `pickle ` extensions subclassing the C-optimized :class: `~pickle.Pickler `
917
1027
can now override the pickling logic of functions and classes by defining the
918
1028
special :meth: `~pickle.Pickler.reducer_override ` method.
@@ -927,6 +1037,32 @@ NSKeyedArchiver-encoded binary plists.
927
1037
(Contributed by Jon Janzen in :issue: `26707 `.)
928
1038
929
1039
1040
+ pprint
1041
+ ------
1042
+
1043
+ The :mod: `pprint ` module added a *sort_dicts * parameter to several functions.
1044
+ By default, those functions continue to sort dictionaries before rendering or
1045
+ printing. However, if *sort_dicts * is set to *False *, the dictionaries retain
1046
+ the order that keys were inserted. This can be useful for comparison to JSON
1047
+ inputs during debugging.
1048
+
1049
+ In addition, there is a convenience new function, :func: `pprint.pp ` that is
1050
+ like :func: `pprint.pprint ` but with *sort_dicts * defaulting to *False *::
1051
+
1052
+ >>> from pprint import pprint, pp
1053
+ >>> d = dict(source='input.txt', operation='filter', destination='output.txt')
1054
+ >>> pp(d, width=40) # Original order
1055
+ {'source': 'input.txt',
1056
+ 'operation': 'filter',
1057
+ 'destination': 'output.txt'}
1058
+ >>> pprint(d, width=40) # Keys sorted alphabetically
1059
+ {'destination': 'output.txt',
1060
+ 'operation': 'filter',
1061
+ 'source': 'input.txt'}
1062
+
1063
+ (Contributed by Rémi Lapeyre in :issue: `30670 `.)
1064
+
1065
+
930
1066
py_compile
931
1067
----------
932
1068
@@ -973,8 +1109,8 @@ The :func:`socket.if_nameindex()`, :func:`socket.if_nametoindex()`, and
973
1109
ssl
974
1110
---
975
1111
976
- Added :attr: `ssl.SSLContext.post_handshake_auth ` to enable and
977
- :meth: `ssl.SSLSocket.verify_client_post_handshake ` to initiate TLS 1.3
1112
+ Added :attr: `~ ssl.SSLContext.post_handshake_auth ` to enable and
1113
+ :meth: `~ ssl.SSLSocket.verify_client_post_handshake ` to initiate TLS 1.3
978
1114
post-handshake authentication.
979
1115
(Contributed by Christian Heimes in :issue: `34670 `.)
980
1116
@@ -1152,8 +1288,9 @@ the string. (Contributed by Max Belanger, David Euresti, and Greg Price in
1152
1288
unittest
1153
1289
--------
1154
1290
1155
- Added :class: `AsyncMock ` to support an asynchronous version of :class: `Mock `.
1156
- Appropriate new assert functions for testing have been added as well.
1291
+ Added :class: `~unittest.mock.AsyncMock ` to support an asynchronous version of
1292
+ :class: `~unittest.mock.Mock `. Appropriate new assert functions for testing
1293
+ have been added as well.
1157
1294
(Contributed by Lisa Roach in :issue: `26467 `).
1158
1295
1159
1296
Added :func: `~unittest.addModuleCleanup() ` and
@@ -1233,6 +1370,16 @@ them in the generated tree.
1233
1370
(Contributed by Stefan Behnel in :issue: `36676 ` and :issue: `36673 `.)
1234
1371
1235
1372
1373
+ xmlrpc
1374
+ ------
1375
+
1376
+ :class: `xmlrpc.client.ServerProxy ` now supports an optional *headers * keyword
1377
+ argument for a sequence of HTTP headers to be sent with each request. Among
1378
+ other things, this makes it possible to upgrade from default basic
1379
+ authentication to faster session authentication.
1380
+ (Contributed by Cédric Krier in :issue: `35153 `.)
1381
+
1382
+
1236
1383
Optimizations
1237
1384
=============
1238
1385
@@ -1456,6 +1603,11 @@ Deprecated
1456
1603
constant nodes.
1457
1604
(Contributed by Serhiy Storchaka in :issue: `36917 `.)
1458
1605
1606
+ * The :func: `asyncio.coroutine ` :term: `decorator ` is deprecated and will be
1607
+ removed in version 3.10. Instead of ``@asyncio.coroutine ``, use
1608
+ :keyword: `async def ` instead.
1609
+ (Contributed by Andrew Svetlov in :issue: `36921 `.)
1610
+
1459
1611
* The following functions and methods are deprecated in the :mod: `gettext `
1460
1612
module: :func: `~gettext.lgettext `, :func: `~gettext.ldgettext `,
1461
1613
:func: `~gettext.lngettext ` and :func: `~gettext.ldngettext `.
@@ -1502,7 +1654,7 @@ Deprecated
1502
1654
:class: `multiprocessing.managers.SharedMemoryServer `.
1503
1655
- *obj * in :func: `weakref.finalize `.
1504
1656
1505
- In future releases of Python they will be :ref: `positional-only
1657
+ In future releases of Python, they will be :ref: `positional-only
1506
1658
<positional-only_parameter>`.
1507
1659
(Contributed by Serhiy Storchaka in :issue: `36492 `.)
1508
1660
@@ -1512,6 +1664,11 @@ API and Feature Removals
1512
1664
1513
1665
The following features and APIs have been removed from Python 3.8:
1514
1666
1667
+ * Starting with Python 3.3, importing ABCs from :mod: `collections ` was
1668
+ deprecated, and importing should be done from :mod: `collections.abc `. Being
1669
+ able to import from collections was marked for removal in 3.8, but has been
1670
+ delayed to 3.9. (See :issue: `36952 `.)
1671
+
1515
1672
* The :mod: `macpath ` module, deprecated in Python 3.7, has been removed.
1516
1673
(Contributed by Victor Stinner in :issue: `35471 `.)
1517
1674
@@ -1630,7 +1787,7 @@ Changes in the Python API
1630
1787
(Contributed by Eric Snow in :issue: `34651 `, modified by Christian Heimes
1631
1788
in :issue: `37951 `.)
1632
1789
1633
- * The :meth: `imap.IMAP4.logout ` method no longer ignores silently arbitrary
1790
+ * The :meth: `imap.IMAP4.logout ` method no longer silently ignores arbitrary
1634
1791
exceptions.
1635
1792
(Contributed by Victor Stinner in :issue: `36348 `.)
1636
1793
@@ -1685,7 +1842,7 @@ Changes in the Python API
1685
1842
* The ``PyGC_Head `` struct has changed completely. All code that touched the
1686
1843
struct member should be rewritten. (See :issue: `33597 `.)
1687
1844
1688
- * The `` PyInterpreterState ` ` struct has been moved into the "internal"
1845
+ * The :c:type: ` PyInterpreterState ` struct has been moved into the "internal"
1689
1846
header files (specifically Include/internal/pycore_pystate.h). An
1690
1847
opaque ``PyInterpreterState `` is still available as part of the public
1691
1848
API (and stable ABI). The docs indicate that none of the struct's
0 commit comments