Tin mới
modular multiplicative inverse
A continue statement was illegal in the finally clause due to a problem with the implementation. In Python 3.8 this restriction was lifted. (Contributed by Serhiy Storchaka in bpo-32489.)
The new importlib.metadata module provides (provisional) support for reading metadata from third-party packages. For example, it can extract an installed package’s version number, list of entry points, and more:
datetime.datetime.fromisocalendar()
AST nodes now have end_lineno and end_col_offset attributes, which give the precise location of the end of the node. (This only applies to nodes that have lineno and col_offset attributes.)
Platform-dependent efficient copy operations
The subprocess module can now use the os.posix_spawn() function in some cases for better performance. Currently, it is only used on macOS and Linux (using glibc 2.24 or newer) if all these conditions are met:
Default sys.abiflags became an empty string: the m flag for pymalloc became useless (builds with and without pymalloc are ABI compatible) and so has been removed. (Contributed by Victor Stinner in bpo-36707.)
contextlib.AsyncExitStack.push_async_callback()
The distutils bdist_wininst command is now deprecated, use bdist_wheel (wheel packages) instead. (Contributed by Victor Stinner in bpo-37481.)
The following features and APIs have been removed from Python 3.8:
Platform-dependent efficient copy operations
This section lists previously described changes and other bugfixes that may require changes to your code.
asyncio.loop.create_datagram_endpoint()
Due to significant security concerns, the reuse_address parameter of asyncio.loop.create_datagram_endpoint() is no longer supported. This is because of the behavior of the socket option SO_REUSEADDR in UDP. For more deta
Notable changes in Python 3.8.10¶
The presence of newline or tab characters in parts of a URL allows for some forms of attacks. Following the WHATWG specification that updates RFC 3986, ASCII newline \n, \r and tab \t characters are stripped from the URL
the eyes and tusks of a walrus
There is new syntax := that assigns values to variables as part of a larger expression. It is affectionately known as “the walrus operator” due to its resemblance to the eyes and tusks of a walrus.
AST nodes now have end_lineno and end_col_offset attributes, which give the precise location of the end of the node. (This only applies to nodes that have lineno and col_offset attributes.)
The compile() built-in has been improved to accept the ast.PyCF_ALLOW_TOP_LEVEL_AWAIT flag. With this new flag passed, compile() will allow top-level await, async for and async with constructs that are usually considered
The _asdict() method for collections.namedtuple() now returns a dict instead of a collections.OrderedDict. This works because regular dicts have guaranteed ordering since Python 3.7. If the extra features of OrderedDict
On Windows, CDLL and subclasses now accept a winmode parameter to specify flags for the underlying LoadLibraryEx call. The default flags are set to only load DLL dependencies from trusted locations, including the path wh
functools.singledispatchmethod()
@functools.lru_cache can now be used as a straight decorator rather than as a function returning a decorator. So both of these are now supported:
Added pgettext() and its variants. (Contributed by Franz Glasner, Éric Araujo, and Cheryl Sabella in bpo-2504.)
In development mode (-X env) and in debug build, the io.IOBase finalizer now logs the exception if the close() method fails. The exception is ignored silently by default in release build. (Contributed by Victor Stinner i
The itertools.accumulate() function added an option initial keyword argument to specify an initial value:
Added a force keyword argument to logging.basicConfig(). When set to true, any existing handlers attached to the root logger are removed and closed before carrying out the configuration specified by the other arguments.
The mmap.mmap class now has an madvise() method to access the madvise() system call. (Contributed by Zackery Spytz in bpo-32941.)
os.path functions that return a boolean result like exists(), lexists(), isdir(), isfile(), islink(), and ismount() now return False instead of raising ValueError or its subclasses UnicodeEncodeError and UnicodeDecodeErr
pathlib.Path methods that return a boolean result like exists(), is_dir(), is_file(), is_mount(), is_symlink(), is_block_device(), is_char_device(), is_fifo(), is_socket() now return False instead of raising ValueError o
pickle extensions subclassing the C-optimized Pickler can now override the pickling logic of functions and classes by defining the special reducer_override() method. (Contributed by Pierre Glaser and Olivier Grisel in bp
py_compile.compile() now supports silent mode. (Contributed by Joannah Nanjekye in bpo-22640.)
Added create_server() and has_dualstack_ipv6() convenience functions to automate the necessary tasks usually involved when creating a server socket, including accepting both IPv4 and IPv6 connections on the same socket.
verify_client_post_handshake()
Added post_handshake_auth to enable and verify_client_post_handshake() to initiate TLS 1.3 post-handshake authentication. (Contributed by Christian Heimes in bpo-34670.)
Added statistics.fmean() as a faster, floating-point variant of statistics.mean(). (Contributed by Raymond Hettinger and Steven D’Aprano in bpo-35904.)
Add new sys.unraisablehook() function which can be overridden to control how “unraisable exceptions” are handled. It is called when an exception has occurred but there is no way for Python to handle it. For example, when
Add a new threading.excepthook() function which handles uncaught threading.Thread.run() exception. It can be overridden to control how uncaught threading.Thread.run() exceptions are handled. (Contributed by Victor Stinne
The tokenize module now implicitly emits a NEWLINE token when provided with input that does not have a trailing new line. This behavior now matches what the C tokenizer does internally. (Contributed by Ammar Askar in bpo
The typing module incorporates several new features:
unittest.IsolatedAsyncioTestCase
Added AsyncMock to support an asynchronous version of Mock. Appropriate new assert functions for testing have been added as well. (Contributed by Lisa Roach in bpo-26467).
venv now includes an Activate.ps1 script on all platforms for activating virtual environments under PowerShell Core 6.1. (Contributed by Brett Cannon in bpo-32718.)
xmlrpc.client.ServerProxy now supports an optional headers keyword argument for a sequence of HTTP headers to be sent with each request. Among other things, this makes it possible to upgrade from default basic authentica
Parsing arguments and building values
The PyCompilerFlags structure got a new cf_feature_version field. It should be initialized to PY_MINOR_VERSION. The field is ignored by default, and is used if and only if PyCF_ONLY_AST flag is set in cf_flags. (Contribu
Starting with Python 3.8.12 the ipaddress module no longer accepts any leading zeros in IPv4 address strings. Leading zeros are ambiguous and interpreted as octal notation by some libraries. For example the legacy functi