Release of the Python 3.11 programming language

After a year of development, a significant release of the Python 3.11 programming language has been published. The new branch will be supported for a year and a half, after which patches with vulnerabilities will be formed for it for another three and a half years.

At the same time, alpha testing of the Python 3.12 branch began (in accordance with the new development schedule, work on a new branch begins five months before the release of the previous branch and reaches the alpha testing stage by the time of the next release). The Python 3.12 branch will be in alpha releases for seven months, during which new features will be added and bugs will be fixed. After that, beta testing will be conducted for three months, during which the addition of new features will be prohibited and all attention will be paid to fixing bugs. The last two months before the release, the branch will be at the release candidate stage, at which the final stabilization will be performed.

New additions in Python 3.11 include:

  • Significant work has been done to optimize performance. The new branch includes changes related to the acceleration and inline-deployment of function calls, the use of fast interpreters of typical operations (x+x, x*x, xx, a[i], a[i] = z, f(arg) C( arg), o.method(), o.attr = z, *seq), as well as optimizations prepared by the Cinder and HotPy projects. Depending on the type of load, there is an increase in the speed of code execution by 10-60%. On average, performance when passing the pyperformance test suite increased by 25%.

    The bytecode caching mechanism has been redesigned, which reduced the interpreter startup time by 10-15%. Objects with code and bytecode are now statically allocated by the interpreter, which made it possible to eliminate the stages of unmarshaling the bytecode extracted from the cache and converting objects with code for placement in dynamic memory.

  • When displaying call traces in diagnostic messages, information is provided about the expression that caused the error (previously, only the line was highlighted without detailing which part of the line caused the error). Extended tracing information can also be retrieved via the API and used to map individual bytecode instructions to a specific position in the source code using the codeobject.co_positions() method or the PyCode_Addr2Location() C API function. The change greatly simplifies debugging problems associated with nested dictionary objects, multiple function calls, and complex arithmetic expressions. Traceback (most recent call last): File "calculation.py", line 54, in result = (x / y / z) * (a / b / c) ~~~~~~^~~ ZeroDivisionError: division by zero
  • Added support for groups of exceptions, giving the program the ability to generate and process several different exceptions at the same time. New types of exceptions ExceptionGroup and BaseExceptionGroup are proposed for grouping several exceptions and their joint invocation, and the expression "except*" is added to separate exceptions from a group.
  • The add_note() method has been added to the BaseException class, which allows you to attach a text note to the exception, for example, to add contextual information not available at the time the exception was thrown.
  • A special type Self has been added to represent the current private class. Self can be used to annotate methods that return an instance of their class in a simpler way than using TypeVar. class MyLock: def __enter__(self) -> Self: self.lock() return self
  • A special LiteralString type has been added that can only include string literals that are compatible with the LiteralString type (i.e., bare strings and strings of LiteralString type, but not arbitrary or combined strings of str type). The LiteralString type can be used to limit the passing of string arguments to functions, the arbitrary substitution of parts of strings in which can lead to vulnerabilities, for example, when generating strings for SQL queries or shell commands. def run_query(sql: LiteralString) -> ... ... def caller( arbitrary_string: str, query_string: LiteralString, table_name: LiteralString, ) -> None: run_query("SELECT * FROM students") # ok run_query(literal_string) # ok run_query( "SELECT * FROM " + literal_string) # ok run_query(arbitrary_string) # Error run_query( # Error f"SELECT * FROM students WHERE name = {arbitrary_string}" )
  • The TypeVarTuple type has been added, which allows the use of variable generics, unlike TypeVar, covering not one type, but an arbitrary number of types.
  • The standard library includes the tomllib module with functions for parsing the TOML format.
  • The ability to mark individual elements of typed dictionaries (TypedDict) with the Required and NotRequired marks is provided to determine required and optional fields (by default, all declared fields are required if the total parameter is not set to False). class Movie(TypedDict): title: str year: NotRequired[int] m1: Movie = {"title": "Black Panther", "year": 2018} # OK m2: Movie = {"title": "Star Wars" } # OK (the year field is optional) m3: Movie = {"year": 2022} # Error, the required title field was not filled)
  • The TaskGroup class has been added to the asyncio module with the implementation of an asynchronous context manager that waits for the task group to complete. Adding tasks to a group is done using the create_task() method. async def main(): async with asyncio.TaskGroup() as tg: task1 = tg.create_task(some_coro(…)) task2 = tg.create_task(another_coro(…)) print("Both tasks have completed now.")
  • Added class, method, and function decorator @dataclass_transform, when specified, the static type checker treats the object as if using the @dataclasses.dataclass decorator. In the example below, the CustomerModel class will be type-checked in the same way as a class with the @dataclasses.dataclass decorator, i.e. as having an __init__ method that allows id and name variables. @dataclass_transform() class ModelBase: … class CustomerModel(ModelBase): id: int name: str
  • Added the ability to use atomic grouping ((?>…)) and jealous (possessive) quantifiers (*+, ++, ?+, {m,n}+) in regular expressions.
  • Added "-P" command line option and PYTHONSAFEPATH environment variable to disable automatic attachment of potentially unsafe file paths to sys.path.
  • Significantly improved the py.exe utility for the Windows platform, which now supports the "-V:" syntax. / " in addition to "- . ".
  • Many macros in the C API have been converted to normal or static inline functions.
  • The uu, cgi, pipes, crypt, aifc, chunk, msilib, telnetlib, audioop, nis, sndhdr, imghdr, nntplib, spwd, xdrlib, cgitb, mailcap, ossaudiodev, and sunau modules have been deprecated and will be removed in the Python 3.13 release. Removed PyUnicode_Encode* functions.

Source: opennet.ru

Add a comment