Merge branch 'master' into topic/34716

This commit is contained in:
Jackson Riley 2020-11-10 16:12:33 +00:00
commit 30709e287e
405 changed files with 13312 additions and 5357 deletions

View File

@ -120,10 +120,11 @@ jobs:
artifactName: unsigned_msix
downloadPath: $(Build.BinariesDirectory)
# MSIX must be signed and timestamped simultaneously
- powershell: |
$failed = $true
foreach ($retry in 1..3) {
signtool sign /a /n "$(SigningCertificate)" /fd sha256 /t http://timestamp.verisign.com/scripts/timestamp.dll /d "$(SigningDescription)" (gi *.msix)
signtool sign /a /n "$(SigningCertificate)" /fd sha256 /tr http://timestamp.digicert.com/ /td sha256 /d "$(SigningDescription)" (gi *.msix)
if ($?) {
$failed = $false
break

View File

@ -4,7 +4,7 @@ jobs:
condition: and(succeeded(), eq(variables['DoNuget'], 'true'))
pool:
vmImage: windows-2019
name: 'Windows Release'
workspace:
clean: all
@ -36,6 +36,14 @@ jobs:
nuget pack "$(Build.BinariesDirectory)\layout\python.nuspec" -OutputDirectory $(Build.ArtifactStagingDirectory) -NoPackageAnalysis -NonInteractive
displayName: 'Create nuget package'
- powershell: |
gci *.nupkg | %{
nuget sign "$_" -CertificateSubjectName "$(SigningCertificate)" -Timestamper http://timestamp.digicert.com/ -Overwrite
}
displayName: 'Sign nuget package'
workingDirectory: $(Build.ArtifactStagingDirectory)
condition: and(succeeded(), variables['SigningCertificate'])
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: nuget'
inputs:

View File

@ -57,7 +57,7 @@ jobs:
$files = (gi ${{ parameters.Include }} -Exclude ${{ parameters.Exclude }})
$failed = $true
foreach ($retry in 1..10) {
signtool timestamp /t http://timestamp.verisign.com/scripts/timestamp.dll $files
signtool timestamp /tr http://timestamp.digicert.com/ /td sha256 $files
if ($?) {
$failed = $false
break

9
.github/dependabot.yml vendored Normal file
View File

@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
labels:
- "skip issue"
- "skip news"

View File

@ -106,7 +106,7 @@ jobs:
run: sudo ./.github/workflows/posix-deps-apt.sh
- name: 'Restore OpenSSL build'
id: cache-openssl
uses: actions/cache@v1
uses: actions/cache@v2.1.2
with:
path: ./multissl/openssl/${{ env.OPENSSL_VER }}
key: ${{ runner.os }}-multissl-openssl-${{ env.OPENSSL_VER }}

View File

@ -32,7 +32,7 @@ jobs:
run: sudo ./.github/workflows/posix-deps-apt.sh
- name: 'Restore OpenSSL build'
id: cache-openssl
uses: actions/cache@v1
uses: actions/cache@v2.1.2
with:
path: ./multissl/openssl/${{ env.OPENSSL_VER }}
key: ${{ runner.os }}-multissl-openssl-${{ env.OPENSSL_VER }}

View File

@ -38,7 +38,7 @@ jobs:
- name: 'Build documentation'
run: xvfb-run make -C Doc/ PYTHON=../python SPHINXOPTS="-q -W --keep-going -j4" doctest suspicious html
- name: 'Upload'
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v2.2.0
with:
name: doc-html
path: Doc/build/html

View File

@ -3,19 +3,19 @@ apt-get update
apt-get -yq install \
build-essential \
zlib1g-dev \
gdb \
lcov \
libbz2-dev \
libffi-dev \
libgdbm-dev \
liblzma-dev \
libncurses5-dev \
libreadline6-dev \
libsqlite3-dev \
libssl-dev \
libgdbm-dev \
tk-dev \
lzma \
lzma-dev \
liblzma-dev \
libffi-dev \
tk-dev \
uuid-dev \
xvfb \
lcov
zlib1g-dev

2
.gitignore vendored
View File

@ -103,6 +103,8 @@ Tools/unicode/data/
/config.status
/config.status.lineno
/platform
/profile-clean-stamp
/profile-run-stamp
/pybuilddir.txt
/pyconfig.h
/python-config

View File

@ -215,12 +215,12 @@ serve:
# for development releases: always build
autobuild-dev:
make dist SPHINXOPTS='$(SPHINXOPTS) -Ea -A daily=1 -A switchers=1'
make dist SPHINXOPTS='$(SPHINXOPTS) -Ea -A daily=1'
-make suspicious
# for quick rebuilds (HTML only)
autobuild-dev-html:
make html SPHINXOPTS='$(SPHINXOPTS) -Ea -A daily=1 -A switchers=1'
make html SPHINXOPTS='$(SPHINXOPTS) -Ea -A daily=1'
# for stable releases: only build if not in pre-release stage (alpha, beta)
# release candidate downloads are okay, since the stable tree can be in that stage

View File

@ -482,7 +482,8 @@ API Functions
*min* and no more than *max*; *min* and *max* may be equal. Additional
arguments must be passed to the function, each of which should be a pointer to a
:c:type:`PyObject*` variable; these will be filled in with the values from
*args*; they will contain borrowed references. The variables which correspond
*args*; they will contain :term:`borrowed references <borrowed reference>`.
The variables which correspond
to optional parameters not given by *args* will not be filled in; these should
be initialized by the caller. This function returns true on success and false if
*args* is not a tuple or contains the wrong number of elements; an exception

View File

@ -182,8 +182,8 @@ For convenience, some of these functions will always return a
.. c:function:: PyObject* PyErr_SetFromErrnoWithFilename(PyObject *type, const char *filename)
Similar to :c:func:`PyErr_SetFromErrnoWithFilenameObject`, but the filename
is given as a C string. *filename* is decoded from the filesystem encoding
(:func:`os.fsdecode`).
is given as a C string. *filename* is decoded from the :term:`filesystem
encoding and error handler`.
.. c:function:: PyObject* PyErr_SetFromWindowsErr(int ierr)
@ -266,7 +266,7 @@ For convenience, some of these functions will always return a
.. c:function:: void PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
Like :c:func:`PyErr_SyntaxLocationObject`, but *filename* is a byte string
decoded from the filesystem encoding (:func:`os.fsdecode`).
decoded from the :term:`filesystem encoding and error handler`.
.. versionadded:: 3.2
@ -343,7 +343,7 @@ an error value).
Similar to :c:func:`PyErr_WarnExplicitObject` except that *message* and
*module* are UTF-8 encoded strings, and *filename* is decoded from the
filesystem encoding (:func:`os.fsdecode`).
:term:`filesystem encoding and error handler`.
.. c:function:: int PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, const char *format, ...)

View File

@ -151,8 +151,9 @@ to 1 and ``-bb`` sets :c:data:`Py_BytesWarningFlag` to 2.
.. c:var:: int Py_LegacyWindowsFSEncodingFlag
If the flag is non-zero, use the ``mbcs`` encoding instead of the UTF-8
encoding for the filesystem encoding.
If the flag is non-zero, use the ``mbcs`` encoding with ``replace`` error
handler, instead of the UTF-8 encoding with ``surrogatepass`` error handler,
for the :term:`filesystem encoding and error handler`.
Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment
variable is set to a non-empty string.
@ -1076,7 +1077,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
Get the current frame of the Python thread state *tstate*.
Return a strong reference. Return ``NULL`` if no frame is currently
Return a :term:`strong reference`. Return ``NULL`` if no frame is currently
executing.
See also :c:func:`PyEval_GetFrame`.

File diff suppressed because it is too large Load Diff

View File

@ -326,7 +326,7 @@ when it's no longer needed---or passing on this responsibility (usually to its
caller). When a function passes ownership of a reference on to its caller, the
caller is said to receive a *new* reference. When no ownership is transferred,
the caller is said to *borrow* the reference. Nothing needs to be done for a
borrowed reference.
:term:`borrowed reference`.
Conversely, when a calling function passes in a reference to an object, there
are two possibilities: the function *steals* a reference to the object, or it

View File

@ -264,7 +264,7 @@ of the following two module creation functions:
instead; only use this if you are sure you need it.
Before it is returned from in the initialization function, the resulting module
object is typically populated using functions like :c:func:`PyModule_AddObject`.
object is typically populated using functions like :c:func:`PyModule_AddObjectRef`.
.. _multi-phase-initialization:
@ -437,26 +437,102 @@ a function called from a module execution slot (if using multi-phase
initialization), can use the following functions to help initialize the module
state:
.. c:function:: int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
Add an object to *module* as *name*. This is a convenience function which
can be used from the module's initialization function.
On success, return ``0``. On error, raise an exception and return ``-1``.
Return ``NULL`` if *value* is ``NULL``. It must be called with an exception
raised in this case.
Example usage::
static int
add_spam(PyObject *module, int value)
{
PyObject *obj = PyLong_FromLong(value);
if (obj == NULL) {
return -1;
}
int res = PyModule_AddObjectRef(module, "spam", obj);
Py_DECREF(obj);
return res;
}
The example can also be written without checking explicitly if *obj* is
``NULL``::
static int
add_spam(PyObject *module, int value)
{
PyObject *obj = PyLong_FromLong(value);
int res = PyModule_AddObjectRef(module, "spam", obj);
Py_XDECREF(obj);
return res;
}
Note that ``Py_XDECREF()`` should be used instead of ``Py_DECREF()`` in
this case, since *obj* can be ``NULL``.
.. versionadded:: 3.10
.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
Add an object to *module* as *name*. This is a convenience function which can
be used from the module's initialization function. This steals a reference to
*value* on success. Return ``-1`` on error, ``0`` on success.
Similar to :c:func:`PyModule_AddObjectRef`, but steals a reference to
*value* on success (if it returns ``0``).
The new :c:func:`PyModule_AddObjectRef` function is recommended, since it is
easy to introduce reference leaks by misusing the
:c:func:`PyModule_AddObject` function.
.. note::
Unlike other functions that steal references, ``PyModule_AddObject()`` only
decrements the reference count of *value* **on success**.
Unlike other functions that steal references, ``PyModule_AddObject()``
only decrements the reference count of *value* **on success**.
This means that its return value must be checked, and calling code must
:c:func:`Py_DECREF` *value* manually on error. Example usage::
:c:func:`Py_DECREF` *value* manually on error.
Example usage::
static int
add_spam(PyObject *module, int value)
{
PyObject *obj = PyLong_FromLong(value);
if (obj == NULL) {
return -1;
}
if (PyModule_AddObject(module, "spam", obj) < 0) {
Py_DECREF(obj);
return -1;
}
// PyModule_AddObject() stole a reference to obj:
// Py_DECREF(obj) is not needed here
return 0;
}
The example can also be written without checking explicitly if *obj* is
``NULL``::
static int
add_spam(PyObject *module, int value)
{
PyObject *obj = PyLong_FromLong(value);
if (PyModule_AddObject(module, "spam", obj) < 0) {
Py_XDECREF(obj);
return -1;
}
// PyModule_AddObject() stole a reference to obj:
// Py_DECREF(obj) is not needed here
return 0;
}
Note that ``Py_XDECREF()`` should be used instead of ``Py_DECREF()`` in
this case, since *obj* can be ``NULL``.
Py_INCREF(spam);
if (PyModule_AddObject(module, "spam", spam) < 0) {
Py_DECREF(module);
Py_DECREF(spam);
return NULL;
}
.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)

View File

@ -13,8 +13,14 @@ objects.
.. c:function:: void Py_INCREF(PyObject *o)
Increment the reference count for object *o*. The object must not be ``NULL``; if
you aren't sure that it isn't ``NULL``, use :c:func:`Py_XINCREF`.
Increment the reference count for object *o*.
This function is usually used to convert a :term:`borrowed reference` to a
:term:`strong reference` in-place. The :c:func:`Py_NewRef` function can be
used to create a new :term:`strong reference`.
The object must not be ``NULL``; if you aren't sure that it isn't
``NULL``, use :c:func:`Py_XINCREF`.
.. c:function:: void Py_XINCREF(PyObject *o)
@ -22,13 +28,55 @@ objects.
Increment the reference count for object *o*. The object may be ``NULL``, in
which case the macro has no effect.
See also :c:func:`Py_XNewRef`.
.. c:function:: PyObject* Py_NewRef(PyObject *o)
Create a new :term:`strong reference` to an object: increment the reference
count of the object *o* and return the object *o*.
When the :term:`strong reference` is no longer needed, :c:func:`Py_DECREF`
should be called on it to decrement the object reference count.
The object *o* must not be ``NULL``; use :c:func:`Py_XNewRef` if *o* can be
``NULL``.
For example::
Py_INCREF(obj);
self->attr = obj;
can be written as::
self->attr = Py_NewRef(obj);
See also :c:func:`Py_INCREF`.
.. versionadded:: 3.10
.. c:function:: PyObject* Py_XNewRef(PyObject *o)
Similar to :c:func:`Py_NewRef`, but the object *o* can be NULL.
If the object *o* is ``NULL``, the function just returns ``NULL``.
.. versionadded:: 3.10
.. c:function:: void Py_DECREF(PyObject *o)
Decrement the reference count for object *o*. The object must not be ``NULL``; if
you aren't sure that it isn't ``NULL``, use :c:func:`Py_XDECREF`. If the reference
count reaches zero, the object's type's deallocation function (which must not be
``NULL``) is invoked.
Decrement the reference count for object *o*.
If the reference count reaches zero, the object's type's deallocation
function (which must not be ``NULL``) is invoked.
This function is usually used to delete a :term:`strong reference` before
exiting its scope.
The object must not be ``NULL``; if you aren't sure that it isn't ``NULL``,
use :c:func:`Py_XDECREF`.
.. warning::

View File

@ -35,7 +35,7 @@ Reflection
Get the *frame* next outer frame.
Return a strong reference, or ``NULL`` if *frame* has no outer frame.
Return a :term:`strong reference`, or ``NULL`` if *frame* has no outer frame.
*frame* must not be ``NULL``.
@ -46,7 +46,7 @@ Reflection
Get the *frame* code.
Return a strong reference.
Return a :term:`strong reference`.
*frame* must not be ``NULL``. The result (frame code) cannot be ``NULL``.

View File

@ -66,7 +66,7 @@ the definition of all other Python objects.
Get the type of the Python object *o*.
Return a borrowed reference.
Return a :term:`borrowed reference`.
.. versionchanged:: 3.10
:c:func:`Py_TYPE()` is changed to the inline static function.
@ -263,10 +263,12 @@ There are these calling conventions:
of :c:type:`PyObject*` values indicating the arguments and the third
parameter is the number of arguments (the length of the array).
This is not part of the :ref:`limited API <stable>`.
.. versionadded:: 3.7
.. versionchanged:: 3.10
``METH_FASTCALL`` is now part of the stable ABI.
.. data:: METH_FASTCALL | METH_KEYWORDS

View File

@ -118,22 +118,21 @@ Operating System Utilities
.. c:function:: wchar_t* Py_DecodeLocale(const char* arg, size_t *size)
Decode a byte string from the locale encoding with the :ref:`surrogateescape
error handler <surrogateescape>`: undecodable bytes are decoded as
characters in range U+DC80..U+DCFF. If a byte sequence can be decoded as a
surrogate character, escape the bytes using the surrogateescape error
handler instead of decoding them.
.. warning::
This function should not be called directly: use the :c:type:`PyConfig`
API with the :c:func:`PyConfig_SetBytesString` function which ensures
that :ref:`Python is preinitialized <c-preinit>`.
Encoding, highest priority to lowest priority:
This function must not be called before :ref:`Python is preinitialized
<c-preinit>` and so that the LC_CTYPE locale is properly configured: see
the :c:func:`Py_PreInitialize` function.
* ``UTF-8`` on macOS, Android, and VxWorks;
* ``UTF-8`` on Windows if :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
* ``UTF-8`` if the Python UTF-8 mode is enabled;
* ``ASCII`` if the ``LC_CTYPE`` locale is ``"C"``,
``nl_langinfo(CODESET)`` returns the ``ASCII`` encoding (or an alias),
and :c:func:`mbstowcs` and :c:func:`wcstombs` functions uses the
``ISO-8859-1`` encoding.
* the current locale encoding.
Decode a byte string from the :term:`filesystem encoding and error handler`.
If the error handler is :ref:`surrogateescape error handler
<surrogateescape>`, undecodable bytes are decoded as characters in range
U+DC80..U+DCFF; and if a byte sequence can be decoded as a surrogate
character, the bytes are escaped using the surrogateescape error handler
instead of decoding them.
Return a pointer to a newly allocated wide character string, use
:c:func:`PyMem_RawFree` to free the memory. If size is not ``NULL``, write
@ -143,6 +142,10 @@ Operating System Utilities
not ``NULL``, ``*size`` is set to ``(size_t)-1`` on memory error or set to
``(size_t)-2`` on decoding error.
The :term:`filesystem encoding and error handler` are selected by
:c:func:`PyConfig_Read`: see :c:member:`~PyConfig.filesystem_encoding` and
:c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
Decoding errors should never happen, unless there is a bug in the C
library.
@ -157,7 +160,8 @@ Operating System Utilities
.. versionadded:: 3.5
.. versionchanged:: 3.7
The function now uses the UTF-8 encoding in the UTF-8 mode.
The function now uses the UTF-8 encoding in the :ref:`Python UTF-8 Mode
<utf8-mode>`.
.. versionchanged:: 3.8
The function now uses the UTF-8 encoding on Windows if
@ -166,22 +170,10 @@ Operating System Utilities
.. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
Encode a wide character string to the locale encoding with the
:ref:`surrogateescape error handler <surrogateescape>`: surrogate characters
in the range U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
Encoding, highest priority to lowest priority:
* ``UTF-8`` on macOS, Android, and VxWorks;
* ``UTF-8`` on Windows if :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
* ``UTF-8`` if the Python UTF-8 mode is enabled;
* ``ASCII`` if the ``LC_CTYPE`` locale is ``"C"``,
``nl_langinfo(CODESET)`` returns the ``ASCII`` encoding (or an alias),
and :c:func:`mbstowcs` and :c:func:`wcstombs` functions uses the
``ISO-8859-1`` encoding.
* the current locale encoding.
The function uses the UTF-8 encoding in the Python UTF-8 mode.
Encode a wide character string to the :term:`filesystem encoding and error
handler`. If the error handler is :ref:`surrogateescape error handler
<surrogateescape>`, surrogate characters in the range U+DC80..U+DCFF are
converted to bytes 0x80..0xFF.
Return a pointer to a newly allocated byte string, use :c:func:`PyMem_Free`
to free the memory. Return ``NULL`` on encoding error or memory allocation
@ -190,9 +182,18 @@ Operating System Utilities
If error_pos is not ``NULL``, ``*error_pos`` is set to ``(size_t)-1`` on
success, or set to the index of the invalid character on encoding error.
The :term:`filesystem encoding and error handler` are selected by
:c:func:`PyConfig_Read`: see :c:member:`~PyConfig.filesystem_encoding` and
:c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
Use the :c:func:`Py_DecodeLocale` function to decode the bytes string back
to a wide character string.
.. warning::
This function must not be called before :ref:`Python is preinitialized
<c-preinit>` and so that the LC_CTYPE locale is properly configured: see
the :c:func:`Py_PreInitialize` function.
.. seealso::
The :c:func:`PyUnicode_EncodeFSDefault` and
@ -201,7 +202,8 @@ Operating System Utilities
.. versionadded:: 3.5
.. versionchanged:: 3.7
The function now uses the UTF-8 encoding in the UTF-8 mode.
The function now uses the UTF-8 encoding in the :ref:`Python UTF-8 Mode
<utf8-mode>`.
.. versionchanged:: 3.8
The function now uses the UTF-8 encoding on Windows if

View File

@ -169,6 +169,10 @@ The following functions and structs are used to create
.. versionadded:: 3.9
.. versionchanged:: 3.10
The function now accepts NULL ``tp_doc`` slot.
.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
Equivalent to ``PyType_FromModuleAndSpec(NULL, spec, bases)``.
@ -259,5 +263,3 @@ The following functions and structs are used to create
The desired value of the slot. In most cases, this is a pointer
to a function.
May not be ``NULL``.

View File

@ -1213,8 +1213,9 @@ and :c:type:`PyType_Type` effectively act as defaults.)
:func:`~gc.get_referents` function will include it.
.. warning::
When implementing :c:member:`~PyTypeObject.tp_traverse`, only the members
that the instance *owns* (by having strong references to them) must be
When implementing :c:member:`~PyTypeObject.tp_traverse`, only the
members that the instance *owns* (by having :term:`strong references
<strong reference>` to them) must be
visited. For instance, if an object supports weak references via the
:c:member:`~PyTypeObject.tp_weaklist` slot, the pointer supporting
the linked list (what *tp_weaklist* points to) must **not** be

View File

@ -783,7 +783,7 @@ system.
:c:data:`Py_FileSystemDefaultEncoding` (the locale encoding read at
Python startup).
This function ignores the Python UTF-8 mode.
This function ignores the :ref:`Python UTF-8 Mode <utf8-mode>`.
.. seealso::
@ -819,7 +819,7 @@ system.
:c:data:`Py_FileSystemDefaultEncoding` (the locale encoding read at
Python startup).
This function ignores the Python UTF-8 mode.
This function ignores the :ref:`Python UTF-8 Mode <utf8-mode>`.
.. seealso::
@ -878,8 +878,7 @@ conversion function:
.. c:function:: PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
Decode a string using :c:data:`Py_FileSystemDefaultEncoding` and the
:c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
Decode a string from the :term:`filesystem encoding and error handler`.
If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
locale encoding.
@ -899,8 +898,8 @@ conversion function:
.. c:function:: PyObject* PyUnicode_DecodeFSDefault(const char *s)
Decode a null-terminated string using :c:data:`Py_FileSystemDefaultEncoding`
and the :c:data:`Py_FileSystemDefaultEncodeErrors` error handler.
Decode a null-terminated string from the :term:`filesystem encoding and
error handler`.
If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
locale encoding.

View File

@ -112,9 +112,9 @@ the same library that the Python runtime is using.
Similar to :c:func:`PyRun_SimpleStringFlags`, but the Python source code is read
from *fp* instead of an in-memory string. *filename* should be the name of
the file, it is decoded from the filesystem encoding
(:func:`sys.getfilesystemencoding`). If *closeit* is true, the file is
closed before PyRun_SimpleFileExFlags returns.
the file, it is decoded from :term:`filesystem encoding and error handler`.
If *closeit* is true, the file is closed before
``PyRun_SimpleFileExFlags()`` returns.
.. note::
On Windows, *fp* should be opened as binary mode (e.g. ``fopen(filename, "rb")``).
@ -132,7 +132,7 @@ the same library that the Python runtime is using.
Read and execute a single statement from a file associated with an
interactive device according to the *flags* argument. The user will be
prompted using ``sys.ps1`` and ``sys.ps2``. *filename* is decoded from the
filesystem encoding (:func:`sys.getfilesystemencoding`).
:term:`filesystem encoding and error handler`.
Returns ``0`` when the input was
executed successfully, ``-1`` if there was an exception, or an error code
@ -151,9 +151,8 @@ the same library that the Python runtime is using.
Read and execute statements from a file associated with an interactive device
until EOF is reached. The user will be prompted using ``sys.ps1`` and
``sys.ps2``. *filename* is decoded from the filesystem encoding
(:func:`sys.getfilesystemencoding`). Returns ``0`` at EOF or a negative
number upon failure.
``sys.ps2``. *filename* is decoded from the :term:`filesystem encoding and
error handler`. Returns ``0`` at EOF or a negative number upon failure.
.. c:var:: int (*PyOS_InputHook)(void)
@ -206,8 +205,8 @@ the same library that the Python runtime is using.
Parse Python source code from *str* using the start token *start* according to
the *flags* argument. The result can be used to create a code object which can
be evaluated efficiently. This is useful if a code fragment must be evaluated
many times. *filename* is decoded from the filesystem encoding
(:func:`sys.getfilesystemencoding`).
many times. *filename* is decoded from the :term:`filesystem encoding and
error handler`.
.. c:function:: struct _node* PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
@ -262,7 +261,7 @@ the same library that the Python runtime is using.
Similar to :c:func:`PyRun_StringFlags`, but the Python source code is read from
*fp* instead of an in-memory string. *filename* should be the name of the file,
it is decoded from the filesystem encoding (:func:`sys.getfilesystemencoding`).
it is decoded from the :term:`filesystem encoding and error handler`.
If *closeit* is true, the file is closed before :c:func:`PyRun_FileExFlags`
returns.
@ -301,7 +300,7 @@ the same library that the Python runtime is using.
.. c:function:: PyObject* Py_CompileStringExFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags, int optimize)
Like :c:func:`Py_CompileStringObject`, but *filename* is a byte string
decoded from the filesystem encoding (:func:`os.fsdecode`).
decoded from the :term:`filesystem encoding and error handler`.
.. versionadded:: 3.2

View File

@ -57,10 +57,10 @@ as much as it can.
.. note::
This function returns a **borrowed reference** to the referenced object.
This function returns a :term:`borrowed reference` to the referenced object.
This means that you should always call :c:func:`Py_INCREF` on the object
except if you know that it cannot be destroyed while you are still
using it.
except it cannot be destroyed before the last usage of the borrowed
reference.
.. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)

View File

@ -3007,6 +3007,9 @@ Py_GetVersion:const char*:::
Py_INCREF:void:::
Py_INCREF:PyObject*:o:+1:
Py_NewRef:void:::
Py_NewRef:PyObject*:o:+1:
Py_Initialize:void:::
Py_IsInitialized:int:::
@ -3028,6 +3031,9 @@ Py_XDECREF:PyObject*:o:-1:if o is not NULL
Py_XINCREF:void:::
Py_XINCREF:PyObject*:o:+1:if o is not NULL
Py_XNewRef:void:::
Py_XNewRef:PyObject*:o:+1:if o is not NULL
_PyImport_Fini:void:::
_PyObject_New:PyObject*::+1:

View File

@ -158,6 +158,18 @@ Glossary
See also :term:`text file` for a file object able to read and write
:class:`str` objects.
borrowed reference
In the Python's C API, a borrowed reference is a reference to an object.
It does not modify the object reference count. It becomes a dangling
pointer if the object is destroyed. For example, a garbage collection can
remove the last :term:`strong reference` to the object and so destroy it.
Calling :c:func:`Py_INCREF` on the :term:`borrowed reference` is
recommended to convert it to a :term:`strong reference` in-place, except
if the object cannot be destroyed before the last usage of the borrowed
reference. The :c:func:`Py_NewRef` function can be used to create a new
:term:`strong reference`.
bytes-like object
An object that supports the :ref:`bufferobjects` and can
export a C-:term:`contiguous` buffer. This includes all :class:`bytes`,
@ -301,7 +313,8 @@ Glossary
including functions, methods, properties, class methods, static methods,
and reference to super classes.
For more information about descriptors' methods, see :ref:`descriptors`.
For more information about descriptors' methods, see :ref:`descriptors`
or the :ref:`Descriptor How To Guide <descriptorhowto>`.
dictionary
An associative array, where arbitrary keys are mapped to values. The
@ -385,6 +398,25 @@ Glossary
file-like object
A synonym for :term:`file object`.
filesystem encoding and error handler
Encoding and error handler used by Python to decode bytes from the
operating system and encode Unicode to the operating system.
The filesystem encoding must guarantee to successfully decode all bytes
below 128. If the file system encoding fails to provide this guarantee,
API functions can raise :exc:`UnicodeError`.
The :func:`sys.getfilesystemencoding` and
:func:`sys.getfilesystemencodeerrors` functions can be used to get the
filesystem encoding and error handler.
The :term:`filesystem encoding and error handler` are configured at
Python startup by the :c:func:`PyConfig_Read` function: see
:c:member:`~PyConfig.filesystem_encoding` and
:c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
See also the :term:`locale encoding`.
finder
An object that tries to find the :term:`loader` for a module that is
being imported.
@ -482,6 +514,13 @@ Glossary
See also the :term:`single dispatch` glossary entry, the
:func:`functools.singledispatch` decorator, and :pep:`443`.
generic type
A :term:`type` that can be parameterized; typically a container like
:class:`list`. Used for :term:`type hints <type hint>` and
:term:`annotations <annotation>`.
See :pep:`483` for more details, and :mod:`typing` or
:ref:`generic alias type <types-genericalias>` for its uses.
GIL
See :term:`global interpreter lock`.
@ -665,6 +704,18 @@ Glossary
thread removes *key* from *mapping* after the test, but before the lookup.
This issue can be solved with locks or by using the EAFP approach.
locale encoding
On Unix, it is the encoding of the LC_CTYPE locale. It can be set with
``locale.setlocale(locale.LC_CTYPE, new_locale)``.
On Windows, it is is the ANSI code page (ex: ``cp1252``).
``locale.getpreferredencoding(False)`` can be used to get the locale
encoding.
Python uses the :term:`filesystem encoding and error handler` to convert
between Unicode filenames and bytes filenames.
list
A built-in Python :term:`sequence`. Despite its name it is more akin
to an array in other languages than to a linked list since access to
@ -1030,7 +1081,7 @@ Glossary
:meth:`index`, :meth:`__contains__`, and
:meth:`__reversed__`. Types that implement this expanded
interface can be registered explicitly using
:func:`~abc.register`.
:func:`~abc.ABCMeta.register`.
set comprehension
A compact way to process all or part of the elements in an iterable and
@ -1061,6 +1112,18 @@ Glossary
an :term:`expression` or one of several constructs with a keyword, such
as :keyword:`if`, :keyword:`while` or :keyword:`for`.
strong reference
In the Python's C API, a strong reference is a reference to an object
which increments object reference count when it is created and
decrements the object reference count when it is deleted.
The :c:func:`Py_NewRef` function can be used to create a strong reference
to an object. Usually, the :c:func:`Py_DECREF` function must be called on
the strong reference before exiting the scope of the strong reference, to
avoid leaking one reference.
See also :term:`borrowed reference`.
text encoding
A codec which encodes Unicode strings to bytes.

File diff suppressed because it is too large Load Diff

View File

@ -609,9 +609,9 @@ implemented by converting the Unicode string into some encoding that
varies depending on the system. Today Python is converging on using
UTF-8: Python on MacOS has used UTF-8 for several versions, and Python
3.6 switched to using UTF-8 on Windows as well. On Unix systems,
there will only be a filesystem encoding if you've set the ``LANG`` or
``LC_CTYPE`` environment variables; if you haven't, the default
encoding is again UTF-8.
there will only be a :term:`filesystem encoding <filesystem encoding and error
handler>`. if you've set the ``LANG`` or ``LC_CTYPE`` environment variables; if
you haven't, the default encoding is again UTF-8.
The :func:`sys.getfilesystemencoding` function returns the encoding to use on
your current system, in case you want to do the encoding manually, but there's
@ -633,8 +633,8 @@ provided the directory path as bytes or a Unicode string. If you pass a
Unicode string as the path, filenames will be decoded using the filesystem's
encoding and a list of Unicode strings will be returned, while passing a byte
path will return the filenames as bytes. For example,
assuming the default filesystem encoding is UTF-8, running the following
program::
assuming the default :term:`filesystem encoding <filesystem encoding and error
handler>` is UTF-8, running the following program::
fn = 'filename\u4500abc'
f = open(fn, 'w')

View File

@ -80,12 +80,13 @@ Node classes
end_col_offset
Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have
:attr:`lineno`, :attr:`col_offset`, :attr:`lineno`, and :attr:`col_offset`
attributes. The :attr:`lineno` and :attr:`end_lineno` are the first and
last line numbers of source text span (1-indexed so the first line is line 1)
and the :attr:`col_offset` and :attr:`end_col_offset` are the corresponding
UTF-8 byte offsets of the first and last tokens that generated the node.
The UTF-8 offset is recorded because the parser uses UTF-8 internally.
:attr:`lineno`, :attr:`col_offset`, :attr:`end_lineno`, and
:attr:`end_col_offset` attributes. The :attr:`lineno` and :attr:`end_lineno`
are the first and last line numbers of source text span (1-indexed so the
first line is line 1) and the :attr:`col_offset` and :attr:`end_col_offset`
are the corresponding UTF-8 byte offsets of the first and last tokens that
generated the node. The UTF-8 offset is recorded because the parser uses
UTF-8 internally.
Note that the end positions are not required by the compiler and are
therefore optional. The end offset is *after* the last symbol, for example

View File

@ -504,10 +504,10 @@ Waiting Primitives
return_when=ALL_COMPLETED)
Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
set concurrently and block until the condition specified
iterable concurrently and block until the condition specified
by *return_when*.
The *aws* set must not be empty.
The *aws* iterable must not be empty.
Returns two sets of Tasks/Futures: ``(done, pending)``.
@ -593,9 +593,9 @@ Waiting Primitives
.. function:: as_completed(aws, \*, loop=None, timeout=None)
Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
set concurrently. Return an iterator of coroutines.
iterable concurrently. Return an iterator of coroutines.
Each coroutine returned can be awaited to get the earliest next
result from the set of the remaining awaitables.
result from the iterable of the remaining awaitables.
Raises :exc:`asyncio.TimeoutError` if the timeout occurs before
all Futures are done.

View File

@ -236,9 +236,9 @@ to a :class:`ProcessPoolExecutor` will result in deadlock.
An :class:`Executor` subclass that executes calls asynchronously using a pool
of at most *max_workers* processes. If *max_workers* is ``None`` or not
given, it will default to the number of processors on the machine.
If *max_workers* is lower or equal to ``0``, then a :exc:`ValueError`
If *max_workers* is less than or equal to ``0``, then a :exc:`ValueError`
will be raised.
On Windows, *max_workers* must be equal or lower than ``61``. If it is not
On Windows, *max_workers* must be less than or equal to ``61``. If it is not
then :exc:`ValueError` will be raised. If *max_workers* is ``None``, then
the default chosen will be at most ``61``, even if more processors are
available.
@ -250,7 +250,7 @@ to a :class:`ProcessPoolExecutor` will result in deadlock.
each worker process; *initargs* is a tuple of arguments passed to the
initializer. Should *initializer* raise an exception, all currently
pending jobs will raise a :exc:`~concurrent.futures.process.BrokenProcessPool`,
as well any attempt to submit more jobs to the pool.
as well as any attempt to submit more jobs to the pool.
.. versionchanged:: 3.3
When one of the worker processes terminates abruptly, a

View File

@ -126,6 +126,31 @@ Functions and classes provided:
.. versionadded:: 3.7
Context managers defined with :func:`asynccontextmanager` can be used
either as decorators or with :keyword:`async with` statements::
import time
async def timeit():
now = time.monotonic()
try:
yield
finally:
print(f'it took {time.monotonic() - now}s to run')
@timeit()
async def main():
# ... async code ...
When used as a decorator, a new generator instance is implicitly created on
each function call. This allows the otherwise "one-shot" context managers
created by :func:`asynccontextmanager` to meet the requirement that context
managers support multiple invocations in order to be used as decorators.
.. versionchanged:: 3.10
Async context managers created with :func:`asynccontextmanager` can
be used as decorators.
.. function:: closing(thing)
@ -154,6 +179,39 @@ Functions and classes provided:
``page.close()`` will be called when the :keyword:`with` block is exited.
.. class:: aclosing(thing)
Return an async context manager that calls the ``aclose()`` method of *thing*
upon completion of the block. This is basically equivalent to::
from contextlib import asynccontextmanager
@asynccontextmanager
async def aclosing(thing):
try:
yield thing
finally:
await thing.aclose()
Significantly, ``aclosing()`` supports deterministic cleanup of async
generators when they happen to exit early by :keyword:`break` or an
exception. For example::
from contextlib import aclosing
async with aclosing(my_generator()) as values:
async for value in values:
if value == 42:
break
This pattern ensures that the generator's async exit code is executed in
the same context as its iterations (so that exceptions and context
variables work as expected, and the exit code isn't run after the
lifetime of some task it depends on).
.. versionadded:: 3.10
.. _simplifying-support-for-single-optional-context-managers:
.. function:: nullcontext(enter_result=None)
@ -185,8 +243,26 @@ Functions and classes provided:
with cm as file:
# Perform processing on the file
It can also be used as a stand-in for
:ref:`asynchronous context managers <async-context-managers>`::
async def send_http(session=None):
if not session:
# If no http session, create it with aiohttp
cm = aiohttp.ClientSession()
else:
# Caller is responsible for closing the session
cm = nullcontext(session)
async with cm as session:
# Send http requests with session
.. versionadded:: 3.7
.. versionchanged:: 3.10
:term:`asynchronous context manager` support was added.
.. function:: suppress(*exceptions)
@ -351,6 +427,45 @@ Functions and classes provided:
.. versionadded:: 3.2
.. class:: AsyncContextDecorator
Similar to :class:`ContextDecorator` but only for asynchronous functions.
Example of ``AsyncContextDecorator``::
from asyncio import run
from contextlib import AsyncContextDecorator
class mycontext(AsyncContextDecorator):
async def __aenter__(self):
print('Starting')
return self
async def __aexit__(self, *exc):
print('Finishing')
return False
>>> @mycontext()
... async def function():
... print('The bit in the middle')
...
>>> run(function())
Starting
The bit in the middle
Finishing
>>> async def function():
... async with mycontext():
... print('The bit in the middle')
...
>>> run(function())
Starting
The bit in the middle
Finishing
.. versionadded:: 3.10
.. class:: ExitStack()
A context manager that is designed to make it easy to programmatically

View File

@ -93,6 +93,9 @@ The Python Development Mode does not prevent the :option:`-O` command line
option from removing :keyword:`assert` statements nor from setting
:const:`__debug__` to ``False``.
The Python Development Mode can only be enabled at the Python startup. Its
value can be read from :data:`sys.flags.dev_mode <sys.flags>`.
.. versionchanged:: 3.8
The :class:`io.IOBase` destructor now logs ``close()`` exceptions.

View File

@ -112,3 +112,6 @@ All defect classes are subclassed from :class:`email.errors.MessageDefect`.
* :class:`InvalidBase64LengthDefect` -- When decoding a block of base64 encoded
bytes, the number of non-padding base64 characters was invalid (1 more than
a multiple of 4). The encoded block was kept as-is.
* :class:`InvalidDateDefect` -- When decoding an invalid or unparsable date field.
The original value is kept as-is.

View File

@ -124,8 +124,10 @@ of the new API.
.. function:: parsedate_to_datetime(date)
The inverse of :func:`format_datetime`. Performs the same function as
:func:`parsedate`, but on success returns a :mod:`~datetime.datetime`. If
the input date has a timezone of ``-0000``, the ``datetime`` will be a naive
:func:`parsedate`, but on success returns a :mod:`~datetime.datetime`;
otherwise ``ValueError`` is raised if *date* contains an invalid value such
as an hour greater than 23 or a timezone offset not between -24 and 24 hours.
If the input date has a timezone of ``-0000``, the ``datetime`` will be a naive
``datetime``, and if the date is conforming to the RFCs it will represent a
time in UTC but with no indication of the actual source timezone of the
message the date comes from. If the input date has any other valid timezone

View File

@ -313,8 +313,8 @@ The following exceptions are the exceptions that are usually raised.
.. versionchanged:: 3.4
The :attr:`filename` attribute is now the original file name passed to
the function, instead of the name encoded to or decoded from the
filesystem encoding. Also, the *filename2* constructor argument and
attribute was added.
:term:`filesystem encoding and error handler`. Also, the *filename2*
constructor argument and attribute was added.
.. exception:: OverflowError

View File

@ -164,8 +164,8 @@ are always available. They are listed here in alphabetical order.
* If it is an *integer*, the array will have that size and will be
initialized with null bytes.
* If it is an object conforming to the *buffer* interface, a read-only buffer
of the object will be used to initialize the bytes array.
* If it is an object conforming to the :ref:`buffer interface <bufferobjects>`,
a read-only buffer of the object will be used to initialize the bytes array.
* If it is an *iterable*, it must be an iterable of integers in the range
``0 <= x < 256``, which are used as the initial contents of the array.
@ -259,7 +259,7 @@ are always available. They are listed here in alphabetical order.
interactive statement (in the latter case, expression statements that
evaluate to something other than ``None`` will be printed).
The optional argument *flags* and *dont_inherit* controls which
The optional arguments *flags* and *dont_inherit* control which
:ref:`compiler options <ast-compiler-flags>` should be activated
and which :ref:`future features <future>` should be allowed. If neither
is present (or both are zero) the code is compiled with the same flags that

View File

@ -73,17 +73,32 @@ The :mod:`functools` module defines the following functions:
def variance(self):
return statistics.variance(self._data)
Note, this decorator interferes with the operation of :pep:`412`
key-sharing dictionaries. This means that instance dictionaries
can take more space than usual.
Also, this decorator requires that the ``__dict__`` attribute on each instance
be a mutable mapping. This means it will not work with some types, such as
metaclasses (since the ``__dict__`` attributes on type instances are
read-only proxies for the class namespace), and those that specify
``__slots__`` without including ``__dict__`` as one of the defined slots
(as such classes don't provide a ``__dict__`` attribute at all).
If a mutable mapping is not available or if space-efficient key sharing
is desired, an effect similar to :func:`cached_property` can be achieved
by a stacking :func:`property` on top of :func:`cache`::
class DataSet:
def __init__(self, sequence_of_numbers):
self._data = sequence_of_numbers
@property
@cache
def stdev(self):
return statistics.stdev(self._data)
.. versionadded:: 3.8
.. note::
This decorator requires that the ``__dict__`` attribute on each instance
be a mutable mapping. This means it will not work with some types, such as
metaclasses (since the ``__dict__`` attributes on type instances are
read-only proxies for the class namespace), and those that specify
``__slots__`` without including ``__dict__`` as one of the defined slots
(as such classes don't provide a ``__dict__`` attribute at all).
.. function:: cmp_to_key(func)

View File

@ -202,6 +202,32 @@ write code that handles both IP versions correctly. Address objects are
.. _iana-ipv4-special-registry: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
.. _iana-ipv6-special-registry: https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
.. method:: IPv4Address.__format__(fmt)
Returns a string representation of the IP address, controlled by
an explicit format string.
*fmt* can be one of the following: ``'s'``, the default option,
equivalent to :func:`str`, ``'b'`` for a zero-padded binary string,
``'X'`` or ``'x'`` for an uppercase or lowercase hexadecimal
representation, or ``'n'``, which is equivalent to ``'b'`` for IPv4
addresses and ``'x'`` for IPv6. For binary and hexadecimal
representations, the form specifier ``'#'`` and the grouping option
``'_'`` are available. ``__format__`` is used by ``format``, ``str.format``
and f-strings.
>>> format(ipaddress.IPv4Address('192.168.0.1'))
'192.168.0.1'
>>> '{:#b}'.format(ipaddress.IPv4Address('192.168.0.1'))
'0b11000000101010000000000000000001'
>>> f'{ipaddress.IPv6Address("2001:db8::1000"):s}'
'2001:db8::1000'
>>> format(ipaddress.IPv6Address('2001:db8::1000'), '_X')
'2001_0DB8_0000_0000_0000_0000_0000_1000'
>>> '{:#_n}'.format(ipaddress.IPv6Address('2001:db8::1000'))
'0x2001_0db8_0000_0000_0000_0000_0000_1000'
.. versionadded:: 3.9
.. class:: IPv6Address(address)
@ -246,8 +272,8 @@ write code that handles both IP versions correctly. Address objects are
groups consisting entirely of zeroes included.
For the following attributes, see the corresponding documentation of the
:class:`IPv4Address` class:
For the following attributes and methods, see the corresponding
documentation of the :class:`IPv4Address` class:
.. attribute:: packed
.. attribute:: reverse_pointer
@ -297,6 +323,12 @@ write code that handles both IP versions correctly. Address objects are
the embedded ``(server, client)`` IP address pair. For any other
address, this property will be ``None``.
.. method:: IPv6Address.__format__(fmt)
Refer to the corresponding method documentation in
:class:`IPv4Address`.
.. versionadded:: 3.9
Conversion to Strings and Integers
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -315,21 +315,25 @@ The :mod:`locale` module defines the following exception and functions:
.. function:: getpreferredencoding(do_setlocale=True)
Return the encoding used for text data, according to user preferences. User
preferences are expressed differently on different systems, and might not be
available programmatically on some systems, so this function only returns a
guess.
Return the :term:`locale encoding` used for text data, according to user
preferences. User preferences are expressed differently on different
systems, and might not be available programmatically on some systems, so
this function only returns a guess.
On some systems, it is necessary to invoke :func:`setlocale` to obtain the user
preferences, so this function is not thread-safe. If invoking setlocale is not
necessary or desired, *do_setlocale* should be set to ``False``.
On some systems, it is necessary to invoke :func:`setlocale` to obtain the
user preferences, so this function is not thread-safe. If invoking setlocale
is not necessary or desired, *do_setlocale* should be set to ``False``.
On Android or in the UTF-8 mode (:option:`-X` ``utf8`` option), always
return ``'UTF-8'``, the locale and the *do_setlocale* argument are ignored.
On Android or if the :ref:`Python UTF-8 Mode <utf8-mode>` is enabled, always
return ``'UTF-8'``, the :term:`locale encoding` and the *do_setlocale*
argument are ignored.
The :ref:`Python preinitialization <c-preinit>` configures the LC_CTYPE
locale. See also the :term:`filesystem encoding and error handler`.
.. versionchanged:: 3.7
The function now always returns ``UTF-8`` on Android or if the UTF-8 mode
is enabled.
The function now always returns ``UTF-8`` on Android or if the
:ref:`Python UTF-8 Mode <utf8-mode>` is enabled.
.. function:: normalize(localename)

View File

@ -68,8 +68,13 @@ File Names, Command Line Arguments, and Environment Variables
In Python, file names, command line arguments, and environment variables are
represented using the string type. On some systems, decoding these strings to
and from bytes is necessary before passing them to the operating system. Python
uses the file system encoding to perform this conversion (see
:func:`sys.getfilesystemencoding`).
uses the :term:`filesystem encoding and error handler` to perform this
conversion (see :func:`sys.getfilesystemencoding`).
The :term:`filesystem encoding and error handler` are configured at Python
startup by the :c:func:`PyConfig_Read` function: see
:c:member:`~PyConfig.filesystem_encoding` and
:c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
.. versionchanged:: 3.1
On some systems, conversion using the file system encoding may fail. In this
@ -79,9 +84,72 @@ uses the file system encoding to perform this conversion (see
original byte on encoding.
The file system encoding must guarantee to successfully decode all bytes
below 128. If the file system encoding fails to provide this guarantee, API
functions may raise UnicodeErrors.
The :term:`file system encoding <filesystem encoding and error handler>` must
guarantee to successfully decode all bytes below 128. If the file system
encoding fails to provide this guarantee, API functions can raise
:exc:`UnicodeError`.
See also the :term:`locale encoding`.
.. _utf8-mode:
Python UTF-8 Mode
-----------------
.. versionadded:: 3.7
See :pep:`540` for more details.
The Python UTF-8 Mode ignores the :term:`locale encoding` and forces the usage
of the UTF-8 encoding:
* Use UTF-8 as the :term:`filesystem encoding <filesystem encoding and error
handler>`.
* :func:`sys.getfilesystemencoding()` returns ``'UTF-8'``.
* :func:`locale.getpreferredencoding()` returns ``'UTF-8'`` (the *do_setlocale*
argument has no effect).
* :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr` all use
UTF-8 as their text encoding, with the ``surrogateescape``
:ref:`error handler <error-handlers>` being enabled for :data:`sys.stdin`
and :data:`sys.stdout` (:data:`sys.stderr` continues to use
``backslashreplace`` as it does in the default locale-aware mode)
* On Unix, :func:`os.device_encoding` returns ``'UTF-8'``. rather than the
device encoding.
Note that the standard stream settings in UTF-8 mode can be overridden by
:envvar:`PYTHONIOENCODING` (just as they can be in the default locale-aware
mode).
As a consequence of the changes in those lower level APIs, other higher
level APIs also exhibit different default behaviours:
* Command line arguments, environment variables and filenames are decoded
to text using the UTF-8 encoding.
* :func:`os.fsdecode()` and :func:`os.fsencode()` use the UTF-8 encoding.
* :func:`open()`, :func:`io.open()`, and :func:`codecs.open()` use the UTF-8
encoding by default. However, they still use the strict error handler by
default so that attempting to open a binary file in text mode is likely
to raise an exception rather than producing nonsense data.
The :ref:`Python UTF-8 Mode <utf8-mode>` is enabled if the LC_CTYPE locale is
``C`` or ``POSIX`` at Python startup (see the :c:func:`PyConfig_Read`
function).
It can be enabled or disabled using the :option:`-X utf8 <-X>` command line
option and the :envvar:`PYTHONUTF8` environment variable.
If the :envvar:`PYTHONUTF8` environment variable is not set at all, then the
interpreter defaults to using the current locale settings, *unless* the current
locale is identified as a legacy ASCII-based locale (as described for
:envvar:`PYTHONCOERCECLOCALE`), and locale coercion is either disabled or
fails. In such legacy locales, the interpreter will default to enabling UTF-8
mode unless explicitly instructed not to do so.
The Python UTF-8 Mode can only be enabled at the Python startup. Its value
can be read from :data:`sys.flags.utf8_mode <sys.flags>`.
See also the :ref:`UTF-8 mode on Windows <win-utf8-mode>`
and the :term:`filesystem encoding and error handler`.
.. _os-procinfo:
@ -165,9 +233,9 @@ process and user.
.. function:: fsencode(filename)
Encode :term:`path-like <path-like object>` *filename* to the filesystem
encoding with ``'surrogateescape'`` error handler, or ``'strict'`` on
Windows; return :class:`bytes` unchanged.
Encode :term:`path-like <path-like object>` *filename* to the
:term:`filesystem encoding and error handler`; return :class:`bytes`
unchanged.
:func:`fsdecode` is the reverse function.
@ -181,8 +249,8 @@ process and user.
.. function:: fsdecode(filename)
Decode the :term:`path-like <path-like object>` *filename* from the
filesystem encoding with ``'surrogateescape'`` error handler, or ``'strict'``
on Windows; return :class:`str` unchanged.
:term:`filesystem encoding and error handler`; return :class:`str`
unchanged.
:func:`fsencode` is the reverse function.
@ -742,6 +810,12 @@ as internal buffering of data.
Return a string describing the encoding of the device associated with *fd*
if it is connected to a terminal; else return :const:`None`.
On Unix, if the :ref:`Python UTF-8 Mode <utf8-mode>` is enabled, return
``'UTF-8'`` rather than the device encoding.
.. versionchanged:: 3.10
On Unix, the function now implements the Python UTF-8 Mode.
.. function:: dup(fd)
@ -3246,7 +3320,7 @@ These functions are all available on Linux only.
Removes the extended filesystem attribute *attribute* from *path*.
*attribute* should be bytes or str (directly or indirectly through the
:class:`PathLike` interface). If it is a string, it is encoded
with the filesystem encoding.
with the :term:`filesystem encoding and error handler`.
This function can support :ref:`specifying a file descriptor <path_fd>` and
:ref:`not following symlinks <follow_symlinks>`.
@ -3262,7 +3336,7 @@ These functions are all available on Linux only.
Set the extended filesystem attribute *attribute* on *path* to *value*.
*attribute* must be a bytes or str with no embedded NULs (directly or
indirectly through the :class:`PathLike` interface). If it is a str,
it is encoded with the filesystem encoding. *flags* may be
it is encoded with the :term:`filesystem encoding and error handler`. *flags* may be
:data:`XATTR_REPLACE` or :data:`XATTR_CREATE`. If :data:`XATTR_REPLACE` is
given and the attribute does not exist, ``EEXISTS`` will be raised.
If :data:`XATTR_CREATE` is given and the attribute already exists, the

View File

@ -209,13 +209,6 @@ Windows Platform
which means the OS version uses debugging code, i.e. code that checks arguments,
ranges, etc.
.. note::
This function works best with Mark Hammond's
:mod:`win32all` package installed, but also on Python 2.3 and
later (support for this was added in Python 2.6). It obviously
only runs on Win32 compatible platforms.
.. function:: win32_edition()
Returns a string representing the current Windows edition. Possible

View File

@ -37,7 +37,7 @@ Large File Support
.. sectionauthor:: Steve Clift <clift@mail.anacapa.net>
Several operating systems (including AIX, HP-UX, Irix and Solaris) provide
Several operating systems (including AIX, HP-UX and Solaris) provide
support for files that are larger than 2 GiB from a C programming model where
:c:type:`int` and :c:type:`long` are 32-bit values. This is typically accomplished
by defining the relevant size and offset types as 64-bit values. Such files are
@ -47,8 +47,8 @@ Large file support is enabled in Python when the size of an :c:type:`off_t` is
larger than a :c:type:`long` and the :c:type:`long long` is at least as large
as an :c:type:`off_t`.
It may be necessary to configure and compile Python with certain compiler flags
to enable this mode. For example, it is enabled by default with recent versions
of Irix, but with Solaris 2.6 and 2.7 you need to do something like::
to enable this mode. For example, with Solaris 2.6 and 2.7 you need to do
something like::
CFLAGS="`getconf LFS_CFLAGS`" OPT="-g -O2 $CFLAGS" \
./configure

View File

@ -319,6 +319,13 @@ be found in any statistics text.
deviation. This is slightly faster than the :func:`normalvariate` function
defined below.
Multithreading note: When two threads call this function
simultaneously, it is possible that they will receive the
same return value. This can be avoided in three ways.
1) Have each thread use a different instance of the random
number generator. 2) Put locks around all calls. 3) Use the
slower, but thread-safe :func:`normalvariate` function instead.
.. function:: lognormvariate(mu, sigma)

View File

@ -25,8 +25,9 @@ lots of shared sub-objects. The keys are ordinary strings.
database file is opened for reading and writing. The optional *flag* parameter
has the same interpretation as the *flag* parameter of :func:`dbm.open`.
By default, version 3 pickles are used to serialize values. The version of the
pickle protocol can be specified with the *protocol* parameter.
By default, pickles created with :data:`pickle.DEFAULT_PROTOCOL` are used
to serialize values. The version of the pickle protocol can be specified
with the *protocol* parameter.
Because of Python semantics, a shelf cannot know when a mutable
persistent-dictionary entry is modified. By default modified objects are
@ -40,6 +41,10 @@ lots of shared sub-objects. The keys are ordinary strings.
determine which accessed entries are mutable, nor which ones were actually
mutated).
.. versionchanged:: 3.10
:data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle
protocol.
.. note::
Do not rely on the shelf being closed automatically; always call
@ -108,9 +113,10 @@ Restrictions
A subclass of :class:`collections.abc.MutableMapping` which stores pickled
values in the *dict* object.
By default, version 3 pickles are used to serialize values. The version of the
pickle protocol can be specified with the *protocol* parameter. See the
:mod:`pickle` documentation for a discussion of the pickle protocols.
By default, pickles created with :data:`pickle.DEFAULT_PROTOCOL` are used
to serialize values. The version of the pickle protocol can be specified
with the *protocol* parameter. See the :mod:`pickle` documentation for a
discussion of the pickle protocols.
If the *writeback* parameter is ``True``, the object will hold a cache of all
entries accessed and write them back to the *dict* at sync and close times.
@ -130,6 +136,10 @@ Restrictions
.. versionchanged:: 3.4
Added context manager support.
.. versionchanged:: 3.10
:data:`pickle.DEFAULT_PROTOCOL` is now used as the default pickle
protocol.
.. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')

View File

@ -4763,25 +4763,231 @@ define these methods must provide them as a normal Python accessible method.
Compared to the overhead of setting up the runtime context, the overhead of a
single class dictionary lookup is negligible.
Type Annotation Types --- :ref:`Generic Alias <types-genericalias>`, :ref:`Union <types-union>`
===============================================================================================
.. index::
single: annotation; type annotation; type hint
The core built-in types for :term:`type annotations <annotation>` are
:ref:`Generic Alias <types-genericalias>` and :ref:`Union <types-union>`.
.. _types-genericalias:
Generic Alias Type
------------------
.. index::
object: GenericAlias
pair: Generic; Alias
``GenericAlias`` objects are created by subscripting a class (usually a
container), such as ``list[int]``. They are intended primarily for
:term:`type annotations <annotation>`.
Usually, the :ref:`subscription <subscriptions>` of container objects calls the
method :meth:`__getitem__` of the object. However, the subscription of some
containers' classes may call the classmethod :meth:`__class_getitem__` of the
class instead. The classmethod :meth:`__class_getitem__` should return a
``GenericAlias`` object.
.. note::
If the :meth:`__getitem__` of the class' metaclass is present, it will take
precedence over the :meth:`__class_getitem__` defined in the class (see
:pep:`560` for more details).
The ``GenericAlias`` object acts as a proxy for :term:`generic types
<generic type>`, implementing *parameterized generics* - a specific instance
of a generic which provides the types for container elements.
The user-exposed type for the ``GenericAlias`` object can be accessed from
:class:`types.GenericAlias` and used for :func:`isinstance` checks. It can
also be used to create ``GenericAlias`` objects directly.
.. describe:: T[X, Y, ...]
Creates a ``GenericAlias`` representing a type ``T`` containing elements
of types *X*, *Y*, and more depending on the ``T`` used.
For example, a function expecting a :class:`list` containing
:class:`float` elements::
def average(values: list[float]) -> float:
return sum(values) / len(values)
Another example for :term:`mapping` objects, using a :class:`dict`, which
is a generic type expecting two type parameters representing the key type
and the value type. In this example, the function expects a ``dict`` with
keys of type :class:`str` and values of type :class:`int`::
def send_post_request(url: str, body: dict[str, int]) -> None:
...
The builtin functions :func:`isinstance` and :func:`issubclass` do not accept
``GenericAlias`` types for their second argument::
>>> isinstance([1, 2], list[str])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() argument 2 cannot be a parameterized generic
The Python runtime does not enforce :term:`type annotations <annotation>`.
This extends to generic types and their type parameters. When creating
an object from a ``GenericAlias``, container elements are not checked
against their type. For example, the following code is discouraged, but will
run without errors::
>>> t = list[str]
>>> t([1, 2, 3])
[1, 2, 3]
Furthermore, parameterized generics erase type parameters during object
creation::
>>> t = list[str]
>>> type(t)
<class 'types.GenericAlias'>
>>> l = t()
>>> type(l)
<class 'list'>
Calling :func:`repr` or :func:`str` on a generic shows the parameterized type::
>>> repr(list[int])
'list[int]'
>>> str(list[int])
'list[int]'
The :meth:`__getitem__` method of generics will raise an exception to disallow
mistakes like ``dict[str][str]``::
>>> dict[str][str]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: There are no type variables left in dict[str]
However, such expressions are valid when :ref:`type variables <generics>` are
used. The index must have as many elements as there are type variable items
in the ``GenericAlias`` object's :attr:`__args__ <genericalias.__args__>`. ::
>>> from typing import TypeVar
>>> Y = TypeVar('Y')
>>> dict[str, Y][int]
dict[str, int]
Standard Generic Collections
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
These standard library collections support parameterized generics.
* :class:`tuple`
* :class:`list`
* :class:`dict`
* :class:`set`
* :class:`frozenset`
* :class:`type`
* :class:`collections.deque`
* :class:`collections.defaultdict`
* :class:`collections.OrderedDict`
* :class:`collections.Counter`
* :class:`collections.ChainMap`
* :class:`collections.abc.Awaitable`
* :class:`collections.abc.Coroutine`
* :class:`collections.abc.AsyncIterable`
* :class:`collections.abc.AsyncIterator`
* :class:`collections.abc.AsyncGenerator`
* :class:`collections.abc.Iterable`
* :class:`collections.abc.Iterator`
* :class:`collections.abc.Generator`
* :class:`collections.abc.Reversible`
* :class:`collections.abc.Container`
* :class:`collections.abc.Collection`
* :class:`collections.abc.Callable`
* :class:`collections.abc.Set`
* :class:`collections.abc.MutableSet`
* :class:`collections.abc.Mapping`
* :class:`collections.abc.MutableMapping`
* :class:`collections.abc.Sequence`
* :class:`collections.abc.MutableSequence`
* :class:`collections.abc.ByteString`
* :class:`collections.abc.MappingView`
* :class:`collections.abc.KeysView`
* :class:`collections.abc.ItemsView`
* :class:`collections.abc.ValuesView`
* :class:`contextlib.AbstractContextManager`
* :class:`contextlib.AbstractAsyncContextManager`
* :ref:`re.Pattern <re-objects>`
* :ref:`re.Match <match-objects>`
Special Attributes of Generic Alias
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
All parameterized generics implement special read-only attributes.
.. attribute:: genericalias.__origin__
This attribute points at the non-parameterized generic class::
>>> list[int].__origin__
<class 'list'>
.. attribute:: genericalias.__args__
This attribute is a :class:`tuple` (possibly of length 1) of generic
types passed to the original :meth:`__class_getitem__`
of the generic container::
>>> dict[str, list[int]].__args__
(<class 'str'>, list[int])
.. attribute:: genericalias.__parameters__
This attribute is a lazily computed tuple (possibly empty) of unique type
variables found in ``__args__``::
>>> from typing import TypeVar
>>> T = TypeVar('T')
>>> list[T].__parameters__
(~T,)
.. seealso::
* :pep:`585` -- "Type Hinting Generics In Standard Collections"
* :meth:`__class_getitem__` -- Used to implement parameterized generics.
* :ref:`generics` -- Generics in the :mod:`typing` module.
.. versionadded:: 3.9
.. _types-union:
Union Type
==========
----------
.. index::
object: Union
pair: union; type
A union object holds the value of the ``|`` (bitwise or) operation on
multiple :ref:`type objects<bltin-type-objects>`. These types are intended
primarily for type annotations. The union type expression enables cleaner
type hinting syntax compared to :data:`typing.Union`.
multiple :ref:`type objects <bltin-type-objects>`. These types are intended
primarily for :term:`type annotations <annotation>`. The union type expression
enables cleaner type hinting syntax compared to :data:`typing.Union`.
.. describe:: X | Y | ...
Defines a union object which holds types *X*, *Y*, and so forth. ``X | Y``
means either X or Y. It is equivalent to ``typing.Union[X, Y]``.
Example::
For example, the following function expects an argument of type
:class:`int` or :class:`float`::
def square(number: int | float) -> int | float:
return number ** 2
@ -4790,15 +4996,15 @@ type hinting syntax compared to :data:`typing.Union`.
Union objects can be tested for equality with other union objects. Details:
* Unions of unions are flattened, e.g.::
* Unions of unions are flattened::
(int | str) | float == int | str | float
* Redundant types are removed, e.g.::
* Redundant types are removed::
int | str | int == int | str
* When comparing unions, the order is ignored, e.g.::
* When comparing unions, the order is ignored::
int | str == str | int
@ -4817,14 +5023,8 @@ type hinting syntax compared to :data:`typing.Union`.
>>> isinstance("", int | str)
True
..
At the time of writing this, there is no documentation for parameterized
generics or PEP 585. Thus the link currently points to PEP 585 itself.
Please change the link for parameterized generics to reference the correct
documentation once documentation for PEP 585 becomes available.
However, union objects containing `parameterized generics
<https://www.python.org/dev/peps/pep-0585/>`_ cannot be used::
However, union objects containing :ref:`parameterized generics
<types-genericalias>` cannot be used::
>>> isinstance(1, int | list[int])
Traceback (most recent call last):
@ -4838,20 +5038,16 @@ type hinting syntax compared to :data:`typing.Union`.
>>> issubclass(bool, int | str)
True
..
Once again, please change the link below for parameterized generics to
reference the correct documentation once documentation for PEP 585
becomes available.
However, union objects containing `parameterized generics
<https://www.python.org/dev/peps/pep-0585/>`_ cannot be used::
However, union objects containing :ref:`parameterized generics
<types-genericalias>` cannot be used::
>>> issubclass(bool, bool | list[str])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: issubclass() argument 2 cannot contain a parameterized generic
The type of a union object is :data:`types.Union`. An object cannot be
The user-exposed type for the union object can be accessed from
:data:`types.Union` and used for :func:`isinstance` checks. An object cannot be
instantiated from the type::
>>> import types

View File

@ -196,6 +196,18 @@ always available.
.. audit-event:: sys._current_frames "" sys._current_frames
.. function:: _current_exceptions()
Return a dictionary mapping each thread's identifier to the topmost exception
currently active in that thread at the time the function is called.
If a thread is not currently handling an exception, it is not included in
the result dictionary.
This is most useful for statistical profiling.
This function should be used for internal and specialized purposes only.
.. audit-event:: sys._current_exceptions "" sys._current_exceptions
.. function:: breakpointhook()
@ -615,30 +627,24 @@ always available.
.. function:: getfilesystemencoding()
Return the name of the encoding used to convert between Unicode
filenames and bytes filenames. For best compatibility, str should be
used for filenames in all cases, although representing filenames as bytes
is also supported. Functions accepting or returning filenames should support
either str or bytes and internally convert to the system's preferred
representation.
Get the :term:`filesystem encoding <filesystem encoding and error handler>`:
the encoding used with the :term:`filesystem error handler <filesystem
encoding and error handler>` to convert between Unicode filenames and bytes
filenames. The filesystem error handler is returned from
:func:`getfilesystemencoding`.
This encoding is always ASCII-compatible.
For best compatibility, str should be used for filenames in all cases,
although representing filenames as bytes is also supported. Functions
accepting or returning filenames should support either str or bytes and
internally convert to the system's preferred representation.
:func:`os.fsencode` and :func:`os.fsdecode` should be used to ensure that
the correct encoding and errors mode are used.
* In the UTF-8 mode, the encoding is ``utf-8`` on any platform.
* On macOS, the encoding is ``'utf-8'``.
* On Unix, the encoding is the locale encoding.
* On Windows, the encoding may be ``'utf-8'`` or ``'mbcs'``, depending
on user configuration.
* On Android, the encoding is ``'utf-8'``.
* On VxWorks, the encoding is ``'utf-8'``.
The :term:`filesystem encoding and error handler` are configured at Python
startup by the :c:func:`PyConfig_Read` function: see
:c:member:`~PyConfig.filesystem_encoding` and
:c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
.. versionchanged:: 3.2
:func:`getfilesystemencoding` result cannot be ``None`` anymore.
@ -648,18 +654,26 @@ always available.
and :func:`_enablelegacywindowsfsencoding` for more information.
.. versionchanged:: 3.7
Return 'utf-8' in the UTF-8 mode.
Return ``'utf-8'`` if the :ref:`Python UTF-8 Mode <utf8-mode>` is
enabled.
.. function:: getfilesystemencodeerrors()
Return the name of the error mode used to convert between Unicode filenames
and bytes filenames. The encoding name is returned from
Get the :term:`filesystem error handler <filesystem encoding and error
handler>`: the error handler used with the :term:`filesystem encoding
<filesystem encoding and error handler>` to convert between Unicode
filenames and bytes filenames. The filesystem encoding is returned from
:func:`getfilesystemencoding`.
:func:`os.fsencode` and :func:`os.fsdecode` should be used to ensure that
the correct encoding and errors mode are used.
The :term:`filesystem encoding and error handler` are configured at Python
startup by the :c:func:`PyConfig_Read` function: see
:c:member:`~PyConfig.filesystem_encoding` and
:c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
.. versionadded:: 3.6
.. function:: getrefcount(object)
@ -1451,12 +1465,16 @@ always available.
.. function:: _enablelegacywindowsfsencoding()
Changes the default filesystem encoding and errors mode to 'mbcs' and
'replace' respectively, for consistency with versions of Python prior to 3.6.
Changes the :term:`filesystem encoding and error handler` to 'mbcs' and
'replace' respectively, for consistency with versions of Python prior to
3.6.
This is equivalent to defining the :envvar:`PYTHONLEGACYWINDOWSFSENCODING`
environment variable before launching Python.
See also :func:`sys.getfilesystemencoding` and
:func:`sys.getfilesystemencodeerrors`.
.. availability:: Windows.
.. versionadded:: 3.6
@ -1479,9 +1497,8 @@ always available.
returned by the :func:`open` function. Their parameters are chosen as
follows:
* The character encoding is platform-dependent. Non-Windows
platforms use the locale encoding (see
:meth:`locale.getpreferredencoding()`).
* The encoding and error handling are is initialized from
:c:member:`PyConfig.stdio_encoding` and :c:member:`PyConfig.stdio_errors`.
On Windows, UTF-8 is used for the console device. Non-character
devices such as disk files and pipes use the system locale
@ -1489,7 +1506,7 @@ always available.
devices such as NUL (i.e. where ``isatty()`` returns ``True``) use the
value of the console input and output codepages at startup,
respectively for stdin and stdout/stderr. This defaults to the
system locale encoding if the process is not initially attached
system :term:`locale encoding` if the process is not initially attached
to a console.
The special behaviour of the console can be overridden

View File

@ -121,6 +121,17 @@ This module defines the following functions:
:meth:`~Thread.run` method is called.
.. function:: gettrace()
.. index::
single: trace function
single: debugger
Get the trace function as set by :func:`settrace`.
.. versionadded:: 3.10
.. function:: setprofile(func)
.. index:: single: profile function
@ -130,6 +141,15 @@ This module defines the following functions:
:meth:`~Thread.run` method is called.
.. function:: getprofile()
.. index:: single: profile function
Get the profiler function as set by :func:`setprofile`.
.. versionadded:: 3.10
.. function:: stack_size([size])
Return the thread stack size used when creating new threads. The optional

View File

@ -36,7 +36,8 @@ The module defines the following functions:
Added negative *limit* support.
.. function:: print_exception(etype, value, tb, limit=None, file=None, chain=True)
.. function:: print_exception(exc, /[, value, tb], limit=None, \
file=None, chain=True)
Print exception information and stack trace entries from traceback object
*tb* to *file*. This differs from :func:`print_tb` in the following
@ -45,7 +46,7 @@ The module defines the following functions:
* if *tb* is not ``None``, it prints a header ``Traceback (most recent
call last):``
* it prints the exception *etype* and *value* after the stack trace
* it prints the exception type and *value* after the stack trace
.. index:: single: ^ (caret); marker
@ -53,6 +54,10 @@ The module defines the following functions:
format, it prints the line where the syntax error occurred with a caret
indicating the approximate position of the error.
Since Python 3.10, instead of passing *value* and *tb*, an exception object
can be passed as the first argument. If *value* and *tb* are provided, the
first argument is ignored in order to provide backwards compatibility.
The optional *limit* argument has the same meaning as for :func:`print_tb`.
If *chain* is true (the default), then chained exceptions (the
:attr:`__cause__` or :attr:`__context__` attributes of the exception) will be
@ -62,6 +67,10 @@ The module defines the following functions:
.. versionchanged:: 3.5
The *etype* argument is ignored and inferred from the type of *value*.
.. versionchanged:: 3.10
The *etype* parameter has been renamed to *exc* and is now
positional-only.
.. function:: print_exc(limit=None, file=None, chain=True)
@ -121,18 +130,26 @@ The module defines the following functions:
text line is not ``None``.
.. function:: format_exception_only(etype, value)
.. function:: format_exception_only(exc, /[, value])
Format the exception part of a traceback. The arguments are the exception
type and value such as given by ``sys.last_type`` and ``sys.last_value``.
The return value is a list of strings, each ending in a newline. Normally,
the list contains a single string; however, for :exc:`SyntaxError`
exceptions, it contains several lines that (when printed) display detailed
information about where the syntax error occurred. The message indicating
which exception occurred is the always last string in the list.
Format the exception part of a traceback using an exception value such as
given by ``sys.last_value``. The return value is a list of strings, each
ending in a newline. Normally, the list contains a single string; however,
for :exc:`SyntaxError` exceptions, it contains several lines that (when
printed) display detailed information about where the syntax error occurred.
The message indicating which exception occurred is the always last string in
the list.
Since Python 3.10, instead of passing *value*, an exception object
can be passed as the first argument. If *value* is provided, the first
argument is ignored in order to provide backwards compatibility.
.. versionchanged:: 3.10
The *etype* parameter has been renamed to *exc* and is now
positional-only.
.. function:: format_exception(etype, value, tb, limit=None, chain=True)
.. function:: format_exception(exc, /[, value, tb], limit=None, chain=True)
Format a stack trace and the exception information. The arguments have the
same meaning as the corresponding arguments to :func:`print_exception`. The
@ -143,6 +160,10 @@ The module defines the following functions:
.. versionchanged:: 3.5
The *etype* argument is ignored and inferred from the type of *value*.
.. versionchanged:: 3.10
This function's behavior and signature were modified to match
:func:`print_exception`.
.. function:: format_exc(limit=None, chain=True)

View File

@ -262,6 +262,24 @@ Standard names are defined for the following types:
.. versionadded:: 3.10
.. class:: GenericAlias(t_origin, t_args)
The type of :ref:`parameterized generics <types-genericalias>` such as
``list[int]``.
``t_origin`` should be a non-parameterized generic class, such as ``list``,
``tuple`` or ``dict``. ``t_args`` should be a :class:`tuple` (possibly of
length 1) of types which parameterize ``t_origin``::
>>> from types import GenericAlias
>>> list[int] == GenericAlias(list, (int,))
True
>>> dict[str, int] == GenericAlias(dict, (str, int))
True
.. versionadded:: 3.9
.. data:: Union
The type of :ref:`union type expressions<types-union>`.

View File

@ -524,7 +524,8 @@ These can be used as types in annotations using ``[]``, each having a unique syn
is equivalent to ``Tuple[Any, ...]``, and in turn to :class:`tuple`.
.. deprecated:: 3.9
:class:`builtins.tuple <tuple>` now supports ``[]``. See :pep:`585`.
:class:`builtins.tuple <tuple>` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. data:: Union
@ -602,7 +603,8 @@ These can be used as types in annotations using ``[]``, each having a unique syn
:class:`collections.abc.Callable`.
.. deprecated:: 3.9
:class:`collections.abc.Callable` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Callable` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: Type(Generic[CT_co])
@ -647,7 +649,8 @@ These can be used as types in annotations using ``[]``, each having a unique syn
.. versionadded:: 3.5.2
.. deprecated:: 3.9
:class:`builtins.type <type>` now supports ``[]``. See :pep:`585`.
:class:`builtins.type <type>` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. data:: Literal
@ -1079,7 +1082,8 @@ Corresponding to built-in types
...
.. deprecated:: 3.9
:class:`builtins.dict <dict>` now supports ``[]``. See :pep:`585`.
:class:`builtins.dict <dict>` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: List(list, MutableSequence[T])
@ -1099,7 +1103,8 @@ Corresponding to built-in types
return [item for item in vector if item > 0]
.. deprecated:: 3.9
:class:`builtins.list <list>` now supports ``[]``. See :pep:`585`.
:class:`builtins.list <list>` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: Set(set, MutableSet[T])
@ -1108,14 +1113,16 @@ Corresponding to built-in types
to use an abstract collection type such as :class:`AbstractSet`.
.. deprecated:: 3.9
:class:`builtins.set <set>` now supports ``[]``. See :pep:`585`.
:class:`builtins.set <set>` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: FrozenSet(frozenset, AbstractSet[T_co])
A generic version of :class:`builtins.frozenset <frozenset>`.
.. deprecated:: 3.9
:class:`builtins.frozenset <frozenset>` now supports ``[]``. See :pep:`585`.
:class:`builtins.frozenset <frozenset>` now supports ``[]``. See
:pep:`585` and :ref:`types-genericalias`.
.. note:: :data:`Tuple` is a special form.
@ -1129,7 +1136,8 @@ Corresponding to types in :mod:`collections`
.. versionadded:: 3.5.2
.. deprecated:: 3.9
:class:`collections.defaultdict` now supports ``[]``. See :pep:`585`.
:class:`collections.defaultdict` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: OrderedDict(collections.OrderedDict, MutableMapping[KT, VT])
@ -1138,7 +1146,8 @@ Corresponding to types in :mod:`collections`
.. versionadded:: 3.7.2
.. deprecated:: 3.9
:class:`collections.OrderedDict` now supports ``[]``. See :pep:`585`.
:class:`collections.OrderedDict` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: ChainMap(collections.ChainMap, MutableMapping[KT, VT])
@ -1148,7 +1157,8 @@ Corresponding to types in :mod:`collections`
.. versionadded:: 3.6.1
.. deprecated:: 3.9
:class:`collections.ChainMap` now supports ``[]``. See :pep:`585`.
:class:`collections.ChainMap` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: Counter(collections.Counter, Dict[T, int])
@ -1158,7 +1168,8 @@ Corresponding to types in :mod:`collections`
.. versionadded:: 3.6.1
.. deprecated:: 3.9
:class:`collections.Counter` now supports ``[]``. See :pep:`585`.
:class:`collections.Counter` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: Deque(deque, MutableSequence[T])
@ -1168,7 +1179,8 @@ Corresponding to types in :mod:`collections`
.. versionadded:: 3.6.1
.. deprecated:: 3.9
:class:`collections.deque` now supports ``[]``. See :pep:`585`.
:class:`collections.deque` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
Other concrete types
""""""""""""""""""""
@ -1193,7 +1205,8 @@ Other concrete types
``Match[bytes]``. These types are also in the ``typing.re`` namespace.
.. deprecated:: 3.9
Classes ``Pattern`` and ``Match`` from :mod:`re` now support ``[]``. See :pep:`585`.
Classes ``Pattern`` and ``Match`` from :mod:`re` now support ``[]``.
See :pep:`585` and :ref:`types-genericalias`.
.. class:: Text
@ -1220,7 +1233,8 @@ Corresponding to collections in :mod:`collections.abc`
A generic version of :class:`collections.abc.Set`.
.. deprecated:: 3.9
:class:`collections.abc.Set` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Set` now supports ``[]``. See :pep:`585` and
:ref:`types-genericalias`.
.. class:: ByteString(Sequence[int])
@ -1233,7 +1247,8 @@ Corresponding to collections in :mod:`collections.abc`
annotate arguments of any of the types mentioned above.
.. deprecated:: 3.9
:class:`collections.abc.ByteString` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.ByteString` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Collection(Sized, Iterable[T_co], Container[T_co])
@ -1242,28 +1257,32 @@ Corresponding to collections in :mod:`collections.abc`
.. versionadded:: 3.6.0
.. deprecated:: 3.9
:class:`collections.abc.Collection` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Collection` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Container(Generic[T_co])
A generic version of :class:`collections.abc.Container`.
.. deprecated:: 3.9
:class:`collections.abc.Container` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Container` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: ItemsView(MappingView, Generic[KT_co, VT_co])
A generic version of :class:`collections.abc.ItemsView`.
.. deprecated:: 3.9
:class:`collections.abc.ItemsView` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.ItemsView` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: KeysView(MappingView[KT_co], AbstractSet[KT_co])
A generic version of :class:`collections.abc.KeysView`.
.. deprecated:: 3.9
:class:`collections.abc.KeysView` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.KeysView` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Mapping(Sized, Collection[KT], Generic[VT_co])
@ -1274,49 +1293,56 @@ Corresponding to collections in :mod:`collections.abc`
return word_list[word]
.. deprecated:: 3.9
:class:`collections.abc.Mapping` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Mapping` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: MappingView(Sized, Iterable[T_co])
A generic version of :class:`collections.abc.MappingView`.
.. deprecated:: 3.9
:class:`collections.abc.MappingView` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.MappingView` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: MutableMapping(Mapping[KT, VT])
A generic version of :class:`collections.abc.MutableMapping`.
.. deprecated:: 3.9
:class:`collections.abc.MutableMapping` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.MutableMapping` now supports ``[]``. See
:pep:`585` and :ref:`types-genericalias`.
.. class:: MutableSequence(Sequence[T])
A generic version of :class:`collections.abc.MutableSequence`.
.. deprecated:: 3.9
:class:`collections.abc.MutableSequence` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.MutableSequence` now supports ``[]``. See
:pep:`585` and :ref:`types-genericalias`.
.. class:: MutableSet(AbstractSet[T])
A generic version of :class:`collections.abc.MutableSet`.
.. deprecated:: 3.9
:class:`collections.abc.MutableSet` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.MutableSet` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Sequence(Reversible[T_co], Collection[T_co])
A generic version of :class:`collections.abc.Sequence`.
.. deprecated:: 3.9
:class:`collections.abc.Sequence` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Sequence` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: ValuesView(MappingView[VT_co])
A generic version of :class:`collections.abc.ValuesView`.
.. deprecated:: 3.9
:class:`collections.abc.ValuesView` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.ValuesView` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
Corresponding to other types in :mod:`collections.abc`
""""""""""""""""""""""""""""""""""""""""""""""""""""""
@ -1326,14 +1352,16 @@ Corresponding to other types in :mod:`collections.abc`
A generic version of :class:`collections.abc.Iterable`.
.. deprecated:: 3.9
:class:`collections.abc.Iterable` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Iterable` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Iterator(Iterable[T_co])
A generic version of :class:`collections.abc.Iterator`.
.. deprecated:: 3.9
:class:`collections.abc.Iterator` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Iterator` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Generator(Iterator[T_co], Generic[T_co, T_contra, V_co])
@ -1367,7 +1395,8 @@ Corresponding to other types in :mod:`collections.abc`
start += 1
.. deprecated:: 3.9
:class:`collections.abc.Generator` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Generator` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Hashable
@ -1378,7 +1407,8 @@ Corresponding to other types in :mod:`collections.abc`
A generic version of :class:`collections.abc.Reversible`.
.. deprecated:: 3.9
:class:`collections.abc.Reversible` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Reversible` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Sized
@ -1403,7 +1433,8 @@ Asynchronous programming
.. versionadded:: 3.5.3
.. deprecated:: 3.9
:class:`collections.abc.Coroutine` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Coroutine` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra])
@ -1439,7 +1470,8 @@ Asynchronous programming
.. versionadded:: 3.6.1
.. deprecated:: 3.9
:class:`collections.abc.AsyncGenerator` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.AsyncGenerator` now supports ``[]``. See
:pep:`585` and :ref:`types-genericalias`.
.. class:: AsyncIterable(Generic[T_co])
@ -1448,7 +1480,8 @@ Asynchronous programming
.. versionadded:: 3.5.2
.. deprecated:: 3.9
:class:`collections.abc.AsyncIterable` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.AsyncIterable` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: AsyncIterator(AsyncIterable[T_co])
@ -1457,7 +1490,8 @@ Asynchronous programming
.. versionadded:: 3.5.2
.. deprecated:: 3.9
:class:`collections.abc.AsyncIterator` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.AsyncIterator` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
.. class:: Awaitable(Generic[T_co])
@ -1466,7 +1500,8 @@ Asynchronous programming
.. versionadded:: 3.5.2
.. deprecated:: 3.9
:class:`collections.abc.Awaitable` now supports ``[]``. See :pep:`585`.
:class:`collections.abc.Awaitable` now supports ``[]``. See :pep:`585`
and :ref:`types-genericalias`.
Context manager types
@ -1480,7 +1515,8 @@ Context manager types
.. versionadded:: 3.6.0
.. deprecated:: 3.9
:class:`contextlib.AbstractContextManager` now supports ``[]``. See :pep:`585`.
:class:`contextlib.AbstractContextManager` now supports ``[]``. See
:pep:`585` and :ref:`types-genericalias`.
.. class:: AsyncContextManager(Generic[T_co])
@ -1490,7 +1526,8 @@ Context manager types
.. versionadded:: 3.6.2
.. deprecated:: 3.9
:class:`contextlib.AbstractAsyncContextManager` now supports ``[]``. See :pep:`585`.
:class:`contextlib.AbstractAsyncContextManager` now supports ``[]``. See
:pep:`585` and :ref:`types-genericalias`.
Protocols
---------

View File

@ -455,6 +455,12 @@ Supported XPath syntax
| | has the given value. The value cannot contain |
| | quotes. |
+-----------------------+------------------------------------------------------+
| ``[@attrib!='value']``| Selects all elements for which the given attribute |
| | does not have the given value. The value cannot |
| | contain quotes. |
| | |
| | .. versionadded:: 3.10 |
+-----------------------+------------------------------------------------------+
| ``[tag]`` | Selects all elements that have a child named |
| | ``tag``. Only immediate children are supported. |
+-----------------------+------------------------------------------------------+
@ -463,10 +469,22 @@ Supported XPath syntax
| | |
| | .. versionadded:: 3.7 |
+-----------------------+------------------------------------------------------+
| ``[.!='text']`` | Selects all elements whose complete text content, |
| | including descendants, does not equal the given |
| | ``text``. |
| | |
| | .. versionadded:: 3.10 |
+-----------------------+------------------------------------------------------+
| ``[tag='text']`` | Selects all elements that have a child named |
| | ``tag`` whose complete text content, including |
| | descendants, equals the given ``text``. |
+-----------------------+------------------------------------------------------+
| ``[tag!='text']`` | Selects all elements that have a child named |
| | ``tag`` whose complete text content, including |
| | descendants, does not equal the given ``text``. |
| | |
| | .. versionadded:: 3.10 |
+-----------------------+------------------------------------------------------+
| ``[position]`` | Selects all elements that are located at the given |
| | position. The position can be either an integer |
| | (1 is the first position), the expression ``last()`` |

View File

@ -2168,7 +2168,7 @@ Emulating callable objects
.. index:: pair: call; instance
Called when the instance is "called" as a function; if this method is defined,
``x(arg1, arg2, ...)`` is a shorthand for ``x.__call__(arg1, arg2, ...)``.
``x(arg1, arg2, ...)`` roughly translates to ``type(x).__call__(x, arg1, ...)``.
.. _sequence-types:

View File

@ -643,6 +643,16 @@ after resuming depends on the method which resumed the execution. If
:meth:`~agen.asend` is used, then the result will be the value passed in to
that method.
If an asynchronous generator happens to exit early by :keyword:`break`, the caller
task being cancelled, or other exceptions, the generator's async cleanup code
will run and possibly raise exceptions or access context variables in an
unexpected context--perhaps after the lifetime of tasks it depends, or
during the event loop shutdown when the async-generator garbage collection hook
is called.
To prevent this, the caller must explicitly close the async generator by calling
:meth:`~agen.aclose` method to finalize the generator and ultimately detach it
from the event loop.
In an asynchronous generator function, yield expressions are allowed anywhere
in a :keyword:`try` construct. However, if an asynchronous generator is not
resumed before it is finalized (by reaching a zero reference count or by
@ -654,9 +664,9 @@ generator-iterator's :meth:`~agen.aclose` method and run the resulting
coroutine object, thus allowing any pending :keyword:`!finally` clauses
to execute.
To take care of finalization, an event loop should define
a *finalizer* function which takes an asynchronous generator-iterator
and presumably calls :meth:`~agen.aclose` and executes the coroutine.
To take care of finalization upon event loop termination, an event loop should
define a *finalizer* function which takes an asynchronous generator-iterator and
presumably calls :meth:`~agen.aclose` and executes the coroutine.
This *finalizer* may be registered by calling :func:`sys.set_asyncgen_hooks`.
When first iterated over, an asynchronous generator-iterator will store the
registered *finalizer* to be called upon finalization. For a reference example
@ -837,14 +847,8 @@ this method will need to explicitly add that support.
A string's items are characters. A character is not a separate data type but a
string of exactly one character.
..
At the time of writing this, there is no documentation for generic alias
or PEP 585. Thus the link currently points to PEP 585 itself.
Please change the link for generic alias to reference the correct
documentation once documentation for PEP 585 becomes available.
Subscription of certain :term:`classes <class>` or :term:`types <type>`
creates a `generic alias <https://www.python.org/dev/peps/pep-0585/>`_.
creates a :ref:`generic alias <types-genericalias>`.
In this case, user-defined classes can support subscription by providing a
:meth:`__class_getitem__` classmethod.

View File

@ -1,156 +0,0 @@
(function() {
'use strict';
// Parses versions in URL segments like:
// "3", "dev", "release/2.7" or "3.6rc2"
var version_regexs = [
'(?:\\d)',
'(?:\\d\\.\\d[\\w\\d\\.]*)',
'(?:dev)',
'(?:release/\\d.\\d[\\x\\d\\.]*)'];
var all_versions = {
'3.10': 'dev (3.10)',
'3.9': 'pre (3.9)',
'3.8': '3.8',
'3.7': '3.7',
'3.6': '3.6',
'2.7': '2.7',
};
var all_languages = {
'en': 'English',
'fr': 'French',
'ja': 'Japanese',
'ko': 'Korean',
'pt-br': 'Brazilian Portuguese',
'zh-cn': 'Simplified Chinese',
};
function build_version_select(current_version, current_release) {
var buf = ['<select>'];
$.each(all_versions, function(version, title) {
buf.push('<option value="' + version + '"');
if (version == current_version)
buf.push(' selected="selected">' + current_release + '</option>');
else
buf.push('>' + title + '</option>');
});
buf.push('</select>');
return buf.join('');
}
function build_language_select(current_language) {
var buf = ['<select>'];
$.each(all_languages, function(language, title) {
if (language == current_language)
buf.push('<option value="' + language + '" selected="selected">' +
all_languages[current_language] + '</option>');
else
buf.push('<option value="' + language + '">' + title + '</option>');
});
if (!(current_language in all_languages)) {
// In case we're browsing a language that is not yet in all_languages.
buf.push('<option value="' + current_language + '" selected="selected">' +
current_language + '</option>');
all_languages[current_language] = current_language;
}
buf.push('</select>');
return buf.join('');
}
function navigate_to_first_existing(urls) {
// Navigate to the first existing URL in urls.
var url = urls.shift();
if (urls.length == 0) {
window.location.href = url;
return;
}
$.ajax({
url: url,
success: function() {
window.location.href = url;
},
error: function() {
navigate_to_first_existing(urls);
}
});
}
function on_version_switch() {
var selected_version = $(this).children('option:selected').attr('value') + '/';
var url = window.location.href;
var current_language = language_segment_from_url(url);
var current_version = version_segment_in_url(url);
var new_url = url.replace('.org/' + current_language + current_version,
'.org/' + current_language + selected_version);
if (new_url != url) {
navigate_to_first_existing([
new_url,
url.replace('.org/' + current_language + current_version,
'.org/' + selected_version),
'https://docs.python.org/' + current_language + selected_version,
'https://docs.python.org/' + selected_version,
'https://docs.python.org/'
]);
}
}
function on_language_switch() {
var selected_language = $(this).children('option:selected').attr('value') + '/';
var url = window.location.href;
var current_language = language_segment_from_url(url);
var current_version = version_segment_in_url(url);
if (selected_language == 'en/') // Special 'default' case for english.
selected_language = '';
var new_url = url.replace('.org/' + current_language + current_version,
'.org/' + selected_language + current_version);
if (new_url != url) {
navigate_to_first_existing([
new_url,
'https://docs.python.org/'
]);
}
}
// Returns the path segment of the language as a string, like 'fr/'
// or '' if not found.
function language_segment_from_url(url) {
var language_regexp = '\.org/([a-z]{2}(?:-[a-z]{2})?/)';
var match = url.match(language_regexp);
if (match !== null)
return match[1];
return '';
}
// Returns the path segment of the version as a string, like '3.6/'
// or '' if not found.
function version_segment_in_url(url) {
var language_segment = '(?:[a-z]{2}(?:-[a-z]{2})?/)';
var version_segment = '(?:(?:' + version_regexs.join('|') + ')/)';
var version_regexp = '\\.org/' + language_segment + '?(' + version_segment + ')';
var match = url.match(version_regexp);
if (match !== null)
return match[1];
return ''
}
$(document).ready(function() {
var release = DOCUMENTATION_OPTIONS.VERSION;
var language_segment = language_segment_from_url(window.location.href);
var current_language = language_segment.replace(/\/+$/g, '') || 'en';
var version = release.substr(0, 3);
var version_select = build_version_select(version, release);
$('.version_switcher_placeholder').html(version_select);
$('.version_switcher_placeholder select').bind('change', on_version_switch);
var language_select = build_language_select(current_language);
$('.language_switcher_placeholder').html(language_select);
$('.language_switcher_placeholder select').bind('change', on_language_switch);
});
})();

View File

@ -5,7 +5,7 @@ c-api/sequence,,:i2,o[i1:i2]
c-api/tuple,,:high,p[low:high]
c-api/unicode,,:end,str[start:end]
c-api/unicode,,:start,unicode[start:start+length]
distutils/examples,267,`,This is the description of the ``foobar`` package.
distutils/examples,,`,This is the description of the ``foobar`` package.
distutils/setupscript,,::,
extending/embedding,,:numargs,"if(!PyArg_ParseTuple(args, "":numargs""))"
extending/extending,,:myfunction,"PyArg_ParseTuple(args, ""D:myfunction"", &c);"
@ -23,6 +23,9 @@ howto/curses,,:blue,"2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white.
howto/curses,,:magenta,"2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The"
howto/curses,,:cyan,"2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The"
howto/curses,,:white,"2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The"
howto/descriptor,,:root,"INFO:root"
howto/descriptor,,:Updating,"root:Updating"
howto/descriptor,,:Accessing,"root:Accessing"
howto/instrumentation,,::,python$target:::function-entry
howto/instrumentation,,:function,python$target:::function-entry
howto/instrumentation,,::,python$target:::function-return
@ -144,8 +147,10 @@ library/ipaddress,,:db8,IPv6Address('2001:db8::')
library/ipaddress,,::,IPv6Address('2001:db8::')
library/ipaddress,,:db8,>>> ipaddress.IPv6Address('2001:db8::1000')
library/ipaddress,,::,>>> ipaddress.IPv6Address('2001:db8::1000')
library/ipaddress,,:db8,IPv6Address('2001:db8::1000')
library/ipaddress,,::,IPv6Address('2001:db8::1000')
library/ipaddress,,:db8,'2001:db8::1000'
library/ipaddress,,::,'2001:db8::1000'
library/ipaddress,231,:db8,">>> f'{ipaddress.IPv6Address(""2001:db8::1000""):s}'"
library/ipaddress,231,::,">>> f'{ipaddress.IPv6Address(""2001:db8::1000""):s}'"
library/ipaddress,,::,IPv6Address('ff02::5678%1')
library/ipaddress,,::,fe80::1234
library/ipaddress,,:db8,">>> ipaddress.ip_address(""2001:db8::1"").reverse_pointer"

1 c-api/arg :ref PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
5 c-api/tuple :high p[low:high]
6 c-api/unicode :end str[start:end]
7 c-api/unicode :start unicode[start:start+length]
8 distutils/examples 267 ` This is the description of the ``foobar`` package.
9 distutils/setupscript ::
10 extending/embedding :numargs if(!PyArg_ParseTuple(args, ":numargs"))
11 extending/extending :myfunction PyArg_ParseTuple(args, "D:myfunction", &c);
23 howto/curses :magenta 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The
24 howto/curses :cyan 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The
25 howto/curses :white 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The
26 howto/descriptor :root INFO:root
27 howto/descriptor :Updating root:Updating
28 howto/descriptor :Accessing root:Accessing
29 howto/instrumentation :: python$target:::function-entry
30 howto/instrumentation :function python$target:::function-entry
31 howto/instrumentation :: python$target:::function-return
147 library/ipaddress :: IPv6Address('2001:db8::')
148 library/ipaddress :db8 >>> ipaddress.IPv6Address('2001:db8::1000')
149 library/ipaddress :: >>> ipaddress.IPv6Address('2001:db8::1000')
150 library/ipaddress :db8 IPv6Address('2001:db8::1000') '2001:db8::1000'
151 library/ipaddress :: IPv6Address('2001:db8::1000') '2001:db8::1000'
152 library/ipaddress 231 :db8 >>> f'{ipaddress.IPv6Address("2001:db8::1000"):s}'
153 library/ipaddress 231 :: >>> f'{ipaddress.IPv6Address("2001:db8::1000"):s}'
154 library/ipaddress :: IPv6Address('ff02::5678%1')
155 library/ipaddress :: fe80::1234
156 library/ipaddress :db8 >>> ipaddress.ip_address("2001:db8::1").reverse_pointer

View File

@ -6,3 +6,12 @@ In extensions/pyspecific.py:
{% trans %}CPython implementation detail:{% endtrans %}
{% trans %}Deprecated since version {deprecated}, will be removed in version {removed}{% endtrans %}
{% trans %}Deprecated since version {deprecated}, removed in version {removed}{% endtrans %}
In docsbuild-scripts, when rewriting indexsidebar.html with actual versions:
{% trans %}in development{% endtrans %}
{% trans %}pre-release{% endtrans %}
{% trans %}stable{% endtrans %}
{% trans %}security-fixes{% endtrans %}
{% trans %}EOL{% endtrans %}

View File

@ -2,12 +2,8 @@
<p><a href="{{ pathto('download') }}">{% trans %}Download these documents{% endtrans %}</a></p>
<h3>{% trans %}Docs by version{% endtrans %}</h3>
<ul>
<li><a href="https://docs.python.org/3.10/">{% trans %}Python 3.10 (in development){% endtrans %}</a></li>
<li><a href="https://docs.python.org/3.9/">{% trans %}Python 3.9 (pre-release){% endtrans %}</a></li>
<li><a href="https://docs.python.org/3.8/">{% trans %}Python 3.8 (stable){% endtrans %}</a></li>
<li><a href="https://docs.python.org/3.7/">{% trans %}Python 3.7 (stable){% endtrans %}</a></li>
<li><a href="https://docs.python.org/3.6/">{% trans %}Python 3.6 (security-fixes){% endtrans %}</a></li>
<li><a href="https://docs.python.org/2.7/">{% trans %}Python 2.7 (EOL){% endtrans %}</a></li>
<li><a href="https://docs.python.org/">{% trans %}Stable{% endtrans %}</a></li>
<li><a href="https://docs.python.org/dev/">{% trans %}In development{% endtrans %}</a></li>
<li><a href="https://www.python.org/doc/versions/">{% trans %}All versions{% endtrans %}</a></li>
</ul>

View File

@ -12,22 +12,14 @@
{% block rootrellink %}
{{ super() }}
<li>
{%- if switchers is defined %}
<span class="language_switcher_placeholder">{{ language or 'en' }}</span>
<span class="version_switcher_placeholder">{{ release }}</span>
<a href="{{ pathto('index') }}">{% trans %}Documentation {% endtrans %}</a>{{ reldelim1 }}
{%- else %}
<li id="cpython-language-and-version">
<a href="{{ pathto('index') }}">{{ shorttitle }}</a>{{ reldelim1 }}
{%- endif %}
</li>
{% endblock %}
{% block extrahead %}
<link rel="canonical" href="https://docs.python.org/3/{{pagename}}.html" />
{% if builder != "htmlhelp" %}
{% if switchers is defined and not embedded %}
<script type="text/javascript" src="{{ pathto('_static/switchers.js', 1) }}"></script>{% endif %}
{% if pagename == 'whatsnew/changelog' and not embedded %}
<script type="text/javascript" src="{{ pathto('_static/changelog_search.js', 1) }}"></script>{% endif %}
{% endif %}

View File

@ -273,15 +273,15 @@ Exception Chaining
==================
The :keyword:`raise` statement allows an optional :keyword:`from` which enables
chaining exceptions by setting the ``__cause__`` attribute of the raised
exception. For example::
chaining exceptions. For example::
raise RuntimeError from OSError
# exc must be exception instance or None.
raise RuntimeError from exc
This can be useful when you are transforming exceptions. For example::
>>> def func():
... raise IOError
... raise IOError
...
>>> try:
... func()
@ -297,12 +297,11 @@ This can be useful when you are transforming exceptions. For example::
<BLANKLINE>
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
RuntimeError
RuntimeError: Failed to open database
The expression following the :keyword:`from` must be either an exception or
``None``. Exception chaining happens automatically when an exception is raised
inside an exception handler or :keyword:`finally` section. Exception chaining
can be disabled by using ``from None`` idiom:
Exception chaining happens automatically when an exception is raised inside an
:keyword:`except` or :keyword:`finally` section. Exception chaining can be
disabled by using ``from None`` idiom:
>>> try:
... open('database.sqlite')
@ -313,6 +312,8 @@ can be disabled by using ``from None`` idiom:
File "<stdin>", line 4, in <module>
RuntimeError
For more information about chaining mechanics, see :ref:`bltin-exceptions`.
.. _tut-userexceptions:

View File

@ -447,10 +447,9 @@ Miscellaneous options
* ``-X dev``: enable :ref:`Python Development Mode <devmode>`, introducing
additional runtime checks that are too expensive to be enabled by
default.
* ``-X utf8`` enables UTF-8 mode for operating system interfaces, overriding
the default locale-aware mode. ``-X utf8=0`` explicitly disables UTF-8
mode (even when it would otherwise activate automatically).
See :envvar:`PYTHONUTF8` for more details.
* ``-X utf8`` enables the :ref:`Python UTF-8 Mode <utf8-mode>`.
``-X utf8=0`` explicitly disables :ref:`Python UTF-8 Mode <utf8-mode>`
(even when it would otherwise activate automatically).
* ``-X pycache_prefix=PATH`` enables writing ``.pyc`` files to a parallel
tree rooted at the given directory instead of to the code tree. See also
:envvar:`PYTHONPYCACHEPREFIX`.
@ -810,9 +809,10 @@ conflict.
.. envvar:: PYTHONLEGACYWINDOWSFSENCODING
If set to a non-empty string, the default filesystem encoding and errors mode
will revert to their pre-3.6 values of 'mbcs' and 'replace', respectively.
Otherwise, the new defaults 'utf-8' and 'surrogatepass' are used.
If set to a non-empty string, the default :term:`filesystem encoding and
error handler` mode will revert to their pre-3.6 values of 'mbcs' and
'replace', respectively. Otherwise, the new defaults 'utf-8' and
'surrogatepass' are used.
This may also be enabled at runtime with
:func:`sys._enablelegacywindowsfsencoding()`.
@ -898,54 +898,14 @@ conflict.
.. envvar:: PYTHONUTF8
If set to ``1``, enables the interpreter's UTF-8 mode, where ``UTF-8`` is
used as the text encoding for system interfaces, regardless of the
current locale setting.
If set to ``1``, enable the :ref:`Python UTF-8 Mode <utf8-mode>`.
This means that:
* :func:`sys.getfilesystemencoding()` returns ``'UTF-8'`` (the locale
encoding is ignored).
* :func:`locale.getpreferredencoding()` returns ``'UTF-8'`` (the locale
encoding is ignored, and the function's ``do_setlocale`` parameter has no
effect).
* :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr` all use
UTF-8 as their text encoding, with the ``surrogateescape``
:ref:`error handler <error-handlers>` being enabled for :data:`sys.stdin`
and :data:`sys.stdout` (:data:`sys.stderr` continues to use
``backslashreplace`` as it does in the default locale-aware mode)
As a consequence of the changes in those lower level APIs, other higher
level APIs also exhibit different default behaviours:
* Command line arguments, environment variables and filenames are decoded
to text using the UTF-8 encoding.
* :func:`os.fsdecode()` and :func:`os.fsencode()` use the UTF-8 encoding.
* :func:`open()`, :func:`io.open()`, and :func:`codecs.open()` use the UTF-8
encoding by default. However, they still use the strict error handler by
default so that attempting to open a binary file in text mode is likely
to raise an exception rather than producing nonsense data.
Note that the standard stream settings in UTF-8 mode can be overridden by
:envvar:`PYTHONIOENCODING` (just as they can be in the default locale-aware
mode).
If set to ``0``, the interpreter runs in its default locale-aware mode.
If set to ``0``, disable the :ref:`Python UTF-8 Mode <utf8-mode>`.
Setting any other non-empty string causes an error during interpreter
initialisation.
If this environment variable is not set at all, then the interpreter defaults
to using the current locale settings, *unless* the current locale is
identified as a legacy ASCII-based locale
(as described for :envvar:`PYTHONCOERCECLOCALE`), and locale coercion is
either disabled or fails. In such legacy locales, the interpreter will
default to enabling UTF-8 mode unless explicitly instructed not to do so.
Also available as the :option:`-X` ``utf8`` option.
.. versionadded:: 3.7
See :pep:`540` for more details.
Debug-mode variables

View File

@ -614,21 +614,14 @@ Page). Python uses it for the default encoding of text files (e.g.
This may cause issues because UTF-8 is widely used on the internet
and most Unix systems, including WSL (Windows Subsystem for Linux).
You can use UTF-8 mode to change the default text encoding to UTF-8.
You can enable UTF-8 mode via the ``-X utf8`` command line option, or
the ``PYTHONUTF8=1`` environment variable. See :envvar:`PYTHONUTF8` for
enabling UTF-8 mode, and :ref:`setting-envvars` for how to modify
environment variables.
You can use the :ref:`Python UTF-8 Mode <utf8-mode>` to change the default text
encoding to UTF-8. You can enable the :ref:`Python UTF-8 Mode <utf8-mode>` via
the ``-X utf8`` command line option, or the ``PYTHONUTF8=1`` environment
variable. See :envvar:`PYTHONUTF8` for enabling UTF-8 mode, and
:ref:`setting-envvars` for how to modify environment variables.
When UTF-8 mode is enabled:
* :func:`locale.getpreferredencoding` returns ``'UTF-8'`` instead of
the system encoding. This function is used for the default text
encoding in many places, including :func:`open`, :class:`Popen`,
:meth:`Path.read_text`, etc.
* :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr`
all use UTF-8 as their text encoding.
* You can still use the system encoding via the "mbcs" codec.
When the :ref:`Python UTF-8 Mode <utf8-mode>` is enabled, you can still use the
system encoding (the ANSI Code Page) via the "mbcs" codec.
Note that adding ``PYTHONUTF8=1`` to the default environment variables
will affect all Python 3.7+ applications on your system.
@ -641,7 +634,8 @@ temporarily or use the ``-X utf8`` command line option.
on Windows for:
* Console I/O including standard I/O (see :pep:`528` for details).
* The filesystem encoding (see :pep:`529` for details).
* The :term:`filesystem encoding <filesystem encoding and error handler>`
(see :pep:`529` for details).
.. _launcher:

View File

@ -134,7 +134,7 @@ arguments of multiple types, :data:`typing.Union` was used::
return number ** 2
Now, type hints can be written in a more succinct manner::
Type hints can now be written in a more succinct manner::
def square(number: int | float) -> int | float:
return number ** 2
@ -176,6 +176,13 @@ codecs
Add a :func:`codecs.unregister` function to unregister a codec search function.
(Contributed by Hai Shi in :issue:`41842`.)
contextlib
----------
Add a :func:`contextlib.aclosing` context manager to safely close async generators
and objects representing asynchronously released resources.
(Contributed by Joongi Kim and John Belmonte in :issue:`41229`.)
curses
------
@ -186,6 +193,12 @@ by :func:`curses.color_content`, :func:`curses.init_color`,
support is provided by the underlying ncurses library.
(Contributed by Jeffrey Kintscher and Hans Petter Jansson in :issue:`36982`.)
doctest
-------
When a module does not define ``__loader__``, fall back to ``__spec__.loader``.
(Contributed by Brett Cannon in :issue:`42133`.)
encodings
---------
:func:`encodings.normalize_encoding` now ignores non-ASCII characters.
@ -198,6 +211,18 @@ Added the *root_dir* and *dir_fd* parameters in :func:`~glob.glob` and
:func:`~glob.iglob` which allow to specify the root directory for searching.
(Contributed by Serhiy Storchaka in :issue:`38144`.)
inspect
-------
When a module does not define ``__loader__``, fall back to ``__spec__.loader``.
(Contributed by Brett Cannon in :issue:`42133`.)
linecache
---------
When a module does not define ``__loader__``, fall back to ``__spec__.loader``.
(Contributed by Brett Cannon in :issue:`42133`.)
os
--
@ -210,6 +235,19 @@ py_compile
Added ``--quiet`` option to command-line interface of :mod:`py_compile`.
(Contributed by Gregory Schevchenko in :issue:`38731`.)
shelve
------
The :mod:`shelve` module now uses :data:`pickle.DEFAULT_PROTOCOL` by default
instead of :mod:`pickle` protocol ``3`` when creating shelves.
(Contributed by Zackery Spytz in :issue:`34204`.)
site
----
When a module does not define ``__loader__``, fall back to ``__spec__.loader``.
(Contributed by Brett Cannon in :issue:`42133`.)
sys
---
@ -217,6 +255,23 @@ Add :data:`sys.orig_argv` attribute: the list of the original command line
arguments passed to the Python executable.
(Contributed by Victor Stinner in :issue:`23427`.)
threading
---------
Added :func:`threading.gettrace` and :func:`threading.getprofile` to
retrieve the functions set by :func:`threading.settrace` and
:func:`threading.setprofile` respectively.
(Contributed by Mario Corchero in :issue:`42251`.)
traceback
---------
The :func:`~traceback.format_exception`,
:func:`~traceback.format_exception_only`, and
:func:`~traceback.print_exception` functions can now take an exception object
as a positional-only argument.
(Contributed by Zackery Spytz and Matthias Bussonnier in :issue:`26389`.)
types
-----
@ -254,7 +309,7 @@ Optimizations
* The ``LOAD_ATTR`` instruction now uses new "per opcode cache" mechanism.
It is about 36% faster now. (Contributed by Pablo Galindo and Yury Selivanov
in :issue:`42093`.)
in :issue:`42093`, based on ideas implemented originally in PyPy and MicroPython.)
* When building Python with ``--enable-optimizations`` now
``-fno-semantic-interposition`` is added to both the compile and link line.
@ -267,6 +322,23 @@ Optimizations
Deprecated
==========
* Starting in this release, there will be a concerted effort to begin
cleaning up old import semantics that were kept for Python 2.7
compatibility. Specifically,
:meth:`~importlib.abc.PathEntryFinder.find_loader`/:meth:`~importlib.abc.Finder.find_module`
(superseded by :meth:`~importlib.abc.Finder.find_spec`),
:meth:`~importlib.abc.Loader.load_module`
(superseded by :meth:`~importlib.abc.Loader.exec_module`),
:meth:`~importlib.abc.Loader.module_repr` (which the import system
takes care of for you), the ``__package__`` attribute
(superseded by ``__spec__.parent``), the ``__loader__`` attribute
(superseded by ``__spec__.loader``), and the ``__cached__`` attribute
(superseded by ``__spec__.cached``) will slowly be removed (as well
as other classes and methods in :mod:`importlib`).
:exc:`ImportWarning` and/or :exc:`DeprecationWarning` will be raised
as appropriate to help identify code which needs updating during
this transition.
Removed
=======
@ -283,6 +355,11 @@ Removed
Python 3.5.
(Contributed by Berker Peksag in :issue:`31844`.)
* Removed the ``unicodedata.ucnhash_CAPI`` attribute which was an internal
PyCapsule object. The related private ``_PyUnicode_Name_CAPI`` structure was
moved to the internal C API.
(Contributed by Victor Stinner in :issue:`42157`.)
Porting to Python 3.10
======================
@ -291,6 +368,15 @@ This section lists previously described changes and other bugfixes
that may require changes to your code.
Changes in the Python API
-------------------------
* The *etype* parameters of the :func:`~traceback.format_exception`,
:func:`~traceback.format_exception_only`, and
:func:`~traceback.print_exception` functions in the :mod:`traceback` module
have been renamed to *exc*.
(Contributed by Zackery Spytz and Matthias Bussonnier in :issue:`26389`.)
Build Changes
=============
@ -337,6 +423,19 @@ New Features
* Added :c:func:`PyUnicode_AsUTF8AndSize` to the limited C API.
(Contributed by Alex Gaynor in :issue:`41784`.)
* Added :c:func:`PyModule_AddObjectRef` function: similar to
:c:func:`PyModule_AddObjectRef` but don't steal a reference to the value on
success.
(Contributed by Victor Stinner in :issue:`1635741`.)
* Added :c:func:`Py_NewRef` and :c:func:`Py_XNewRef` functions to increment the
reference count of an object and return the object.
(Contributed by Victor Stinner in :issue:`42262`.)
* The :c:func:`PyType_FromModuleAndSpec` function now accepts NULL ``tp_doc``
slot.
(Contributed by Hai Shi in :issue:`41832`.)
Porting to Python 3.10
----------------------
@ -390,6 +489,10 @@ Porting to Python 3.10
Unicode object without initial data.
(Contributed by Inada Naoki in :issue:`36346`.)
* The private ``_PyUnicode_Name_CAPI`` structure of the PyCapsule API
``unicodedata.ucnhash_CAPI`` has been moved to the internal C API.
(Contributed by Victor Stinner in :issue:`42157`.)
Deprecated
----------

View File

@ -232,7 +232,8 @@ PEP 540: Forced UTF-8 Runtime Mode
-----------------------------------
The new :option:`-X` ``utf8`` command line option and :envvar:`PYTHONUTF8`
environment variable can be used to enable the CPython *UTF-8 mode*.
environment variable can be used to enable the :ref:`Python UTF-8 Mode
<utf8-mode>`.
When in UTF-8 mode, CPython ignores the locale settings, and uses the
UTF-8 encoding by default. The error handlers for :data:`sys.stdin` and

View File

@ -4,22 +4,21 @@
:Release: |release|
:Date: |today|
:Editor: Łukasz Langa
.. Rules for maintenance:
* Anyone can add text to this document. Do not spend very much time
on the wording of your changes, because your text will probably
get rewritten to some degree.
* Anyone can add text to this document. Your text might get
rewritten to some degree.
* The maintainer will go through Misc/NEWS periodically and add
changes; it's therefore more important to add your changes to
Misc/NEWS than to this file.
* This is not a complete list of every single change; completeness
is the purpose of Misc/NEWS. Some changes I consider too small
is the purpose of Misc/NEWS. Some changes will be too small
or esoteric to include. If such a change is added to the text,
I'll just remove it. (This is another reason you shouldn't spend
too much time on writing your addition.)
it might get removed during final editing.
* If you want to draw your new text to the attention of the
maintainer, add 'XXX' to the beginning of the paragraph or
@ -46,14 +45,13 @@
when researching a change.
This article explains the new features in Python 3.9, compared to 3.8.
Python 3.9 was released on October 5th, 2020.
For full details, see the :ref:`changelog <changelog>`.
.. note::
.. seealso::
Prerelease users should be aware that this document is currently in draft
form. It will be updated substantially as Python 3.9 moves towards release,
so it's worth checking back even after reading earlier versions.
:pep:`596` - Python 3.9 Release Schedule
Summary -- Release highlights
@ -62,27 +60,70 @@ Summary -- Release highlights
.. This section singles out the most important changes in Python 3.9.
Brevity is key.
New syntax features:
.. PEP-sized items next.
* :pep:`584`, union operators added to ``dict``;
* :pep:`585`, type hinting generics in standard collections;
* :pep:`614`, relaxed grammar restrictions on decorators.
New built-in features:
* :pep:`616`, string methods to remove prefixes and suffixes.
New features in the standard library:
* :pep:`593`, flexible function and variable annotations;
* :func:`os.pidfd_open` added that allows process management without races
and signals.
Interpreter improvements:
* :pep:`573`, fast access to module state from methods of C extension
types;
* :pep:`617`, CPython now uses a new parser based on PEG;
* a number of Python builtins (range, tuple, set, frozenset, list, dict) are
now sped up using :pep:`590` vectorcall;
* garbage collection does not block on resurrected objects;
* a number of Python modules (:mod:`_abc`, :mod:`audioop`, :mod:`_bz2`,
:mod:`_codecs`, :mod:`_contextvars`, :mod:`_crypt`, :mod:`_functools`,
:mod:`_json`, :mod:`_locale`, :mod:`math`, :mod:`operator`, :mod:`resource`,
:mod:`time`, :mod:`_weakref`) now use multiphase initialization as defined
by PEP 489;
* a number of standard library modules (:mod:`audioop`, :mod:`ast`, :mod:`grp`,
:mod:`_hashlib`, :mod:`pwd`, :mod:`_posixsubprocess`, :mod:`random`,
:mod:`select`, :mod:`struct`, :mod:`termios`, :mod:`zlib`) are now using
the stable ABI defined by PEP 384.
New library modules:
* :pep:`615`, the IANA Time Zone Database is now present in the standard
library in the :mod:`zoneinfo` module;
* an implementation of a topological sort of a graph is now provided in
the new :mod:`graphlib` module.
Release process changes:
* :pep:`602`, CPython adopts an annual release cycle.
You should check for DeprecationWarning in your code
====================================================
When Python 2.7 was still supported, many functions were kept for backward
compatibility with Python 2.7. With the end of Python 2.7 support, these
backward compatibility layers have been removed, or will be removed soon.
Most of them emitted a :exc:`DeprecationWarning` warning for several years. For
example, using ``collections.Mapping`` instead of ``collections.abc.Mapping``
emits a :exc:`DeprecationWarning` since Python 3.3, released in 2012.
When Python 2.7 was still supported, a lot of functionality in Python 3
was kept for backward compatibility with Python 2.7. With the end of Python
2 support, these backward compatibility layers have been removed, or will
be removed soon. Most of them emitted a :exc:`DeprecationWarning` warning for
several years. For example, using ``collections.Mapping`` instead of
``collections.abc.Mapping`` emits a :exc:`DeprecationWarning` since Python
3.3, released in 2012.
Test your application with the :option:`-W` ``default`` command-line option to see
:exc:`DeprecationWarning` and :exc:`PendingDeprecationWarning`, or even with
:option:`-W` ``error`` to treat them as errors. :ref:`Warnings Filter
<warning-filter>` can be used to ignore warnings from third-party code.
It has been decided to keep a few backward compatibility layers for one last
release, to give more time to Python projects maintainers to organize the
Python 3.9 is the last version providing those Python 2 backward compatibility
layers, to give more time to Python projects maintainers to organize the
removal of the Python 2 support and add support for Python 3.9.
Aliases to :ref:`Abstract Base Classes <collections-abstract-base-classes>` in
@ -94,6 +135,9 @@ More generally, try to run your tests in the :ref:`Python Development Mode
<devmode>` which helps to prepare your code to make it compatible with the
next Python version.
Note: a number of pre-existing deprecatations were removed in this version
of Python as well. Consult the :ref:`removed-in-python-39` section.
New Features
============
@ -102,11 +146,23 @@ Dictionary Merge & Update Operators
-----------------------------------
Merge (``|``) and update (``|=``) operators have been added to the built-in
:class:`dict` class. See :pep:`584` for a full description.
:class:`dict` class. Those complement the existing ``dict.update`` and
``{**d1, **d2}`` methods of merging dictionaries.
Example::
>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
>>> y = {"key2": "value2 from y", "key3": "value3 from y"}
>>> x | y
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
>>> y | x
{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}
See :pep:`584` for a full description.
(Contributed by Brandt Bucher in :issue:`36144`.)
PEP 616: New removeprefix() and removesuffix() string methods
-------------------------------------------------------------
New String Methods to Remove Prefixes and Suffixes
--------------------------------------------------
:meth:`str.removeprefix(prefix)<str.removeprefix>` and
:meth:`str.removesuffix(suffix)<str.removesuffix>` have been added
@ -115,8 +171,8 @@ to easily remove an unneeded prefix or a suffix from a string. Corresponding
added. See :pep:`616` for a full description. (Contributed by Dennis Sweeney in
:issue:`39939`.)
PEP 585: Builtin Generic Types
------------------------------
Type Hinting Generics in Standard Collections
---------------------------------------------
In type annotations you can now use built-in collection types such as
``list`` and ``dict`` as generic types instead of importing the
@ -135,8 +191,8 @@ Example:
See :pep:`585` for more details. (Contributed by Guido van Rossum,
Ethan Smith, and Batuhan Taşkaya in :issue:`39481`.)
PEP 617: New Parser
-------------------
New Parser
----------
Python 3.9 uses a new parser, based on `PEG
<https://en.wikipedia.org/wiki/Parsing_expression_grammar>`_ instead
@ -167,7 +223,6 @@ Other Language Changes
its top-level package.
(Contributed by Ngalim Siregar in :issue:`37444`.)
* Python now gets the absolute path of the script filename specified on
the command line (ex: ``python3 script.py``): the ``__file__`` attribute of
the :mod:`__main__` module became an absolute path, rather than a relative
@ -201,6 +256,17 @@ Other Language Changes
for the correspondent concrete type (``list`` in this case).
(Contributed by Serhiy Storchaka in :issue:`40257`.)
* Parallel running of :meth:`~agen.aclose` / :meth:`~agen.asend` /
:meth:`~agen.athrow` is now prohibited, and ``ag_running`` now reflects
the actual running status of the async generator.
(Contributed by Yury Selivanov in :issue:`30773`.)
* Unexpected errors in calling the ``__iter__`` method are no longer masked by
``TypeError`` in the :keyword:`in` operator and functions
:func:`~operator.contains`, :func:`~operator.indexOf` and
:func:`~operator.countOf` of the :mod:`operator` module.
(Contributed by Serhiy Storchaka in :issue:`40824`.)
New Modules
===========
@ -248,9 +314,10 @@ PyPI and maintained by the CPython core team.
graphlib
---------
Add the :mod:`graphlib` that contains the :class:`graphlib.TopologicalSorter` class
to offer functionality to perform topological sorting of graphs. (Contributed by Pablo
Galindo, Tim Peters and Larry Hastings in :issue:`17005`.)
A new module, :mod:`graphlib`, was added that contains the
:class:`graphlib.TopologicalSorter` class to offer functionality to perform
topological sorting of graphs. (Contributed by Pablo Galindo, Tim Peters and
Larry Hastings in :issue:`17005`.)
Improved Modules
@ -296,6 +363,15 @@ loop, and essentially works as a high-level version of
:meth:`~asyncio.loop.run_in_executor` that can directly take keyword arguments.
(Contributed by Kyle Stanley and Yury Selivanov in :issue:`32309`.)
When cancelling the task due to a timeout, :meth:`asyncio.wait_for` will now
wait until the cancellation is complete also in the case when *timeout* is
<= 0, like it does with positive timeouts.
(Contributed by Elvis Pranskevichus in :issue:`32751`.)
:mod:`asyncio` now raises :exc:`TyperError` when calling incompatible
methods with an :class:`ssl.SSLSocket` socket.
(Contributed by Ido Michael in :issue:`37404`.)
compileall
----------
@ -328,7 +404,7 @@ startup overhead and reduces the amount of lost CPU time to idle workers.
curses
------
Add :func:`curses.get_escdelay`, :func:`curses.set_escdelay`,
Added :func:`curses.get_escdelay`, :func:`curses.set_escdelay`,
:func:`curses.get_tabsize`, and :func:`curses.set_tabsize` functions.
(Contributed by Anthony Sottile in :issue:`38312`.)
@ -375,12 +451,17 @@ finalized by the garbage collector. (Contributed by Pablo Galindo in
hashlib
-------
The :mod:`hashlib` module can now use SHA3 hashes and SHAKE XOF from OpenSSL
when available.
(Contributed by Christian Heimes in :issue:`37630`.)
Builtin hash modules can now be disabled with
``./configure --without-builtin-hashlib-hashes`` or selectively enabled with
e.g. ``./configure --with-builtin-hashlib-hashes=sha3,blake2`` to force use
of OpenSSL based implementation.
(Contributed by Christian Heimes in :issue:`40479`)
http
----
@ -390,13 +471,13 @@ HTTP status codes ``103 EARLY_HINTS``, ``418 IM_A_TEAPOT`` and ``425 TOO_EARLY``
IDLE and idlelib
----------------
Add option to toggle cursor blink off. (Contributed by Zackery Spytz
Added option to toggle cursor blink off. (Contributed by Zackery Spytz
in :issue:`4603`.)
Escape key now closes IDLE completion windows. (Contributed by Johnny
Najera in :issue:`38944`.)
Add keywords to module name completion list. (Contributed by Terry J.
Added keywords to module name completion list. (Contributed by Terry J.
Reedy in :issue:`37765`.)
The changes above have been backported to 3.8 maintenance releases.
@ -426,6 +507,17 @@ now raises :exc:`ImportError` instead of :exc:`ValueError` for invalid relative
import attempts.
(Contributed by Ngalim Siregar in :issue:`37444`.)
Import loaders which publish immutable module objects can now publish
immutable packages in addition to individual modules.
(Contributed by Dino Viehland in :issue:`39336`.)
Added :func:`importlib.resources.files` function with support for
subdirectories in package data, matching backport in ``importlib_resources``
version 1.5.
(Contributed by Jason R. Coombs in :issue:`39791`.)
Refreshed ``importlib.metadata`` from ``importlib_metadata`` version 1.6.1.
inspect
-------
@ -448,15 +540,15 @@ Expanded the :func:`math.gcd` function to handle multiple arguments.
Formerly, it only supported two arguments.
(Contributed by Serhiy Storchaka in :issue:`39648`.)
Add :func:`math.lcm`: return the least common multiple of specified arguments.
Added :func:`math.lcm`: return the least common multiple of specified arguments.
(Contributed by Mark Dickinson, Ananthakrishnan and Serhiy Storchaka in
:issue:`39479` and :issue:`39648`.)
Add :func:`math.nextafter`: return the next floating-point value after *x*
Added :func:`math.nextafter`: return the next floating-point value after *x*
towards *y*.
(Contributed by Victor Stinner in :issue:`39288`.)
Add :func:`math.ulp`: return the value of the least significant bit
Added :func:`math.ulp`: return the value of the least significant bit
of a float.
(Contributed by Victor Stinner in :issue:`39310`.)
@ -492,7 +584,7 @@ The :func:`os.putenv` and :func:`os.unsetenv` functions are now always
available.
(Contributed by Victor Stinner in :issue:`39395`.)
Add :func:`os.waitstatus_to_exitcode` function:
Added :func:`os.waitstatus_to_exitcode` function:
convert a wait status to an exit code.
(Contributed by Victor Stinner in :issue:`40094`.)
@ -503,6 +595,12 @@ Added :meth:`pathlib.Path.readlink()` which acts similarly to
:func:`os.readlink`.
(Contributed by Girts Folkmanis in :issue:`30618`)
pdb
---
On Windows now :class:`~pdb.Pdb` supports ``~/.pdbrc``.
(Contributed by Tim Hopper and Dan Lidral-Porter in :issue:`20523`.)
poplib
------
@ -526,7 +624,7 @@ method etc, but for any object that has its own ``__doc__`` attribute.
random
------
Add a new :attr:`random.Random.randbytes` method: generate random bytes.
Added a new :attr:`random.Random.randbytes` method: generate random bytes.
(Contributed by Victor Stinner in :issue:`40286`.)
signal
@ -555,6 +653,11 @@ constant on Linux 4.1 and greater.
The socket module now supports the :data:`~socket.CAN_J1939` protocol on
platforms that support it. (Contributed by Karl Ding in :issue:`40291`.)
The socket module now has the :func:`socket.send_fds` and
:func:`socket.recv.fds` methods. (Contributed by Joannah Nanjekye, Shinya
Okano and Victor Stinner in :issue:`28724`.)
time
----
@ -566,7 +669,7 @@ which has nanosecond resolution, rather than
sys
---
Add a new :attr:`sys.platlibdir` attribute: name of the platform-specific
Added a new :attr:`sys.platlibdir` attribute: name of the platform-specific
library directory. It is used to build the path of standard library and the
paths of installed extension modules. It is equal to ``"lib"`` on most
platforms. On Fedora and SuSE, it is equal to ``"lib64"`` on 64-bit platforms.
@ -630,7 +733,7 @@ Optimizations
(Contributed by Serhiy Storchaka in :issue:`32856`.)
* Optimize signal handling in multithreaded applications. If a thread different
* Optimized signal handling in multithreaded applications. If a thread different
than the main thread gets a signal, the bytecode evaluation loop is no longer
interrupted at each bytecode instruction to check for pending signals which
cannot be handled. Only the main thread of the main interpreter can handle
@ -640,10 +743,36 @@ Optimizations
until the main thread handles signals.
(Contributed by Victor Stinner in :issue:`40010`.)
* Optimize the :mod:`subprocess` module on FreeBSD using ``closefrom()``.
* Optimized the :mod:`subprocess` module on FreeBSD using ``closefrom()``.
(Contributed by Ed Maste, Conrad Meyer, Kyle Evans, Kubilay Kocak and Victor
Stinner in :issue:`38061`.)
* :c:func:`PyLong_FromDouble` is now up to 1.87x faster for values that
fit into :c:type:`long`.
(Contributed by Sergey Fedoseev in :issue:`37986`.)
* A number of Python builtins (:class:`range`, :class:`tuple`, :class:`set`,
:class:`frozenset`, :class:`list`, :class:`dict`) are now sped up by using
:pep:`590` vectorcall protocol.
(Contributed by Dong-hee Na, Mark Shannon, Jeroen Demeyer and Petr Viktorin in :issue:`37207`.)
* Optimized :func:`~set.difference_update` for the case when the other set
is much larger than the base set.
(Suggested by Evgeny Kapun with code contributed by Michele Orrù in :issue:`8425`.)
* Python's small object allocator (``obmalloc.c``) now allows (no more than)
one empty arena to remain available for immediate reuse, without returning
it to the OS. This prevents thrashing in simple loops where an arena could
be created and destroyed anew on each iteration.
(Contributed by Tim Peters in :issue:`37257`.)
* :term:`floor division` of float operation now has a better performance. Also
the message of :exc:`ZeroDivisionError` for this operation is updated.
(Contributed by Dong-hee Na in :issue:`39434`.)
* Decoding short ASCII strings with UTF-8 and ascii codecs is now about
15% faster. (Contributed by Inada Naoki in :issue:`37348`.)
Here's a summary of performance improvements from Python 3.4 through Python 3.9:
.. code-block:: none
@ -699,10 +828,6 @@ in nanoseconds. The benchmarks were measured on an
running the macOS 64-bit builds found at
`python.org <https://www.python.org/downloads/mac-osx/>`_.
* A number of Python builtins (:class:`range`, :class:`tuple`, :class:`set`, :class:`frozenset`, :class:`list`, :class:`dict`)
are now sped up by using :pep:`590` vectorcall protocol.
(Contributed by Dong-hee Na, Mark Shannon, Jeroen Demeyer and Petr Viktorin in :issue:`37207`.)
Deprecated
==========
@ -784,6 +909,9 @@ Deprecated
* Passing ``None`` as the first argument to the :func:`shlex.split` function
has been deprecated. (Contributed by Zackery Spytz in :issue:`33262`.)
* :func:`smtpd.MailmanProxy` is now deprecated as it is unusable without
an external module, ``mailman``. (Contributed by Samuel Colvin in :issue:`35800`.)
* The :mod:`lib2to3` module now emits a :exc:`PendingDeprecationWarning`.
Python 3.9 switched to a PEG parser (see :pep:`617`), and Python 3.10 may
include new language syntax that is not parsable by lib2to3's LL(1) parser.
@ -798,6 +926,8 @@ Deprecated
.. _LibCST: https://libcst.readthedocs.io/
.. _parso: https://parso.readthedocs.io/
.. _removed-in-python-39:
Removed
=======
@ -967,6 +1097,19 @@ Changes in the Python API
of ``wchar_t`` since Python 3.3.
(Contributed by Inada Naoki in :issue:`34538`.)
* The :func:`logging.getLogger` API now returns the root logger when passed
the name ``'root'``, whereas previously it returned a non-root logger named
``'root'``. This could affect cases where user code explicitly wants a
non-root logger named ``'root'``, or instantiates a logger using
``logging.getLogger(__name__)`` in some top-level module called ``'root.py'``.
(Contributed by Vinay Sajip in :issue:`37742`.)
* Division handling of :class:`~pathlib.PurePath` now returns ``NotImplemented``
instead of raising a :exc:`TypeError` when passed something other than an
instance of ``str`` or :class:`~pathlib.PurePath`. This allows creating
compatible classes that don't inherit from those mentioned types.
(Contributed by Roger Aiudi in :issue:`34775`).
Changes in the C API
--------------------
@ -1017,6 +1160,11 @@ Changes in the C API
(See :issue:`35810` and :issue:`40217` for more information.)
* The functions ``PyEval_CallObject``, ``PyEval_CallFunction``,
``PyEval_CallMethod`` and ``PyEval_CallObjectWithKeywords`` are deprecated.
Use :c:func:`PyObject_Call` and its variants instead.
(See more details in :issue:`29548`.)
CPython bytecode changes
------------------------
@ -1025,11 +1173,21 @@ CPython bytecode changes
correctly if the :exc:`AssertionError` exception was being shadowed.
(Contributed by Zackery Spytz in :issue:`34880`.)
* The :opcode:`COMPARE_OP` opcode was split into four distinct instructions:
* ``COMPARE_OP`` for rich comparisons
* ``IS_OP`` for 'is' and 'is not' tests
* ``CONTAINS_OP`` for 'in' and 'not in' tests
* ``JUMP_IF_NOT_EXC_MATCH`` for checking exceptions in 'try-except'
statements.
(Contributed by Mark Shannon in :issue:`39156`.)
Build Changes
=============
* Add ``--with-platlibdir`` option to the ``configure`` script: name of the
* Added ``--with-platlibdir`` option to the ``configure`` script: name of the
platform-specific library directory, stored in the new :attr:`sys.platlibdir`
attribute. See :attr:`sys.platlibdir` attribute for more information.
(Contributed by Jan Matějek, Matěj Cepl, Charalampos Stratakis
@ -1042,6 +1200,34 @@ Build Changes
functions are now required to build Python.
(Contributed by Victor Stinner in :issue:`39395`.)
* On non-Windows platforms, creating ``bdist_wininst`` installers is now
officially unsupported. (See :issue:`10945` for more details.)
* When building Python on macOS from source, ``_tkinter`` now links with
non-system Tcl and Tk frameworks if they are installed in
``/Library/Frameworks``, as had been the case on older releases
of macOS. If a macOS SDK is explicitly configured, by using
``--enable-universalsdk=`` or ``-isysroot``, only the SDK itself is
searched. The default behavior can still be overridden with
``--with-tcltk-includes`` and ``--with-tcltk-libs``.
(Contributed by Ned Deily in :issue:`34956`.)
* Python can now be built for Windows 10 ARM64.
(Contributed by Steve Dower in :issue:`33125`.)
* Some individual tests are now skipped when ``--pgo`` is used. The tests
in question increased the PGO task time significantly and likely
didn't help improve optimization of the final executable. This
speeds up the task by a factor of about 15x. Running the full unit test
suite is slow. This change may result in a slightly less optimized build
since not as many code branches will be executed. If you are willing to
wait for the much slower build, the old behavior can be restored using
``./configure [..] PROFILE_TASK="-m test --pgo-extended"``. We make no
guarantees as to which PGO task set produces a faster build. Users who care
should run their own relevant benchmarks as results can depend on the
environment, workload, and compiler tool chain.
(See :issue:`36044` and :issue:`37707` for more details.)
C API Changes
=============
@ -1049,29 +1235,29 @@ C API Changes
New Features
------------
* :pep:`573`: Add :c:func:`PyType_FromModuleAndSpec` to associate
* :pep:`573`: Added :c:func:`PyType_FromModuleAndSpec` to associate
a module with a class; :c:func:`PyType_GetModule` and
:c:func:`PyType_GetModuleState` to retrieve the module and its state; and
:c:data:`PyCMethod` and :c:data:`METH_METHOD` to allow a method to
access the class it was defined in.
(Contributed by Marcel Plch and Petr Viktorin in :issue:`38787`.)
* Add :c:func:`PyFrame_GetCode` function: get a frame code.
Add :c:func:`PyFrame_GetBack` function: get the frame next outer frame.
* Added :c:func:`PyFrame_GetCode` function: get a frame code.
Added :c:func:`PyFrame_GetBack` function: get the frame next outer frame.
(Contributed by Victor Stinner in :issue:`40421`.)
* Add :c:func:`PyFrame_GetLineNumber` to the limited C API.
* Added :c:func:`PyFrame_GetLineNumber` to the limited C API.
(Contributed by Victor Stinner in :issue:`40421`.)
* Add :c:func:`PyThreadState_GetInterpreter` and
* Added :c:func:`PyThreadState_GetInterpreter` and
:c:func:`PyInterpreterState_Get` functions to get the interpreter.
Add :c:func:`PyThreadState_GetFrame` function to get the current frame of a
Added :c:func:`PyThreadState_GetFrame` function to get the current frame of a
Python thread state.
Add :c:func:`PyThreadState_GetID` function: get the unique identifier of a
Added :c:func:`PyThreadState_GetID` function: get the unique identifier of a
Python thread state.
(Contributed by Victor Stinner in :issue:`39947`.)
* Add a new public :c:func:`PyObject_CallNoArgs` function to the C API, which
* Added a new public :c:func:`PyObject_CallNoArgs` function to the C API, which
calls a callable Python object without any arguments. It is the most efficient
way to call a callable Python object without any argument.
(Contributed by Victor Stinner in :issue:`37194`.)
@ -1093,11 +1279,19 @@ New Features
to a module.
(Contributed by Dong-hee Na in :issue:`40024`.)
* Add the functions :c:func:`PyObject_GC_IsTracked` and
* Added the functions :c:func:`PyObject_GC_IsTracked` and
:c:func:`PyObject_GC_IsFinalized` to the public API to allow to query if
Python objects are being currently tracked or have been already finalized by
the garbage collector respectively. (Contributed by Pablo Galindo in
:issue:`40241`.)
the garbage collector respectively.
(Contributed by Pablo Galindo Salgado in :issue:`40241`.)
* Added :c:func:`_PyObject_FunctionStr` to get a user-friendly string
representation of a function-like object.
(Patch by Jeroen Demeyer in :issue:`37645`.)
* Added :c:func:`PyObject_CallOneArg` for calling an object with one
positional argument
(Patch by Jeroen Demeyer in :issue:`37483`.)
Porting to Python 3.9
@ -1144,10 +1338,44 @@ Porting to Python 3.9
Python 3.3.
(Contributed by Inada Naoki in :issue:`36346`.)
* The :c:func:`Py_FatalError` function is replaced with a macro which logs
automatically the name of the current function, unless the
``Py_LIMITED_API`` macro is defined.
(Contributed by Victor Stinner in :issue:`39882`.)
* The vectorcall protocol now requires that the caller passes only strings as
keyword names. (See :issue:`37540` for more information.)
* Implementation details of a number of macros and functions are now hidden:
* :c:func:`PyObject_IS_GC` macro was converted to a function.
* The :c:func:`PyObject_NEW` macro becomes an alias to the
:c:func:`PyObject_New` macro, and the :c:func:`PyObject_NEW_VAR` macro
becomes an alias to the :c:func:`PyObject_NewVar` macro. They no longer
access directly the :c:member:`PyTypeObject.tp_basicsize` member.
* :c:func:`PyType_HasFeature` now always calls :c:func:`PyType_GetFlags`.
Previously, it accessed directly the :c:member:`PyTypeObject.tp_flags`
member when the limited C API was not used.
* :c:func:`PyObject_GET_WEAKREFS_LISTPTR` macro was converted to a function:
the macro accessed directly the :c:member:`PyTypeObject.tp_weaklistoffset`
member.
* :c:func:`PyObject_CheckBuffer` macro was converted to a function: the macro
accessed directly the :c:member:`PyTypeObject.tp_as_buffer` member.
* :c:func:`PyIndex_Check` is now always declared as an opaque function to hide
implementation details: removed the ``PyIndex_Check()`` macro. The macro accessed
directly the :c:member:`PyTypeObject.tp_as_number` member.
(See :issue:`40170` for more details.)
Removed
-------
* Exclude ``PyFPE_START_PROTECT()`` and ``PyFPE_END_PROTECT()`` macros of
* Excluded ``PyFPE_START_PROTECT()`` and ``PyFPE_END_PROTECT()`` macros of
``pyfpe.h`` from the limited C API.
(Contributed by Victor Stinner in :issue:`38835`.)
@ -1158,7 +1386,7 @@ Removed
* Changes in the limited C API (if ``Py_LIMITED_API`` macro is defined):
* Exclude the following functions from the limited C API:
* Excluded the following functions from the limited C API:
* ``PyThreadState_DeleteCurrent()``
(Contributed by Joannah Nanjekye in :issue:`37878`.)
@ -1175,7 +1403,7 @@ Removed
* ``Py_TRASHCAN_SAFE_BEGIN``
* ``Py_TRASHCAN_SAFE_END``
* Move following functions and definitions to the internal C API:
* Moved following functions and definitions to the internal C API:
* ``_PyDebug_PrintTotalRefs()``
* ``_Py_PrintReferences()``
@ -1185,12 +1413,12 @@ Removed
(Contributed by Victor Stinner in :issue:`38644` and :issue:`39542`.)
* Remove ``_PyRuntime.getframe`` hook and remove ``_PyThreadState_GetFrame``
* Removed ``_PyRuntime.getframe`` hook and removed ``_PyThreadState_GetFrame``
macro which was an alias to ``_PyRuntime.getframe``. They were only exposed
by the internal C API. Remove also ``PyThreadFrameGetter`` type.
by the internal C API. Removed also ``PyThreadFrameGetter`` type.
(Contributed by Victor Stinner in :issue:`39946`.)
* Remove the following functions from the C API. Call :c:func:`PyGC_Collect`
* Removed the following functions from the C API. Call :c:func:`PyGC_Collect`
explicitly to clear all free lists.
(Contributed by Inada Naoki and Victor Stinner in :issue:`37340`,
:issue:`38896` and :issue:`40428`.)
@ -1209,10 +1437,20 @@ Removed
* ``PyUnicode_ClearFreeList()``: the Unicode free list has been removed in
Python 3.3.
* Remove ``_PyUnicode_ClearStaticStrings()`` function.
* Removed ``_PyUnicode_ClearStaticStrings()`` function.
(Contributed by Victor Stinner in :issue:`39465`.)
* Remove ``Py_UNICODE_MATCH``. It has been deprecated by :pep:`393`, and
* Removed ``Py_UNICODE_MATCH``. It has been deprecated by :pep:`393`, and
broken since Python 3.3. The :c:func:`PyUnicode_Tailmatch` function can be
used instead.
(Contributed by Inada Naoki in :issue:`36346`.)
* Cleaned header files of interfaces defined but with no implementation.
The public API symbols being removed are:
``_PyBytes_InsertThousandsGroupingLocale``,
``_PyBytes_InsertThousandsGrouping``, ``_Py_InitializeFromArgs``,
``_Py_InitializeFromWideArgs``, ``_PyFloat_Repr``, ``_PyFloat_Digits``,
``_PyFloat_DigitsInit``, ``PyFrame_ExtendStack``, ``_PyAIterWrapper_Type``,
``PyNullImporter_Type``, ``PyCmpWrapper_Type``, ``PySortWrapper_Type``,
``PyNoArgsFunction``.
(Contributed by Pablo Galindo Salgado in :issue:`39372`.)

View File

@ -428,7 +428,7 @@ compare_op_bitwise_or_pair[CmpopExprPair*]:
| is_bitwise_or
eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
noteq_bitwise_or[CmpopExprPair*]:
| (tok='!=' {_PyPegen_check_barry_as_flufl(p) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
| (tok='!=' { _PyPegen_check_barry_as_flufl(p, tok) ? NULL : tok}) a=bitwise_or {_PyPegen_cmpop_expr_pair(p, NotEq, a) }
lte_bitwise_or[CmpopExprPair*]: '<=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, LtE, a) }
lt_bitwise_or[CmpopExprPair*]: '<' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Lt, a) }
gte_bitwise_or[CmpopExprPair*]: '>=' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, GtE, a) }
@ -475,6 +475,7 @@ await_primary[expr_ty] (memo):
| AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _Py_Await(a, EXTRA)) }
| primary
primary[expr_ty]:
| invalid_primary # must be before 'primay genexp' because of invalid_genexp
| a=primary '.' b=NAME { _Py_Attribute(a, b->v.Name.id, Load, EXTRA) }
| a=primary b=genexp { _Py_Call(a, CHECK(asdl_expr_seq*, (asdl_expr_seq*)_PyPegen_singleton_seq(p, b)), NULL, EXTRA) }
| a=primary '(' b=[arguments] ')' {
@ -551,7 +552,7 @@ yield_expr[expr_ty]:
arguments[expr_ty] (memo):
| a=args [','] &')' { a }
| incorrect_arguments
| invalid_arguments
args[expr_ty]:
| a[asdl_expr_seq*]=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
| a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
@ -637,7 +638,7 @@ t_atom[expr_ty]:
# From here on, there are rules for invalid syntax with specialised error messages
incorrect_arguments:
invalid_arguments:
| args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
| a=expression for_if_clauses ',' [args | expression for_if_clauses] {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
@ -682,6 +683,8 @@ invalid_del_stmt:
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
invalid_block:
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
invalid_primary:
| primary a='{' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid syntax") }
invalid_comprehension:
| ('[' | '(' | '{') a=starred_expression for_if_clauses {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }

2
Include/Python-ast.h generated
View File

@ -1,4 +1,4 @@
/* File automatically generated by Parser/asdl_c.py. */
// File automatically generated by Parser/asdl_c.py.
#ifndef Py_PYTHON_AST_H
#define Py_PYTHON_AST_H

View File

@ -22,8 +22,8 @@ PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct;
#define Py_True ((PyObject *) &_Py_TrueStruct)
/* Macros for returning Py_True or Py_False, respectively */
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
#define Py_RETURN_TRUE return Py_NewRef(Py_True)
#define Py_RETURN_FALSE return Py_NewRef(Py_False)
/* Function to return a bool from a C long */
PyAPI_FUNC(PyObject *) PyBool_FromLong(long);

View File

@ -46,7 +46,8 @@ PyAPI_FUNC(int) _PyDict_Next(
/* Get the number of items of a dictionary. */
#define PyDict_GET_SIZE(mp) (assert(PyDict_Check(mp)),((PyDictObject *)mp)->ma_used)
PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash);
PyAPI_FUNC(int) _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, struct _Py_Identifier *);
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
@ -63,7 +64,6 @@ PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
argument is raised.
*/
PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
PyAPI_FUNC(PyObject *) _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key);
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item);
PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, struct _Py_Identifier *key);

View File

@ -41,7 +41,7 @@ PyAPI_FUNC(PyStatus) PyWideStringList_Insert(PyWideStringList *list,
/* --- PyPreConfig ----------------------------------------------- */
typedef struct {
typedef struct PyPreConfig {
int _config_init; /* _PyConfigInitEnum value */
/* Parse Py_PreInitializeFromBytesArgs() arguments?
@ -127,296 +127,84 @@ PyAPI_FUNC(void) PyPreConfig_InitIsolatedConfig(PyPreConfig *config);
/* --- PyConfig ---------------------------------------------- */
typedef struct {
/* This structure is best documented in the Doc/c-api/init_config.rst file. */
typedef struct PyConfig {
int _config_init; /* _PyConfigInitEnum value */
int isolated; /* Isolated mode? see PyPreConfig.isolated */
int use_environment; /* Use environment variables? see PyPreConfig.use_environment */
int dev_mode; /* Python Development Mode? See PyPreConfig.dev_mode */
/* Install signal handlers? Yes by default. */
int isolated;
int use_environment;
int dev_mode;
int install_signal_handlers;
int use_hash_seed; /* PYTHONHASHSEED=x */
int use_hash_seed;
unsigned long hash_seed;
/* Enable faulthandler?
Set to 1 by -X faulthandler and PYTHONFAULTHANDLER. -1 means unset. */
int faulthandler;
/* Enable tracemalloc?
Set by -X tracemalloc=N and PYTHONTRACEMALLOC. -1 means unset */
int tracemalloc;
int import_time; /* PYTHONPROFILEIMPORTTIME, -X importtime */
int show_ref_count; /* -X showrefcount */
int dump_refs; /* PYTHONDUMPREFS */
int malloc_stats; /* PYTHONMALLOCSTATS */
/* Python filesystem encoding and error handler:
sys.getfilesystemencoding() and sys.getfilesystemencodeerrors().
Default encoding and error handler:
* if Py_SetStandardStreamEncoding() has been called: they have the
highest priority;
* PYTHONIOENCODING environment variable;
* The UTF-8 Mode uses UTF-8/surrogateescape;
* If Python forces the usage of the ASCII encoding (ex: C locale
or POSIX locale on FreeBSD or HP-UX), use ASCII/surrogateescape;
* locale encoding: ANSI code page on Windows, UTF-8 on Android and
VxWorks, LC_CTYPE locale encoding on other platforms;
* On Windows, "surrogateescape" error handler;
* "surrogateescape" error handler if the LC_CTYPE locale is "C" or "POSIX";
* "surrogateescape" error handler if the LC_CTYPE locale has been coerced
(PEP 538);
* "strict" error handler.
Supported error handlers: "strict", "surrogateescape" and
"surrogatepass". The surrogatepass error handler is only supported
if Py_DecodeLocale() and Py_EncodeLocale() use directly the UTF-8 codec;
it's only used on Windows.
initfsencoding() updates the encoding to the Python codec name.
For example, "ANSI_X3.4-1968" is replaced with "ascii".
On Windows, sys._enablelegacywindowsfsencoding() sets the
encoding/errors to mbcs/replace at runtime.
See Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors.
*/
int import_time;
int show_ref_count;
int dump_refs;
int malloc_stats;
wchar_t *filesystem_encoding;
wchar_t *filesystem_errors;
wchar_t *pycache_prefix; /* PYTHONPYCACHEPREFIX, -X pycache_prefix=PATH */
int parse_argv; /* Parse argv command line arguments? */
/* Command line arguments (sys.argv).
Set parse_argv to 1 to parse argv as Python command line arguments
and then strip Python arguments from argv.
If argv is empty, an empty string is added to ensure that sys.argv
always exists and is never empty. */
wchar_t *pycache_prefix;
int parse_argv;
PyWideStringList orig_argv;
PyWideStringList argv;
/* Program name:
- If Py_SetProgramName() was called, use its value.
- On macOS, use PYTHONEXECUTABLE environment variable if set.
- If WITH_NEXT_FRAMEWORK macro is defined, use __PYVENV_LAUNCHER__
environment variable is set.
- Use argv[0] if available and non-empty.
- Use "python" on Windows, or "python3 on other platforms. */
wchar_t *program_name;
PyWideStringList xoptions; /* Command line -X options */
/* Warnings options: lowest to highest priority. warnings.filters
is built in the reverse order (highest to lowest priority). */
PyWideStringList xoptions;
PyWideStringList warnoptions;
/* If equal to zero, disable the import of the module site and the
site-dependent manipulations of sys.path that it entails. Also disable
these manipulations if site is explicitly imported later (call
site.main() if you want them to be triggered).
Set to 0 by the -S command line option. If set to -1 (default), it is
set to !Py_NoSiteFlag. */
int site_import;
/* Bytes warnings:
* If equal to 1, issue a warning when comparing bytes or bytearray with
str or bytes with int.
* If equal or greater to 2, issue an error.
Incremented by the -b command line option. If set to -1 (default), inherit
Py_BytesWarningFlag value. */
int bytes_warning;
/* If greater than 0, enable inspect: when a script is passed as first
argument or the -c option is used, enter interactive mode after
executing the script or the command, even when sys.stdin does not appear
to be a terminal.
Incremented by the -i command line option. Set to 1 if the PYTHONINSPECT
environment variable is non-empty. If set to -1 (default), inherit
Py_InspectFlag value. */
int inspect;
/* If greater than 0: enable the interactive mode (REPL).
Incremented by the -i command line option. If set to -1 (default),
inherit Py_InteractiveFlag value. */
int interactive;
/* Optimization level.
Incremented by the -O command line option. Set by the PYTHONOPTIMIZE
environment variable. If set to -1 (default), inherit Py_OptimizeFlag
value. */
int optimization_level;
/* If greater than 0, enable the debug mode: turn on parser debugging
output (for expert only, depending on compilation options).
Incremented by the -d command line option. Set by the PYTHONDEBUG
environment variable. If set to -1 (default), inherit Py_DebugFlag
value. */
int parser_debug;
/* If equal to 0, Python won't try to write ``.pyc`` files on the
import of source modules.
Set to 0 by the -B command line option and the PYTHONDONTWRITEBYTECODE
environment variable. If set to -1 (default), it is set to
!Py_DontWriteBytecodeFlag. */
int write_bytecode;
/* If greater than 0, enable the verbose mode: print a message each time a
module is initialized, showing the place (filename or built-in module)
from which it is loaded.
If greater or equal to 2, print a message for each file that is checked
for when searching for a module. Also provides information on module
cleanup at exit.
Incremented by the -v option. Set by the PYTHONVERBOSE environment
variable. If set to -1 (default), inherit Py_VerboseFlag value. */
int verbose;
/* If greater than 0, enable the quiet mode: Don't display the copyright
and version messages even in interactive mode.
Incremented by the -q option. If set to -1 (default), inherit
Py_QuietFlag value. */
int quiet;
/* If greater than 0, don't add the user site-packages directory to
sys.path.
Set to 0 by the -s and -I command line options , and the PYTHONNOUSERSITE
environment variable. If set to -1 (default), it is set to
!Py_NoUserSiteDirectory. */
int user_site_directory;
/* If non-zero, configure C standard steams (stdio, stdout,
stderr):
- Set O_BINARY mode on Windows.
- If buffered_stdio is equal to zero, make streams unbuffered.
Otherwise, enable streams buffering if interactive is non-zero. */
int configure_c_stdio;
/* If equal to 0, enable unbuffered mode: force the stdout and stderr
streams to be unbuffered.
Set to 0 by the -u option. Set by the PYTHONUNBUFFERED environment
variable.
If set to -1 (default), it is set to !Py_UnbufferedStdioFlag. */
int buffered_stdio;
/* Encoding of sys.stdin, sys.stdout and sys.stderr.
Value set from PYTHONIOENCODING environment variable and
Py_SetStandardStreamEncoding() function.
See also 'stdio_errors' attribute. */
wchar_t *stdio_encoding;
/* Error handler of sys.stdin and sys.stdout.
Value set from PYTHONIOENCODING environment variable and
Py_SetStandardStreamEncoding() function.
See also 'stdio_encoding' attribute. */
wchar_t *stdio_errors;
#ifdef MS_WINDOWS
/* If greater than zero, use io.FileIO instead of WindowsConsoleIO for sys
standard streams.
Set to 1 if the PYTHONLEGACYWINDOWSSTDIO environment variable is set to
a non-empty string. If set to -1 (default), inherit
Py_LegacyWindowsStdioFlag value.
See PEP 528 for more details. */
int legacy_windows_stdio;
#endif
/* Value of the --check-hash-based-pycs command line option:
- "default" means the 'check_source' flag in hash-based pycs
determines invalidation
- "always" causes the interpreter to hash the source file for
invalidation regardless of value of 'check_source' bit
- "never" causes the interpreter to always assume hash-based pycs are
valid
The default value is "default".
See PEP 552 "Deterministic pycs" for more details. */
wchar_t *check_hash_pycs_mode;
/* --- Path configuration inputs ------------ */
/* If greater than 0, suppress _PyPathConfig_Calculate() warnings on Unix.
The parameter has no effect on Windows.
If set to -1 (default), inherit !Py_FrozenFlag value. */
int pathconfig_warnings;
wchar_t *pythonpath_env; /* PYTHONPATH environment variable */
wchar_t *home; /* PYTHONHOME environment variable,
see also Py_SetPythonHome(). */
wchar_t *program_name;
wchar_t *pythonpath_env;
wchar_t *home;
wchar_t *platlibdir;
/* --- Path configuration outputs ----------- */
int module_search_paths_set; /* If non-zero, use module_search_paths */
PyWideStringList module_search_paths; /* sys.path paths. Computed if
module_search_paths_set is equal
to zero. */
wchar_t *executable; /* sys.executable */
wchar_t *base_executable; /* sys._base_executable */
wchar_t *prefix; /* sys.prefix */
wchar_t *base_prefix; /* sys.base_prefix */
wchar_t *exec_prefix; /* sys.exec_prefix */
wchar_t *base_exec_prefix; /* sys.base_exec_prefix */
wchar_t *platlibdir; /* sys.platlibdir */
int module_search_paths_set;
PyWideStringList module_search_paths;
wchar_t *executable;
wchar_t *base_executable;
wchar_t *prefix;
wchar_t *base_prefix;
wchar_t *exec_prefix;
wchar_t *base_exec_prefix;
/* --- Parameter only used by Py_Main() ---------- */
/* Skip the first line of the source ('run_filename' parameter), allowing use of non-Unix forms of
"#!cmd". This is intended for a DOS specific hack only.
Set by the -x command line option. */
int skip_source_first_line;
wchar_t *run_command; /* -c command line argument */
wchar_t *run_module; /* -m command line argument */
wchar_t *run_filename; /* Trailing command line argument without -c or -m */
wchar_t *run_command;
wchar_t *run_module;
wchar_t *run_filename;
/* --- Private fields ---------------------------- */
/* Install importlib? If set to 0, importlib is not initialized at all.
Needed by freeze_importlib. */
// Install importlib? If equals to 0, importlib is not initialized at all.
// Needed by freeze_importlib.
int _install_importlib;
/* If equal to 0, stop Python initialization before the "main" phase */
// If equal to 0, stop Python initialization before the "main" phase.
int _init_main;
/* If non-zero, disallow threads, subprocesses, and fork.
Default: 0. */
// If non-zero, disallow threads, subprocesses, and fork.
// Default: 0.
int _isolated_interpreter;
/* The list of the original command line arguments passed to the Python
executable.
If 'orig_argv' list is empty and 'argv' is not a list only containing an
empty string, PyConfig_Read() copies 'argv' into 'orig_argv' before
modifying 'argv' (if 'parse_argv is non-zero).
_PyConfig_Write() initializes Py_GetArgcArgv() to this list. */
PyWideStringList orig_argv;
} PyConfig;
PyAPI_FUNC(void) PyConfig_InitPythonConfig(PyConfig *config);

View File

@ -296,6 +296,8 @@ PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
struct PyModuleDef;
PyAPI_FUNC(PyObject *) _PyType_GetModuleByDef(PyTypeObject *, struct PyModuleDef *);
struct _Py_Identifier;
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);

View File

@ -79,10 +79,6 @@ PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
/* Test if an object implements the garbage collector protocol */
PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);

View File

@ -167,6 +167,11 @@ PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
*/
PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
/* The implementation of sys._current_exceptions() Returns a dict mapping
thread id to that thread's current exception.
*/
PyAPI_FUNC(PyObject *) _PyThread_CurrentExceptions(void);
/* Routines for advanced debuggers, requested by David Beazley.
Don't use unless you know what you are doing! */
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
@ -188,6 +193,36 @@ PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *interp);
/* Get a copy of the current interpreter configuration.
Return 0 on success. Raise an exception and return -1 on error.
The caller must initialize 'config', using PyConfig_InitPythonConfig()
for example.
Python must be preinitialized to call this method.
The caller must hold the GIL. */
PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy(
struct PyConfig *config);
/* Set the configuration of the current interpreter.
This function should be called during or just after the Python
initialization.
Update the sys module with the new configuration. If the sys module was
modified directly after the Python initialization, these changes are lost.
Some configuration like faulthandler or warnoptions can be updated in the
configuration, but don't reconfigure Python (don't enable/disable
faulthandler and don't reconfigure warnings filters).
Return 0 on success. Raise an exception and return -1 on error.
The configuration should come from _PyInterpreterState_GetConfigCopy(). */
PyAPI_FUNC(int) _PyInterpreterState_SetConfig(
const struct PyConfig *config);
// Get the configuration of the currrent interpreter.
// The caller must hold the GIL.
PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);

View File

@ -0,0 +1,234 @@
// File automatically generated by Parser/asdl_c.py.
#ifndef Py_INTERNAL_AST_H
#define Py_INTERNAL_AST_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
struct ast_state {
int initialized;
PyObject *AST_type;
PyObject *Add_singleton;
PyObject *Add_type;
PyObject *And_singleton;
PyObject *And_type;
PyObject *AnnAssign_type;
PyObject *Assert_type;
PyObject *Assign_type;
PyObject *AsyncFor_type;
PyObject *AsyncFunctionDef_type;
PyObject *AsyncWith_type;
PyObject *Attribute_type;
PyObject *AugAssign_type;
PyObject *Await_type;
PyObject *BinOp_type;
PyObject *BitAnd_singleton;
PyObject *BitAnd_type;
PyObject *BitOr_singleton;
PyObject *BitOr_type;
PyObject *BitXor_singleton;
PyObject *BitXor_type;
PyObject *BoolOp_type;
PyObject *Break_type;
PyObject *Call_type;
PyObject *ClassDef_type;
PyObject *Compare_type;
PyObject *Constant_type;
PyObject *Continue_type;
PyObject *Del_singleton;
PyObject *Del_type;
PyObject *Delete_type;
PyObject *DictComp_type;
PyObject *Dict_type;
PyObject *Div_singleton;
PyObject *Div_type;
PyObject *Eq_singleton;
PyObject *Eq_type;
PyObject *ExceptHandler_type;
PyObject *Expr_type;
PyObject *Expression_type;
PyObject *FloorDiv_singleton;
PyObject *FloorDiv_type;
PyObject *For_type;
PyObject *FormattedValue_type;
PyObject *FunctionDef_type;
PyObject *FunctionType_type;
PyObject *GeneratorExp_type;
PyObject *Global_type;
PyObject *GtE_singleton;
PyObject *GtE_type;
PyObject *Gt_singleton;
PyObject *Gt_type;
PyObject *IfExp_type;
PyObject *If_type;
PyObject *ImportFrom_type;
PyObject *Import_type;
PyObject *In_singleton;
PyObject *In_type;
PyObject *Interactive_type;
PyObject *Invert_singleton;
PyObject *Invert_type;
PyObject *IsNot_singleton;
PyObject *IsNot_type;
PyObject *Is_singleton;
PyObject *Is_type;
PyObject *JoinedStr_type;
PyObject *LShift_singleton;
PyObject *LShift_type;
PyObject *Lambda_type;
PyObject *ListComp_type;
PyObject *List_type;
PyObject *Load_singleton;
PyObject *Load_type;
PyObject *LtE_singleton;
PyObject *LtE_type;
PyObject *Lt_singleton;
PyObject *Lt_type;
PyObject *MatMult_singleton;
PyObject *MatMult_type;
PyObject *Mod_singleton;
PyObject *Mod_type;
PyObject *Module_type;
PyObject *Mult_singleton;
PyObject *Mult_type;
PyObject *Name_type;
PyObject *NamedExpr_type;
PyObject *Nonlocal_type;
PyObject *NotEq_singleton;
PyObject *NotEq_type;
PyObject *NotIn_singleton;
PyObject *NotIn_type;
PyObject *Not_singleton;
PyObject *Not_type;
PyObject *Or_singleton;
PyObject *Or_type;
PyObject *Pass_type;
PyObject *Pow_singleton;
PyObject *Pow_type;
PyObject *RShift_singleton;
PyObject *RShift_type;
PyObject *Raise_type;
PyObject *Return_type;
PyObject *SetComp_type;
PyObject *Set_type;
PyObject *Slice_type;
PyObject *Starred_type;
PyObject *Store_singleton;
PyObject *Store_type;
PyObject *Sub_singleton;
PyObject *Sub_type;
PyObject *Subscript_type;
PyObject *Try_type;
PyObject *Tuple_type;
PyObject *TypeIgnore_type;
PyObject *UAdd_singleton;
PyObject *UAdd_type;
PyObject *USub_singleton;
PyObject *USub_type;
PyObject *UnaryOp_type;
PyObject *While_type;
PyObject *With_type;
PyObject *YieldFrom_type;
PyObject *Yield_type;
PyObject *__dict__;
PyObject *__doc__;
PyObject *__module__;
PyObject *_attributes;
PyObject *_fields;
PyObject *alias_type;
PyObject *annotation;
PyObject *arg;
PyObject *arg_type;
PyObject *args;
PyObject *argtypes;
PyObject *arguments_type;
PyObject *asname;
PyObject *ast;
PyObject *attr;
PyObject *bases;
PyObject *body;
PyObject *boolop_type;
PyObject *cause;
PyObject *cmpop_type;
PyObject *col_offset;
PyObject *comparators;
PyObject *comprehension_type;
PyObject *context_expr;
PyObject *conversion;
PyObject *ctx;
PyObject *decorator_list;
PyObject *defaults;
PyObject *elt;
PyObject *elts;
PyObject *end_col_offset;
PyObject *end_lineno;
PyObject *exc;
PyObject *excepthandler_type;
PyObject *expr_context_type;
PyObject *expr_type;
PyObject *finalbody;
PyObject *format_spec;
PyObject *func;
PyObject *generators;
PyObject *handlers;
PyObject *id;
PyObject *ifs;
PyObject *is_async;
PyObject *items;
PyObject *iter;
PyObject *key;
PyObject *keys;
PyObject *keyword_type;
PyObject *keywords;
PyObject *kind;
PyObject *kw_defaults;
PyObject *kwarg;
PyObject *kwonlyargs;
PyObject *left;
PyObject *level;
PyObject *lineno;
PyObject *lower;
PyObject *mod_type;
PyObject *module;
PyObject *msg;
PyObject *name;
PyObject *names;
PyObject *op;
PyObject *operand;
PyObject *operator_type;
PyObject *ops;
PyObject *optional_vars;
PyObject *orelse;
PyObject *posonlyargs;
PyObject *returns;
PyObject *right;
PyObject *simple;
PyObject *slice;
PyObject *step;
PyObject *stmt_type;
PyObject *tag;
PyObject *target;
PyObject *targets;
PyObject *test;
PyObject *type;
PyObject *type_comment;
PyObject *type_ignore_type;
PyObject *type_ignores;
PyObject *unaryop_type;
PyObject *upper;
PyObject *value;
PyObject *values;
PyObject *vararg;
PyObject *withitem_type;
};
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_AST_H */

View File

@ -50,6 +50,9 @@ PyAPI_FUNC(int) _Py_GetLocaleconvNumeric(
PyAPI_FUNC(void) _Py_closerange(int first, int last);
PyAPI_FUNC(wchar_t*) _Py_GetLocaleEncoding(void);
PyAPI_FUNC(PyObject*) _Py_GetLocaleEncodingObject(void);
#ifdef __cplusplus
}
#endif

View File

@ -161,7 +161,9 @@ struct _gc_runtime_state {
Py_ssize_t long_lived_pending;
};
PyAPI_FUNC(void) _PyGC_InitState(struct _gc_runtime_state *);
extern void _PyGC_InitState(struct _gc_runtime_state *);
extern Py_ssize_t _PyGC_CollectNoFail(PyThreadState *tstate);
// Functions to clear types free lists

View File

@ -44,6 +44,8 @@ struct pyruntimestate;
#define _PyStatus_UPDATE_FUNC(err) \
do { err.func = _PyStatus_GET_FUNC(); } while (0)
PyObject* _PyErr_SetFromPyStatus(PyStatus status);
/* --- PyWideStringList ------------------------------------------------ */
#define _PyWideStringList_INIT (PyWideStringList){.length = 0, .items = NULL}
@ -150,12 +152,16 @@ extern PyStatus _PyConfig_Copy(
PyConfig *config,
const PyConfig *config2);
extern PyStatus _PyConfig_InitPathConfig(PyConfig *config);
extern PyStatus _PyConfig_Read(PyConfig *config, int compute_path_config);
extern PyStatus _PyConfig_Write(const PyConfig *config,
struct pyruntimestate *runtime);
extern PyStatus _PyConfig_SetPyArgv(
PyConfig *config,
const _PyArgv *args);
PyAPI_FUNC(PyObject*) _PyConfig_AsDict(const PyConfig *config);
PyAPI_FUNC(int) _PyConfig_FromDict(PyConfig *config, PyObject *dict);
/* --- Function used for testing ---------------------------------- */

View File

@ -8,10 +8,11 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif
#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_gil.h" /* struct _gil_runtime_state */
#include "pycore_gc.h" /* struct _gc_runtime_state */
#include "pycore_warnings.h" /* struct _warnings_runtime_state */
#include "pycore_atomic.h" // _Py_atomic_address
#include "pycore_ast.h" // struct ast_state
#include "pycore_gil.h" // struct _gil_runtime_state
#include "pycore_gc.h" // struct _gc_runtime_state
#include "pycore_warnings.h" // struct _warnings_runtime_state
struct _pending_calls {
PyThread_type_lock lock;
@ -163,6 +164,11 @@ struct _Py_exc_state {
#define _PY_NSMALLPOSINTS 257
#define _PY_NSMALLNEGINTS 5
// _PyLong_GetZero() and _PyLong_GetOne() must always be available
#if _PY_NSMALLPOSINTS < 2
# error "_PY_NSMALLPOSINTS must be greater than 1"
#endif
// The PyInterpreterState typedef is in Include/pystate.h.
struct _is {
@ -233,14 +239,12 @@ struct _is {
PyObject *audit_hooks;
#if _PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS > 0
/* Small integers are preallocated in this array so that they
can be shared.
The integers that are preallocated are those in the range
-_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (not inclusive).
*/
PyLongObject* small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS];
#endif
struct _Py_bytes_state bytes;
struct _Py_unicode_state unicode;
struct _Py_float_state float_state;
@ -255,15 +259,12 @@ struct _is {
struct _Py_async_gen_state async_gen;
struct _Py_context_state context;
struct _Py_exc_state exc_state;
struct ast_state ast;
};
/* Used by _PyImport_Cleanup() */
extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);
extern PyStatus _PyInterpreterState_SetConfig(
PyInterpreterState *interp,
const PyConfig *config);
extern void _PyInterpreterState_Clear(PyThreadState *tstate);
/* cross-interpreter data registry */

View File

@ -0,0 +1,43 @@
#ifndef Py_INTERNAL_LONG_H
#define Py_INTERNAL_LONG_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
#include "pycore_interp.h" // PyInterpreterState.small_ints
#include "pycore_pystate.h" // _PyThreadState_GET()
// Don't call this function but _PyLong_GetZero() and _PyLong_GetOne()
static inline PyObject* __PyLong_GetSmallInt_internal(int value)
{
PyThreadState *tstate = _PyThreadState_GET();
#ifdef Py_DEBUG
_Py_EnsureTstateNotNULL(tstate);
#endif
assert(-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS);
size_t index = _PY_NSMALLNEGINTS + value;
PyObject *obj = (PyObject*)tstate->interp->small_ints[index];
// _PyLong_GetZero() and _PyLong_GetOne() must not be called
// before _PyLong_Init() nor after _PyLong_Fini()
assert(obj != NULL);
return obj;
}
// Return a borrowed reference to the zero singleton.
// The function cannot return NULL.
static inline PyObject* _PyLong_GetZero(void)
{ return __PyLong_GetSmallInt_internal(0); }
// Return a borrowed reference to the one singleton.
// The function cannot return NULL.
static inline PyObject* _PyLong_GetOne(void)
{ return __PyLong_GetSmallInt_internal(1); }
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_LONG_H */

View File

@ -44,7 +44,7 @@ extern PyStatus _PySys_Create(
PyObject **sysmod_p);
extern PyStatus _PySys_ReadPreinitWarnOptions(PyWideStringList *options);
extern PyStatus _PySys_ReadPreinitXOptions(PyConfig *config);
extern int _PySys_InitMain(PyThreadState *tstate);
extern int _PySys_UpdateConfig(PyThreadState *tstate);
extern PyStatus _PyExc_Init(PyThreadState *tstate);
extern PyStatus _PyErr_Init(void);
extern PyStatus _PyBuiltins_AddExceptions(PyObject * bltinmod);
@ -84,7 +84,7 @@ extern void _PyFaulthandler_Fini(void);
extern void _PyHash_Fini(void);
extern void _PyTraceMalloc_Fini(void);
extern void _PyWarnings_Fini(PyInterpreterState *interp);
extern void _PyAST_Fini(PyThreadState *tstate);
extern void _PyAST_Fini(PyInterpreterState *interp);
extern PyStatus _PyGILState_Init(PyThreadState *tstate);
extern void _PyGILState_Fini(PyThreadState *tstate);

View File

@ -0,0 +1,34 @@
/* Unicode name database interface */
#ifndef Py_INTERNAL_UCNHASH_H
#define Py_INTERNAL_UCNHASH_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
/* revised ucnhash CAPI interface (exported through a "wrapper") */
#define PyUnicodeData_CAPSULE_NAME "unicodedata._ucnhash_CAPI"
typedef struct {
/* Get name for a given character code.
Returns non-zero if success, zero if not.
Does not set Python exceptions. */
int (*getname)(Py_UCS4 code, char* buffer, int buflen,
int with_alias_and_seq);
/* Get character code for a given name.
Same error handling as for getname(). */
int (*getcode)(const char* name, int namelen, Py_UCS4* code,
int with_named_seq);
} _PyUnicode_Name_CAPI;
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_UCNHASH_H */

View File

@ -10,6 +10,7 @@ extern "C" {
PyAPI_FUNC(PyObject *) _Py_Union(PyObject *args);
PyAPI_DATA(PyTypeObject) _Py_UnionType;
PyAPI_FUNC(PyObject *) _Py_union_type_or(PyObject* self, PyObject* param);
#ifdef __cplusplus
}

View File

@ -210,9 +210,6 @@ PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *);
#endif /* !Py_LIMITED_API */
#ifndef Py_LIMITED_API
PyAPI_DATA(PyObject *) _PyLong_Zero;
PyAPI_DATA(PyObject *) _PyLong_One;
PyAPI_FUNC(PyObject *) _PyLong_Rshift(PyObject *, size_t);
PyAPI_FUNC(PyObject *) _PyLong_Lshift(PyObject *, size_t);
#endif

View File

@ -73,7 +73,7 @@ PyAPI_FUNC(PyObject *) PyCMethod_New(PyMethodDef *, PyObject *,
#define METH_COEXIST 0x0040
#ifndef Py_LIMITED_API
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03100000
#define METH_FASTCALL 0x0080
#endif

View File

@ -136,7 +136,15 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywords(
void _PyArg_Fini(void);
#endif /* Py_LIMITED_API */
PyAPI_FUNC(int) PyModule_AddObject(PyObject *, const char *, PyObject *);
// Add an attribute with name 'name' and value 'obj' to the module 'mod.
// On success, return 0 on success.
// On error, raise an exception and return -1.
PyAPI_FUNC(int) PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value);
// Similar to PyModule_AddObjectRef() but steal a reference to 'obj'
// (Py_DECREF(obj)) on success (if it returns 0).
PyAPI_FUNC(int) PyModule_AddObject(PyObject *mod, const char *, PyObject *value);
PyAPI_FUNC(int) PyModule_AddIntConstant(PyObject *, const char *, long);
PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char *);
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000

View File

@ -526,6 +526,32 @@ they can have object code that is not dependent on Python compilation flags.
PyAPI_FUNC(void) Py_IncRef(PyObject *);
PyAPI_FUNC(void) Py_DecRef(PyObject *);
// Create a new strong reference to an object:
// increment the reference count of the object and return the object.
PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);
// Similar to Py_NewRef(), but the object can be NULL.
PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj);
static inline PyObject* _Py_NewRef(PyObject *obj)
{
Py_INCREF(obj);
return obj;
}
static inline PyObject* _Py_XNewRef(PyObject *obj)
{
Py_XINCREF(obj);
return obj;
}
// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
// Names overriden with macros by static inline functions for best
// performances.
#define Py_NewRef(obj) _Py_NewRef(obj)
#define Py_XNewRef(obj) _Py_XNewRef(obj)
/*
_Py_NoneStruct is an object of undefined type which can be used in contexts
where NULL (nil) is not suitable (since NULL often means 'error').
@ -536,7 +562,7 @@ PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
#define Py_None (&_Py_NoneStruct)
/* Macro for returning Py_None from a function */
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
#define Py_RETURN_NONE return Py_NewRef(Py_None)
/*
Py_NotImplemented is a singleton used to signal that an operation is
@ -546,8 +572,7 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
#define Py_NotImplemented (&_Py_NotImplementedStruct)
/* Macro for returning Py_NotImplemented from a function */
#define Py_RETURN_NOTIMPLEMENTED \
return Py_INCREF(Py_NotImplemented), Py_NotImplemented
#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)
/* Rich comparison opcodes */
#define Py_LT 0

View File

@ -20,10 +20,10 @@
#define PY_MINOR_VERSION 10
#define PY_MICRO_VERSION 0
#define PY_RELEASE_LEVEL PY_RELEASE_LEVEL_ALPHA
#define PY_RELEASE_SERIAL 1
#define PY_RELEASE_SERIAL 2
/* Version as a string */
#define PY_VERSION "3.10.0a1+"
#define PY_VERSION "3.10.0a2+"
/*--end constants--*/
/* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2.

View File

@ -841,12 +841,16 @@ extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
#endif
#if defined(__ANDROID__) || defined(__VXWORKS__)
/* Ignore the locale encoding: force UTF-8 */
// Use UTF-8 as the locale encoding, ignore the LC_CTYPE locale.
// See _Py_GetLocaleEncoding(), PyUnicode_DecodeLocale()
// and PyUnicode_EncodeLocale().
# define _Py_FORCE_UTF8_LOCALE
#endif
#if defined(_Py_FORCE_UTF8_LOCALE) || defined(__APPLE__)
/* Use UTF-8 as filesystem encoding */
// Use UTF-8 as the filesystem encoding.
// See PyUnicode_DecodeFSDefaultAndSize(), PyUnicode_EncodeFSDefault(),
// Py_DecodeLocale() and Py_EncodeLocale().
# define _Py_FORCE_UTF8_FS_ENCODING
#endif

View File

@ -1,36 +0,0 @@
/* Unicode name database interface */
#ifndef Py_LIMITED_API
#ifndef Py_UCNHASH_H
#define Py_UCNHASH_H
#ifdef __cplusplus
extern "C" {
#endif
/* revised ucnhash CAPI interface (exported through a "wrapper") */
#define PyUnicodeData_CAPSULE_NAME "unicodedata.ucnhash_CAPI"
typedef struct {
/* Size of this struct */
int size;
/* Get name for a given character code. Returns non-zero if
success, zero if not. Does not set Python exceptions.
If self is NULL, data come from the default version of the database.
If it is not NULL, it should be a unicodedata.ucd_X_Y_Z object */
int (*getname)(PyObject *self, Py_UCS4 code, char* buffer, int buflen,
int with_alias_and_seq);
/* Get character code for a given name. Same error handling
as for getname. */
int (*getcode)(PyObject *self, const char* name, int namelen, Py_UCS4* code,
int with_named_seq);
} _PyUnicode_Name_CAPI;
#ifdef __cplusplus
}
#endif
#endif /* !Py_UCNHASH_H */
#endif /* !Py_LIMITED_API */

View File

@ -1,46 +0,0 @@
"""A minimal subset of the locale module used at interpreter startup
(imported by the _io module), in order to reduce startup time.
Don't import directly from third-party code; use the `locale` module instead!
"""
import sys
import _locale
if sys.platform.startswith("win"):
def getpreferredencoding(do_setlocale=True):
if sys.flags.utf8_mode:
return 'UTF-8'
return _locale._getdefaultlocale()[1]
else:
try:
_locale.CODESET
except AttributeError:
if hasattr(sys, 'getandroidapilevel'):
# On Android langinfo.h and CODESET are missing, and UTF-8 is
# always used in mbstowcs() and wcstombs().
def getpreferredencoding(do_setlocale=True):
return 'UTF-8'
else:
def getpreferredencoding(do_setlocale=True):
if sys.flags.utf8_mode:
return 'UTF-8'
# This path for legacy systems needs the more complex
# getdefaultlocale() function, import the full locale module.
import locale
return locale.getpreferredencoding(do_setlocale)
else:
def getpreferredencoding(do_setlocale=True):
assert not do_setlocale
if sys.flags.utf8_mode:
return 'UTF-8'
result = _locale.nl_langinfo(_locale.CODESET)
if not result and sys.platform == 'darwin':
# nl_langinfo can return an empty string
# when the setting has an invalid value.
# Default to UTF-8 in that case because
# UTF-8 is the default charset on OSX and
# returning nothing will crash the
# interpreter.
result = 'UTF-8'
return result

View File

@ -110,6 +110,26 @@ def _get_system_version():
return _SYSTEM_VERSION
_SYSTEM_VERSION_TUPLE = None
def _get_system_version_tuple():
"""
Return the macOS system version as a tuple
The return value is safe to use to compare
two version numbers.
"""
global _SYSTEM_VERSION_TUPLE
if _SYSTEM_VERSION_TUPLE is None:
osx_version = _get_system_version()
if osx_version:
try:
_SYSTEM_VERSION_TUPLE = tuple(int(i) for i in osx_version.split('.'))
except ValueError:
_SYSTEM_VERSION_TUPLE = ()
return _SYSTEM_VERSION_TUPLE
def _remove_original_values(_config_vars):
"""Remove original unmodified values for testing"""
# This is needed for higher-level cross-platform tests of get_platform.
@ -132,14 +152,18 @@ def _supports_universal_builds():
# builds, in particular -isysroot and -arch arguments to the compiler. This
# is in support of allowing 10.4 universal builds to run on 10.3.x systems.
osx_version = _get_system_version()
if osx_version:
try:
osx_version = tuple(int(i) for i in osx_version.split('.'))
except ValueError:
osx_version = ''
osx_version = _get_system_version_tuple()
return bool(osx_version >= (10, 4)) if osx_version else False
def _supports_arm64_builds():
"""Returns True if arm64 builds are supported on this system"""
# There are two sets of systems supporting macOS/arm64 builds:
# 1. macOS 11 and later, unconditionally
# 2. macOS 10.15 with Xcode 12.2 or later
# For now the second category is ignored.
osx_version = _get_system_version_tuple()
return osx_version >= (11, 0) if osx_version else False
def _find_appropriate_compiler(_config_vars):
"""Find appropriate C compiler for extension module builds"""
@ -331,6 +355,12 @@ def compiler_fixup(compiler_so, cc_args):
except ValueError:
break
elif not _supports_arm64_builds():
# Look for "-arch arm64" and drop that
for idx in range(len(compiler_so)):
if compiler_so[idx] == '-arch' and compiler_so[idx+1] == "arm64":
del compiler_so[idx:idx+2]
if 'ARCHFLAGS' in os.environ and not stripArch:
# User specified different -arch flags in the environ,
# see also distutils.sysconfig
@ -481,6 +511,8 @@ def get_platform_osx(_config_vars, osname, release, machine):
if len(archs) == 1:
machine = archs[0]
elif archs == ('arm64', 'x86_64'):
machine = 'universal2'
elif archs == ('i386', 'ppc'):
machine = 'fat'
elif archs == ('i386', 'x86_64'):

View File

@ -1,6 +1,7 @@
__all__ = ()
import reprlib
from _thread import get_ident
from . import format_helpers
@ -41,6 +42,16 @@ def _format_callbacks(cb):
return f'cb=[{cb}]'
# bpo-42183: _repr_running is needed for repr protection
# when a Future or Task result contains itself directly or indirectly.
# The logic is borrowed from @reprlib.recursive_repr decorator.
# Unfortunately, the direct decorator usage is impossible because of
# AttributeError: '_asyncio.Task' object has no attribute '__module__' error.
#
# After fixing this thing we can return to the decorator based approach.
_repr_running = set()
def _future_repr_info(future):
# (Future) -> str
"""helper function for Future.__repr__"""
@ -49,9 +60,17 @@ def _future_repr_info(future):
if future._exception is not None:
info.append(f'exception={future._exception!r}')
else:
# use reprlib to limit the length of the output, especially
# for very long strings
result = reprlib.repr(future._result)
key = id(future), get_ident()
if key in _repr_running:
result = '...'
else:
_repr_running.add(key)
try:
# use reprlib to limit the length of the output, especially
# for very long strings
result = reprlib.repr(future._result)
finally:
_repr_running.discard(key)
info.append(f'result={result}')
if future._callbacks:
info.append(_format_callbacks(future._callbacks))

View File

@ -373,7 +373,7 @@ ALL_COMPLETED = concurrent.futures.ALL_COMPLETED
async def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
"""Wait for the Futures and coroutines given by fs to complete.
The sequence futures must not be empty.
The fs iterable must not be empty.
Coroutines will be wrapped in Tasks.
@ -573,7 +573,7 @@ def as_completed(fs, *, loop=None, timeout=None):
Note: The futures 'f' are not necessarily members of fs.
"""
if futures.isfuture(fs) or coroutines.iscoroutine(fs):
raise TypeError(f"expect a list of futures, not {type(fs).__name__}")
raise TypeError(f"expect an iterable of futures, not {type(fs).__name__}")
from .queues import Queue # Import here to avoid circular import problem.
done = Queue(loop=loop)

View File

@ -117,12 +117,12 @@ class _Hqxcoderengine:
first = 0
while first <= len(self.hqxdata) - self.linelen:
last = first + self.linelen
self.ofp.write(self.hqxdata[first:last] + b'\n')
self.ofp.write(self.hqxdata[first:last] + b'\r')
self.linelen = LINELEN
first = last
self.hqxdata = self.hqxdata[first:]
if force:
self.ofp.write(self.hqxdata + b':\n')
self.ofp.write(self.hqxdata + b':\r')
def close(self):
if self.data:

View File

@ -80,6 +80,22 @@ class ContextDecorator(object):
return inner
class AsyncContextDecorator(object):
"A base class or mixin that enables async context managers to work as decorators."
def _recreate_cm(self):
"""Return a recreated instance of self.
"""
return self
def __call__(self, func):
@wraps(func)
async def inner(*args, **kwds):
async with self._recreate_cm():
return await func(*args, **kwds)
return inner
class _GeneratorContextManagerBase:
"""Shared functionality for @contextmanager and @asynccontextmanager."""
@ -167,9 +183,16 @@ class _GeneratorContextManager(_GeneratorContextManagerBase,
class _AsyncGeneratorContextManager(_GeneratorContextManagerBase,
AbstractAsyncContextManager):
AbstractAsyncContextManager,
AsyncContextDecorator):
"""Helper for @asynccontextmanager."""
def _recreate_cm(self):
# _AGCM instances are one-shot context managers, so the
# ACM must be recreated each time a decorated function is
# called
return self.__class__(self.func, self.args, self.kwds)
async def __aenter__(self):
try:
return await self.gen.__anext__()
@ -303,6 +326,32 @@ class closing(AbstractContextManager):
self.thing.close()
class aclosing(AbstractAsyncContextManager):
"""Async context manager for safely finalizing an asynchronously cleaned-up
resource such as an async generator, calling its ``aclose()`` method.
Code like this:
async with aclosing(<module>.fetch(<arguments>)) as agen:
<block>
is equivalent to this:
agen = <module>.fetch(<arguments>)
try:
<block>
finally:
await agen.aclose()
"""
def __init__(self, thing):
self.thing = thing
async def __aenter__(self):
return self.thing
async def __aexit__(self, *exc_info):
await self.thing.aclose()
class _RedirectStream(AbstractContextManager):
_stream = None
@ -655,7 +704,7 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
return received_exc and suppressed_exc
class nullcontext(AbstractContextManager):
class nullcontext(AbstractContextManager, AbstractAsyncContextManager):
"""Context manager that does no additional processing.
Used as a stand-in for a normal context manager, when a particular
@ -674,3 +723,9 @@ class nullcontext(AbstractContextManager):
def __exit__(self, *excinfo):
pass
async def __aenter__(self):
return self.enter_result
async def __aexit__(self, *excinfo):
pass

View File

@ -48,6 +48,7 @@ def _reconstructor(cls, base, state):
return obj
_HEAPTYPE = 1<<9
_new_type = type(int.__new__)
# Python code for object.__reduce_ex__ for protocols 0 and 1
@ -57,6 +58,9 @@ def _reduce_ex(self, proto):
for base in cls.__mro__:
if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
break
new = base.__new__
if isinstance(new, _new_type) and new.__self__ is base:
break
else:
base = object # not really reachable
if base is object:

View File

@ -6,6 +6,11 @@ import os
from ctypes.macholib.framework import framework_info
from ctypes.macholib.dylib import dylib_info
from itertools import *
try:
from _ctypes import _dyld_shared_cache_contains_path
except ImportError:
def _dyld_shared_cache_contains_path(*args):
raise NotImplementedError
__all__ = [
'dyld_find', 'framework_find',
@ -122,8 +127,15 @@ def dyld_find(name, executable_path=None, env=None):
dyld_executable_path_search(name, executable_path),
dyld_default_search(name, env),
), env):
if os.path.isfile(path):
return path
try:
if _dyld_shared_cache_contains_path(path):
return path
except NotImplementedError:
pass
raise ValueError("dylib %s could not be found" % (name,))
def framework_find(fn, executable_path=None, env=None):

View File

@ -45,19 +45,22 @@ def find_lib(name):
class MachOTest(unittest.TestCase):
@unittest.skipUnless(sys.platform == "darwin", 'OSX-specific test')
def test_find(self):
self.assertEqual(find_lib('pthread'),
'/usr/lib/libSystem.B.dylib')
# On Mac OS 11, system dylibs are only present in the shared cache,
# so symlinks like libpthread.dylib -> libSystem.B.dylib will not
# be resolved by dyld_find
self.assertIn(find_lib('pthread'),
('/usr/lib/libSystem.B.dylib', '/usr/lib/libpthread.dylib'))
result = find_lib('z')
# Issue #21093: dyld default search path includes $HOME/lib and
# /usr/local/lib before /usr/lib, which caused test failures if
# a local copy of libz exists in one of them. Now ignore the head
# of the path.
self.assertRegex(result, r".*/lib/libz\..*.*\.dylib")
self.assertRegex(result, r".*/lib/libz.*\.dylib")
self.assertEqual(find_lib('IOKit'),
'/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit')
self.assertIn(find_lib('IOKit'),
('/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit',
'/System/Library/Frameworks/IOKit.framework/IOKit'))
if __name__ == "__main__":
unittest.main()

View File

@ -493,7 +493,7 @@ class BuildExtTestCase(TempdirManager,
# format the target value as defined in the Apple
# Availability Macros. We can't use the macro names since
# at least one value we test with will not exist yet.
if target[1] < 10:
if target[:2] < (10, 10):
# for 10.1 through 10.9.x -> "10n0"
target = '%02d%01d0' % target
else:

Some files were not shown because too many files have changed in this diff Show More