Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into docs/whatsnew/bytecode
# Conflicts:
#	Doc/whatsnew/3.12.rst
  • Loading branch information
AA-Turner committed Sep 27, 2023
commit 6ddc85289e65242190a064a02dc0b8050851343f
98 changes: 98 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,104 @@ See :mod:`sys.monitoring` for details.

(Contributed by Mark Shannon in :gh:`103082`.)

.. _whatsnew312-pep688:

PEP 688: Making the buffer protocol accessible in Python
--------------------------------------------------------

:pep:`688` introduces a way to use the :ref:`buffer protocol <bufferobjects>`
from Python code. Classes that implement the :meth:`~object.__buffer__` method
are now usable as buffer types.

The new :class:`collections.abc.Buffer` ABC provides a standard
way to represent buffer objects, for example in type annotations.
The new :class:`inspect.BufferFlags` enum represents the flags that
can be used to customize buffer creation.
(Contributed by Jelle Zijlstra in :gh:`102500`.)

.. _whatsnew312-pep709:

PEP 709: Comprehension inlining
-------------------------------

Dictionary, list, and set comprehensions are now inlined, rather than creating a
new single-use function object for each execution of the comprehension. This
speeds up execution of a comprehension by up to two times.
See :pep:`709` for further details.

Comprehension iteration variables remain isolated and don't overwrite a
variable of the same name in the outer scope, nor are they visible after the
comprehension. Inlining does result in a few visible behavior changes:

* There is no longer a separate frame for the comprehension in tracebacks,
and tracing/profiling no longer shows the comprehension as a function call.
* The :mod:`symtable` module will no longer produce child symbol tables for each
comprehension; instead, the comprehension's locals will be included in the
parent function's symbol table.
* Calling :func:`locals` inside a comprehension now includes variables
from outside the comprehension, and no longer includes the synthetic ``.0``
variable for the comprehension "argument".
* A comprehension iterating directly over ``locals()`` (e.g. ``[k for k in
locals()]``) may see "RuntimeError: dictionary changed size during iteration"
when run under tracing (e.g. code coverage measurement). This is the same
behavior already seen in e.g. ``for k in locals():``. To avoid the error, first
create a list of keys to iterate over: ``keys = list(locals()); [k for k in
keys]``.

(Contributed by Carl Meyer and Vladimir Matveev in :pep:`709`.)

Improved Error Messages
-----------------------

* Modules from the standard library are now potentially suggested as part of
the error messages displayed by the interpreter when a :exc:`NameError` is
raised to the top level. (Contributed by Pablo Galindo in :gh:`98254`.)

>>> sys.version_info
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined. Did you forget to import 'sys'?

* Improve the error suggestion for :exc:`NameError` exceptions for instances.
Now if a :exc:`NameError` is raised in a method and the instance has an
attribute that's exactly equal to the name in the exception, the suggestion
will include ``self.<NAME>`` instead of the closest match in the method
scope. (Contributed by Pablo Galindo in :gh:`99139`.)

>>> class A:
... def __init__(self):
... self.blech = 1
...
... def foo(self):
... somethin = blech
...
>>> A().foo()
Traceback (most recent call last):
File "<stdin>", line 1
somethin = blech
^^^^^
NameError: name 'blech' is not defined. Did you mean: 'self.blech'?

* Improve the :exc:`SyntaxError` error message when the user types ``import x
from y`` instead of ``from y import x``. (Contributed by Pablo Galindo in :gh:`98931`.)

>>> import a.y.z from b.y.z
Traceback (most recent call last):
File "<stdin>", line 1
import a.y.z from b.y.z
^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Did you mean to use 'from ... import ...' instead?

* :exc:`ImportError` exceptions raised from failed ``from <module> import
<name>`` statements now include suggestions for the value of ``<name>`` based on the
available names in ``<module>``. (Contributed by Pablo Galindo in :gh:`91058`.)

>>> from collections import chainmap
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'chainmap' from 'collections'. Did you mean: 'ChainMap'?


New Features Related to Type Hints
==================================

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.