diff --git a/.azure-pipelines/ci.yml b/.azure-pipelines/ci.yml index 0fe754bb071ea3..d0efa77f93052b 100644 --- a/.azure-pipelines/ci.yml +++ b/.azure-pipelines/ci.yml @@ -1,14 +1,14 @@ variables: coverage: false -trigger: ['master', '3.9', '3.8', '3.7'] +trigger: ['main', '3.10', '3.9', '3.8', '3.7'] jobs: - job: Prebuild displayName: Pre-build checks pool: - vmImage: ubuntu-18.04 + vmImage: ubuntu-20.04 steps: - template: ./prebuild-checks.yml @@ -20,7 +20,7 @@ jobs: condition: and(succeeded(), eq(dependencies.Prebuild.outputs['docs.run'], 'true')) pool: - vmImage: ubuntu-18.04 + vmImage: ubuntu-20.04 steps: - template: ./docs-steps.yml @@ -40,7 +40,7 @@ jobs: testRunPlatform: macos pool: - vmImage: macos-10.14 + vmImage: macos-10.15 steps: - template: ./macos-steps.yml @@ -52,12 +52,12 @@ jobs: condition: and(succeeded(), eq(dependencies.Prebuild.outputs['tests.run'], 'true')) pool: - vmImage: ubuntu-18.04 + vmImage: ubuntu-20.04 variables: testRunTitle: '$(build.sourceBranchName)-linux' testRunPlatform: linux - openssl_version: 1.1.1k + openssl_version: 1.1.1n steps: - template: ./posix-steps.yml @@ -78,12 +78,12 @@ jobs: ) pool: - vmImage: ubuntu-18.04 + vmImage: ubuntu-20.04 variables: testRunTitle: '$(Build.SourceBranchName)-linux-coverage' testRunPlatform: linux-coverage - openssl_version: 1.1.1k + openssl_version: 1.1.1n steps: - template: ./posix-steps.yml diff --git a/.azure-pipelines/pr.yml b/.azure-pipelines/pr.yml index 2d32e6d49bcc0e..a4f32460c7ea02 100644 --- a/.azure-pipelines/pr.yml +++ b/.azure-pipelines/pr.yml @@ -1,14 +1,14 @@ variables: coverage: false -pr: ['master', '3.9', '3.8', '3.7'] +pr: ['main', '3.10', '3.9', '3.8', '3.7'] jobs: - job: Prebuild displayName: Pre-build checks pool: - vmImage: ubuntu-18.04 + vmImage: ubuntu-20.04 steps: - template: ./prebuild-checks.yml @@ -20,7 +20,7 @@ jobs: condition: and(succeeded(), eq(dependencies.Prebuild.outputs['docs.run'], 'true')) pool: - vmImage: ubuntu-18.04 + vmImage: ubuntu-20.04 steps: - template: ./docs-steps.yml @@ -38,7 +38,7 @@ jobs: testRunPlatform: macos pool: - vmImage: macos-10.14 + vmImage: macos-10.15 steps: - template: ./macos-steps.yml @@ -52,12 +52,12 @@ jobs: condition: and(succeeded(), eq(dependencies.Prebuild.outputs['tests.run'], 'true')) pool: - vmImage: ubuntu-18.04 + vmImage: ubuntu-20.04 variables: testRunTitle: '$(system.pullRequest.TargetBranch)-linux' testRunPlatform: linux - openssl_version: 1.1.1k + openssl_version: 1.1.1n steps: - template: ./posix-steps.yml @@ -78,12 +78,12 @@ jobs: ) pool: - vmImage: ubuntu-18.04 + vmImage: ubuntu-20.04 variables: testRunTitle: '$(Build.SourceBranchName)-linux-coverage' testRunPlatform: linux-coverage - openssl_version: 1.1.1k + openssl_version: 1.1.1n steps: - template: ./posix-steps.yml diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000000000..81445d2d79c739 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*.{py,c,cpp,h,rst,md,yml}] +trim_trailing_whitespace = true +insert_final_newline = true +indent_style = space + +[*.{py,c,cpp,h}] +indent_size = 4 + +[*.yml] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes index c66e765266382f..be369d2a5c63c4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -40,11 +40,8 @@ PCbuild/readme.txt text eol=crlf PC/readme.txt text eol=crlf # Generated files -# https://github.com/github/linguist#generated-code -Modules/clinic/*.h linguist-generated=true -Objects/clinic/*.h linguist-generated=true -PC/clinic/*.h linguist-generated=true -Python/clinic/*.h linguist-generated=true +# https://github.com/github/linguist/blob/master/docs/overrides.md +**/clinic/*.h linguist-generated=true Python/importlib.h linguist-generated=true Python/importlib_external.h linguist-generated=true Include/internal/pycore_ast.h linguist-generated=true diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 5b86d39dc9c259..173b957b108c60 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -96,7 +96,7 @@ Lib/ast.py @isidentical /Lib/unittest/test/testmock/* @cjw296 # SQLite 3 -**/*sqlite* @berkerpeksag +**/*sqlite* @berkerpeksag @erlend-aasland # subprocess /Lib/subprocess.py @gpshead @@ -128,7 +128,7 @@ Lib/ast.py @isidentical **/*idlelib* @terryjreedy -**/*typing* @gvanrossum @ilevkivskyi +**/*typing* @gvanrossum @Fidget-Spinner @JelleZijlstra **/*asyncore @giampaolo **/*asynchat @giampaolo diff --git a/.github/SECURITY.md b/.github/SECURITY.md index 28aea946623cc5..82ae4ca8c30977 100644 --- a/.github/SECURITY.md +++ b/.github/SECURITY.md @@ -10,9 +10,8 @@ https://devguide.python.org/#status-of-python-branches ## Reporting a Vulnerability Please read the guidelines on reporting security issues [on the -official website]( -https://www.python.org/news/security/#reporting-security-issues-in-python -) for instructions on how to report a security-related problem to +official website](https://www.python.org/dev/security/) for +instructions on how to report a security-related problem to the Python team responsibly. To reach the response team, email `security at python dot org`. diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 41200bb957e47c..61cbef982a9ef1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,12 +7,14 @@ on: push: branches: - master + - 3.10 - 3.9 - 3.8 - 3.7 pull_request: branches: - master + - 3.10 - 3.9 - 3.8 - 3.7 @@ -25,7 +27,7 @@ jobs: run_tests: ${{ steps.check.outputs.run_tests }} run_ssl_tests: ${{ steps.check.outputs.run_ssl_tests }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Check for source changes id: check run: | @@ -51,16 +53,50 @@ jobs: git diff --name-only origin/$GITHUB_BASE_REF.. | grep -qE '(ssl|hashlib|hmac|^.github)' && echo '::set-output name=run_ssl_tests::true' || true fi + check_abi: + name: 'Check if the ABI has changed' + runs-on: ubuntu-20.04 + needs: check_source + if: needs.check_source.outputs.run_tests == 'true' + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - name: Install Dependencies + run: | + sudo ./.github/workflows/posix-deps-apt.sh + sudo apt-get install -yq abigail-tools + - name: Build CPython + env: + CFLAGS: -g3 -O0 + run: | + # Build Python with the libpython dynamic library + ./configure --enable-shared + make -j4 + - name: Check for changes in the ABI + run: make check-abidump + check_generated_files: name: 'Check if generated files are up to date' runs-on: ubuntu-latest needs: check_source if: needs.check_source.outputs.run_tests == 'true' steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 - name: Install Dependencies run: sudo ./.github/workflows/posix-deps-apt.sh + - name: Add ccache to PATH + run: echo "PATH=/usr/lib/ccache:$PATH" >> $GITHUB_ENV + - name: Configure ccache action + uses: hendrikmuhs/ccache-action@v1 + - name: Check Autoconf version 2.69 and aclocal 1.16.3 + run: | + grep "Generated by GNU Autoconf 2.69" configure + grep "aclocal 1.16.3" aclocal.m4 + grep -q "runstatedir" configure + grep -q "PKG_PROG_PKG_CONFIG" aclocal.m4 + - name: Regenerate autoconf files + run: docker run --rm -v $(pwd):/src quay.io/tiran/cpython_autoconf:269 - name: Build CPython run: | # Build Python with the libpython dynamic library @@ -71,9 +107,10 @@ jobs: run: | changes=$(git status --porcelain) # Check for changes in regenerated files - if ! test -z "$changes" - then - echo "Generated files not up to date. Perhaps you forgot to run make regen-all ;)" + if test -n "$changes"; then + echo "Generated files not up to date." + echo "Perhaps you forgot to run make regen-all or build.bat --regen. ;)" + echo "configure files must be regenerated with a specific, unpatched version of autoconf." echo "$changes" exit 1 fi @@ -87,8 +124,10 @@ jobs: runs-on: windows-latest needs: check_source if: needs.check_source.outputs.run_tests == 'true' + env: + IncludeUwp: 'true' steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Build CPython run: .\PCbuild\build.bat -e -p Win32 - name: Display build info @@ -101,8 +140,10 @@ jobs: runs-on: windows-latest needs: check_source if: needs.check_source.outputs.run_tests == 'true' + env: + IncludeUwp: 'true' steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Register MSVC problem matcher run: echo "::add-matcher::.github/problem-matchers/msvc.json" - name: Build CPython @@ -117,10 +158,16 @@ jobs: runs-on: macos-latest needs: check_source if: needs.check_source.outputs.run_tests == 'true' + env: + PYTHONSTRICTEXTENSIONBUILD: 1 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 + - name: Prepare homebrew environment variables + run: | + echo "LDFLAGS=-L$(brew --prefix tcl-tk)/lib" >> $GITHUB_ENV + echo "PKG_CONFIG_PATH=$(brew --prefix openssl@1.1)/lib/pkgconfig:$(brew --prefix tcl-tk)/lib/pkgconfig" >> $GITHUB_ENV - name: Configure CPython - run: ./configure --with-pydebug --with-openssl=/usr/local/opt/openssl --prefix=/opt/python-dev + run: ./configure --with-pydebug --prefix=/opt/python-dev - name: Build CPython run: make -j4 - name: Display build info @@ -134,9 +181,10 @@ jobs: needs: check_source if: needs.check_source.outputs.run_tests == 'true' env: - OPENSSL_VER: 1.1.1k + OPENSSL_VER: 1.1.1n + PYTHONSTRICTEXTENSIONBUILD: 1 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Register gcc problem matcher run: echo "::add-matcher::.github/problem-matchers/gcc.json" - name: Install Dependencies @@ -148,7 +196,7 @@ jobs: echo "LD_LIBRARY_PATH=${GITHUB_WORKSPACE}/multissl/openssl/${OPENSSL_VER}/lib" >> $GITHUB_ENV - name: 'Restore OpenSSL build' id: cache-openssl - uses: actions/cache@v2.1.4 + uses: actions/cache@v3.0.2 with: path: ./multissl/openssl/${{ env.OPENSSL_VER }} key: ${{ runner.os }}-multissl-openssl-${{ env.OPENSSL_VER }} @@ -177,14 +225,14 @@ jobs: strategy: fail-fast: false matrix: - openssl_ver: [1.1.1k, 3.0.0-alpha15] + openssl_ver: [1.1.1n, 3.0.2] env: OPENSSL_VER: ${{ matrix.openssl_ver }} MULTISSL_DIR: ${{ github.workspace }}/multissl OPENSSL_DIR: ${{ github.workspace }}/multissl/openssl/${{ matrix.openssl_ver }} LD_LIBRARY_PATH: ${{ github.workspace }}/multissl/openssl/${{ matrix.openssl_ver }}/lib steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Register gcc problem matcher run: echo "::add-matcher::.github/problem-matchers/gcc.json" - name: Install Dependencies @@ -196,7 +244,7 @@ jobs: echo "LD_LIBRARY_PATH=${GITHUB_WORKSPACE}/multissl/openssl/${OPENSSL_VER}/lib" >> $GITHUB_ENV - name: 'Restore OpenSSL build' id: cache-openssl - uses: actions/cache@v2.1.4 + uses: actions/cache@v3.0.2 with: path: ./multissl/openssl/${{ env.OPENSSL_VER }} key: ${{ runner.os }}-multissl-openssl-${{ env.OPENSSL_VER }} diff --git a/.github/workflows/build_msi.yml b/.github/workflows/build_msi.yml index 182eb7ce571671..201098a635799a 100644 --- a/.github/workflows/build_msi.yml +++ b/.github/workflows/build_msi.yml @@ -4,6 +4,7 @@ on: push: branches: - master + - 3.10 - 3.9 - 3.8 - 3.7 @@ -12,6 +13,7 @@ on: pull_request: branches: - master + - 3.10 - 3.9 - 3.8 - 3.7 @@ -23,7 +25,7 @@ jobs: name: 'Windows (x86) Installer' runs-on: windows-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Build CPython installer run: .\Tools\msi\build.bat -x86 @@ -31,6 +33,6 @@ jobs: name: 'Windows (x64) Installer' runs-on: windows-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Build CPython installer run: .\Tools\msi\build.bat -x64 diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index 8924fc992e25e6..36b9f9f1d3497f 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -12,6 +12,7 @@ on: pull_request: branches: - master + - 3.10 - 3.9 - 3.8 - 3.7 @@ -24,7 +25,7 @@ jobs: name: 'Docs' runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Register Sphinx problem matcher run: echo "::add-matcher::.github/problem-matchers/sphinx.json" - name: 'Install Dependencies' @@ -36,9 +37,9 @@ jobs: - name: 'Install build dependencies' run: make -C Doc/ PYTHON=../python venv - name: 'Build documentation' - run: xvfb-run make -C Doc/ PYTHON=../python SPHINXOPTS="-q -W --keep-going -j4" doctest html suspicious + run: xvfb-run make -C Doc/ PYTHON=../python SPHINXOPTS="-q -W --keep-going" doctest html suspicious - name: 'Upload' - uses: actions/upload-artifact@v2.2.2 + uses: actions/upload-artifact@v3 with: name: doc-html path: Doc/build/html diff --git a/.github/workflows/posix-deps-apt.sh b/.github/workflows/posix-deps-apt.sh index 56cc70edf60001..0119843e47eeb1 100755 --- a/.github/workflows/posix-deps-apt.sh +++ b/.github/workflows/posix-deps-apt.sh @@ -9,6 +9,7 @@ apt-get -yq install \ libbz2-dev \ libffi-dev \ libgdbm-dev \ + libgdbm-compat-dev \ liblzma-dev \ libncurses5-dev \ libreadline6-dev \ diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index 26806fad814f1a..4a08ef0d8dac5f 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -13,7 +13,8 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/stale@v3 + - name: "Check PRs" + uses: actions/stale@v5 with: repo-token: ${{ secrets.GITHUB_TOKEN }} stale-pr-message: 'This PR is stale because it has been open for 30 days with no activity.' diff --git a/.gitignore b/.gitignore index 80dcf34bf47a6f..09d08c8050cb2f 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ gmon.out .coverage .mypy_cache/ .pytest_cache/ +.DS_Store *.exe !Lib/distutils/command/*.exe @@ -60,6 +61,15 @@ Lib/test/data/* !Lib/test/data/README /Makefile /Makefile.pre +Mac/Makefile +Mac/PythonLauncher/Info.plist +Mac/PythonLauncher/Makefile +Mac/PythonLauncher/Python Launcher +Mac/PythonLauncher/Python Launcher.app/* +Mac/Resources/app/Info.plist +Mac/Resources/framework/Info.plist +Mac/pythonw +/*.framework/ Misc/python.pc Misc/python-embed.pc Misc/python-config.sh @@ -102,6 +112,8 @@ Tools/unicode/data/ /config.log /config.status /config.status.lineno +# hendrikmuhs/ccache-action@v1 +/.ccache /platform /profile-clean-stamp /profile-run-stamp @@ -124,3 +136,9 @@ Tools/ssl/win32 # Ignore ./python binary on Unix but still look into ./Python/ directory. /python !/Python/ + +# Artifacts generated by 3.11 lying around when switching branches: +/_bootstrap_python +/Programs/_freeze_module +/Python/deepfreeze/ +/Python/frozen_modules/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 1112a0b266227b..ee6cabe5ae0da3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,7 @@ env: # Set rpath with env var instead of -Wl,-rpath linker flag # OpenSSL ignores LDFLAGS when linking bin/openssl - LD_RUN_PATH="${OPENSSL_DIR}/lib" + - PYTHONSTRICTEXTENSIONBUILD=1 branches: only: diff --git a/Doc/Makefile b/Doc/Makefile index f113dd06539869..939498e548a069 100644 --- a/Doc/Makefile +++ b/Doc/Makefile @@ -17,7 +17,7 @@ SPHINXERRORHANDLING = -W PAPEROPT_a4 = -D latex_elements.papersize=a4paper PAPEROPT_letter = -D latex_elements.papersize=letterpaper -ALLSPHINXOPTS = -b $(BUILDER) -d build/doctrees $(PAPEROPT_$(PAPER)) \ +ALLSPHINXOPTS = -b $(BUILDER) -d build/doctrees $(PAPEROPT_$(PAPER)) -j auto \ $(SPHINXOPTS) $(SPHINXERRORHANDLING) . build/$(BUILDER) $(SOURCES) .PHONY: help build html htmlhelp latex text texinfo changes linkcheck \ @@ -137,14 +137,22 @@ pydoc-topics: build htmlview: html $(PYTHON) -c "import webbrowser; webbrowser.open('build/html/index.html')" -clean: - -rm -rf build/* $(VENVDIR)/* +clean: clean-venv + -rm -rf build/* + +clean-venv: + rm -rf $(VENVDIR) venv: - $(PYTHON) -m venv $(VENVDIR) - $(VENVDIR)/bin/python3 -m pip install -U pip setuptools - $(VENVDIR)/bin/python3 -m pip install -r requirements.txt - @echo "The venv has been created in the $(VENVDIR) directory" + @if [ -d $(VENVDIR) ] ; then \ + echo "venv already exists."; \ + echo "To recreate it, remove it first with \`make clean-venv'."; \ + else \ + $(PYTHON) -m venv $(VENVDIR); \ + $(VENVDIR)/bin/python3 -m pip install -U pip setuptools; \ + $(VENVDIR)/bin/python3 -m pip install -r requirements.txt; \ + echo "The venv has been created in the $(VENVDIR) directory"; \ + fi dist: rm -rf dist diff --git a/Doc/README.rst b/Doc/README.rst index 380ea4fa9b26ad..7e8a27b4066d87 100644 --- a/Doc/README.rst +++ b/Doc/README.rst @@ -28,28 +28,31 @@ install the tools into there. Using make ---------- -To get started on UNIX, you can create a virtual environment with the command :: +To get started on UNIX, you can create a virtual environment and build +documentation with the commands:: make venv - -That will install all the tools necessary to build the documentation. Assuming -the virtual environment was created in the ``venv`` directory (the default; -configurable with the VENVDIR variable), you can run the following command to -build the HTML output files:: - make html -By default, if the virtual environment is not created, the Makefile will -look for instances of sphinxbuild and blurb installed on your process PATH -(configurable with the SPHINXBUILD and BLURB variables). +The virtual environment in the ``venv`` directory will contain all the tools +necessary to build the documentation downloaded and installed from PyPI. +If you'd like to create the virtual environment in a different location, +you can specify it using the ``VENVDIR`` variable. + +You can also skip creating the virtual environment altogether, in which case +the Makefile will look for instances of ``sphinxbuild`` and ``blurb`` +installed on your process ``PATH`` (configurable with the ``SPHINXBUILD`` and +``BLURB`` variables). On Windows, we try to emulate the Makefile as closely as possible with a ``make.bat`` file. If you need to specify the Python interpreter to use, -set the PYTHON environment variable instead. +set the PYTHON environment variable. Available make targets are: -* "clean", which removes all build files. +* "clean", which removes all build files and the virtual environment. + +* "clean-venv", which removes the virtual environment directory. * "venv", which creates a virtual environment with all necessary tools installed. diff --git a/Doc/about.rst b/Doc/about.rst index 3ea311fa629dd2..f0b908487b2d09 100644 --- a/Doc/about.rst +++ b/Doc/about.rst @@ -23,9 +23,8 @@ Many thanks go to: and writer of much of the content; * the `Docutils `_ project for creating reStructuredText and the Docutils suite; -* Fredrik Lundh for his `Alternative Python Reference - `_ project from which Sphinx got many good - ideas. +* Fredrik Lundh for his Alternative Python Reference project from which Sphinx + got many good ideas. Contributors to the Python Documentation diff --git a/Doc/bugs.rst b/Doc/bugs.rst index a17f04d26fa40b..0feddeb5793fcd 100644 --- a/Doc/bugs.rst +++ b/Doc/bugs.rst @@ -35,13 +35,17 @@ though it may take a while to be processed. `Helping with Documentation `_ Comprehensive guide for individuals that are interested in contributing to Python documentation. + `Documentation Translations `_ + A list of GitHub pages for documentation translation and their primary contacts. + + .. _using-the-tracker: Using the Python issue tracker ============================== Bug reports for Python itself should be submitted via the Python Bug Tracker -(https://bugs.python.org/). The bug tracker offers a Web form which allows +(https://bugs.python.org/). The bug tracker offers a web form which allows pertinent information to be entered and submitted to the developers. The first step in filing a report is to determine whether the problem has @@ -80,7 +84,7 @@ taken on the bug. Article which goes into some detail about how to create a useful bug report. This describes what kind of information is useful and why it is useful. - `Bug Report Writing Guidelines `_ + `Bug Writing Guidelines `_ Information about writing a good bug report. Some of this is specific to the Mozilla project, but describes general good practices. diff --git a/Doc/c-api/abstract.rst b/Doc/c-api/abstract.rst index f5df09fa7fd786..1823f9d70c79f3 100644 --- a/Doc/c-api/abstract.rst +++ b/Doc/c-api/abstract.rst @@ -24,3 +24,4 @@ but whose items have not been set to some non-\ ``NULL`` value yet. mapping.rst iter.rst buffer.rst + objbuffer.rst diff --git a/Doc/c-api/apiabiversion.rst b/Doc/c-api/apiabiversion.rst index b8a8f2ff886219..04050f7dabe172 100644 --- a/Doc/c-api/apiabiversion.rst +++ b/Doc/c-api/apiabiversion.rst @@ -6,34 +6,57 @@ API and ABI Versioning *********************** -``PY_VERSION_HEX`` is the Python version number encoded in a single integer. - -For example if the ``PY_VERSION_HEX`` is set to ``0x030401a2``, the underlying -version information can be found by treating it as a 32 bit number in -the following manner: - - +-------+-------------------------+------------------------------------------------+ - | Bytes | Bits (big endian order) | Meaning | - +=======+=========================+================================================+ - | ``1`` | ``1-8`` | ``PY_MAJOR_VERSION`` (the ``3`` in | - | | | ``3.4.1a2``) | - +-------+-------------------------+------------------------------------------------+ - | ``2`` | ``9-16`` | ``PY_MINOR_VERSION`` (the ``4`` in | - | | | ``3.4.1a2``) | - +-------+-------------------------+------------------------------------------------+ - | ``3`` | ``17-24`` | ``PY_MICRO_VERSION`` (the ``1`` in | - | | | ``3.4.1a2``) | - +-------+-------------------------+------------------------------------------------+ - | ``4`` | ``25-28`` | ``PY_RELEASE_LEVEL`` (``0xA`` for alpha, | - | | | ``0xB`` for beta, ``0xC`` for release | - | | | candidate and ``0xF`` for final), in this | - | | | case it is alpha. | - +-------+-------------------------+------------------------------------------------+ - | | ``29-32`` | ``PY_RELEASE_SERIAL`` (the ``2`` in | - | | | ``3.4.1a2``, zero for final releases) | - +-------+-------------------------+------------------------------------------------+ - -Thus ``3.4.1a2`` is hexversion ``0x030401a2``. +CPython exposes its version number in the following macros. +Note that these correspond to the version code is **built** with, +not necessarily the version used at **run time**. -All the given macros are defined in :source:`Include/patchlevel.h`. +See :ref:`stable` for a discussion of API and ABI stability across versions. + +.. c:macro:: PY_MAJOR_VERSION + + The ``3`` in ``3.4.1a2``. + +.. c:macro:: PY_MINOR_VERSION + + The ``4`` in ``3.4.1a2``. + +.. c:macro:: PY_MICRO_VERSION + + The ``1`` in ``3.4.1a2``. + +.. c:macro:: PY_RELEASE_LEVEL + + The ``a`` in ``3.4.1a2``. + This can be ``0xA`` for alpha, ``0xB`` for beta, ``0xC`` for release + candidate or ``0xF`` for final. +.. c:macro:: PY_RELEASE_SERIAL + + The ``2`` in ``3.4.1a2``. Zero for final releases. + +.. c:macro:: PY_VERSION_HEX + + The Python version number encoded in a single integer. + + The underlying version information can be found by treating it as a 32 bit + number in the following manner: + + +-------+-------------------------+-------------------------+--------------------------+ + | Bytes | Bits (big endian order) | Meaning | Value for ``3.4.1a2`` | + +=======+=========================+=========================+==========================+ + | 1 | 1-8 | ``PY_MAJOR_VERSION`` | ``0x03`` | + +-------+-------------------------+-------------------------+--------------------------+ + | 2 | 9-16 | ``PY_MINOR_VERSION`` | ``0x04`` | + +-------+-------------------------+-------------------------+--------------------------+ + | 3 | 17-24 | ``PY_MICRO_VERSION`` | ``0x01`` | + +-------+-------------------------+-------------------------+--------------------------+ + | 4 | 25-28 | ``PY_RELEASE_LEVEL`` | ``0xA`` | + + +-------------------------+-------------------------+--------------------------+ + | | 29-32 | ``PY_RELEASE_SERIAL`` | ``0x2`` | + +-------+-------------------------+-------------------------+--------------------------+ + + Thus ``3.4.1a2`` is hexversion ``0x030401a2`` and ``3.10.0`` is + hexversion ``0x030a00f0``. + + +All the given macros are defined in :source:`Include/patchlevel.h`. diff --git a/Doc/c-api/arg.rst b/Doc/c-api/arg.rst index 1d93b35dc1c884..926e5249347f84 100644 --- a/Doc/c-api/arg.rst +++ b/Doc/c-api/arg.rst @@ -286,7 +286,7 @@ Numbers Convert a Python integer to a C :c:type:`unsigned long long` without overflow checking. -``n`` (:class:`int`) [Py_ssize_t] +``n`` (:class:`int`) [:c:type:`Py_ssize_t`] Convert a Python integer to a C :c:type:`Py_ssize_t`. ``c`` (:class:`bytes` or :class:`bytearray` of length 1) [char] @@ -613,7 +613,7 @@ Building values ``K`` (:class:`int`) [unsigned long long] Convert a C :c:type:`unsigned long long` to a Python integer object. - ``n`` (:class:`int`) [Py_ssize_t] + ``n`` (:class:`int`) [:c:type:`Py_ssize_t`] Convert a C :c:type:`Py_ssize_t` to a Python integer. ``c`` (:class:`bytes` of length 1) [char] diff --git a/Doc/c-api/bytearray.rst b/Doc/c-api/bytearray.rst index 30bcfc7cf9f500..85a7d1373c2a81 100644 --- a/Doc/c-api/bytearray.rst +++ b/Doc/c-api/bytearray.rst @@ -42,8 +42,6 @@ Direct API functions Return a new bytearray object from any object, *o*, that implements the :ref:`buffer protocol `. - .. XXX expand about the buffer protocol, at least somewhere - .. c:function:: PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index de65701037a7c1..ffbfb5a61b44bb 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -5,7 +5,7 @@ Bytes Objects ------------- -These functions raise :exc:`TypeError` when expecting a bytes parameter and are +These functions raise :exc:`TypeError` when expecting a bytes parameter and called with a non-bytes parameter. .. index:: object: bytes @@ -84,8 +84,8 @@ called with a non-bytes parameter. | :attr:`%lu` | unsigned long | Equivalent to | | | | ``printf("%lu")``. [1]_ | +-------------------+---------------+--------------------------------+ - | :attr:`%zd` | Py_ssize_t | Equivalent to | - | | | ``printf("%zd")``. [1]_ | + | :attr:`%zd` | :c:type:`\ | Equivalent to | + | | Py_ssize_t` | ``printf("%zd")``. [1]_ | +-------------------+---------------+--------------------------------+ | :attr:`%zu` | size_t | Equivalent to | | | | ``printf("%zu")``. [1]_ | diff --git a/Doc/c-api/call.rst b/Doc/c-api/call.rst index 31dc9c8031fdb6..13ef8b217a1622 100644 --- a/Doc/c-api/call.rst +++ b/Doc/c-api/call.rst @@ -26,7 +26,7 @@ This convention is not only used by *tp_call*: :c:member:`~PyTypeObject.tp_new` and :c:member:`~PyTypeObject.tp_init` also pass arguments this way. -To call an object, use :c:func:`PyObject_Call` or other +To call an object, use :c:func:`PyObject_Call` or another :ref:`call API `. @@ -144,8 +144,6 @@ Vectorcall Support API However, the function ``PyVectorcall_NARGS`` should be used to allow for future extensions. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.8 .. c:function:: vectorcallfunc PyVectorcall_Function(PyObject *op) @@ -158,8 +156,6 @@ Vectorcall Support API This is mostly useful to check whether or not *op* supports vectorcall, which can be done by checking ``PyVectorcall_Function(op) != NULL``. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.8 .. c:function:: PyObject* PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict) @@ -172,8 +168,6 @@ Vectorcall Support API It does not check the :const:`Py_TPFLAGS_HAVE_VECTORCALL` flag and it does not fall back to ``tp_call``. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.8 @@ -185,7 +179,7 @@ Object Calling API Various functions are available for calling a Python object. Each converts its arguments to a convention supported by the called object – either *tp_call* or vectorcall. -In order to do as litle conversion as possible, pick one that best fits +In order to do as little conversion as possible, pick one that best fits the format of data you have available. The following table summarizes the available functions; @@ -256,8 +250,6 @@ please see individual documentation for details. Return the result of the call on success, or raise an exception and return *NULL* on failure. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.9 @@ -343,8 +335,6 @@ please see individual documentation for details. Return the result of the call on success, or raise an exception and return *NULL* on failure. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.9 @@ -357,8 +347,6 @@ please see individual documentation for details. Return the result of the call on success, or raise an exception and return *NULL* on failure. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.9 @@ -372,8 +360,6 @@ please see individual documentation for details. Return the result of the call on success, or raise an exception and return *NULL* on failure. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.9 .. c:function:: PyObject* PyObject_VectorcallDict(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwdict) @@ -388,8 +374,6 @@ please see individual documentation for details. already has a dictionary ready to use for the keyword arguments, but not a tuple for the positional arguments. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.9 .. c:function:: PyObject* PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, size_t nargsf, PyObject *kwnames) @@ -410,8 +394,6 @@ please see individual documentation for details. Return the result of the call on success, or raise an exception and return *NULL* on failure. - This function is not part of the :ref:`limited API `. - .. versionadded:: 3.9 diff --git a/Doc/c-api/complex.rst b/Doc/c-api/complex.rst index e2ea766b3a32a7..c25894681bca35 100644 --- a/Doc/c-api/complex.rst +++ b/Doc/c-api/complex.rst @@ -46,9 +46,9 @@ pointers. This is consistent throughout the API. :c:type:`Py_complex` representation. -.. c:function:: Py_complex _Py_c_neg(Py_complex complex) +.. c:function:: Py_complex _Py_c_neg(Py_complex num) - Return the negation of the complex number *complex*, using the C + Return the negation of the complex number *num*, using the C :c:type:`Py_complex` representation. diff --git a/Doc/c-api/concrete.rst b/Doc/c-api/concrete.rst index c1d9fa1b41a3fe..84224dcca523b9 100644 --- a/Doc/c-api/concrete.rst +++ b/Doc/c-api/concrete.rst @@ -115,3 +115,4 @@ Other Objects coro.rst contextvars.rst datetime.rst + typehints.rst diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst index 21c508dc30ae22..fc90fa2b746e36 100644 --- a/Doc/c-api/exceptions.rst +++ b/Doc/c-api/exceptions.rst @@ -100,7 +100,7 @@ For convenience, some of these functions will always return a This is the most common way to set the error indicator. The first argument specifies the exception type; it is normally one of the standard exceptions, e.g. :c:data:`PyExc_RuntimeError`. You need not increment its reference count. - The second argument is an error message; it is decoded from ``'utf-8``'. + The second argument is an error message; it is decoded from ``'utf-8'``. .. c:function:: void PyErr_SetObject(PyObject *type, PyObject *value) @@ -253,6 +253,14 @@ For convenience, some of these functions will always return a .. versionadded:: 3.3 +.. c:function:: PyObject* PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, PyObject *name, PyObject *path) + + Much like :c:func:`PyErr_SetImportError` but this function allows for + specifying a subclass of :exc:`ImportError` to raise. + + .. versionadded:: 3.6 + + .. c:function:: void PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset) Set file, line, and offset information for the current exception. If the @@ -273,7 +281,7 @@ For convenience, some of these functions will always return a .. c:function:: void PyErr_SyntaxLocation(const char *filename, int lineno) - Like :c:func:`PyErr_SyntaxLocationEx`, but the col_offset parameter is + Like :c:func:`PyErr_SyntaxLocationEx`, but the *col_offset* parameter is omitted. @@ -320,19 +328,12 @@ an error value). :mod:`warnings` module and the :option:`-W` option in the command line documentation. There is no C API for warning control. -.. c:function:: PyObject* PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, PyObject *name, PyObject *path) - - Much like :c:func:`PyErr_SetImportError` but this function allows for - specifying a subclass of :exc:`ImportError` to raise. - - .. versionadded:: 3.6 - .. c:function:: int PyErr_WarnExplicitObject(PyObject *category, PyObject *message, PyObject *filename, int lineno, PyObject *module, PyObject *registry) Issue a warning message with explicit control over all warning attributes. This is a straightforward wrapper around the Python function - :func:`warnings.warn_explicit`, see there for more information. The *module* + :func:`warnings.warn_explicit`; see there for more information. The *module* and *registry* arguments may be set to ``NULL`` to get the default effect described there. @@ -440,7 +441,7 @@ Querying the error indicator error indicator. -.. c:function:: void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb) +.. c:function:: void PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) Under certain circumstances, the values returned by :c:func:`PyErr_Fetch` below can be "unnormalized", meaning that ``*exc`` is a class object but ``*val`` is @@ -888,11 +889,11 @@ the variables: +-----------------------------------------+---------------------------------+----------+ | C Name | Python Name | Notes | +=========================================+=================================+==========+ -| :c:data:`PyExc_BaseException` | :exc:`BaseException` | \(1) | +| :c:data:`PyExc_BaseException` | :exc:`BaseException` | [1]_ | +-----------------------------------------+---------------------------------+----------+ -| :c:data:`PyExc_Exception` | :exc:`Exception` | \(1) | +| :c:data:`PyExc_Exception` | :exc:`Exception` | [1]_ | +-----------------------------------------+---------------------------------+----------+ -| :c:data:`PyExc_ArithmeticError` | :exc:`ArithmeticError` | \(1) | +| :c:data:`PyExc_ArithmeticError` | :exc:`ArithmeticError` | [1]_ | +-----------------------------------------+---------------------------------+----------+ | :c:data:`PyExc_AssertionError` | :exc:`AssertionError` | | +-----------------------------------------+---------------------------------+----------+ @@ -938,7 +939,7 @@ the variables: +-----------------------------------------+---------------------------------+----------+ | :c:data:`PyExc_KeyboardInterrupt` | :exc:`KeyboardInterrupt` | | +-----------------------------------------+---------------------------------+----------+ -| :c:data:`PyExc_LookupError` | :exc:`LookupError` | \(1) | +| :c:data:`PyExc_LookupError` | :exc:`LookupError` | [1]_ | +-----------------------------------------+---------------------------------+----------+ | :c:data:`PyExc_MemoryError` | :exc:`MemoryError` | | +-----------------------------------------+---------------------------------+----------+ @@ -950,7 +951,7 @@ the variables: +-----------------------------------------+---------------------------------+----------+ | :c:data:`PyExc_NotImplementedError` | :exc:`NotImplementedError` | | +-----------------------------------------+---------------------------------+----------+ -| :c:data:`PyExc_OSError` | :exc:`OSError` | \(1) | +| :c:data:`PyExc_OSError` | :exc:`OSError` | [1]_ | +-----------------------------------------+---------------------------------+----------+ | :c:data:`PyExc_OverflowError` | :exc:`OverflowError` | | +-----------------------------------------+---------------------------------+----------+ @@ -960,7 +961,7 @@ the variables: +-----------------------------------------+---------------------------------+----------+ | :c:data:`PyExc_RecursionError` | :exc:`RecursionError` | | +-----------------------------------------+---------------------------------+----------+ -| :c:data:`PyExc_ReferenceError` | :exc:`ReferenceError` | \(2) | +| :c:data:`PyExc_ReferenceError` | :exc:`ReferenceError` | | +-----------------------------------------+---------------------------------+----------+ | :c:data:`PyExc_RuntimeError` | :exc:`RuntimeError` | | +-----------------------------------------+---------------------------------+----------+ @@ -1025,7 +1026,7 @@ These are compatibility aliases to :c:data:`PyExc_OSError`: +-------------------------------------+----------+ | :c:data:`PyExc_IOError` | | +-------------------------------------+----------+ -| :c:data:`PyExc_WindowsError` | \(3) | +| :c:data:`PyExc_WindowsError` | [2]_ | +-------------------------------------+----------+ .. versionchanged:: 3.3 @@ -1033,10 +1034,10 @@ These are compatibility aliases to :c:data:`PyExc_OSError`: Notes: -(1) +.. [1] This is a base class for other standard exceptions. -(2) +.. [2] Only defined on Windows; protect code that uses this by testing that the preprocessor macro ``MS_WINDOWS`` is defined. @@ -1066,7 +1067,7 @@ the variables: +------------------------------------------+---------------------------------+----------+ | C Name | Python Name | Notes | +==========================================+=================================+==========+ -| :c:data:`PyExc_Warning` | :exc:`Warning` | \(1) | +| :c:data:`PyExc_Warning` | :exc:`Warning` | [3]_ | +------------------------------------------+---------------------------------+----------+ | :c:data:`PyExc_BytesWarning` | :exc:`BytesWarning` | | +------------------------------------------+---------------------------------+----------+ @@ -1094,5 +1095,5 @@ the variables: Notes: -(1) +.. [3] This is a base class for other standard warning categories. diff --git a/Doc/c-api/gcsupport.rst b/Doc/c-api/gcsupport.rst index 55ed9d4f7fad48..8c90d1e8991c10 100644 --- a/Doc/c-api/gcsupport.rst +++ b/Doc/c-api/gcsupport.rst @@ -33,6 +33,26 @@ Constructors for container types must conform to two rules: #. Once all the fields which may contain references to other containers are initialized, it must call :c:func:`PyObject_GC_Track`. +Similarly, the deallocator for the object must conform to a similar pair of +rules: + +#. Before fields which refer to other containers are invalidated, + :c:func:`PyObject_GC_UnTrack` must be called. + +#. The object's memory must be deallocated using :c:func:`PyObject_GC_Del`. + + .. warning:: + If a type adds the Py_TPFLAGS_HAVE_GC, then it *must* implement at least + a :c:member:`~PyTypeObject.tp_traverse` handler or explicitly use one + from its subclass or subclasses. + + When calling :c:func:`PyType_Ready` or some of the APIs that indirectly + call it like :c:func:`PyType_FromSpecWithBases` or + :c:func:`PyType_FromSpec` the interpreter will automatically populate the + :c:member:`~PyTypeObject.tp_flags`, :c:member:`~PyTypeObject.tp_traverse` + and :c:member:`~PyTypeObject.tp_clear` fields if the type inherits from a + class that implements the garbage collector protocol and the child class + does *not* include the :const:`Py_TPFLAGS_HAVE_GC` flag. .. c:function:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type) @@ -88,14 +108,6 @@ Constructors for container types must conform to two rules: .. versionadded:: 3.9 -Similarly, the deallocator for the object must conform to a similar pair of -rules: - -#. Before fields which refer to other containers are invalidated, - :c:func:`PyObject_GC_UnTrack` must be called. - -#. The object's memory must be deallocated using :c:func:`PyObject_GC_Del`. - .. c:function:: void PyObject_GC_Del(void *op) diff --git a/Doc/c-api/import.rst b/Doc/c-api/import.rst index c6fc33076f0f51..d2ae6b6d4e4711 100644 --- a/Doc/c-api/import.rst +++ b/Doc/c-api/import.rst @@ -299,4 +299,8 @@ Importing Modules field; failure to provide the sentinel value can result in a memory fault. Returns ``0`` on success or ``-1`` if insufficient memory could be allocated to extend the internal table. In the event of failure, no modules are added to the - internal table. This should be called before :c:func:`Py_Initialize`. + internal table. This must be called before :c:func:`Py_Initialize`. + + If Python is initialized multiple times, :c:func:`PyImport_AppendInittab` or + :c:func:`PyImport_ExtendInittab` must be called before each Python + initialization. diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index 0f759732b35ea8..a44442a5015550 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -473,7 +473,7 @@ Process-wide parameters (set by :c:func:`Py_SetProgramName` above) and some environment variables. The returned string consists of a series of directory names separated by a platform dependent delimiter character. The delimiter character is ``':'`` - on Unix and Mac OS X, ``';'`` on Windows. The returned string points into + on Unix and macOS, ``';'`` on Windows. The returned string points into static storage; the caller should not modify its value. The list :data:`sys.path` is initialized with this value on interpreter startup; it can be (and usually is) modified later to change the search path for loading @@ -500,7 +500,7 @@ Process-wide parameters default search path but uses the one provided instead. This is useful if Python is embedded by an application that has full knowledge of the location of all modules. The path components should be separated by the platform - dependent delimiter character, which is ``':'`` on Unix and Mac OS X, ``';'`` + dependent delimiter character, which is ``':'`` on Unix and macOS, ``';'`` on Windows. This also causes :data:`sys.executable` to be set to the program @@ -529,7 +529,7 @@ Process-wide parameters .. index:: single: version (in module sys) The first word (up to the first space character) is the current Python version; - the first three characters are the major and minor version separated by a + the first characters are the major and minor version separated by a period. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as :data:`sys.version`. @@ -541,7 +541,7 @@ Process-wide parameters Return the platform identifier for the current platform. On Unix, this is formed from the "official" name of the operating system, converted to lower case, followed by the major revision number; e.g., for Solaris 2.x, which is - also known as SunOS 5.x, the value is ``'sunos5'``. On Mac OS X, it is + also known as SunOS 5.x, the value is ``'sunos5'``. On macOS, it is ``'darwin'``. On Windows, it is ``'win'``. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as ``sys.platform``. @@ -1725,7 +1725,7 @@ is not possible due to its implementation being opaque at build time. argument is `NULL`. .. note:: - A freed key becomes a dangling pointer, you should reset the key to + A freed key becomes a dangling pointer. You should reset the key to `NULL`. diff --git a/Doc/c-api/init_config.rst b/Doc/c-api/init_config.rst index de85029481185a..2b6da2a7810bc4 100644 --- a/Doc/c-api/init_config.rst +++ b/Doc/c-api/init_config.rst @@ -16,13 +16,13 @@ There are two kinds of configuration: * The :ref:`Python Configuration ` can be used to build a customized Python which behaves as the regular Python. For example, - environments variables and command line arguments are used to configure + environment variables and command line arguments are used to configure Python. * The :ref:`Isolated Configuration ` can be used to embed Python into an application. It isolates Python from the system. For example, - environments variables are ignored, the LC_CTYPE locale is left unchanged and - no signal handler is registred. + environment variables are ignored, the LC_CTYPE locale is left unchanged and + no signal handler is registered. The :c:func:`Py_RunMain` function can be used to write a customized Python program. @@ -634,7 +634,7 @@ PyConfig .. c:member:: int dump_refs - Dump Python refererences? + Dump Python references? If non-zero, dump all objects which are still alive at exit. @@ -689,14 +689,13 @@ PyConfig * ``"utf-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero. * ``"ascii"`` if Python detects that ``nl_langinfo(CODESET)`` announces - the ASCII encoding (or Roman8 encoding on HP-UX), whereas the - ``mbstowcs()`` function decodes from a different encoding (usually - Latin1). + the ASCII encoding, whereas the ``mbstowcs()`` function + decodes from a different encoding (usually Latin1). * ``"utf-8"`` if ``nl_langinfo(CODESET)`` returns an empty string. * Otherwise, use the :term:`locale encoding`: ``nl_langinfo(CODESET)`` result. - At Python statup, the encoding name is normalized to the Python codec + At Python startup, the encoding name is normalized to the Python codec name. For example, ``"ANSI_X3.4-1968"`` is replaced with ``"ascii"``. See also the :c:member:`~PyConfig.filesystem_errors` member. @@ -1193,7 +1192,10 @@ The caller is responsible to handle exceptions (error or exit) using If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or :c:func:`PyImport_ExtendInittab` are used, they must be set or called after -Python preinitialization and before the Python initialization. +Python preinitialization and before the Python initialization. If Python is +initialized multiple times, :c:func:`PyImport_AppendInittab` or +:c:func:`PyImport_ExtendInittab` must be called before each Python +initialization. The current configuration (``PyConfig`` type) is stored in ``PyInterpreterState.config``. @@ -1284,7 +1286,7 @@ Isolated Configuration isolate Python from the system. For example, to embed Python into an application. -This configuration ignores global configuration variables, environments +This configuration ignores global configuration variables, environment variables, command line arguments (:c:member:`PyConfig.argv` is not parsed) and user site directory. The C standard streams (ex: ``stdout``) and the LC_CTYPE locale are left unchanged. Signal handlers are not installed. @@ -1429,7 +1431,7 @@ Multi-Phase Initialization Private Provisional API ================================================== This section is a private provisional API introducing multi-phase -initialization, the core feature of the :pep:`432`: +initialization, the core feature of :pep:`432`: * "Core" initialization phase, "bare minimum Python": diff --git a/Doc/c-api/intro.rst b/Doc/c-api/intro.rst index 2d85d30702df9c..083f6d32c79f83 100644 --- a/Doc/c-api/intro.rst +++ b/Doc/c-api/intro.rst @@ -502,6 +502,13 @@ data attributes of a new object type, and another is used to describe the value of a complex number. These will be discussed together with the functions that use them. +.. c:type:: Py_ssize_t + + A signed integral type such that ``sizeof(Py_ssize_t) == sizeof(size_t)``. + C99 doesn't define such a thing directly (size_t is an unsigned integral type). + See :pep:`353` for details. ``PY_SSIZE_T_MAX`` is the largest positive value + of type :c:type:`Py_ssize_t`. + .. _api-exceptions: diff --git a/Doc/c-api/iter.rst b/Doc/c-api/iter.rst index 63290e0a7f0bf7..434d2021cea8e6 100644 --- a/Doc/c-api/iter.rst +++ b/Doc/c-api/iter.rst @@ -9,22 +9,23 @@ There are two functions specifically for working with iterators. .. c:function:: int PyIter_Check(PyObject *o) - Return non-zero if the object *o* supports the iterator protocol, and ``0`` - otherwise. This function always succeeds. + Return non-zero if the object *o* can be safely passed to + :c:func:`PyIter_Next`, and ``0`` otherwise. This function always succeeds. -.. c:function:: int PyAiter_Check(PyObject *o) +.. c:function:: int PyAIter_Check(PyObject *o) - Returns non-zero if the object 'obj' provides :class:`AsyncIterator` - protocols, and ``0`` otherwise. This function always succeeds. + Return non-zero if the object *o* provides the :class:`AsyncIterator` + protocol, and ``0`` otherwise. This function always succeeds. .. versionadded:: 3.10 .. c:function:: PyObject* PyIter_Next(PyObject *o) - Return the next value from the iteration *o*. The object must be an iterator - (it is up to the caller to check this). If there are no remaining values, - returns ``NULL`` with no exception set. If an error occurs while retrieving - the item, returns ``NULL`` and passes along the exception. + Return the next value from the iterator *o*. The object must be an iterator + according to :c:func:`PyIter_Check` (it is up to the caller to check this). + If there are no remaining values, returns ``NULL`` with no exception set. + If an error occurs while retrieving the item, returns ``NULL`` and passes + along the exception. To write a loop which iterates over an iterator, the C code should look something like this:: diff --git a/Doc/c-api/long.rst b/Doc/c-api/long.rst index 4201490286b82f..620344e71373b2 100644 --- a/Doc/c-api/long.rst +++ b/Doc/c-api/long.rst @@ -41,7 +41,7 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate. Return a new :c:type:`PyLongObject` object from *v*, or ``NULL`` on failure. The current implementation keeps an array of integer objects for all integers - between ``-5`` and ``256``, when you create an int in that range you actually + between ``-5`` and ``256``. When you create an int in that range you actually just get back a reference to the existing object. diff --git a/Doc/c-api/mapping.rst b/Doc/c-api/mapping.rst index 682160d1475c1c..3c9d282c6d0ab0 100644 --- a/Doc/c-api/mapping.rst +++ b/Doc/c-api/mapping.rst @@ -11,10 +11,10 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and .. c:function:: int PyMapping_Check(PyObject *o) - Return ``1`` if the object provides mapping protocol or supports slicing, + Return ``1`` if the object provides the mapping protocol or supports slicing, and ``0`` otherwise. Note that it returns ``1`` for Python classes with - a :meth:`__getitem__` method since in general case it is impossible to - determine what type of keys it supports. This function always succeeds. + a :meth:`__getitem__` method, since in general it is impossible to + determine what type of keys the class supports. This function always succeeds. .. c:function:: Py_ssize_t PyMapping_Size(PyObject *o) diff --git a/Doc/c-api/memory.rst b/Doc/c-api/memory.rst index efddc6f7be5e71..f17d24bda589c1 100644 --- a/Doc/c-api/memory.rst +++ b/Doc/c-api/memory.rst @@ -306,7 +306,7 @@ memory from the Python heap. .. note:: There is no guarantee that the memory returned by these allocators can be - successfully casted to a Python object when intercepting the allocating + successfully cast to a Python object when intercepting the allocating functions in this domain by the methods described in the :ref:`Customize Memory Allocators ` section. @@ -403,7 +403,7 @@ Customize Memory Allocators .. c:type:: PyMemAllocatorEx Structure used to describe a memory block allocator. The structure has - four fields: + the following fields: +----------------------------------------------------------+---------------------------------------+ | Field | Meaning | @@ -488,7 +488,7 @@ Customize Memory Allocators Debug hooks on the Python memory allocators =========================================== -When :ref:`Python is built is debug mode `, the +When :ref:`Python is built in debug mode `, the :c:func:`PyMem_SetupDebugHooks` function is called at the :ref:`Python preinitialization ` to setup debug hooks on Python memory allocators to detect memory errors. diff --git a/Doc/c-api/method.rst b/Doc/c-api/method.rst index 23852251dfe020..6e7e1e21aa93f2 100644 --- a/Doc/c-api/method.rst +++ b/Doc/c-api/method.rst @@ -27,7 +27,7 @@ to bind a :c:data:`PyCFunction` to a class object. It replaces the former call .. c:function:: PyObject* PyInstanceMethod_New(PyObject *func) - Return a new instance method object, with *func* being any callable object + Return a new instance method object, with *func* being any callable object. *func* is the function that will be called when the instance method is called. diff --git a/Doc/c-api/module.rst b/Doc/c-api/module.rst index a2541afb685c30..94c8d9f981713f 100644 --- a/Doc/c-api/module.rst +++ b/Doc/c-api/module.rst @@ -221,6 +221,12 @@ or request "multi-phase initialization" by returning the definition struct itsel than 0 and the module state (as returned by :c:func:`PyModule_GetState`) is ``NULL``. + Like :c:member:`PyTypeObject.tp_clear`, this function is not *always* + called before a module is deallocated. For example, when reference + counting is enough to determine that an object is no longer used, + the cyclic garbage collector is not involved and + :c:member:`~PyModuleDef.m_free` is called directly. + .. versionchanged:: 3.9 No longer called before the module state is allocated. diff --git a/Doc/c-api/number.rst b/Doc/c-api/number.rst index 37979bb506bcfa..70b91f8c2d0ca1 100644 --- a/Doc/c-api/number.rst +++ b/Doc/c-api/number.rst @@ -44,7 +44,7 @@ Number Protocol .. c:function:: PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2) Return the floor of *o1* divided by *o2*, or ``NULL`` on failure. This is - equivalent to the "classic" division of integers. + the equivalent of the Python expression ``o1 // o2``. .. c:function:: PyObject* PyNumber_TrueDivide(PyObject *o1, PyObject *o2) @@ -53,7 +53,7 @@ Number Protocol *o2*, or ``NULL`` on failure. The return value is "approximate" because binary floating point numbers are approximate; it is not possible to represent all real numbers in base two. This function can return a floating point value when - passed two integers. + passed two integers. This is the equivalent of the Python expression ``o1 / o2``. .. c:function:: PyObject* PyNumber_Remainder(PyObject *o1, PyObject *o2) @@ -180,6 +180,7 @@ Number Protocol floating point numbers are approximate; it is not possible to represent all real numbers in base two. This function can return a floating point value when passed two integers. The operation is done *in-place* when *o1* supports it. + This is the equivalent of the Python statement ``o1 /= o2``. .. c:function:: PyObject* PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2) @@ -272,11 +273,11 @@ Number Protocol .. c:function:: Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc) - Returns *o* converted to a Py_ssize_t value if *o* can be interpreted as an + Returns *o* converted to a :c:type:`Py_ssize_t` value if *o* can be interpreted as an integer. If the call fails, an exception is raised and ``-1`` is returned. If *o* can be converted to a Python int but the attempt to - convert to a Py_ssize_t value would raise an :exc:`OverflowError`, then the + convert to a :c:type:`Py_ssize_t` value would raise an :exc:`OverflowError`, then the *exc* argument is the type of exception that will be raised (usually :exc:`IndexError` or :exc:`OverflowError`). If *exc* is ``NULL``, then the exception is cleared and the value is clipped to ``PY_SSIZE_T_MIN`` for a negative @@ -285,6 +286,6 @@ Number Protocol .. c:function:: int PyIndex_Check(PyObject *o) - Returns ``1`` if *o* is an index integer (has the nb_index slot of the - tp_as_number structure filled in), and ``0`` otherwise. + Returns ``1`` if *o* is an index integer (has the ``nb_index`` slot of the + ``tp_as_number`` structure filled in), and ``0`` otherwise. This function always succeeds. diff --git a/Doc/c-api/objbuffer.rst b/Doc/c-api/objbuffer.rst new file mode 100644 index 00000000000000..6b82a642d7ee42 --- /dev/null +++ b/Doc/c-api/objbuffer.rst @@ -0,0 +1,55 @@ +.. highlight:: c + +Old Buffer Protocol +------------------- + +.. deprecated:: 3.0 + +These functions were part of the "old buffer protocol" API in Python 2. +In Python 3, this protocol doesn't exist anymore but the functions are still +exposed to ease porting 2.x code. They act as a compatibility wrapper +around the :ref:`new buffer protocol `, but they don't give +you control over the lifetime of the resources acquired when a buffer is +exported. + +Therefore, it is recommended that you call :c:func:`PyObject_GetBuffer` +(or the ``y*`` or ``w*`` :ref:`format codes ` with the +:c:func:`PyArg_ParseTuple` family of functions) to get a buffer view over +an object, and :c:func:`PyBuffer_Release` when the buffer view can be released. + + +.. c:function:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len) + + Returns a pointer to a read-only memory location usable as character-based + input. The *obj* argument must support the single-segment character buffer + interface. On success, returns ``0``, sets *buffer* to the memory location + and *buffer_len* to the buffer length. Returns ``-1`` and sets a + :exc:`TypeError` on error. + + +.. c:function:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len) + + Returns a pointer to a read-only memory location containing arbitrary data. + The *obj* argument must support the single-segment readable buffer + interface. On success, returns ``0``, sets *buffer* to the memory location + and *buffer_len* to the buffer length. Returns ``-1`` and sets a + :exc:`TypeError` on error. + + +.. c:function:: int PyObject_CheckReadBuffer(PyObject *o) + + Returns ``1`` if *o* supports the single-segment readable buffer interface. + Otherwise returns ``0``. This function always succeeds. + + Note that this function tries to get and release a buffer, and exceptions + which occur while calling corresponding functions will get suppressed. + To get error reporting use :c:func:`PyObject_GetBuffer()` instead. + + +.. c:function:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len) + + Returns a pointer to a writable memory location. The *obj* argument must + support the single-segment, character buffer interface. On success, + returns ``0``, sets *buffer* to the memory location and *buffer_len* to the + buffer length. Returns ``-1`` and sets a :exc:`TypeError` on error. + diff --git a/Doc/c-api/object.rst b/Doc/c-api/object.rst index 42e3340acb79a0..07a625bac02fc4 100644 --- a/Doc/c-api/object.rst +++ b/Doc/c-api/object.rst @@ -81,8 +81,9 @@ Object Protocol return ``0`` on success. This is the equivalent of the Python statement ``o.attr_name = v``. - If *v* is ``NULL``, the attribute is deleted, however this feature is - deprecated in favour of using :c:func:`PyObject_DelAttr`. + If *v* is ``NULL``, the attribute is deleted. This behaviour is deprecated + in favour of using :c:func:`PyObject_DelAttr`, but there are currently no + plans to remove it. .. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v) @@ -92,7 +93,7 @@ Object Protocol return ``0`` on success. This is the equivalent of the Python statement ``o.attr_name = v``. - If *v* is ``NULL``, the attribute is deleted, however this feature is + If *v* is ``NULL``, the attribute is deleted, but this feature is deprecated in favour of using :c:func:`PyObject_DelAttrString`. @@ -257,7 +258,7 @@ Object Protocol .. versionchanged:: 3.2 The return type is now Py_hash_t. This is a signed integer the same size - as Py_ssize_t. + as :c:type:`Py_ssize_t`. .. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o) @@ -290,7 +291,7 @@ Object Protocol of object *o*. On failure, raises :exc:`SystemError` and returns ``NULL``. This is equivalent to the Python expression ``type(o)``. This function increments the reference count of the return value. There's really no reason to use this - function instead of the common expression ``o->ob_type``, which returns a + function instead of the :c:func:`Py_TYPE()` function, which returns a pointer of type :c:type:`PyTypeObject*`, except when the incremented reference count is needed. @@ -311,12 +312,12 @@ Object Protocol returned. This is the equivalent to the Python expression ``len(o)``. -.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t default) +.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) Return an estimated length for the object *o*. First try to return its actual length, then an estimate using :meth:`~object.__length_hint__`, and finally return the default value. On error return ``-1``. This is the - equivalent to the Python expression ``operator.length_hint(o, default)``. + equivalent to the Python expression ``operator.length_hint(o, defaultvalue)``. .. versionadded:: 3.4 @@ -358,7 +359,7 @@ Object Protocol iterated. -.. c:function:: PyObject* PyObject_GetAiter(PyObject *o) +.. c:function:: PyObject* PyObject_GetAIter(PyObject *o) This is the equivalent to the Python expression ``aiter(o)``. Takes an :class:`AsyncIterable` object and returns an :class:`AsyncIterator` for it. diff --git a/Doc/c-api/refcounting.rst b/Doc/c-api/refcounting.rst index 391907c8c2976a..738bd77e9ce42f 100644 --- a/Doc/c-api/refcounting.rst +++ b/Doc/c-api/refcounting.rst @@ -109,11 +109,17 @@ objects. It is a good idea to use this macro whenever decrementing the reference count of an object that might be traversed during garbage collection. +.. c:function:: void Py_IncRef(PyObject *o) + + Increment the reference count for object *o*. A function version of :c:func:`Py_XINCREF`. + It can be used for runtime dynamic embedding of Python. + + +.. c:function:: void Py_DecRef(PyObject *o) + + Decrement the reference count for object *o*. A function version of :c:func:`Py_XDECREF`. + It can be used for runtime dynamic embedding of Python. -The following functions are for runtime dynamic embedding of Python: -``Py_IncRef(PyObject *o)``, ``Py_DecRef(PyObject *o)``. They are -simply exported function versions of :c:func:`Py_XINCREF` and -:c:func:`Py_XDECREF`, respectively. The following functions or macros are only for use within the interpreter core: :c:func:`_Py_Dealloc`, :c:func:`_Py_ForgetReference`, :c:func:`_Py_NewReference`, diff --git a/Doc/c-api/reflection.rst b/Doc/c-api/reflection.rst index 64ce4d1d0c34df..fe7741a2d7a9fb 100644 --- a/Doc/c-api/reflection.rst +++ b/Doc/c-api/reflection.rst @@ -31,7 +31,7 @@ Reflection See also :c:func:`PyThreadState_GetFrame`. -.. c:function:: int PyFrame_GetBack(PyFrameObject *frame) +.. c:function:: PyFrameObject* PyFrame_GetBack(PyFrameObject *frame) Get the *frame* next outer frame. @@ -42,7 +42,7 @@ Reflection .. versionadded:: 3.9 -.. c:function:: int PyFrame_GetCode(PyFrameObject *frame) +.. c:function:: PyCodeObject* PyFrame_GetCode(PyFrameObject *frame) Get the *frame* code. diff --git a/Doc/c-api/sequence.rst b/Doc/c-api/sequence.rst index 65818859041179..c78d273f9f149f 100644 --- a/Doc/c-api/sequence.rst +++ b/Doc/c-api/sequence.rst @@ -8,10 +8,10 @@ Sequence Protocol .. c:function:: int PySequence_Check(PyObject *o) - Return ``1`` if the object provides sequence protocol, and ``0`` otherwise. + Return ``1`` if the object provides the sequence protocol, and ``0`` otherwise. Note that it returns ``1`` for Python classes with a :meth:`__getitem__` - method unless they are :class:`dict` subclasses since in general case it - is impossible to determine what the type of keys it supports. This + method, unless they are :class:`dict` subclasses, since in general it + is impossible to determine what type of keys the class supports. This function always succeeds. @@ -69,7 +69,7 @@ Sequence Protocol is the equivalent of the Python statement ``o[i] = v``. This function *does not* steal a reference to *v*. - If *v* is ``NULL``, the element is deleted, however this feature is + If *v* is ``NULL``, the element is deleted, but this feature is deprecated in favour of using :c:func:`PySequence_DelItem`. @@ -147,7 +147,7 @@ Sequence Protocol Returns the length of *o*, assuming that *o* was returned by :c:func:`PySequence_Fast` and that *o* is not ``NULL``. The size can also be - gotten by calling :c:func:`PySequence_Size` on *o*, but + retrieved by calling :c:func:`PySequence_Size` on *o*, but :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list or tuple. diff --git a/Doc/c-api/set.rst b/Doc/c-api/set.rst index eca19c4d816474..f0d905bae8ae44 100644 --- a/Doc/c-api/set.rst +++ b/Doc/c-api/set.rst @@ -13,7 +13,7 @@ Set Objects object: frozenset This section details the public API for :class:`set` and :class:`frozenset` -objects. Any functionality not listed below is best accessed using the either +objects. Any functionality not listed below is best accessed using either the abstract object protocol (including :c:func:`PyObject_CallMethod`, :c:func:`PyObject_RichCompareBool`, :c:func:`PyObject_Hash`, :c:func:`PyObject_Repr`, :c:func:`PyObject_IsTrue`, :c:func:`PyObject_Print`, and @@ -31,7 +31,7 @@ the abstract object protocol (including :c:func:`PyObject_CallMethod`, in that it is a fixed size for small sets (much like tuple storage) and will point to a separate, variable sized block of memory for medium and large sized sets (much like list storage). None of the fields of this structure should be - considered public and are subject to change. All access should be done through + considered public and all are subject to change. All access should be done through the documented API rather than by manipulating the values in the structure. @@ -131,7 +131,7 @@ or :class:`frozenset` or instances of their subtypes. .. c:function:: int PySet_Add(PyObject *set, PyObject *key) Add *key* to a :class:`set` instance. Also works with :class:`frozenset` - instances (like :c:func:`PyTuple_SetItem` it can be used to fill-in the values + instances (like :c:func:`PyTuple_SetItem` it can be used to fill in the values of brand new frozensets before they are exposed to other code). Return ``0`` on success or ``-1`` on failure. Raise a :exc:`TypeError` if the *key* is unhashable. Raise a :exc:`MemoryError` if there is no room to grow. Raise a diff --git a/Doc/c-api/stable.rst b/Doc/c-api/stable.rst index 9c05cb3c82dfbe..4ae20e93e36785 100644 --- a/Doc/c-api/stable.rst +++ b/Doc/c-api/stable.rst @@ -2,37 +2,157 @@ .. _stable: -*********************************** +*************** +C API Stability +*************** + +Python's C API is covered by the Backwards Compatibility Policy, :pep:`387`. +While the C API will change with every minor release (e.g. from 3.9 to 3.10), +most changes will be source-compatible, typically by only adding new API. +Changing existing API or removing API is only done after a deprecation period +or to fix serious issues. + +CPython's Application Binary Interface (ABI) is forward- and +backwards-compatible across a minor release (if these are compiled the same +way; see :ref:`stable-abi-platform` below). +So, code compiled for Python 3.10.0 will work on 3.10.8 and vice versa, +but will need to be compiled separately for 3.9.x and 3.10.x. + +Names prefixed by an underscore, such as ``_Py_InternalState``, +are private API that can change without notice even in patch releases. + + Stable Application Binary Interface -*********************************** - -Traditionally, the C API of Python will change with every release. Most changes -will be source-compatible, typically by only adding API, rather than changing -existing API or removing API (although some interfaces do get removed after -being deprecated first). - -Unfortunately, the API compatibility does not extend to binary compatibility -(the ABI). The reason is primarily the evolution of struct definitions, where -addition of a new field, or changing the type of a field, might not break the -API, but can break the ABI. As a consequence, extension modules need to be -recompiled for every Python release (although an exception is possible on Unix -when none of the affected interfaces are used). In addition, on Windows, -extension modules link with a specific pythonXY.dll and need to be recompiled to -link with a newer one. - -Since Python 3.2, a subset of the API has been declared to guarantee a stable -ABI. Extension modules wishing to use this API (called "limited API") need to -define ``Py_LIMITED_API``. A number of interpreter details then become hidden -from the extension module; in return, a module is built that works on any 3.x -version (x>=2) without recompilation. - -In some cases, the stable ABI needs to be extended with new functions. -Extension modules wishing to use these new APIs need to set ``Py_LIMITED_API`` -to the ``PY_VERSION_HEX`` value (see :ref:`apiabiversion`) of the minimum Python -version they want to support (e.g. ``0x03030000`` for Python 3.3). Such modules -will work on all subsequent Python releases, but fail to load (because of -missing symbols) on the older releases. - -As of Python 3.2, the set of functions available to the limited API is -documented in :pep:`384`. In the C API documentation, API elements that are not -part of the limited API are marked as "Not part of the limited API." +=================================== + +Python 3.2 introduced the *Limited API*, a subset of Python's C API. +Extensions that only use the Limited API can be +compiled once and work with multiple versions of Python. +Contents of the Limited API are :ref:`listed below `. + +To enable this, Python provides a *Stable ABI*: a set of symbols that will +remain compatible across Python 3.x versions. The Stable ABI contains symbols +exposed in the Limited API, but also other ones – for example, functions +necessary to support older versions of the Limited API. + +(For simplicity, this document talks about *extensions*, but the Limited API +and Stable ABI work the same way for all uses of the API – for example, +embedding Python.) + +.. c:macro:: Py_LIMITED_API + + Define this macro before including ``Python.h`` to opt in to only use + the Limited API, and to select the Limited API version. + + Define ``Py_LIMITED_API`` to the value of :c:data:`PY_VERSION_HEX` + corresponding to the lowest Python version your extension supports. + The extension will work without recompilation with all Python 3 releases + from the specified one onward, and can use Limited API introduced up to that + version. + + Rather than using the ``PY_VERSION_HEX`` macro directly, hardcode a minimum + minor version (e.g. ``0x030A0000`` for Python 3.10) for stability when + compiling with future Python versions. + + You can also define ``Py_LIMITED_API`` to ``3``. This works the same as + ``0x03020000`` (Python 3.2, the version that introduced Limited API). + +On Windows, extensions that use the Stable ABI should be linked against +``python3.dll`` rather than a version-specific library such as +``python39.dll``. + +On some platforms, Python will look for and load shared library files named +with the ``abi3`` tag (e.g. ``mymodule.abi3.so``). +It does not check if such extensions conform to a Stable ABI. +The user (or their packaging tools) need to ensure that, for example, +extensions built with the 3.10+ Limited API are not installed for lower +versions of Python. + +All functions in the Stable ABI are present as functions in Python's shared +library, not solely as macros. This makes them usable from languages that don't +use the C preprocessor. + + +Limited API Scope and Performance +--------------------------------- + +The goal for the Limited API is to allow everything that is possible with the +full C API, but possibly with a performance penalty. + +For example, while :c:func:`PyList_GetItem` is available, its “unsafe” macro +variant :c:func:`PyList_GET_ITEM` is not. +The macro can be faster because it can rely on version-specific implementation +details of the list object. + +Without ``Py_LIMITED_API`` defined, some C API functions are inlined or +replaced by macros. +Defining ``Py_LIMITED_API`` disables this inlining, allowing stability as +Python's data structures are improved, but possibly reducing performance. + +By leaving out the ``Py_LIMITED_API`` definition, it is possible to compile +a Limited API extension with a version-specific ABI. This can improve +performance for that Python version, but will limit compatibility. +Compiling with ``Py_LIMITED_API`` will then yield an extension that can be +distributed where a version-specific one is not available – for example, +for prereleases of an upcoming Python version. + + +Limited API Caveats +------------------- + +Note that compiling with ``Py_LIMITED_API`` is *not* a complete guarantee that +code conforms to the Limited API or the Stable ABI. ``Py_LIMITED_API`` only +covers definitions, but an API also includes other issues, such as expected +semantics. + +One issue that ``Py_LIMITED_API`` does not guard against is calling a function +with arguments that are invalid in a lower Python version. +For example, consider a function that starts accepting ``NULL`` for an +argument. In Python 3.9, ``NULL`` now selects a default behavior, but in +Python 3.8, the argument will be used directly, causing a ``NULL`` dereference +and crash. A similar argument works for fields of structs. + +Another issue is that some struct fields are currently not hidden when +``Py_LIMITED_API`` is defined, even though they're part of the Limited API. + +For these reasons, we recommend testing an extension with *all* minor Python +versions it supports, and preferably to build with the *lowest* such version. + +We also recommend reviewing documentation of all used API to check +if it is explicitly part of the Limited API. Even with ``Py_LIMITED_API`` +defined, a few private declarations are exposed for technical reasons (or +even unintentionally, as bugs). + +Also note that the Limited API is not necessarily stable: compiling with +``Py_LIMITED_API`` with Python 3.8 means that the extension will +run with Python 3.12, but it will not necessarily *compile* with Python 3.12. +In particular, parts of the Limited API may be deprecated and removed, +provided that the Stable ABI stays stable. + + +.. _stable-abi-platform: + +Platform Considerations +======================= + +ABI stability depends not only on Python, but also on the compiler used, +lower-level libraries and compiler options. For the purposes of the Stable ABI, +these details define a “platform”. They usually depend on the OS +type and processor architecture + +It is the responsibility of each particular distributor of Python +to ensure that all Python versions on a particular platform are built +in a way that does not break the Stable ABI. +This is the case with Windows and macOS releases from ``python.org`` and many +third-party distributors. + + +.. _stable-abi-list: + +Contents of Limited API +======================= + + +Currently, the Limited API includes the following items: + +.. limited-api-list:: diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst index 20d5485d5544c2..c1c83b035c8515 100644 --- a/Doc/c-api/structures.rst +++ b/Doc/c-api/structures.rst @@ -309,8 +309,6 @@ There are these calling conventions: or possibly ``NULL`` if there are no keywords. The values of the keyword arguments are stored in the *args* array, after the positional arguments. - This is not part of the :ref:`limited API `. - .. versionadded:: 3.7 @@ -469,6 +467,21 @@ Accessing attributes of extension types {NULL} /* Sentinel */ }; + +.. c:function:: PyObject* PyMember_GetOne(const char *obj_addr, struct PyMemberDef *m) + + Get an attribute belonging to the object at address *obj_addr*. The + attribute is described by ``PyMemberDef`` *m*. Returns ``NULL`` + on error. + + +.. c:function:: int PyMember_SetOne(char *obj_addr, struct PyMemberDef *m, PyObject *o) + + Set an attribute belonging to the object at address *obj_addr* to object *o*. + The attribute to set is described by ``PyMemberDef`` *m*. Returns ``0`` + if successful and a negative value on failure. + + .. c:type:: PyGetSetDef Structure to define property-like access for a type. See also description of @@ -479,7 +492,7 @@ Accessing attributes of extension types +=============+==================+===================================+ | name | const char \* | attribute name | +-------------+------------------+-----------------------------------+ - | get | getter | C Function to get the attribute | + | get | getter | C function to get the attribute | +-------------+------------------+-----------------------------------+ | set | setter | optional C function to set or | | | | delete the attribute, if omitted | diff --git a/Doc/c-api/sys.rst b/Doc/c-api/sys.rst index 97717f5fc19230..7cf0a6b51f7ddc 100644 --- a/Doc/c-api/sys.rst +++ b/Doc/c-api/sys.rst @@ -177,7 +177,7 @@ Operating System Utilities 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 - error + error. 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. @@ -207,7 +207,7 @@ Operating System Utilities .. versionchanged:: 3.8 The function now uses the UTF-8 encoding on Windows if - :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero; + :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero. .. _systemfunctions: @@ -323,7 +323,7 @@ accessible to C code. They all work with the current interpreter thread's leaks.) Note that ``#`` format characters should always be treated as - ``Py_ssize_t``, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined. + :c:type:`Py_ssize_t`, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined. :func:`sys.audit` performs the same function from Python code. @@ -331,14 +331,14 @@ accessible to C code. They all work with the current interpreter thread's .. versionchanged:: 3.8.2 - Require ``Py_ssize_t`` for ``#`` format characters. Previously, an + Require :c:type:`Py_ssize_t` for ``#`` format characters. Previously, an unavoidable deprecation warning was raised. .. c:function:: int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData) Append the callable *hook* to the list of active auditing hooks. - Return zero for success + Return zero on success and non-zero on failure. If the runtime has been initialized, also set an error on failure. Hooks added through this API are called for all interpreters created by the runtime. diff --git a/Doc/c-api/type.rst b/Doc/c-api/type.rst index bdb636dff326f2..01d00bede544d7 100644 --- a/Doc/c-api/type.rst +++ b/Doc/c-api/type.rst @@ -13,7 +13,7 @@ Type Objects The C structure of the objects used to describe built-in types. -.. c:var:: PyObject* PyType_Type +.. c:var:: PyTypeObject PyType_Type This is the type object for type objects; it is the same object as :class:`type` in the Python layer. @@ -97,6 +97,15 @@ Type Objects from a type's base class. Return ``0`` on success, or return ``-1`` and sets an exception on error. + .. note:: + If some of the base classes implements the GC protocol and the provided + type does not include the :const:`Py_TPFLAGS_HAVE_GC` in its flags, then + the GC protocol will be automatically implemented from its parents. On + the contrary, if the type being created does include + :const:`Py_TPFLAGS_HAVE_GC` in its flags then it **must** implement the + GC protocol itself by at least implementing the + :c:member:`~PyTypeObject.tp_traverse` handle. + .. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot) Return the function pointer stored in the given slot. If the @@ -165,7 +174,7 @@ The following functions and structs are used to create The *module* argument can be used to record the module in which the new class is defined. It must be a module object or ``NULL``. If not ``NULL``, the module is associated with the new type and can later be - retreived with :c:func:`PyType_GetModule`. + retrieved with :c:func:`PyType_GetModule`. The associated module is not inherited by subclasses; it must be specified for each class individually. @@ -263,7 +272,7 @@ The following functions and structs are used to create .. versionchanged:: 3.9 - Slots in :c:type:`PyBufferProcs` in may be set in the unlimited API. + Slots in :c:type:`PyBufferProcs` may be set in the unlimited API. .. c:member:: void *PyType_Slot.pfunc diff --git a/Doc/c-api/typehints.rst b/Doc/c-api/typehints.rst new file mode 100644 index 00000000000000..dfda96a47243bd --- /dev/null +++ b/Doc/c-api/typehints.rst @@ -0,0 +1,47 @@ +.. highlight:: c + +.. _typehintobjects: + +Objects for Type Hinting +------------------------ + +Various built-in types for type hinting are provided. Currently, +two types exist -- :ref:`GenericAlias ` and +:ref:`Union `. Only ``GenericAlias`` is exposed to C. + +.. c:function:: PyObject* Py_GenericAlias(PyObject *origin, PyObject *args) + + Create a :ref:`GenericAlias ` object. + Equivalent to calling the Python class + :class:`types.GenericAlias`. The *origin* and *args* arguments set the + ``GenericAlias``\ 's ``__origin__`` and ``__args__`` attributes respectively. + *origin* should be a :c:type:`PyTypeObject*`, and *args* can be a + :c:type:`PyTupleObject*` or any ``PyObject*``. If *args* passed is + not a tuple, a 1-tuple is automatically constructed and ``__args__`` is set + to ``(args,)``. + Minimal checking is done for the arguments, so the function will succeed even + if *origin* is not a type. + The ``GenericAlias``\ 's ``__parameters__`` attribute is constructed lazily + from ``__args__``. On failure, an exception is raised and ``NULL`` is + returned. + + Here's an example of how to make an extension type generic:: + + ... + static PyMethodDef my_obj_methods[] = { + // Other methods. + ... + {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, "See PEP 585"} + ... + } + + .. seealso:: The data model method :meth:`__class_getitem__`. + + .. versionadded:: 3.9 + +.. c:var:: PyTypeObject Py_GenericAliasType + + The C type of the object returned by :c:func:`Py_GenericAlias`. Equivalent to + :class:`types.GenericAlias` in Python. + + .. versionadded:: 3.9 diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index 82f2ab53451166..6fcb4e0d1677b9 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -43,13 +43,13 @@ Quick Reference +================================================+===================================+===================+===+===+===+===+ | :c:member:`~PyTypeObject.tp_name` | const char * | __name__ | X | X | | | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ - | :c:member:`~PyTypeObject.tp_basicsize` | Py_ssize_t | | X | X | | X | + | :c:member:`~PyTypeObject.tp_basicsize` | :c:type:`Py_ssize_t` | | X | X | | X | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ - | :c:member:`~PyTypeObject.tp_itemsize` | Py_ssize_t | | | X | | X | + | :c:member:`~PyTypeObject.tp_itemsize` | :c:type:`Py_ssize_t` | | | X | | X | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | :c:member:`~PyTypeObject.tp_dealloc` | :c:type:`destructor` | | X | X | | X | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ - | :c:member:`~PyTypeObject.tp_vectorcall_offset` | Py_ssize_t | | | X | | X | + | :c:member:`~PyTypeObject.tp_vectorcall_offset` | :c:type:`Py_ssize_t` | | | X | | X | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | (:c:member:`~PyTypeObject.tp_getattr`) | :c:type:`getattrfunc` | __getattribute__, | | | | G | | | | __getattr__ | | | | | @@ -96,7 +96,7 @@ Quick Reference | | | __gt__, | | | | | | | | __ge__ | | | | | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ - | :c:member:`~PyTypeObject.tp_weaklistoffset` | Py_ssize_t | | | X | | ? | + | :c:member:`~PyTypeObject.tp_weaklistoffset` | :c:type:`Py_ssize_t` | | | X | | ? | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | :c:member:`~PyTypeObject.tp_iter` | :c:type:`getiterfunc` | __iter__ | | | | X | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ @@ -117,7 +117,7 @@ Quick Reference | :c:member:`~PyTypeObject.tp_descr_set` | :c:type:`descrsetfunc` | __set__, | | | | X | | | | __delete__ | | | | | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ - | :c:member:`~PyTypeObject.tp_dictoffset` | Py_ssize_t | | | X | | ? | + | :c:member:`~PyTypeObject.tp_dictoffset` | :c:type:`Py_ssize_t` | | | X | | ? | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ | :c:member:`~PyTypeObject.tp_init` | :c:type:`initproc` | __init__ | X | X | | X | +------------------------------------------------+-----------------------------------+-------------------+---+---+---+---+ @@ -189,138 +189,138 @@ sub-slots .. table:: :widths: 26,17,12 - +---------------------------------------------------------+-----------------------------------+--------------+ - | Slot | :ref:`Type ` | special | - | | | methods | - +=========================================================+===================================+==============+ - | :c:member:`~PyAsyncMethods.am_await` | :c:type:`unaryfunc` | __await__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyAsyncMethods.am_aiter` | :c:type:`unaryfunc` | __aiter__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyAsyncMethods.am_anext` | :c:type:`unaryfunc` | __anext__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyAsyncMethods.am_send` | :c:type:`sendfunc` | | - +---------------------------------------------------------+-----------------------------------+--------------+ - | | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_add` | :c:type:`binaryfunc` | __add__ | - | | | __radd__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_add` | :c:type:`binaryfunc` | __iadd__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_subtract` | :c:type:`binaryfunc` | __sub__ | - | | | __rsub__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_subtract` | :c:type:`binaryfunc` | __sub__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_multiply` | :c:type:`binaryfunc` | __mul__ | - | | | __rmul__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_multiply` | :c:type:`binaryfunc` | __mul__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_remainder` | :c:type:`binaryfunc` | __mod__ | - | | | __rmod__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_remainder` | :c:type:`binaryfunc` | __mod__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_divmod` | :c:type:`binaryfunc` | __divmod__ | - | | | __rdivmod__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_power` | :c:type:`ternaryfunc` | __pow__ | - | | | __rpow__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_power` | :c:type:`ternaryfunc` | __pow__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_negative` | :c:type:`unaryfunc` | __neg__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_positive` | :c:type:`unaryfunc` | __pos__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_absolute` | :c:type:`unaryfunc` | __abs__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_bool` | :c:type:`inquiry` | __bool__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_invert` | :c:type:`unaryfunc` | __invert__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_lshift` | :c:type:`binaryfunc` | __lshift__ | - | | | __rlshift__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_lshift` | :c:type:`binaryfunc` | __lshift__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_rshift` | :c:type:`binaryfunc` | __rshift__ | - | | | __rrshift__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_rshift` | :c:type:`binaryfunc` | __rshift__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_and` | :c:type:`binaryfunc` | __and__ | - | | | __rand__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_and` | :c:type:`binaryfunc` | __and__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_xor` | :c:type:`binaryfunc` | __xor__ | - | | | __rxor__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_xor` | :c:type:`binaryfunc` | __xor__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_or` | :c:type:`binaryfunc` | __or__ | - | | | __ror__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_or` | :c:type:`binaryfunc` | __or__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_int` | :c:type:`unaryfunc` | __int__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_reserved` | void * | | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_float` | :c:type:`unaryfunc` | __float__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_floor_divide` | :c:type:`binaryfunc` | __floordiv__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_floor_divide` | :c:type:`binaryfunc` | __floordiv__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_true_divide` | :c:type:`binaryfunc` | __truediv__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_true_divide` | :c:type:`binaryfunc` | __truediv__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_index` | :c:type:`unaryfunc` | __index__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_matrix_multiply` | :c:type:`binaryfunc` | __matmul__ | - | | | __rmatmul__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyNumberMethods.nb_inplace_matrix_multiply` | :c:type:`binaryfunc` | __matmul__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyMappingMethods.mp_length` | :c:type:`lenfunc` | __len__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyMappingMethods.mp_subscript` | :c:type:`binaryfunc` | __getitem__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyMappingMethods.mp_ass_subscript` | :c:type:`objobjargproc` | __setitem__, | - | | | __delitem__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PySequenceMethods.sq_length` | :c:type:`lenfunc` | __len__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PySequenceMethods.sq_concat` | :c:type:`binaryfunc` | __add__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PySequenceMethods.sq_repeat` | :c:type:`ssizeargfunc` | __mul__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PySequenceMethods.sq_item` | :c:type:`ssizeargfunc` | __getitem__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PySequenceMethods.sq_ass_item` | :c:type:`ssizeobjargproc` | __setitem__ | - | | | __delitem__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PySequenceMethods.sq_contains` | :c:type:`objobjproc` | __contains__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PySequenceMethods.sq_inplace_concat` | :c:type:`binaryfunc` | __iadd__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PySequenceMethods.sq_inplace_repeat` | :c:type:`ssizeargfunc` | __imul__ | - +---------------------------------------------------------+-----------------------------------+--------------+ - | | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyBufferProcs.bf_getbuffer` | :c:func:`getbufferproc` | | - +---------------------------------------------------------+-----------------------------------+--------------+ - | :c:member:`~PyBufferProcs.bf_releasebuffer` | :c:func:`releasebufferproc` | | - +---------------------------------------------------------+-----------------------------------+--------------+ + +---------------------------------------------------------+-----------------------------------+---------------+ + | Slot | :ref:`Type ` | special | + | | | methods | + +=========================================================+===================================+===============+ + | :c:member:`~PyAsyncMethods.am_await` | :c:type:`unaryfunc` | __await__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyAsyncMethods.am_aiter` | :c:type:`unaryfunc` | __aiter__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyAsyncMethods.am_anext` | :c:type:`unaryfunc` | __anext__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyAsyncMethods.am_send` | :c:type:`sendfunc` | | + +---------------------------------------------------------+-----------------------------------+---------------+ + | | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_add` | :c:type:`binaryfunc` | __add__ | + | | | __radd__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_add` | :c:type:`binaryfunc` | __iadd__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_subtract` | :c:type:`binaryfunc` | __sub__ | + | | | __rsub__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_subtract` | :c:type:`binaryfunc` | __isub__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_multiply` | :c:type:`binaryfunc` | __mul__ | + | | | __rmul__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_multiply` | :c:type:`binaryfunc` | __imul__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_remainder` | :c:type:`binaryfunc` | __mod__ | + | | | __rmod__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_remainder` | :c:type:`binaryfunc` | __imod__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_divmod` | :c:type:`binaryfunc` | __divmod__ | + | | | __rdivmod__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_power` | :c:type:`ternaryfunc` | __pow__ | + | | | __rpow__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_power` | :c:type:`ternaryfunc` | __ipow__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_negative` | :c:type:`unaryfunc` | __neg__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_positive` | :c:type:`unaryfunc` | __pos__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_absolute` | :c:type:`unaryfunc` | __abs__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_bool` | :c:type:`inquiry` | __bool__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_invert` | :c:type:`unaryfunc` | __invert__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_lshift` | :c:type:`binaryfunc` | __lshift__ | + | | | __rlshift__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_lshift` | :c:type:`binaryfunc` | __ilshift__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_rshift` | :c:type:`binaryfunc` | __rshift__ | + | | | __rrshift__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_rshift` | :c:type:`binaryfunc` | __irshift__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_and` | :c:type:`binaryfunc` | __and__ | + | | | __rand__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_and` | :c:type:`binaryfunc` | __iand__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_xor` | :c:type:`binaryfunc` | __xor__ | + | | | __rxor__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_xor` | :c:type:`binaryfunc` | __ixor__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_or` | :c:type:`binaryfunc` | __or__ | + | | | __ror__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_or` | :c:type:`binaryfunc` | __ior__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_int` | :c:type:`unaryfunc` | __int__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_reserved` | void * | | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_float` | :c:type:`unaryfunc` | __float__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_floor_divide` | :c:type:`binaryfunc` | __floordiv__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_floor_divide` | :c:type:`binaryfunc` | __ifloordiv__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_true_divide` | :c:type:`binaryfunc` | __truediv__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_true_divide` | :c:type:`binaryfunc` | __itruediv__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_index` | :c:type:`unaryfunc` | __index__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_matrix_multiply` | :c:type:`binaryfunc` | __matmul__ | + | | | __rmatmul__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyNumberMethods.nb_inplace_matrix_multiply` | :c:type:`binaryfunc` | __imatmul__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyMappingMethods.mp_length` | :c:type:`lenfunc` | __len__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyMappingMethods.mp_subscript` | :c:type:`binaryfunc` | __getitem__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyMappingMethods.mp_ass_subscript` | :c:type:`objobjargproc` | __setitem__, | + | | | __delitem__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PySequenceMethods.sq_length` | :c:type:`lenfunc` | __len__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PySequenceMethods.sq_concat` | :c:type:`binaryfunc` | __add__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PySequenceMethods.sq_repeat` | :c:type:`ssizeargfunc` | __mul__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PySequenceMethods.sq_item` | :c:type:`ssizeargfunc` | __getitem__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PySequenceMethods.sq_ass_item` | :c:type:`ssizeobjargproc` | __setitem__ | + | | | __delitem__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PySequenceMethods.sq_contains` | :c:type:`objobjproc` | __contains__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PySequenceMethods.sq_inplace_concat` | :c:type:`binaryfunc` | __iadd__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PySequenceMethods.sq_inplace_repeat` | :c:type:`ssizeargfunc` | __imul__ | + +---------------------------------------------------------+-----------------------------------+---------------+ + | | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyBufferProcs.bf_getbuffer` | :c:func:`getbufferproc` | | + +---------------------------------------------------------+-----------------------------------+---------------+ + | :c:member:`~PyBufferProcs.bf_releasebuffer` | :c:func:`releasebufferproc` | | + +---------------------------------------------------------+-----------------------------------+---------------+ .. _slot-typedefs-table: @@ -333,7 +333,7 @@ slot typedefs | :c:type:`allocfunc` | .. line-block:: | :c:type:`PyObject` * | | | | | | | :c:type:`PyTypeObject` * | | -| | Py_ssize_t | | +| | :c:type:`Py_ssize_t` | | +-----------------------------+-----------------------------+----------------------+ | :c:type:`destructor` | void * | void | +-----------------------------+-----------------------------+----------------------+ @@ -405,7 +405,7 @@ slot typedefs +-----------------------------+-----------------------------+----------------------+ | :c:type:`iternextfunc` | :c:type:`PyObject` * | :c:type:`PyObject` * | +-----------------------------+-----------------------------+----------------------+ -| :c:type:`lenfunc` | :c:type:`PyObject` * | Py_ssize_t | +| :c:type:`lenfunc` | :c:type:`PyObject` * | :c:type:`Py_ssize_t` | +-----------------------------+-----------------------------+----------------------+ | :c:type:`getbufferproc` | .. line-block:: | int | | | | | @@ -438,12 +438,12 @@ slot typedefs | :c:type:`ssizeargfunc` | .. line-block:: | :c:type:`PyObject` * | | | | | | | :c:type:`PyObject` * | | -| | Py_ssize_t | | +| | :c:type:`Py_ssize_t` | | +-----------------------------+-----------------------------+----------------------+ | :c:type:`ssizeobjargproc` | .. line-block:: | int | | | | | | | :c:type:`PyObject` * | | -| | Py_ssize_t | | +| | :c:type:`Py_ssize_t` | | +-----------------------------+-----------------------------+----------------------+ | :c:type:`objobjproc` | .. line-block:: | int | | | | | @@ -476,7 +476,7 @@ PyObject Slots -------------- The type object structure extends the :c:type:`PyVarObject` structure. The -:attr:`ob_size` field is used for dynamic types (created by :func:`type_new`, +:attr:`ob_size` field is used for dynamic types (created by :func:`type_new`, usually called from a class statement). Note that :c:data:`PyType_Type` (the metatype) initializes :c:member:`~PyTypeObject.tp_itemsize`, which means that its instances (i.e. type objects) *must* have the :attr:`ob_size` field. @@ -668,6 +668,18 @@ and :c:type:`PyType_Type` effectively act as defaults.) :c:func:`PyObject_GC_Del` if the instance was allocated using :c:func:`PyObject_GC_New` or :c:func:`PyObject_GC_NewVar`. + If the type supports garbage collection (has the :const:`Py_TPFLAGS_HAVE_GC` + flag bit set), the destructor should call :c:func:`PyObject_GC_UnTrack` + before clearing any member fields. + + .. code-block:: c + + static void foo_dealloc(foo_object *self) { + PyObject_GC_UnTrack(self); + Py_CLEAR(self->ref); + Py_TYPE(self)->tp_free((PyObject *)self); + } + Finally, if the type is heap allocated (:const:`Py_TPFLAGS_HEAPTYPE`), the deallocator should decrement the reference count for its type object after calling the type deallocator. In order to avoid dangling pointers, the @@ -715,12 +727,6 @@ and :c:type:`PyType_Type` effectively act as defaults.) When a user sets :attr:`__call__` in Python code, only *tp_call* is updated, likely making it inconsistent with the vectorcall function. - .. note:: - - The semantics of the ``tp_vectorcall_offset`` slot are provisional and - expected to be finalized in Python 3.9. - If you use vectorcall, plan for updating your code for Python 3.9. - .. versionchanged:: 3.8 Before version 3.8, this slot was named ``tp_print``. @@ -1097,8 +1103,7 @@ and :c:type:`PyType_Type` effectively act as defaults.) This is a bitmask of all the bits that pertain to the existence of certain fields in the type object and its extension structures. Currently, it includes - the following bits: :const:`Py_TPFLAGS_HAVE_STACKLESS_EXTENSION`, - :const:`Py_TPFLAGS_HAVE_VERSION_TAG`. + the following bits: :const:`Py_TPFLAGS_HAVE_STACKLESS_EXTENSION`. **Inheritance:** @@ -1178,14 +1183,6 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. versionadded:: 3.9 - - .. data:: Py_TPFLAGS_HAVE_AM_SEND - - This bit is set when the :c:member:`~PyAsyncMethods.am_send` entry is present in the - :c:member:`~PyTypeObject.tp_as_async` slot of type structure. - - .. versionadded:: 3.10 - .. data:: Py_TPFLAGS_IMMUTABLETYPE This bit is set for type objects that are immutable: type attributes cannot be set nor deleted. @@ -1229,7 +1226,7 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. note:: :const:`Py_TPFLAGS_MAPPING` and :const:`Py_TPFLAGS_SEQUENCE` are - mutually exclusive; it is an error enable both flags simultaneously. + mutually exclusive; it is an error to enable both flags simultaneously. **Inheritance:** @@ -1251,7 +1248,7 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. note:: :const:`Py_TPFLAGS_MAPPING` and :const:`Py_TPFLAGS_SEQUENCE` are - mutually exclusive; it is an error enable both flags simultaneously. + mutually exclusive; it is an error to enable both flags simultaneously. **Inheritance:** @@ -1389,6 +1386,12 @@ and :c:type:`PyType_Type` effectively act as defaults.) so that *self* knows the contained object can no longer be used. The :c:func:`Py_CLEAR` macro performs the operations in a safe order. + Note that :c:member:`~PyTypeObject.tp_clear` is not *always* called + before an instance is deallocated. For example, when reference counting + is enough to determine that an object is no longer used, the cyclic garbage + collector is not involved and :c:member:`~PyTypeObject.tp_dealloc` is + called directly. + Because the goal of :c:member:`~PyTypeObject.tp_clear` functions is to break reference cycles, it's not necessary to clear contained objects like Python strings or Python integers, which can't participate in reference cycles. On the other hand, it may @@ -1511,9 +1514,9 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. c:member:: getiterfunc PyTypeObject.tp_iter - An optional pointer to a function that returns an iterator for the object. Its - presence normally signals that the instances of this type are iterable (although - sequences may be iterable without this function). + An optional pointer to a function that returns an :term:`iterator` for the + object. Its presence normally signals that the instances of this type are + :term:`iterable` (although sequences may be iterable without this function). This function has the same signature as :c:func:`PyObject_GetIter`:: @@ -1526,8 +1529,8 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. c:member:: iternextfunc PyTypeObject.tp_iternext - An optional pointer to a function that returns the next item in an iterator. - The signature is:: + An optional pointer to a function that returns the next item in an + :term:`iterator`. The signature is:: PyObject *tp_iternext(PyObject *self); @@ -1990,6 +1993,17 @@ and :c:type:`PyType_Type` effectively act as defaults.) For this field to be taken into account (even through inheritance), you must also set the :const:`Py_TPFLAGS_HAVE_FINALIZE` flags bit. + Also, note that, in a garbage collected Python, + :c:member:`~PyTypeObject.tp_dealloc` may be called from + any Python thread, not just the thread which created the object (if the object + becomes part of a refcount cycle, that cycle might be collected by a garbage + collection on any thread). This is not a problem for Python API calls, since + the thread on which tp_dealloc is called will own the Global Interpreter Lock + (GIL). However, if the object being destroyed in turn destroys objects from some + other C or C++ library, care should be taken to ensure that destroying those + objects on the thread which called tp_dealloc will not violate any assumptions + of the library. + **Inheritance:** This field is inherited by subtypes. @@ -2014,17 +2028,6 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. versionadded:: 3.9 (the field exists since 3.8 but it's only used since 3.9) -Also, note that, in a garbage collected Python, :c:member:`~PyTypeObject.tp_dealloc` may be called from -any Python thread, not just the thread which created the object (if the object -becomes part of a refcount cycle, that cycle might be collected by a garbage -collection on any thread). This is not a problem for Python API calls, since -the thread on which tp_dealloc is called will own the Global Interpreter Lock -(GIL). However, if the object being destroyed in turn destroys objects from some -other C or C++ library, care should be taken to ensure that destroying those -objects on the thread which called tp_dealloc will not violate any assumptions -of the library. - - .. _static-types: Static Types @@ -2419,8 +2422,8 @@ Async Object Structures PyObject *am_await(PyObject *self); - The returned object must be an iterator, i.e. :c:func:`PyIter_Check` must - return ``1`` for it. + The returned object must be an :term:`iterator`, i.e. :c:func:`PyIter_Check` + must return ``1`` for it. This slot may be set to ``NULL`` if an object is not an :term:`awaitable`. @@ -2430,7 +2433,8 @@ Async Object Structures PyObject *am_aiter(PyObject *self); - Must return an :term:`awaitable` object. See :meth:`__anext__` for details. + Must return an :term:`asynchronous iterator` object. + See :meth:`__anext__` for details. This slot may be set to ``NULL`` if an object does not implement asynchronous iteration protocol. @@ -2453,6 +2457,8 @@ Async Object Structures See :c:func:`PyIter_Send` for details. This slot may be set to ``NULL``. + .. versionadded:: 3.10 + .. _slot-typedefs: @@ -2584,7 +2590,7 @@ A basic :ref:`static type `:: PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "mymod.MyObject", .tp_basicsize = sizeof(MyObject), - .tp_doc = "My objects", + .tp_doc = PyDoc_STR("My objects"), .tp_new = myobj_new, .tp_dealloc = (destructor)myobj_dealloc, .tp_repr = (reprfunc)myobj_repr, @@ -2614,7 +2620,7 @@ with a more verbose initializer:: 0, /* tp_setattro */ 0, /* tp_as_buffer */ 0, /* tp_flags */ - "My objects", /* tp_doc */ + PyDoc_STR("My objects"), /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -2647,7 +2653,7 @@ A type that supports weakrefs, instance dicts, and hashing:: PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "mymod.MyObject", .tp_basicsize = sizeof(MyObject), - .tp_doc = "My objects", + .tp_doc = PyDoc_STR("My objects"), .tp_weaklistoffset = offsetof(MyObject, weakreflist), .tp_dictoffset = offsetof(MyObject, inst_dict), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, @@ -2675,7 +2681,7 @@ to create instances (e.g. uses a separate factory func) using .tp_name = "mymod.MyStr", .tp_basicsize = sizeof(MyStr), .tp_base = NULL, // set to &PyUnicode_Type in module init - .tp_doc = "my custom str", + .tp_doc = PyDoc_STR("my custom str"), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, .tp_repr = (reprfunc)myobj_repr, }; diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst index 8d4eef87db9fb6..09ded4c32db07e 100644 --- a/Doc/c-api/unicode.rst +++ b/Doc/c-api/unicode.rst @@ -149,7 +149,7 @@ access internal read-only data of Unicode objects: ``PyUnicode_WCHAR_KIND`` is deprecated. -.. c:function:: int PyUnicode_KIND(PyObject *o) +.. c:function:: unsigned int PyUnicode_KIND(PyObject *o) Return one of the PyUnicode kind constants (see above) that indicate how many bytes per character this Unicode object uses to store its data. *o* has to @@ -268,57 +268,57 @@ are available through these macros which are mapped to C functions depending on the Python configuration. -.. c:function:: int Py_UNICODE_ISSPACE(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISSPACE(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is a whitespace character. -.. c:function:: int Py_UNICODE_ISLOWER(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISLOWER(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is a lowercase character. -.. c:function:: int Py_UNICODE_ISUPPER(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISUPPER(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is an uppercase character. -.. c:function:: int Py_UNICODE_ISTITLE(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISTITLE(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is a titlecase character. -.. c:function:: int Py_UNICODE_ISLINEBREAK(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISLINEBREAK(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is a linebreak character. -.. c:function:: int Py_UNICODE_ISDECIMAL(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISDECIMAL(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is a decimal character. -.. c:function:: int Py_UNICODE_ISDIGIT(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISDIGIT(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is a digit character. -.. c:function:: int Py_UNICODE_ISNUMERIC(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISNUMERIC(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is a numeric character. -.. c:function:: int Py_UNICODE_ISALPHA(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISALPHA(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is an alphabetic character. -.. c:function:: int Py_UNICODE_ISALNUM(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISALNUM(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is an alphanumeric character. -.. c:function:: int Py_UNICODE_ISPRINTABLE(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_ISPRINTABLE(Py_UCS4 ch) Return ``1`` or ``0`` depending on whether *ch* is a printable character. Nonprintable characters are those characters defined in the Unicode character @@ -332,7 +332,7 @@ the Python configuration. These APIs can be used for fast direct character conversions: -.. c:function:: Py_UNICODE Py_UNICODE_TOLOWER(Py_UNICODE ch) +.. c:function:: Py_UCS4 Py_UNICODE_TOLOWER(Py_UCS4 ch) Return the character *ch* converted to lower case. @@ -340,7 +340,7 @@ These APIs can be used for fast direct character conversions: This function uses simple case mappings. -.. c:function:: Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch) +.. c:function:: Py_UCS4 Py_UNICODE_TOUPPER(Py_UCS4 ch) Return the character *ch* converted to upper case. @@ -348,7 +348,7 @@ These APIs can be used for fast direct character conversions: This function uses simple case mappings. -.. c:function:: Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch) +.. c:function:: Py_UCS4 Py_UNICODE_TOTITLE(Py_UCS4 ch) Return the character *ch* converted to title case. @@ -356,19 +356,19 @@ These APIs can be used for fast direct character conversions: This function uses simple case mappings. -.. c:function:: int Py_UNICODE_TODECIMAL(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_TODECIMAL(Py_UCS4 ch) Return the character *ch* converted to a decimal positive integer. Return ``-1`` if this is not possible. This macro does not raise exceptions. -.. c:function:: int Py_UNICODE_TODIGIT(Py_UNICODE ch) +.. c:function:: int Py_UNICODE_TODIGIT(Py_UCS4 ch) Return the character *ch* converted to a single digit integer. Return ``-1`` if this is not possible. This macro does not raise exceptions. -.. c:function:: double Py_UNICODE_TONUMERIC(Py_UNICODE ch) +.. c:function:: double Py_UNICODE_TONUMERIC(Py_UCS4 ch) Return the character *ch* converted to a double. Return ``-1.0`` if this is not possible. This macro does not raise exceptions. @@ -490,11 +490,11 @@ APIs: | :attr:`%llu` | unsigned long long | Equivalent to | | | | ``printf("%llu")``. [1]_ | +-------------------+---------------------+----------------------------------+ - | :attr:`%zd` | Py_ssize_t | Equivalent to | - | | | ``printf("%zd")``. [1]_ | + | :attr:`%zd` | :c:type:`\ | Equivalent to | + | | Py_ssize_t` | ``printf("%zd")``. [1]_ | +-------------------+---------------------+----------------------------------+ - | :attr:`%zi` | Py_ssize_t | Equivalent to | - | | | ``printf("%zi")``. [1]_ | + | :attr:`%zi` | :c:type:`\ | Equivalent to | + | | Py_ssize_t` | ``printf("%zi")``. [1]_ | +-------------------+---------------------+----------------------------------+ | :attr:`%zu` | size_t | Equivalent to | | | | ``printf("%zu")``. [1]_ | @@ -1008,7 +1008,7 @@ Error handling is set by errors which may also be set to ``NULL`` meaning to use the default handling defined for the codec. Default error handling for all built-in codecs is "strict" (:exc:`ValueError` is raised). -The codecs all use a similar interface. Only deviation from the following +The codecs all use a similar interface. Only deviations from the following generic ones are documented for simplicity. @@ -1093,7 +1093,8 @@ These are the UTF-8 codec APIs: This caches the UTF-8 representation of the string in the Unicode object, and subsequent calls will return a pointer to the same buffer. The caller is not - responsible for deallocating the buffer. + responsible for deallocating the buffer. The buffer is deallocated and + pointers to it become invalid when the Unicode object is garbage collected. .. versionadded:: 3.3 @@ -1225,7 +1226,7 @@ These are the UTF-16 codec APIs: ``1``, any byte order mark is copied to the output (where it will result in either a ``\ufeff`` or a ``\ufffe`` character). - After completion, *\*byteorder* is set to the current byte order at the end + After completion, ``*byteorder`` is set to the current byte order at the end of input data. If *byteorder* is ``NULL``, the codec starts in native order mode. @@ -1443,7 +1444,7 @@ Character Map Codecs This codec is special in that it can be used to implement many different codecs (and this is in fact what was done to obtain most of the standard codecs -included in the :mod:`encodings` package). The codec uses mapping to encode and +included in the :mod:`encodings` package). The codec uses mappings to encode and decode characters. The mapping objects provided must support the :meth:`__getitem__` mapping interface; dictionaries and sequences work well. @@ -1605,7 +1606,7 @@ They all return ``NULL`` or ``-1`` if an exception occurs. .. c:function:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend) Split a Unicode string at line breaks, returning a list of Unicode strings. - CRLF is considered to be one line break. If *keepend* is ``0``, the Line break + CRLF is considered to be one line break. If *keepend* is ``0``, the line break characters are not included in the resulting strings. diff --git a/Doc/c-api/veryhigh.rst b/Doc/c-api/veryhigh.rst index 0f760eaa7ad578..5b8735de75e9d0 100644 --- a/Doc/c-api/veryhigh.rst +++ b/Doc/c-api/veryhigh.rst @@ -75,6 +75,8 @@ the same library that the Python runtime is using. :c:func:`PyRun_SimpleFile`. *filename* is decoded from the filesystem encoding (:func:`sys.getfilesystemencoding`). If *filename* is ``NULL``, this function uses ``"???"`` as the filename. + If *closeit* is true, the file is closed before + ``PyRun_SimpleFileExFlags()`` returns. .. c:function:: int PyRun_SimpleString(const char *command) @@ -185,42 +187,6 @@ the same library that the Python runtime is using. :c:func:`PyMem_RawRealloc`, instead of being allocated by :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. - -.. c:function:: struct _node* PyParser_SimpleParseString(const char *str, int start) - - This is a simplified interface to - :c:func:`PyParser_SimpleParseStringFlagsFilename` below, leaving *filename* set - to ``NULL`` and *flags* set to ``0``. - - -.. c:function:: struct _node* PyParser_SimpleParseStringFlags( const char *str, int start, int flags) - - This is a simplified interface to - :c:func:`PyParser_SimpleParseStringFlagsFilename` below, leaving *filename* set - to ``NULL``. - - -.. c:function:: struct _node* PyParser_SimpleParseStringFlagsFilename( const char *str, const char *filename, int start, int flags) - - 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 :term:`filesystem encoding and - error handler`. - - -.. c:function:: struct _node* PyParser_SimpleParseFile(FILE *fp, const char *filename, int start) - - This is a simplified interface to :c:func:`PyParser_SimpleParseFileFlags` below, - leaving *flags* set to ``0``. - - -.. c:function:: struct _node* PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags) - - Similar to :c:func:`PyParser_SimpleParseStringFlagsFilename`, but the Python - source code is read from *fp* instead of an in-memory string. - - .. c:function:: PyObject* PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals) This is a simplified interface to :c:func:`PyRun_StringFlags` below, leaving diff --git a/Doc/conf.py b/Doc/conf.py index cf250981f58752..d81085883d0d48 100644 --- a/Doc/conf.py +++ b/Doc/conf.py @@ -45,7 +45,7 @@ highlight_language = 'python3' # Minimum version of sphinx required -needs_sphinx = '1.8' +needs_sphinx = '3.2' # Ignore any .rst files in the venv/ directory. exclude_patterns = ['venv/*', 'README.rst'] @@ -69,7 +69,8 @@ html_theme_path = ['tools'] html_theme_options = { 'collapsiblesidebar': True, - 'issues_url': 'https://docs.python.org/3/bugs.html', + 'issues_url': '/bugs.html', + 'license_url': '/license.html', 'root_include_title': False # We use the version switcher instead. } @@ -85,7 +86,7 @@ # Custom sidebar templates, filenames relative to this file. html_sidebars = { - # Defaults taken from http://www.sphinx-doc.org/en/stable/config.html#confval-html_sidebars + # Defaults taken from https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_sidebars # Removes the quick search block '**': ['localtoc.html', 'relations.html', 'customsourcelink.html'], 'index': ['indexsidebar.html'], @@ -225,8 +226,9 @@ # Options for extensions # ---------------------- -# Relative filename of the reference count data file. +# Relative filename of the data files refcount_file = 'data/refcounts.dat' +stable_abi_file = 'data/stable_abi.dat' # Sphinx 2 and Sphinx 3 compatibility # ----------------------------------- diff --git a/Doc/copyright.rst b/Doc/copyright.rst index 4191c0bb63a2c1..e64a49328b4723 100644 --- a/Doc/copyright.rst +++ b/Doc/copyright.rst @@ -4,7 +4,7 @@ Copyright Python and this documentation is: -Copyright © 2001-2021 Python Software Foundation. All rights reserved. +Copyright © 2001-2022 Python Software Foundation. All rights reserved. Copyright © 2000 BeOpen.com. All rights reserved. diff --git a/Doc/data/python3.10.abi b/Doc/data/python3.10.abi new file mode 100644 index 00000000000000..b7886ae28f7007 --- /dev/null +++ b/Doc/data/python3.10.abi @@ -0,0 +1,20719 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Doc/data/refcounts.dat b/Doc/data/refcounts.dat index 505f1203dd1bdd..89b64e6c083155 100644 --- a/Doc/data/refcounts.dat +++ b/Doc/data/refcounts.dat @@ -1073,8 +1073,8 @@ PyInterpreterState_New:PyInterpreterState*::: PyIter_Check:int::: PyIter_Check:PyObject*:o:0: -PyAiter_Check:int::: -PyAiter_Check:PyObject*:o:0: +PyAIter_Check:int::: +PyAIter_Check:PyObject*:o:0: PyIter_Next:PyObject*::+1: PyIter_Next:PyObject*:o:0: @@ -1571,6 +1571,21 @@ PyOS_FSPath:PyObject*:path:0: PyObject_ASCII:PyObject*::+1: PyObject_ASCII:PyObject*:o:0: +PyObject_AsCharBuffer:int::: +PyObject_AsCharBuffer:PyObject*:obj:0: +PyObject_AsCharBuffer:const char**:buffer:: +PyObject_AsCharBuffer:Py_ssize_t*:buffer_len:: + +PyObject_AsReadBuffer:int::: +PyObject_AsReadBuffer:PyObject*:obj:0: +PyObject_AsReadBuffer:const void**:buffer:: +PyObject_AsReadBuffer:Py_ssize_t*:buffer_len:: + +PyObject_AsWriteBuffer:int::: +PyObject_AsWriteBuffer:PyObject*:obj:0: +PyObject_AsWriteBuffer:void**:buffer:: +PyObject_AsWriteBuffer:Py_ssize_t*:buffer_len:: + PyObject_Bytes:PyObject*::+1: PyObject_Bytes:PyObject*:o:0: @@ -1606,6 +1621,9 @@ PyObject_CallObject:PyObject*:args:0: PyObject_CheckBuffer:int::: PyObject_CheckBuffer:PyObject*:obj:0: +PyObject_CheckReadBuffer:int::: +PyObject_CheckReadBuffer:PyObject*:o:0: + PyObject_DelAttr:int::: PyObject_DelAttr:PyObject*:o:0: PyObject_DelAttr:PyObject*:attr_name:0: @@ -1682,8 +1700,8 @@ PyObject_GetItem:PyObject*:key:0: PyObject_GetIter:PyObject*::+1: PyObject_GetIter:PyObject*:o:0: -PyObject_GetAiter:PyObject*::+1: -PyObject_GetAiter:PyObject*:o:0: +PyObject_GetAIter:PyObject*::+1: +PyObject_GetAIter:PyObject*:o:0: PyObject_HasAttr:int::: PyObject_HasAttr:PyObject*:o:0: @@ -1791,32 +1809,6 @@ PyObject_TypeCheck:int::: PyObject_TypeCheck:PyObject*:o:0: PyObject_TypeCheck:PyTypeObject*:type:0: -PyParser_SimpleParseFile:struct _node*::: -PyParser_SimpleParseFile:FILE*:fp:: -PyParser_SimpleParseFile:const char*:filename:: -PyParser_SimpleParseFile:int:start:: - -PyParser_SimpleParseFileFlags:struct _node*::: -PyParser_SimpleParseFileFlags:FILE*:fp:: -PyParser_SimpleParseFileFlags:const char*:filename:: -PyParser_SimpleParseFileFlags:int:start:: -PyParser_SimpleParseFileFlags:int:flags:: - -PyParser_SimpleParseString:struct _node*::: -PyParser_SimpleParseString:const char*:str:: -PyParser_SimpleParseString:int:start:: - -PyParser_SimpleParseStringFlags:struct _node*::: -PyParser_SimpleParseStringFlags:const char*:str:: -PyParser_SimpleParseStringFlags:int:start:: -PyParser_SimpleParseStringFlags:int:flags:: - -PyParser_SimpleParseStringFlagsFilename:struct _node*::: -PyParser_SimpleParseStringFlagsFilename:const char*:str:: -PyParser_SimpleParseStringFlagsFilename:const char*:filename:: -PyParser_SimpleParseStringFlagsFilename:int:start:: -PyParser_SimpleParseStringFlagsFilename:int:flags:: - PyRun_AnyFile:int::: PyRun_AnyFile:FILE*:fp:: PyRun_AnyFile:const char*:filename:: diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat index 833228f7fd755d..ea102b989c8649 100644 --- a/Doc/data/stable_abi.dat +++ b/Doc/data/stable_abi.dat @@ -1,953 +1,863 @@ -# Generated by Tools/scripts/stable_abi.py - -METH_CLASS -METH_COEXIST -METH_FASTCALL -METH_METHOD -METH_NOARGS -METH_O -METH_STATIC -METH_VARARGS -PyAiter_Check -PyArg_Parse -PyArg_ParseTuple -PyArg_ParseTupleAndKeywords -PyArg_UnpackTuple -PyArg_VaParse -PyArg_VaParseTupleAndKeywords -PyArg_ValidateKeywordArguments -PyBaseObject_Type -PyBool_FromLong -PyBool_Type -PyByteArrayIter_Type -PyByteArray_AsString -PyByteArray_Concat -PyByteArray_FromObject -PyByteArray_FromStringAndSize -PyByteArray_Resize -PyByteArray_Size -PyByteArray_Type -PyBytesIter_Type -PyBytes_AsString -PyBytes_AsStringAndSize -PyBytes_Concat -PyBytes_ConcatAndDel -PyBytes_DecodeEscape -PyBytes_FromFormat -PyBytes_FromFormatV -PyBytes_FromObject -PyBytes_FromString -PyBytes_FromStringAndSize -PyBytes_Repr -PyBytes_Size -PyBytes_Type -PyCFunction -PyCFunctionWithKeywords -PyCFunction_Call -PyCFunction_GetFlags -PyCFunction_GetFunction -PyCFunction_GetSelf -PyCFunction_New -PyCFunction_NewEx -PyCFunction_Type -PyCMethod_New -PyCallIter_New -PyCallIter_Type -PyCallable_Check -PyCapsule_Destructor -PyCapsule_GetContext -PyCapsule_GetDestructor -PyCapsule_GetName -PyCapsule_GetPointer -PyCapsule_Import -PyCapsule_IsValid -PyCapsule_New -PyCapsule_SetContext -PyCapsule_SetDestructor -PyCapsule_SetName -PyCapsule_SetPointer -PyCapsule_Type -PyClassMethodDescr_Type -PyCodec_BackslashReplaceErrors -PyCodec_Decode -PyCodec_Decoder -PyCodec_Encode -PyCodec_Encoder -PyCodec_IgnoreErrors -PyCodec_IncrementalDecoder -PyCodec_IncrementalEncoder -PyCodec_KnownEncoding -PyCodec_LookupError -PyCodec_NameReplaceErrors -PyCodec_Register -PyCodec_RegisterError -PyCodec_ReplaceErrors -PyCodec_StreamReader -PyCodec_StreamWriter -PyCodec_StrictErrors -PyCodec_Unregister -PyCodec_XMLCharRefReplaceErrors -PyComplex_FromDoubles -PyComplex_ImagAsDouble -PyComplex_RealAsDouble -PyComplex_Type -PyDescr_NewClassMethod -PyDescr_NewGetSet -PyDescr_NewMember -PyDescr_NewMethod -PyDictItems_Type -PyDictIterItem_Type -PyDictIterKey_Type -PyDictIterValue_Type -PyDictKeys_Type -PyDictProxy_New -PyDictProxy_Type -PyDictRevIterItem_Type -PyDictRevIterKey_Type -PyDictRevIterValue_Type -PyDictValues_Type -PyDict_Clear -PyDict_Contains -PyDict_Copy -PyDict_DelItem -PyDict_DelItemString -PyDict_GetItem -PyDict_GetItemString -PyDict_GetItemWithError -PyDict_Items -PyDict_Keys -PyDict_Merge -PyDict_MergeFromSeq2 -PyDict_New -PyDict_Next -PyDict_SetItem -PyDict_SetItemString -PyDict_Size -PyDict_Type -PyDict_Update -PyDict_Values -PyEllipsis_Type -PyEnum_Type -PyErr_BadArgument -PyErr_BadInternalCall -PyErr_CheckSignals -PyErr_Clear -PyErr_Display -PyErr_ExceptionMatches -PyErr_Fetch -PyErr_Format -PyErr_FormatV -PyErr_GetExcInfo -PyErr_GivenExceptionMatches -PyErr_NewException -PyErr_NewExceptionWithDoc -PyErr_NoMemory -PyErr_NormalizeException -PyErr_Occurred -PyErr_Print -PyErr_PrintEx -PyErr_ProgramText -PyErr_ResourceWarning -PyErr_Restore -PyErr_SetExcFromWindowsErr -PyErr_SetExcFromWindowsErrWithFilename -PyErr_SetExcFromWindowsErrWithFilenameObject -PyErr_SetExcFromWindowsErrWithFilenameObjects -PyErr_SetExcInfo -PyErr_SetFromErrno -PyErr_SetFromErrnoWithFilename -PyErr_SetFromErrnoWithFilenameObject -PyErr_SetFromErrnoWithFilenameObjects -PyErr_SetFromWindowsErr -PyErr_SetFromWindowsErrWithFilename -PyErr_SetImportError -PyErr_SetImportErrorSubclass -PyErr_SetInterrupt -PyErr_SetInterruptEx -PyErr_SetNone -PyErr_SetObject -PyErr_SetString -PyErr_SyntaxLocation -PyErr_SyntaxLocationEx -PyErr_WarnEx -PyErr_WarnExplicit -PyErr_WarnFormat -PyErr_WriteUnraisable -PyEval_AcquireLock -PyEval_AcquireThread -PyEval_CallFunction -PyEval_CallMethod -PyEval_CallObjectWithKeywords -PyEval_EvalCode -PyEval_EvalCodeEx -PyEval_EvalFrame -PyEval_EvalFrameEx -PyEval_GetBuiltins -PyEval_GetFrame -PyEval_GetFuncDesc -PyEval_GetFuncName -PyEval_GetGlobals -PyEval_GetLocals -PyEval_InitThreads -PyEval_ReleaseLock -PyEval_ReleaseThread -PyEval_RestoreThread -PyEval_SaveThread -PyEval_ThreadsInitialized -PyExc_ArithmeticError -PyExc_AssertionError -PyExc_AttributeError -PyExc_BaseException -PyExc_BlockingIOError -PyExc_BrokenPipeError -PyExc_BufferError -PyExc_BytesWarning -PyExc_ChildProcessError -PyExc_ConnectionAbortedError -PyExc_ConnectionError -PyExc_ConnectionRefusedError -PyExc_ConnectionResetError -PyExc_DeprecationWarning -PyExc_EOFError -PyExc_EncodingWarning -PyExc_EnvironmentError -PyExc_Exception -PyExc_FileExistsError -PyExc_FileNotFoundError -PyExc_FloatingPointError -PyExc_FutureWarning -PyExc_GeneratorExit -PyExc_IOError -PyExc_ImportError -PyExc_ImportWarning -PyExc_IndentationError -PyExc_IndexError -PyExc_InterruptedError -PyExc_IsADirectoryError -PyExc_KeyError -PyExc_KeyboardInterrupt -PyExc_LookupError -PyExc_MemoryError -PyExc_ModuleNotFoundError -PyExc_NameError -PyExc_NotADirectoryError -PyExc_NotImplementedError -PyExc_OSError -PyExc_OverflowError -PyExc_PendingDeprecationWarning -PyExc_PermissionError -PyExc_ProcessLookupError -PyExc_RecursionError -PyExc_ReferenceError -PyExc_ResourceWarning -PyExc_RuntimeError -PyExc_RuntimeWarning -PyExc_StopAsyncIteration -PyExc_StopIteration -PyExc_SyntaxError -PyExc_SyntaxWarning -PyExc_SystemError -PyExc_SystemExit -PyExc_TabError -PyExc_TimeoutError -PyExc_TypeError -PyExc_UnboundLocalError -PyExc_UnicodeDecodeError -PyExc_UnicodeEncodeError -PyExc_UnicodeError -PyExc_UnicodeTranslateError -PyExc_UnicodeWarning -PyExc_UserWarning -PyExc_ValueError -PyExc_Warning -PyExc_WindowsError -PyExc_ZeroDivisionError -PyExceptionClass_Name -PyException_GetCause -PyException_GetContext -PyException_GetTraceback -PyException_SetCause -PyException_SetContext -PyException_SetTraceback -PyFile_FromFd -PyFile_GetLine -PyFile_WriteObject -PyFile_WriteString -PyFilter_Type -PyFloat_AsDouble -PyFloat_FromDouble -PyFloat_FromString -PyFloat_GetInfo -PyFloat_GetMax -PyFloat_GetMin -PyFloat_Type -PyFrame_GetCode -PyFrame_GetLineNumber -PyFrozenSet_New -PyFrozenSet_Type -PyGC_Collect -PyGC_Disable -PyGC_Enable -PyGC_IsEnabled -PyGILState_Ensure -PyGILState_GetThisThreadState -PyGILState_Release -PyGILState_STATE -PyGetSetDef -PyGetSetDescr_Type -PyImport_AddModule -PyImport_AddModuleObject -PyImport_AppendInittab -PyImport_ExecCodeModule -PyImport_ExecCodeModuleEx -PyImport_ExecCodeModuleObject -PyImport_ExecCodeModuleWithPathnames -PyImport_GetImporter -PyImport_GetMagicNumber -PyImport_GetMagicTag -PyImport_GetModule -PyImport_GetModuleDict -PyImport_Import -PyImport_ImportFrozenModule -PyImport_ImportFrozenModuleObject -PyImport_ImportModule -PyImport_ImportModuleLevel -PyImport_ImportModuleLevelObject -PyImport_ImportModuleNoBlock -PyImport_ReloadModule -PyIndex_Check -PyInterpreterState -PyInterpreterState_Clear -PyInterpreterState_Delete -PyInterpreterState_Get -PyInterpreterState_GetDict -PyInterpreterState_GetID -PyInterpreterState_New -PyIter_Check -PyIter_Next -PyIter_Send -PyListIter_Type -PyListRevIter_Type -PyList_Append -PyList_AsTuple -PyList_GetItem -PyList_GetSlice -PyList_Insert -PyList_New -PyList_Reverse -PyList_SetItem -PyList_SetSlice -PyList_Size -PyList_Sort -PyList_Type -PyLongObject -PyLongRangeIter_Type -PyLong_AsDouble -PyLong_AsLong -PyLong_AsLongAndOverflow -PyLong_AsLongLong -PyLong_AsLongLongAndOverflow -PyLong_AsSize_t -PyLong_AsSsize_t -PyLong_AsUnsignedLong -PyLong_AsUnsignedLongLong -PyLong_AsUnsignedLongLongMask -PyLong_AsUnsignedLongMask -PyLong_AsVoidPtr -PyLong_FromDouble -PyLong_FromLong -PyLong_FromLongLong -PyLong_FromSize_t -PyLong_FromSsize_t -PyLong_FromString -PyLong_FromUnsignedLong -PyLong_FromUnsignedLongLong -PyLong_FromVoidPtr -PyLong_GetInfo -PyLong_Type -PyMap_Type -PyMapping_Check -PyMapping_GetItemString -PyMapping_HasKey -PyMapping_HasKeyString -PyMapping_Items -PyMapping_Keys -PyMapping_Length -PyMapping_SetItemString -PyMapping_Size -PyMapping_Values -PyMem_Calloc -PyMem_Free -PyMem_Malloc -PyMem_Realloc -PyMemberDef -PyMemberDescr_Type -PyMemoryView_FromMemory -PyMemoryView_FromObject -PyMemoryView_GetContiguous -PyMemoryView_Type -PyMethodDef -PyMethodDescr_Type -PyModuleDef -PyModuleDef_Base -PyModuleDef_Init -PyModuleDef_Type -PyModule_AddFunctions -PyModule_AddIntConstant -PyModule_AddObject -PyModule_AddObjectRef -PyModule_AddStringConstant -PyModule_AddType -PyModule_Create2 -PyModule_ExecDef -PyModule_FromDefAndSpec2 -PyModule_GetDef -PyModule_GetDict -PyModule_GetFilename -PyModule_GetFilenameObject -PyModule_GetName -PyModule_GetNameObject -PyModule_GetState -PyModule_New -PyModule_NewObject -PyModule_SetDocString -PyModule_Type -PyNumber_Absolute -PyNumber_Add -PyNumber_And -PyNumber_AsSsize_t -PyNumber_Check -PyNumber_Divmod -PyNumber_Float -PyNumber_FloorDivide -PyNumber_InPlaceAdd -PyNumber_InPlaceAnd -PyNumber_InPlaceFloorDivide -PyNumber_InPlaceLshift -PyNumber_InPlaceMatrixMultiply -PyNumber_InPlaceMultiply -PyNumber_InPlaceOr -PyNumber_InPlacePower -PyNumber_InPlaceRemainder -PyNumber_InPlaceRshift -PyNumber_InPlaceSubtract -PyNumber_InPlaceTrueDivide -PyNumber_InPlaceXor -PyNumber_Index -PyNumber_Invert -PyNumber_Long -PyNumber_Lshift -PyNumber_MatrixMultiply -PyNumber_Multiply -PyNumber_Negative -PyNumber_Or -PyNumber_Positive -PyNumber_Power -PyNumber_Remainder -PyNumber_Rshift -PyNumber_Subtract -PyNumber_ToBase -PyNumber_TrueDivide -PyNumber_Xor -PyOS_AfterFork -PyOS_AfterFork_Child -PyOS_AfterFork_Parent -PyOS_BeforeFork -PyOS_CheckStack -PyOS_FSPath -PyOS_InputHook -PyOS_InterruptOccurred -PyOS_double_to_string -PyOS_getsig -PyOS_mystricmp -PyOS_mystrnicmp -PyOS_setsig -PyOS_sighandler_t -PyOS_snprintf -PyOS_string_to_double -PyOS_strtol -PyOS_strtoul -PyOS_vsnprintf -PyObject -PyObject_ASCII -PyObject_AsFileDescriptor -PyObject_Bytes -PyObject_Call -PyObject_CallFunction -PyObject_CallFunctionObjArgs -PyObject_CallMethod -PyObject_CallMethodObjArgs -PyObject_CallNoArgs -PyObject_CallObject -PyObject_Calloc -PyObject_ClearWeakRefs -PyObject_DelItem -PyObject_DelItemString -PyObject_Dir -PyObject_Format -PyObject_Free -PyObject_GC_Del -PyObject_GC_IsFinalized -PyObject_GC_IsTracked -PyObject_GC_Track -PyObject_GC_UnTrack -PyObject_GenericGetAttr -PyObject_GenericGetDict -PyObject_GenericSetAttr -PyObject_GenericSetDict -PyObject_GetAiter -PyObject_GetAttr -PyObject_GetAttrString -PyObject_GetItem -PyObject_GetIter -PyObject_HasAttr -PyObject_HasAttrString -PyObject_Hash -PyObject_HashNotImplemented -PyObject_Init -PyObject_InitVar -PyObject_IsInstance -PyObject_IsSubclass -PyObject_IsTrue -PyObject_Length -PyObject_Malloc -PyObject_Not -PyObject_Realloc -PyObject_Repr -PyObject_RichCompare -PyObject_RichCompareBool -PyObject_SelfIter -PyObject_SetAttr -PyObject_SetAttrString -PyObject_SetItem -PyObject_Size -PyObject_Str -PyObject_Type -PyProperty_Type -PyRangeIter_Type -PyRange_Type -PyReversed_Type -PySeqIter_New -PySeqIter_Type -PySequence_Check -PySequence_Concat -PySequence_Contains -PySequence_Count -PySequence_DelItem -PySequence_DelSlice -PySequence_Fast -PySequence_GetItem -PySequence_GetSlice -PySequence_In -PySequence_InPlaceConcat -PySequence_InPlaceRepeat -PySequence_Index -PySequence_Length -PySequence_List -PySequence_Repeat -PySequence_SetItem -PySequence_SetSlice -PySequence_Size -PySequence_Tuple -PySetIter_Type -PySet_Add -PySet_Clear -PySet_Contains -PySet_Discard -PySet_New -PySet_Pop -PySet_Size -PySet_Type -PySlice_AdjustIndices -PySlice_GetIndices -PySlice_GetIndicesEx -PySlice_New -PySlice_Type -PySlice_Unpack -PyState_AddModule -PyState_FindModule -PyState_RemoveModule -PyStructSequence_Desc -PyStructSequence_Field -PyStructSequence_GetItem -PyStructSequence_New -PyStructSequence_NewType -PyStructSequence_SetItem -PySuper_Type -PySys_AddWarnOption -PySys_AddWarnOptionUnicode -PySys_AddXOption -PySys_FormatStderr -PySys_FormatStdout -PySys_GetObject -PySys_GetXOptions -PySys_HasWarnOptions -PySys_ResetWarnOptions -PySys_SetArgv -PySys_SetArgvEx -PySys_SetObject -PySys_SetPath -PySys_WriteStderr -PySys_WriteStdout -PyThreadState -PyThreadState_Clear -PyThreadState_Delete -PyThreadState_Get -PyThreadState_GetDict -PyThreadState_GetFrame -PyThreadState_GetID -PyThreadState_GetInterpreter -PyThreadState_New -PyThreadState_SetAsyncExc -PyThreadState_Swap -PyThread_GetInfo -PyThread_ReInitTLS -PyThread_acquire_lock -PyThread_acquire_lock_timed -PyThread_allocate_lock -PyThread_create_key -PyThread_delete_key -PyThread_delete_key_value -PyThread_exit_thread -PyThread_free_lock -PyThread_get_key_value -PyThread_get_stacksize -PyThread_get_thread_ident -PyThread_get_thread_native_id -PyThread_init_thread -PyThread_release_lock -PyThread_set_key_value -PyThread_set_stacksize -PyThread_start_new_thread -PyThread_tss_alloc -PyThread_tss_create -PyThread_tss_delete -PyThread_tss_free -PyThread_tss_get -PyThread_tss_is_created -PyThread_tss_set -PyTraceBack_Here -PyTraceBack_Print -PyTraceBack_Type -PyTupleIter_Type -PyTuple_GetItem -PyTuple_GetSlice -PyTuple_New -PyTuple_Pack -PyTuple_SetItem -PyTuple_Size -PyTuple_Type -PyTypeObject -PyType_ClearCache -PyType_FromModuleAndSpec -PyType_FromSpec -PyType_FromSpecWithBases -PyType_GenericAlloc -PyType_GenericNew -PyType_GetFlags -PyType_GetModule -PyType_GetModuleState -PyType_GetSlot -PyType_IsSubtype -PyType_Modified -PyType_Ready -PyType_Slot -PyType_Spec -PyType_Type -PyUnicodeDecodeError_Create -PyUnicodeDecodeError_GetEncoding -PyUnicodeDecodeError_GetEnd -PyUnicodeDecodeError_GetObject -PyUnicodeDecodeError_GetReason -PyUnicodeDecodeError_GetStart -PyUnicodeDecodeError_SetEnd -PyUnicodeDecodeError_SetReason -PyUnicodeDecodeError_SetStart -PyUnicodeEncodeError_GetEncoding -PyUnicodeEncodeError_GetEnd -PyUnicodeEncodeError_GetObject -PyUnicodeEncodeError_GetReason -PyUnicodeEncodeError_GetStart -PyUnicodeEncodeError_SetEnd -PyUnicodeEncodeError_SetReason -PyUnicodeEncodeError_SetStart -PyUnicodeIter_Type -PyUnicodeTranslateError_GetEnd -PyUnicodeTranslateError_GetObject -PyUnicodeTranslateError_GetReason -PyUnicodeTranslateError_GetStart -PyUnicodeTranslateError_SetEnd -PyUnicodeTranslateError_SetReason -PyUnicodeTranslateError_SetStart -PyUnicode_Append -PyUnicode_AppendAndDel -PyUnicode_AsASCIIString -PyUnicode_AsCharmapString -PyUnicode_AsDecodedObject -PyUnicode_AsDecodedUnicode -PyUnicode_AsEncodedObject -PyUnicode_AsEncodedString -PyUnicode_AsEncodedUnicode -PyUnicode_AsLatin1String -PyUnicode_AsMBCSString -PyUnicode_AsRawUnicodeEscapeString -PyUnicode_AsUCS4 -PyUnicode_AsUCS4Copy -PyUnicode_AsUTF16String -PyUnicode_AsUTF32String -PyUnicode_AsUTF8AndSize -PyUnicode_AsUTF8String -PyUnicode_AsUnicodeEscapeString -PyUnicode_AsWideChar -PyUnicode_AsWideCharString -PyUnicode_BuildEncodingMap -PyUnicode_Compare -PyUnicode_CompareWithASCIIString -PyUnicode_Concat -PyUnicode_Contains -PyUnicode_Count -PyUnicode_Decode -PyUnicode_DecodeASCII -PyUnicode_DecodeCharmap -PyUnicode_DecodeCodePageStateful -PyUnicode_DecodeFSDefault -PyUnicode_DecodeFSDefaultAndSize -PyUnicode_DecodeLatin1 -PyUnicode_DecodeLocale -PyUnicode_DecodeLocaleAndSize -PyUnicode_DecodeMBCS -PyUnicode_DecodeMBCSStateful -PyUnicode_DecodeRawUnicodeEscape -PyUnicode_DecodeUTF16 -PyUnicode_DecodeUTF16Stateful -PyUnicode_DecodeUTF32 -PyUnicode_DecodeUTF32Stateful -PyUnicode_DecodeUTF7 -PyUnicode_DecodeUTF7Stateful -PyUnicode_DecodeUTF8 -PyUnicode_DecodeUTF8Stateful -PyUnicode_DecodeUnicodeEscape -PyUnicode_EncodeCodePage -PyUnicode_EncodeFSDefault -PyUnicode_EncodeLocale -PyUnicode_FSConverter -PyUnicode_FSDecoder -PyUnicode_Find -PyUnicode_FindChar -PyUnicode_Format -PyUnicode_FromEncodedObject -PyUnicode_FromFormat -PyUnicode_FromFormatV -PyUnicode_FromObject -PyUnicode_FromOrdinal -PyUnicode_FromString -PyUnicode_FromStringAndSize -PyUnicode_FromWideChar -PyUnicode_GetDefaultEncoding -PyUnicode_GetLength -PyUnicode_GetSize -PyUnicode_InternFromString -PyUnicode_InternImmortal -PyUnicode_InternInPlace -PyUnicode_IsIdentifier -PyUnicode_Join -PyUnicode_Partition -PyUnicode_RPartition -PyUnicode_RSplit -PyUnicode_ReadChar -PyUnicode_Replace -PyUnicode_Resize -PyUnicode_RichCompare -PyUnicode_Split -PyUnicode_Splitlines -PyUnicode_Substring -PyUnicode_Tailmatch -PyUnicode_Translate -PyUnicode_Type -PyUnicode_WriteChar -PyVarObject -PyWeakReference -PyWeakref_GetObject -PyWeakref_NewProxy -PyWeakref_NewRef -PyWrapperDescr_Type -PyWrapper_New -PyZip_Type -Py_AddPendingCall -Py_AtExit -Py_BEGIN_ALLOW_THREADS -Py_BLOCK_THREADS -Py_BuildValue -Py_BytesMain -Py_CompileString -Py_DecRef -Py_DecodeLocale -Py_END_ALLOW_THREADS -Py_EncodeLocale -Py_EndInterpreter -Py_EnterRecursiveCall -Py_Exit -Py_FatalError -Py_FileSystemDefaultEncodeErrors -Py_FileSystemDefaultEncoding -Py_Finalize -Py_FinalizeEx -Py_FrozenMain -Py_GenericAlias -Py_GenericAliasType -Py_GetBuildInfo -Py_GetCompiler -Py_GetCopyright -Py_GetExecPrefix -Py_GetPath -Py_GetPlatform -Py_GetPrefix -Py_GetProgramFullPath -Py_GetProgramName -Py_GetPythonHome -Py_GetRecursionLimit -Py_GetVersion -Py_HasFileSystemDefaultEncoding -Py_IncRef -Py_Initialize -Py_InitializeEx -Py_Is -Py_IsFalse -Py_IsInitialized -Py_IsNone -Py_IsTrue -Py_LeaveRecursiveCall -Py_Main -Py_MakePendingCalls -Py_NewInterpreter -Py_NewRef -Py_ReprEnter -Py_ReprLeave -Py_SetPath -Py_SetProgramName -Py_SetPythonHome -Py_SetRecursionLimit -Py_TPFLAGS_BASETYPE -Py_TPFLAGS_DEFAULT -Py_TPFLAGS_HAVE_GC -Py_TPFLAGS_METHOD_DESCRIPTOR -Py_UCS4 -Py_UNBLOCK_THREADS -Py_UTF8Mode -Py_VaBuildValue -Py_XNewRef -Py_am_aiter -Py_am_anext -Py_am_await -Py_am_send -Py_intptr_t -Py_mp_ass_subscript -Py_mp_length -Py_mp_subscript -Py_nb_absolute -Py_nb_add -Py_nb_and -Py_nb_bool -Py_nb_divmod -Py_nb_float -Py_nb_floor_divide -Py_nb_index -Py_nb_inplace_add -Py_nb_inplace_and -Py_nb_inplace_floor_divide -Py_nb_inplace_lshift -Py_nb_inplace_matrix_multiply -Py_nb_inplace_multiply -Py_nb_inplace_or -Py_nb_inplace_power -Py_nb_inplace_remainder -Py_nb_inplace_rshift -Py_nb_inplace_subtract -Py_nb_inplace_true_divide -Py_nb_inplace_xor -Py_nb_int -Py_nb_invert -Py_nb_lshift -Py_nb_matrix_multiply -Py_nb_multiply -Py_nb_negative -Py_nb_or -Py_nb_positive -Py_nb_power -Py_nb_remainder -Py_nb_rshift -Py_nb_subtract -Py_nb_true_divide -Py_nb_xor -Py_sq_ass_item -Py_sq_concat -Py_sq_contains -Py_sq_inplace_concat -Py_sq_inplace_repeat -Py_sq_item -Py_sq_length -Py_sq_repeat -Py_ssize_t -Py_tp_alloc -Py_tp_base -Py_tp_bases -Py_tp_call -Py_tp_clear -Py_tp_dealloc -Py_tp_del -Py_tp_descr_get -Py_tp_descr_set -Py_tp_doc -Py_tp_finalize -Py_tp_free -Py_tp_getattr -Py_tp_getattro -Py_tp_getset -Py_tp_hash -Py_tp_init -Py_tp_is_gc -Py_tp_iter -Py_tp_iternext -Py_tp_members -Py_tp_methods -Py_tp_new -Py_tp_repr -Py_tp_richcompare -Py_tp_setattr -Py_tp_setattro -Py_tp_str -Py_tp_traverse -Py_uintptr_t -_frame -_node -allocfunc -binaryfunc -descrgetfunc -descrsetfunc -destructor -getattrfunc -getattrofunc -getiterfunc -getter -hashfunc -initproc -inquiry -iternextfunc -lenfunc -newfunc -objobjargproc -objobjproc -reprfunc -richcmpfunc -setattrfunc -setattrofunc -setter -ssizeargfunc -ssizeobjargproc -ssizessizeargfunc -ssizessizeobjargproc -symtable -ternaryfunc -traverseproc -unaryfunc -visitproc +role,name,added,ifdef_note +function,PyAIter_Check,3.10, +function,PyArg_Parse,3.2, +function,PyArg_ParseTuple,3.2, +function,PyArg_ParseTupleAndKeywords,3.2, +function,PyArg_UnpackTuple,3.2, +function,PyArg_VaParse,3.2, +function,PyArg_VaParseTupleAndKeywords,3.2, +function,PyArg_ValidateKeywordArguments,3.2, +var,PyBaseObject_Type,3.2, +function,PyBool_FromLong,3.2, +var,PyBool_Type,3.2, +var,PyByteArrayIter_Type,3.2, +function,PyByteArray_AsString,3.2, +function,PyByteArray_Concat,3.2, +function,PyByteArray_FromObject,3.2, +function,PyByteArray_FromStringAndSize,3.2, +function,PyByteArray_Resize,3.2, +function,PyByteArray_Size,3.2, +var,PyByteArray_Type,3.2, +var,PyBytesIter_Type,3.2, +function,PyBytes_AsString,3.2, +function,PyBytes_AsStringAndSize,3.2, +function,PyBytes_Concat,3.2, +function,PyBytes_ConcatAndDel,3.2, +function,PyBytes_DecodeEscape,3.2, +function,PyBytes_FromFormat,3.2, +function,PyBytes_FromFormatV,3.2, +function,PyBytes_FromObject,3.2, +function,PyBytes_FromString,3.2, +function,PyBytes_FromStringAndSize,3.2, +function,PyBytes_Repr,3.2, +function,PyBytes_Size,3.2, +var,PyBytes_Type,3.2, +type,PyCFunction,3.2, +type,PyCFunctionWithKeywords,3.2, +function,PyCFunction_Call,3.2, +function,PyCFunction_GetFlags,3.2, +function,PyCFunction_GetFunction,3.2, +function,PyCFunction_GetSelf,3.2, +function,PyCFunction_New,3.4, +function,PyCFunction_NewEx,3.2, +var,PyCFunction_Type,3.2, +function,PyCMethod_New,3.9, +function,PyCallIter_New,3.2, +var,PyCallIter_Type,3.2, +function,PyCallable_Check,3.2, +type,PyCapsule_Destructor,3.2, +function,PyCapsule_GetContext,3.2, +function,PyCapsule_GetDestructor,3.2, +function,PyCapsule_GetName,3.2, +function,PyCapsule_GetPointer,3.2, +function,PyCapsule_Import,3.2, +function,PyCapsule_IsValid,3.2, +function,PyCapsule_New,3.2, +function,PyCapsule_SetContext,3.2, +function,PyCapsule_SetDestructor,3.2, +function,PyCapsule_SetName,3.2, +function,PyCapsule_SetPointer,3.2, +var,PyCapsule_Type,3.2, +var,PyClassMethodDescr_Type,3.2, +function,PyCodec_BackslashReplaceErrors,3.2, +function,PyCodec_Decode,3.2, +function,PyCodec_Decoder,3.2, +function,PyCodec_Encode,3.2, +function,PyCodec_Encoder,3.2, +function,PyCodec_IgnoreErrors,3.2, +function,PyCodec_IncrementalDecoder,3.2, +function,PyCodec_IncrementalEncoder,3.2, +function,PyCodec_KnownEncoding,3.2, +function,PyCodec_LookupError,3.2, +function,PyCodec_NameReplaceErrors,3.7, +function,PyCodec_Register,3.2, +function,PyCodec_RegisterError,3.2, +function,PyCodec_ReplaceErrors,3.2, +function,PyCodec_StreamReader,3.2, +function,PyCodec_StreamWriter,3.2, +function,PyCodec_StrictErrors,3.2, +function,PyCodec_Unregister,3.10, +function,PyCodec_XMLCharRefReplaceErrors,3.2, +function,PyComplex_FromDoubles,3.2, +function,PyComplex_ImagAsDouble,3.2, +function,PyComplex_RealAsDouble,3.2, +var,PyComplex_Type,3.2, +function,PyDescr_NewClassMethod,3.2, +function,PyDescr_NewGetSet,3.2, +function,PyDescr_NewMember,3.2, +function,PyDescr_NewMethod,3.2, +var,PyDictItems_Type,3.2, +var,PyDictIterItem_Type,3.2, +var,PyDictIterKey_Type,3.2, +var,PyDictIterValue_Type,3.2, +var,PyDictKeys_Type,3.2, +function,PyDictProxy_New,3.2, +var,PyDictProxy_Type,3.2, +var,PyDictRevIterItem_Type,3.8, +var,PyDictRevIterKey_Type,3.8, +var,PyDictRevIterValue_Type,3.8, +var,PyDictValues_Type,3.2, +function,PyDict_Clear,3.2, +function,PyDict_Contains,3.2, +function,PyDict_Copy,3.2, +function,PyDict_DelItem,3.2, +function,PyDict_DelItemString,3.2, +function,PyDict_GetItem,3.2, +function,PyDict_GetItemString,3.2, +function,PyDict_GetItemWithError,3.2, +function,PyDict_Items,3.2, +function,PyDict_Keys,3.2, +function,PyDict_Merge,3.2, +function,PyDict_MergeFromSeq2,3.2, +function,PyDict_New,3.2, +function,PyDict_Next,3.2, +function,PyDict_SetItem,3.2, +function,PyDict_SetItemString,3.2, +function,PyDict_Size,3.2, +var,PyDict_Type,3.2, +function,PyDict_Update,3.2, +function,PyDict_Values,3.2, +var,PyEllipsis_Type,3.2, +var,PyEnum_Type,3.2, +function,PyErr_BadArgument,3.2, +function,PyErr_BadInternalCall,3.2, +function,PyErr_CheckSignals,3.2, +function,PyErr_Clear,3.2, +function,PyErr_Display,3.2, +function,PyErr_ExceptionMatches,3.2, +function,PyErr_Fetch,3.2, +function,PyErr_Format,3.2, +function,PyErr_FormatV,3.5, +function,PyErr_GetExcInfo,3.7, +function,PyErr_GivenExceptionMatches,3.2, +function,PyErr_NewException,3.2, +function,PyErr_NewExceptionWithDoc,3.2, +function,PyErr_NoMemory,3.2, +function,PyErr_NormalizeException,3.2, +function,PyErr_Occurred,3.2, +function,PyErr_Print,3.2, +function,PyErr_PrintEx,3.2, +function,PyErr_ProgramText,3.2, +function,PyErr_ResourceWarning,3.6, +function,PyErr_Restore,3.2, +function,PyErr_SetExcFromWindowsErr,3.7,on Windows +function,PyErr_SetExcFromWindowsErrWithFilename,3.7,on Windows +function,PyErr_SetExcFromWindowsErrWithFilenameObject,3.7,on Windows +function,PyErr_SetExcFromWindowsErrWithFilenameObjects,3.7,on Windows +function,PyErr_SetExcInfo,3.7, +function,PyErr_SetFromErrno,3.2, +function,PyErr_SetFromErrnoWithFilename,3.2, +function,PyErr_SetFromErrnoWithFilenameObject,3.2, +function,PyErr_SetFromErrnoWithFilenameObjects,3.7, +function,PyErr_SetFromWindowsErr,3.7,on Windows +function,PyErr_SetFromWindowsErrWithFilename,3.7,on Windows +function,PyErr_SetImportError,3.7, +function,PyErr_SetImportErrorSubclass,3.6, +function,PyErr_SetInterrupt,3.2, +function,PyErr_SetInterruptEx,3.10, +function,PyErr_SetNone,3.2, +function,PyErr_SetObject,3.2, +function,PyErr_SetString,3.2, +function,PyErr_SyntaxLocation,3.2, +function,PyErr_SyntaxLocationEx,3.7, +function,PyErr_WarnEx,3.2, +function,PyErr_WarnExplicit,3.2, +function,PyErr_WarnFormat,3.2, +function,PyErr_WriteUnraisable,3.2, +function,PyEval_AcquireLock,3.2, +function,PyEval_AcquireThread,3.2, +function,PyEval_CallFunction,3.2, +function,PyEval_CallMethod,3.2, +function,PyEval_CallObjectWithKeywords,3.2, +function,PyEval_EvalCode,3.2, +function,PyEval_EvalCodeEx,3.2, +function,PyEval_EvalFrame,3.2, +function,PyEval_EvalFrameEx,3.2, +function,PyEval_GetBuiltins,3.2, +function,PyEval_GetFrame,3.2, +function,PyEval_GetFuncDesc,3.2, +function,PyEval_GetFuncName,3.2, +function,PyEval_GetGlobals,3.2, +function,PyEval_GetLocals,3.2, +function,PyEval_InitThreads,3.2, +function,PyEval_ReleaseLock,3.2, +function,PyEval_ReleaseThread,3.2, +function,PyEval_RestoreThread,3.2, +function,PyEval_SaveThread,3.2, +function,PyEval_ThreadsInitialized,3.2, +var,PyExc_ArithmeticError,3.2, +var,PyExc_AssertionError,3.2, +var,PyExc_AttributeError,3.2, +var,PyExc_BaseException,3.2, +var,PyExc_BlockingIOError,3.7, +var,PyExc_BrokenPipeError,3.7, +var,PyExc_BufferError,3.2, +var,PyExc_BytesWarning,3.2, +var,PyExc_ChildProcessError,3.7, +var,PyExc_ConnectionAbortedError,3.7, +var,PyExc_ConnectionError,3.7, +var,PyExc_ConnectionRefusedError,3.7, +var,PyExc_ConnectionResetError,3.7, +var,PyExc_DeprecationWarning,3.2, +var,PyExc_EOFError,3.2, +var,PyExc_EncodingWarning,3.10, +var,PyExc_EnvironmentError,3.2, +var,PyExc_Exception,3.2, +var,PyExc_FileExistsError,3.7, +var,PyExc_FileNotFoundError,3.7, +var,PyExc_FloatingPointError,3.2, +var,PyExc_FutureWarning,3.2, +var,PyExc_GeneratorExit,3.2, +var,PyExc_IOError,3.2, +var,PyExc_ImportError,3.2, +var,PyExc_ImportWarning,3.2, +var,PyExc_IndentationError,3.2, +var,PyExc_IndexError,3.2, +var,PyExc_InterruptedError,3.7, +var,PyExc_IsADirectoryError,3.7, +var,PyExc_KeyError,3.2, +var,PyExc_KeyboardInterrupt,3.2, +var,PyExc_LookupError,3.2, +var,PyExc_MemoryError,3.2, +var,PyExc_ModuleNotFoundError,3.6, +var,PyExc_NameError,3.2, +var,PyExc_NotADirectoryError,3.7, +var,PyExc_NotImplementedError,3.2, +var,PyExc_OSError,3.2, +var,PyExc_OverflowError,3.2, +var,PyExc_PendingDeprecationWarning,3.2, +var,PyExc_PermissionError,3.7, +var,PyExc_ProcessLookupError,3.7, +var,PyExc_RecursionError,3.7, +var,PyExc_ReferenceError,3.2, +var,PyExc_ResourceWarning,3.7, +var,PyExc_RuntimeError,3.2, +var,PyExc_RuntimeWarning,3.2, +var,PyExc_StopAsyncIteration,3.7, +var,PyExc_StopIteration,3.2, +var,PyExc_SyntaxError,3.2, +var,PyExc_SyntaxWarning,3.2, +var,PyExc_SystemError,3.2, +var,PyExc_SystemExit,3.2, +var,PyExc_TabError,3.2, +var,PyExc_TimeoutError,3.7, +var,PyExc_TypeError,3.2, +var,PyExc_UnboundLocalError,3.2, +var,PyExc_UnicodeDecodeError,3.2, +var,PyExc_UnicodeEncodeError,3.2, +var,PyExc_UnicodeError,3.2, +var,PyExc_UnicodeTranslateError,3.2, +var,PyExc_UnicodeWarning,3.2, +var,PyExc_UserWarning,3.2, +var,PyExc_ValueError,3.2, +var,PyExc_Warning,3.2, +var,PyExc_WindowsError,3.7,on Windows +var,PyExc_ZeroDivisionError,3.2, +function,PyExceptionClass_Name,3.8, +function,PyException_GetCause,3.2, +function,PyException_GetContext,3.2, +function,PyException_GetTraceback,3.2, +function,PyException_SetCause,3.2, +function,PyException_SetContext,3.2, +function,PyException_SetTraceback,3.2, +function,PyFile_FromFd,3.2, +function,PyFile_GetLine,3.2, +function,PyFile_WriteObject,3.2, +function,PyFile_WriteString,3.2, +var,PyFilter_Type,3.2, +function,PyFloat_AsDouble,3.2, +function,PyFloat_FromDouble,3.2, +function,PyFloat_FromString,3.2, +function,PyFloat_GetInfo,3.2, +function,PyFloat_GetMax,3.2, +function,PyFloat_GetMin,3.2, +var,PyFloat_Type,3.2, +type,PyFrameObject,3.2, +function,PyFrame_GetCode,3.10, +function,PyFrame_GetLineNumber,3.10, +function,PyFrozenSet_New,3.2, +var,PyFrozenSet_Type,3.2, +function,PyGC_Collect,3.2, +function,PyGC_Disable,3.10, +function,PyGC_Enable,3.10, +function,PyGC_IsEnabled,3.10, +function,PyGILState_Ensure,3.2, +function,PyGILState_GetThisThreadState,3.2, +function,PyGILState_Release,3.2, +type,PyGILState_STATE,3.2, +type,PyGetSetDef,3.2, +var,PyGetSetDescr_Type,3.2, +function,PyImport_AddModule,3.2, +function,PyImport_AddModuleObject,3.7, +function,PyImport_AppendInittab,3.2, +function,PyImport_ExecCodeModule,3.2, +function,PyImport_ExecCodeModuleEx,3.2, +function,PyImport_ExecCodeModuleObject,3.7, +function,PyImport_ExecCodeModuleWithPathnames,3.2, +function,PyImport_GetImporter,3.2, +function,PyImport_GetMagicNumber,3.2, +function,PyImport_GetMagicTag,3.2, +function,PyImport_GetModule,3.8, +function,PyImport_GetModuleDict,3.2, +function,PyImport_Import,3.2, +function,PyImport_ImportFrozenModule,3.2, +function,PyImport_ImportFrozenModuleObject,3.7, +function,PyImport_ImportModule,3.2, +function,PyImport_ImportModuleLevel,3.2, +function,PyImport_ImportModuleLevelObject,3.7, +function,PyImport_ImportModuleNoBlock,3.2, +function,PyImport_ReloadModule,3.2, +function,PyIndex_Check,3.8, +type,PyInterpreterState,3.2, +function,PyInterpreterState_Clear,3.2, +function,PyInterpreterState_Delete,3.2, +function,PyInterpreterState_Get,3.9, +function,PyInterpreterState_GetDict,3.8, +function,PyInterpreterState_GetID,3.7, +function,PyInterpreterState_New,3.2, +function,PyIter_Check,3.8, +function,PyIter_Next,3.2, +function,PyIter_Send,3.10, +var,PyListIter_Type,3.2, +var,PyListRevIter_Type,3.2, +function,PyList_Append,3.2, +function,PyList_AsTuple,3.2, +function,PyList_GetItem,3.2, +function,PyList_GetSlice,3.2, +function,PyList_Insert,3.2, +function,PyList_New,3.2, +function,PyList_Reverse,3.2, +function,PyList_SetItem,3.2, +function,PyList_SetSlice,3.2, +function,PyList_Size,3.2, +function,PyList_Sort,3.2, +var,PyList_Type,3.2, +type,PyLongObject,3.2, +var,PyLongRangeIter_Type,3.2, +function,PyLong_AsDouble,3.2, +function,PyLong_AsLong,3.2, +function,PyLong_AsLongAndOverflow,3.2, +function,PyLong_AsLongLong,3.2, +function,PyLong_AsLongLongAndOverflow,3.2, +function,PyLong_AsSize_t,3.2, +function,PyLong_AsSsize_t,3.2, +function,PyLong_AsUnsignedLong,3.2, +function,PyLong_AsUnsignedLongLong,3.2, +function,PyLong_AsUnsignedLongLongMask,3.2, +function,PyLong_AsUnsignedLongMask,3.2, +function,PyLong_AsVoidPtr,3.2, +function,PyLong_FromDouble,3.2, +function,PyLong_FromLong,3.2, +function,PyLong_FromLongLong,3.2, +function,PyLong_FromSize_t,3.2, +function,PyLong_FromSsize_t,3.2, +function,PyLong_FromString,3.2, +function,PyLong_FromUnsignedLong,3.2, +function,PyLong_FromUnsignedLongLong,3.2, +function,PyLong_FromVoidPtr,3.2, +function,PyLong_GetInfo,3.2, +var,PyLong_Type,3.2, +var,PyMap_Type,3.2, +function,PyMapping_Check,3.2, +function,PyMapping_GetItemString,3.2, +function,PyMapping_HasKey,3.2, +function,PyMapping_HasKeyString,3.2, +function,PyMapping_Items,3.2, +function,PyMapping_Keys,3.2, +function,PyMapping_Length,3.2, +function,PyMapping_SetItemString,3.2, +function,PyMapping_Size,3.2, +function,PyMapping_Values,3.2, +function,PyMem_Calloc,3.7, +function,PyMem_Free,3.2, +function,PyMem_Malloc,3.2, +function,PyMem_Realloc,3.2, +type,PyMemberDef,3.2, +var,PyMemberDescr_Type,3.2, +function,PyMemoryView_FromMemory,3.7, +function,PyMemoryView_FromObject,3.2, +function,PyMemoryView_GetContiguous,3.2, +var,PyMemoryView_Type,3.2, +type,PyMethodDef,3.2, +var,PyMethodDescr_Type,3.2, +type,PyModuleDef,3.2, +type,PyModuleDef_Base,3.2, +function,PyModuleDef_Init,3.5, +var,PyModuleDef_Type,3.5, +function,PyModule_AddFunctions,3.7, +function,PyModule_AddIntConstant,3.2, +function,PyModule_AddObject,3.2, +function,PyModule_AddObjectRef,3.10, +function,PyModule_AddStringConstant,3.2, +function,PyModule_AddType,3.10, +function,PyModule_Create2,3.2, +function,PyModule_ExecDef,3.7, +function,PyModule_FromDefAndSpec2,3.7, +function,PyModule_GetDef,3.2, +function,PyModule_GetDict,3.2, +function,PyModule_GetFilename,3.2, +function,PyModule_GetFilenameObject,3.2, +function,PyModule_GetName,3.2, +function,PyModule_GetNameObject,3.7, +function,PyModule_GetState,3.2, +function,PyModule_New,3.2, +function,PyModule_NewObject,3.7, +function,PyModule_SetDocString,3.7, +var,PyModule_Type,3.2, +function,PyNumber_Absolute,3.2, +function,PyNumber_Add,3.2, +function,PyNumber_And,3.2, +function,PyNumber_AsSsize_t,3.2, +function,PyNumber_Check,3.2, +function,PyNumber_Divmod,3.2, +function,PyNumber_Float,3.2, +function,PyNumber_FloorDivide,3.2, +function,PyNumber_InPlaceAdd,3.2, +function,PyNumber_InPlaceAnd,3.2, +function,PyNumber_InPlaceFloorDivide,3.2, +function,PyNumber_InPlaceLshift,3.2, +function,PyNumber_InPlaceMatrixMultiply,3.7, +function,PyNumber_InPlaceMultiply,3.2, +function,PyNumber_InPlaceOr,3.2, +function,PyNumber_InPlacePower,3.2, +function,PyNumber_InPlaceRemainder,3.2, +function,PyNumber_InPlaceRshift,3.2, +function,PyNumber_InPlaceSubtract,3.2, +function,PyNumber_InPlaceTrueDivide,3.2, +function,PyNumber_InPlaceXor,3.2, +function,PyNumber_Index,3.2, +function,PyNumber_Invert,3.2, +function,PyNumber_Long,3.2, +function,PyNumber_Lshift,3.2, +function,PyNumber_MatrixMultiply,3.7, +function,PyNumber_Multiply,3.2, +function,PyNumber_Negative,3.2, +function,PyNumber_Or,3.2, +function,PyNumber_Positive,3.2, +function,PyNumber_Power,3.2, +function,PyNumber_Remainder,3.2, +function,PyNumber_Rshift,3.2, +function,PyNumber_Subtract,3.2, +function,PyNumber_ToBase,3.2, +function,PyNumber_TrueDivide,3.2, +function,PyNumber_Xor,3.2, +function,PyOS_AfterFork,3.2,on platforms with fork() +function,PyOS_AfterFork_Child,3.7,on platforms with fork() +function,PyOS_AfterFork_Parent,3.7,on platforms with fork() +function,PyOS_BeforeFork,3.7,on platforms with fork() +function,PyOS_CheckStack,3.7,on platforms with USE_STACKCHECK +function,PyOS_FSPath,3.6, +var,PyOS_InputHook,3.2, +function,PyOS_InterruptOccurred,3.2, +function,PyOS_double_to_string,3.2, +function,PyOS_getsig,3.2, +function,PyOS_mystricmp,3.2, +function,PyOS_mystrnicmp,3.2, +function,PyOS_setsig,3.2, +type,PyOS_sighandler_t,3.2, +function,PyOS_snprintf,3.2, +function,PyOS_string_to_double,3.2, +function,PyOS_strtol,3.2, +function,PyOS_strtoul,3.2, +function,PyOS_vsnprintf,3.2, +type,PyObject,3.2, +function,PyObject_ASCII,3.2, +function,PyObject_AsCharBuffer,3.2, +function,PyObject_AsFileDescriptor,3.2, +function,PyObject_AsReadBuffer,3.2, +function,PyObject_AsWriteBuffer,3.2, +function,PyObject_Bytes,3.2, +function,PyObject_Call,3.2, +function,PyObject_CallFunction,3.2, +function,PyObject_CallFunctionObjArgs,3.2, +function,PyObject_CallMethod,3.2, +function,PyObject_CallMethodObjArgs,3.2, +function,PyObject_CallNoArgs,3.10, +function,PyObject_CallObject,3.2, +function,PyObject_Calloc,3.7, +function,PyObject_CheckReadBuffer,3.2, +function,PyObject_ClearWeakRefs,3.2, +function,PyObject_DelItem,3.2, +function,PyObject_DelItemString,3.2, +function,PyObject_Dir,3.2, +function,PyObject_Format,3.2, +function,PyObject_Free,3.2, +function,PyObject_GC_Del,3.2, +function,PyObject_GC_IsFinalized,3.9, +function,PyObject_GC_IsTracked,3.9, +function,PyObject_GC_Track,3.2, +function,PyObject_GC_UnTrack,3.2, +function,PyObject_GenericGetAttr,3.2, +function,PyObject_GenericGetDict,3.10, +function,PyObject_GenericSetAttr,3.2, +function,PyObject_GenericSetDict,3.7, +function,PyObject_GetAIter,3.10, +function,PyObject_GetAttr,3.2, +function,PyObject_GetAttrString,3.2, +function,PyObject_GetItem,3.2, +function,PyObject_GetIter,3.2, +function,PyObject_HasAttr,3.2, +function,PyObject_HasAttrString,3.2, +function,PyObject_Hash,3.2, +function,PyObject_HashNotImplemented,3.2, +function,PyObject_Init,3.2, +function,PyObject_InitVar,3.2, +function,PyObject_IsInstance,3.2, +function,PyObject_IsSubclass,3.2, +function,PyObject_IsTrue,3.2, +function,PyObject_Length,3.2, +function,PyObject_Malloc,3.2, +function,PyObject_Not,3.2, +function,PyObject_Realloc,3.2, +function,PyObject_Repr,3.2, +function,PyObject_RichCompare,3.2, +function,PyObject_RichCompareBool,3.2, +function,PyObject_SelfIter,3.2, +function,PyObject_SetAttr,3.2, +function,PyObject_SetAttrString,3.2, +function,PyObject_SetItem,3.2, +function,PyObject_Size,3.2, +function,PyObject_Str,3.2, +function,PyObject_Type,3.2, +var,PyProperty_Type,3.2, +var,PyRangeIter_Type,3.2, +var,PyRange_Type,3.2, +var,PyReversed_Type,3.2, +function,PySeqIter_New,3.2, +var,PySeqIter_Type,3.2, +function,PySequence_Check,3.2, +function,PySequence_Concat,3.2, +function,PySequence_Contains,3.2, +function,PySequence_Count,3.2, +function,PySequence_DelItem,3.2, +function,PySequence_DelSlice,3.2, +function,PySequence_Fast,3.2, +function,PySequence_GetItem,3.2, +function,PySequence_GetSlice,3.2, +function,PySequence_In,3.2, +function,PySequence_InPlaceConcat,3.2, +function,PySequence_InPlaceRepeat,3.2, +function,PySequence_Index,3.2, +function,PySequence_Length,3.2, +function,PySequence_List,3.2, +function,PySequence_Repeat,3.2, +function,PySequence_SetItem,3.2, +function,PySequence_SetSlice,3.2, +function,PySequence_Size,3.2, +function,PySequence_Tuple,3.2, +var,PySetIter_Type,3.2, +function,PySet_Add,3.2, +function,PySet_Clear,3.2, +function,PySet_Contains,3.2, +function,PySet_Discard,3.2, +function,PySet_New,3.2, +function,PySet_Pop,3.2, +function,PySet_Size,3.2, +var,PySet_Type,3.2, +function,PySlice_AdjustIndices,3.7, +function,PySlice_GetIndices,3.2, +function,PySlice_GetIndicesEx,3.2, +function,PySlice_New,3.2, +var,PySlice_Type,3.2, +function,PySlice_Unpack,3.7, +function,PyState_AddModule,3.3, +function,PyState_FindModule,3.2, +function,PyState_RemoveModule,3.3, +type,PyStructSequence_Desc,3.2, +type,PyStructSequence_Field,3.2, +function,PyStructSequence_GetItem,3.2, +function,PyStructSequence_New,3.2, +function,PyStructSequence_NewType,3.2, +function,PyStructSequence_SetItem,3.2, +var,PySuper_Type,3.2, +function,PySys_AddWarnOption,3.2, +function,PySys_AddWarnOptionUnicode,3.2, +function,PySys_AddXOption,3.7, +function,PySys_FormatStderr,3.2, +function,PySys_FormatStdout,3.2, +function,PySys_GetObject,3.2, +function,PySys_GetXOptions,3.7, +function,PySys_HasWarnOptions,3.2, +function,PySys_ResetWarnOptions,3.2, +function,PySys_SetArgv,3.2, +function,PySys_SetArgvEx,3.2, +function,PySys_SetObject,3.2, +function,PySys_SetPath,3.2, +function,PySys_WriteStderr,3.2, +function,PySys_WriteStdout,3.2, +type,PyThreadState,3.2, +function,PyThreadState_Clear,3.2, +function,PyThreadState_Delete,3.2, +function,PyThreadState_Get,3.2, +function,PyThreadState_GetDict,3.2, +function,PyThreadState_GetFrame,3.10, +function,PyThreadState_GetID,3.10, +function,PyThreadState_GetInterpreter,3.10, +function,PyThreadState_New,3.2, +function,PyThreadState_SetAsyncExc,3.2, +function,PyThreadState_Swap,3.2, +function,PyThread_GetInfo,3.3, +function,PyThread_ReInitTLS,3.2, +function,PyThread_acquire_lock,3.2, +function,PyThread_acquire_lock_timed,3.2, +function,PyThread_allocate_lock,3.2, +function,PyThread_create_key,3.2, +function,PyThread_delete_key,3.2, +function,PyThread_delete_key_value,3.2, +function,PyThread_exit_thread,3.2, +function,PyThread_free_lock,3.2, +function,PyThread_get_key_value,3.2, +function,PyThread_get_stacksize,3.2, +function,PyThread_get_thread_ident,3.2, +function,PyThread_get_thread_native_id,3.2, +function,PyThread_init_thread,3.2, +function,PyThread_release_lock,3.2, +function,PyThread_set_key_value,3.2, +function,PyThread_set_stacksize,3.2, +function,PyThread_start_new_thread,3.2, +function,PyThread_tss_alloc,3.7, +function,PyThread_tss_create,3.7, +function,PyThread_tss_delete,3.7, +function,PyThread_tss_free,3.7, +function,PyThread_tss_get,3.7, +function,PyThread_tss_is_created,3.7, +function,PyThread_tss_set,3.7, +function,PyTraceBack_Here,3.2, +function,PyTraceBack_Print,3.2, +var,PyTraceBack_Type,3.2, +var,PyTupleIter_Type,3.2, +function,PyTuple_GetItem,3.2, +function,PyTuple_GetSlice,3.2, +function,PyTuple_New,3.2, +function,PyTuple_Pack,3.2, +function,PyTuple_SetItem,3.2, +function,PyTuple_Size,3.2, +var,PyTuple_Type,3.2, +type,PyTypeObject,3.2, +function,PyType_ClearCache,3.2, +function,PyType_FromModuleAndSpec,3.10, +function,PyType_FromSpec,3.2, +function,PyType_FromSpecWithBases,3.3, +function,PyType_GenericAlloc,3.2, +function,PyType_GenericNew,3.2, +function,PyType_GetFlags,3.2, +function,PyType_GetModule,3.10, +function,PyType_GetModuleState,3.10, +function,PyType_GetSlot,3.4, +function,PyType_IsSubtype,3.2, +function,PyType_Modified,3.2, +function,PyType_Ready,3.2, +type,PyType_Slot,3.2, +type,PyType_Spec,3.2, +var,PyType_Type,3.2, +function,PyUnicodeDecodeError_Create,3.2, +function,PyUnicodeDecodeError_GetEncoding,3.2, +function,PyUnicodeDecodeError_GetEnd,3.2, +function,PyUnicodeDecodeError_GetObject,3.2, +function,PyUnicodeDecodeError_GetReason,3.2, +function,PyUnicodeDecodeError_GetStart,3.2, +function,PyUnicodeDecodeError_SetEnd,3.2, +function,PyUnicodeDecodeError_SetReason,3.2, +function,PyUnicodeDecodeError_SetStart,3.2, +function,PyUnicodeEncodeError_GetEncoding,3.2, +function,PyUnicodeEncodeError_GetEnd,3.2, +function,PyUnicodeEncodeError_GetObject,3.2, +function,PyUnicodeEncodeError_GetReason,3.2, +function,PyUnicodeEncodeError_GetStart,3.2, +function,PyUnicodeEncodeError_SetEnd,3.2, +function,PyUnicodeEncodeError_SetReason,3.2, +function,PyUnicodeEncodeError_SetStart,3.2, +var,PyUnicodeIter_Type,3.2, +function,PyUnicodeTranslateError_GetEnd,3.2, +function,PyUnicodeTranslateError_GetObject,3.2, +function,PyUnicodeTranslateError_GetReason,3.2, +function,PyUnicodeTranslateError_GetStart,3.2, +function,PyUnicodeTranslateError_SetEnd,3.2, +function,PyUnicodeTranslateError_SetReason,3.2, +function,PyUnicodeTranslateError_SetStart,3.2, +function,PyUnicode_Append,3.2, +function,PyUnicode_AppendAndDel,3.2, +function,PyUnicode_AsASCIIString,3.2, +function,PyUnicode_AsCharmapString,3.2, +function,PyUnicode_AsDecodedObject,3.2, +function,PyUnicode_AsDecodedUnicode,3.2, +function,PyUnicode_AsEncodedObject,3.2, +function,PyUnicode_AsEncodedString,3.2, +function,PyUnicode_AsEncodedUnicode,3.2, +function,PyUnicode_AsLatin1String,3.2, +function,PyUnicode_AsMBCSString,3.7,on Windows +function,PyUnicode_AsRawUnicodeEscapeString,3.2, +function,PyUnicode_AsUCS4,3.7, +function,PyUnicode_AsUCS4Copy,3.7, +function,PyUnicode_AsUTF16String,3.2, +function,PyUnicode_AsUTF32String,3.2, +function,PyUnicode_AsUTF8AndSize,3.10, +function,PyUnicode_AsUTF8String,3.2, +function,PyUnicode_AsUnicodeEscapeString,3.2, +function,PyUnicode_AsWideChar,3.2, +function,PyUnicode_AsWideCharString,3.7, +function,PyUnicode_BuildEncodingMap,3.2, +function,PyUnicode_Compare,3.2, +function,PyUnicode_CompareWithASCIIString,3.2, +function,PyUnicode_Concat,3.2, +function,PyUnicode_Contains,3.2, +function,PyUnicode_Count,3.2, +function,PyUnicode_Decode,3.2, +function,PyUnicode_DecodeASCII,3.2, +function,PyUnicode_DecodeCharmap,3.2, +function,PyUnicode_DecodeCodePageStateful,3.7,on Windows +function,PyUnicode_DecodeFSDefault,3.2, +function,PyUnicode_DecodeFSDefaultAndSize,3.2, +function,PyUnicode_DecodeLatin1,3.2, +function,PyUnicode_DecodeLocale,3.7, +function,PyUnicode_DecodeLocaleAndSize,3.7, +function,PyUnicode_DecodeMBCS,3.7,on Windows +function,PyUnicode_DecodeMBCSStateful,3.7,on Windows +function,PyUnicode_DecodeRawUnicodeEscape,3.2, +function,PyUnicode_DecodeUTF16,3.2, +function,PyUnicode_DecodeUTF16Stateful,3.2, +function,PyUnicode_DecodeUTF32,3.2, +function,PyUnicode_DecodeUTF32Stateful,3.2, +function,PyUnicode_DecodeUTF7,3.2, +function,PyUnicode_DecodeUTF7Stateful,3.2, +function,PyUnicode_DecodeUTF8,3.2, +function,PyUnicode_DecodeUTF8Stateful,3.2, +function,PyUnicode_DecodeUnicodeEscape,3.2, +function,PyUnicode_EncodeCodePage,3.7,on Windows +function,PyUnicode_EncodeFSDefault,3.2, +function,PyUnicode_EncodeLocale,3.7, +function,PyUnicode_FSConverter,3.2, +function,PyUnicode_FSDecoder,3.2, +function,PyUnicode_Find,3.2, +function,PyUnicode_FindChar,3.7, +function,PyUnicode_Format,3.2, +function,PyUnicode_FromEncodedObject,3.2, +function,PyUnicode_FromFormat,3.2, +function,PyUnicode_FromFormatV,3.2, +function,PyUnicode_FromObject,3.2, +function,PyUnicode_FromOrdinal,3.2, +function,PyUnicode_FromString,3.2, +function,PyUnicode_FromStringAndSize,3.2, +function,PyUnicode_FromWideChar,3.2, +function,PyUnicode_GetDefaultEncoding,3.2, +function,PyUnicode_GetLength,3.7, +function,PyUnicode_GetSize,3.2, +function,PyUnicode_InternFromString,3.2, +function,PyUnicode_InternImmortal,3.2, +function,PyUnicode_InternInPlace,3.2, +function,PyUnicode_IsIdentifier,3.2, +function,PyUnicode_Join,3.2, +function,PyUnicode_Partition,3.2, +function,PyUnicode_RPartition,3.2, +function,PyUnicode_RSplit,3.2, +function,PyUnicode_ReadChar,3.7, +function,PyUnicode_Replace,3.2, +function,PyUnicode_Resize,3.2, +function,PyUnicode_RichCompare,3.2, +function,PyUnicode_Split,3.2, +function,PyUnicode_Splitlines,3.2, +function,PyUnicode_Substring,3.7, +function,PyUnicode_Tailmatch,3.2, +function,PyUnicode_Translate,3.2, +var,PyUnicode_Type,3.2, +function,PyUnicode_WriteChar,3.7, +type,PyVarObject,3.2, +type,PyWeakReference,3.2, +function,PyWeakref_GetObject,3.2, +function,PyWeakref_NewProxy,3.2, +function,PyWeakref_NewRef,3.2, +var,PyWrapperDescr_Type,3.2, +function,PyWrapper_New,3.2, +var,PyZip_Type,3.2, +function,Py_AddPendingCall,3.2, +function,Py_AtExit,3.2, +macro,Py_BEGIN_ALLOW_THREADS,3.2, +macro,Py_BLOCK_THREADS,3.2, +function,Py_BuildValue,3.2, +function,Py_BytesMain,3.8, +function,Py_CompileString,3.2, +function,Py_DecRef,3.2, +function,Py_DecodeLocale,3.7, +macro,Py_END_ALLOW_THREADS,3.2, +function,Py_EncodeLocale,3.7, +function,Py_EndInterpreter,3.2, +function,Py_EnterRecursiveCall,3.9, +function,Py_Exit,3.2, +function,Py_FatalError,3.2, +var,Py_FileSystemDefaultEncodeErrors,3.10, +var,Py_FileSystemDefaultEncoding,3.2, +function,Py_Finalize,3.2, +function,Py_FinalizeEx,3.6, +function,Py_GenericAlias,3.9, +var,Py_GenericAliasType,3.9, +function,Py_GetBuildInfo,3.2, +function,Py_GetCompiler,3.2, +function,Py_GetCopyright,3.2, +function,Py_GetExecPrefix,3.2, +function,Py_GetPath,3.2, +function,Py_GetPlatform,3.2, +function,Py_GetPrefix,3.2, +function,Py_GetProgramFullPath,3.2, +function,Py_GetProgramName,3.2, +function,Py_GetPythonHome,3.2, +function,Py_GetRecursionLimit,3.2, +function,Py_GetVersion,3.2, +var,Py_HasFileSystemDefaultEncoding,3.2, +function,Py_IncRef,3.2, +function,Py_Initialize,3.2, +function,Py_InitializeEx,3.2, +function,Py_Is,3.10, +function,Py_IsFalse,3.10, +function,Py_IsInitialized,3.2, +function,Py_IsNone,3.10, +function,Py_IsTrue,3.10, +function,Py_LeaveRecursiveCall,3.9, +function,Py_Main,3.2, +function,Py_MakePendingCalls,3.2, +function,Py_NewInterpreter,3.2, +function,Py_NewRef,3.10, +function,Py_ReprEnter,3.2, +function,Py_ReprLeave,3.2, +function,Py_SetPath,3.7, +function,Py_SetProgramName,3.2, +function,Py_SetPythonHome,3.2, +function,Py_SetRecursionLimit,3.2, +type,Py_UCS4,3.2, +macro,Py_UNBLOCK_THREADS,3.2, +var,Py_UTF8Mode,3.8, +function,Py_VaBuildValue,3.2, +function,Py_XNewRef,3.10, +type,Py_intptr_t,3.2, +type,Py_ssize_t,3.2, +type,Py_uintptr_t,3.2, +type,allocfunc,3.2, +type,binaryfunc,3.2, +type,descrgetfunc,3.2, +type,descrsetfunc,3.2, +type,destructor,3.2, +type,getattrfunc,3.2, +type,getattrofunc,3.2, +type,getiterfunc,3.2, +type,getter,3.2, +type,hashfunc,3.2, +type,initproc,3.2, +type,inquiry,3.2, +type,iternextfunc,3.2, +type,lenfunc,3.2, +type,newfunc,3.2, +type,objobjargproc,3.2, +type,objobjproc,3.2, +type,reprfunc,3.2, +type,richcmpfunc,3.2, +type,setattrfunc,3.2, +type,setattrofunc,3.2, +type,setter,3.2, +type,ssizeargfunc,3.2, +type,ssizeobjargproc,3.2, +type,ssizessizeargfunc,3.2, +type,ssizessizeobjargproc,3.2, +type,symtable,3.2, +type,ternaryfunc,3.2, +type,traverseproc,3.2, +type,unaryfunc,3.2, +type,visitproc,3.2, diff --git a/Doc/distributing/index.rst b/Doc/distributing/index.rst index 02379946244d84..136cf4e77b1543 100644 --- a/Doc/distributing/index.rst +++ b/Doc/distributing/index.rst @@ -31,7 +31,7 @@ installing other Python projects, refer to the Key terms ========= -* the `Python Packaging Index `__ is a public +* the `Python Package Index `__ is a public repository of open source licensed packages made available for use by other Python users * the `Python Packaging Authority @@ -101,7 +101,7 @@ by invoking the ``pip`` module at the command line:: .. note:: - For POSIX users (including Mac OS X and Linux users), these instructions + For POSIX users (including macOS and Linux users), these instructions assume the use of a :term:`virtual environment`. For Windows users, these instructions assume that the option to @@ -127,15 +127,15 @@ involved in creating and publishing a project: * `Project structure`_ * `Building and packaging the project`_ -* `Uploading the project to the Python Packaging Index`_ +* `Uploading the project to the Python Package Index`_ * `The .pypirc file`_ .. _Project structure: \ - https://packaging.python.org/tutorials/distributing-packages/ + https://packaging.python.org/tutorials/packaging-projects/#packaging-python-projects .. _Building and packaging the project: \ - https://packaging.python.org/tutorials/distributing-packages/#packaging-your-project -.. _Uploading the project to the Python Packaging Index: \ - https://packaging.python.org/tutorials/distributing-packages/#uploading-your-project-to-pypi + https://packaging.python.org/tutorials/packaging-projects/#creating-the-package-files +.. _Uploading the project to the Python Package Index: \ + https://packaging.python.org/tutorials/packaging-projects/#uploading-the-distribution-archives .. _The .pypirc file: \ https://packaging.python.org/specifications/pypirc/ @@ -150,7 +150,7 @@ These are quick answers or links for some common tasks. This isn't an easy topic, but here are a few tips: -* check the Python Packaging Index to see if the name is already in use +* check the Python Package Index to see if the name is already in use * check popular hosting sites like GitHub, Bitbucket, etc to see if there is already a project with that name * check what comes up in a web search for the name you're considering diff --git a/Doc/distutils/apiref.rst b/Doc/distutils/apiref.rst index e4437f4106b519..3291303ccd84bd 100644 --- a/Doc/distutils/apiref.rst +++ b/Doc/distutils/apiref.rst @@ -373,7 +373,7 @@ This module provides the following functions. compiler object under Unix---if you supply a value for *compiler*, *plat* is ignored. - .. % Is the posix/nt only thing still true? Mac OS X seems to work, and + .. % Is the posix/nt only thing still true? macOS seems to work, and .. % returns a UnixCCompiler instance. How to document this... hmm. @@ -1119,11 +1119,11 @@ other utility module. For non-POSIX platforms, currently just returns ``sys.platform``. - For Mac OS X systems the OS version reflects the minimal version on which + For macOS systems the OS version reflects the minimal version on which binaries will run (that is, the value of ``MACOSX_DEPLOYMENT_TARGET`` during the build of Python), not the OS version of the current system. - For universal binary builds on Mac OS X the architecture value reflects + For universal binary builds on macOS the architecture value reflects the universal binary status instead of the architecture of the current processor. For 32-bit universal binaries the architecture is ``fat``, for 64-bit universal binaries the architecture is ``fat64``, and @@ -1132,7 +1132,7 @@ other utility module. a 3-way universal build (ppc, i386, x86_64) and ``intel`` is used for a universal build with the i386 and x86_64 architectures - Examples of returned values on Mac OS X: + Examples of returned values on macOS: * ``macosx-10.3-ppc`` diff --git a/Doc/distutils/sourcedist.rst b/Doc/distutils/sourcedist.rst index 0600663d00e9dc..7b1e22f824e8ce 100644 --- a/Doc/distutils/sourcedist.rst +++ b/Doc/distutils/sourcedist.rst @@ -23,25 +23,25 @@ option, for example:: to create a gzipped tarball and a zip file. The available formats are: -+-----------+-------------------------+---------+ -| Format | Description | Notes | -+===========+=========================+=========+ -| ``zip`` | zip file (:file:`.zip`) | (1),(3) | -+-----------+-------------------------+---------+ -| ``gztar`` | gzip'ed tar file | \(2) | -| | (:file:`.tar.gz`) | | -+-----------+-------------------------+---------+ -| ``bztar`` | bzip2'ed tar file | | -| | (:file:`.tar.bz2`) | | -+-----------+-------------------------+---------+ -| ``xztar`` | xz'ed tar file | | -| | (:file:`.tar.xz`) | | -+-----------+-------------------------+---------+ -| ``ztar`` | compressed tar file | \(4) | -| | (:file:`.tar.Z`) | | -+-----------+-------------------------+---------+ -| ``tar`` | tar file (:file:`.tar`) | | -+-----------+-------------------------+---------+ ++-----------+-------------------------+-------------+ +| Format | Description | Notes | ++===========+=========================+=============+ +| ``zip`` | zip file (:file:`.zip`) | (1),(3) | ++-----------+-------------------------+-------------+ +| ``gztar`` | gzip'ed tar file | \(2) | +| | (:file:`.tar.gz`) | | ++-----------+-------------------------+-------------+ +| ``bztar`` | bzip2'ed tar file | \(5) | +| | (:file:`.tar.bz2`) | | ++-----------+-------------------------+-------------+ +| ``xztar`` | xz'ed tar file | \(5) | +| | (:file:`.tar.xz`) | | ++-----------+-------------------------+-------------+ +| ``ztar`` | compressed tar file | (4),(5) | +| | (:file:`.tar.Z`) | | ++-----------+-------------------------+-------------+ +| ``tar`` | tar file (:file:`.tar`) | \(5) | ++-----------+-------------------------+-------------+ .. versionchanged:: 3.5 Added support for the ``xztar`` format. @@ -61,6 +61,9 @@ Notes: (4) requires the :program:`compress` program. Notice that this format is now pending for deprecation and will be removed in the future versions of Python. +(5) + deprecated by `PEP 527 `_; + `PyPI `_ only accepts ``.zip`` and ``.tar.gz`` files. When using any ``tar`` format (``gztar``, ``bztar``, ``xztar``, ``ztar`` or ``tar``), under Unix you can specify the ``owner`` and ``group`` names diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst index 561d1c616cc10e..2e3362b834e6fb 100644 --- a/Doc/extending/extending.rst +++ b/Doc/extending/extending.rst @@ -127,13 +127,11 @@ Intermezzo: Errors and Exceptions An important convention throughout the Python interpreter is the following: when a function fails, it should set an exception condition and return an error value -(usually a ``NULL`` pointer). Exceptions are stored in a static global variable -inside the interpreter; if this variable is ``NULL`` no exception has occurred. A -second global variable stores the "associated value" of the exception (the -second argument to :keyword:`raise`). A third variable contains the stack -traceback in case the error originated in Python code. These three variables -are the C equivalents of the result in Python of :meth:`sys.exc_info` (see the -section on module :mod:`sys` in the Python Library Reference). It is important +(usually ``-1`` or a ``NULL`` pointer). Exception information is stored in +three members of the interpreter's thread state. These are ``NULL`` if +there is no exception. Otherwise they are the C equivalents of the members +of the Python tuple returned by :meth:`sys.exc_info`. These are the +exception type, exception instance, and a traceback object. It is important to know about them to understand how errors are passed around. The Python API defines a number of functions to set various types of exceptions. diff --git a/Doc/extending/newtypes.rst b/Doc/extending/newtypes.rst index 6e17897ed2c805..f75bee9e6f2a2b 100644 --- a/Doc/extending/newtypes.rst +++ b/Doc/extending/newtypes.rst @@ -73,7 +73,19 @@ function:: newdatatype_dealloc(newdatatypeobject *obj) { free(obj->obj_UnderlyingDatatypePtr); - Py_TYPE(obj)->tp_free(obj); + Py_TYPE(obj)->tp_free((PyObject *)obj); + } + +If your type supports garbage collection, the destructor should call +:c:func:`PyObject_GC_UnTrack` before clearing any member fields:: + + static void + newdatatype_dealloc(newdatatypeobject *obj) + { + PyObject_GC_UnTrack(obj); + Py_CLEAR(obj->other_obj); + ... + Py_TYPE(obj)->tp_free((PyObject *)obj); } .. index:: @@ -381,7 +393,7 @@ analogous to the :ref:`rich comparison methods `, like :c:func:`PyObject_RichCompareBool`. This function is called with two Python objects and the operator as arguments, -where the operator is one of ``Py_EQ``, ``Py_NE``, ``Py_LE``, ``Py_GT``, +where the operator is one of ``Py_EQ``, ``Py_NE``, ``Py_LE``, ``Py_GE``, ``Py_LT`` or ``Py_GT``. It should compare the two objects with respect to the specified operator and return ``Py_True`` or ``Py_False`` if the comparison is successful, ``Py_NotImplemented`` to indicate that comparison is not diff --git a/Doc/extending/newtypes_tutorial.rst b/Doc/extending/newtypes_tutorial.rst index 530e2c4d35f848..34c25d1f6f199c 100644 --- a/Doc/extending/newtypes_tutorial.rst +++ b/Doc/extending/newtypes_tutorial.rst @@ -67,8 +67,8 @@ The first bit is:: This is what a Custom object will contain. ``PyObject_HEAD`` is mandatory at the start of each object struct and defines a field called ``ob_base`` of type :c:type:`PyObject`, containing a pointer to a type object and a -reference count (these can be accessed using the macros :c:macro:`Py_REFCNT` -and :c:macro:`Py_TYPE` respectively). The reason for the macro is to +reference count (these can be accessed using the macros :c:macro:`Py_TYPE` +and :c:macro:`Py_REFCNT` respectively). The reason for the macro is to abstract away the layout and to enable additional fields in :ref:`debug builds `. @@ -90,7 +90,7 @@ The second bit is the definition of the type object. :: static PyTypeObject CustomType = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "custom.Custom", - .tp_doc = "Custom objects", + .tp_doc = PyDoc_STR("Custom objects"), .tp_basicsize = sizeof(CustomObject), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT, @@ -161,7 +161,7 @@ you will need to OR the corresponding flags. We provide a doc string for the type in :c:member:`~PyTypeObject.tp_doc`. :: - .tp_doc = "Custom objects", + .tp_doc = PyDoc_STR("Custom objects"), To enable object creation, we have to provide a :c:member:`~PyTypeObject.tp_new` handler. This is the equivalent of the Python method :meth:`__new__`, but diff --git a/Doc/extending/windows.rst b/Doc/extending/windows.rst index c7b92c6ea24ca8..28d0350f6f114d 100644 --- a/Doc/extending/windows.rst +++ b/Doc/extending/windows.rst @@ -106,8 +106,7 @@ Using DLLs in Practice Windows Python is built in Microsoft Visual C++; using other compilers may or -may not work (though Borland seems to). The rest of this section is MSVC++ -specific. +may not work. The rest of this section is MSVC++ specific. When creating DLLs in Windows, you must pass :file:`pythonXY.lib` to the linker. To build two DLLs, spam and ni (which uses C functions found in spam), you could @@ -134,4 +133,3 @@ Developer Studio will throw in a lot of import libraries that you do not really need, adding about 100K to your executable. To get rid of them, use the Project Settings dialog, Link tab, to specify *ignore default libraries*. Add the correct :file:`msvcrtxx.lib` to the list of libraries. - diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 68570b33e2f626..a624fdb07a17df 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -266,12 +266,9 @@ For cases where you need to choose from a very large number of possibilities, you can create a dictionary mapping case values to functions to call. For example:: - def function_1(...): - ... - functions = {'a': function_1, 'b': function_2, - 'c': self.method_1, ...} + 'c': self.method_1} func = functions[value] func() @@ -279,14 +276,14 @@ example:: For calling methods on objects, you can simplify yet further by using the :func:`getattr` built-in to retrieve methods with a particular name:: - def visit_a(self, ...): - ... - ... + class MyVisitor: + def visit_a(self): + ... - def dispatch(self, value): - method_name = 'visit_' + str(value) - method = getattr(self, method_name) - method() + def dispatch(self, value): + method_name = 'visit_' + str(value) + method = getattr(self, method_name) + method() It's suggested that you use a prefix for the method names, such as ``visit_`` in this example. Without such a prefix, if values are coming from an untrusted @@ -327,8 +324,7 @@ Can Python be compiled to machine code, C or some other language? `Cython `_ compiles a modified version of Python with optional annotations into C extensions. `Nuitka `_ is an up-and-coming compiler of Python into C++ code, aiming to support the full -Python language. For compiling to Java you can consider -`VOC `_. +Python language. How does Python manage memory? @@ -708,6 +704,15 @@ bindings are resolved at run-time in Python, and the second version only needs to perform the resolution once. +Why don't generators support the with statement? +------------------------------------------------ + +For technical reasons, a generator used directly as a context manager +would not work correctly. When, as is most common, a generator is used as +an iterator run to completion, no closing is needed. When it is, wrap +it as "contextlib.closing(generator)" in the 'with' statement. + + Why are colons required for the if/while/def/class statements? -------------------------------------------------------------- diff --git a/Doc/faq/extending.rst b/Doc/faq/extending.rst index aecb56eaa4fd2f..1d2aca6f4c8d97 100644 --- a/Doc/faq/extending.rst +++ b/Doc/faq/extending.rst @@ -254,7 +254,6 @@ For Red Hat, install the python-devel RPM to get the necessary files. For Debian, run ``apt-get install python-dev``. - How do I tell "incomplete input" from "invalid input"? ------------------------------------------------------ @@ -273,161 +272,6 @@ you. You can also set the :c:func:`PyOS_ReadlineFunctionPointer` to point at you custom input function. See ``Modules/readline.c`` and ``Parser/myreadline.c`` for more hints. -However sometimes you have to run the embedded Python interpreter in the same -thread as your rest application and you can't allow the -:c:func:`PyRun_InteractiveLoop` to stop while waiting for user input. The one -solution then is to call :c:func:`PyParser_ParseString` and test for ``e.error`` -equal to ``E_EOF``, which means the input is incomplete. Here's a sample code -fragment, untested, inspired by code from Alex Farber:: - - #define PY_SSIZE_T_CLEAN - #include - #include - #include - #include - #include - #include - - int testcomplete(char *code) - /* code should end in \n */ - /* return -1 for error, 0 for incomplete, 1 for complete */ - { - node *n; - perrdetail e; - - n = PyParser_ParseString(code, &_PyParser_Grammar, - Py_file_input, &e); - if (n == NULL) { - if (e.error == E_EOF) - return 0; - return -1; - } - - PyNode_Free(n); - return 1; - } - -Another solution is trying to compile the received string with -:c:func:`Py_CompileString`. If it compiles without errors, try to execute the -returned code object by calling :c:func:`PyEval_EvalCode`. Otherwise save the -input for later. If the compilation fails, find out if it's an error or just -more input is required - by extracting the message string from the exception -tuple and comparing it to the string "unexpected EOF while parsing". Here is a -complete example using the GNU readline library (you may want to ignore -**SIGINT** while calling readline()):: - - #include - #include - - #define PY_SSIZE_T_CLEAN - #include - #include - #include - #include - - int main (int argc, char* argv[]) - { - int i, j, done = 0; /* lengths of line, code */ - char ps1[] = ">>> "; - char ps2[] = "... "; - char *prompt = ps1; - char *msg, *line, *code = NULL; - PyObject *src, *glb, *loc; - PyObject *exc, *val, *trb, *obj, *dum; - - Py_Initialize (); - loc = PyDict_New (); - glb = PyDict_New (); - PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ()); - - while (!done) - { - line = readline (prompt); - - if (NULL == line) /* Ctrl-D pressed */ - { - done = 1; - } - else - { - i = strlen (line); - - if (i > 0) - add_history (line); /* save non-empty lines */ - - if (NULL == code) /* nothing in code yet */ - j = 0; - else - j = strlen (code); - - code = realloc (code, i + j + 2); - if (NULL == code) /* out of memory */ - exit (1); - - if (0 == j) /* code was empty, so */ - code[0] = '\0'; /* keep strncat happy */ - - strncat (code, line, i); /* append line to code */ - code[i + j] = '\n'; /* append '\n' to code */ - code[i + j + 1] = '\0'; - - src = Py_CompileString (code, "", Py_single_input); - - if (NULL != src) /* compiled just fine - */ - { - if (ps1 == prompt || /* ">>> " or */ - '\n' == code[i + j - 1]) /* "... " and double '\n' */ - { /* so execute it */ - dum = PyEval_EvalCode (src, glb, loc); - Py_XDECREF (dum); - Py_XDECREF (src); - free (code); - code = NULL; - if (PyErr_Occurred ()) - PyErr_Print (); - prompt = ps1; - } - } /* syntax error or E_EOF? */ - else if (PyErr_ExceptionMatches (PyExc_SyntaxError)) - { - PyErr_Fetch (&exc, &val, &trb); /* clears exception! */ - - if (PyArg_ParseTuple (val, "sO", &msg, &obj) && - !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */ - { - Py_XDECREF (exc); - Py_XDECREF (val); - Py_XDECREF (trb); - prompt = ps2; - } - else /* some other syntax error */ - { - PyErr_Restore (exc, val, trb); - PyErr_Print (); - free (code); - code = NULL; - prompt = ps1; - } - } - else /* some non-syntax error */ - { - PyErr_Print (); - free (code); - code = NULL; - prompt = ps1; - } - - free (line); - } - } - - Py_XDECREF(glb); - Py_XDECREF(loc); - Py_Finalize(); - exit(0); - } - - How do I find undefined g++ symbols __builtin_new or __pure_virtual? -------------------------------------------------------------------- diff --git a/Doc/faq/general.rst b/Doc/faq/general.rst index cf70f16c6fe328..7723114bcc18d3 100644 --- a/Doc/faq/general.rst +++ b/Doc/faq/general.rst @@ -113,7 +113,7 @@ to many different classes of problems. The language comes with a large standard library that covers areas such as string processing (regular expressions, Unicode, calculating differences between -files), Internet protocols (HTTP, FTP, SMTP, XML-RPC, POP, IMAP, CGI +files), internet protocols (HTTP, FTP, SMTP, XML-RPC, POP, IMAP, CGI programming), software engineering (unit testing, logging, profiling, parsing Python code), and operating system interfaces (system calls, filesystems, TCP/IP sockets). Look at the table of contents for :ref:`library-index` to get an idea diff --git a/Doc/faq/gui.rst b/Doc/faq/gui.rst index 781da467d18013..86c56d957cdfec 100644 --- a/Doc/faq/gui.rst +++ b/Doc/faq/gui.rst @@ -14,17 +14,8 @@ Graphic User Interface FAQ General GUI Questions ===================== -What platform-independent GUI toolkits exist for Python? -======================================================== - -Depending on what platform(s) you are aiming at, there are several. Some -of them haven't been ported to Python 3 yet. At least `Tkinter`_ and `Qt`_ -are known to be Python 3-compatible. - -.. XXX check links - -Tkinter -------- +What GUI toolkits exist for Python? +=================================== Standard builds of Python include an object-oriented interface to the Tcl/Tk widget set, called :ref:`tkinter `. This is probably the easiest to @@ -32,85 +23,14 @@ install (since it comes included with most `binary distributions `_ of Python) and use. For more info about Tk, including pointers to the source, see the `Tcl/Tk home page `_. Tcl/Tk is fully portable to the -Mac OS X, Windows, and Unix platforms. - -wxWidgets ---------- - -wxWidgets (https://www.wxwidgets.org) is a free, portable GUI class -library written in C++ that provides a native look and feel on a -number of platforms, with Windows, Mac OS X, GTK, X11, all listed as -current stable targets. Language bindings are available for a number -of languages including Python, Perl, Ruby, etc. - -`wxPython `_ is the Python binding for -wxwidgets. While it often lags slightly behind the official wxWidgets -releases, it also offers a number of features via pure Python -extensions that are not available in other language bindings. There -is an active wxPython user and developer community. - -Both wxWidgets and wxPython are free, open source, software with -permissive licences that allow their use in commercial products as -well as in freeware or shareware. - - -Qt ---- - -There are bindings available for the Qt toolkit (using either `PyQt -`_ or `PySide -`_) and for KDE (`PyKDE4 `__). -PyQt is currently more mature than PySide, but you must buy a PyQt license from -`Riverbank Computing `_ -if you want to write proprietary applications. PySide is free for all applications. - -Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses -are available from `The Qt Company `_. - -Gtk+ ----- - -The `GObject introspection bindings `_ -for Python allow you to write GTK+ 3 applications. There is also a -`Python GTK+ 3 Tutorial `_. - -The older PyGtk bindings for the `Gtk+ 2 toolkit `_ have -been implemented by James Henstridge; see . - -Kivy ----- - -`Kivy `_ is a cross-platform GUI library supporting both -desktop operating systems (Windows, macOS, Linux) and mobile devices (Android, -iOS). It is written in Python and Cython, and can use a range of windowing -backends. - -Kivy is free and open source software distributed under the MIT license. - -FLTK ----- - -Python bindings for `the FLTK toolkit `_, a simple yet -powerful and mature cross-platform windowing system, are available from `the -PyFLTK project `_. - -OpenGL ------- - -For OpenGL bindings, see `PyOpenGL `_. - - -What platform-specific GUI toolkits exist for Python? -======================================================== - -By installing the `PyObjc Objective-C bridge -`_, Python programs can use Mac OS X's -Cocoa libraries. - -:ref:`Pythonwin ` by Mark Hammond includes an interface to the -Microsoft Foundation Classes and a Python programming environment -that's written mostly in Python using the MFC classes. - +macOS, Windows, and Unix platforms. + +Depending on what platform(s) you are aiming at, there are also several +alternatives. A `list of cross-platform +`_ and +`platform-specific +`_ GUI +frameworks can be found on the python wiki. Tkinter questions ================= diff --git a/Doc/faq/installed.rst b/Doc/faq/installed.rst index 42296533e26f32..16c9a74daffb1d 100644 --- a/Doc/faq/installed.rst +++ b/Doc/faq/installed.rst @@ -29,7 +29,7 @@ there are several possible ways it could have gotten there. * Some Windows machines also have Python installed. At this writing we're aware of computers from Hewlett-Packard and Compaq that include Python. Apparently some of HP/Compaq's administrative tools are written in Python. -* Many Unix-compatible operating systems, such as Mac OS X and some Linux +* Many Unix-compatible operating systems, such as macOS and some Linux distributions, have Python installed by default; it's included in the base installation. diff --git a/Doc/faq/library.rst b/Doc/faq/library.rst index 4d27abd4351ec0..85b04c963f5906 100644 --- a/Doc/faq/library.rst +++ b/Doc/faq/library.rst @@ -20,7 +20,7 @@ library and will be able to skip this step.) For third-party packages, search the `Python Package Index `_ or try `Google `_ or -another Web search engine. Searching for "Python" plus a keyword or two for +another web search engine. Searching for "Python" plus a keyword or two for your topic of interest will usually find something helpful. @@ -106,9 +106,6 @@ support, pads, and mouse support. This means the module isn't compatible with operating systems that only have BSD curses, but there don't seem to be any currently maintained OSes that fall into this category. -For Windows: use `the consolelib module -`_. - Is there an equivalent to C's onexit() in Python? ------------------------------------------------- @@ -243,9 +240,6 @@ Be sure to use the :mod:`threading` module and not the :mod:`_thread` module. The :mod:`threading` module builds convenient abstractions on top of the low-level primitives provided by the :mod:`_thread` module. -Aahz has a set of slides from his threading tutorial that are helpful; see -http://www.pythoncraft.com/OSCON2001/. - None of my threads seem to run: why? ------------------------------------ @@ -489,8 +483,14 @@ including :func:`~shutil.copyfile`, :func:`~shutil.copytree`, and How do I copy a file? --------------------- -The :mod:`shutil` module contains a :func:`~shutil.copyfile` function. Note -that on MacOS 9 it doesn't copy the resource fork and Finder info. +The :mod:`shutil` module contains a :func:`~shutil.copyfile` function. +Note that on Windows NTFS volumes, it does not copy +`alternate data streams +`_ +nor `resource forks `__ +on macOS HFS+ volumes, though both are now rarely used. +It also doesn't copy file permissions and metadata, though using +:func:`shutil.copy2` instead will preserve most (though not all) of it. How do I read (or write) binary data? @@ -617,9 +617,9 @@ use ``p.read(n)``. How do I access the serial (RS232) port? ---------------------------------------- -For Win32, POSIX (Linux, BSD, etc.), Jython: +For Win32, OSX, Linux, BSD, Jython, IronPython: - http://pyserial.sourceforge.net + https://pypi.org/project/pyserial/ For Unix, see a Usenet post by Mitch Chapman: diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index ed03c494388805..a1701bd4184bb4 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -66,6 +66,8 @@ Static type checkers such as `Mypy `_, source code. +.. _faq-create-standalone-binary: + How can I create a stand-alone binary from a Python script? ----------------------------------------------------------- @@ -76,7 +78,7 @@ set of modules required by a program and bind these modules together with a Python binary to produce a single executable. One is to use the freeze tool, which is included in the Python source tree as -``Tools/freeze``. It converts Python byte code to C arrays; a C compiler you can +``Tools/freeze``. It converts Python byte code to C arrays; with a C compiler you can embed all your modules into a new program, which is then linked with the standard Python modules. @@ -89,14 +91,15 @@ only contains those built-in modules which are actually used in the program. It then compiles the generated C code and links it with the rest of the Python interpreter to form a self-contained binary which acts exactly like your script. -Obviously, freeze requires a C compiler. There are several other utilities -which don't: - -* `py2exe `_ for Windows binaries -* `py2app `_ for Mac OS X binaries -* `cx_Freeze `_ for cross-platform - binaries +The following packages can help with the creation of console and GUI +executables: +* `Nuitka `_ (Cross-platform) +* `PyInstaller `_ (Cross-platform) +* `PyOxidizer `_ (Cross-platform) +* `cx_Freeze `_ (Cross-platform) +* `py2app `_ (macOS only) +* `py2exe `_ (Windows only) Are there coding standards or a style guide for Python programs? ---------------------------------------------------------------- @@ -833,6 +836,27 @@ ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to bite. +How do I get int literal attribute instead of SyntaxError? +---------------------------------------------------------- + +Trying to lookup an ``int`` literal attribute in the normal manner gives +a syntax error because the period is seen as a decimal point:: + + >>> 1.__class__ + File "", line 1 + 1.__class__ + ^ + SyntaxError: invalid decimal literal + +The solution is to separate the literal from the period +with either a space or parentheses. + + >>> 1 .__class__ + + >>> (1).__class__ + + + How do I convert a string to a number? -------------------------------------- @@ -1795,7 +1819,7 @@ for ``None``. This reads like plain English in code and avoids confusion with other objects that may have boolean values that evaluate to false. 2) Detecting optional arguments can be tricky when ``None`` is a valid input -value. In those situations, you can create an singleton sentinel object +value. In those situations, you can create a singleton sentinel object guaranteed to be distinct from other objects. For example, here is how to implement a method that behaves like :meth:`dict.pop`:: @@ -1824,6 +1848,134 @@ For example, here is the implementation of return False +How can a subclass control what data is stored in an immutable instance? +------------------------------------------------------------------------ + +When subclassing an immutable type, override the :meth:`__new__` method +instead of the :meth:`__init__` method. The latter only runs *after* an +instance is created, which is too late to alter data in an immutable +instance. + +All of these immutable classes have a different signature than their +parent class: + +.. testcode:: + + from datetime import date + + class FirstOfMonthDate(date): + "Always choose the first day of the month" + def __new__(cls, year, month, day): + return super().__new__(cls, year, month, 1) + + class NamedInt(int): + "Allow text names for some numbers" + xlat = {'zero': 0, 'one': 1, 'ten': 10} + def __new__(cls, value): + value = cls.xlat.get(value, value) + return super().__new__(cls, value) + + class TitleStr(str): + "Convert str to name suitable for a URL path" + def __new__(cls, s): + s = s.lower().replace(' ', '-') + s = ''.join([c for c in s if c.isalnum() or c == '-']) + return super().__new__(cls, s) + +The classes can be used like this: + +.. doctest:: + + >>> FirstOfMonthDate(2012, 2, 14) + FirstOfMonthDate(2012, 2, 1) + >>> NamedInt('ten') + 10 + >>> NamedInt(20) + 20 + >>> TitleStr('Blog: Why Python Rocks') + 'blog-why-python-rocks' + + +How do I cache method calls? +---------------------------- + +The two principal tools for caching methods are +:func:`functools.cached_property` and :func:`functools.lru_cache`. The +former stores results at the instance level and the latter at the class +level. + +The *cached_property* approach only works with methods that do not take +any arguments. It does not create a reference to the instance. The +cached method result will be kept only as long as the instance is alive. + +The advantage is that when an instance is no longer used, the cached +method result will be released right away. The disadvantage is that if +instances accumulate, so too will the accumulated method results. They +can grow without bound. + +The *lru_cache* approach works with methods that have hashable +arguments. It creates a reference to the instance unless special +efforts are made to pass in weak references. + +The advantage of the least recently used algorithm is that the cache is +bounded by the specified *maxsize*. The disadvantage is that instances +are kept alive until they age out of the cache or until the cache is +cleared. + +This example shows the various techniques:: + + class Weather: + "Lookup weather information on a government website" + + def __init__(self, station_id): + self._station_id = station_id + # The _station_id is private and immutable + + def current_temperature(self): + "Latest hourly observation" + # Do not cache this because old results + # can be out of date. + + @cached_property + def location(self): + "Return the longitude/latitude coordinates of the station" + # Result only depends on the station_id + + @lru_cache(maxsize=20) + def historic_rainfall(self, date, units='mm'): + "Rainfall on a given date" + # Depends on the station_id, date, and units. + +The above example assumes that the *station_id* never changes. If the +relevant instance attributes are mutable, the *cached_property* approach +can't be made to work because it cannot detect changes to the +attributes. + +The *lru_cache* approach can be made to work, but the class needs to define the +*__eq__* and *__hash__* methods so the cache can detect relevant attribute +updates:: + + class Weather: + "Example with a mutable station identifier" + + def __init__(self, station_id): + self.station_id = station_id + + def change_station(self, station_id): + self.station_id = station_id + + def __eq__(self, other): + return self.station_id == other.station_id + + def __hash__(self): + return hash(self.station_id) + + @lru_cache(maxsize=20) + def historic_rainfall(self, date, units='cm'): + 'Rainfall on a given date' + # Depends on the station_id, date, and units. + + Modules ======= @@ -1940,7 +2092,7 @@ Jim Roskind suggests performing steps in the following order in each module: * ``import`` statements * active code (including globals that are initialized from imported values). -van Rossum doesn't like this approach much because the imports appear in a +Van Rossum doesn't like this approach much because the imports appear in a strange place, but it does work. Matthias Urlichs recommends restructuring your code so that the recursive import diff --git a/Doc/faq/windows.rst b/Doc/faq/windows.rst index 186dac2e255b37..4f50b3f1b93f1a 100644 --- a/Doc/faq/windows.rst +++ b/Doc/faq/windows.rst @@ -26,8 +26,8 @@ with running programs from the Windows command line then everything will seem obvious; otherwise, you might need a little more guidance. Unless you use some sort of integrated development environment, you will end up -*typing* Windows commands into what is variously referred to as a "DOS window" -or "Command prompt window". Usually you can create such a window from your +*typing* Windows commands into what is referred to as a +"Command prompt window". Usually you can create such a window from your search bar by searching for ``cmd``. You should be able to recognize when you have started such a window because you will see a Windows "command prompt", which usually looks like this: @@ -140,9 +140,8 @@ offender. How do I make an executable from a Python script? ------------------------------------------------- -See `cx_Freeze `_ and -`py2exe `_, both are distutils extensions -that allow you to create console and GUI executables from Python code. +See :ref:`faq-create-standalone-binary` for a list of tools that can be used to +make executables. Is a ``*.pyd`` file the same as a DLL? @@ -187,9 +186,6 @@ Embedding the Python interpreter in a Windows app can be summarized as follows: by the Windows ``GetProcAddress()`` routine. Macros can make using these pointers transparent to any C code that calls routines in Python's C API. - Borland note: convert :file:`python{NN}.lib` to OMF format using Coff2Omf.exe - first. - .. XXX what about static linking? 2. If you use SWIG, it is easy to create a Python "extension module" that will @@ -280,4 +276,3 @@ How do I check for a keypress without blocking? Use the :mod:`msvcrt` module. This is a standard Windows-specific extension module. It defines a function ``kbhit()`` which checks whether a keyboard hit is present, and ``getch()`` which gets one character without echoing it. - diff --git a/Doc/glossary.rst b/Doc/glossary.rst index 29c68ed72c6d70..62c9b867ca12e3 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -292,12 +292,12 @@ Glossary The decorator syntax is merely syntactic sugar, the following two function definitions are semantically equivalent:: - def f(...): + def f(arg): ... f = staticmethod(f) @staticmethod - def f(...): + def f(arg): ... The same concept exists for classes, but is less commonly used there. See @@ -461,12 +461,13 @@ Glossary for best practices on working with annotations. __future__ - A pseudo-module which programmers can use to enable new language features - which are not compatible with the current interpreter. - - By importing the :mod:`__future__` module and evaluating its variables, - you can see when a new feature was first added to the language and when it - becomes the default:: + A :ref:`future statement `, ``from __future__ import ``, + directs the compiler to compile the current module using syntax or + semantics that will become standard in a future release of Python. + The :mod:`__future__` module documents the possible values of + *feature*. By importing this module and evaluating its variables, + you can see when a new feature was first added to the language and + when it will (or did) become the default:: >>> import __future__ >>> __future__.division @@ -519,12 +520,13 @@ Glossary :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 ` and + A :term:`type` that can be parameterized; typically a + :ref:`container class` such as :class:`list` or + :class:`dict`. Used for :term:`type hints ` and :term:`annotations `. - See :pep:`483` for more details, and :mod:`typing` or - :ref:`generic alias type ` for its uses. + For more details, see :ref:`generic alias types`, + :pep:`483`, :pep:`484`, :pep:`585`, and the :mod:`typing` module. GIL See :term:`global interpreter lock`. @@ -667,6 +669,11 @@ Glossary More information can be found in :ref:`typeiter`. + .. impl-detail:: + + CPython does not consistently apply the requirement that an iterator + define :meth:`__iter__`. + key function A key function or collation function is a callable that returns a value used for sorting or ordering. For example, :func:`locale.strxfrm` is @@ -712,7 +719,7 @@ Glossary 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``). + On Windows, it is the ANSI code page (ex: ``cp1252``). ``locale.getpreferredencoding(False)`` can be used to get the locale encoding. @@ -1129,7 +1136,16 @@ Glossary See also :term:`borrowed reference`. text encoding - A codec which encodes Unicode strings to bytes. + A string in Python is a sequence of Unicode code points (in range + ``U+0000``--``U+10FFFF``). To store or transfer a string, it needs to be + serialized as a sequence of bytes. + + Serializing a string into a sequence of bytes is known as "encoding", and + recreating the string from the sequence of bytes is known as "decoding". + + There are a variety of different text serialization + :ref:`codecs `, which are collectively referred to as + "text encodings". text file A :term:`file object` able to read and write :class:`str` objects. diff --git a/Doc/howto/annotations.rst b/Doc/howto/annotations.rst index 3e61103e99c9a6..2bc2f2d4c839e2 100644 --- a/Doc/howto/annotations.rst +++ b/Doc/howto/annotations.rst @@ -156,7 +156,7 @@ Manually Un-Stringizing Stringized Annotations require annotating with string values that specifically *can't* be evaluated. For example: - * :pep:`604` union types using `|`, before support for this + * :pep:`604` union types using ``|``, before support for this was added to Python 3.10. * Definitions that aren't needed at runtime, only imported when :const:`typing.TYPE_CHECKING` is true. diff --git a/Doc/howto/clinic.rst b/Doc/howto/clinic.rst index 3a3653a5ee3a3e..f2195405fe96fe 100644 --- a/Doc/howto/clinic.rst +++ b/Doc/howto/clinic.rst @@ -1350,7 +1350,7 @@ Here's the simplest example of a custom converter, from ``Modules/zlibmodule.c`` /*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/ This block adds a converter to Argument Clinic named ``ssize_t``. Parameters -declared as ``ssize_t`` will be declared as type ``Py_ssize_t``, and will +declared as ``ssize_t`` will be declared as type :c:type:`Py_ssize_t`, and will be parsed by the ``'O&'`` format unit, which will call the ``ssize_t_converter`` converter function. ``ssize_t`` variables automatically support default values. diff --git a/Doc/howto/curses.rst b/Doc/howto/curses.rst index cc4b4785b12290..c0149ffff37716 100644 --- a/Doc/howto/curses.rst +++ b/Doc/howto/curses.rst @@ -55,11 +55,7 @@ everything, though. The Windows version of Python doesn't include the :mod:`curses` module. A ported version called `UniCurses -`_ is available. You could -also try `the Console module `_ -written by Fredrik Lundh, which doesn't -use the same API as curses but provides cursor-addressable text output -and full support for mouse and keyboard input. +`_ is available. The Python curses module diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst index 575caeb720f3d0..f2e2f7ee68c2e2 100644 --- a/Doc/howto/descriptor.rst +++ b/Doc/howto/descriptor.rst @@ -696,10 +696,14 @@ a pure Python equivalent: >>> b.g == b['g'] == ('getattr_hook', b, 'g') True +Note, there is no :meth:`__getattr__` hook in the :meth:`__getattribute__` +code. That is why calling :meth:`__getattribute__` directly or with +``super().__getattribute__`` will bypass :meth:`__getattr__` entirely. -Interestingly, attribute lookup doesn't call :meth:`object.__getattribute__` -directly. Instead, both the dot operator and the :func:`getattr` function -perform attribute lookup by way of a helper function: +Instead, it is the dot operator and the :func:`getattr` function that are +responsible for invoking :meth:`__getattr__` whenever :meth:`__getattribute__` +raises an :exc:`AttributeError`. Their logic is encapsulated in a helper +function: .. testcode:: @@ -744,12 +748,6 @@ perform attribute lookup by way of a helper function: ... AttributeError: 'ClassWithoutGetAttr' object has no attribute 'z' -So if :meth:`__getattr__` exists, it is called whenever :meth:`__getattribute__` -raises :exc:`AttributeError` (either directly or in one of the descriptor calls). - -Also, if a user calls :meth:`object.__getattribute__` directly, the -:meth:`__getattr__` hook is bypassed entirely. - Invocation from a class ----------------------- @@ -1264,6 +1262,9 @@ Using the non-data descriptor protocol, a pure Python version of def __get__(self, obj, objtype=None): return self.f + def __call__(self, *args, **kwds): + return self.f(*args, **kwds) + .. testcode:: :hide: @@ -1272,6 +1273,8 @@ Using the non-data descriptor protocol, a pure Python version of def f(x): return x * 10 + wrapped_ord = StaticMethod(ord) + .. doctest:: :hide: @@ -1279,6 +1282,8 @@ Using the non-data descriptor protocol, a pure Python version of 30 >>> E_sim().f(3) 30 + >>> wrapped_ord('A') + 65 Class methods @@ -1344,7 +1349,7 @@ Using the non-data descriptor protocol, a pure Python version of if cls is None: cls = type(obj) if hasattr(type(self.f), '__get__'): - return self.f.__get__(cls) + return self.f.__get__(cls, cls) return MethodType(self.f, cls) .. testcode:: @@ -1537,7 +1542,7 @@ variables: 'Simulate how the type metaclass adds member objects for slots' def __new__(mcls, clsname, bases, mapping): - 'Emuluate type_new() in Objects/typeobject.c' + 'Emulate type_new() in Objects/typeobject.c' # type_new() calls PyTypeReady() which calls add_methods() slot_names = mapping.get('slot_names', []) for offset, name in enumerate(slot_names): diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst deleted file mode 100644 index 9ece93e660504f..00000000000000 --- a/Doc/howto/enum.rst +++ /dev/null @@ -1,1416 +0,0 @@ -========== -Enum HOWTO -========== - -:Author: Ethan Furman - -.. _enum-basic-tutorial: - -.. currentmodule:: enum - -Basic Enum Tutorial -------------------- - -An :class:`Enum` is a set of symbolic names bound to unique values. They are -similar to global variables, but they offer a more useful :func:`repr()`, -grouping, type-safety, and a few other features. - -They are most useful when you have a variable that can take one of a limited -selection of values. For example, the days of the week:: - - >>> from enum import Enum - >>> class Weekday(Enum): - ... MONDAY = 1 - ... TUESDAY = 2 - ... WEDNESDAY = 3 - ... THURSDAY = 4 - ... FRIDAY = 5 - ... SATURDAY = 6 - ... SUNDAY = 7 - -As you can see, creating an :class:`Enum` is as simple as writing a class that -inherits from :class:`Enum` itself. - -.. note:: Case of Enum Members - - Because Enums are used to represent constants we recommend using - UPPER_CASE names for members, and will be using that style in our examples. - -Depending on the nature of the enum a member's value may or may not be -important, but either way that value can be used to get the corresponding -member:: - - >>> Weekday(3) - Weekday.WEDNESDAY - -As you can see, the ``repr()`` of a member shows the enum name and the -member name. The ``str()`` on a member shows only its name:: - - >>> print(Weekday.THURSDAY) - THURSDAY - -The *type* of an enumeration member is the enum it belongs to:: - - >>> type(Weekday.MONDAY) - - >>> isinstance(Weekday.FRIDAY, Weekday) - True - -Enum members have an attribute that contains just their :attr:`name`:: - - >>> print(Weekday.TUESDAY.name) - TUESDAY - -Likewise, they have an attribute for their :attr:`value`:: - - - >>> Weekday.WEDNESDAY.value - 3 - -Unlike many languages that treat enumerations solely as name/value pairs, -Python Enums can have behavior added. For example, :class:`datetime.date` -has two methods for returning the weekday: :meth:`weekday` and :meth:`isoweekday`. -The difference is that one of them counts from 0-6 and the other from 1-7. -Rather than keep track of that ourselves we can add a method to the :class:`Weekday` -enum to extract the day from the :class:`date` instance and return the matching -enum member:: - - @classmethod - def from_date(cls, date): - return cls(date.isoweekday()) - -The complete :class:`Weekday` enum now looks like this:: - - >>> class Weekday(Enum): - ... MONDAY = 1 - ... TUESDAY = 2 - ... WEDNESDAY = 3 - ... THURSDAY = 4 - ... FRIDAY = 5 - ... SATURDAY = 6 - ... SUNDAY = 7 - ... # - ... @classmethod - ... def from_date(cls, date): - ... return cls(date.isoweekday()) - -Now we can find out what today is! Observe:: - - >>> from datetime import date - >>> Weekday.from_date(date.today()) - Weekday.TUESDAY - -Of course, if you're reading this on some other day, you'll see that day instead. - -This :class:`Weekday` enum is great if our variable only needs one day, but -what if we need several? Maybe we're writing a function to plot chores during -a week, and don't want to use a :class:`list` -- we could use a different type -of :class:`Enum`:: - - >>> from enum import Flag - >>> class Weekday(Flag): - ... MONDAY = 1 - ... TUESDAY = 2 - ... WEDNESDAY = 4 - ... THURSDAY = 8 - ... FRIDAY = 16 - ... SATURDAY = 32 - ... SUNDAY = 64 - -We've changed two things: we're inherited from :class:`Flag`, and the values are -all powers of 2. - -Just like the original :class:`Weekday` enum above, we can have a single selection:: - - >>> first_week_day = Weekday.MONDAY - >>> first_week_day - Weekday.MONDAY - -But :class:`Flag` also allows us to combine several members into a single -variable:: - - >>> weekend = Weekday.SATURDAY | Weekday.SUNDAY - >>> weekend - Weekday.SATURDAY|Weekday.SUNDAY - -You can even iterate over a :class:`Flag` variable:: - - >>> for day in weekend: - ... print(day) - SATURDAY - SUNDAY - -Okay, let's get some chores set up:: - - >>> chores_for_ethan = { - ... 'feed the cat': Weekday.MONDAY | Weekday.WEDNESDAY | Weekday.FRIDAY, - ... 'do the dishes': Weekday.TUESDAY | Weekday.THURSDAY, - ... 'answer SO questions': Weekday.SATURDAY, - ... } - -And a function to display the chores for a given day:: - - >>> def show_chores(chores, day): - ... for chore, days in chores.items(): - ... if day in days: - ... print(chore) - >>> show_chores(chores_for_ethan, Weekday.SATURDAY) - answer SO questions - -In cases where the actual values of the members do not matter, you can save -yourself some work and use :func:`auto()` for the values:: - - >>> from enum import auto - >>> class Weekday(Flag): - ... MONDAY = auto() - ... TUESDAY = auto() - ... WEDNESDAY = auto() - ... THURSDAY = auto() - ... FRIDAY = auto() - ... SATURDAY = auto() - ... SUNDAY = auto() - - -.. _enum-advanced-tutorial: - -Programmatic access to enumeration members and their attributes ---------------------------------------------------------------- - -Sometimes it's useful to access members in enumerations programmatically (i.e. -situations where ``Color.RED`` won't do because the exact color is not known -at program-writing time). ``Enum`` allows such access:: - - >>> Color(1) - Color.RED - >>> Color(3) - Color.BLUE - -If you want to access enum members by *name*, use item access:: - - >>> Color['RED'] - Color.RED - >>> Color['GREEN'] - Color.GREEN - -If you have an enum member and need its :attr:`name` or :attr:`value`:: - - >>> member = Color.RED - >>> member.name - 'RED' - >>> member.value - 1 - - -Duplicating enum members and values ------------------------------------ - -Having two enum members with the same name is invalid:: - - >>> class Shape(Enum): - ... SQUARE = 2 - ... SQUARE = 3 - ... - Traceback (most recent call last): - ... - TypeError: 'SQUARE' already defined as: 2 - -However, an enum member can have other names associated with it. Given two -entries ``A`` and ``B`` with the same value (and ``A`` defined first), ``B`` -is an alias for the member ``A``. By-value lookup of the value of ``A`` will -return the member ``A``. By-name lookup of ``A`` will return the member ``A``. -By-name lookup of ``B`` will also return the member ``A``:: - - >>> class Shape(Enum): - ... SQUARE = 2 - ... DIAMOND = 1 - ... CIRCLE = 3 - ... ALIAS_FOR_SQUARE = 2 - ... - >>> Shape.SQUARE - Shape.SQUARE - >>> Shape.ALIAS_FOR_SQUARE - Shape.SQUARE - >>> Shape(2) - Shape.SQUARE - -.. note:: - - Attempting to create a member with the same name as an already - defined attribute (another member, a method, etc.) or attempting to create - an attribute with the same name as a member is not allowed. - - -Ensuring unique enumeration values ----------------------------------- - -By default, enumerations allow multiple names as aliases for the same value. -When this behavior isn't desired, you can use the :func:`unique` decorator:: - - >>> from enum import Enum, unique - >>> @unique - ... class Mistake(Enum): - ... ONE = 1 - ... TWO = 2 - ... THREE = 3 - ... FOUR = 3 - ... - Traceback (most recent call last): - ... - ValueError: duplicate values found in : FOUR -> THREE - - -Using automatic values ----------------------- - -If the exact value is unimportant you can use :class:`auto`:: - - >>> from enum import Enum, auto - >>> class Color(Enum): - ... RED = auto() - ... BLUE = auto() - ... GREEN = auto() - ... - >>> [member.value for member in Color] - [1, 2, 3] - -The values are chosen by :func:`_generate_next_value_`, which can be -overridden:: - - >>> class AutoName(Enum): - ... def _generate_next_value_(name, start, count, last_values): - ... return name - ... - >>> class Ordinal(AutoName): - ... NORTH = auto() - ... SOUTH = auto() - ... EAST = auto() - ... WEST = auto() - ... - >>> [member.value for member in Color] - ['NORTH', 'SOUTH', 'EAST', 'WEST'] - -.. note:: - - The :meth:`_generate_next_value_` method must be defined before any members. - -Iteration ---------- - -Iterating over the members of an enum does not provide the aliases:: - - >>> list(Shape) - [Shape.SQUARE, Shape.DIAMOND, Shape.CIRCLE] - -The special attribute ``__members__`` is a read-only ordered mapping of names -to members. It includes all names defined in the enumeration, including the -aliases:: - - >>> for name, member in Shape.__members__.items(): - ... name, member - ... - ('SQUARE', Shape.SQUARE) - ('DIAMOND', Shape.DIAMOND) - ('CIRCLE', Shape.CIRCLE) - ('ALIAS_FOR_SQUARE', Shape.SQUARE) - -The ``__members__`` attribute can be used for detailed programmatic access to -the enumeration members. For example, finding all the aliases:: - - >>> [name for name, member in Shape.__members__.items() if member.name != name] - ['ALIAS_FOR_SQUARE'] - - -Comparisons ------------ - -Enumeration members are compared by identity:: - - >>> Color.RED is Color.RED - True - >>> Color.RED is Color.BLUE - False - >>> Color.RED is not Color.BLUE - True - -Ordered comparisons between enumeration values are *not* supported. Enum -members are not integers (but see `IntEnum`_ below):: - - >>> Color.RED < Color.BLUE - Traceback (most recent call last): - File "", line 1, in - TypeError: '<' not supported between instances of 'Color' and 'Color' - -Equality comparisons are defined though:: - - >>> Color.BLUE == Color.RED - False - >>> Color.BLUE != Color.RED - True - >>> Color.BLUE == Color.BLUE - True - -Comparisons against non-enumeration values will always compare not equal -(again, :class:`IntEnum` was explicitly designed to behave differently, see -below):: - - >>> Color.BLUE == 2 - False - - -Allowed members and attributes of enumerations ----------------------------------------------- - -Most of the examples above use integers for enumeration values. Using integers is -short and handy (and provided by default by the `Functional API`_), but not -strictly enforced. In the vast majority of use-cases, one doesn't care what -the actual value of an enumeration is. But if the value *is* important, -enumerations can have arbitrary values. - -Enumerations are Python classes, and can have methods and special methods as -usual. If we have this enumeration:: - - >>> class Mood(Enum): - ... FUNKY = 1 - ... HAPPY = 3 - ... - ... def describe(self): - ... # self is the member here - ... return self.name, self.value - ... - ... def __str__(self): - ... return 'my custom str! {0}'.format(self.value) - ... - ... @classmethod - ... def favorite_mood(cls): - ... # cls here is the enumeration - ... return cls.HAPPY - ... - -Then:: - - >>> Mood.favorite_mood() - Mood.HAPPY - >>> Mood.HAPPY.describe() - ('HAPPY', 3) - >>> str(Mood.FUNKY) - 'my custom str! 1' - -The rules for what is allowed are as follows: names that start and end with -a single underscore are reserved by enum and cannot be used; all other -attributes defined within an enumeration will become members of this -enumeration, with the exception of special methods (:meth:`__str__`, -:meth:`__add__`, etc.), descriptors (methods are also descriptors), and -variable names listed in :attr:`_ignore_`. - -Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__` then -any value(s) given to the enum member will be passed into those methods. -See `Planet`_ for an example. - - -Restricted Enum subclassing ---------------------------- - -A new :class:`Enum` class must have one base enum class, up to one concrete -data type, and as many :class:`object`-based mixin classes as needed. The -order of these base classes is:: - - class EnumName([mix-in, ...,] [data-type,] base-enum): - pass - -Also, subclassing an enumeration is allowed only if the enumeration does not define -any members. So this is forbidden:: - - >>> class MoreColor(Color): - ... PINK = 17 - ... - Traceback (most recent call last): - ... - TypeError: MoreColor: cannot extend enumeration 'Color' - -But this is allowed:: - - >>> class Foo(Enum): - ... def some_behavior(self): - ... pass - ... - >>> class Bar(Foo): - ... HAPPY = 1 - ... SAD = 2 - ... - -Allowing subclassing of enums that define members would lead to a violation of -some important invariants of types and instances. On the other hand, it makes -sense to allow sharing some common behavior between a group of enumerations. -(See `OrderedEnum`_ for an example.) - - -Pickling --------- - -Enumerations can be pickled and unpickled:: - - >>> from test.test_enum import Fruit - >>> from pickle import dumps, loads - >>> Fruit.TOMATO is loads(dumps(Fruit.TOMATO)) - True - -The usual restrictions for pickling apply: picklable enums must be defined in -the top level of a module, since unpickling requires them to be importable -from that module. - -.. note:: - - With pickle protocol version 4 it is possible to easily pickle enums - nested in other classes. - -It is possible to modify how enum members are pickled/unpickled by defining -:meth:`__reduce_ex__` in the enumeration class. - - -Functional API --------------- - -The :class:`Enum` class is callable, providing the following functional API:: - - >>> Animal = Enum('Animal', 'ANT BEE CAT DOG') - >>> Animal - - >>> Animal.ANT - Animal.ANT - >>> Animal.ANT.value - 1 - >>> list(Animal) - [Animal.ANT, Animal.BEE, Animal.CAT, Animal.DOG] - -The semantics of this API resemble :class:`~collections.namedtuple`. The first -argument of the call to :class:`Enum` is the name of the enumeration. - -The second argument is the *source* of enumeration member names. It can be a -whitespace-separated string of names, a sequence of names, a sequence of -2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to -values. The last two options enable assigning arbitrary values to -enumerations; the others auto-assign increasing integers starting with 1 (use -the ``start`` parameter to specify a different starting value). A -new class derived from :class:`Enum` is returned. In other words, the above -assignment to :class:`Animal` is equivalent to:: - - >>> class Animal(Enum): - ... ANT = 1 - ... BEE = 2 - ... CAT = 3 - ... DOG = 4 - ... - -The reason for defaulting to ``1`` as the starting number and not ``0`` is -that ``0`` is ``False`` in a boolean sense, but by default enum members all -evaluate to ``True``. - -Pickling enums created with the functional API can be tricky as frame stack -implementation details are used to try and figure out which module the -enumeration is being created in (e.g. it will fail if you use a utility -function in separate module, and also may not work on IronPython or Jython). -The solution is to specify the module name explicitly as follows:: - - >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', module=__name__) - -.. warning:: - - If ``module`` is not supplied, and Enum cannot determine what it is, - the new Enum members will not be unpicklable; to keep errors closer to - the source, pickling will be disabled. - -The new pickle protocol 4 also, in some circumstances, relies on -:attr:`~definition.__qualname__` being set to the location where pickle will be able -to find the class. For example, if the class was made available in class -SomeData in the global scope:: - - >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal') - -The complete signature is:: - - Enum( - value='NewEnumName', - names=<...>, - *, - module='...', - qualname='...', - type=, - start=1, - ) - -:value: What the new enum class will record as its name. - -:names: The enum members. This can be a whitespace or comma separated string - (values will start at 1 unless otherwise specified):: - - 'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE' - - or an iterator of names:: - - ['RED', 'GREEN', 'BLUE'] - - or an iterator of (name, value) pairs:: - - [('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)] - - or a mapping:: - - {'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42} - -:module: name of module where new enum class can be found. - -:qualname: where in module new enum class can be found. - -:type: type to mix in to new enum class. - -:start: number to start counting at if only names are passed in. - -.. versionchanged:: 3.5 - The *start* parameter was added. - - -Derived Enumerations --------------------- - -IntEnum -^^^^^^^ - -The first variation of :class:`Enum` that is provided is also a subclass of -:class:`int`. Members of an :class:`IntEnum` can be compared to integers; -by extension, integer enumerations of different types can also be compared -to each other:: - - >>> from enum import IntEnum - >>> class Shape(IntEnum): - ... CIRCLE = 1 - ... SQUARE = 2 - ... - >>> class Request(IntEnum): - ... POST = 1 - ... GET = 2 - ... - >>> Shape == 1 - False - >>> Shape.CIRCLE == 1 - True - >>> Shape.CIRCLE == Request.POST - True - -However, they still can't be compared to standard :class:`Enum` enumerations:: - - >>> class Shape(IntEnum): - ... CIRCLE = 1 - ... SQUARE = 2 - ... - >>> class Color(Enum): - ... RED = 1 - ... GREEN = 2 - ... - >>> Shape.CIRCLE == Color.RED - False - -:class:`IntEnum` values behave like integers in other ways you'd expect:: - - >>> int(Shape.CIRCLE) - 1 - >>> ['a', 'b', 'c'][Shape.CIRCLE] - 'b' - >>> [i for i in range(Shape.SQUARE)] - [0, 1] - - -StrEnum -^^^^^^^ - -The second variation of :class:`Enum` that is provided is also a subclass of -:class:`str`. Members of a :class:`StrEnum` can be compared to strings; -by extension, string enumerations of different types can also be compared -to each other. :class:`StrEnum` exists to help avoid the problem of getting -an incorrect member:: - - >>> from enum import StrEnum - >>> class Directions(StrEnum): - ... NORTH = 'north', # notice the trailing comma - ... SOUTH = 'south' - -Before :class:`StrEnum`, ``Directions.NORTH`` would have been the :class:`tuple` -``('north',)``. - -.. versionadded:: 3.10 - - -IntFlag -^^^^^^^ - -The next variation of :class:`Enum` provided, :class:`IntFlag`, is also based -on :class:`int`. The difference being :class:`IntFlag` members can be combined -using the bitwise operators (&, \|, ^, ~) and the result is still an -:class:`IntFlag` member, if possible. However, as the name implies, :class:`IntFlag` -members also subclass :class:`int` and can be used wherever an :class:`int` is -used. - -.. note:: - - Any operation on an :class:`IntFlag` member besides the bit-wise operations will - lose the :class:`IntFlag` membership. - - Bit-wise operations that result in invalid :class:`IntFlag` values will lose the - :class:`IntFlag` membership. See :class:`FlagBoundary` for - details. - -.. versionadded:: 3.6 -.. versionchanged:: 3.10 - -Sample :class:`IntFlag` class:: - - >>> from enum import IntFlag - >>> class Perm(IntFlag): - ... R = 4 - ... W = 2 - ... X = 1 - ... - >>> Perm.R | Perm.W - Perm.R|Perm.W - >>> Perm.R + Perm.W - 6 - >>> RW = Perm.R | Perm.W - >>> Perm.R in RW - True - -It is also possible to name the combinations:: - - >>> class Perm(IntFlag): - ... R = 4 - ... W = 2 - ... X = 1 - ... RWX = 7 - >>> Perm.RWX - Perm.RWX - >>> ~Perm.RWX - Perm(0) - >>> Perm(7) - Perm.RWX - -.. note:: - - Named combinations are considered aliases. Aliases do not show up during - iteration, but can be returned from by-value lookups. - -.. versionchanged:: 3.10 - -Another important difference between :class:`IntFlag` and :class:`Enum` is that -if no flags are set (the value is 0), its boolean evaluation is :data:`False`:: - - >>> Perm.R & Perm.X - Perm(0) - >>> bool(Perm.R & Perm.X) - False - -Because :class:`IntFlag` members are also subclasses of :class:`int` they can -be combined with them (but may lose :class:`IntFlag` membership:: - - >>> Perm.X | 4 - Perm.R|Perm.X - - >>> Perm.X | 8 - 9 - -.. note:: - - The negation operator, ``~``, always returns an :class:`IntFlag` member with a - positive value:: - - >>> (~Perm.X).value == (Perm.R|Perm.W).value == 6 - True - -:class:`IntFlag` members can also be iterated over:: - - >>> list(RW) - [Perm.R, Perm.W] - -.. versionadded:: 3.10 - - -Flag -^^^^ - -The last variation is :class:`Flag`. Like :class:`IntFlag`, :class:`Flag` -members can be combined using the bitwise operators (&, \|, ^, ~). Unlike -:class:`IntFlag`, they cannot be combined with, nor compared against, any -other :class:`Flag` enumeration, nor :class:`int`. While it is possible to -specify the values directly it is recommended to use :class:`auto` as the -value and let :class:`Flag` select an appropriate value. - -.. versionadded:: 3.6 - -Like :class:`IntFlag`, if a combination of :class:`Flag` members results in no -flags being set, the boolean evaluation is :data:`False`:: - - >>> from enum import Flag, auto - >>> class Color(Flag): - ... RED = auto() - ... BLUE = auto() - ... GREEN = auto() - ... - >>> Color.RED & Color.GREEN - Color(0) - >>> bool(Color.RED & Color.GREEN) - False - -Individual flags should have values that are powers of two (1, 2, 4, 8, ...), -while combinations of flags won't:: - - >>> class Color(Flag): - ... RED = auto() - ... BLUE = auto() - ... GREEN = auto() - ... WHITE = RED | BLUE | GREEN - ... - >>> Color.WHITE - Color.WHITE - -Giving a name to the "no flags set" condition does not change its boolean -value:: - - >>> class Color(Flag): - ... BLACK = 0 - ... RED = auto() - ... BLUE = auto() - ... GREEN = auto() - ... - >>> Color.BLACK - Color.BLACK - >>> bool(Color.BLACK) - False - -:class:`Flag` members can also be iterated over:: - - >>> purple = Color.RED | Color.BLUE - >>> list(purple) - [Color.RED, Color.BLUE] - -.. versionadded:: 3.10 - -.. note:: - - For the majority of new code, :class:`Enum` and :class:`Flag` are strongly - recommended, since :class:`IntEnum` and :class:`IntFlag` break some - semantic promises of an enumeration (by being comparable to integers, and - thus by transitivity to other unrelated enumerations). :class:`IntEnum` - and :class:`IntFlag` should be used only in cases where :class:`Enum` and - :class:`Flag` will not do; for example, when integer constants are replaced - with enumerations, or for interoperability with other systems. - - -Others -^^^^^^ - -While :class:`IntEnum` is part of the :mod:`enum` module, it would be very -simple to implement independently:: - - class IntEnum(int, Enum): - pass - -This demonstrates how similar derived enumerations can be defined; for example -a :class:`StrEnum` that mixes in :class:`str` instead of :class:`int`. - -Some rules: - -1. When subclassing :class:`Enum`, mix-in types must appear before - :class:`Enum` itself in the sequence of bases, as in the :class:`IntEnum` - example above. -2. While :class:`Enum` can have members of any type, once you mix in an - additional type, all the members must have values of that type, e.g. - :class:`int` above. This restriction does not apply to mix-ins which only - add methods and don't specify another type. -3. When another data type is mixed in, the :attr:`value` attribute is *not the - same* as the enum member itself, although it is equivalent and will compare - equal. -4. %-style formatting: `%s` and `%r` call the :class:`Enum` class's - :meth:`__str__` and :meth:`__repr__` respectively; other codes (such as - `%i` or `%h` for IntEnum) treat the enum member as its mixed-in type. -5. :ref:`Formatted string literals `, :meth:`str.format`, - and :func:`format` will use the mixed-in type's :meth:`__format__` - unless :meth:`__str__` or :meth:`__format__` is overridden in the subclass, - in which case the overridden methods or :class:`Enum` methods will be used. - Use the !s and !r format codes to force usage of the :class:`Enum` class's - :meth:`__str__` and :meth:`__repr__` methods. - -When to use :meth:`__new__` vs. :meth:`__init__` ------------------------------------------------- - -:meth:`__new__` must be used whenever you want to customize the actual value of -the :class:`Enum` member. Any other modifications may go in either -:meth:`__new__` or :meth:`__init__`, with :meth:`__init__` being preferred. - -For example, if you want to pass several items to the constructor, but only -want one of them to be the value:: - - >>> class Coordinate(bytes, Enum): - ... """ - ... Coordinate with binary codes that can be indexed by the int code. - ... """ - ... def __new__(cls, value, label, unit): - ... obj = bytes.__new__(cls, [value]) - ... obj._value_ = value - ... obj.label = label - ... obj.unit = unit - ... return obj - ... PX = (0, 'P.X', 'km') - ... PY = (1, 'P.Y', 'km') - ... VX = (2, 'V.X', 'km/s') - ... VY = (3, 'V.Y', 'km/s') - ... - - >>> print(Coordinate['PY']) - PY - - >>> print(Coordinate(3)) - VY - - -Finer Points -^^^^^^^^^^^^ - -Supported ``__dunder__`` names -"""""""""""""""""""""""""""""" - -:attr:`__members__` is a read-only ordered mapping of ``member_name``:``member`` -items. It is only available on the class. - -:meth:`__new__`, if specified, must create and return the enum members; it is -also a very good idea to set the member's :attr:`_value_` appropriately. Once -all the members are created it is no longer used. - - -Supported ``_sunder_`` names -"""""""""""""""""""""""""""" - -- ``_name_`` -- name of the member -- ``_value_`` -- value of the member; can be set / modified in ``__new__`` - -- ``_missing_`` -- a lookup function used when a value is not found; may be - overridden -- ``_ignore_`` -- a list of names, either as a :class:`list` or a :class:`str`, - that will not be transformed into members, and will be removed from the final - class -- ``_order_`` -- used in Python 2/3 code to ensure member order is consistent - (class attribute, removed during class creation) -- ``_generate_next_value_`` -- used by the `Functional API`_ and by - :class:`auto` to get an appropriate value for an enum member; may be - overridden - -.. note:: - - For standard :class:`Enum` classes the next value chosen is the last value seen - incremented by one. - - For :class:`Flag` classes the next value chosen will be the next highest - power-of-two, regardless of the last value seen. - -.. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_`` -.. versionadded:: 3.7 ``_ignore_`` - -To help keep Python 2 / Python 3 code in sync an :attr:`_order_` attribute can -be provided. It will be checked against the actual order of the enumeration -and raise an error if the two do not match:: - - >>> class Color(Enum): - ... _order_ = 'RED GREEN BLUE' - ... RED = 1 - ... BLUE = 3 - ... GREEN = 2 - ... - Traceback (most recent call last): - ... - TypeError: member order does not match _order_: - ['RED', 'BLUE', 'GREEN'] - ['RED', 'GREEN', 'BLUE'] - -.. note:: - - In Python 2 code the :attr:`_order_` attribute is necessary as definition - order is lost before it can be recorded. - - -_Private__names -""""""""""""""" - -Private names are not converted to enum members, but remain normal attributes. - -.. versionchanged:: 3.10 - - -``Enum`` member type -"""""""""""""""""""" - -Enum members are instances of their enum class, and are normally accessed as -``EnumClass.member``. In Python versions ``3.5`` to ``3.9`` you could access -members from other members -- this practice was discouraged, and in ``3.12`` -:class:`Enum` will return to not allowing it, while in ``3.10`` and ``3.11`` -it will raise a :exc:`DeprecationWarning`:: - - >>> class FieldTypes(Enum): - ... name = 0 - ... value = 1 - ... size = 2 - ... - >>> FieldTypes.value.size # doctest: +SKIP - DeprecationWarning: accessing one member from another is not supported, - and will be disabled in 3.12 - - -.. versionchanged:: 3.5 -.. versionchanged:: 3.10 - - -Creating members that are mixed with other data types -""""""""""""""""""""""""""""""""""""""""""""""""""""" - -When subclassing other data types, such as :class:`int` or :class:`str`, with -an :class:`Enum`, all values after the `=` are passed to that data type's -constructor. For example:: - - >>> class MyEnum(IntEnum): - ... example = '11', 16 # '11' will be interpreted as a hexadecimal - ... # number - >>> MyEnum.example.value - 17 - - -Boolean value of ``Enum`` classes and members -""""""""""""""""""""""""""""""""""""""""""""" - -Enum classes that are mixed with non-:class:`Enum` types (such as -:class:`int`, :class:`str`, etc.) are evaluated according to the mixed-in -type's rules; otherwise, all members evaluate as :data:`True`. To make your -own enum's boolean evaluation depend on the member's value add the following to -your class:: - - def __bool__(self): - return bool(self.value) - -Plain :class:`Enum` classes always evaluate as :data:`True`. - - -``Enum`` classes with methods -""""""""""""""""""""""""""""" - -If you give your enum subclass extra methods, like the `Planet`_ -class above, those methods will show up in a :func:`dir` of the member, -but not of the class:: - - >>> dir(Planet) - ['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__members__', '__module__'] - >>> dir(Planet.EARTH) - ['__class__', '__doc__', '__module__', 'mass', 'name', 'radius', 'surface_gravity', 'value'] - - -Combining members of ``Flag`` -""""""""""""""""""""""""""""" - -Iterating over a combination of :class:`Flag` members will only return the members that -are comprised of a single bit:: - - >>> class Color(Flag): - ... RED = auto() - ... GREEN = auto() - ... BLUE = auto() - ... MAGENTA = RED | BLUE - ... YELLOW = RED | GREEN - ... CYAN = GREEN | BLUE - ... - >>> Color(3) # named combination - Color.YELLOW - >>> Color(7) # not named combination - Color.RED|Color.GREEN|Color.BLUE - -``StrEnum`` and :meth:`str.__str__` -""""""""""""""""""""""""""""""""""" - -An important difference between :class:`StrEnum` and other Enums is the -:meth:`__str__` method; because :class:`StrEnum` members are strings, some -parts of Python will read the string data directly, while others will call -:meth:`str()`. To make those two operations have the same result, -:meth:`StrEnum.__str__` will be the same as :meth:`str.__str__` so that -``str(StrEnum.member) == StrEnum.member`` is true. - -``Flag`` and ``IntFlag`` minutia -"""""""""""""""""""""""""""""""" - -Using the following snippet for our examples:: - - >>> class Color(IntFlag): - ... BLACK = 0 - ... RED = 1 - ... GREEN = 2 - ... BLUE = 4 - ... PURPLE = RED | BLUE - ... WHITE = RED | GREEN | BLUE - ... - -the following are true: - -- single-bit flags are canonical -- multi-bit and zero-bit flags are aliases -- only canonical flags are returned during iteration:: - - >>> list(Color.WHITE) - [Color.RED, Color.GREEN, Color.BLUE] - -- negating a flag or flag set returns a new flag/flag set with the - corresponding positive integer value:: - - >>> Color.BLUE - Color.BLUE - - >>> ~Color.BLUE - Color.RED|Color.GREEN - -- names of pseudo-flags are constructed from their members' names:: - - >>> (Color.RED | Color.GREEN).name - 'RED|GREEN' - -- multi-bit flags, aka aliases, can be returned from operations:: - - >>> Color.RED | Color.BLUE - Color.PURPLE - - >>> Color(7) # or Color(-1) - Color.WHITE - - >>> Color(0) - Color.BLACK - -- membership / containment checking has changed slightly -- zero valued flags - are never considered to be contained:: - - >>> Color.BLACK in Color.WHITE - False - - otherwise, if all bits of one flag are in the other flag, True is returned:: - - >>> Color.PURPLE in Color.WHITE - True - -There is a new boundary mechanism that controls how out-of-range / invalid -bits are handled: ``STRICT``, ``CONFORM``, ``EJECT``, and ``KEEP``: - - * STRICT --> raises an exception when presented with invalid values - * CONFORM --> discards any invalid bits - * EJECT --> lose Flag status and become a normal int with the given value - * KEEP --> keep the extra bits - - keeps Flag status and extra bits - - extra bits do not show up in iteration - - extra bits do show up in repr() and str() - -The default for Flag is ``STRICT``, the default for ``IntFlag`` is ``EJECT``, -and the default for ``_convert_`` is ``KEEP`` (see ``ssl.Options`` for an -example of when ``KEEP`` is needed). - - -.. _enum-class-differences: - -How are Enums different? ------------------------- - -Enums have a custom metaclass that affects many aspects of both derived :class:`Enum` -classes and their instances (members). - - -Enum Classes -^^^^^^^^^^^^ - -The :class:`EnumType` metaclass is responsible for providing the -:meth:`__contains__`, :meth:`__dir__`, :meth:`__iter__` and other methods that -allow one to do things with an :class:`Enum` class that fail on a typical -class, such as `list(Color)` or `some_enum_var in Color`. :class:`EnumType` is -responsible for ensuring that various other methods on the final :class:`Enum` -class are correct (such as :meth:`__new__`, :meth:`__getnewargs__`, -:meth:`__str__` and :meth:`__repr__`). - - -Enum Members (aka instances) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -The most interesting thing about enum members is that they are singletons. -:class:`EnumType` creates them all while it is creating the enum class itself, -and then puts a custom :meth:`__new__` in place to ensure that no new ones are -ever instantiated by returning only the existing member instances. - - -.. _enum-cookbook: - - -While :class:`Enum`, :class:`IntEnum`, :class:`StrEnum`, :class:`Flag`, and -:class:`IntFlag` are expected to cover the majority of use-cases, they cannot -cover them all. Here are recipes for some different types of enumerations -that can be used directly, or as examples for creating one's own. - - -Omitting values -^^^^^^^^^^^^^^^ - -In many use-cases one doesn't care what the actual value of an enumeration -is. There are several ways to define this type of simple enumeration: - -- use instances of :class:`auto` for the value -- use instances of :class:`object` as the value -- use a descriptive string as the value -- use a tuple as the value and a custom :meth:`__new__` to replace the - tuple with an :class:`int` value - -Using any of these methods signifies to the user that these values are not -important, and also enables one to add, remove, or reorder members without -having to renumber the remaining members. - - -Using :class:`auto` -""""""""""""""""""" - -Using :class:`auto` would look like:: - - >>> class Color(Enum): - ... RED = auto() - ... BLUE = auto() - ... GREEN = auto() - ... - >>> Color.GREEN - - - -Using :class:`object` -""""""""""""""""""""" - -Using :class:`object` would look like:: - - >>> class Color(Enum): - ... RED = object() - ... GREEN = object() - ... BLUE = object() - ... - >>> Color.GREEN - - - -Using a descriptive string -"""""""""""""""""""""""""" - -Using a string as the value would look like:: - - >>> class Color(Enum): - ... RED = 'stop' - ... GREEN = 'go' - ... BLUE = 'too fast!' - ... - >>> Color.GREEN - - >>> Color.GREEN.value - 'go' - - -Using a custom :meth:`__new__` -"""""""""""""""""""""""""""""" - -Using an auto-numbering :meth:`__new__` would look like:: - - >>> class AutoNumber(Enum): - ... def __new__(cls): - ... value = len(cls.__members__) + 1 - ... obj = object.__new__(cls) - ... obj._value_ = value - ... return obj - ... - >>> class Color(AutoNumber): - ... RED = () - ... GREEN = () - ... BLUE = () - ... - >>> Color.GREEN - - >>> Color.GREEN.value - 2 - -To make a more general purpose ``AutoNumber``, add ``*args`` to the signature:: - - >>> class AutoNumber(Enum): - ... def __new__(cls, *args): # this is the only change from above - ... value = len(cls.__members__) + 1 - ... obj = object.__new__(cls) - ... obj._value_ = value - ... return obj - ... - -Then when you inherit from ``AutoNumber`` you can write your own ``__init__`` -to handle any extra arguments:: - - >>> class Swatch(AutoNumber): - ... def __init__(self, pantone='unknown'): - ... self.pantone = pantone - ... AUBURN = '3497' - ... SEA_GREEN = '1246' - ... BLEACHED_CORAL = () # New color, no Pantone code yet! - ... - >>> Swatch.SEA_GREEN - - >>> Swatch.SEA_GREEN.pantone - '1246' - >>> Swatch.BLEACHED_CORAL.pantone - 'unknown' - -.. note:: - - The :meth:`__new__` method, if defined, is used during creation of the Enum - members; it is then replaced by Enum's :meth:`__new__` which is used after - class creation for lookup of existing members. - - -OrderedEnum -^^^^^^^^^^^ - -An ordered enumeration that is not based on :class:`IntEnum` and so maintains -the normal :class:`Enum` invariants (such as not being comparable to other -enumerations):: - - >>> class OrderedEnum(Enum): - ... def __ge__(self, other): - ... if self.__class__ is other.__class__: - ... return self.value >= other.value - ... return NotImplemented - ... def __gt__(self, other): - ... if self.__class__ is other.__class__: - ... return self.value > other.value - ... return NotImplemented - ... def __le__(self, other): - ... if self.__class__ is other.__class__: - ... return self.value <= other.value - ... return NotImplemented - ... def __lt__(self, other): - ... if self.__class__ is other.__class__: - ... return self.value < other.value - ... return NotImplemented - ... - >>> class Grade(OrderedEnum): - ... A = 5 - ... B = 4 - ... C = 3 - ... D = 2 - ... F = 1 - ... - >>> Grade.C < Grade.A - True - - -DuplicateFreeEnum -^^^^^^^^^^^^^^^^^ - -Raises an error if a duplicate member name is found instead of creating an -alias:: - - >>> class DuplicateFreeEnum(Enum): - ... def __init__(self, *args): - ... cls = self.__class__ - ... if any(self.value == e.value for e in cls): - ... a = self.name - ... e = cls(self.value).name - ... raise ValueError( - ... "aliases not allowed in DuplicateFreeEnum: %r --> %r" - ... % (a, e)) - ... - >>> class Color(DuplicateFreeEnum): - ... RED = 1 - ... GREEN = 2 - ... BLUE = 3 - ... GRENE = 2 - ... - Traceback (most recent call last): - ... - ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN' - -.. note:: - - This is a useful example for subclassing Enum to add or change other - behaviors as well as disallowing aliases. If the only desired change is - disallowing aliases, the :func:`unique` decorator can be used instead. - - -Planet -^^^^^^ - -If :meth:`__new__` or :meth:`__init__` is defined the value of the enum member -will be passed to those methods:: - - >>> class Planet(Enum): - ... MERCURY = (3.303e+23, 2.4397e6) - ... VENUS = (4.869e+24, 6.0518e6) - ... EARTH = (5.976e+24, 6.37814e6) - ... MARS = (6.421e+23, 3.3972e6) - ... JUPITER = (1.9e+27, 7.1492e7) - ... SATURN = (5.688e+26, 6.0268e7) - ... URANUS = (8.686e+25, 2.5559e7) - ... NEPTUNE = (1.024e+26, 2.4746e7) - ... def __init__(self, mass, radius): - ... self.mass = mass # in kilograms - ... self.radius = radius # in meters - ... @property - ... def surface_gravity(self): - ... # universal gravitational constant (m3 kg-1 s-2) - ... G = 6.67300E-11 - ... return G * self.mass / (self.radius * self.radius) - ... - >>> Planet.EARTH.value - (5.976e+24, 6378140.0) - >>> Planet.EARTH.surface_gravity - 9.802652743337129 - -.. _enum-time-period: - -TimePeriod -^^^^^^^^^^ - -An example to show the :attr:`_ignore_` attribute in use:: - - >>> from datetime import timedelta - >>> class Period(timedelta, Enum): - ... "different lengths of time" - ... _ignore_ = 'Period i' - ... Period = vars() - ... for i in range(367): - ... Period['day_%d' % i] = i - ... - >>> list(Period)[:2] - [Period.day_0, Period.day_1] - >>> list(Period)[-2:] - [Period.day_365, Period.day_366] - - -Conforming input to Flag -^^^^^^^^^^^^^^^^^^^^^^^^ - -Creating a :class:`Flag` enum that is more resilient out-of-bounds results to -mathematical operations, you can use the :attr:`FlagBoundary.CONFORM` setting:: - - >>> from enum import Flag, CONFORM, auto - >>> class Weekday(Flag, boundary=CONFORM): - ... MONDAY = auto() - ... TUESDAY = auto() - ... WEDNESDAY = auto() - ... THURSDAY = auto() - ... FRIDAY = auto() - ... SATURDAY = auto() - ... SUNDAY = auto() - >>> today = Weekday.TUESDAY - >>> Weekday(today + 22) # what day is three weeks from tomorrow? - >>> Weekday.WEDNESDAY - - -.. _enumtype-examples: - -Subclassing EnumType --------------------- - -While most enum needs can be met by customizing :class:`Enum` subclasses, -either with class decorators or custom functions, :class:`EnumType` can be -subclassed to provide a different Enum experience. - diff --git a/Doc/howto/functional.rst b/Doc/howto/functional.rst index 74e861480d2ff8..695b9b31a762bd 100644 --- a/Doc/howto/functional.rst +++ b/Doc/howto/functional.rst @@ -65,11 +65,10 @@ output must only depend on its input. Some languages are very strict about purity and don't even have assignment statements such as ``a=3`` or ``c = a + b``, but it's difficult to avoid all -side effects. Printing to the screen or writing to a disk file are side -effects, for example. For example, in Python a call to the :func:`print` or -:func:`time.sleep` function both return no useful value; they're only called for -their side effects of sending some text to the screen or pausing execution for a -second. +side effects, such as printing to the screen or writing to a disk file. Another +example is a call to the :func:`print` or :func:`time.sleep` function, neither +of which returns a useful value. Both are called only for their side effects +of sending some text to the screen or pausing execution for a second. Python programs written in functional style usually won't go to the extreme of avoiding all I/O or all assignments; instead, they'll provide a @@ -590,7 +589,7 @@ generator function. In addition to :meth:`~generator.send`, there are two other methods on generators: -* :meth:`throw(type, value=None, traceback=None) ` is used to +* :meth:`throw(value) ` is used to raise an exception inside the generator; the exception is raised by the ``yield`` expression where the generator's execution is paused. diff --git a/Doc/howto/index.rst b/Doc/howto/index.rst index eae8f143ee206f..01a78a556591c9 100644 --- a/Doc/howto/index.rst +++ b/Doc/howto/index.rst @@ -17,7 +17,6 @@ Currently, the HOWTOs are: cporting.rst curses.rst descriptor.rst - enum.rst functional.rst logging.rst logging-cookbook.rst diff --git a/Doc/howto/ipaddress.rst b/Doc/howto/ipaddress.rst index 452e3671060a5e..e852db98156fac 100644 --- a/Doc/howto/ipaddress.rst +++ b/Doc/howto/ipaddress.rst @@ -32,7 +32,7 @@ A Note on IP Versions --------------------- For readers that aren't particularly familiar with IP addressing, it's -important to know that the Internet Protocol is currently in the process +important to know that the Internet Protocol (IP) is currently in the process of moving from version 4 of the protocol to version 6. This transition is occurring largely because version 4 of the protocol doesn't provide enough addresses to handle the needs of the whole world, especially given the diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst index 5777a4c5031f85..2bf9df847b7d99 100644 --- a/Doc/howto/logging-cookbook.rst +++ b/Doc/howto/logging-cookbook.rst @@ -332,7 +332,7 @@ Dealing with handlers that block .. currentmodule:: logging.handlers Sometimes you have to get your logging handlers to do their work without -blocking the thread you're logging from. This is common in Web applications, +blocking the thread you're logging from. This is common in web applications, though of course it also occurs in other scenarios. A common culprit which demonstrates sluggish behaviour is the @@ -541,6 +541,17 @@ alternative there, as well as adapting the above script to use your alternative serialization. +Running a logging socket listener in production +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +To run a logging listener in production, you may need to use a process-management tool +such as `Supervisor `_. `Here +`_ is a Gist which +provides the bare-bones files to run the above functionality using Supervisor: you +will need to change the `/path/to/` parts in the Gist to reflect the actual paths you +want to use. + + .. _context-info: Adding contextual information to your logging output @@ -982,6 +993,17 @@ to this (remembering to first import :mod:`concurrent.futures`):: for i in range(10): executor.submit(worker_process, queue, worker_configurer) +Deploying Web applications using Gunicorn and uWSGI +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When deploying Web applications using `Gunicorn `_ or `uWSGI +`_ (or similar), multiple worker +processes are created to handle client requests. In such environments, avoid creating +file-based handlers directly in your web application. Instead, use a +:class:`SocketHandler` to log from the web application to a listener in a separate +process. This can be set up using a process management tool such as Supervisor - see +`Running a logging socket listener in production`_ for more details. + Using file rotation ------------------- @@ -2979,3 +3001,171 @@ refer to the comments in the code snippet for more detailed information. if __name__=='__main__': main() + +Logging to syslog with RFC5424 support +-------------------------------------- + +Although :rfc:`5424` dates from 2009, most syslog servers are configured by detault to +use the older :rfc:`3164`, which hails from 2001. When ``logging`` was added to Python +in 2003, it supported the earlier (and only existing) protocol at the time. Since +RFC5424 came out, as there has not been widespread deployment of it in syslog +servers, the :class:`~logging.handlers.SysLogHandler` functionality has not been +updated. + +RFC 5424 contains some useful features such as support for structured data, and if you +need to be able to log to a syslog server with support for it, you can do so with a +subclassed handler which looks something like this:: + + import datetime + import logging.handlers + import re + import socket + import time + + class SysLogHandler5424(logging.handlers.SysLogHandler): + + tz_offset = re.compile(r'([+-]\d{2})(\d{2})$') + escaped = re.compile(r'([\]"\\])') + + def __init__(self, *args, **kwargs): + self.msgid = kwargs.pop('msgid', None) + self.appname = kwargs.pop('appname', None) + super().__init__(*args, **kwargs) + + def format(self, record): + version = 1 + asctime = datetime.datetime.fromtimestamp(record.created).isoformat() + m = self.tz_offset.match(time.strftime('%z')) + has_offset = False + if m and time.timezone: + hrs, mins = m.groups() + if int(hrs) or int(mins): + has_offset = True + if not has_offset: + asctime += 'Z' + else: + asctime += f'{hrs}:{mins}' + try: + hostname = socket.gethostname() + except Exception: + hostname = '-' + appname = self.appname or '-' + procid = record.process + msgid = '-' + msg = super().format(record) + sdata = '-' + if hasattr(record, 'structured_data'): + sd = record.structured_data + # This should be a dict where the keys are SD-ID and the value is a + # dict mapping PARAM-NAME to PARAM-VALUE (refer to the RFC for what these + # mean) + # There's no error checking here - it's purely for illustration, and you + # can adapt this code for use in production environments + parts = [] + + def replacer(m): + g = m.groups() + return '\\' + g[0] + + for sdid, dv in sd.items(): + part = f'[{sdid}' + for k, v in dv.items(): + s = str(v) + s = self.escaped.sub(replacer, s) + part += f' {k}="{s}"' + part += ']' + parts.append(part) + sdata = ''.join(parts) + return f'{version} {asctime} {hostname} {appname} {procid} {msgid} {sdata} {msg}' + +You'll need to be familiar with RFC 5424 to fully understand the above code, and it +may be that you have slightly different needs (e.g. for how you pass structural data +to the log). Nevertheless, the above should be adaptable to your speciric needs. With +the above handler, you'd pass structured data using something like this:: + + sd = { + 'foo@12345': {'bar': 'baz', 'baz': 'bozz', 'fizz': r'buzz'}, + 'foo@54321': {'rab': 'baz', 'zab': 'bozz', 'zzif': r'buzz'} + } + extra = {'structured_data': sd} + i = 1 + logger.debug('Message %d', i, extra=extra) + + +.. patterns-to-avoid: + +Patterns to avoid +----------------- + +Although the preceding sections have described ways of doing things you might +need to do or deal with, it is worth mentioning some usage patterns which are +*unhelpful*, and which should therefore be avoided in most cases. The following +sections are in no particular order. + + +Opening the same log file multiple times +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +On Windows, you will generally not be able to open the same file multiple times +as this will lead to a "file is in use by another process" error. However, on +POSIX platforms you'll not get any errors if you open the same file multiple +times. This could be done accidentally, for example by: + +* Adding a file handler more than once which references the same file (e.g. by + a copy/paste/forget-to-change error). + +* Opening two files that look different, as they have different names, but are + the same because one is a symbolic link to the other. + +* Forking a process, following which both parent and child have a reference to + the same file. This might be through use of the :mod:`multiprocessing` module, + for example. + +Opening a file multiple times might *appear* to work most of the time, but can +lead to a number of problems in practice: + +* Logging output can be garbled because multiple threads or processes try to + write to the same file. Although logging guards against concurrent use of the + same handler instance by multiple threads, there is no such protection if + concurrent writes are attempted by two different threads using two different + handler instances which happen to point to the same file. + +* An attempt to delete a file (e.g. during file rotation) silently fails, + because there is another reference pointing to it. This can lead to confusion + and wasted debugging time - log entries end up in unexpected places, or are + lost altogether. + +Use the techniques outlined in :ref:`multiple-processes` to circumvent such +issues. + +Using loggers as attributes in a class or passing them as parameters +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +While there might be unusual cases where you'll need to do this, in general +there is no point because loggers are singletons. Code can always access a +given logger instance by name using ``logging.getLogger(name)``, so passing +instances around and holding them as instance attributes is pointless. Note +that in other languages such as Java and C#, loggers are often static class +attributes. However, this pattern doesn't make sense in Python, where the +module (and not the class) is the unit of software decomposition. + + +Adding handlers other than :class:`NullHandler` to a logger in a library +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Configuring logging by adding handlers, formatters and filters is the +responsibility of the application developer, not the library developer. If you +are maintaining a library, ensure that you don't add handlers to any of your +loggers other than a :class:`~logging.NullHandler` instance. + + +Creating a lot of loggers +^^^^^^^^^^^^^^^^^^^^^^^^^ + +Loggers are singletons that are never freed during a script execution, and so +creating lots of loggers will use up memory which can't then be freed. Rather +than create a logger per e.g. file processed or network connection made, use +the :ref:`existing mechanisms ` for passing contextual +information into your logs and restrict the loggers created to those describing +areas within your application (generally modules, but occasionally slightly +more fine-grained than that). diff --git a/Doc/howto/logging.rst b/Doc/howto/logging.rst index fcc6bec7688002..ff4e1d73a96531 100644 --- a/Doc/howto/logging.rst +++ b/Doc/howto/logging.rst @@ -178,10 +178,11 @@ following example:: raise ValueError('Invalid log level: %s' % loglevel) logging.basicConfig(level=numeric_level, ...) -The call to :func:`basicConfig` should come *before* any calls to :func:`debug`, -:func:`info` etc. As it's intended as a one-off simple configuration facility, -only the first call will actually do anything: subsequent calls are effectively -no-ops. +The call to :func:`basicConfig` should come *before* any calls to +:func:`debug`, :func:`info`, etc. Otherwise, those functions will call +:func:`basicConfig` for you with the default options. As it's intended as a +one-off simple configuration facility, only the first call will actually do +anything: subsequent calls are effectively no-ops. If you run the above script several times, the messages from successive runs are appended to the file *example.log*. If you want each run to start afresh, diff --git a/Doc/howto/logging_flow.png b/Doc/howto/logging_flow.png index fac4acd7755302..d65e597f811db5 100644 Binary files a/Doc/howto/logging_flow.png and b/Doc/howto/logging_flow.png differ diff --git a/Doc/howto/pyporting.rst b/Doc/howto/pyporting.rst index 1543823c104c28..abcc34287e3d29 100644 --- a/Doc/howto/pyporting.rst +++ b/Doc/howto/pyporting.rst @@ -20,8 +20,8 @@ Porting Python 2 Code to Python 3 came into existence, you can read Nick Coghlan's `Python 3 Q & A`_ or Brett Cannon's `Why Python 3 exists`_. - For help with porting, you can email the python-porting_ mailing list with - questions. + + For help with porting, you can view the archived python-porting_ mailing list. The Short Explanation ===================== @@ -446,7 +446,7 @@ to make sure everything functions as expected in both versions of Python. .. _pytype: https://github.com/google/pytype .. _python-future: http://python-future.org/ -.. _python-porting: https://mail.python.org/mailman/listinfo/python-porting +.. _python-porting: https://mail.python.org/pipermail/python-porting/ .. _six: https://pypi.org/project/six .. _tox: https://pypi.org/project/tox .. _trove classifier: https://pypi.org/classifiers diff --git a/Doc/howto/regex.rst b/Doc/howto/regex.rst index d574c3736b1cb7..c4ebbd311e0fe3 100644 --- a/Doc/howto/regex.rst +++ b/Doc/howto/regex.rst @@ -89,7 +89,7 @@ is the same as ``[a-c]``, which uses a range to express the same set of characters. If you wanted to match only lowercase letters, your RE would be ``[a-z]``. -Metacharacters are not active inside classes. For example, ``[akm$]`` will +Metacharacters (except ``\``) are not active inside classes. For example, ``[akm$]`` will match any of the characters ``'a'``, ``'k'``, ``'m'``, or ``'$'``; ``'$'`` is usually a metacharacter, but inside a character class it's stripped of its special nature. diff --git a/Doc/howto/sockets.rst b/Doc/howto/sockets.rst index d6ed128e073fd6..0bbf97da39768d 100644 --- a/Doc/howto/sockets.rst +++ b/Doc/howto/sockets.rst @@ -45,7 +45,7 @@ likely to be other forms of IPC that are faster, but for cross-platform communication, sockets are about the only game in town. They were invented in Berkeley as part of the BSD flavor of Unix. They spread -like wildfire with the Internet. With good reason --- the combination of sockets +like wildfire with the internet. With good reason --- the combination of sockets with INET makes talking to arbitrary machines around the world unbelievably easy (at least compared to other schemes). @@ -252,20 +252,25 @@ Binary Data ----------- It is perfectly possible to send binary data over a socket. The major problem is -that not all machines use the same formats for binary data. For example, a -Motorola chip will represent a 16 bit integer with the value 1 as the two hex -bytes 00 01. Intel and DEC, however, are byte-reversed - that same 1 is 01 00. +that not all machines use the same formats for binary data. For example, +`network byte order `_ +is big-endian, with the most significant byte first, +so a 16 bit integer with the value ``1`` would be the two hex bytes ``00 01``. +However, most common processors (x86/AMD64, ARM, RISC-V), are little-endian, +with the least significant byte first - that same ``1`` would be ``01 00``. + Socket libraries have calls for converting 16 and 32 bit integers - ``ntohl, htonl, ntohs, htons`` where "n" means *network* and "h" means *host*, "s" means *short* and "l" means *long*. Where network order is host order, these do nothing, but where the machine is byte-reversed, these swap the bytes around appropriately. -In these days of 32 bit machines, the ascii representation of binary data is +In these days of 64-bit machines, the ASCII representation of binary data is frequently smaller than the binary representation. That's because a surprising -amount of the time, all those longs have the value 0, or maybe 1. The string "0" -would be two bytes, while binary is four. Of course, this doesn't fit well with -fixed-length messages. Decisions, decisions. +amount of the time, most integers have the value 0, or maybe 1. +The string ``"0"`` would be two bytes, while a full 64-bit integer would be 8. +Of course, this doesn't fit well with fixed-length messages. +Decisions, decisions. Disconnecting diff --git a/Doc/howto/sorting.rst b/Doc/howto/sorting.rst index 37328c82dff270..32b47711f85705 100644 --- a/Doc/howto/sorting.rst +++ b/Doc/howto/sorting.rst @@ -325,7 +325,7 @@ Odd and Ends >>> standard_way [('red', 1), ('red', 2), ('blue', 1), ('blue', 2)] -* The sort routines are guaranteed to use :meth:`__lt__` when making comparisons +* The sort routines use ``<`` when making comparisons between two objects. So, it is easy to add a standard sort order to a class by defining an :meth:`__lt__` method: @@ -335,6 +335,9 @@ Odd and Ends >>> sorted(student_objects) [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] + However, note that ``<`` can fall back to using :meth:`__gt__` if + :meth:`__lt__` is not implemented (see :func:`object.__lt__`). + * Key functions need not depend directly on the objects being sorted. A key function can also access external resources. For instance, if the student grades are stored in a dictionary, they can be used to sort a separate list of student diff --git a/Doc/includes/custom.c b/Doc/includes/custom.c index f361baf830dd1b..26ca754964733d 100644 --- a/Doc/includes/custom.c +++ b/Doc/includes/custom.c @@ -9,7 +9,7 @@ typedef struct { static PyTypeObject CustomType = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "custom.Custom", - .tp_doc = "Custom objects", + .tp_doc = PyDoc_STR("Custom objects"), .tp_basicsize = sizeof(CustomObject), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT, diff --git a/Doc/includes/custom2.c b/Doc/includes/custom2.c index 5bacab7a2a9714..2a3c59f8f04c3d 100644 --- a/Doc/includes/custom2.c +++ b/Doc/includes/custom2.c @@ -98,7 +98,7 @@ static PyMethodDef Custom_methods[] = { static PyTypeObject CustomType = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "custom2.Custom", - .tp_doc = "Custom objects", + .tp_doc = PyDoc_STR("Custom objects"), .tp_basicsize = sizeof(CustomObject), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, diff --git a/Doc/includes/custom3.c b/Doc/includes/custom3.c index 2b7a99ecf96c76..5a47530f0a6b0d 100644 --- a/Doc/includes/custom3.c +++ b/Doc/includes/custom3.c @@ -148,7 +148,7 @@ static PyMethodDef Custom_methods[] = { static PyTypeObject CustomType = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "custom3.Custom", - .tp_doc = "Custom objects", + .tp_doc = PyDoc_STR("Custom objects"), .tp_basicsize = sizeof(CustomObject), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, diff --git a/Doc/includes/custom4.c b/Doc/includes/custom4.c index 584992fc5f1a8a..c7ee55578488ed 100644 --- a/Doc/includes/custom4.c +++ b/Doc/includes/custom4.c @@ -160,7 +160,7 @@ static PyMethodDef Custom_methods[] = { static PyTypeObject CustomType = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "custom4.Custom", - .tp_doc = "Custom objects", + .tp_doc = PyDoc_STR("Custom objects"), .tp_basicsize = sizeof(CustomObject), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, diff --git a/Doc/includes/sqlite3/countcursors.py b/Doc/includes/sqlite3/countcursors.py deleted file mode 100644 index 112f47703a2ff4..00000000000000 --- a/Doc/includes/sqlite3/countcursors.py +++ /dev/null @@ -1,17 +0,0 @@ -import sqlite3 - -class CountCursorsConnection(sqlite3.Connection): - def __init__(self, *args, **kwargs): - sqlite3.Connection.__init__(self, *args, **kwargs) - self.numcursors = 0 - - def cursor(self, *args, **kwargs): - self.numcursors += 1 - return sqlite3.Connection.cursor(self, *args, **kwargs) - -con = sqlite3.connect(":memory:", factory=CountCursorsConnection) -cur1 = con.cursor() -cur2 = con.cursor() -print(con.numcursors) - -con.close() diff --git a/Doc/includes/sqlite3/createdb.py b/Doc/includes/sqlite3/createdb.py deleted file mode 100644 index ee2950bdf81646..00000000000000 --- a/Doc/includes/sqlite3/createdb.py +++ /dev/null @@ -1,28 +0,0 @@ -# Not referenced from the documentation, but builds the database file the other -# code snippets expect. - -import sqlite3 -import os - -DB_FILE = "mydb" - -if os.path.exists(DB_FILE): - os.remove(DB_FILE) - -con = sqlite3.connect(DB_FILE) -cur = con.cursor() -cur.execute(""" - create table people - ( - name_last varchar(20), - age integer - ) - """) - -cur.execute("insert into people (name_last, age) values ('Yeltsin', 72)") -cur.execute("insert into people (name_last, age) values ('Putin', 51)") - -con.commit() - -cur.close() -con.close() diff --git a/Doc/includes/sqlite3/ctx_manager.py b/Doc/includes/sqlite3/ctx_manager.py index 6db77d45046e1f..2e1175ef44c641 100644 --- a/Doc/includes/sqlite3/ctx_manager.py +++ b/Doc/includes/sqlite3/ctx_manager.py @@ -1,19 +1,19 @@ import sqlite3 con = sqlite3.connect(":memory:") -con.execute("create table person (id integer primary key, firstname varchar unique)") +con.execute("create table lang (id integer primary key, name varchar unique)") # Successful, con.commit() is called automatically afterwards with con: - con.execute("insert into person(firstname) values (?)", ("Joe",)) + con.execute("insert into lang(name) values (?)", ("Python",)) # con.rollback() is called after the with block finishes with an exception, the # exception is still raised and must be caught try: with con: - con.execute("insert into person(firstname) values (?)", ("Joe",)) + con.execute("insert into lang(name) values (?)", ("Python",)) except sqlite3.IntegrityError: - print("couldn't add Joe twice") + print("couldn't add Python twice") # Connection object used as context manager only commits or rollbacks transactions, # so the connection object should be closed manually diff --git a/Doc/includes/sqlite3/execsql_fetchonerow.py b/Doc/includes/sqlite3/execsql_fetchonerow.py deleted file mode 100644 index 115bcb50c7c754..00000000000000 --- a/Doc/includes/sqlite3/execsql_fetchonerow.py +++ /dev/null @@ -1,19 +0,0 @@ -import sqlite3 - -con = sqlite3.connect("mydb") - -cur = con.cursor() -SELECT = "select name_last, age from people order by age, name_last" - -# 1. Iterate over the rows available from the cursor, unpacking the -# resulting sequences to yield their elements (name_last, age): -cur.execute(SELECT) -for (name_last, age) in cur: - print('%s is %d years old.' % (name_last, age)) - -# 2. Equivalently: -cur.execute(SELECT) -for row in cur: - print('%s is %d years old.' % (row[0], row[1])) - -con.close() diff --git a/Doc/includes/sqlite3/execsql_printall_1.py b/Doc/includes/sqlite3/execsql_printall_1.py deleted file mode 100644 index 19306e6e3ca7d1..00000000000000 --- a/Doc/includes/sqlite3/execsql_printall_1.py +++ /dev/null @@ -1,15 +0,0 @@ -import sqlite3 - -# Create a connection to the database file "mydb": -con = sqlite3.connect("mydb") - -# Get a Cursor object that operates in the context of Connection con: -cur = con.cursor() - -# Execute the SELECT statement: -cur.execute("select * from people order by age") - -# Retrieve all rows as a sequence and print that sequence: -print(cur.fetchall()) - -con.close() diff --git a/Doc/includes/sqlite3/execute_1.py b/Doc/includes/sqlite3/execute_1.py index 42aad4d5839f06..ee0000e2b94a32 100644 --- a/Doc/includes/sqlite3/execute_1.py +++ b/Doc/includes/sqlite3/execute_1.py @@ -2,22 +2,21 @@ con = sqlite3.connect(":memory:") cur = con.cursor() -cur.execute("create table lang (lang_name, lang_age)") +cur.execute("create table lang (name, first_appeared)") # This is the qmark style: -cur.execute("insert into lang values (?, ?)", ("C", 49)) +cur.execute("insert into lang values (?, ?)", ("C", 1972)) # The qmark style used with executemany(): lang_list = [ - ("Fortran", 64), - ("Python", 30), - ("Go", 11), + ("Fortran", 1957), + ("Python", 1991), + ("Go", 2009), ] cur.executemany("insert into lang values (?, ?)", lang_list) # And this is the named style: -cur.execute("select * from lang where lang_name=:name and lang_age=:age", - {"name": "C", "age": 49}) +cur.execute("select * from lang where first_appeared=:year", {"year": 1972}) print(cur.fetchall()) con.close() diff --git a/Doc/includes/sqlite3/insert_more_people.py b/Doc/includes/sqlite3/insert_more_people.py deleted file mode 100644 index 10cf937243f6da..00000000000000 --- a/Doc/includes/sqlite3/insert_more_people.py +++ /dev/null @@ -1,18 +0,0 @@ -import sqlite3 - -con = sqlite3.connect("mydb") - -cur = con.cursor() - -newPeople = ( - ('Lebed' , 53), - ('Zhirinovsky' , 57), - ) - -for person in newPeople: - cur.execute("insert into people (name_last, age) values (?, ?)", person) - -# The changes will not be saved unless the transaction is committed explicitly: -con.commit() - -con.close() diff --git a/Doc/includes/sqlite3/parse_colnames.py b/Doc/includes/sqlite3/parse_colnames.py deleted file mode 100644 index 5f01dbfe1cb524..00000000000000 --- a/Doc/includes/sqlite3/parse_colnames.py +++ /dev/null @@ -1,10 +0,0 @@ -import sqlite3 -import datetime - -con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES) -cur = con.cursor() -cur.execute('select ? as "x [timestamp]"', (datetime.datetime.now(),)) -dt = cur.fetchone()[0] -print(dt, type(dt)) - -con.close() diff --git a/Doc/includes/sqlite3/shared_cache.py b/Doc/includes/sqlite3/shared_cache.py deleted file mode 100644 index 30e71c935ff62e..00000000000000 --- a/Doc/includes/sqlite3/shared_cache.py +++ /dev/null @@ -1,6 +0,0 @@ -import sqlite3 - -# The shared cache is only available in SQLite versions 3.3.3 or later -# See the SQLite documentation for details. - -sqlite3.enable_shared_cache(True) diff --git a/Doc/includes/sqlite3/shortcut_methods.py b/Doc/includes/sqlite3/shortcut_methods.py index 98a39411495cba..48ea6fad15a898 100644 --- a/Doc/includes/sqlite3/shortcut_methods.py +++ b/Doc/includes/sqlite3/shortcut_methods.py @@ -1,23 +1,23 @@ import sqlite3 -persons = [ - ("Hugo", "Boss"), - ("Calvin", "Klein") - ] +langs = [ + ("C++", 1985), + ("Objective-C", 1984), +] con = sqlite3.connect(":memory:") # Create the table -con.execute("create table person(firstname, lastname)") +con.execute("create table lang(name, first_appeared)") # Fill the table -con.executemany("insert into person(firstname, lastname) values (?, ?)", persons) +con.executemany("insert into lang(name, first_appeared) values (?, ?)", langs) # Print the table contents -for row in con.execute("select firstname, lastname from person"): +for row in con.execute("select name, first_appeared from lang"): print(row) -print("I just deleted", con.execute("delete from person").rowcount, "rows") +print("I just deleted", con.execute("delete from lang").rowcount, "rows") # close is not a shortcut method and it's not called automatically, # so the connection object should be closed manually diff --git a/Doc/includes/sqlite3/simple_tableprinter.py b/Doc/includes/sqlite3/simple_tableprinter.py deleted file mode 100644 index 148a1707f948bc..00000000000000 --- a/Doc/includes/sqlite3/simple_tableprinter.py +++ /dev/null @@ -1,28 +0,0 @@ -import sqlite3 - -FIELD_MAX_WIDTH = 20 -TABLE_NAME = 'people' -SELECT = 'select * from %s order by age, name_last' % TABLE_NAME - -con = sqlite3.connect("mydb") - -cur = con.cursor() -cur.execute(SELECT) - -# Print a header. -for fieldDesc in cur.description: - print(fieldDesc[0].ljust(FIELD_MAX_WIDTH), end=' ') -print() # Finish the header with a newline. -print('-' * 78) - -# For each row, print the value of each field left-justified within -# the maximum possible width of that field. -fieldIndices = range(len(cur.description)) -for row in cur: - for fieldIndex in fieldIndices: - fieldValue = str(row[fieldIndex]) - print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ') - - print() # Finish the row with a newline. - -con.close() diff --git a/Doc/includes/sqlite3/text_factory.py b/Doc/includes/sqlite3/text_factory.py index a857a155cdd4ff..c0d87cd559118c 100644 --- a/Doc/includes/sqlite3/text_factory.py +++ b/Doc/includes/sqlite3/text_factory.py @@ -3,9 +3,9 @@ con = sqlite3.connect(":memory:") cur = con.cursor() -AUSTRIA = "\xd6sterreich" +AUSTRIA = "Österreich" -# by default, rows are returned as Unicode +# by default, rows are returned as str cur.execute("select ?", (AUSTRIA,)) row = cur.fetchone() assert row[0] == AUSTRIA diff --git a/Doc/includes/sublist.c b/Doc/includes/sublist.c index b2c26e73ebaf7e..b36dadf07eae87 100644 --- a/Doc/includes/sublist.c +++ b/Doc/includes/sublist.c @@ -31,7 +31,7 @@ SubList_init(SubListObject *self, PyObject *args, PyObject *kwds) static PyTypeObject SubListType = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "sublist.SubList", - .tp_doc = "SubList objects", + .tp_doc = PyDoc_STR("SubList objects"), .tp_basicsize = sizeof(SubListObject), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, diff --git a/Doc/install/index.rst b/Doc/install/index.rst index 48c6e76a682a46..7f7be117009887 100644 --- a/Doc/install/index.rst +++ b/Doc/install/index.rst @@ -206,7 +206,7 @@ directory. If you don't choose an installation directory---i.e., if you just run ``setup.py install``\ ---then the :command:`install` command installs to the standard location for third-party Python modules. This location varies by platform and -by how you built/installed Python itself. On Unix (and Mac OS X, which is also +by how you built/installed Python itself. On Unix (and macOS, which is also Unix-based), it also depends on whether the module distribution being installed is pure Python or contains extensions ("non-pure"): @@ -236,7 +236,7 @@ Notes: :file:`{prefix}` and :file:`{exec-prefix}` stand for the directories that Python is installed to, and where it finds its libraries at run-time. They are always -the same under Windows, and very often the same under Unix and Mac OS X. You +the same under Windows, and very often the same under Unix and macOS. You can find out what your Python installation uses for :file:`{prefix}` and :file:`{exec-prefix}` by running Python in interactive mode and typing a few simple commands. Under Unix, just type ``python`` at the shell prompt. Under @@ -312,7 +312,7 @@ install into it. It is enabled with a simple option:: Files will be installed into subdirectories of :data:`site.USER_BASE` (written as :file:`{userbase}` hereafter). This scheme installs pure Python modules and extension modules in the same location (also known as :data:`site.USER_SITE`). -Here are the values for UNIX, including Mac OS X: +Here are the values for UNIX, including macOS: =============== =========================================================== Type of file Installation directory @@ -735,7 +735,7 @@ Location and names of config files ---------------------------------- The names and locations of the configuration files vary slightly across -platforms. On Unix and Mac OS X, the three configuration files (in the order +platforms. On Unix and macOS, the three configuration files (in the order they are processed) are: +--------------+----------------------------------------------------------+-------+ @@ -953,7 +953,7 @@ Borland/CodeGear C++ This subsection describes the necessary steps to use Distutils with the Borland C++ compiler version 5.5. First you have to know that Borland's object file format (OMF) is different from the format used by the Python version you can -download from the Python or ActiveState Web site. (Python is built with +download from the Python or ActiveState web site. (Python is built with Microsoft Visual C++, which uses COFF as the object file format.) For this reason you have to convert Python's library :file:`python25.lib` into the Borland format. You can do this as follows: diff --git a/Doc/installing/index.rst b/Doc/installing/index.rst index 31e9b0bde07244..4bacc7ba0c2cf2 100644 --- a/Doc/installing/index.rst +++ b/Doc/installing/index.rst @@ -44,7 +44,7 @@ Key terms ``venv``. It allows virtual environments to be used on versions of Python prior to 3.4, which either don't provide ``venv`` at all, or aren't able to automatically install ``pip`` into created environments. -* The `Python Packaging Index `__ is a public +* The `Python Package Index `__ is a public repository of open source licensed packages made available for use by other Python users. * the `Python Packaging Authority @@ -78,13 +78,13 @@ The standard packaging tools are all designed to be used from the command line. The following command will install the latest version of a module and its -dependencies from the Python Packaging Index:: +dependencies from the Python Package Index:: python -m pip install SomePackage .. note:: - For POSIX users (including Mac OS X and Linux users), the examples in + For POSIX users (including macOS and Linux users), the examples in this guide assume the use of a :term:`virtual environment`. For Windows users, the examples in this guide assume that the option to @@ -163,7 +163,7 @@ rather than attempting to install them with ``pip``. ... work with multiple versions of Python installed in parallel? ---------------------------------------------------------------- -On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands +On Linux, macOS, and other POSIX systems, use the versioned Python commands in combination with the ``-m`` switch to run the appropriate copy of ``pip``:: @@ -225,8 +225,8 @@ users being expected to compile extension modules from source as part of the installation process. With the introduction of support for the binary ``wheel`` format, and the -ability to publish wheels for at least Windows and Mac OS X through the -Python Packaging Index, this problem is expected to diminish over time, +ability to publish wheels for at least Windows and macOS through the +Python Package Index, this problem is expected to diminish over time, as users are more regularly able to install pre-built extensions rather than needing to build them themselves. diff --git a/Doc/library/2to3.rst b/Doc/library/2to3.rst index 1d7bd262872905..5e1b010e9bb5f2 100644 --- a/Doc/library/2to3.rst +++ b/Doc/library/2to3.rst @@ -333,7 +333,8 @@ and off individually. They are described here in more detail. .. 2to3fixer:: nonzero - Renames :meth:`__nonzero__` to :meth:`~object.__bool__`. + Renames definitions of methods called :meth:`__nonzero__` + to :meth:`~object.__bool__`. .. 2to3fixer:: numliterals @@ -464,12 +465,15 @@ and off individually. They are described here in more detail. -------------- -.. deprecated:: 3.10 - Python 3.9 will switch 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. - The ``lib2to3`` module may be removed from the standard library in a future - Python version. Consider third-party alternatives such as `LibCST`_ or - `parso`_. +.. deprecated-removed:: 3.11 3.13 + Python 3.9 switched to a PEG parser (see :pep:`617`) while lib2to3 is + using a less flexible LL(1) parser. Python 3.10 includes new language + syntax that is not parsable by lib2to3's LL(1) parser (see :pep:`634`). + The ``lib2to3`` module was marked pending for deprecation in Python 3.9 + (raising :exc:`PendingDeprecationWarning` on import) and fully deprecated + in Python 3.11 (raising :exc:`DeprecationWarning`). + It will be removed from the standard library in Python 3.13. + Consider third-party alternatives such as `LibCST`_ or `parso`_. .. note:: diff --git a/Doc/library/__future__.rst b/Doc/library/__future__.rst index 24bbd90d02cf76..8bd23daee73977 100644 --- a/Doc/library/__future__.rst +++ b/Doc/library/__future__.rst @@ -90,12 +90,20 @@ language using this mechanism: | generator_stop | 3.5.0b1 | 3.7 | :pep:`479`: | | | | | *StopIteration handling inside generators* | +------------------+-------------+--------------+---------------------------------------------+ -| annotations | 3.7.0b1 | 3.11 | :pep:`563`: | +| annotations | 3.7.0b1 | TBD [1]_ | :pep:`563`: | | | | | *Postponed evaluation of annotations* | +------------------+-------------+--------------+---------------------------------------------+ .. XXX Adding a new entry? Remember to update simple_stmts.rst, too. +.. [1] + ``from __future__ import annotations`` was previously scheduled to + become mandatory in Python 3.10, but the Python Steering Council + twice decided to delay the change + (`announcement for Python 3.10 `__; + `announcement for Python 3.11 `__). + No final decision has been made yet. See also :pep:`563` and :pep:`649`. + .. seealso:: diff --git a/Doc/library/__main__.rst b/Doc/library/__main__.rst index a64faf1bbe3c84..d0a65e76b84237 100644 --- a/Doc/library/__main__.rst +++ b/Doc/library/__main__.rst @@ -1,25 +1,368 @@ - -:mod:`__main__` --- Top-level script environment -================================================ +:mod:`__main__` --- Top-level code environment +============================================== .. module:: __main__ - :synopsis: The environment where the top-level script is run. + :synopsis: The environment where top-level code is run. Covers command-line + interfaces, import-time behavior, and ``__name__ == '__main__'``. -------------- -``'__main__'`` is the name of the scope in which top-level code executes. -A module's __name__ is set equal to ``'__main__'`` when read from -standard input, a script, or from an interactive prompt. +In Python, the special name ``__main__`` is used for two important constructs: + +1. the name of the top-level environment of the program, which can be + checked using the ``__name__ == '__main__'`` expression; and +2. the ``__main__.py`` file in Python packages. + +Both of these mechanisms are related to Python modules; how users interact with +them and how they interact with each other. They are explained in detail +below. If you're new to Python modules, see the tutorial section +:ref:`tut-modules` for an introduction. + + +.. _name_equals_main: + +``__name__ == '__main__'`` +--------------------------- + +When a Python module or package is imported, ``__name__`` is set to the +module's name. Usually, this is the name of the Python file itself without the +``.py`` extension:: + + >>> import configparser + >>> configparser.__name__ + 'configparser' + +If the file is part of a package, ``__name__`` will also include the parent +package's path:: + + >>> from concurrent.futures import process + >>> process.__name__ + 'concurrent.futures.process' + +However, if the module is executed in the top-level code environment, +its ``__name__`` is set to the string ``'__main__'``. + +What is the "top-level code environment"? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +``__main__`` is the name of the environment where top-level code is run. +"Top-level code" is the first user-specified Python module that starts running. +It's "top-level" because it imports all other modules that the program needs. +Sometimes "top-level code" is called an *entry point* to the application. + +The top-level code environment can be: + +* the scope of an interactive prompt:: + + >>> __name__ + '__main__' + +* the Python module passed to the Python interpreter as a file argument: + + .. code-block:: shell-session + + $ python3 helloworld.py + Hello, world! + +* the Python module or package passed to the Python interpreter with the + :option:`-m` argument: + + .. code-block:: shell-session + + $ python3 -m tarfile + usage: tarfile.py [-h] [-v] (...) + +* Python code read by the Python interpreter from standard input: + + .. code-block:: shell-session + + $ echo "import this" | python3 + The Zen of Python, by Tim Peters + + Beautiful is better than ugly. + Explicit is better than implicit. + ... + +* Python code passed to the Python interpreter with the :option:`-c` argument: + + .. code-block:: shell-session + + $ python3 -c "import this" + The Zen of Python, by Tim Peters + + Beautiful is better than ugly. + Explicit is better than implicit. + ... + +In each of these situations, the top-level module's ``__name__`` is set to +``'__main__'``. + +As a result, a module can discover whether or not it is running in the +top-level environment by checking its own ``__name__``, which allows a common +idiom for conditionally executing code when the module is not initialized from +an import statement:: + + if __name__ == '__main__': + # Execute when the module is not initialized from an import statement. + ... + +.. seealso:: + + For a more detailed look at how ``__name__`` is set in all situations, see + the tutorial section :ref:`tut-modules`. + + +Idiomatic Usage +^^^^^^^^^^^^^^^ + +Some modules contain code that is intended for script use only, like parsing +command-line arguments or fetching data from standard input. If a module +like this was imported from a different module, for example to unit test +it, the script code would unintentionally execute as well. + +This is where using the ``if __name__ == '__main__'`` code block comes in +handy. Code within this block won't run unless the module is executed in the +top-level environment. + +Putting as few statements as possible in the block below ``if __name___ == +'__main__'`` can improve code clarity and correctness. Most often, a function +named ``main`` encapsulates the program's primary behavior:: + + # echo.py + + import shlex + import sys + + def echo(phrase: str) -> None: + """A dummy wrapper around print.""" + # for demonstration purposes, you can imagine that there is some + # valuable and reusable logic inside this function + print(phrase) + + def main() -> int: + """Echo the input arguments to standard output""" + phrase = shlex.join(sys.argv) + echo(phrase) + return 0 + + if __name__ == '__main__': + sys.exit(main()) # next section explains the use of sys.exit + +Note that if the module didn't encapsulate code inside the ``main`` function +but instead put it directly within the ``if __name__ == '__main__'`` block, +the ``phrase`` variable would be global to the entire module. This is +error-prone as other functions within the module could be unintentionally using +the global variable instead of a local name. A ``main`` function solves this +problem. + +Using a ``main`` function has the added benefit of the ``echo`` function itself +being isolated and importable elsewhere. When ``echo.py`` is imported, the +``echo`` and ``main`` functions will be defined, but neither of them will be +called, because ``__name__ != '__main__'``. + + +Packaging Considerations +^^^^^^^^^^^^^^^^^^^^^^^^ + +``main`` functions are often used to create command-line tools by specifying +them as entry points for console scripts. When this is done, +`pip `_ inserts the function call into a template script, +where the return value of ``main`` is passed into :func:`sys.exit`. +For example:: + + sys.exit(main()) + +Since the call to ``main`` is wrapped in :func:`sys.exit`, the expectation is +that your function will return some value acceptable as an input to +:func:`sys.exit`; typically, an integer or ``None`` (which is implicitly +returned if your function does not have a return statement). + +By proactively following this convention ourselves, our module will have the +same behavior when run directly (i.e. ``python3 echo.py``) as it will have if +we later package it as a console script entry-point in a pip-installable +package. + +In particular, be careful about returning strings from your ``main`` function. +:func:`sys.exit` will interpret a string argument as a failure message, so +your program will have an exit code of ``1``, indicating failure, and the +string will be written to :data:`sys.stderr`. The ``echo.py`` example from +earlier exemplifies using the ``sys.exit(main())`` convention. + +.. seealso:: + + `Python Packaging User Guide `_ + contains a collection of tutorials and references on how to distribute and + install Python packages with modern tools. + + +``__main__.py`` in Python Packages +---------------------------------- + +If you are not familiar with Python packages, see section :ref:`tut-packages` +of the tutorial. Most commonly, the ``__main__.py`` file is used to provide +a command-line interface for a package. Consider the following hypothetical +package, "bandclass": + +.. code-block:: text + + bandclass + ├── __init__.py + ├── __main__.py + └── student.py + +``__main__.py`` will be executed when the package itself is invoked +directly from the command line using the :option:`-m` flag. For example: + +.. code-block:: shell-session + + $ python3 -m bandclass + +This command will cause ``__main__.py`` to run. How you utilize this mechanism +will depend on the nature of the package you are writing, but in this +hypothetical case, it might make sense to allow the teacher to search for +students:: + + # bandclass/__main__.py + + import sys + from .student import search_students + + student_name = sys.argv[2] if len(sys.argv) >= 2 else '' + print(f'Found student: {search_students(student_name)}') + +Note that ``from .student import search_students`` is an example of a relative +import. This import style can be used when referencing modules within a +package. For more details, see :ref:`intra-package-references` in the +:ref:`tut-modules` section of the tutorial. + +Idiomatic Usage +^^^^^^^^^^^^^^^ + +The contents of ``__main__.py`` typically isn't fenced with +``if __name__ == '__main__'`` blocks. Instead, those files are kept short, +functions to execute from other modules. Those other modules can then be +easily unit-tested and are properly reusable. + +If used, an ``if __name__ == '__main__'`` block will still work as expected +for a ``__main__.py`` file within a package, because its ``__name__`` +attribute will include the package's path if imported:: + + >>> import asyncio.__main__ + >>> asyncio.__main__.__name__ + 'asyncio.__main__' + +This won't work for ``__main__.py`` files in the root directory of a .zip file +though. Hence, for consistency, minimal ``__main__.py`` like the :mod:`venv` +one mentioned below are preferred. + +.. seealso:: + + See :mod:`venv` for an example of a package with a minimal ``__main__.py`` + in the standard library. It doesn't contain a ``if __name__ == '__main__'`` + block. You can invoke it with ``python3 -m venv [directory]``. + + See :mod:`runpy` for more details on the :option:`-m` flag to the + interpreter executable. + + See :mod:`zipapp` for how to run applications packaged as *.zip* files. In + this case Python looks for a ``__main__.py`` file in the root directory of + the archive. + + + +``import __main__`` +------------------- + +Regardless of which module a Python program was started with, other modules +running within that same program can import the top-level environment's scope +(:term:`namespace`) by importing the ``__main__`` module. This doesn't import +a ``__main__.py`` file but rather whichever module that received the special +name ``'__main__'``. + +Here is an example module that consumes the ``__main__`` namespace:: + + # namely.py + + import __main__ + + def did_user_define_their_name(): + return 'my_name' in dir(__main__) + + def print_user_name(): + if not did_user_define_their_name(): + raise ValueError('Define the variable `my_name`!') + + if '__file__' in dir(__main__): + print(__main__.my_name, "found in file", __main__.__file__) + else: + print(__main__.my_name) + +Example usage of this module could be as follows:: + + # start.py + + import sys + + from namely import print_user_name + + # my_name = "Dinsdale" + + def main(): + try: + print_user_name() + except ValueError as ve: + return str(ve) + + if __name__ == "__main__": + sys.exit(main()) + +Now, if we started our program, the result would look like this: + +.. code-block:: shell-session + + $ python3 start.py + Define the variable `my_name`! + +The exit code of the program would be 1, indicating an error. Uncommenting the +line with ``my_name = "Dinsdale"`` fixes the program and now it exits with +status code 0, indicating success: + +.. code-block:: shell-session + + $ python3 start.py + Dinsdale found in file /path/to/start.py + +Note that importing ``__main__`` doesn't cause any issues with unintentionally +running top-level code meant for script use which is put in the +``if __name__ == "__main__"`` block of the ``start`` module. Why does this work? + +Python inserts an empty ``__main__`` module in :attr:`sys.modules` at +interpreter startup, and populates it by running top-level code. In our example +this is the ``start`` module which runs line by line and imports ``namely``. +In turn, ``namely`` imports ``__main__`` (which is really ``start``). That's an +import cycle! Fortunately, since the partially populated ``__main__`` +module is present in :attr:`sys.modules`, Python passes that to ``namely``. +See :ref:`Special considerations for __main__ ` in the +import system's reference for details on how this works. + +The Python REPL is another example of a "top-level environment", so anything +defined in the REPL becomes part of the ``__main__`` scope:: -A module can discover whether or not it is running in the main scope by -checking its own ``__name__``, which allows a common idiom for conditionally -executing code in a module when it is run as a script or with ``python --m`` but not when it is imported:: + >>> import namely + >>> namely.did_user_define_their_name() + False + >>> namely.print_user_name() + Traceback (most recent call last): + ... + ValueError: Define the variable `my_name`! + >>> my_name = 'Jabberwocky' + >>> namely.did_user_define_their_name() + True + >>> namely.print_user_name() + Jabberwocky - if __name__ == "__main__": - # execute only if run as a script - main() +Note that in this case the ``__main__`` scope doesn't contain a ``__file__`` +attribute as it's interactive. -For a package, the same effect can be achieved by including a -``__main__.py`` module, the contents of which will be executed when the -module is run with ``-m``. +The ``__main__`` scope is used in the implementation of :mod:`pdb` and +:mod:`rlcompleter`. diff --git a/Doc/library/abc.rst b/Doc/library/abc.rst index 1a6ed474ff21da..3b74622e7ff46c 100644 --- a/Doc/library/abc.rst +++ b/Doc/library/abc.rst @@ -186,15 +186,15 @@ The :mod:`abc` module also provides the following decorator: class C(ABC): @abstractmethod - def my_abstract_method(self, ...): + def my_abstract_method(self, arg1): ... @classmethod @abstractmethod - def my_abstract_classmethod(cls, ...): + def my_abstract_classmethod(cls, arg2): ... @staticmethod @abstractmethod - def my_abstract_staticmethod(...): + def my_abstract_staticmethod(arg3): ... @property @@ -255,7 +255,7 @@ The :mod:`abc` module also supports the following legacy decorators: class C(ABC): @classmethod @abstractmethod - def my_abstract_classmethod(cls, ...): + def my_abstract_classmethod(cls, arg): ... @@ -276,7 +276,7 @@ The :mod:`abc` module also supports the following legacy decorators: class C(ABC): @staticmethod @abstractmethod - def my_abstract_staticmethod(...): + def my_abstract_staticmethod(arg): ... diff --git a/Doc/library/aifc.rst b/Doc/library/aifc.rst index 2e917cf7321b85..fa277857574a3a 100644 --- a/Doc/library/aifc.rst +++ b/Doc/library/aifc.rst @@ -3,6 +3,7 @@ .. module:: aifc :synopsis: Read and write audio files in AIFF or AIFC format. + :deprecated: **Source code:** :source:`Lib/aifc.py` @@ -11,6 +12,11 @@ single: AIFF single: AIFF-C + +.. deprecated:: 3.11 + The :mod:`aifc` module is deprecated + (see :pep:`PEP 594 <594#aifc>` for details). + -------------- This module provides support for reading and writing AIFF and AIFF-C files. diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index b2eb9eff914c69..d96f17b80a5f0f 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -148,7 +148,8 @@ ArgumentParser objects as keyword arguments. Each parameter has its own more detailed description below, but in short they are: - * prog_ - The name of the program (default: ``sys.argv[0]``) + * prog_ - The name of the program (default: + ``os.path.basename(sys.argv[0])``) * usage_ - The string describing the program usage (default: generated from arguments added to parser) @@ -501,7 +502,7 @@ disallowed. fromfile_prefix_chars ^^^^^^^^^^^^^^^^^^^^^ -Sometimes, for example when dealing with a particularly long argument lists, it +Sometimes, for example when dealing with a particularly long argument list, it may make sense to keep the list of arguments in a file rather than typing it out at the command line. If the ``fromfile_prefix_chars=`` argument is given to the :class:`ArgumentParser` constructor, then arguments that start with any of the @@ -853,6 +854,8 @@ is available in ``argparse`` and adds support for boolean actions such as >>> parser.parse_args(['--no-foo']) Namespace(foo=False) +.. versionadded:: 3.9 + The recommended way to create a custom action is to extend :class:`Action`, overriding the ``__call__`` method and optionally the ``__init__`` and ``format_usage`` methods. @@ -1102,7 +1105,7 @@ Anything with more interesting error-handling or resource management should be done downstream after the arguments are parsed. For example, JSON or YAML conversions have complex error cases that require -better reporting than can be given by the ``type`` keyword. An +better reporting than can be given by the ``type`` keyword. A :exc:`~json.JSONDecodeError` would not be well formatted and a :exc:`FileNotFound` exception would not be handled at all. @@ -1612,7 +1615,7 @@ Sub-commands .. method:: ArgumentParser.add_subparsers([title], [description], [prog], \ [parser_class], [action], \ - [option_string], [dest], [required], \ + [option_strings], [dest], [required], \ [help], [metavar]) Many programs split up their functionality into a number of sub-commands, diff --git a/Doc/library/array.rst b/Doc/library/array.rst index c7f137d15b4b86..975670cc81a202 100644 --- a/Doc/library/array.rst +++ b/Doc/library/array.rst @@ -52,7 +52,7 @@ Notes: .. versionchanged:: 3.9 ``array('u')`` now uses ``wchar_t`` as C type instead of deprecated - ``Py_UNICODE``. This change doesn't affect to its behavior because + ``Py_UNICODE``. This change doesn't affect its behavior because ``Py_UNICODE`` is alias of ``wchar_t`` since Python 3.3. .. deprecated-removed:: 3.3 4.0 diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index c7074c40f280c6..e29b5e88d71d41 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1266,7 +1266,7 @@ Pattern matching the pattern matches the subject. ``body`` contains a list of nodes to execute if the pattern matches and - the result of evaluating the guard expression is truthy. + the result of evaluating the guard expression is true. .. doctest:: @@ -1621,7 +1621,7 @@ Function and class definitions A function definition. * ``name`` is a raw string of the function name. - * ``args`` is a :class:`arguments` node. + * ``args`` is an :class:`arguments` node. * ``body`` is the list of nodes inside the function. * ``decorator_list`` is the list of decorators to be applied, stored outermost first (i.e. the first in the list will be applied last). @@ -1795,7 +1795,7 @@ Function and class definitions * ``bases`` is a list of nodes for explicitly specified base classes. * ``keywords`` is a list of :class:`keyword` nodes, principally for 'metaclass'. Other keywords will be passed to the metaclass, as per `PEP-3115 - `_. + `_. * ``starargs`` and ``kwargs`` are each a single node, as in a function call. starargs will be expanded to join the list of base classes, and kwargs will be passed to the metaclass. @@ -1917,6 +1917,19 @@ and classes for traversing abstract syntax trees: ``await`` as variable names. The lowest supported version is ``(3, 4)``; the highest is ``sys.version_info[0:2]``. + If source contains a null character ('\0'), :exc:`ValueError` is raised. + + .. warning:: + Note that successfully parsing source code into an AST object doesn't + guarantee that the source code provided is valid Python code that can + be executed as the compilation step can raise further :exc:`SyntaxError` + exceptions. For instance, the source ``return 42`` generates a valid + AST node for a return statement, but it cannot be compiled alone (it needs + to be inside a function node). + + In particular, :func:`ast.parse` won't do any scoping checks, which the + compilation step does. + .. warning:: It is possible to crash the Python interpreter with a sufficiently large/complex string due to stack depth limitations diff --git a/Doc/library/asynchat.rst b/Doc/library/asynchat.rst index 9e51416b83a570..7cc9d99779bbbb 100644 --- a/Doc/library/asynchat.rst +++ b/Doc/library/asynchat.rst @@ -3,6 +3,7 @@ .. module:: asynchat :synopsis: Support for asynchronous command/response protocols. + :deprecated: .. moduleauthor:: Sam Rushing .. sectionauthor:: Steve Holden @@ -10,6 +11,8 @@ **Source code:** :source:`Lib/asynchat.py` .. deprecated:: 3.6 + :mod:`asynchat` will be removed in Python 3.12 + (see :pep:`PEP 594 <594#asynchat>` for details). Please use :mod:`asyncio` instead. -------------- diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index ca91efec260db2..300092d71b1cb1 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -55,7 +55,7 @@ an event loop: .. deprecated:: 3.10 Deprecation warning is emitted if there is no running event loop. - If future Python releases this function will be an alias of + In future Python releases, this function will be an alias of :func:`get_running_loop`. .. function:: set_event_loop(loop) @@ -64,7 +64,7 @@ an event loop: .. function:: new_event_loop() - Create a new event loop object. + Create and return a new event loop object. Note that the behaviour of :func:`get_event_loop`, :func:`set_event_loop`, and :func:`new_event_loop` functions can be altered by @@ -216,6 +216,10 @@ Scheduling callbacks A thread-safe variant of :meth:`call_soon`. Must be used to schedule callbacks *from another thread*. + Raises :exc:`RuntimeError` if called on a loop that's been closed. + This can happen on a secondary thread when the main application is + shutting down. + See the :ref:`concurrency and multithreading ` section of the documentation. @@ -339,7 +343,7 @@ Creating Futures and Tasks the name of the task using :meth:`Task.set_name`. .. versionchanged:: 3.8 - Added the ``name`` parameter. + Added the *name* parameter. .. method:: loop.set_task_factory(factory) @@ -445,14 +449,27 @@ Opening network connections and *local_addr* should be specified. * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used - to bind the socket to locally. The *local_host* and *local_port* + to bind the socket locally. The *local_host* and *local_port* are looked up using ``getaddrinfo()``, similarly to *host* and *port*. * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds to wait for the TLS handshake to complete before aborting the connection. ``60.0`` seconds if ``None`` (default). - .. versionadded:: 3.8 + .. versionchanged:: 3.5 + + Added support for SSL/TLS in :class:`ProactorEventLoop`. + + .. versionchanged:: 3.6 + + The socket option :py:data:`~socket.TCP_NODELAY` is set by default + for all TCP connections. + + .. versionchanged:: 3.7 + + Added the *ssl_handshake_timeout* parameter. + + .. versionchanged:: 3.8 Added the *happy_eyeballs_delay* and *interleave* parameters. @@ -467,19 +484,6 @@ Opening network connections For more information: https://tools.ietf.org/html/rfc6555 - .. versionadded:: 3.7 - - The *ssl_handshake_timeout* parameter. - - .. versionchanged:: 3.6 - - The socket option :py:data:`~socket.TCP_NODELAY` is set by default - for all TCP connections. - - .. versionchanged:: 3.5 - - Added support for SSL/TLS in :class:`ProactorEventLoop`. - .. seealso:: The :func:`open_connection` function is a high-level alternative @@ -523,7 +527,7 @@ Opening network connections Other arguments: * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used - to bind the socket to locally. The *local_host* and *local_port* + to bind the socket locally. The *local_host* and *local_port* are looked up using :meth:`getaddrinfo`. * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used @@ -584,12 +588,8 @@ Opening network connections .. availability:: Unix. - .. versionadded:: 3.7 - - The *ssl_handshake_timeout* parameter. - .. versionchanged:: 3.7 - + Added the *ssl_handshake_timeout* parameter. The *path* parameter can now be a :term:`path-like object`. @@ -627,6 +627,11 @@ Creating network servers assumed and a list of multiple sockets will be returned (most likely one for IPv4 and another one for IPv6). + * The *port* parameter can be set to specify which port the server should + listen on. If ``0`` or ``None`` (the default), a random unused port will + be selected (note that if *host* resolves to multiple network interfaces, + a different random port will be selected for each interface). + * *family* can be set to either :data:`socket.AF_INET` or :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6. If not set, the *family* will be determined from host name @@ -663,15 +668,6 @@ Creating network servers :meth:`Server.serve_forever` to make the server to start accepting connections. - .. versionadded:: 3.7 - - Added *ssl_handshake_timeout* and *start_serving* parameters. - - .. versionchanged:: 3.6 - - The socket option :py:data:`~socket.TCP_NODELAY` is set by default - for all TCP connections. - .. versionchanged:: 3.5 Added support for SSL/TLS in :class:`ProactorEventLoop`. @@ -680,6 +676,12 @@ Creating network servers The *host* parameter can be a sequence of strings. + .. versionchanged:: 3.6 + + Added *ssl_handshake_timeout* and *start_serving* parameters. + The socket option :py:data:`~socket.TCP_NODELAY` is set by default + for all TCP connections. + .. seealso:: The :func:`start_server` function is a higher-level alternative API @@ -704,12 +706,9 @@ Creating network servers .. availability:: Unix. - .. versionadded:: 3.7 - - The *ssl_handshake_timeout* and *start_serving* parameters. - .. versionchanged:: 3.7 + Added the *ssl_handshake_timeout* and *start_serving* parameters. The *path* parameter can now be a :class:`~pathlib.Path` object. .. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \ @@ -737,11 +736,11 @@ Creating network servers Returns a ``(transport, protocol)`` pair. - .. versionadded:: 3.7 + .. versionadded:: 3.5.3 - The *ssl_handshake_timeout* parameter. + .. versionchanged:: 3.7 - .. versionadded:: 3.5.3 + Added the *ssl_handshake_timeout* parameter. Transferring files @@ -1238,9 +1237,10 @@ async/await code consider using the high-level .. note:: - The default asyncio event loop on **Windows** does not support - subprocesses. See :ref:`Subprocess Support on Windows - ` for details. + On Windows, the default event loop :class:`ProactorEventLoop` supports + subprocesses, whereas :class:`SelectorEventLoop` does not. See + :ref:`Subprocess Support on Windows ` for + details. .. coroutinemethod:: loop.subprocess_exec(protocol_factory, *args, \ stdin=subprocess.PIPE, stdout=subprocess.PIPE, \ diff --git a/Doc/library/asyncio-future.rst b/Doc/library/asyncio-future.rst index ef496a23f5cd4c..7426e8291e1424 100644 --- a/Doc/library/asyncio-future.rst +++ b/Doc/library/asyncio-future.rst @@ -54,6 +54,9 @@ Future Functions See also the :func:`create_task` function which is the preferred way for creating new Tasks. + Save a reference to the result of this function, to avoid + a task disappearing mid execution. + .. versionchanged:: 3.5.1 The function accepts any :term:`awaitable` object. @@ -191,7 +194,7 @@ Future Object schedule the callbacks, and return ``True``. .. versionchanged:: 3.9 - Added the ``msg`` parameter. + Added the *msg* parameter. .. method:: exception() diff --git a/Doc/library/asyncio-protocol.rst b/Doc/library/asyncio-protocol.rst index 9dbd3ab46a3f68..8b67f4b8957ef6 100644 --- a/Doc/library/asyncio-protocol.rst +++ b/Doc/library/asyncio-protocol.rst @@ -683,7 +683,7 @@ factories passed to the :meth:`loop.create_datagram_endpoint` method. Subprocess Protocols -------------------- -Datagram Protocol instances should be constructed by protocol +Subprocess Protocol instances should be constructed by protocol factories passed to the :meth:`loop.subprocess_exec` and :meth:`loop.subprocess_shell` methods. diff --git a/Doc/library/asyncio-queue.rst b/Doc/library/asyncio-queue.rst index 289ad1b014c356..d86fbc21351e2d 100644 --- a/Doc/library/asyncio-queue.rst +++ b/Doc/library/asyncio-queue.rst @@ -36,6 +36,9 @@ Queue the queue is always known and can be returned by calling the :meth:`qsize` method. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + This class is :ref:`not thread safe `. diff --git a/Doc/library/asyncio-stream.rst b/Doc/library/asyncio-stream.rst index ad3c7442ad56cd..3558277920c703 100644 --- a/Doc/library/asyncio-stream.rst +++ b/Doc/library/asyncio-stream.rst @@ -51,7 +51,8 @@ and work with streams: .. coroutinefunction:: open_connection(host=None, port=None, *, \ limit=None, ssl=None, family=0, proto=0, \ flags=0, sock=None, local_addr=None, \ - server_hostname=None, ssl_handshake_timeout=None) + server_hostname=None, ssl_handshake_timeout=None, \ + happy_eyeballs_delay=None, interleave=None) Establish a network connection and return a pair of ``(reader, writer)`` objects. @@ -66,9 +67,15 @@ and work with streams: The rest of the arguments are passed directly to :meth:`loop.create_connection`. - .. versionadded:: 3.7 + .. versionchanged:: 3.7 + Added the *ssl_handshake_timeout* parameter. + + .. versionadded:: 3.8 + Added *happy_eyeballs_delay* and *interleave* parameters. + + .. versionchanged:: 3.10 + Removed the *loop* parameter. - The *ssl_handshake_timeout* parameter. .. coroutinefunction:: start_server(client_connected_cb, host=None, \ port=None, *, limit=None, \ @@ -96,9 +103,11 @@ and work with streams: The rest of the arguments are passed directly to :meth:`loop.create_server`. - .. versionadded:: 3.7 + .. versionchanged:: 3.7 + Added the *ssl_handshake_timeout* and *start_serving* parameters. - The *ssl_handshake_timeout* and *start_serving* parameters. + .. versionchanged:: 3.10 + Removed the *loop* parameter. .. rubric:: Unix Sockets @@ -116,14 +125,13 @@ and work with streams: .. availability:: Unix. - .. versionadded:: 3.7 - - The *ssl_handshake_timeout* parameter. - .. versionchanged:: 3.7 - + Added the *ssl_handshake_timeout* parameter. The *path* parameter can now be a :term:`path-like object` + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. coroutinefunction:: start_unix_server(client_connected_cb, path=None, \ *, limit=None, sock=None, backlog=100, ssl=None, \ @@ -137,14 +145,13 @@ and work with streams: .. availability:: Unix. - .. versionadded:: 3.7 - - The *ssl_handshake_timeout* and *start_serving* parameters. - .. versionchanged:: 3.7 - + Added the *ssl_handshake_timeout* and *start_serving* parameters. The *path* parameter can now be a :term:`path-like object`. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + StreamReader ============ @@ -366,8 +373,8 @@ TCP echo server using the :func:`asyncio.start_server` function:: server = await asyncio.start_server( handle_echo, '127.0.0.1', 8888) - addr = server.sockets[0].getsockname() - print(f'Serving on {addr}') + addrs = ', '.join(str(sock.getsockname()) for sock in server.sockets) + print(f'Serving on {addrs}') async with server: await server.serve_forever() diff --git a/Doc/library/asyncio-subprocess.rst b/Doc/library/asyncio-subprocess.rst index ef4d9bcc434c9f..28d0b21e8180b6 100644 --- a/Doc/library/asyncio-subprocess.rst +++ b/Doc/library/asyncio-subprocess.rst @@ -75,6 +75,9 @@ Creating Subprocesses See the documentation of :meth:`loop.subprocess_exec` for other parameters. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. coroutinefunction:: create_subprocess_shell(cmd, stdin=None, \ stdout=None, stderr=None, limit=None, **kwds) @@ -99,6 +102,9 @@ Creating Subprocesses escape whitespace and special shell characters in strings that are going to be used to construct shell commands. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. note:: Subprocesses are available for Windows if a :class:`ProactorEventLoop` is @@ -118,6 +124,7 @@ Constants ========= .. data:: asyncio.subprocess.PIPE + :module: Can be passed to the *stdin*, *stdout* or *stderr* parameters. @@ -131,11 +138,13 @@ Constants attributes will point to :class:`StreamReader` instances. .. data:: asyncio.subprocess.STDOUT + :module: Special value that can be used as the *stderr* argument and indicates that standard error should be redirected into standard output. .. data:: asyncio.subprocess.DEVNULL + :module: Special value that can be used as the *stdin*, *stdout* or *stderr* argument to process creation functions. It indicates that the special file @@ -151,6 +160,7 @@ wrapper that allows communicating with subprocesses and watching for their completion. .. class:: asyncio.subprocess.Process + :module: An object that wraps OS processes created by the :func:`create_subprocess_exec` and :func:`create_subprocess_shell` @@ -269,7 +279,7 @@ their completion. Use the :meth:`communicate` method rather than :attr:`process.stdin.write() `, :attr:`await process.stdout.read() ` or - :attr:`await process.stderr.read `. + :attr:`await process.stderr.read() `. This avoids deadlocks due to streams pausing reading or writing and blocking the child process. diff --git a/Doc/library/asyncio-sync.rst b/Doc/library/asyncio-sync.rst index d12630afc6a326..f4063db2ee86e6 100644 --- a/Doc/library/asyncio-sync.rst +++ b/Doc/library/asyncio-sync.rst @@ -63,6 +63,9 @@ Lock finally: lock.release() + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. coroutinemethod:: acquire() Acquire the lock. @@ -105,6 +108,9 @@ Event :meth:`clear` method. The :meth:`~Event.wait` method blocks until the flag is set to *true*. The flag is set to *false* initially. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. _asyncio_example_sync_event: Example:: @@ -177,6 +183,9 @@ Condition ``None``. In the latter case a new Lock object is created automatically. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + The preferred way to use a Condition is an :keyword:`async with` statement:: @@ -273,6 +282,9 @@ Semaphore internal counter (``1`` by default). If the given value is less than ``0`` a :exc:`ValueError` is raised. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + The preferred way to use a Semaphore is an :keyword:`async with` statement:: @@ -325,6 +337,9 @@ BoundedSemaphore a :exc:`ValueError` in :meth:`~Semaphore.release` if it increases the internal counter above the initial *value*. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + --------- diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 3f54ecb08efc1f..e535a5bf042542 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -20,7 +20,7 @@ Coroutines :term:`Coroutines ` declared with the async/await syntax is the preferred way of writing asyncio applications. For example, the following -snippet of code (requires Python 3.7+) prints "hello", waits 1 second, +snippet of code prints "hello", waits 1 second, and then prints "world":: >>> import asyncio @@ -259,25 +259,32 @@ Creating Tasks :exc:`RuntimeError` is raised if there is no running loop in current thread. - This function has been **added in Python 3.7**. Prior to - Python 3.7, the low-level :func:`asyncio.ensure_future` function - can be used instead:: + .. important:: - async def coro(): - ... + Save a reference to the result of this function, to avoid + a task disappearing mid execution. The event loop only keeps + weak references to tasks. A task that isn't referenced elsewhere + may get garbage-collected at any time, even before it's done. + For reliable "fire-and-forget" background tasks, gather them in + a collection:: - # In Python 3.7+ - task = asyncio.create_task(coro()) - ... + background_tasks = set() - # This works in all Python versions but is less readable - task = asyncio.ensure_future(coro()) - ... + for i in range(10): + task = asyncio.create_task(some_coro(param=i)) + + # Add task to the set. This creates a strong reference. + background_tasks.add(task) + + # To prevent keeping references to finished tasks forever, + # make each task remove its own reference from the set after + # completion: + task.add_done_callback(background_tasks.discard) .. versionadded:: 3.7 .. versionchanged:: 3.8 - Added the ``name`` parameter. + Added the *name* parameter. Sleeping @@ -297,6 +304,12 @@ Sleeping tasks to run. This can be used by long-running functions to avoid blocking the event loop for the full duration of the function call. + .. deprecated-removed:: 3.8 3.10 + The ``loop`` parameter. This function has been implicitly getting the + current running loop since 3.7. See + :ref:`What's New in 3.10's Removed section ` + for more information. + .. _asyncio_example_sleep: Example of coroutine displaying the current date every second @@ -317,6 +330,10 @@ Sleeping asyncio.run(display_date()) + .. versionchanged:: 3.10 + Removed the *loop* parameter. + + Running Tasks Concurrently ========================== @@ -349,6 +366,9 @@ Running Tasks Concurrently cancellation of one submitted Task/Future to cause other Tasks/Futures to be cancelled. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. _asyncio_example_gather: Example:: @@ -358,32 +378,35 @@ Running Tasks Concurrently async def factorial(name, number): f = 1 for i in range(2, number + 1): - print(f"Task {name}: Compute factorial({i})...") + print(f"Task {name}: Compute factorial({number}), currently i={i}...") await asyncio.sleep(1) f *= i print(f"Task {name}: factorial({number}) = {f}") + return f async def main(): # Schedule three calls *concurrently*: - await asyncio.gather( + L = await asyncio.gather( factorial("A", 2), factorial("B", 3), factorial("C", 4), ) + print(L) asyncio.run(main()) # Expected output: # - # Task A: Compute factorial(2)... - # Task B: Compute factorial(2)... - # Task C: Compute factorial(2)... + # Task A: Compute factorial(2), currently i=2... + # Task B: Compute factorial(3), currently i=2... + # Task C: Compute factorial(4), currently i=2... # Task A: factorial(2) = 2 - # Task B: Compute factorial(3)... - # Task C: Compute factorial(3)... + # Task B: Compute factorial(3), currently i=3... + # Task C: Compute factorial(4), currently i=3... # Task B: factorial(3) = 6 - # Task C: Compute factorial(4)... + # Task C: Compute factorial(4), currently i=4... # Task C: factorial(4) = 24 + # [2, 6, 24] .. note:: If *return_exceptions* is False, cancelling gather() after it @@ -397,6 +420,9 @@ Running Tasks Concurrently If the *gather* itself is cancelled, the cancellation is propagated regardless of *return_exceptions*. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. deprecated:: 3.10 Deprecation warning is emitted if no positional arguments are provided or not all positional arguments are Future-like objects @@ -439,6 +465,9 @@ Shielding From Cancellation except CancelledError: res = None + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. deprecated:: 3.10 Deprecation warning is emitted if *aw* is not Future-like object and there is no running event loop. @@ -470,6 +499,9 @@ Timeouts If the wait is cancelled, the future *aw* is also cancelled. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. _asyncio_example_waitfor: Example:: @@ -497,6 +529,9 @@ Timeouts for *aw* to be cancelled. Previously, it raised :exc:`asyncio.TimeoutError` immediately. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + Waiting Primitives ================== @@ -553,6 +588,9 @@ Waiting Primitives ``wait()`` directly is deprecated as it leads to :ref:`confusing behavior `. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. _asyncio_example_wait_coroutine: .. note:: @@ -585,6 +623,9 @@ Waiting Primitives Passing coroutine objects to ``wait()`` directly is deprecated. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. function:: as_completed(aws, *, timeout=None) @@ -596,12 +637,18 @@ Waiting Primitives Raises :exc:`asyncio.TimeoutError` if the timeout occurs before all Futures are done. + .. versionchanged:: 3.10 + Removed the *loop* parameter. + Example:: for coro in as_completed(aws): earliest_result = await coro # ... + .. versionchanged:: 3.10 + Removed the *loop* parameter. + .. deprecated:: 3.10 Deprecation warning is emitted if not all awaitable objects in the *aws* iterable are Future-like objects and there is no running event loop. @@ -694,7 +741,7 @@ Scheduling From Other Threads try: result = future.result(timeout) - except asyncio.TimeoutError: + except concurrent.futures.TimeoutError: print('The coroutine took too long, cancelling the task...') future.cancel() except Exception as exc: @@ -783,10 +830,7 @@ Task Object Added support for the :mod:`contextvars` module. .. versionchanged:: 3.8 - Added the ``name`` parameter. - - .. deprecated-removed:: 3.8 3.10 - The *loop* parameter. + Added the *name* parameter. .. deprecated:: 3.10 Deprecation warning is emitted if *loop* is not specified @@ -808,7 +852,7 @@ Task Object discouraged. .. versionchanged:: 3.9 - Added the ``msg`` parameter. + Added the *msg* parameter. .. _asyncio_example_task_cancel: @@ -980,7 +1024,7 @@ Generator-based Coroutines .. note:: Support for generator-based coroutines is **deprecated** and - is scheduled for removal in Python 3.10. + is removed in Python 3.11. Generator-based coroutines predate async/await syntax. They are Python generators that use ``yield from`` expressions to await @@ -1008,7 +1052,7 @@ enforced. This decorator should not be used for :keyword:`async def` coroutines. - .. deprecated-removed:: 3.8 3.10 + .. deprecated-removed:: 3.8 3.11 Use :keyword:`async def` instead. diff --git a/Doc/library/asyncio.rst b/Doc/library/asyncio.rst index 94a853259d3483..a6429394389b10 100644 --- a/Doc/library/asyncio.rst +++ b/Doc/library/asyncio.rst @@ -17,7 +17,6 @@ await asyncio.sleep(1) print('... World!') - # Python 3.7+ asyncio.run(main()) asyncio is a library to write **concurrent** code using diff --git a/Doc/library/asyncore.rst b/Doc/library/asyncore.rst index a86518ebff2777..a732fd7ba4f152 100644 --- a/Doc/library/asyncore.rst +++ b/Doc/library/asyncore.rst @@ -4,6 +4,7 @@ .. module:: asyncore :synopsis: A base class for developing asynchronous socket handling services. + :deprecated: .. moduleauthor:: Sam Rushing .. sectionauthor:: Christopher Petrilli @@ -13,6 +14,8 @@ **Source code:** :source:`Lib/asyncore.py` .. deprecated:: 3.6 + :mod:`asyncore` will be removed in Python 3.12 + (see :pep:`PEP 594 <594#asyncore>` for details). Please use :mod:`asyncio` instead. -------------- diff --git a/Doc/library/atexit.rst b/Doc/library/atexit.rst index c2c058e474cbdc..f7f038107d11fe 100644 --- a/Doc/library/atexit.rst +++ b/Doc/library/atexit.rst @@ -39,7 +39,7 @@ internal error is detected, or when :func:`os._exit` is called. If an exception is raised during execution of the exit handlers, a traceback is printed (unless :exc:`SystemExit` is raised) and the exception information is - saved. After all exit handlers have had a chance to run the last exception to + saved. After all exit handlers have had a chance to run, the last exception to be raised is re-raised. This function returns *func*, which makes it possible to use it as a @@ -48,11 +48,12 @@ internal error is detected, or when :func:`os._exit` is called. .. function:: unregister(func) - Remove *func* from the list of functions to be run at interpreter - shutdown. After calling :func:`unregister`, *func* is guaranteed not to be - called when the interpreter shuts down, even if it was registered more than - once. :func:`unregister` silently does nothing if *func* was not previously - registered. + Remove *func* from the list of functions to be run at interpreter shutdown. + :func:`unregister` silently does nothing if *func* was not previously + registered. If *func* has been registered more than once, every occurrence + of that function in the :mod:`atexit` call stack will be removed. Equality + comparisons (``==``) are used internally during unregistration, so function + references do not need to have matching identities. .. seealso:: @@ -73,7 +74,7 @@ automatically when the program terminates without relying on the application making an explicit call into this module at termination. :: try: - with open("counterfile") as infile: + with open('counterfile') as infile: _count = int(infile.read()) except FileNotFoundError: _count = 0 @@ -83,21 +84,22 @@ making an explicit call into this module at termination. :: _count = _count + n def savecounter(): - with open("counterfile", "w") as outfile: - outfile.write("%d" % _count) + with open('counterfile', 'w') as outfile: + outfile.write('%d' % _count) import atexit + atexit.register(savecounter) Positional and keyword arguments may also be passed to :func:`register` to be passed along to the registered function when it is called:: def goodbye(name, adjective): - print('Goodbye, %s, it was %s to meet you.' % (name, adjective)) + print('Goodbye %s, it was %s to meet you.' % (name, adjective)) import atexit - atexit.register(goodbye, 'Donny', 'nice') + atexit.register(goodbye, 'Donny', 'nice') # or: atexit.register(goodbye, adjective='nice', name='Donny') @@ -107,6 +109,6 @@ Usage as a :term:`decorator`:: @atexit.register def goodbye(): - print("You are now leaving the Python sector.") + print('You are now leaving the Python sector.') This only works with functions that can be called without arguments. diff --git a/Doc/library/audioop.rst b/Doc/library/audioop.rst index bad9da2ec62e56..649c99e796282c 100644 --- a/Doc/library/audioop.rst +++ b/Doc/library/audioop.rst @@ -3,6 +3,11 @@ .. module:: audioop :synopsis: Manipulate raw audio data. + :deprecated: + +.. deprecated:: 3.11 + The :mod:`audioop` module is deprecated + (see :pep:`PEP 594 <594#audioop>` for details). -------------- diff --git a/Doc/library/base64.rst b/Doc/library/base64.rst index 2f24bb63912fb6..35fb7b69fa4925 100644 --- a/Doc/library/base64.rst +++ b/Doc/library/base64.rst @@ -2,7 +2,7 @@ =============================================================== .. module:: base64 - :synopsis: RFC 3548: Base16, Base32, Base64 Data Encodings; + :synopsis: RFC 4648: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85 **Source code:** :source:`Lib/base64.py` @@ -16,10 +16,10 @@ This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. It provides encoding and decoding functions for the encodings specified in -:rfc:`3548`, which defines the Base16, Base32, and Base64 algorithms, +:rfc:`4648`, which defines the Base16, Base32, and Base64 algorithms, and for the de-facto standard Ascii85 and Base85 encodings. -The :rfc:`3548` encodings are suitable for encoding binary data so that it can +The :rfc:`4648` encodings are suitable for encoding binary data so that it can be safely sent by email, used as parts of URLs, or included as part of an HTTP POST request. The encoding algorithm is not the same as the :program:`uuencode` program. @@ -28,7 +28,7 @@ There are two interfaces provided by this module. The modern interface supports encoding :term:`bytes-like objects ` to ASCII :class:`bytes`, and decoding :term:`bytes-like objects ` or strings containing ASCII to :class:`bytes`. Both base-64 alphabets -defined in :rfc:`3548` (normal, and URL- and filesystem-safe) are supported. +defined in :rfc:`4648` (normal, and URL- and filesystem-safe) are supported. The legacy interface does not support decoding from strings, but it does provide functions for encoding and decoding to and from :term:`file objects @@ -152,7 +152,7 @@ The modern interface provides: This version does not allow the digit 0 (zero) to the letter O (oh) and digit 1 (one) to either the letter I (eye) or letter L (el) mappings, all these characters are included in the Extended Hex Alphabet and are not - interchangable. + interchangeable. .. versionadded:: 3.10 @@ -287,6 +287,13 @@ An example usage of the module: >>> data b'data to be encoded' +.. _base64-security: + +Security Considerations +----------------------- + +A new security considerations section was added to :rfc:`4648` (section 12); it's +recommended to review the security section for any code deployed to production. .. seealso:: diff --git a/Doc/library/binascii.rst b/Doc/library/binascii.rst index 2c0c1bce5d7f8f..5cd058c0b6d74f 100644 --- a/Doc/library/binascii.rst +++ b/Doc/library/binascii.rst @@ -135,7 +135,7 @@ The :mod:`binascii` module defines the following functions: .. function:: crc32(data[, value]) - Compute CRC-32, the 32-bit checksum of *data*, starting with an + Compute CRC-32, the unsigned 32-bit checksum of *data*, starting with an initial CRC of *value*. The default initial CRC is zero. The algorithm is consistent with the ZIP file checksum. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash @@ -149,9 +149,8 @@ The :mod:`binascii` module defines the following functions: .. versionchanged:: 3.0 The result is always unsigned. - To generate the same numeric value across all Python versions and - platforms, use ``crc32(data) & 0xffffffff``. - + To generate the same numeric value when using Python 2 or earlier, + use ``crc32(data) & 0xffffffff``. .. function:: b2a_hex(data[, sep[, bytes_per_sep=1]]) hexlify(data[, sep[, bytes_per_sep=1]]) diff --git a/Doc/library/bisect.rst b/Doc/library/bisect.rst index f34ee175ba6574..513675d3685a52 100644 --- a/Doc/library/bisect.rst +++ b/Doc/library/bisect.rst @@ -35,15 +35,18 @@ The following functions are provided: ``all(val >= x for val in a[i : hi])`` for the right side. *key* specifies a :term:`key function` of one argument that is used to - extract a comparison key from each input element. The default value is - ``None`` (compare the elements directly). + extract a comparison key from each element in the array. To support + searching complex records, the key function is not applied to the *x* value. + + If *key* is ``None``, the elements are compared directly with no + intervening function call. .. versionchanged:: 3.10 Added the *key* parameter. .. function:: bisect_right(a, x, lo=0, hi=len(a), *, key=None) - bisect(a, x, lo=0, hi=len(a)) + bisect(a, x, lo=0, hi=len(a), *, key=None) Similar to :func:`bisect_left`, but returns an insertion point which comes after (to the right of) any existing entries of *x* in *a*. @@ -53,8 +56,11 @@ The following functions are provided: ``all(val > x for val in a[i : hi])`` for the right side. *key* specifies a :term:`key function` of one argument that is used to - extract a comparison key from each input element. The default value is - ``None`` (compare the elements directly). + extract a comparison key from each element in the array. To support + searching complex records, the key function is not applied to the *x* value. + + If *key* is ``None``, the elements are compared directly with no + intervening function call. .. versionchanged:: 3.10 Added the *key* parameter. @@ -64,14 +70,13 @@ The following functions are provided: Insert *x* in *a* in sorted order. - *key* specifies a :term:`key function` of one argument that is used to - extract a comparison key from each input element. The default value is - ``None`` (compare the elements directly). - This function first runs :func:`bisect_left` to locate an insertion point. Next, it runs the :meth:`insert` method on *a* to insert *x* at the appropriate position to maintain sort order. + To support inserting records in a table, the *key* function (if any) is + applied to *x* for the search step but not for the insertion step. + Keep in mind that the ``O(log n)`` search is dominated by the slow O(n) insertion step. @@ -80,19 +85,18 @@ The following functions are provided: .. function:: insort_right(a, x, lo=0, hi=len(a), *, key=None) - insort(a, x, lo=0, hi=len(a)) + insort(a, x, lo=0, hi=len(a), *, key=None) Similar to :func:`insort_left`, but inserting *x* in *a* after any existing entries of *x*. - *key* specifies a :term:`key function` of one argument that is used to - extract a comparison key from each input element. The default value is - ``None`` (compare the elements directly). - This function first runs :func:`bisect_right` to locate an insertion point. Next, it runs the :meth:`insert` method on *a* to insert *x* at the appropriate position to maintain sort order. + To support inserting records in a table, the *key* function (if any) is + applied to *x* for the search step but not for the insertion step. + Keep in mind that the ``O(log n)`` search is dominated by the slow O(n) insertion step. @@ -194,8 +198,42 @@ a 'B', and so on:: >>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]] ['F', 'A', 'C', 'C', 'B', 'A', 'A'] -One technique to avoid repeated calls to a key function is to search a list of -precomputed keys to find the index of a record:: +The :func:`bisect` and :func:`insort` functions also work with lists of +tuples. The *key* argument can serve to extract the field used for ordering +records in a table:: + + >>> from collections import namedtuple + >>> from operator import attrgetter + >>> from bisect import bisect, insort + >>> from pprint import pprint + + >>> Movie = namedtuple('Movie', ('name', 'released', 'director')) + + >>> movies = [ + ... Movie('Jaws', 1975, 'Speilberg'), + ... Movie('Titanic', 1997, 'Cameron'), + ... Movie('The Birds', 1963, 'Hitchcock'), + ... Movie('Aliens', 1986, 'Scott') + ... ] + + >>> # Find the first movie released on or after 1960 + >>> by_year = attrgetter('released') + >>> movies.sort(key=by_year) + >>> movies[bisect(movies, 1960, key=by_year)] + Movie(name='The Birds', released=1963, director='Hitchcock') + + >>> # Insert a movie while maintaining sort order + >>> romance = Movie('Love Story', 1970, 'Hiller') + >>> insort(movies, romance, key=by_year) + >>> pprint(movies) + [Movie(name='The Birds', released=1963, director='Hitchcock'), + Movie(name='Love Story', released=1970, director='Hiller'), + Movie(name='Jaws', released=1975, director='Speilberg'), + Movie(name='Aliens', released=1986, director='Scott'), + Movie(name='Titanic', released=1997, director='Cameron')] + +If the key function is expensive, it is possible to avoid repeated function +calls by searching a list of precomputed keys to find the index of a record:: >>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)] >>> data.sort(key=lambda r: r[1]) # Or use operator.itemgetter(1). @@ -208,4 +246,3 @@ precomputed keys to find the index of a record:: ('red', 5) >>> data[bisect_left(keys, 8)] ('yellow', 8) - diff --git a/Doc/library/bz2.rst b/Doc/library/bz2.rst index f6787ab120ed57..999892e95f4715 100644 --- a/Doc/library/bz2.rst +++ b/Doc/library/bz2.rst @@ -325,3 +325,8 @@ Writing and reading a bzip2-compressed file in binary mode: ... content = f.read() >>> content == data # Check equality to original object after round-trip True + +.. testcleanup:: + + import os + os.remove("myfile.bz2") diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index c3c04db853ed2d..f641760d1bd1a9 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -31,7 +31,7 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is .. class:: Calendar(firstweekday=0) Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the - first day of the week. ``0`` is Monday (the default), ``6`` is Sunday. + first day of the week. :const:`MONDAY` is ``0`` (the default), :const:`SUNDAY` is ``6``. A :class:`Calendar` object provides several methods that can be used for preparing the calendar data for formatting. This class doesn't do any formatting @@ -409,6 +409,15 @@ The :mod:`calendar` module exports the following data attributes: locale. This follows normal convention of January being month number 1, so it has a length of 13 and ``month_abbr[0]`` is the empty string. +.. data:: MONDAY + TUESDAY + WEDNESDAY + THURSDAY + FRIDAY + SATURDAY + SUNDAY + + Aliases for day numbers, where ``MONDAY`` is ``0`` and ``SUNDAY`` is ``6``. .. seealso:: diff --git a/Doc/library/cgi.rst b/Doc/library/cgi.rst index 05d9cdf424073f..eeb80ed57ef35d 100644 --- a/Doc/library/cgi.rst +++ b/Doc/library/cgi.rst @@ -3,6 +3,7 @@ .. module:: cgi :synopsis: Helpers for running Python scripts via the Common Gateway Interface. + :deprecated: **Source code:** :source:`Lib/cgi.py` @@ -14,6 +15,10 @@ single: URL single: Common Gateway Interface +.. deprecated:: 3.11 + The :mod:`cgi` module is deprecated + (see :pep:`PEP 594 <594#cgi>` for details and alternatives). + -------------- Support module for Common Gateway Interface (CGI) scripts. @@ -73,7 +78,7 @@ When you write a new script, consider adding these lines:: cgitb.enable() This activates a special exception handler that will display detailed reports in -the Web browser if any errors occur. If you'd rather not show the guts of your +the web browser if any errors occur. If you'd rather not show the guts of your program to users of your script, you can have the reports saved to files instead, with code like this:: @@ -89,7 +94,7 @@ To get at submitted form data, use the :class:`FieldStorage` class. If the form contains non-ASCII characters, use the *encoding* keyword parameter set to the value of the encoding defined for the document. It is usually contained in the META tag in the HEAD section of the HTML document or by the -:mailheader:`Content-Type` header). This reads the form contents from the +:mailheader:`Content-Type` header. This reads the form contents from the standard input or the environment (depending on the value of various environment variables set according to the CGI standard). Since it may consume standard input, it should be instantiated only once. @@ -316,7 +321,7 @@ algorithms implemented in this module in other circumstances. .. function:: test() Robust test CGI script, usable as main program. Writes minimal HTTP headers and - formats all information provided to the script in HTML form. + formats all information provided to the script in HTML format. .. function:: print_environ() @@ -346,11 +351,11 @@ Caring about security .. index:: pair: CGI; security -There's one important rule: if you invoke an external program (via the -:func:`os.system` or :func:`os.popen` functions. or others with similar +There's one important rule: if you invoke an external program (via +:func:`os.system`, :func:`os.popen` or other functions with similar functionality), make very sure you don't pass arbitrary strings received from the client to the shell. This is a well-known security hole whereby clever -hackers anywhere on the Web can exploit a gullible CGI script to invoke +hackers anywhere on the web can exploit a gullible CGI script to invoke arbitrary shell commands. Even parts of the URL or field names cannot be trusted, since the request doesn't have to come from your form! @@ -424,7 +429,7 @@ above on installing your CGI script carefully can save you a lot of time. If you wonder whether you have understood the installation procedure correctly, try installing a copy of this module file (:file:`cgi.py`) as a CGI script. When invoked as a script, the file will dump its environment and the contents of the -form in HTML form. Give it the right mode etc, and send it a request. If it's +form in HTML format. Give it the right mode etc., and send it a request. If it's installed in the standard :file:`cgi-bin` directory, it should be possible to send it a request by entering a URL into your browser of the form: @@ -457,7 +462,7 @@ likely the traceback will end up in one of the HTTP server's log files, or be discarded altogether. Fortunately, once you have managed to get your script to execute *some* code, -you can easily send tracebacks to the Web browser using the :mod:`cgitb` module. +you can easily send tracebacks to the web browser using the :mod:`cgitb` module. If you haven't done so already, just add the lines:: import cgitb diff --git a/Doc/library/cgitb.rst b/Doc/library/cgitb.rst index 5f3a6476dd8cdc..3b0b106abacd50 100644 --- a/Doc/library/cgitb.rst +++ b/Doc/library/cgitb.rst @@ -3,6 +3,7 @@ .. module:: cgitb :synopsis: Configurable traceback handler for CGI scripts. + :deprecated: .. moduleauthor:: Ka-Ping Yee .. sectionauthor:: Fred L. Drake, Jr. @@ -15,6 +16,10 @@ single: exceptions; in CGI scripts single: tracebacks; in CGI scripts +.. deprecated:: 3.11 + The :mod:`cgitb` module is deprecated + (see :pep:`PEP 594 <594#cgitb>` for details). + -------------- The :mod:`cgitb` module provides a special exception handler for Python scripts. diff --git a/Doc/library/chunk.rst b/Doc/library/chunk.rst index 5e24df923ed210..5a84c8904f7145 100644 --- a/Doc/library/chunk.rst +++ b/Doc/library/chunk.rst @@ -3,6 +3,7 @@ .. module:: chunk :synopsis: Module to read IFF chunks. + :deprecated: .. moduleauthor:: Sjoerd Mullender .. sectionauthor:: Sjoerd Mullender @@ -16,6 +17,10 @@ single: Real Media File Format single: RMFF +.. deprecated:: 3.11 + The :mod:`chunk` module is deprecated + (see :pep:`PEP 594 <594#chunk>` for details). + -------------- This module provides an interface for reading files that use EA IFF 85 chunks. diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index 0dcd88f9fd5b7f..1c10462c1509c3 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -23,11 +23,11 @@ This module defines base classes for standard Python codecs (encoders and decoders) and provides access to the internal Python codec registry, which manages the codec and error handling lookup process. Most standard codecs -are :term:`text encodings `, which encode text to bytes, -but there are also codecs provided that encode text to text, and bytes to -bytes. Custom codecs may encode and decode between arbitrary types, but some -module features are restricted to use specifically with -:term:`text encodings `, or with codecs that encode to +are :term:`text encodings `, which encode text to bytes (and +decode bytes to text), but there are also codecs provided that encode text to +text, and bytes to bytes. Custom codecs may encode and decode between arbitrary +types, but some module features are restricted to be used specifically with +:term:`text encodings ` or with codecs that encode to :class:`bytes`. The module defines the following functions for encoding and decoding with @@ -297,58 +297,56 @@ codec will handle encoding and decoding errors. Error Handlers ^^^^^^^^^^^^^^ -To simplify and standardize error handling, -codecs may implement different error handling schemes by -accepting the *errors* string argument. The following string values are -defined and implemented by all standard Python codecs: +To simplify and standardize error handling, codecs may implement different +error handling schemes by accepting the *errors* string argument: -.. tabularcolumns:: |l|L| - -+-------------------------+-----------------------------------------------+ -| Value | Meaning | -+=========================+===============================================+ -| ``'strict'`` | Raise :exc:`UnicodeError` (or a subclass); | -| | this is the default. Implemented in | -| | :func:`strict_errors`. | -+-------------------------+-----------------------------------------------+ -| ``'ignore'`` | Ignore the malformed data and continue | -| | without further notice. Implemented in | -| | :func:`ignore_errors`. | -+-------------------------+-----------------------------------------------+ - -The following error handlers are only applicable to -:term:`text encodings `: + >>> 'German ß, ♬'.encode(encoding='ascii', errors='backslashreplace') + b'German \\xdf, \\u266c' + >>> 'German ß, ♬'.encode(encoding='ascii', errors='xmlcharrefreplace') + b'German ß, ♬' .. index:: + pair: strict; error handler's name + pair: ignore; error handler's name + pair: replace; error handler's name + pair: backslashreplace; error handler's name + pair: surrogateescape; error handler's name single: ? (question mark); replacement character single: \ (backslash); escape sequence single: \x; escape sequence single: \u; escape sequence single: \U; escape sequence - single: \N; escape sequence + +The following error handlers can be used with all Python +:ref:`standard-encodings` codecs: + +.. tabularcolumns:: |l|L| +-------------------------+-----------------------------------------------+ | Value | Meaning | +=========================+===============================================+ -| ``'replace'`` | Replace with a suitable replacement | -| | marker; Python will use the official | -| | ``U+FFFD`` REPLACEMENT CHARACTER for the | -| | built-in codecs on decoding, and '?' on | -| | encoding. Implemented in | -| | :func:`replace_errors`. | +| ``'strict'`` | Raise :exc:`UnicodeError` (or a subclass), | +| | this is the default. Implemented in | +| | :func:`strict_errors`. | +-------------------------+-----------------------------------------------+ -| ``'xmlcharrefreplace'`` | Replace with the appropriate XML character | -| | reference (only for encoding). Implemented | -| | in :func:`xmlcharrefreplace_errors`. | +| ``'ignore'`` | Ignore the malformed data and continue without| +| | further notice. Implemented in | +| | :func:`ignore_errors`. | ++-------------------------+-----------------------------------------------+ +| ``'replace'`` | Replace with a replacement marker. On | +| | encoding, use ``?`` (ASCII character). On | +| | decoding, use ``�`` (U+FFFD, the official | +| | REPLACEMENT CHARACTER). Implemented in | +| | :func:`replace_errors`. | +-------------------------+-----------------------------------------------+ | ``'backslashreplace'`` | Replace with backslashed escape sequences. | +| | On encoding, use hexadecimal form of Unicode | +| | code point with formats ``\xhh`` ``\uxxxx`` | +| | ``\Uxxxxxxxx``. On decoding, use hexadecimal | +| | form of byte value with format ``\xhh``. | | | Implemented in | | | :func:`backslashreplace_errors`. | +-------------------------+-----------------------------------------------+ -| ``'namereplace'`` | Replace with ``\N{...}`` escape sequences | -| | (only for encoding). Implemented in | -| | :func:`namereplace_errors`. | -+-------------------------+-----------------------------------------------+ | ``'surrogateescape'`` | On decoding, replace byte with individual | | | surrogate code ranging from ``U+DC80`` to | | | ``U+DCFF``. This code will then be turned | @@ -358,27 +356,55 @@ The following error handlers are only applicable to | | more.) | +-------------------------+-----------------------------------------------+ +.. index:: + pair: xmlcharrefreplace; error handler's name + pair: namereplace; error handler's name + single: \N; escape sequence + +The following error handlers are only applicable to encoding (within +:term:`text encodings `): + ++-------------------------+-----------------------------------------------+ +| Value | Meaning | ++=========================+===============================================+ +| ``'xmlcharrefreplace'`` | Replace with XML/HTML numeric character | +| | reference, which is a decimal form of Unicode | +| | code point with format ``&#num;`` Implemented | +| | in :func:`xmlcharrefreplace_errors`. | ++-------------------------+-----------------------------------------------+ +| ``'namereplace'`` | Replace with ``\N{...}`` escape sequences, | +| | what appears in the braces is the Name | +| | property from Unicode Character Database. | +| | Implemented in :func:`namereplace_errors`. | ++-------------------------+-----------------------------------------------+ + +.. index:: + pair: surrogatepass; error handler's name + In addition, the following error handler is specific to the given codecs: +-------------------+------------------------+-------------------------------------------+ | Value | Codecs | Meaning | +===================+========================+===========================================+ -|``'surrogatepass'``| utf-8, utf-16, utf-32, | Allow encoding and decoding of surrogate | -| | utf-16-be, utf-16-le, | codes. These codecs normally treat the | -| | utf-32-be, utf-32-le | presence of surrogates as an error. | +|``'surrogatepass'``| utf-8, utf-16, utf-32, | Allow encoding and decoding surrogate code| +| | utf-16-be, utf-16-le, | point (``U+D800`` - ``U+DFFF``) as normal | +| | utf-32-be, utf-32-le | code point. Otherwise these codecs treat | +| | | the presence of surrogate code point in | +| | | :class:`str` as an error. | +-------------------+------------------------+-------------------------------------------+ .. versionadded:: 3.1 The ``'surrogateescape'`` and ``'surrogatepass'`` error handlers. .. versionchanged:: 3.4 - The ``'surrogatepass'`` error handlers now works with utf-16\* and utf-32\* codecs. + The ``'surrogatepass'`` error handler now works with utf-16\* and utf-32\* + codecs. .. versionadded:: 3.5 The ``'namereplace'`` error handler. .. versionchanged:: 3.5 - The ``'backslashreplace'`` error handlers now works with decoding and + The ``'backslashreplace'`` error handler now works with decoding and translating. The set of allowed values can be extended by registering a new named error @@ -421,42 +447,59 @@ functions: .. function:: strict_errors(exception) - Implements the ``'strict'`` error handling: each encoding or - decoding error raises a :exc:`UnicodeError`. + Implements the ``'strict'`` error handling. + Each encoding or decoding error raises a :exc:`UnicodeError`. -.. function:: replace_errors(exception) - Implements the ``'replace'`` error handling (for :term:`text encodings - ` only): substitutes ``'?'`` for encoding errors - (to be encoded by the codec), and ``'\ufffd'`` (the Unicode replacement - character) for decoding errors. +.. function:: ignore_errors(exception) + Implements the ``'ignore'`` error handling. -.. function:: ignore_errors(exception) + Malformed data is ignored; encoding or decoding is continued without + further notice. - Implements the ``'ignore'`` error handling: malformed data is ignored and - encoding or decoding is continued without further notice. +.. function:: replace_errors(exception) -.. function:: xmlcharrefreplace_errors(exception) + Implements the ``'replace'`` error handling. - Implements the ``'xmlcharrefreplace'`` error handling (for encoding with - :term:`text encodings ` only): the - unencodable character is replaced by an appropriate XML character reference. + Substitutes ``?`` (ASCII character) for encoding errors or ``�`` (U+FFFD, + the official REPLACEMENT CHARACTER) for decoding errors. .. function:: backslashreplace_errors(exception) - Implements the ``'backslashreplace'`` error handling (for - :term:`text encodings ` only): malformed data is - replaced by a backslashed escape sequence. + Implements the ``'backslashreplace'`` error handling. + + Malformed data is replaced by a backslashed escape sequence. + On encoding, use the hexadecimal form of Unicode code point with formats + ``\xhh`` ``\uxxxx`` ``\Uxxxxxxxx``. On decoding, use the hexadecimal form of + byte value with format ``\xhh``. + + .. versionchanged:: 3.5 + Works with decoding and translating. + + +.. function:: xmlcharrefreplace_errors(exception) + + Implements the ``'xmlcharrefreplace'`` error handling (for encoding within + :term:`text encoding` only). + + The unencodable character is replaced by an appropriate XML/HTML numeric + character reference, which is a decimal form of Unicode code point with + format ``&#num;`` . + .. function:: namereplace_errors(exception) - Implements the ``'namereplace'`` error handling (for encoding with - :term:`text encodings ` only): the - unencodable character is replaced by a ``\N{...}`` escape sequence. + Implements the ``'namereplace'`` error handling (for encoding within + :term:`text encoding` only). + + The unencodable character is replaced by a ``\N{...}`` escape sequence. The + set of characters that appear in the braces is the Name property from + Unicode Character Database. For example, the German lowercase letter ``'ß'`` + will be converted to byte sequence ``\N{LATIN SMALL LETTER SHARP S}`` . .. versionadded:: 3.5 @@ -470,7 +513,7 @@ The base :class:`Codec` class defines these methods which also define the function interfaces of the stateless encoder and decoder: -.. method:: Codec.encode(input[, errors]) +.. method:: Codec.encode(input, errors='strict') Encodes the object *input* and returns a tuple (output object, length consumed). For instance, :term:`text encoding` converts @@ -488,7 +531,7 @@ function interfaces of the stateless encoder and decoder: of the output object type in this situation. -.. method:: Codec.decode(input[, errors]) +.. method:: Codec.decode(input, errors='strict') Decodes the object *input* and returns a tuple (output object, length consumed). For instance, for a :term:`text encoding`, decoding converts @@ -555,7 +598,7 @@ define in order to be compatible with the Python codec registry. object. - .. method:: encode(object[, final]) + .. method:: encode(object, final=False) Encodes *object* (taking the current state of the encoder into account) and returns the resulting encoded object. If this is the last call to @@ -612,7 +655,7 @@ define in order to be compatible with the Python codec registry. object. - .. method:: decode(object[, final]) + .. method:: decode(object, final=False) Decodes *object* (taking the current state of the decoder into account) and returns the resulting decoded object. If this is the last call to @@ -694,8 +737,9 @@ compatible with the Python codec registry. .. method:: writelines(list) - Writes the concatenated list of strings to the stream (possibly by reusing - the :meth:`write` method). The standard bytes-to-bytes codecs + Writes the concatenated iterable of strings to the stream (possibly by reusing + the :meth:`write` method). Infinite or + very large iterables are not supported. The standard bytes-to-bytes codecs do not support this method. @@ -745,7 +789,7 @@ compatible with the Python codec registry. :func:`register_error`. - .. method:: read([size[, chars, [firstline]]]) + .. method:: read(size=-1, chars=-1, firstline=False) Decodes data from the stream and returns the resulting object. @@ -771,7 +815,7 @@ compatible with the Python codec registry. available on the stream, these should be read too. - .. method:: readline([size[, keepends]]) + .. method:: readline(size=None, keepends=True) Read one line from the input stream and return the decoded data. @@ -782,7 +826,7 @@ compatible with the Python codec registry. returned. - .. method:: readlines([sizehint[, keepends]]) + .. method:: readlines(sizehint=None, keepends=True) Read all lines available on the input stream and return them as a list of lines. @@ -873,7 +917,7 @@ Encodings and Unicode --------------------- Strings are stored internally as sequences of code points in -range ``0x0``--``0x10FFFF``. (See :pep:`393` for +range ``U+0000``--``U+10FFFF``. (See :pep:`393` for more details about the implementation.) Once a string object is used outside of CPU and memory, endianness and how these arrays are stored as bytes become an issue. As with other @@ -911,7 +955,7 @@ there's the so called BOM ("Byte Order Mark"). This is the Unicode character ``U+FEFF``. This character can be prepended to every ``UTF-16`` or ``UTF-32`` byte sequence. The byte swapped version of this character (``0xFFFE``) is an illegal character that may not appear in a Unicode text. So when the -first character in an ``UTF-16`` or ``UTF-32`` byte sequence +first character in a ``UTF-16`` or ``UTF-32`` byte sequence appears to be a ``U+FFFE`` the bytes have to be swapped on decoding. Unfortunately the character ``U+FEFF`` had a second purpose as a ``ZERO WIDTH NO-BREAK SPACE``: a character that has no width and doesn't allow @@ -923,7 +967,7 @@ it's a device to determine the storage layout of the encoded bytes, and vanishes once the byte sequence has been decoded into a string; as a ``ZERO WIDTH NO-BREAK SPACE`` it's a normal character that will be decoded like any other. -There's another encoding that is able to encoding the full range of Unicode +There's another encoding that is able to encode the full range of Unicode characters: UTF-8. UTF-8 is an 8-bit encoding, which means there are no issues with byte order in UTF-8. Each byte in a UTF-8 byte sequence consists of two parts: marker bits (the most significant bits) and payload bits. The marker bits @@ -954,7 +998,7 @@ encoding was used for encoding a string. Each charmap encoding can decode any random byte sequence. However that's not possible with UTF-8, as UTF-8 byte sequences have a structure that doesn't allow arbitrary byte sequences. To increase the reliability with which a UTF-8 encoding can be -detected, Microsoft invented a variant of UTF-8 (that Python 2.5 calls +detected, Microsoft invented a variant of UTF-8 (that Python calls ``"utf-8-sig"``) for its Notepad program: Before any of the Unicode characters is written to the file, a UTF-8 encoded BOM (which looks like this as a byte sequence: ``0xef``, ``0xbb``, ``0xbf``) is written. As it's rather improbable @@ -1422,7 +1466,7 @@ Internationalized Domain Names (IDN)). It builds upon the ``punycode`` encoding and :mod:`stringprep`. If you need the IDNA 2008 standard from :rfc:`5891` and :rfc:`5895`, use the -third-party `idna module _`. +third-party `idna module `_. These RFCs together define a protocol to support non-ASCII characters in domain names. A domain name containing non-ASCII characters (such as diff --git a/Doc/library/collections.abc.rst b/Doc/library/collections.abc.rst index 2345e78a17e4f5..2c941b47748d36 100644 --- a/Doc/library/collections.abc.rst +++ b/Doc/library/collections.abc.rst @@ -14,7 +14,7 @@ .. testsetup:: * - from collections import * + from collections.abc import * import itertools __name__ = '' @@ -24,6 +24,89 @@ This module provides :term:`abstract base classes ` that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a mapping. +An :func:`issubclass` or :func:`isinstance` test for an interface works in one +of three ways. + +1) A newly written class can inherit directly from one of the +abstract base classes. The class must supply the required abstract +methods. The remaining mixin methods come from inheritance and can be +overridden if desired. Other methods may be added as needed: + +.. testcode:: + + class C(Sequence): # Direct inheritance + def __init__(self): ... # Extra method not required by the ABC + def __getitem__(self, index): ... # Required abstract method + def __len__(self): ... # Required abstract method + def count(self, value): ... # Optionally override a mixin method + +.. doctest:: + + >>> issubclass(C, Sequence) + True + >>> isinstance(C(), Sequence) + True + +2) Existing classes and built-in classes can be registered as "virtual +subclasses" of the ABCs. Those classes should define the full API +including all of the abstract methods and all of the mixin methods. +This lets users rely on :func:`issubclass` or :func:`isinstance` tests +to determine whether the full interface is supported. The exception to +this rule is for methods that are automatically inferred from the rest +of the API: + +.. testcode:: + + class D: # No inheritance + def __init__(self): ... # Extra method not required by the ABC + def __getitem__(self, index): ... # Abstract method + def __len__(self): ... # Abstract method + def count(self, value): ... # Mixin method + def index(self, value): ... # Mixin method + + Sequence.register(D) # Register instead of inherit + +.. doctest:: + + >>> issubclass(D, Sequence) + True + >>> isinstance(D(), Sequence) + True + +In this example, class :class:`D` does not need to define +``__contains__``, ``__iter__``, and ``__reversed__`` because the +:ref:`in-operator `, the :term:`iteration ` +logic, and the :func:`reversed` function automatically fall back to +using ``__getitem__`` and ``__len__``. + +3) Some simple interfaces are directly recognizable by the presence of +the required methods (unless those methods have been set to +:const:`None`): + +.. testcode:: + + class E: + def __iter__(self): ... + def __next__(next): ... + +.. doctest:: + + >>> issubclass(E, Iterable) + True + >>> isinstance(E(), Iterable) + True + +Complex interfaces do not support this last technique because an +interface is more than just the presence of method names. Interfaces +specify semantics and relationships between methods that cannot be +inferred solely from the presence of specific method names. For +example, knowing that a class supplies ``__getitem__``, ``__len__``, and +``__iter__`` is insufficient for distinguishing a :class:`Sequence` from +a :class:`Mapping`. + +.. versionadded:: 3.9 + These abstract classes now support ``[]``. See :ref:`types-genericalias` + and :pep:`585`. .. _collections-abstract-base-classes: @@ -34,67 +117,86 @@ The collections module offers the following :term:`ABCs `: .. tabularcolumns:: |l|L|L|L| -========================== ====================== ======================= ==================================================== -ABC Inherits from Abstract Methods Mixin Methods -========================== ====================== ======================= ==================================================== -:class:`Container` ``__contains__`` -:class:`Hashable` ``__hash__`` -:class:`Iterable` ``__iter__`` -:class:`Iterator` :class:`Iterable` ``__next__`` ``__iter__`` -:class:`Reversible` :class:`Iterable` ``__reversed__`` -:class:`Generator` :class:`Iterator` ``send``, ``throw`` ``close``, ``__iter__``, ``__next__`` -:class:`Sized` ``__len__`` -:class:`Callable` ``__call__`` -:class:`Collection` :class:`Sized`, ``__contains__``, - :class:`Iterable`, ``__iter__``, - :class:`Container` ``__len__`` - -:class:`Sequence` :class:`Reversible`, ``__getitem__``, ``__contains__``, ``__iter__``, ``__reversed__``, - :class:`Collection` ``__len__`` ``index``, and ``count`` - -:class:`MutableSequence` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods and - ``__setitem__``, ``append``, ``reverse``, ``extend``, ``pop``, - ``__delitem__``, ``remove``, and ``__iadd__`` - ``__len__``, - ``insert`` - -:class:`ByteString` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods - ``__len__`` - -:class:`Set` :class:`Collection` ``__contains__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``, - ``__iter__``, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``, - ``__len__`` ``__sub__``, ``__xor__``, and ``isdisjoint`` - -:class:`MutableSet` :class:`Set` ``__contains__``, Inherited :class:`Set` methods and - ``__iter__``, ``clear``, ``pop``, ``remove``, ``__ior__``, - ``__len__``, ``__iand__``, ``__ixor__``, and ``__isub__`` - ``add``, - ``discard`` - -:class:`Mapping` :class:`Collection` ``__getitem__``, ``__contains__``, ``keys``, ``items``, ``values``, - ``__iter__``, ``get``, ``__eq__``, and ``__ne__`` - ``__len__`` - -:class:`MutableMapping` :class:`Mapping` ``__getitem__``, Inherited :class:`Mapping` methods and - ``__setitem__``, ``pop``, ``popitem``, ``clear``, ``update``, - ``__delitem__``, and ``setdefault`` - ``__iter__``, - ``__len__`` - - -:class:`MappingView` :class:`Sized` ``__len__`` -:class:`ItemsView` :class:`MappingView`, ``__contains__``, - :class:`Set` ``__iter__`` -:class:`KeysView` :class:`MappingView`, ``__contains__``, - :class:`Set` ``__iter__`` -:class:`ValuesView` :class:`MappingView`, ``__contains__``, ``__iter__`` - :class:`Collection` -:class:`Awaitable` ``__await__`` -:class:`Coroutine` :class:`Awaitable` ``send``, ``throw`` ``close`` -:class:`AsyncIterable` ``__aiter__`` -:class:`AsyncIterator` :class:`AsyncIterable` ``__anext__`` ``__aiter__`` -:class:`AsyncGenerator` :class:`AsyncIterator` ``asend``, ``athrow`` ``aclose``, ``__aiter__``, ``__anext__`` -========================== ====================== ======================= ==================================================== +============================== ====================== ======================= ==================================================== +ABC Inherits from Abstract Methods Mixin Methods +============================== ====================== ======================= ==================================================== +:class:`Container` [1]_ ``__contains__`` +:class:`Hashable` [1]_ ``__hash__`` +:class:`Iterable` [1]_ [2]_ ``__iter__`` +:class:`Iterator` [1]_ :class:`Iterable` ``__next__`` ``__iter__`` +:class:`Reversible` [1]_ :class:`Iterable` ``__reversed__`` +:class:`Generator` [1]_ :class:`Iterator` ``send``, ``throw`` ``close``, ``__iter__``, ``__next__`` +:class:`Sized` [1]_ ``__len__`` +:class:`Callable` [1]_ ``__call__`` +:class:`Collection` [1]_ :class:`Sized`, ``__contains__``, + :class:`Iterable`, ``__iter__``, + :class:`Container` ``__len__`` + +:class:`Sequence` :class:`Reversible`, ``__getitem__``, ``__contains__``, ``__iter__``, ``__reversed__``, + :class:`Collection` ``__len__`` ``index``, and ``count`` + +:class:`MutableSequence` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods and + ``__setitem__``, ``append``, ``reverse``, ``extend``, ``pop``, + ``__delitem__``, ``remove``, and ``__iadd__`` + ``__len__``, + ``insert`` + +:class:`ByteString` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods + ``__len__`` + +:class:`Set` :class:`Collection` ``__contains__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``, + ``__iter__``, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``, + ``__len__`` ``__sub__``, ``__xor__``, and ``isdisjoint`` + +:class:`MutableSet` :class:`Set` ``__contains__``, Inherited :class:`Set` methods and + ``__iter__``, ``clear``, ``pop``, ``remove``, ``__ior__``, + ``__len__``, ``__iand__``, ``__ixor__``, and ``__isub__`` + ``add``, + ``discard`` + +:class:`Mapping` :class:`Collection` ``__getitem__``, ``__contains__``, ``keys``, ``items``, ``values``, + ``__iter__``, ``get``, ``__eq__``, and ``__ne__`` + ``__len__`` + +:class:`MutableMapping` :class:`Mapping` ``__getitem__``, Inherited :class:`Mapping` methods and + ``__setitem__``, ``pop``, ``popitem``, ``clear``, ``update``, + ``__delitem__``, and ``setdefault`` + ``__iter__``, + ``__len__`` + + +:class:`MappingView` :class:`Sized` ``__len__`` +:class:`ItemsView` :class:`MappingView`, ``__contains__``, + :class:`Set` ``__iter__`` +:class:`KeysView` :class:`MappingView`, ``__contains__``, + :class:`Set` ``__iter__`` +:class:`ValuesView` :class:`MappingView`, ``__contains__``, ``__iter__`` + :class:`Collection` +:class:`Awaitable` [1]_ ``__await__`` +:class:`Coroutine` [1]_ :class:`Awaitable` ``send``, ``throw`` ``close`` +:class:`AsyncIterable` [1]_ ``__aiter__`` +:class:`AsyncIterator` [1]_ :class:`AsyncIterable` ``__anext__`` ``__aiter__`` +:class:`AsyncGenerator` [1]_ :class:`AsyncIterator` ``asend``, ``athrow`` ``aclose``, ``__aiter__``, ``__anext__`` +============================== ====================== ======================= ==================================================== + + +.. rubric:: Footnotes + +.. [1] These ABCs override :meth:`object.__subclasshook__` to support + testing an interface by verifying the required methods are present + and have not been set to :const:`None`. This only works for simple + interfaces. More complex interfaces require registration or direct + subclassing. + +.. [2] Checking ``isinstance(obj, Iterable)`` detects classes that are + registered as :class:`Iterable` or that have an :meth:`__iter__` + method, but it does not detect classes that iterate with the + :meth:`__getitem__` method. The only reliable way to determine + whether an object is :term:`iterable` is to call ``iter(obj)``. + + +Collections Abstract Base Classes -- Detailed Descriptions +---------------------------------------------------------- .. class:: Container @@ -244,8 +346,10 @@ ABC Inherits from Abstract Methods Mixin .. versionadded:: 3.6 +Examples and Recipes +-------------------- -These ABCs allow us to ask classes or instances if they provide +ABCs allow us to ask classes or instances if they provide particular functionality, for example:: size = None diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 94166ec6c754a7..67b64ddda7a2ca 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -271,12 +271,12 @@ For example:: .. versionadded:: 3.1 .. versionchanged:: 3.7 As a :class:`dict` subclass, :class:`Counter` - Inherited the capability to remember insertion order. Math operations + inherited the capability to remember insertion order. Math operations on *Counter* objects also preserve order. Results are ordered according to when an element is first encountered in the left operand and then by the order encountered in the right operand. - Counter objects support three methods beyond those available for all + Counter objects support additional methods beyond those available for all dictionaries: .. method:: elements() @@ -343,7 +343,7 @@ All of those tests treat missing elements as having zero counts so that ``Counter(a=1) == Counter(a=1, b=0)`` returns true. .. versionadded:: 3.10 - Rich comparison operations we were added + Rich comparison operations were added. .. versionchanged:: 3.10 In equality tests, missing elements are treated as having zero counts. @@ -366,19 +366,26 @@ Several mathematical operations are provided for combining :class:`Counter` objects to produce multisets (counters that have counts greater than zero). Addition and subtraction combine counters by adding or subtracting the counts of corresponding elements. Intersection and union return the minimum and -maximum of corresponding counts. Each operation can accept inputs with signed +maximum of corresponding counts. Equality and inclusion compare +corresponding counts. Each operation can accept inputs with signed counts, but the output will exclude results with counts of zero or less. +.. doctest:: + >>> c = Counter(a=3, b=1) >>> d = Counter(a=1, b=2) >>> c + d # add two counters together: c[x] + d[x] Counter({'a': 4, 'b': 3}) >>> c - d # subtract (keeping only positive counts) Counter({'a': 2}) - >>> c & d # intersection: min(c[x], d[x]) # doctest: +SKIP + >>> c & d # intersection: min(c[x], d[x]) Counter({'a': 1, 'b': 1}) >>> c | d # union: max(c[x], d[x]) Counter({'a': 3, 'b': 2}) + >>> c == d # equality: c[x] == d[x] + False + >>> c <= d # inclusion: c[x] <= d[x] + False Unary addition and subtraction are shortcuts for adding an empty counter or subtracting from an empty counter. @@ -707,9 +714,9 @@ stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``, :class:`defaultdict` objects ---------------------------- -.. class:: defaultdict([default_factory[, ...]]) +.. class:: defaultdict(default_factory=None, /, [...]) - Returns a new dictionary-like object. :class:`defaultdict` is a subclass of the + Return a new dictionary-like object. :class:`defaultdict` is a subclass of the built-in :class:`dict` class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the :class:`dict` class and is not documented here. @@ -1085,18 +1092,35 @@ Some differences from :class:`dict` still remain: Space efficiency, iteration speed, and the performance of update operations were secondary. -* Algorithmically, :class:`OrderedDict` can handle frequent reordering - operations better than :class:`dict`. This makes it suitable for tracking - recent accesses (for example in an `LRU cache - `_). +* The :class:`OrderedDict` algorithm can handle frequent reordering operations + better than :class:`dict`. As shown in the recipes below, this makes it + suitable for implementing various kinds of LRU caches. * The equality operation for :class:`OrderedDict` checks for matching order. + A regular :class:`dict` can emulate the order sensitive equality test with + ``p == q and all(k1 == k2 for k1, k2 in zip(p, q))``. + * The :meth:`popitem` method of :class:`OrderedDict` has a different signature. It accepts an optional argument to specify which item is popped. -* :class:`OrderedDict` has a :meth:`move_to_end` method to - efficiently reposition an element to an endpoint. + A regular :class:`dict` can emulate OrderedDict's ``od.popitem(last=True)`` + with ``d.popitem()`` which is guaranteed to pop the rightmost (last) item. + + A regular :class:`dict` can emulate OrderedDict's ``od.popitem(last=False)`` + with ``(k := next(iter(d)), d.pop(k))`` which will return and remove the + leftmost (first) item if it exists. + +* :class:`OrderedDict` has a :meth:`move_to_end` method to efficiently + reposition an element to an endpoint. + + A regular :class:`dict` can emulate OrderedDict's ``od.move_to_end(k, + last=True)`` with ``d[k] = d.pop(k)`` which will move the key and its + associated value to the rightmost (last) position. + + A regular :class:`dict` does not have an efficient equivalent for + OrderedDict's ``od.move_to_end(k, last=False)`` which moves the key + and its associated value to the leftmost (first) position. * Until Python 3.8, :class:`dict` lacked a :meth:`__reversed__` method. @@ -1120,14 +1144,16 @@ Some differences from :class:`dict` still remain: Move an existing *key* to either end of an ordered dictionary. The item is moved to the right end if *last* is true (the default) or to the beginning if *last* is false. Raises :exc:`KeyError` if the *key* does - not exist:: + not exist: + + .. doctest:: >>> d = OrderedDict.fromkeys('abcde') >>> d.move_to_end('b') - >>> ''.join(d.keys()) + >>> ''.join(d) 'acdeb' >>> d.move_to_end('b', last=False) - >>> ''.join(d.keys()) + >>> ''.join(d) 'bacde' .. versionadded:: 3.2 @@ -1171,28 +1197,101 @@ original insertion position is changed and moved to the end:: self.move_to_end(key) An :class:`OrderedDict` would also be useful for implementing -variants of :func:`functools.lru_cache`:: +variants of :func:`functools.lru_cache`: - class LRU(OrderedDict): - 'Limit size, evicting the least recently looked-up key when full' +.. testcode:: - def __init__(self, maxsize=128, /, *args, **kwds): - self.maxsize = maxsize - super().__init__(*args, **kwds) + from time import time - def __getitem__(self, key): - value = super().__getitem__(key) - self.move_to_end(key) - return value + class TimeBoundedLRU: + "LRU Cache that invalidates and refreshes old entries." - def __setitem__(self, key, value): - if key in self: - self.move_to_end(key) - super().__setitem__(key, value) - if len(self) > self.maxsize: - oldest = next(iter(self)) - del self[oldest] + def __init__(self, func, maxsize=128, maxage=30): + self.cache = OrderedDict() # { args : (timestamp, result)} + self.func = func + self.maxsize = maxsize + self.maxage = maxage + + def __call__(self, *args): + if args in self.cache: + self.cache.move_to_end(args) + timestamp, result = self.cache[args] + if time() - timestamp <= self.maxage: + return result + result = self.func(*args) + self.cache[args] = time(), result + if len(self.cache) > self.maxsize: + self.cache.popitem(0) + return result + + +.. testcode:: + + class MultiHitLRUCache: + """ LRU cache that defers caching a result until + it has been requested multiple times. + + To avoid flushing the LRU cache with one-time requests, + we don't cache until a request has been made more than once. + + """ + + def __init__(self, func, maxsize=128, maxrequests=4096, cache_after=1): + self.requests = OrderedDict() # { uncached_key : request_count } + self.cache = OrderedDict() # { cached_key : function_result } + self.func = func + self.maxrequests = maxrequests # max number of uncached requests + self.maxsize = maxsize # max number of stored return values + self.cache_after = cache_after + + def __call__(self, *args): + if args in self.cache: + self.cache.move_to_end(args) + return self.cache[args] + result = self.func(*args) + self.requests[args] = self.requests.get(args, 0) + 1 + if self.requests[args] <= self.cache_after: + self.requests.move_to_end(args) + if len(self.requests) > self.maxrequests: + self.requests.popitem(0) + else: + self.requests.pop(args, None) + self.cache[args] = result + if len(self.cache) > self.maxsize: + self.cache.popitem(0) + return result +.. doctest:: + :hide: + + >>> def square(x): + ... return x * x + ... + >>> f = MultiHitLRUCache(square, maxsize=4, maxrequests=6) + >>> list(map(f, range(10))) # First requests, don't cache + [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + >>> f(4) # Cache the second request + 16 + >>> f(6) # Cache the second request + 36 + >>> f(2) # The first request aged out, so don't cache + 4 + >>> f(6) # Cache hit + 36 + >>> f(4) # Cache hit and move to front + 16 + >>> list(f.cache.values()) + [36, 16] + >>> set(f.requests).isdisjoint(f.cache) + True + >>> list(map(f, [9, 8, 7])) # Cache these second requests + [81, 64, 49] + >>> list(map(f, [7, 9])) # Cache hits + [49, 81] + >>> list(f.cache.values()) + [16, 64, 49, 81] + >>> set(f.requests).isdisjoint(f.cache) + True :class:`UserDict` objects ------------------------- @@ -1209,7 +1308,7 @@ attribute. regular dictionary, which is accessible via the :attr:`data` attribute of :class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is initialized with its contents; note that a reference to *initialdata* will not - be kept, allowing it be used for other purposes. + be kept, allowing it to be used for other purposes. In addition to supporting the methods and operations of mappings, :class:`UserDict` instances provide the following attribute: diff --git a/Doc/library/compileall.rst b/Doc/library/compileall.rst index 5c6e68f9304753..de34664acb84ab 100644 --- a/Doc/library/compileall.rst +++ b/Doc/library/compileall.rst @@ -166,9 +166,10 @@ Public functions If *force* is true, modules are re-compiled even if the timestamps are up to date. - If *rx* is given, its search method is called on the complete path to each + If *rx* is given, its ``search`` method is called on the complete path to each file considered for compilation, and if it returns a true value, the file - is skipped. + is skipped. This can be used to exclude files matching a regular expression, + given as a :ref:`re.Pattern ` object. If *quiet* is ``False`` or ``0`` (the default), the filenames and other information are printed to standard out. Set to ``1``, only errors are @@ -242,9 +243,10 @@ Public functions cases where the source file does not exist at the time the byte-code file is executed. - If *rx* is given, its search method is passed the full path name to the + If *rx* is given, its ``search`` method is passed the full path name to the file being compiled, and if it returns a true value, the file is not - compiled and ``True`` is returned. + compiled and ``True`` is returned. This can be used to exclude files matching + a regular expression, given as a :ref:`re.Pattern ` object. If *quiet* is ``False`` or ``0`` (the default), the filenames and other information are printed to standard out. Set to ``1``, only errors are diff --git a/Doc/library/concurrent.futures.rst b/Doc/library/concurrent.futures.rst index d57f8ce23d12c4..70a17a23119c12 100644 --- a/Doc/library/concurrent.futures.rst +++ b/Doc/library/concurrent.futures.rst @@ -30,7 +30,7 @@ Executor Objects .. method:: submit(fn, /, *args, **kwargs) - Schedules the callable, *fn*, to be executed as ``fn(*args **kwargs)`` + Schedules the callable, *fn*, to be executed as ``fn(*args, **kwargs)`` and returns a :class:`Future` object representing the execution of the callable. :: @@ -350,7 +350,7 @@ The :class:`Future` class encapsulates the asynchronous execution of a callable. If the future is cancelled before completing then :exc:`.CancelledError` will be raised. - If the call raised, this method will raise the same exception. + If the call raised an exception, this method will raise the same exception. .. method:: exception(timeout=None) @@ -435,7 +435,8 @@ Module Functions .. function:: wait(fs, timeout=None, return_when=ALL_COMPLETED) Wait for the :class:`Future` instances (possibly created by different - :class:`Executor` instances) given by *fs* to complete. Returns a named + :class:`Executor` instances) given by *fs* to complete. Duplicate futures + given to *fs* are removed and will be returned only once. Returns a named 2-tuple of sets. The first set, named ``done``, contains the futures that completed (finished or cancelled futures) before the wait completed. The second set, named ``not_done``, contains the futures that did not complete diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 646e8a317f52c3..72aa20d73d8bd3 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -46,6 +46,11 @@ can be customized by end users easily. import configparser +.. testcleanup:: + + import os + os.remove("example.ini") + Quick Start ----------- @@ -256,11 +261,15 @@ A configuration file consists of sections, each led by a ``[section]`` header, followed by key/value entries separated by a specific string (``=`` or ``:`` by default [1]_). By default, section names are case sensitive but keys are not [1]_. Leading and trailing whitespace is removed from keys and values. -Values can be omitted, in which case the key/value delimiter may also be left +Values can be omitted if the parser is configured to allow it [1]_, +in which case the key/value delimiter may also be left out. Values can also span multiple lines, as long as they are indented deeper than the first line of the value. Depending on the parser's mode, blank lines may be treated as parts of multiline values or ignored. +By default, a valid section name can be any string that does not contain '\\n' or ']'. +To change this, see :attr:`ConfigParser.SECTCRE`. + Configuration files may include comments, prefixed by specific characters (``#`` and ``;`` by default [1]_). Comments may appear on their own on an otherwise empty line, possibly indented. [1]_ @@ -338,7 +347,8 @@ from ``get()`` calls. my_pictures: %(my_dir)s/Pictures [Escape] - gain: 80%% # use a %% to escape the % sign (% is the only character that needs to be escaped) + # use a %% to escape the % sign (% is the only character that needs to be escaped): + gain: 80%% In the example above, :class:`ConfigParser` with *interpolation* set to ``BasicInterpolation()`` would resolve ``%(home_dir)s`` to the value of @@ -373,7 +383,8 @@ from ``get()`` calls. my_pictures: ${my_dir}/Pictures [Escape] - cost: $$80 # use a $$ to escape the $ sign ($ is the only character that needs to be escaped) + # use a $$ to escape the $ sign ($ is the only character that needs to be escaped): + cost: $$80 Values from other sections can be fetched as well: @@ -1153,6 +1164,13 @@ ConfigParser Objects *space_around_delimiters* is true, delimiters between keys and values are surrounded by spaces. + .. note:: + + Comments in the original configuration file are not preserved when + writing the configuration back. + What is considered a comment, depends on the given values for + *comment_prefix* and *inline_comment_prefix*. + .. method:: remove_option(section, option) diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst index b92f703509fcac..7c0b8314079683 100644 --- a/Doc/library/contextlib.rst +++ b/Doc/library/contextlib.rst @@ -130,7 +130,9 @@ Functions and classes provided: either as decorators or with :keyword:`async with` statements:: import time + from contextlib import asynccontextmanager + @asynccontextmanager async def timeit(): now = time.monotonic() try: @@ -171,7 +173,7 @@ Functions and classes provided: from contextlib import closing from urllib.request import urlopen - with closing(urlopen('http://www.python.org')) as page: + with closing(urlopen('https://www.python.org')) as page: for line in page: print(line) @@ -179,7 +181,7 @@ Functions and classes provided: ``page.close()`` will be called when the :keyword:`with` block is exited. -.. class:: aclosing(thing) +.. function:: aclosing(thing) Return an async context manager that calls the ``aclose()`` method of *thing* upon completion of the block. This is basically equivalent to:: @@ -267,8 +269,9 @@ Functions and classes provided: .. function:: suppress(*exceptions) Return a context manager that suppresses any of the specified exceptions - if they occur in the body of a with statement and then resumes execution - with the first statement following the end of the with statement. + if they occur in the body of a :keyword:`!with` statement and then + resumes execution with the first statement following the end of the + :keyword:`!with` statement. As with any other mechanism that completely suppresses exceptions, this context manager should be used only to cover very specific errors where @@ -312,10 +315,11 @@ Functions and classes provided: For example, the output of :func:`help` normally is sent to *sys.stdout*. You can capture that output in a string by redirecting the output to an - :class:`io.StringIO` object:: + :class:`io.StringIO` object. The replacement stream is returned from the + ``__enter__`` method and so is available as the target of the + :keyword:`with` statement:: - f = io.StringIO() - with redirect_stdout(f): + with redirect_stdout(io.StringIO()) as f: help(pow) s = f.getvalue() @@ -481,6 +485,9 @@ Functions and classes provided: # the with statement, even if attempts to open files later # in the list raise an exception + The :meth:`__enter__` method returns the :class:`ExitStack` instance, and + performs no additional operations. + Each instance maintains a stack of registered callbacks that are called in reverse order when the instance is closed (either explicitly or implicitly at the end of a :keyword:`with` statement). Note that callbacks are *not* @@ -578,7 +585,7 @@ Functions and classes provided: The :meth:`close` method is not implemented, :meth:`aclose` must be used instead. - .. method:: enter_async_context(cm) + .. coroutinemethod:: enter_async_context(cm) Similar to :meth:`enter_context` but expects an asynchronous context manager. @@ -592,7 +599,7 @@ Functions and classes provided: Similar to :meth:`callback` but expects a coroutine function. - .. method:: aclose() + .. coroutinemethod:: aclose() Similar to :meth:`close` but properly handles awaitables. diff --git a/Doc/library/contextvars.rst b/Doc/library/contextvars.rst index 14ac47f4c9eb16..be1dd0c9eb57e8 100644 --- a/Doc/library/contextvars.rst +++ b/Doc/library/contextvars.rst @@ -94,7 +94,7 @@ Context Variables # var.get() would raise a LookupError. -.. class:: contextvars.Token +.. class:: Token *Token* objects are returned by the :meth:`ContextVar.set` method. They can be passed to the :meth:`ContextVar.reset` method to revert diff --git a/Doc/library/copy.rst b/Doc/library/copy.rst index 01ebf198d7c501..a8bc2fa55ea8c3 100644 --- a/Doc/library/copy.rst +++ b/Doc/library/copy.rst @@ -60,7 +60,7 @@ The :func:`deepcopy` function avoids these problems by: components copied. This module does not copy types like module, method, stack trace, stack frame, -file, socket, window, array, or any similar types. It does "copy" functions and +file, socket, window, or any similar types. It does "copy" functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the :mod:`pickle` module. diff --git a/Doc/library/copyreg.rst b/Doc/library/copyreg.rst index 43920210095951..dc35965be3e40d 100644 --- a/Doc/library/copyreg.rst +++ b/Doc/library/copyreg.rst @@ -33,8 +33,8 @@ Such constructors may be factory functions or class instances. The optional *constructor* parameter, if provided, is a callable object which can be used to reconstruct the object when called with the tuple of arguments - returned by *function* at pickling time. :exc:`TypeError` will be raised if - *object* is a class or *constructor* is not callable. + returned by *function* at pickling time. A :exc:`TypeError` is raised if the + *constructor* is not callable. See the :mod:`pickle` module for more details on the interface expected of *function* and *constructor*. Note that the diff --git a/Doc/library/crypt.rst b/Doc/library/crypt.rst index d25c626a175854..e795f10f50eec3 100644 --- a/Doc/library/crypt.rst +++ b/Doc/library/crypt.rst @@ -4,6 +4,7 @@ .. module:: crypt :platform: Unix :synopsis: The crypt() function used to check Unix passwords. + :deprecated: .. moduleauthor:: Steven D. Majewski .. sectionauthor:: Steven D. Majewski @@ -15,6 +16,11 @@ single: crypt(3) pair: cipher; DES +.. deprecated:: 3.11 + The :mod:`crypt` module is deprecated + (see :pep:`PEP 594 <594#crypt>` for details and alternatives). + The :mod:`hashlib` module is a potential replacement for certain use cases. + -------------- This module implements an interface to the :manpage:`crypt(3)` routine, which is @@ -92,8 +98,7 @@ The :mod:`crypt` module defines the following functions: :func:`mksalt`, one of the ``crypt.METHOD_*`` values (though not all may be available on all platforms), or a full encrypted password including salt, as returned by this function. If *salt* is not - provided, the strongest method will be used (as returned by - :func:`methods`). + provided, the strongest method available in :attr:`methods` will be used. Checking a password is usually done by passing the plain-text password as *word* and the full results of a previous :func:`crypt` call, @@ -121,8 +126,8 @@ The :mod:`crypt` module defines the following functions: .. function:: mksalt(method=None, *, rounds=None) Return a randomly generated salt of the specified method. If no - *method* is given, the strongest method available as returned by - :func:`methods` is used. + *method* is given, the strongest method available in :attr:`methods` is + used. The return value is a string suitable for passing as the *salt* argument to :func:`crypt`. diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 7a72c26d5badeb..899ce0225ce7f3 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -94,8 +94,8 @@ The :mod:`csv` module defines the following functions: :class:`Dialect` class or one of the strings returned by the :func:`list_dialects` function. The other optional *fmtparams* keyword arguments can be given to override individual formatting parameters in the current - dialect. For full details about the dialect and formatting parameters, see - section :ref:`csv-fmt-params`. To make it + dialect. For full details about dialects and formatting parameters, see + the :ref:`csv-fmt-params` section. To make it as easy as possible to interface with modules which implement the DB API, the value :const:`None` is written as the empty string. While this isn't a reversible transformation, it makes it easier to dump SQL NULL data values to @@ -117,7 +117,7 @@ The :mod:`csv` module defines the following functions: Associate *dialect* with *name*. *name* must be a string. The dialect can be specified either by passing a sub-class of :class:`Dialect`, or by *fmtparams* keyword arguments, or both, with keyword arguments overriding - parameters of the dialect. For full details about the dialect and formatting + parameters of the dialect. For full details about dialects and formatting parameters, see section :ref:`csv-fmt-params`. @@ -225,9 +225,21 @@ The :mod:`csv` module defines the following classes: .. class:: Dialect - The :class:`Dialect` class is a container class relied on primarily for its - attributes, which are used to define the parameters for a specific - :class:`reader` or :class:`writer` instance. + The :class:`Dialect` class is a container class whose attributes contain + information for how to handle doublequotes, whitespace, delimiters, etc. + Due to the lack of a strict CSV specification, different applications + produce subtly different CSV data. :class:`Dialect` instances define how + :class:`reader` and :class:`writer` instances behave. + + All available :class:`Dialect` names are returned by :func:`list_dialects`, + and they can be registered with specific :class:`reader` and :class:`writer` + classes through their initializer (``__init__``) functions like this:: + + import csv + + with open('students.csv', 'w', newline='') as csvfile: + writer = csv.writer(csvfile, dialect='unix') + ^^^^^^^^^^^^^^ .. class:: excel() @@ -269,6 +281,20 @@ The :mod:`csv` module defines the following classes: Analyze the sample text (presumed to be in CSV format) and return :const:`True` if the first row appears to be a series of column headers. + Inspecting each column, one of two key criteria will be considered to + estimate if the sample contains a header: + + - the second through n-th rows contain numeric values + - the second through n-th rows contain strings where at least one value's + length differs from that of the putative header of that column. + + Twenty rows after the first row are sampled; if more than half of columns + + rows meet the criteria, :const:`True` is returned. + + .. note:: + + This method is a rough heuristic and may produce both false positives and + negatives. An example for :class:`Sniffer` use:: @@ -405,8 +431,8 @@ Reader objects (:class:`DictReader` instances and objects returned by the Return the next row of the reader's iterable object as a list (if the object was returned from :func:`reader`) or a dict (if it is a :class:`DictReader` - instance), parsed according to the current dialect. Usually you should call - this as ``next(reader)``. + instance), parsed according to the current :class:`Dialect`. Usually you + should call this as ``next(reader)``. Reader objects have the following public attributes: @@ -446,9 +472,9 @@ read CSV files (assuming they support complex numbers at all). .. method:: csvwriter.writerow(row) - Write the *row* parameter to the writer's file object, formatted according to - the current dialect. Return the return value of the call to the *write* method - of the underlying file object. + Write the *row* parameter to the writer's file object, formatted according + to the current :class:`Dialect`. Return the return value of the call to the + *write* method of the underlying file object. .. versionchanged:: 3.5 Added support of arbitrary iterables. diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index fd6422cc8c06c5..19ce19b1d70d1d 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -20,7 +20,7 @@ ctypes tutorial Note: The code samples in this tutorial use :mod:`doctest` to make sure that they actually work. Since some code samples behave differently under Linux, -Windows, or Mac OS X, they contain doctest directives in comments. +Windows, or macOS, they contain doctest directives in comments. Note: Some code samples reference the ctypes :class:`c_int` type. On platforms where ``sizeof(long) == sizeof(int)`` it is an alias to :class:`c_long`. @@ -80,7 +80,7 @@ the library by creating an instance of CDLL by calling the constructor:: >>> -.. XXX Add section for Mac OS X. +.. XXX Add section for macOS. .. _ctypes-accessing-functions-from-loaded-dlls: @@ -1288,7 +1288,7 @@ Here are some examples:: 'libbz2.so.1.0' >>> -On OS X, :func:`find_library` tries several predefined naming schemes and paths +On macOS, :func:`find_library` tries several predefined naming schemes and paths to locate the library, and returns a full pathname if successful:: >>> from ctypes.util import find_library @@ -1320,7 +1320,7 @@ There are several ways to load shared libraries into the Python process. One way is to instantiate one of the following classes: -.. class:: CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=0) +.. class:: CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None) Instances of this class represent loaded shared libraries. Functions in these libraries use the standard C calling convention, and are assumed to return @@ -1342,7 +1342,7 @@ way is to instantiate one of the following classes: -- A tool to find DLL dependents. -.. class:: OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=0) +.. class:: OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None) Windows only: Instances of this class represent loaded shared libraries, functions in these libraries use the ``stdcall`` calling convention, and are @@ -1355,16 +1355,12 @@ way is to instantiate one of the following classes: :exc:`WindowsError` used to be raised. -.. class:: WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=0) +.. class:: WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None) Windows only: Instances of this class represent loaded shared libraries, functions in these libraries use the ``stdcall`` calling convention, and are assumed to return :c:type:`int` by default. - On Windows CE only the standard calling convention is used, for convenience the - :class:`WinDLL` and :class:`OleDLL` use the standard calling convention on this - platform. - The Python :term:`global interpreter lock` is released before calling any function exported by these libraries, and reacquired afterwards. @@ -1665,8 +1661,7 @@ See :ref:`ctypes-callback-functions` for examples. .. function:: WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False) Windows only: The returned function prototype creates functions that use the - ``stdcall`` calling convention, except on Windows CE where - :func:`WINFUNCTYPE` is the same as :func:`CFUNCTYPE`. The function will + ``stdcall`` calling convention. The function will release the GIL during the call. *use_errno* and *use_last_error* have the same meaning as above. @@ -2513,7 +2508,7 @@ Arrays and pointers Abstract base class for arrays. The recommended way to create concrete array types is by multiplying any - :mod:`ctypes` data type with a positive integer. Alternatively, you can subclass + :mod:`ctypes` data type with a non-negative integer. Alternatively, you can subclass this type and define :attr:`_length_` and :attr:`_type_` class variables. Array elements can be read and written using standard subscript and slice accesses; for slice reads, the resulting object is diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index 64540b3e963b52..1fe5bda3b9415d 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -17,7 +17,7 @@ adding generated :term:`special method`\s such as :meth:`__init__` and in :pep:`557`. The member variables to use in these generated methods are defined -using :pep:`526` type annotations. For example this code:: +using :pep:`526` type annotations. For example, this code:: from dataclasses import dataclass @@ -31,7 +31,7 @@ using :pep:`526` type annotations. For example this code:: def total_cost(self) -> float: return self.unit_price * self.quantity_on_hand -Will add, among other things, a :meth:`__init__` that looks like:: +will add, among other things, a :meth:`__init__` that looks like:: def __init__(self, name: str, unit_price: float, quantity_on_hand: int = 0): self.name = name @@ -52,7 +52,7 @@ Module contents :term:`special method`\s to classes, as described below. The :func:`dataclass` decorator examines the class to find - ``field``\s. A ``field`` is defined as class variable that has a + ``field``\s. A ``field`` is defined as a class variable that has a :term:`type annotation `. With two exceptions described below, nothing in :func:`dataclass` examines the type specified in the variable annotation. @@ -62,8 +62,8 @@ Module contents The :func:`dataclass` decorator will add various "dunder" methods to the class, described below. If any of the added methods already - exist on the class, the behavior depends on the parameter, as documented - below. The decorator returns the same class that is called on; no new + exist in the class, the behavior depends on the parameter, as documented + below. The decorator returns the same class that it is called on; no new class is created. If :func:`dataclass` is used just as a simple decorator with no parameters, @@ -202,10 +202,10 @@ Module contents def __init__(self, a: int, b: int = 0): :exc:`TypeError` will be raised if a field without a default value - follows a field with a default value. This is true either when this + follows a field with a default value. This is true whether this occurs in a single class, or as a result of class inheritance. -.. function:: field(*, default=MISSING, default_factory=MISSING, init=True, repr=True, hash=None, compare=True, metadata=None, kw_only=MISSING): +.. function:: field(*, default=MISSING, default_factory=MISSING, init=True, repr=True, hash=None, compare=True, metadata=None, kw_only=MISSING) For common and simple use cases, no other functionality is required. There are, however, some dataclass features that @@ -221,10 +221,9 @@ Module contents c.mylist += [1, 2, 3] As shown above, the :const:`MISSING` value is a sentinel object used to - detect if the ``default`` and ``default_factory`` parameters are - provided. This sentinel is used because ``None`` is a valid value - for ``default``. No code should directly use the :const:`MISSING` - value. + detect if some parameters are provided by the user. This sentinel is + used because ``None`` is a valid value for some parameters with + a distinct meaning. No code should directly use the :const:`MISSING` value. The parameters to :func:`field` are: @@ -308,7 +307,7 @@ Module contents - ``default``, ``default_factory``, ``init``, ``repr``, ``hash``, ``compare``, ``metadata``, and ``kw_only`` have the identical - meaning and values as they do in the :func:`field` declaration. + meaning and values as they do in the :func:`field` function. Other attributes may exist, but they are private and must not be inspected or relied on. @@ -320,12 +319,15 @@ Module contents Raises :exc:`TypeError` if not passed a dataclass or instance of one. Does not return pseudo-fields which are ``ClassVar`` or ``InitVar``. -.. function:: asdict(instance, *, dict_factory=dict) +.. function:: asdict(obj, *, dict_factory=dict) - Converts the dataclass ``instance`` to a dict (by using the + Converts the dataclass ``obj`` to a dict (by using the factory function ``dict_factory``). Each dataclass is converted to a dict of its fields, as ``name: value`` pairs. dataclasses, dicts, - lists, and tuples are recursed into. For example:: + lists, and tuples are recursed into. Other objects are copied with + :func:`copy.deepcopy`. + + Example of using :func:`asdict` on nested dataclasses:: @dataclass class Point: @@ -342,21 +344,32 @@ Module contents c = C([Point(0, 0), Point(10, 4)]) assert asdict(c) == {'mylist': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]} - Raises :exc:`TypeError` if ``instance`` is not a dataclass instance. + To create a shallow copy, the following workaround may be used:: + + dict((field.name, getattr(obj, field.name)) for field in fields(obj)) -.. function:: astuple(instance, *, tuple_factory=tuple) + :func:`asdict` raises :exc:`TypeError` if ``obj`` is not a dataclass + instance. - Converts the dataclass ``instance`` to a tuple (by using the +.. function:: astuple(obj, *, tuple_factory=tuple) + + Converts the dataclass ``obj`` to a tuple (by using the factory function ``tuple_factory``). Each dataclass is converted to a tuple of its field values. dataclasses, dicts, lists, and - tuples are recursed into. + tuples are recursed into. Other objects are copied with + :func:`copy.deepcopy`. Continuing from the previous example:: assert astuple(p) == (10, 20) assert astuple(c) == ([(0, 0), (10, 4)],) - Raises :exc:`TypeError` if ``instance`` is not a dataclass instance. + To create a shallow copy, the following workaround may be used:: + + tuple(getattr(obj, field.name) for field in dataclasses.fields(obj)) + + :func:`astuple` raises :exc:`TypeError` if ``obj`` is not a dataclass + instance. .. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False) @@ -393,10 +406,10 @@ Module contents def add_one(self): return self.x + 1 -.. function:: replace(instance, /, **changes) +.. function:: replace(obj, /, **changes) - Creates a new object of the same type of ``instance``, replacing - fields with values from ``changes``. If ``instance`` is not a Data + Creates a new object of the same type as ``obj``, replacing + fields with values from ``changes``. If ``obj`` is not a Data Class, raises :exc:`TypeError`. If values in ``changes`` do not specify fields, raises :exc:`TypeError`. @@ -421,7 +434,7 @@ Module contents ``replace()`` (or similarly named) method which handles instance copying. -.. function:: is_dataclass(class_or_instance) +.. function:: is_dataclass(obj) Return ``True`` if its parameter is a dataclass or an instance of one, otherwise return ``False``. @@ -462,6 +475,8 @@ Module contents In a single dataclass, it is an error to specify more than one field whose type is :const:`KW_ONLY`. + .. versionadded:: 3.10 + .. exception:: FrozenInstanceError Raised when an implicitly defined :meth:`__setattr__` or @@ -491,6 +506,27 @@ depend on one or more other fields. For example:: def __post_init__(self): self.c = self.a + self.b +The :meth:`__init__` method generated by :func:`dataclass` does not call base +class :meth:`__init__` methods. If the base class has an :meth:`__init__` method +that has to be called, it is common to call this method in a +:meth:`__post_init__` method:: + + @dataclass + class Rectangle: + height: float + width: float + + @dataclass + class Square(Rectangle): + side: float + + def __post_init__(self): + super().__init__(self.side, self.side) + +Note, however, that in general the dataclass-generated :meth:`__init__` methods +don't need to be called, since the derived dataclass will take care of +initializing all fields of any base class that is a dataclass itself. + See the section below on init-only variables for ways to pass parameters to :meth:`__post_init__`. Also see the warning about how :func:`replace` handles ``init=False`` fields. diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index dae0dd7aa55898..f447b7bc9491e4 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -27,6 +27,9 @@ on efficient attribute extraction for output formatting and manipulation. Module :mod:`time` Time access and conversions. + Module :mod:`zoneinfo` + Concrete time zones representing the IANA time zone database. + Package `dateutil `_ Third-party library with expanded time zone and parsing support. @@ -2174,14 +2177,13 @@ only EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)). .. seealso:: - `dateutil.tz `_ + :mod:`zoneinfo` The :mod:`datetime` module has a basic :class:`timezone` class (for handling arbitrary fixed offsets from UTC) and its :attr:`timezone.utc` attribute (a UTC timezone instance). - *dateutil.tz* library brings the *IANA timezone database* - (also known as the Olson database) to Python, and its usage is - recommended. + ``zoneinfo`` brings the *IANA timezone database* (also known as the Olson + database) to Python, and its usage is recommended. `IANA timezone database `_ The Time Zone Database (often called tz, tzdata or zoneinfo) contains code @@ -2357,8 +2359,8 @@ requires, and these work on all platforms with a standard C implementation. | | decimal number. | | \(9) | +-----------+--------------------------------+------------------------+-------+ | ``%f`` | Microsecond as a decimal | 000000, 000001, ..., | \(5) | -| | number, zero-padded on the | 999999 | | -| | left. | | | +| | number, zero-padded to 6 | 999999 | | +| | digits. | | | +-----------+--------------------------------+------------------------+-------+ | ``%z`` | UTC offset in the form | (empty), +0000, | \(6) | | | ``±HHMM[SS[.ffffff]]`` (empty | -0400, +1030, | | @@ -2373,7 +2375,7 @@ requires, and these work on all platforms with a standard C implementation. +-----------+--------------------------------+------------------------+-------+ | ``%U`` | Week number of the year | 00, 01, ..., 53 | \(7), | | | (Sunday as the first day of | | \(9) | -| | the week) as a zero padded | | | +| | the week) as a zero-padded | | | | | decimal number. All days in a | | | | | new year preceding the first | | | | | Sunday are considered to be in | | | @@ -2381,10 +2383,10 @@ requires, and these work on all platforms with a standard C implementation. +-----------+--------------------------------+------------------------+-------+ | ``%W`` | Week number of the year | 00, 01, ..., 53 | \(7), | | | (Monday as the first day of | | \(9) | -| | the week) as a decimal number. | | | -| | All days in a new year | | | -| | preceding the first Monday | | | -| | are considered to be in | | | +| | the week) as a zero-padded | | | +| | decimal number. All days in a | | | +| | new year preceding the first | | | +| | Monday are considered to be in | | | | | week 0. | | | +-----------+--------------------------------+------------------------+-------+ | ``%c`` | Locale's appropriate date and || Tue Aug 16 21:30:00 | \(1) | @@ -2431,7 +2433,8 @@ incomplete or ambiguous ISO 8601 directives will raise a :exc:`ValueError`. The full set of format codes supported varies across platforms, because Python calls the platform C library's :func:`strftime` function, and platform variations are common. To see the full set of format codes supported on your -platform, consult the :manpage:`strftime(3)` documentation. +platform, consult the :manpage:`strftime(3)` documentation. There are also +differences between platforms in handling of unsupported format specifiers. .. versionadded:: 3.6 ``%G``, ``%u`` and ``%V`` were added. diff --git a/Doc/library/dbm.rst b/Doc/library/dbm.rst index 57ae547b833cc0..ff01ae90f64257 100644 --- a/Doc/library/dbm.rst +++ b/Doc/library/dbm.rst @@ -216,7 +216,7 @@ supported. contains them all:: k = db.firstkey() - while k != None: + while k is not None: print(k) k = db.nextkey(k) diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst index e759c5cf23b9e7..47627c02561b94 100644 --- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -571,9 +571,10 @@ Decimal objects >>> Decimal(321).exp() Decimal('2.561702493119680037517373933E+139') - .. method:: from_float(f) + .. classmethod:: from_float(f) - Classmethod that converts a float to a decimal number, exactly. + Alternative constructor that only accepts instances of :class:`float` or + :class:`int`. Note `Decimal.from_float(0.1)` is not the same as `Decimal('0.1')`. Since 0.1 is not exactly representable in binary floating point, the diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index 0491a9d4a072bf..74820f9bdb5783 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -770,17 +770,20 @@ iterations of the loop. .. opcode:: MATCH_MAPPING - If TOS is an instance of :class:`collections.abc.Mapping`, push ``True`` onto - the stack. Otherwise, push ``False``. + If TOS is an instance of :class:`collections.abc.Mapping` (or, more technically: if + it has the :const:`Py_TPFLAGS_MAPPING` flag set in its + :c:member:`~PyTypeObject.tp_flags`), push ``True`` onto the stack. Otherwise, push + ``False``. .. versionadded:: 3.10 .. opcode:: MATCH_SEQUENCE - If TOS is an instance of :class:`collections.abc.Sequence` and is *not* an - instance of :class:`str`/:class:`bytes`/:class:`bytearray`, push ``True`` - onto the stack. Otherwise, push ``False``. + If TOS is an instance of :class:`collections.abc.Sequence` and is *not* an instance + of :class:`str`/:class:`bytes`/:class:`bytearray` (or, more technically: if it has + the :const:`Py_TPFLAGS_SEQUENCE` flag set in its :c:member:`~PyTypeObject.tp_flags`), + push ``True`` onto the stack. Otherwise, push ``False``. .. versionadded:: 3.10 @@ -1249,9 +1252,8 @@ All of the following opcodes use their arguments. .. opcode:: GEN_START (kind) - Pops TOS. If TOS was not ``None``, raises an exception. The ``kind`` - operand corresponds to the type of generator or coroutine and determines - the error message. The legal kinds are 0 for generator, 1 for coroutine, + Pops TOS. The ``kind`` operand corresponds to the type of generator or + coroutine. The legal kinds are 0 for generator, 1 for coroutine, and 2 for async generator. .. versionadded:: 3.10 diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst index a77322f83acbde..dbb54c99133f39 100644 --- a/Doc/library/doctest.rst +++ b/Doc/library/doctest.rst @@ -288,10 +288,6 @@ strings are treated as if they were docstrings. In output, a key ``K`` in Any classes found are recursively searched similarly, to test docstrings in their contained methods and nested classes. -.. impl-detail:: - Prior to version 3.4, extension modules written in C were not fully - searched by doctest. - .. _doctest-finding-examples: @@ -568,41 +564,35 @@ doctest decides whether actual output matches an example's expected output: .. data:: IGNORE_EXCEPTION_DETAIL - When specified, an example that expects an exception passes if an exception of - the expected type is raised, even if the exception detail does not match. For - example, an example expecting ``ValueError: 42`` will pass if the actual - exception raised is ``ValueError: 3*14``, but will fail, e.g., if - :exc:`TypeError` is raised. + When specified, doctests expecting exceptions pass so long as an exception + of the expected type is raised, even if the details + (message and fully-qualified exception name) don't match. + + For example, an example expecting ``ValueError: 42`` will pass if the actual + exception raised is ``ValueError: 3*14``, but will fail if, say, a + :exc:`TypeError` is raised instead. + It will also ignore any fully-qualified name included before the + exception class, which can vary between implementations and versions + of Python and the code/libraries in use. + Hence, all three of these variations will work with the flag specified: - It will also ignore the module name used in Python 3 doctest reports. Hence - both of these variations will work with the flag specified, regardless of - whether the test is run under Python 2.7 or Python 3.2 (or later versions):: + .. code-block:: pycon - >>> raise CustomError('message') + >>> raise Exception('message') Traceback (most recent call last): - CustomError: message + Exception: message - >>> raise CustomError('message') + >>> raise Exception('message') Traceback (most recent call last): - my_module.CustomError: message + builtins.Exception: message - Note that :const:`ELLIPSIS` can also be used to ignore the - details of the exception message, but such a test may still fail based - on whether or not the module details are printed as part of the - exception name. Using :const:`IGNORE_EXCEPTION_DETAIL` and the details - from Python 2.3 is also the only clear way to write a doctest that doesn't - care about the exception detail yet continues to pass under Python 2.3 or - earlier (those releases do not support :ref:`doctest directives - ` and ignore them as irrelevant comments). For example:: - - >>> (1, 2)[3] = 'moo' + >>> raise Exception('message') Traceback (most recent call last): - File "", line 1, in - TypeError: object doesn't support item assignment + __main__.Exception: message - passes under Python 2.3 and later Python versions with the flag specified, - even though the detail - changed in Python 2.4 to say "does not" instead of "doesn't". + Note that :const:`ELLIPSIS` can also be used to ignore the + details of the exception message, but such a test may still fail based + on whether the module name is present or matches exactly. .. versionchanged:: 3.2 :const:`IGNORE_EXCEPTION_DETAIL` now also ignores any information relating @@ -719,36 +709,51 @@ above. An example's doctest directives modify doctest's behavior for that single example. Use ``+`` to enable the named behavior, or ``-`` to disable it. -For example, this test passes:: +For example, this test passes: - >>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE +.. doctest:: + :no-trim-doctest-flags: + + >>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] Without the directive it would fail, both because the actual output doesn't have two blanks before the single-digit list elements, and because the actual output is on a single line. This test also passes, and also requires a directive to do -so:: +so: + +.. doctest:: + :no-trim-doctest-flags: - >>> print(list(range(20))) # doctest: +ELLIPSIS + >>> print(list(range(20))) # doctest: +ELLIPSIS [0, 1, ..., 18, 19] Multiple directives can be used on a single physical line, separated by -commas:: +commas: - >>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE +.. doctest:: + :no-trim-doctest-flags: + + >>> print(list(range(20))) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE [0, 1, ..., 18, 19] If multiple directive comments are used for a single example, then they are -combined:: +combined: + +.. doctest:: + :no-trim-doctest-flags: - >>> print(list(range(20))) # doctest: +ELLIPSIS - ... # doctest: +NORMALIZE_WHITESPACE + >>> print(list(range(20))) # doctest: +ELLIPSIS + ... # doctest: +NORMALIZE_WHITESPACE [0, 1, ..., 18, 19] As the previous example shows, you can add ``...`` lines to your example containing only directives. This can be useful when an example is too long for -a directive to comfortably fit on the same line:: +a directive to comfortably fit on the same line: + +.. doctest:: + :no-trim-doctest-flags: >>> print(list(range(5)) + list(range(10, 20)) + list(range(30, 40))) ... # doctest: +ELLIPSIS @@ -786,25 +791,25 @@ instead. Another is to do :: >>> d ['Harry', 'Hermione'] -.. note:: - - Before Python 3.6, when printing a dict, Python did not guarantee that - the key-value pairs was printed in any particular order. - There are others, but you get the idea. -Another bad idea is to print things that embed an object address, like :: +Another bad idea is to print things that embed an object address, like + +.. doctest:: - >>> id(1.0) # certain to fail some of the time + >>> id(1.0) # certain to fail some of the time # doctest: +SKIP 7948648 >>> class C: pass - >>> C() # the default repr() for instances embeds an address - <__main__.C instance at 0x00AC18F0> + >>> C() # the default repr() for instances embeds an address # doctest: +SKIP + + +The :const:`ELLIPSIS` directive gives a nice approach for the last example: -The :const:`ELLIPSIS` directive gives a nice approach for the last example:: +.. doctest:: + :no-trim-doctest-flags: - >>> C() #doctest: +ELLIPSIS - <__main__.C instance at 0x...> + >>> C() # doctest: +ELLIPSIS + Floating-point numbers are also subject to small output variations across platforms, because Python defers to the platform C library for float formatting, diff --git a/Doc/library/email.charset.rst b/Doc/library/email.charset.rst index 38fda23bd8237f..adbe6c1c7d29b8 100644 --- a/Doc/library/email.charset.rst +++ b/Doc/library/email.charset.rst @@ -58,9 +58,9 @@ Import this class from the :mod:`email.charset` module. .. attribute:: header_encoding If the character set must be encoded before it can be used in an email - header, this attribute will be set to ``Charset.QP`` (for - quoted-printable), ``Charset.BASE64`` (for base64 encoding), or - ``Charset.SHORTEST`` for the shortest of QP or BASE64 encoding. Otherwise, + header, this attribute will be set to ``charset.QP`` (for + quoted-printable), ``charset.BASE64`` (for base64 encoding), or + ``charset.SHORTEST`` for the shortest of QP or BASE64 encoding. Otherwise, it will be ``None``. @@ -68,7 +68,7 @@ Import this class from the :mod:`email.charset` module. Same as *header_encoding*, but describes the encoding for the mail message's body, which indeed may be different than the header encoding. - ``Charset.SHORTEST`` is not allowed for *body_encoding*. + ``charset.SHORTEST`` is not allowed for *body_encoding*. .. attribute:: output_charset @@ -175,9 +175,9 @@ new entries to the global character set, alias, and codec registries: *charset* is the input character set, and must be the canonical name of a character set. - Optional *header_enc* and *body_enc* is either ``Charset.QP`` for - quoted-printable, ``Charset.BASE64`` for base64 encoding, - ``Charset.SHORTEST`` for the shortest of quoted-printable or base64 encoding, + Optional *header_enc* and *body_enc* is either ``charset.QP`` for + quoted-printable, ``charset.BASE64`` for base64 encoding, + ``charset.SHORTEST`` for the shortest of quoted-printable or base64 encoding, or ``None`` for no encoding. ``SHORTEST`` is only valid for *header_enc*. The default is ``None`` for no encoding. diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index b5f9c2f08b1873..7d166bfb1fcc61 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -13,617 +13,1200 @@ **Source code:** :source:`Lib/enum.py` -.. sidebar:: Important +---------------- - This page contains the API reference information. For tutorial - information and discussion of more advanced topics, see +An enumeration is a set of symbolic names (members) bound to unique, +constant values. Within an enumeration, the members can be compared +by identity, and the enumeration itself can be iterated over. - * :ref:`Basic Tutorial ` - * :ref:`Advanced Tutorial ` - * :ref:`Enum Cookbook ` +.. note:: Case of Enum Members ----------------- + Because Enums are used to represent constants we recommend using + UPPER_CASE names for enum members, and will be using that style + in our examples. -An enumeration: -* is a set of symbolic names (members) bound to unique values -* can be iterated over to return its members in definition order -* uses :meth:`call` syntax to return members by value -* uses :meth:`index` syntax to return members by name +Module Contents +--------------- -Enumerations are created either by using the :keyword:`class` syntax, or by -using function-call syntax:: +This module defines four enumeration classes that can be used to define unique +sets of names and values: :class:`Enum`, :class:`IntEnum`, :class:`Flag`, and +:class:`IntFlag`. It also defines one decorator, :func:`unique`, and one +helper, :class:`auto`. - >>> from enum import Enum +.. class:: Enum - >>> # class syntax - >>> class Color(Enum): - ... RED = 1 - ... GREEN = 2 - ... BLUE = 3 + Base class for creating enumerated constants. See section + `Functional API`_ for an alternate construction syntax. - >>> # functional syntax - >>> Color = Enum('Color', ['RED', 'GREEN', 'BLUE']) +.. class:: IntEnum -Even though we can use the :keyword:`class` syntax to create Enums, Enums -are not normal Python classes. See -:ref:`How are Enums different? ` for more details. + Base class for creating enumerated constants that are also + subclasses of :class:`int`. -.. note:: Nomenclature +.. class:: IntFlag - - The class :class:`Color` is an *enumeration* (or *enum*) - - The attributes :attr:`Color.RED`, :attr:`Color.GREEN`, etc., are - *enumeration members* (or *enum members*) and are functionally constants. - - The enum members have *names* and *values* (the name of - :attr:`Color.RED` is ``RED``, the value of :attr:`Color.BLUE` is - ``3``, etc.) + Base class for creating enumerated constants that can be combined using + the bitwise operators without losing their :class:`IntFlag` membership. + :class:`IntFlag` members are also subclasses of :class:`int`. +.. class:: Flag -Module Contents ---------------- + Base class for creating enumerated constants that can be combined using + the bitwise operations without losing their :class:`Flag` membership. - :class:`EnumType` +.. function:: unique + :noindex: - The ``type`` for Enum and its subclasses. + Enum class decorator that ensures only one name is bound to any one value. - :class:`Enum` +.. class:: auto - Base class for creating enumerated constants. + Instances are replaced with an appropriate value for Enum members. By default, the initial value starts at 1. - :class:`IntEnum` +.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto`` - Base class for creating enumerated constants that are also - subclasses of :class:`int`. - :class:`StrEnum` +Creating an Enum +---------------- - Base class for creating enumerated constants that are also - subclasses of :class:`str`. +Enumerations are created using the :keyword:`class` syntax, which makes them +easy to read and write. An alternative creation method is described in +`Functional API`_. To define an enumeration, subclass :class:`Enum` as +follows:: - :class:`Flag` + >>> from enum import Enum + >>> class Color(Enum): + ... RED = 1 + ... GREEN = 2 + ... BLUE = 3 + ... - Base class for creating enumerated constants that can be combined using - the bitwise operations without losing their :class:`Flag` membership. +.. note:: Enum member values - :class:`IntFlag` + Member values can be anything: :class:`int`, :class:`str`, etc.. If + the exact value is unimportant you may use :class:`auto` instances and an + appropriate value will be chosen for you. Care must be taken if you mix + :class:`auto` with other values. - Base class for creating enumerated constants that can be combined using - the bitwise operators without losing their :class:`IntFlag` membership. - :class:`IntFlag` members are also subclasses of :class:`int`. +.. note:: Nomenclature - :class:`FlagBoundary` + - The class :class:`Color` is an *enumeration* (or *enum*) + - The attributes :attr:`Color.RED`, :attr:`Color.GREEN`, etc., are + *enumeration members* (or *enum members*) and are functionally constants. + - The enum members have *names* and *values* (the name of + :attr:`Color.RED` is ``RED``, the value of :attr:`Color.BLUE` is + ``3``, etc.) - An enumeration with the values ``STRICT``, ``CONFORM``, ``EJECT``, and - ``KEEP`` which allows for more fine-grained control over how invalid values - are dealt with in an enumeration. +.. note:: - :class:`auto` + Even though we use the :keyword:`class` syntax to create Enums, Enums + are not normal Python classes. See `How are Enums different?`_ for + more details. - Instances are replaced with an appropriate value for Enum members. - :class:`StrEnum` defaults to the lower-cased version of the member name, - while other Enums default to 1 and increase from there. +Enumeration members have human readable string representations:: - :func:`global_enum` + >>> print(Color.RED) + Color.RED - :class:`Enum` class decorator to apply the appropriate global `__repr__`, - and export its members into the global name space. +...while their ``repr`` has more information:: - :func:`property` + >>> print(repr(Color.RED)) + - Allows :class:`Enum` members to have attributes without conflicting with - other members' names. +The *type* of an enumeration member is the enumeration it belongs to:: - :func:`unique` + >>> type(Color.RED) + + >>> isinstance(Color.GREEN, Color) + True + >>> - Enum class decorator that ensures only one name is bound to any one value. +Enum members also have a property that contains just their item name:: + >>> print(Color.RED.name) + RED -.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto`` -.. versionadded:: 3.10 ``StrEnum`` +Enumerations support iteration, in definition order:: + >>> class Shake(Enum): + ... VANILLA = 7 + ... CHOCOLATE = 4 + ... COOKIES = 9 + ... MINT = 3 + ... + >>> for shake in Shake: + ... print(shake) + ... + Shake.VANILLA + Shake.CHOCOLATE + Shake.COOKIES + Shake.MINT -Data Types ----------- +Enumeration members are hashable, so they can be used in dictionaries and sets:: + >>> apples = {} + >>> apples[Color.RED] = 'red delicious' + >>> apples[Color.GREEN] = 'granny smith' + >>> apples == {Color.RED: 'red delicious', Color.GREEN: 'granny smith'} + True -.. class:: EnumType - *EnumType* is the :term:`metaclass` for *enum* enumerations. It is possible - to subclass *EnumType* -- see :ref:`Subclassing EnumType ` - for details. +Programmatic access to enumeration members and their attributes +--------------------------------------------------------------- - .. method:: EnumType.__contains__(cls, member) +Sometimes it's useful to access members in enumerations programmatically (i.e. +situations where ``Color.RED`` won't do because the exact color is not known +at program-writing time). ``Enum`` allows such access:: - Returns ``True`` if member belongs to the ``cls``:: + >>> Color(1) + + >>> Color(3) + - >>> some_var = Color.RED - >>> some_var in Color - True +If you want to access enum members by *name*, use item access:: - .. note:: + >>> Color['RED'] + + >>> Color['GREEN'] + - In Python 3.12 it will be possible to check for member values and not - just members; until then, a ``TypeError`` will be raised if a - non-Enum-member is used in a containment check. +If you have an enum member and need its :attr:`name` or :attr:`value`:: - .. method:: EnumType.__dir__(cls) + >>> member = Color.RED + >>> member.name + 'RED' + >>> member.value + 1 - Returns ``['__class__', '__doc__', '__members__', '__module__']`` and the - names of the members in *cls*:: - >>> dir(Color) - ['BLUE', 'GREEN', 'RED', '__class__', '__doc__', '__members__', '__module__'] +Duplicating enum members and values +----------------------------------- - .. method:: EnumType.__getattr__(cls, name) +Having two enum members with the same name is invalid:: - Returns the Enum member in *cls* matching *name*, or raises an :exc:`AttributeError`:: + >>> class Shape(Enum): + ... SQUARE = 2 + ... SQUARE = 3 + ... + Traceback (most recent call last): + ... + TypeError: Attempted to reuse key: 'SQUARE' - >>> Color.GREEN - Color.GREEN +However, two enum members are allowed to have the same value. Given two members +A and B with the same value (and A defined first), B is an alias to A. By-value +lookup of the value of A and B will return A. By-name lookup of B will also +return A:: - .. method:: EnumType.__getitem__(cls, name) + >>> class Shape(Enum): + ... SQUARE = 2 + ... DIAMOND = 1 + ... CIRCLE = 3 + ... ALIAS_FOR_SQUARE = 2 + ... + >>> Shape.SQUARE + + >>> Shape.ALIAS_FOR_SQUARE + + >>> Shape(2) + - Returns the Enum member in *cls* matching *name*, or raises an :exc:`KeyError`:: +.. note:: - >>> Color['BLUE'] - Color.BLUE + Attempting to create a member with the same name as an already + defined attribute (another member, a method, etc.) or attempting to create + an attribute with the same name as a member is not allowed. - .. method:: EnumType.__iter__(cls) - Returns each member in *cls* in definition order:: +Ensuring unique enumeration values +---------------------------------- - >>> list(Color) - [Color.RED, Color.GREEN, Color.BLUE] +By default, enumerations allow multiple names as aliases for the same value. +When this behavior isn't desired, the following decorator can be used to +ensure each value is used only once in the enumeration: - .. method:: EnumType.__len__(cls) +.. decorator:: unique - Returns the number of member in *cls*:: +A :keyword:`class` decorator specifically for enumerations. It searches an +enumeration's :attr:`__members__` gathering any aliases it finds; if any are +found :exc:`ValueError` is raised with the details:: + + >>> from enum import Enum, unique + >>> @unique + ... class Mistake(Enum): + ... ONE = 1 + ... TWO = 2 + ... THREE = 3 + ... FOUR = 3 + ... + Traceback (most recent call last): + ... + ValueError: duplicate values found in : FOUR -> THREE + + +Using automatic values +---------------------- + +If the exact value is unimportant you can use :class:`auto`:: + + >>> from enum import Enum, auto + >>> class Color(Enum): + ... RED = auto() + ... BLUE = auto() + ... GREEN = auto() + ... + >>> list(Color) + [, , ] + +The values are chosen by :func:`_generate_next_value_`, which can be +overridden:: + + >>> class AutoName(Enum): + ... def _generate_next_value_(name, start, count, last_values): + ... return name + ... + >>> class Ordinal(AutoName): + ... NORTH = auto() + ... SOUTH = auto() + ... EAST = auto() + ... WEST = auto() + ... + >>> list(Ordinal) + [, , , ] - >>> len(Color) - 3 +.. note:: - .. method:: EnumType.__reversed__(cls) + The goal of the default :meth:`_generate_next_value_` method is to provide + the next :class:`int` in sequence with the last :class:`int` provided, but + the way it does this is an implementation detail and may change. - Returns each member in *cls* in reverse definition order:: +.. note:: - >>> list(reversed(Color)) - [Color.BLUE, Color.GREEN, Color.RED] + The :meth:`_generate_next_value_` method must be defined before any members. +Iteration +--------- -.. class:: Enum +Iterating over the members of an enum does not provide the aliases:: + + >>> list(Shape) + [, , ] + +The special attribute ``__members__`` is a read-only ordered mapping of names +to members. It includes all names defined in the enumeration, including the +aliases:: + + >>> for name, member in Shape.__members__.items(): + ... name, member + ... + ('SQUARE', ) + ('DIAMOND', ) + ('CIRCLE', ) + ('ALIAS_FOR_SQUARE', ) - *Enum* is the base class for all *enum* enumerations. +The ``__members__`` attribute can be used for detailed programmatic access to +the enumeration members. For example, finding all the aliases:: - .. attribute:: Enum.name + >>> [name for name, member in Shape.__members__.items() if member.name != name] + ['ALIAS_FOR_SQUARE'] - The name used to define the ``Enum`` member:: - >>> Color.BLUE.name - 'BLUE' +Comparisons +----------- - .. attribute:: Enum.value +Enumeration members are compared by identity:: - The value given to the ``Enum`` member:: + >>> Color.RED is Color.RED + True + >>> Color.RED is Color.BLUE + False + >>> Color.RED is not Color.BLUE + True - >>> Color.RED.value - 1 +Ordered comparisons between enumeration values are *not* supported. Enum +members are not integers (but see `IntEnum`_ below):: + + >>> Color.RED < Color.BLUE + Traceback (most recent call last): + File "", line 1, in + TypeError: '<' not supported between instances of 'Color' and 'Color' + +Equality comparisons are defined though:: + + >>> Color.BLUE == Color.RED + False + >>> Color.BLUE != Color.RED + True + >>> Color.BLUE == Color.BLUE + True - .. note:: Enum member values +Comparisons against non-enumeration values will always compare not equal +(again, :class:`IntEnum` was explicitly designed to behave differently, see +below):: - Member values can be anything: :class:`int`, :class:`str`, etc.. If - the exact value is unimportant you may use :class:`auto` instances and an - appropriate value will be chosen for you. Care must be taken if you mix - :class:`auto` with other values. + >>> Color.BLUE == 2 + False - .. attribute:: Enum._ignore_ - ``_ignore_`` is only used during creation and is removed from the - enumeration once that is complete. +Allowed members and attributes of enumerations +---------------------------------------------- - ``_ignore_`` is a list of names that will not become members, and whose - names will also be removed from the completed enumeration. See - :ref:`TimePeriod ` for an example. +The examples above use integers for enumeration values. Using integers is +short and handy (and provided by default by the `Functional API`_), but not +strictly enforced. In the vast majority of use-cases, one doesn't care what +the actual value of an enumeration is. But if the value *is* important, +enumerations can have arbitrary values. - .. method:: Enum.__call__(cls, value, names=None, \*, module=None, qualname=None, type=None, start=1, boundary=None) +Enumerations are Python classes, and can have methods and special methods as +usual. If we have this enumeration:: - This method is called in two different ways: + >>> class Mood(Enum): + ... FUNKY = 1 + ... HAPPY = 3 + ... + ... def describe(self): + ... # self is the member here + ... return self.name, self.value + ... + ... def __str__(self): + ... return 'my custom str! {0}'.format(self.value) + ... + ... @classmethod + ... def favorite_mood(cls): + ... # cls here is the enumeration + ... return cls.HAPPY + ... - * to look up an existing member: +Then:: - :cls: The enum class being called. - :value: The value to lookup. - - * to use the ``cls`` enum to create a new enum: - - :cls: The enum class being called. - :value: The name of the new Enum to create. - :names: The names/values of the members for the new Enum. - :module: The name of the module the new Enum is created in. - :qualname: The actual location in the module where this Enum can be found. - :type: A mix-in type for the new Enum. - :start: The first integer value for the Enum (used by :class:`auto`) - :boundary: How to handle out-of-range values from bit operations (:class:`Flag` only) - - .. method:: Enum.__dir__(self) - - Returns ``['__class__', '__doc__', '__module__', 'name', 'value']`` and - any public methods defined on *self.__class__*:: - - >>> from datetime import date - >>> class Weekday(Enum): - ... MONDAY = 1 - ... TUESDAY = 2 - ... WEDNESDAY = 3 - ... THURSDAY = 4 - ... FRIDAY = 5 - ... SATURDAY = 6 - ... SUNDAY = 7 - ... @classmethod - ... def today(cls): - ... print('today is %s' % cls(date.today.isoweekday).naem) - >>> dir(Weekday.SATURDAY) - ['__class__', '__doc__', '__module__', 'name', 'today', 'value'] - - .. method:: Enum._generate_next_value_(name, start, count, last_values) - - :name: The name of the member being defined (e.g. 'RED'). - :start: The start value for the Enum; the default is 1. - :count: The number of members currently defined, not including this one. - :last_values: A list of the previous values. - - A *staticmethod* that is used to determine the next value returned by - :class:`auto`:: - - >>> from enum import auto - >>> class PowersOfThree(Enum): - ... @staticmethod - ... def _generate_next_value_(name, start, count, last_values): - ... return (count + 1) * 3 - ... FIRST = auto() - ... SECOND = auto() - >>> PowersOfThree.SECOND.value - 6 - - .. method:: Enum._missing_(cls, value) - - A *classmethod* for looking up values not found in *cls*. By default it - does nothing, but can be overridden to implement custom search behavior:: - - >>> from enum import StrEnum - >>> class Build(StrEnum): - ... DEBUG = auto() - ... OPTIMIZED = auto() - ... @classmethod - ... def _missing_(cls, value): - ... value = value.lower() - ... for member in cls: - ... if member.value == value: - ... return member - ... return None - >>> Build.DEBUG.value - 'debug' - >>> Build('deBUG') - Build.DEBUG - - .. method:: Enum.__repr__(self) - - Returns the string used for *repr()* calls. By default, returns the - *Enum* name and the member name, but can be overridden:: - - >>> class OldStyle(Enum): - ... RETRO = auto() - ... OLD_SCHOOl = auto() - ... YESTERYEAR = auto() - ... def __repr__(self): - ... cls_name = self.__class__.__name__ - ... return f'<{cls_name}.{self.name}: {self.value}>' - >>> OldStyle.RETRO - - - .. method:: Enum.__str__(self) - - Returns the string used for *str()* calls. By default, returns the - member name, but can be overridden:: - - >>> class OldStyle(Enum): - ... RETRO = auto() - ... OLD_SCHOOl = auto() - ... YESTERYEAR = auto() - ... def __str__(self): - ... cls_name = self.__class__.__name__ - ... return f'{cls_name}.{self.name}' - >>> OldStyle.RETRO - OldStyle.RETRO + >>> Mood.favorite_mood() + + >>> Mood.HAPPY.describe() + ('HAPPY', 3) + >>> str(Mood.FUNKY) + 'my custom str! 1' + +The rules for what is allowed are as follows: names that start and end with +a single underscore are reserved by enum and cannot be used; all other +attributes defined within an enumeration will become members of this +enumeration, with the exception of special methods (:meth:`__str__`, +:meth:`__add__`, etc.), descriptors (methods are also descriptors), and +variable names listed in :attr:`_ignore_`. + +Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__` then +any value(s) given to the enum member will be passed into those methods. +See `Planet`_ for an example. + + +Restricted Enum subclassing +--------------------------- + +A new :class:`Enum` class must have one base Enum class, up to one concrete +data type, and as many :class:`object`-based mixin classes as needed. The +order of these base classes is:: + + class EnumName([mix-in, ...,] [data-type,] base-enum): + pass + +Also, subclassing an enumeration is allowed only if the enumeration does not define +any members. So this is forbidden:: + + >>> class MoreColor(Color): + ... PINK = 17 + ... + Traceback (most recent call last): + ... + TypeError: MoreColor: cannot extend enumeration 'Color' + +But this is allowed:: + + >>> class Foo(Enum): + ... def some_behavior(self): + ... pass + ... + >>> class Bar(Foo): + ... HAPPY = 1 + ... SAD = 2 + ... + +Allowing subclassing of enums that define members would lead to a violation of +some important invariants of types and instances. On the other hand, it makes +sense to allow sharing some common behavior between a group of enumerations. +(See `OrderedEnum`_ for an example.) + + +Pickling +-------- + +Enumerations can be pickled and unpickled:: + + >>> from test.test_enum import Fruit + >>> from pickle import dumps, loads + >>> Fruit.TOMATO is loads(dumps(Fruit.TOMATO)) + True + +The usual restrictions for pickling apply: picklable enums must be defined in +the top level of a module, since unpickling requires them to be importable +from that module. .. note:: - Using :class:`auto` with :class:`Enum` results in integers of increasing value, - starting with ``1``. + With pickle protocol version 4 it is possible to easily pickle enums + nested in other classes. +It is possible to modify how Enum members are pickled/unpickled by defining +:meth:`__reduce_ex__` in the enumeration class. -.. class:: IntEnum - *IntEnum* is the same as *Enum*, but its members are also integers and can be - used anywhere that an integer can be used. If any integer operation is performed - with an *IntEnum* member, the resulting value loses its enumeration status. - - >>> from enum import IntEnum - >>> class Numbers(IntEnum): - ... ONE = 1 - ... TWO = 2 - ... THREE = 3 - >>> Numbers.THREE - Numbers.THREE - >>> Numbers.ONE + Numbers.TWO - 3 - >>> Numbers.THREE + 5 - 8 - >>> Numbers.THREE == 3 - True +Functional API +-------------- -.. note:: +The :class:`Enum` class is callable, providing the following functional API:: - Using :class:`auto` with :class:`IntEnum` results in integers of increasing value, - starting with ``1``. + >>> Animal = Enum('Animal', 'ANT BEE CAT DOG') + >>> Animal + + >>> Animal.ANT + + >>> Animal.ANT.value + 1 + >>> list(Animal) + [, , , ] +The semantics of this API resemble :class:`~collections.namedtuple`. The first +argument of the call to :class:`Enum` is the name of the enumeration. -.. class:: StrEnum +The second argument is the *source* of enumeration member names. It can be a +whitespace-separated string of names, a sequence of names, a sequence of +2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to +values. The last two options enable assigning arbitrary values to +enumerations; the others auto-assign increasing integers starting with 1 (use +the ``start`` parameter to specify a different starting value). A +new class derived from :class:`Enum` is returned. In other words, the above +assignment to :class:`Animal` is equivalent to:: - *StrEnum* is the same as *Enum*, but its members are also strings and can be used - in most of the same places that a string can be used. The result of any string - operation performed on or with a *StrEnum* member is not part of the enumeration. + >>> class Animal(Enum): + ... ANT = 1 + ... BEE = 2 + ... CAT = 3 + ... DOG = 4 + ... - .. note:: There are places in the stdlib that check for an exact :class:`str` - instead of a :class:`str` subclass (i.e. ``type(unknown) == str`` - instead of ``isinstance(str, unknown)``), and in those locations you - will need to use ``str(StrEnum.member)``. +The reason for defaulting to ``1`` as the starting number and not ``0`` is +that ``0`` is ``False`` in a boolean sense, but enum members all evaluate +to ``True``. +Pickling enums created with the functional API can be tricky as frame stack +implementation details are used to try and figure out which module the +enumeration is being created in (e.g. it will fail if you use a utility +function in separate module, and also may not work on IronPython or Jython). +The solution is to specify the module name explicitly as follows:: -.. note:: + >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', module=__name__) - Using :class:`auto` with :class:`StrEnum` results in values of the member name, - lower-cased. +.. warning:: + If ``module`` is not supplied, and Enum cannot determine what it is, + the new Enum members will not be unpicklable; to keep errors closer to + the source, pickling will be disabled. -.. class:: Flag +The new pickle protocol 4 also, in some circumstances, relies on +:attr:`~definition.__qualname__` being set to the location where pickle will be able +to find the class. For example, if the class was made available in class +SomeData in the global scope:: - *Flag* members support the bitwise operators ``&`` (*AND*), ``|`` (*OR*), - ``^`` (*XOR*), and ``~`` (*INVERT*); the results of those operators are members - of the enumeration. + >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal') - .. method:: __contains__(self, value) +The complete signature is:: - Returns *True* if value is in self:: + Enum(value='NewEnumName', names=<...>, *, module='...', qualname='...', type=, start=1) - >>> from enum import Flag, auto - >>> class Color(Flag): - ... RED = auto() - ... GREEN = auto() - ... BLUE = auto() - >>> purple = Color.RED | Color.BLUE - >>> white = Color.RED | Color.GREEN | Color.BLUE - >>> Color.GREEN in purple - False - >>> Color.GREEN in white - True - >>> purple in white - True - >>> white in purple - False +:value: What the new Enum class will record as its name. - .. method:: __iter__(self): +:names: The Enum members. This can be a whitespace or comma separated string + (values will start at 1 unless otherwise specified):: - Returns all contained members:: + 'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE' - >>> list(Color.RED) - [Color.RED] - >>> list(purple) - [Color.RED, Color.BLUE] + or an iterator of names:: - .. method:: __len__(self): + ['RED', 'GREEN', 'BLUE'] - Returns number of members in flag:: + or an iterator of (name, value) pairs:: - >>> len(Color.GREEN) - 1 - >>> len(white) - 3 + [('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)] - .. method:: __bool__(self): + or a mapping:: - Returns *True* if any members in flag, *False* otherwise:: + {'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42} - >>> bool(Color.GREEN) - True - >>> bool(white) - True - >>> black = Color(0) - >>> bool(black) - False +:module: name of module where new Enum class can be found. - .. method:: __or__(self, other) +:qualname: where in module new Enum class can be found. - Returns current flag binary or'ed with other:: +:type: type to mix in to new Enum class. - >>> Color.RED | Color.GREEN - Color.RED|Color.GREEN +:start: number to start counting at if only names are passed in. - .. method:: __and__(self, other) +.. versionchanged:: 3.5 + The *start* parameter was added. - Returns current flag binary and'ed with other:: - >>> purple & white - Color.RED|Color.BLUE - >>> purple & Color.GREEN - 0x0 +Derived Enumerations +-------------------- - .. method:: __xor__(self, other) +IntEnum +^^^^^^^ - Returns current flag binary xor'ed with other:: +The first variation of :class:`Enum` that is provided is also a subclass of +:class:`int`. Members of an :class:`IntEnum` can be compared to integers; +by extension, integer enumerations of different types can also be compared +to each other:: - >>> purple ^ white - Color.GREEN - >>> purple ^ Color.GREEN - Color.RED|Color.GREEN|Color.BLUE + >>> from enum import IntEnum + >>> class Shape(IntEnum): + ... CIRCLE = 1 + ... SQUARE = 2 + ... + >>> class Request(IntEnum): + ... POST = 1 + ... GET = 2 + ... + >>> Shape == 1 + False + >>> Shape.CIRCLE == 1 + True + >>> Shape.CIRCLE == Request.POST + True - .. method:: __invert__(self): +However, they still can't be compared to standard :class:`Enum` enumerations:: - Returns all the flags in *type(self)* that are not in self:: + >>> class Shape(IntEnum): + ... CIRCLE = 1 + ... SQUARE = 2 + ... + >>> class Color(Enum): + ... RED = 1 + ... GREEN = 2 + ... + >>> Shape.CIRCLE == Color.RED + False - >>> ~white - 0x0 - >>> ~purple - Color.GREEN - >>> ~Color.RED - Color.GREEN|Color.BLUE +:class:`IntEnum` values behave like integers in other ways you'd expect:: -.. note:: + >>> int(Shape.CIRCLE) + 1 + >>> ['a', 'b', 'c'][Shape.CIRCLE] + 'b' + >>> [i for i in range(Shape.SQUARE)] + [0, 1] - Using :class:`auto` with :class:`Flag` results in integers that are powers - of two, starting with ``1``. +IntFlag +^^^^^^^ -.. class:: IntFlag +The next variation of :class:`Enum` provided, :class:`IntFlag`, is also based +on :class:`int`. The difference being :class:`IntFlag` members can be combined +using the bitwise operators (&, \|, ^, ~) and the result is still an +:class:`IntFlag` member. However, as the name implies, :class:`IntFlag` +members also subclass :class:`int` and can be used wherever an :class:`int` is +used. Any operation on an :class:`IntFlag` member besides the bit-wise +operations will lose the :class:`IntFlag` membership. - *IntFlag* is the same as *Flag*, but its members are also integers and can be - used anywhere that an integer can be used. +.. versionadded:: 3.6 - >>> from enum import IntFlag, auto - >>> class Color(IntFlag): - ... RED = auto() - ... GREEN = auto() - ... BLUE = auto() - >>> Color.RED & 2 - 0x0 - >>> Color.RED | 2 - Color.RED|Color.GREEN +Sample :class:`IntFlag` class:: - If any integer operation is performed with an *IntFlag* member, the result is - not an *IntFlag*:: + >>> from enum import IntFlag + >>> class Perm(IntFlag): + ... R = 4 + ... W = 2 + ... X = 1 + ... + >>> Perm.R | Perm.W + + >>> Perm.R + Perm.W + 6 + >>> RW = Perm.R | Perm.W + >>> Perm.R in RW + True + +It is also possible to name the combinations:: + + >>> class Perm(IntFlag): + ... R = 4 + ... W = 2 + ... X = 1 + ... RWX = 7 + >>> Perm.RWX + + >>> ~Perm.RWX + + +Another important difference between :class:`IntFlag` and :class:`Enum` is that +if no flags are set (the value is 0), its boolean evaluation is :data:`False`:: + + >>> Perm.R & Perm.X + + >>> bool(Perm.R & Perm.X) + False + +Because :class:`IntFlag` members are also subclasses of :class:`int` they can +be combined with them:: + + >>> Perm.X | 8 + + + +Flag +^^^^ + +The last variation is :class:`Flag`. Like :class:`IntFlag`, :class:`Flag` +members can be combined using the bitwise operators (&, \|, ^, ~). Unlike +:class:`IntFlag`, they cannot be combined with, nor compared against, any +other :class:`Flag` enumeration, nor :class:`int`. While it is possible to +specify the values directly it is recommended to use :class:`auto` as the +value and let :class:`Flag` select an appropriate value. + +.. versionadded:: 3.6 + +Like :class:`IntFlag`, if a combination of :class:`Flag` members results in no +flags being set, the boolean evaluation is :data:`False`:: + + >>> from enum import Flag, auto + >>> class Color(Flag): + ... RED = auto() + ... BLUE = auto() + ... GREEN = auto() + ... + >>> Color.RED & Color.GREEN + + >>> bool(Color.RED & Color.GREEN) + False + +Individual flags should have values that are powers of two (1, 2, 4, 8, ...), +while combinations of flags won't:: + + >>> class Color(Flag): + ... RED = auto() + ... BLUE = auto() + ... GREEN = auto() + ... WHITE = RED | BLUE | GREEN + ... + >>> Color.WHITE + + +Giving a name to the "no flags set" condition does not change its boolean +value:: + + >>> class Color(Flag): + ... BLACK = 0 + ... RED = auto() + ... BLUE = auto() + ... GREEN = auto() + ... + >>> Color.BLACK + + >>> bool(Color.BLACK) + False + +.. note:: - >>> Color.RED + 2 - 3 + For the majority of new code, :class:`Enum` and :class:`Flag` are strongly + recommended, since :class:`IntEnum` and :class:`IntFlag` break some + semantic promises of an enumeration (by being comparable to integers, and + thus by transitivity to other unrelated enumerations). :class:`IntEnum` + and :class:`IntFlag` should be used only in cases where :class:`Enum` and + :class:`Flag` will not do; for example, when integer constants are replaced + with enumerations, or for interoperability with other systems. + + +Others +^^^^^^ + +While :class:`IntEnum` is part of the :mod:`enum` module, it would be very +simple to implement independently:: + + class IntEnum(int, Enum): + pass + +This demonstrates how similar derived enumerations can be defined; for example +a :class:`StrEnum` that mixes in :class:`str` instead of :class:`int`. + +Some rules: + +1. When subclassing :class:`Enum`, mix-in types must appear before + :class:`Enum` itself in the sequence of bases, as in the :class:`IntEnum` + example above. +2. While :class:`Enum` can have members of any type, once you mix in an + additional type, all the members must have values of that type, e.g. + :class:`int` above. This restriction does not apply to mix-ins which only + add methods and don't specify another type. +3. When another data type is mixed in, the :attr:`value` attribute is *not the + same* as the enum member itself, although it is equivalent and will compare + equal. +4. %-style formatting: `%s` and `%r` call the :class:`Enum` class's + :meth:`__str__` and :meth:`__repr__` respectively; other codes (such as + `%i` or `%h` for IntEnum) treat the enum member as its mixed-in type. +5. :ref:`Formatted string literals `, :meth:`str.format`, + and :func:`format` will use the mixed-in type's :meth:`__format__` + unless :meth:`__str__` or :meth:`__format__` is overridden in the subclass, + in which case the overridden methods or :class:`Enum` methods will be used. + Use the !s and !r format codes to force usage of the :class:`Enum` class's + :meth:`__str__` and :meth:`__repr__` methods. + +When to use :meth:`__new__` vs. :meth:`__init__` +------------------------------------------------ + +:meth:`__new__` must be used whenever you want to customize the actual value of +the :class:`Enum` member. Any other modifications may go in either +:meth:`__new__` or :meth:`__init__`, with :meth:`__init__` being preferred. + +For example, if you want to pass several items to the constructor, but only +want one of them to be the value:: + + >>> class Coordinate(bytes, Enum): + ... """ + ... Coordinate with binary codes that can be indexed by the int code. + ... """ + ... def __new__(cls, value, label, unit): + ... obj = bytes.__new__(cls, [value]) + ... obj._value_ = value + ... obj.label = label + ... obj.unit = unit + ... return obj + ... PX = (0, 'P.X', 'km') + ... PY = (1, 'P.Y', 'km') + ... VX = (2, 'V.X', 'km/s') + ... VY = (3, 'V.Y', 'km/s') + ... + + >>> print(Coordinate['PY']) + Coordinate.PY + + >>> print(Coordinate(3)) + Coordinate.VY + +Interesting examples +-------------------- + +While :class:`Enum`, :class:`IntEnum`, :class:`IntFlag`, and :class:`Flag` are +expected to cover the majority of use-cases, they cannot cover them all. Here +are recipes for some different types of enumerations that can be used directly, +or as examples for creating one's own. + + +Omitting values +^^^^^^^^^^^^^^^ + +In many use-cases one doesn't care what the actual value of an enumeration +is. There are several ways to define this type of simple enumeration: + +- use instances of :class:`auto` for the value +- use instances of :class:`object` as the value +- use a descriptive string as the value +- use a tuple as the value and a custom :meth:`__new__` to replace the + tuple with an :class:`int` value + +Using any of these methods signifies to the user that these values are not +important, and also enables one to add, remove, or reorder members without +having to renumber the remaining members. + +Whichever method you choose, you should provide a :meth:`repr` that also hides +the (unimportant) value:: + + >>> class NoValue(Enum): + ... def __repr__(self): + ... return '<%s.%s>' % (self.__class__.__name__, self.name) + ... + + +Using :class:`auto` +""""""""""""""""""" + +Using :class:`auto` would look like:: + + >>> class Color(NoValue): + ... RED = auto() + ... BLUE = auto() + ... GREEN = auto() + ... + >>> Color.GREEN + + + +Using :class:`object` +""""""""""""""""""""" + +Using :class:`object` would look like:: + + >>> class Color(NoValue): + ... RED = object() + ... GREEN = object() + ... BLUE = object() + ... + >>> Color.GREEN + + + +Using a descriptive string +"""""""""""""""""""""""""" + +Using a string as the value would look like:: + + >>> class Color(NoValue): + ... RED = 'stop' + ... GREEN = 'go' + ... BLUE = 'too fast!' + ... + >>> Color.GREEN + + >>> Color.GREEN.value + 'go' + + +Using a custom :meth:`__new__` +"""""""""""""""""""""""""""""" + +Using an auto-numbering :meth:`__new__` would look like:: + + >>> class AutoNumber(NoValue): + ... def __new__(cls): + ... value = len(cls.__members__) + 1 + ... obj = object.__new__(cls) + ... obj._value_ = value + ... return obj + ... + >>> class Color(AutoNumber): + ... RED = () + ... GREEN = () + ... BLUE = () + ... + >>> Color.GREEN + + >>> Color.GREEN.value + 2 + +To make a more general purpose ``AutoNumber``, add ``*args`` to the signature:: + + >>> class AutoNumber(NoValue): + ... def __new__(cls, *args): # this is the only change from above + ... value = len(cls.__members__) + 1 + ... obj = object.__new__(cls) + ... obj._value_ = value + ... return obj + ... + +Then when you inherit from ``AutoNumber`` you can write your own ``__init__`` +to handle any extra arguments:: + + >>> class Swatch(AutoNumber): + ... def __init__(self, pantone='unknown'): + ... self.pantone = pantone + ... AUBURN = '3497' + ... SEA_GREEN = '1246' + ... BLEACHED_CORAL = () # New color, no Pantone code yet! + ... + >>> Swatch.SEA_GREEN + + >>> Swatch.SEA_GREEN.pantone + '1246' + >>> Swatch.BLEACHED_CORAL.pantone + 'unknown' - If a *Flag* operation is performed with an *IntFlag* member and: +.. note:: - * the result is a valid *IntFlag*: an *IntFlag* is returned - * the result is not a valid *IntFlag*: the result depends on the *FlagBoundary* setting + The :meth:`__new__` method, if defined, is used during creation of the Enum + members; it is then replaced by Enum's :meth:`__new__` which is used after + class creation for lookup of existing members. + + +OrderedEnum +^^^^^^^^^^^ + +An ordered enumeration that is not based on :class:`IntEnum` and so maintains +the normal :class:`Enum` invariants (such as not being comparable to other +enumerations):: + + >>> class OrderedEnum(Enum): + ... def __ge__(self, other): + ... if self.__class__ is other.__class__: + ... return self.value >= other.value + ... return NotImplemented + ... def __gt__(self, other): + ... if self.__class__ is other.__class__: + ... return self.value > other.value + ... return NotImplemented + ... def __le__(self, other): + ... if self.__class__ is other.__class__: + ... return self.value <= other.value + ... return NotImplemented + ... def __lt__(self, other): + ... if self.__class__ is other.__class__: + ... return self.value < other.value + ... return NotImplemented + ... + >>> class Grade(OrderedEnum): + ... A = 5 + ... B = 4 + ... C = 3 + ... D = 2 + ... F = 1 + ... + >>> Grade.C < Grade.A + True + + +DuplicateFreeEnum +^^^^^^^^^^^^^^^^^ + +Raises an error if a duplicate member name is found instead of creating an +alias:: + + >>> class DuplicateFreeEnum(Enum): + ... def __init__(self, *args): + ... cls = self.__class__ + ... if any(self.value == e.value for e in cls): + ... a = self.name + ... e = cls(self.value).name + ... raise ValueError( + ... "aliases not allowed in DuplicateFreeEnum: %r --> %r" + ... % (a, e)) + ... + >>> class Color(DuplicateFreeEnum): + ... RED = 1 + ... GREEN = 2 + ... BLUE = 3 + ... GRENE = 2 + ... + Traceback (most recent call last): + ... + ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN' .. note:: - Using :class:`auto` with :class:`IntFlag` results in integers that are powers - of two, starting with ``1``. + This is a useful example for subclassing Enum to add or change other + behaviors as well as disallowing aliases. If the only desired change is + disallowing aliases, the :func:`unique` decorator can be used instead. -.. class:: FlagBoundary - *FlagBoundary* controls how out-of-range values are handled in *Flag* and its - subclasses. +Planet +^^^^^^ - .. attribute:: STRICT +If :meth:`__new__` or :meth:`__init__` is defined the value of the enum member +will be passed to those methods:: + + >>> class Planet(Enum): + ... MERCURY = (3.303e+23, 2.4397e6) + ... VENUS = (4.869e+24, 6.0518e6) + ... EARTH = (5.976e+24, 6.37814e6) + ... MARS = (6.421e+23, 3.3972e6) + ... JUPITER = (1.9e+27, 7.1492e7) + ... SATURN = (5.688e+26, 6.0268e7) + ... URANUS = (8.686e+25, 2.5559e7) + ... NEPTUNE = (1.024e+26, 2.4746e7) + ... def __init__(self, mass, radius): + ... self.mass = mass # in kilograms + ... self.radius = radius # in meters + ... @property + ... def surface_gravity(self): + ... # universal gravitational constant (m3 kg-1 s-2) + ... G = 6.67300E-11 + ... return G * self.mass / (self.radius * self.radius) + ... + >>> Planet.EARTH.value + (5.976e+24, 6378140.0) + >>> Planet.EARTH.surface_gravity + 9.802652743337129 + + +TimePeriod +^^^^^^^^^^ + +An example to show the :attr:`_ignore_` attribute in use:: + + >>> from datetime import timedelta + >>> class Period(timedelta, Enum): + ... "different lengths of time" + ... _ignore_ = 'Period i' + ... Period = vars() + ... for i in range(367): + ... Period['day_%d' % i] = i + ... + >>> list(Period)[:2] + [, ] + >>> list(Period)[-2:] + [, ] + + +How are Enums different? +------------------------ + +Enums have a custom metaclass that affects many aspects of both derived Enum +classes and their instances (members). + + +Enum Classes +^^^^^^^^^^^^ + +The :class:`EnumMeta` metaclass is responsible for providing the +:meth:`__contains__`, :meth:`__dir__`, :meth:`__iter__` and other methods that +allow one to do things with an :class:`Enum` class that fail on a typical +class, such as `list(Color)` or `some_enum_var in Color`. :class:`EnumMeta` is +responsible for ensuring that various other methods on the final :class:`Enum` +class are correct (such as :meth:`__new__`, :meth:`__getnewargs__`, +:meth:`__str__` and :meth:`__repr__`). + + +Enum Members (aka instances) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The most interesting thing about Enum members is that they are singletons. +:class:`EnumMeta` creates them all while it is creating the :class:`Enum` +class itself, and then puts a custom :meth:`__new__` in place to ensure +that no new ones are ever instantiated by returning only the existing +member instances. + + +Finer Points +^^^^^^^^^^^^ + +Supported ``__dunder__`` names +"""""""""""""""""""""""""""""" + +:attr:`__members__` is a read-only ordered mapping of ``member_name``:``member`` +items. It is only available on the class. + +:meth:`__new__`, if specified, must create and return the enum members; it is +also a very good idea to set the member's :attr:`_value_` appropriately. Once +all the members are created it is no longer used. + + +Supported ``_sunder_`` names +"""""""""""""""""""""""""""" + +- ``_name_`` -- name of the member +- ``_value_`` -- value of the member; can be set / modified in ``__new__`` + +- ``_missing_`` -- a lookup function used when a value is not found; may be + overridden +- ``_ignore_`` -- a list of names, either as a :class:`list` or a :class:`str`, + that will not be transformed into members, and will be removed from the final + class +- ``_order_`` -- used in Python 2/3 code to ensure member order is consistent + (class attribute, removed during class creation) +- ``_generate_next_value_`` -- used by the `Functional API`_ and by + :class:`auto` to get an appropriate value for an enum member; may be + overridden + +.. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_`` +.. versionadded:: 3.7 ``_ignore_`` + +To help keep Python 2 / Python 3 code in sync an :attr:`_order_` attribute can +be provided. It will be checked against the actual order of the enumeration +and raise an error if the two do not match:: + + >>> class Color(Enum): + ... _order_ = 'RED GREEN BLUE' + ... RED = 1 + ... BLUE = 3 + ... GREEN = 2 + ... + Traceback (most recent call last): + ... + TypeError: member order does not match _order_ - Out-of-range values cause a :exc:`ValueError` to be raised. This is the - default for :class:`Flag`:: +.. note:: - >>> from enum import Flag, STRICT - >>> class StrictFlag(Flag, boundary=STRICT): - ... RED = auto() - ... GREEN = auto() - ... BLUE = auto() - >>> StrictFlag(2**2 + 2**4) - Traceback (most recent call last): - ... - ValueError: StrictFlag: invalid value: 20 - given 0b0 10100 - allowed 0b0 00111 + In Python 2 code the :attr:`_order_` attribute is necessary as definition + order is lost before it can be recorded. - .. attribute:: CONFORM - Out-of-range values have invalid values removed, leaving a valid *Flag* - value:: +_Private__names +""""""""""""""" - >>> from enum import Flag, CONFORM - >>> class ConformFlag(Flag, boundary=CONFORM): - ... RED = auto() - ... GREEN = auto() - ... BLUE = auto() - >>> ConformFlag(2**2 + 2**4) - ConformFlag.BLUE +:ref:`Private names ` will be normal attributes in Python +3.11 instead of either an error or a member (depending on if the name ends with +an underscore). Using these names in 3.10 will issue a :exc:`DeprecationWarning`. - .. attribute:: EJECT - Out-of-range values lose their *Flag* membership and revert to :class:`int`. - This is the default for :class:`IntFlag`:: +``Enum`` member type +"""""""""""""""""""" - >>> from enum import Flag, EJECT - >>> class EjectFlag(Flag, boundary=EJECT): - ... RED = auto() - ... GREEN = auto() - ... BLUE = auto() - >>> EjectFlag(2**2 + 2**4) - 20 +:class:`Enum` members are instances of their :class:`Enum` class, and are +normally accessed as ``EnumClass.member``. Under certain circumstances they +can also be accessed as ``EnumClass.member.member``, but you should never do +this as that lookup may fail or, worse, return something besides the +:class:`Enum` member you are looking for (this is another good reason to use +all-uppercase names for members):: - .. attribute:: KEEP + >>> class FieldTypes(Enum): + ... name = 0 + ... value = 1 + ... size = 2 + ... + >>> FieldTypes.value.size + + >>> FieldTypes.size.value + 2 - Out-of-range values are kept, and the *Flag* membership is kept. This is - used for some stdlib flags: +.. note:: - >>> from enum import Flag, KEEP - >>> class KeepFlag(Flag, boundary=KEEP): - ... RED = auto() - ... GREEN = auto() - ... BLUE = auto() - >>> KeepFlag(2**2 + 2**4) - KeepFlag.BLUE|0x10 + This behavior is deprecated and will be removed in 3.11. +.. versionchanged:: 3.5 -Utilites and Decorators ------------------------ -.. class:: auto +Boolean value of ``Enum`` classes and members +""""""""""""""""""""""""""""""""""""""""""""" - *auto* can be used in place of a value. If used, the *Enum* machinery will - call an *Enum*'s :meth:`_generate_next_value_` to get an appropriate value. - For *Enum* and *IntEnum* that appropriate value will be the last value plus - one; for *Flag* and *IntFlag* it will be the first power-of-two greater - than the last value; for *StrEnum* it will be the lower-cased version of the - member's name. +:class:`Enum` members that are mixed with non-:class:`Enum` types (such as +:class:`int`, :class:`str`, etc.) are evaluated according to the mixed-in +type's rules; otherwise, all members evaluate as :data:`True`. To make your +own Enum's boolean evaluation depend on the member's value add the following to +your class:: - ``_generate_next_value_`` can be overridden to customize the values used by - *auto*. + def __bool__(self): + return bool(self.value) -.. decorator:: global_enum +:class:`Enum` classes always evaluate as :data:`True`. - A :keyword:`class` decorator specifically for enumerations. It replaces the - :meth:`__repr__` method with one that shows *module_name*.*member_name*. It - also injects the members, and their aliases, into the the global namespace - they were defined in. +``Enum`` classes with methods +""""""""""""""""""""""""""""" -.. decorator:: property +If you give your :class:`Enum` subclass extra methods, like the `Planet`_ +class above, those methods will show up in a :func:`dir` of the member, +but not of the class:: - A decorator similar to the built-in *property*, but specifically for - enumerations. It allows member attributes to have the same names as members - themselves. + >>> dir(Planet) + ['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__members__', '__module__'] + >>> dir(Planet.EARTH) + ['__class__', '__doc__', '__module__', 'mass', 'name', 'radius', 'surface_gravity', 'value'] - .. note:: the *property* and the member must be defined in separate classes; - for example, the *value* and *name* attributes are defined in the - *Enum* class, and *Enum* subclasses can define members with the - names ``value`` and ``name``. -.. decorator:: unique +Combining members of ``Flag`` +""""""""""""""""""""""""""""" + +If a combination of Flag members is not named, the :func:`repr` will include +all named flags and all named combinations of flags that are in the value:: + + >>> class Color(Flag): + ... RED = auto() + ... GREEN = auto() + ... BLUE = auto() + ... MAGENTA = RED | BLUE + ... YELLOW = RED | GREEN + ... CYAN = GREEN | BLUE + ... + >>> Color(3) # named combination + + >>> Color(7) # not named combination + + +.. note:: + + In 3.11 unnamed combinations of flags will only produce the canonical flag + members (aka single-value flags). So ``Color(7)`` will produce something + like ````. - A :keyword:`class` decorator specifically for enumerations. It searches an - enumeration's :attr:`__members__`, gathering any aliases it finds; if any are - found :exc:`ValueError` is raised with the details:: - - >>> from enum import Enum, unique - >>> @unique - ... class Mistake(Enum): - ... ONE = 1 - ... TWO = 2 - ... THREE = 3 - ... FOUR = 3 - ... - Traceback (most recent call last): - ... - ValueError: duplicate values found in : FOUR -> THREE diff --git a/Doc/library/errno.rst b/Doc/library/errno.rst index 1cbd51c582c0cf..c87da091bf84f6 100644 --- a/Doc/library/errno.rst +++ b/Doc/library/errno.rst @@ -8,7 +8,7 @@ This module makes available standard ``errno`` system symbols. The value of each symbol is the corresponding integer value. The names and descriptions are -borrowed from :file:`linux/include/errno.h`, which should be pretty +borrowed from :file:`linux/include/errno.h`, which should be all-inclusive. @@ -27,25 +27,26 @@ defined by the module. The specific list of defined symbols is available as .. data:: EPERM - Operation not permitted + Operation not permitted. This error is mapped to the exception + :exc:`PermissionError`. .. data:: ENOENT - No such file or directory + No such file or directory. This error is mapped to the exception + :exc:`FileNotFoundError`. .. data:: ESRCH - No such process + No such process. This error is mapped to the exception + :exc:`ProcessLookupError`. .. data:: EINTR - Interrupted system call. - - .. seealso:: - This error is mapped to the exception :exc:`InterruptedError`. + Interrupted system call. This error is mapped to the exception + :exc:`InterruptedError`. .. data:: EIO @@ -75,12 +76,13 @@ defined by the module. The specific list of defined symbols is available as .. data:: ECHILD - No child processes + No child processes. This error is mapped to the exception + :exc:`ChildProcessError`. .. data:: EAGAIN - Try again + Try again. This error is mapped to the exception :exc:`BlockingIOError`. .. data:: ENOMEM @@ -90,7 +92,8 @@ defined by the module. The specific list of defined symbols is available as .. data:: EACCES - Permission denied + Permission denied. This error is mapped to the exception + :exc:`PermissionError`. .. data:: EFAULT @@ -110,7 +113,8 @@ defined by the module. The specific list of defined symbols is available as .. data:: EEXIST - File exists + File exists. This error is mapped to the exception + :exc:`FileExistsError`. .. data:: EXDEV @@ -125,12 +129,14 @@ defined by the module. The specific list of defined symbols is available as .. data:: ENOTDIR - Not a directory + Not a directory. This error is mapped to the exception + :exc:`NotADirectoryError`. .. data:: EISDIR - Is a directory + Is a directory. This error is mapped to the exception + :exc:`IsADirectoryError`. .. data:: EINVAL @@ -185,7 +191,8 @@ defined by the module. The specific list of defined symbols is available as .. data:: EPIPE - Broken pipe + Broken pipe. This error is mapped to the exception + :exc:`BrokenPipeError`. .. data:: EDOM @@ -230,7 +237,8 @@ defined by the module. The specific list of defined symbols is available as .. data:: EWOULDBLOCK - Operation would block + Operation would block. This error is mapped to the exception + :exc:`BlockingIOError`. .. data:: ENOMSG @@ -540,12 +548,14 @@ defined by the module. The specific list of defined symbols is available as .. data:: ECONNABORTED - Software caused connection abort + Software caused connection abort. This error is mapped to the + exception :exc:`ConnectionAbortedError`. .. data:: ECONNRESET - Connection reset by peer + Connection reset by peer. This error is mapped to the exception + :exc:`ConnectionResetError`. .. data:: ENOBUFS @@ -565,7 +575,8 @@ defined by the module. The specific list of defined symbols is available as .. data:: ESHUTDOWN - Cannot send after transport endpoint shutdown + Cannot send after transport endpoint shutdown. This error is mapped + to the exception :exc:`BrokenPipeError`. .. data:: ETOOMANYREFS @@ -575,12 +586,14 @@ defined by the module. The specific list of defined symbols is available as .. data:: ETIMEDOUT - Connection timed out + Connection timed out. This error is mapped to the exception + :exc:`TimeoutError`. .. data:: ECONNREFUSED - Connection refused + Connection refused. This error is mapped to the exception + :exc:`ConnectionRefusedError`. .. data:: EHOSTDOWN @@ -595,12 +608,14 @@ defined by the module. The specific list of defined symbols is available as .. data:: EALREADY - Operation already in progress + Operation already in progress. This error is mapped to the + exception :exc:`BlockingIOError`. .. data:: EINPROGRESS - Operation now in progress + Operation now in progress. This error is mapped to the exception + :exc:`BlockingIOError`. .. data:: ESTALE diff --git a/Doc/library/exceptions.rst b/Doc/library/exceptions.rst index c83daae302c19d..9a3c92554df632 100644 --- a/Doc/library/exceptions.rst +++ b/Doc/library/exceptions.rst @@ -34,15 +34,18 @@ class or one of its subclasses, and not from :exc:`BaseException`. More information on defining exceptions is available in the Python Tutorial under :ref:`tut-userexceptions`. -When raising (or re-raising) an exception in an :keyword:`except` or -:keyword:`finally` clause -:attr:`__context__` is automatically set to the last exception caught; if the -new exception is not handled the traceback that is eventually displayed will -include the originating exception(s) and the final exception. - -When raising a new exception (rather than using a bare ``raise`` to re-raise -the exception currently being handled), the implicit exception context can be -supplemented with an explicit cause by using :keyword:`from` with + +Exception context +----------------- + +When raising a new exception while another exception +is already being handled, the new exception's +:attr:`__context__` attribute is automatically set to the handled +exception. An exception may be handled when an :keyword:`except` or +:keyword:`finally` clause, or a :keyword:`with` statement, is used. + +This implicit exception context can be +supplemented with an explicit cause by using :keyword:`!from` with :keyword:`raise`:: raise new_exc from original_exc @@ -67,6 +70,25 @@ exceptions so that the final line of the traceback always shows the last exception that was raised. +Inheriting from built-in exceptions +----------------------------------- + +User code can create subclasses that inherit from an exception type. +It's recommended to only subclass one exception type at a time to avoid +any possible conflicts between how the bases handle the ``args`` +attribute, as well as due to possible memory layout incompatibilities. + +.. impl-detail:: + + Most built-in exceptions are implemented in C for efficiency, see: + :source:`Objects/exceptions.c`. Some have custom memory layouts + which makes it impossible to create a subclass that inherits from + multiple exception types. The memory layout of a type is an implementation + detail and might change between Python versions, leading to new + conflicts in the future. Therefore, it's recommended to avoid + subclassing multiple exception types altogether. + + Base classes ------------ @@ -96,7 +118,7 @@ The following exceptions are used mostly as base classes for other exceptions. instance of ``OtherException`` while preserving the traceback. Once raised, the current frame is pushed onto the traceback of the ``OtherException``, as would have happened to the traceback of the - original ``SomeException`` had we allowed it to propagate to the caller. + original ``SomeException`` had we allowed it to propagate to the caller. :: try: ... @@ -224,6 +246,15 @@ The following exceptions are the exceptions that are usually raised. accidentally caught by code that catches :exc:`Exception` and thus prevent the interpreter from exiting. + .. note:: + + Catching a :exc:`KeyboardInterrupt` requires special consideration. + Because it can be raised at unpredictable points, it may, in some + circumstances, leave the running program in an inconsistent state. It is + generally best to allow :exc:`KeyboardInterrupt` to end the program as + quickly as possible or avoid raising it entirely. (See + :ref:`handlers-and-exceptions`.) + .. exception:: MemoryError @@ -409,14 +440,16 @@ The following exceptions are the exceptions that are usually raised. .. versionadded:: 3.5 -.. exception:: SyntaxError +.. exception:: SyntaxError(message, details) Raised when the parser encounters a syntax error. This may occur in an - :keyword:`import` statement, in a call to the built-in functions :func:`exec` + :keyword:`import` statement, in a call to the built-in functions + :func:`compile`, :func:`exec`, or :func:`eval`, or when reading the initial script or standard input (also interactively). The :func:`str` of the exception instance returns only the error message. + Details is a tuple whose members are also available as separate attributes. .. attribute:: filename @@ -446,6 +479,11 @@ The following exceptions are the exceptions that are usually raised. The column in the end line where the error occurred finishes. This is 1-indexed: the first character in the line has an ``offset`` of 1. + For errors in f-string fields, the message is prefixed by "f-string: " + and the offsets are offsets in a text constructed from the replacement + expression. For example, compiling f'Bad {a b} field' results in this + args attribute: ('f-string: ...', ('', 1, 2, '(a b)\n', 1, 5)). + .. versionchanged:: 3.10 Added the :attr:`end_lineno` and :attr:`end_offset` attributes. @@ -606,8 +644,8 @@ depending on the system error code. Raised when an operation would block on an object (e.g. socket) set for non-blocking operation. - Corresponds to :c:data:`errno` ``EAGAIN``, ``EALREADY``, - ``EWOULDBLOCK`` and ``EINPROGRESS``. + Corresponds to :c:data:`errno` :py:data:`~errno.EAGAIN`, :py:data:`~errno.EALREADY`, + :py:data:`~errno.EWOULDBLOCK` and :py:data:`~errno.EINPROGRESS`. In addition to those of :exc:`OSError`, :exc:`BlockingIOError` can have one more attribute: @@ -621,7 +659,7 @@ depending on the system error code. .. exception:: ChildProcessError Raised when an operation on a child process failed. - Corresponds to :c:data:`errno` ``ECHILD``. + Corresponds to :c:data:`errno` :py:data:`~errno.ECHILD`. .. exception:: ConnectionError @@ -635,35 +673,35 @@ depending on the system error code. A subclass of :exc:`ConnectionError`, raised when trying to write on a pipe while the other end has been closed, or trying to write on a socket which has been shutdown for writing. - Corresponds to :c:data:`errno` ``EPIPE`` and ``ESHUTDOWN``. + Corresponds to :c:data:`errno` :py:data:`~errno.EPIPE` and :py:data:`~errno.ESHUTDOWN`. .. exception:: ConnectionAbortedError A subclass of :exc:`ConnectionError`, raised when a connection attempt is aborted by the peer. - Corresponds to :c:data:`errno` ``ECONNABORTED``. + Corresponds to :c:data:`errno` :py:data:`~errno.ECONNABORTED`. .. exception:: ConnectionRefusedError A subclass of :exc:`ConnectionError`, raised when a connection attempt is refused by the peer. - Corresponds to :c:data:`errno` ``ECONNREFUSED``. + Corresponds to :c:data:`errno` :py:data:`~errno.ECONNREFUSED`. .. exception:: ConnectionResetError A subclass of :exc:`ConnectionError`, raised when a connection is reset by the peer. - Corresponds to :c:data:`errno` ``ECONNRESET``. + Corresponds to :c:data:`errno` :py:data:`~errno.ECONNRESET`. .. exception:: FileExistsError Raised when trying to create a file or directory which already exists. - Corresponds to :c:data:`errno` ``EEXIST``. + Corresponds to :c:data:`errno` :py:data:`~errno.EEXIST`. .. exception:: FileNotFoundError Raised when a file or directory is requested but doesn't exist. - Corresponds to :c:data:`errno` ``ENOENT``. + Corresponds to :c:data:`errno` :py:data:`~errno.ENOENT`. .. exception:: InterruptedError @@ -679,29 +717,31 @@ depending on the system error code. Raised when a file operation (such as :func:`os.remove`) is requested on a directory. - Corresponds to :c:data:`errno` ``EISDIR``. + Corresponds to :c:data:`errno` :py:data:`~errno.EISDIR`. .. exception:: NotADirectoryError - Raised when a directory operation (such as :func:`os.listdir`) is requested - on something which is not a directory. - Corresponds to :c:data:`errno` ``ENOTDIR``. + Raised when a directory operation (such as :func:`os.listdir`) is requested on + something which is not a directory. On most POSIX platforms, it may also be + raised if an operation attempts to open or traverse a non-directory file as if + it were a directory. + Corresponds to :c:data:`errno` :py:data:`~errno.ENOTDIR`. .. exception:: PermissionError Raised when trying to run an operation without the adequate access rights - for example filesystem permissions. - Corresponds to :c:data:`errno` ``EACCES`` and ``EPERM``. + Corresponds to :c:data:`errno` :py:data:`~errno.EACCES` and :py:data:`~errno.EPERM`. .. exception:: ProcessLookupError Raised when a given process doesn't exist. - Corresponds to :c:data:`errno` ``ESRCH``. + Corresponds to :c:data:`errno` :py:data:`~errno.ESRCH`. .. exception:: TimeoutError Raised when a system function timed out at the system level. - Corresponds to :c:data:`errno` ``ETIMEDOUT``. + Corresponds to :c:data:`errno` :py:data:`~errno.ETIMEDOUT`. .. versionadded:: 3.3 All the above :exc:`OSError` subclasses were added. @@ -739,6 +779,8 @@ The following exceptions are used as warning categories; see the (:pep:`565`). Enabling the :ref:`Python Development Mode ` shows this warning. + The deprecation policy is described in :pep:`387`. + .. exception:: PendingDeprecationWarning @@ -753,6 +795,8 @@ The following exceptions are used as warning categories; see the Ignored by the default warning filters. Enabling the :ref:`Python Development Mode ` shows this warning. + The deprecation policy is described in :pep:`387`. + .. exception:: SyntaxWarning diff --git a/Doc/library/faulthandler.rst b/Doc/library/faulthandler.rst index 59274c1dd7ec35..be0912376bd8ef 100644 --- a/Doc/library/faulthandler.rst +++ b/Doc/library/faulthandler.rst @@ -76,6 +76,10 @@ Fault handler state .. versionchanged:: 3.6 On Windows, a handler for Windows exception is also installed. + .. versionchanged:: 3.10 + The dump now mentions if a garbage collector collection is running + if *all_threads* is true. + .. function:: disable() Disable the fault handler: uninstall the signal handlers installed by diff --git a/Doc/library/fcntl.rst b/Doc/library/fcntl.rst index 9d8021150c42f5..d9b579fc47d3d8 100644 --- a/Doc/library/fcntl.rst +++ b/Doc/library/fcntl.rst @@ -37,7 +37,8 @@ descriptor. On macOS, the fcntl module exposes the ``F_GETPATH`` constant, which obtains the path of a file from a file descriptor. On Linux(>=3.15), the fcntl module exposes the ``F_OFD_GETLK``, ``F_OFD_SETLK`` - and ``F_OFD_SETLKW`` constants, which working with open file description locks. + and ``F_OFD_SETLKW`` constants, which are used when working with open file + description locks. .. versionchanged:: 3.10 On Linux >= 2.6.11, the fcntl module exposes the ``F_GETPIPE_SZ`` and diff --git a/Doc/library/filecmp.rst b/Doc/library/filecmp.rst index c60603b30a6d7d..83e9e14ddcacd8 100644 --- a/Doc/library/filecmp.rst +++ b/Doc/library/filecmp.rst @@ -22,8 +22,11 @@ The :mod:`filecmp` module defines the following functions: Compare the files named *f1* and *f2*, returning ``True`` if they seem equal, ``False`` otherwise. - If *shallow* is true, files with identical :func:`os.stat` signatures are - taken to be equal. Otherwise, the contents of the files are compared. + If *shallow* is true and the :func:`os.stat` signatures (file type, size, and + modification time) of both files are identical, the files are taken to be + equal. + + Otherwise, the files are treated as different if their sizes or contents differ. Note that no external programs are called from this function, giving it portability and efficiency. diff --git a/Doc/library/fileformats.rst b/Doc/library/fileformats.rst index e9c2e1fbbdf3e8..7b33b3364572d9 100644 --- a/Doc/library/fileformats.rst +++ b/Doc/library/fileformats.rst @@ -13,5 +13,4 @@ that aren't markup languages and are not related to e-mail. csv.rst configparser.rst netrc.rst - xdrlib.rst plistlib.rst diff --git a/Doc/library/fileinput.rst b/Doc/library/fileinput.rst index 819640045cadf7..2df895a69c43dd 100644 --- a/Doc/library/fileinput.rst +++ b/Doc/library/fileinput.rst @@ -50,8 +50,8 @@ You can control how files are opened by providing an opening hook via the *openhook* parameter to :func:`fileinput.input` or :class:`FileInput()`. The hook must be a function that takes two arguments, *filename* and *mode*, and returns an accordingly opened file-like object. If *encoding* and/or *errors* -are specified, they will be passed to the hook as aditional keyword arguments. -This module provides a :func:`hook_encoded` to support compressed files. +are specified, they will be passed to the hook as additional keyword arguments. +This module provides a :func:`hook_compressed` to support compressed files. The following function is the primary interface of this module: @@ -205,7 +205,7 @@ The two following opening hooks are provided by this module: modules. If the filename extension is not ``'.gz'`` or ``'.bz2'``, the file is opened normally (ie, using :func:`open` without any decompression). - The *encoding* and *errors* values are passed to to :class:`io.TextIOWrapper` + The *encoding* and *errors* values are passed to :class:`io.TextIOWrapper` for compressed files and open for normal files. Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed, encoding="utf-8")`` @@ -227,5 +227,5 @@ The two following opening hooks are provided by this module: Added the optional *errors* parameter. .. deprecated:: 3.10 - This function is deprecated since :func:`input` and :class:`FileInput` + This function is deprecated since :func:`fileinput.input` and :class:`FileInput` now have *encoding* and *errors* parameters. diff --git a/Doc/library/fractions.rst b/Doc/library/fractions.rst index a4d006eb58ffeb..0f7940ae68bef4 100644 --- a/Doc/library/fractions.rst +++ b/Doc/library/fractions.rst @@ -105,10 +105,10 @@ another rational number, or from a string. .. versionadded:: 3.8 - .. method:: from_float(flt) + .. classmethod:: from_float(flt) - This class method constructs a :class:`Fraction` representing the exact - value of *flt*, which must be a :class:`float`. Beware that + Alternative constructor which only accepts instances of + :class:`float` or :class:`numbers.Integral`. Beware that ``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``. .. note:: @@ -117,10 +117,10 @@ another rational number, or from a string. :class:`Fraction` instance directly from a :class:`float`. - .. method:: from_decimal(dec) + .. classmethod:: from_decimal(dec) - This class method constructs a :class:`Fraction` representing the exact - value of *dec*, which must be a :class:`decimal.Decimal` instance. + Alternative constructor which only accepts instances of + :class:`decimal.Decimal` or :class:`numbers.Integral`. .. note:: diff --git a/Doc/library/ftplib.rst b/Doc/library/ftplib.rst index f4d4cdf9ada9d9..2f94ac499285db 100644 --- a/Doc/library/ftplib.rst +++ b/Doc/library/ftplib.rst @@ -17,7 +17,7 @@ This module defines the class :class:`FTP` and a few related items. The this to write Python programs that perform a variety of automated FTP jobs, such as mirroring other FTP servers. It is also used by the module :mod:`urllib.request` to handle URLs that use FTP. For more information on FTP -(File Transfer Protocol), see Internet :rfc:`959`. +(File Transfer Protocol), see internet :rfc:`959`. The default encoding is UTF-8, following :rfc:`2640`. @@ -28,6 +28,7 @@ Here's a sample session using the :mod:`ftplib` module:: >>> ftp.login() # user anonymous, passwd anonymous@ '230 Login successful.' >>> ftp.cwd('debian') # change into "debian" directory + '250 Directory successfully changed.' >>> ftp.retrlines('LIST') # list directory contents -rw-rw-r-- 1 1176 1176 1063 Jun 15 10:18 README ... @@ -39,6 +40,7 @@ Here's a sample session using the :mod:`ftplib` module:: >>> ftp.retrbinary('RETR README', fp.write) '226 Transfer complete.' >>> ftp.quit() + '221 Goodbye.' The module defines the following items: diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 7d8a669f9e16d4..85e7d12ceb91fe 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -66,9 +66,6 @@ are always available. They are listed here in alphabetical order. Return an :term:`asynchronous iterator` for an :term:`asynchronous iterable`. Equivalent to calling ``x.__aiter__()``. - ``aiter(x)`` itself has an ``__aiter__()`` method that returns ``x``, - so ``aiter(aiter(x))`` is the same as ``aiter(x)``. - Note: Unlike :func:`iter`, :func:`aiter` has no 2-argument variant. .. versionadded:: 3.10 @@ -116,7 +113,7 @@ are always available. They are listed here in alphabetical order. As :func:`repr`, return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by - :func:`repr` using ``\x``, ``\u`` or ``\U`` escapes. This generates a string + :func:`repr` using ``\x``, ``\u``, or ``\U`` escapes. This generates a string similar to that returned by :func:`repr` in Python 2. @@ -132,7 +129,7 @@ are always available. They are listed here in alphabetical order. >>> bin(-10) '-0b1010' - If prefix "0b" is desired or not, you can use either of the following ways. + If the prefix "0b" is desired or not, you can use either of the following ways. >>> format(14, '#b'), format(14, 'b') ('0b1110', '1110') @@ -146,7 +143,7 @@ are always available. They are listed here in alphabetical order. Return a Boolean value, i.e. one of ``True`` or ``False``. *x* is converted using the standard :ref:`truth testing procedure `. If *x* is false - or omitted, this returns ``False``; otherwise it returns ``True``. The + or omitted, this returns ``False``; otherwise, it returns ``True``. The :class:`bool` class is a subclass of :class:`int` (see :ref:`typesnumeric`). It cannot be subclassed further. Its only instances are ``False`` and ``True`` (see :ref:`bltin-boolean-values`). @@ -206,7 +203,7 @@ are always available. They are listed here in alphabetical order. .. class:: bytes([source[, encoding[, errors]]]) :noindex: - Return a new "bytes" object, which is an immutable sequence of integers in + Return a new "bytes" object which is an immutable sequence of integers in the range ``0 <= x < 256``. :class:`bytes` is an immutable version of :class:`bytearray` -- it has the same non-mutating methods and the same indexing and slicing behavior. @@ -245,13 +242,13 @@ are always available. They are listed here in alphabetical order. Transform a method into a class method. - A class method receives the class as implicit first argument, just like an + A class method receives the class as an implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:: class C: @classmethod - def f(cls, arg1, arg2, ...): ... + def f(cls, arg1, arg2): ... The ``@classmethod`` form is a function :term:`decorator` -- see :ref:`function` for details. @@ -342,7 +339,7 @@ are always available. They are listed here in alphabetical order. object due to stack depth limitations in Python's AST compiler. .. versionchanged:: 3.2 - Allowed use of Windows and Mac newlines. Also input in ``'exec'`` mode + Allowed use of Windows and Mac newlines. Also, input in ``'exec'`` mode does not have to end in a newline anymore. Added the *optimize* parameter. .. versionchanged:: 3.5 @@ -420,7 +417,7 @@ are always available. They are listed here in alphabetical order. If the object does not provide :meth:`__dir__`, the function tries its best to gather information from the object's :attr:`~object.__dict__` attribute, if defined, and - from its type object. The resulting list is not necessarily complete, and may + from its type object. The resulting list is not necessarily complete and may be inaccurate when the object has a custom :func:`__getattr__`. The default :func:`dir` mechanism behaves differently with different types of @@ -466,7 +463,7 @@ are always available. They are listed here in alphabetical order. .. function:: divmod(a, b) - Take two (non complex) numbers as arguments and return a pair of numbers + Take two (non-complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as ``(a // b, a % b)``. For floating point @@ -528,13 +525,13 @@ are always available. They are listed here in alphabetical order. 2 This function can also be used to execute arbitrary code objects (such as - those created by :func:`compile`). In this case pass a code object instead + those created by :func:`compile`). In this case, pass a code object instead of a string. If the code object has been compiled with ``'exec'`` as the *mode* argument, :func:`eval`\'s return value will be ``None``. Hints: dynamic execution of statements is supported by the :func:`exec` function. The :func:`globals` and :func:`locals` functions - returns the current global and local dictionary, respectively, which may be + return the current global and local dictionary, respectively, which may be useful to pass around for use by :func:`eval` or :func:`exec`. If the given source is a string, then leading and trailing spaces and tabs @@ -557,7 +554,7 @@ are always available. They are listed here in alphabetical order. a suite of Python statements which is then executed (unless a syntax error occurs). [#]_ If it is a code object, it is simply executed. In all cases, the code that's executed is expected to be valid as file input (see the - section "File input" in the Reference Manual). Be aware that the + section :ref:`file-input` in the Reference Manual). Be aware that the :keyword:`nonlocal`, :keyword:`yield`, and :keyword:`return` statements may not be used outside of function definitions even within the context of code passed to the @@ -569,7 +566,7 @@ are always available. They are listed here in alphabetical order. will be used for both the global and the local variables. If *globals* and *locals* are given, they are used for the global and local variables, respectively. If provided, *locals* can be any mapping object. Remember - that at module level, globals and locals are the same dictionary. If exec + that at the module level, globals and locals are the same dictionary. If exec gets two separate objects as *globals* and *locals*, the code will be executed as if it were embedded in a class definition. @@ -627,7 +624,7 @@ are always available. They are listed here in alphabetical order. preceded by a sign, and optionally embedded in whitespace. The optional sign may be ``'+'`` or ``'-'``; a ``'+'`` sign has no effect on the value produced. The argument may also be a string representing a NaN - (not-a-number), or a positive or negative infinity. More precisely, the + (not-a-number), or positive or negative infinity. More precisely, the input must conform to the following grammar after leading and trailing whitespace characters are removed: @@ -640,7 +637,7 @@ are always available. They are listed here in alphabetical order. Here ``floatnumber`` is the form of a Python floating-point literal, described in :ref:`floating`. Case is not significant, so, for example, - "inf", "Inf", "INFINITY" and "iNfINity" are all acceptable spellings for + "inf", "Inf", "INFINITY", and "iNfINity" are all acceptable spellings for positive infinity. Otherwise, if the argument is an integer or a floating point number, a @@ -687,7 +684,7 @@ are always available. They are listed here in alphabetical order. Convert a *value* to a "formatted" representation, as controlled by *format_spec*. The interpretation of *format_spec* will depend on the type - of the *value* argument, however there is a standard formatting syntax that + of the *value* argument; however, there is a standard formatting syntax that is used by most built-in types: :ref:`formatspec`. The default *format_spec* is an empty string which usually gives the same @@ -736,9 +733,9 @@ are always available. They are listed here in alphabetical order. .. function:: globals() - Return a dictionary representing the current global symbol table. This is always - the dictionary of the current module (inside a function or method, this is the - module where it is defined, not the module from which it is called). + Return the dictionary implementing the current module namespace. For code within + functions, this is set when the function is defined and remains the same + regardless of where the function is called. .. function:: hasattr(object, name) @@ -771,7 +768,7 @@ are always available. They are listed here in alphabetical order. topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated. - Note that if a slash(/) appears in the parameter list of a function, when + Note that if a slash(/) appears in the parameter list of a function when invoking :func:`help`, it means that the parameters prior to the slash are positional-only. For more info, see :ref:`the FAQ entry on positional-only parameters `. @@ -849,8 +846,8 @@ are always available. They are listed here in alphabetical order. .. audit-event:: builtins.input/result result input - Raises an auditing event ``builtins.input/result`` with the result after - successfully reading input. + Raises an :ref:`auditing event ` ``builtins.input/result`` + with the result after successfully reading input. .. class:: int([x]) @@ -898,7 +895,7 @@ are always available. They are listed here in alphabetical order. .. function:: isinstance(object, classinfo) Return ``True`` if the *object* argument is an instance of the *classinfo* - argument, or of a (direct, indirect or :term:`virtual `) subclass thereof. If *object* is not an object of the given type, the function always returns ``False``. If *classinfo* is a tuple of type objects (or recursively, other such @@ -913,12 +910,13 @@ are always available. They are listed here in alphabetical order. .. function:: issubclass(class, classinfo) - Return ``True`` if *class* is a subclass (direct, indirect or :term:`virtual + Return ``True`` if *class* is a subclass (direct, indirect, or :term:`virtual `) of *classinfo*. A class is considered a subclass of itself. *classinfo* may be a tuple of class - objects or a :ref:`types-union`, in which case every entry in *classinfo* - will be checked. In any other - case, a :exc:`TypeError` exception is raised. + objects (or recursively, other such tuples) + or a :ref:`types-union`, in which case return ``True`` if *class* is a + subclass of any entry in *classinfo*. In any other case, a :exc:`TypeError` + exception is raised. .. versionchanged:: 3.10 *classinfo* can be a :ref:`types-union`. @@ -929,8 +927,8 @@ are always available. They are listed here in alphabetical order. Return an :term:`iterator` object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, *object* must be a collection object which supports the - iteration protocol (the :meth:`__iter__` method), or it must support the - sequence protocol (the :meth:`__getitem__` method with integer arguments + :term:`iterable` protocol (the :meth:`__iter__` method), or it must support + the sequence protocol (the :meth:`__getitem__` method with integer arguments starting at ``0``). If it does not support either of those protocols, :exc:`TypeError` is raised. If the second argument, *sentinel*, is given, then *object* must be a callable object. The iterator created in this case @@ -1022,7 +1020,7 @@ are always available. They are listed here in alphabetical order. .. _func-memoryview: -.. class:: memoryview(obj) +.. class:: memoryview(object) :noindex: Return a "memory view" object created from the given argument. See @@ -1060,7 +1058,7 @@ are always available. They are listed here in alphabetical order. .. function:: next(iterator[, default]) - Retrieve the next item from the *iterator* by calling its + Retrieve the next item from the :term:`iterator` by calling its :meth:`~iterator.__next__` method. If *default* is given, it is returned if the iterator is exhausted, otherwise :exc:`StopIteration` is raised. @@ -1068,7 +1066,7 @@ are always available. They are listed here in alphabetical order. .. class:: object() Return a new featureless object. :class:`object` is a base for all classes. - It has the methods that are common to all instances of Python classes. This + It has methods that are common to all instances of Python classes. This function does not accept any arguments. .. note:: @@ -1089,7 +1087,7 @@ are always available. They are listed here in alphabetical order. >>> oct(-56) '-0o70' - If you want to convert an integer number to octal string either with prefix + If you want to convert an integer number to an octal string either with the prefix "0o" or not, you can use either of the following ways. >>> '%#o' % 10, '%o' % 10 @@ -1113,16 +1111,16 @@ are always available. They are listed here in alphabetical order. *file* is a :term:`path-like object` giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is - given, it is closed when the returned I/O object is closed, unless *closefd* + given, it is closed when the returned I/O object is closed unless *closefd* is set to ``False``.) *mode* is an optional string that specifies the mode in which the file is opened. It defaults to ``'r'`` which means open for reading in text mode. Other common values are ``'w'`` for writing (truncating the file if it - already exists), ``'x'`` for exclusive creation and ``'a'`` for appending + already exists), ``'x'`` for exclusive creation, and ``'a'`` for appending (which on *some* Unix systems, means that *all* writes append to the end of the file regardless of the current seek position). In text mode, if - *encoding* is not specified the encoding used is platform dependent: + *encoding* is not specified the encoding used is platform-dependent: ``locale.getpreferredencoding(False)`` is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leave *encoding* unspecified.) The available modes are: @@ -1138,13 +1136,13 @@ are always available. They are listed here in alphabetical order. ``'r'`` open for reading (default) ``'w'`` open for writing, truncating the file first ``'x'`` open for exclusive creation, failing if the file already exists - ``'a'`` open for writing, appending to the end of the file if it exists + ``'a'`` open for writing, appending to the end of file if it exists ``'b'`` binary mode ``'t'`` text mode (default) ``'+'`` open for updating (reading and writing) ========= =============================================================== - The default mode is ``'r'`` (open for reading text, synonym of ``'rt'``). + The default mode is ``'r'`` (open for reading text, a synonym of ``'rt'``). Modes ``'w+'`` and ``'w+b'`` open and truncate the file. Modes ``'r+'`` and ``'r+b'`` open the file with no truncation. @@ -1158,7 +1156,7 @@ are always available. They are listed here in alphabetical order. There is an additional mode character permitted, ``'U'``, which no longer has any effect, and is considered deprecated. It previously enabled - :term:`universal newlines` in text mode, which became the default behaviour + :term:`universal newlines` in text mode, which became the default behavior in Python 3.0. Refer to the documentation of the :ref:`newline ` parameter for further details. @@ -1171,7 +1169,11 @@ are always available. They are listed here in alphabetical order. *buffering* is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size - in bytes of a fixed-size chunk buffer. When no *buffering* argument is + in bytes of a fixed-size chunk buffer. Note that specifying a buffer size this + way applies for binary buffered I/O, but ``TextIOWrapper`` (i.e., files opened + with ``mode='r+'``) would have another buffering. To disable buffering in + ``TextIOWrapper``, consider using the ``write_through`` flag for + :func:`io.TextIOWrapper.reconfigure`. When no *buffering* argument is given, the default buffering policy works as follows: * Binary files are buffered in fixed-size chunks; the size of the buffer is @@ -1208,9 +1210,9 @@ are always available. They are listed here in alphabetical order. * ``'replace'`` causes a replacement marker (such as ``'?'``) to be inserted where there is malformed data. - * ``'surrogateescape'`` will represent any incorrect bytes as code - points in the Unicode Private Use Area ranging from U+DC80 to - U+DCFF. These private code points will then be turned back into + * ``'surrogateescape'`` will represent any incorrect bytes as low + surrogate code units ranging from U+DC80 to U+DCFF. + These surrogate code units will then be turned back into the same bytes when the ``surrogateescape`` error handler is used when writing data. This is useful for processing files in an unknown encoding. @@ -1250,8 +1252,8 @@ are always available. They are listed here in alphabetical order. If *closefd* is ``False`` and a file descriptor rather than a filename was given, the underlying file descriptor will be kept open when the file is - closed. If a filename is given *closefd* must be ``True`` (the default) - otherwise an error will be raised. + closed. If a filename is given *closefd* must be ``True`` (the default); + otherwise, an error will be raised. A custom opener can be used by passing a callable as *opener*. The underlying file descriptor for the file object is then obtained by calling *opener* with @@ -1295,7 +1297,7 @@ are always available. They are listed here in alphabetical order. single: text mode module: sys - See also the file handling modules, such as, :mod:`fileinput`, :mod:`io` + See also the file handling modules, such as :mod:`fileinput`, :mod:`io` (where :func:`open` is declared), :mod:`os`, :mod:`os.path`, :mod:`tempfile`, and :mod:`shutil`. @@ -1356,8 +1358,11 @@ are always available. They are listed here in alphabetical order. coercion rules for binary arithmetic operators apply. For :class:`int` operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are - converted to float and a float result is delivered. For example, ``10**2`` - returns ``100``, but ``10**-2`` returns ``0.01``. + converted to float and a float result is delivered. For example, ``pow(10, 2)`` + returns ``100``, but ``pow(10, -2)`` returns ``0.01``. For a negative base of + type :class:`int` or :class:`float` and a non-integral exponent, a complex + result is delivered. For example, ``pow(-9, 0.5)`` returns a value close + to ``3j``. For :class:`int` operands *base* and *exp*, if *mod* is present, *mod* must also be of integer type and *mod* must be nonzero. If *mod* is present and @@ -1385,7 +1390,7 @@ are always available. They are listed here in alphabetical order. .. function:: print(*objects, sep=' ', end='\\n', file=sys.stdout, flush=False) Print *objects* to the text stream *file*, separated by *sep* and followed - by *end*. *sep*, *end*, *file* and *flush*, if present, must be given as keyword + by *end*. *sep*, *end*, *file*, and *flush*, if present, must be given as keyword arguments. All non-keyword arguments are converted to strings like :func:`str` does and @@ -1399,7 +1404,7 @@ are always available. They are listed here in alphabetical order. arguments are converted to text strings, :func:`print` cannot be used with binary mode file objects. For these, use ``file.write(...)`` instead. - Whether output is buffered is usually determined by *file*, but if the + Whether the output is buffered is usually determined by *file*, but if the *flush* keyword argument is true, the stream is forcibly flushed. .. versionchanged:: 3.3 @@ -1432,7 +1437,7 @@ are always available. They are listed here in alphabetical order. x = property(getx, setx, delx, "I'm the 'x' property.") If *c* is an instance of *C*, ``c.x`` will invoke the getter, - ``c.x = value`` will invoke the setter and ``del c.x`` the deleter. + ``c.x = value`` will invoke the setter, and ``del c.x`` the deleter. If given, *doc* will be the docstring of the property attribute. Otherwise, the property will copy *fget*'s docstring (if it exists). This makes it possible to @@ -1497,7 +1502,7 @@ are always available. They are listed here in alphabetical order. Return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an - object with the same value when passed to :func:`eval`, otherwise the + object with the same value when passed to :func:`eval`; otherwise, the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this @@ -1525,7 +1530,7 @@ are always available. They are listed here in alphabetical order. ``2``). Any integer value is valid for *ndigits* (positive, zero, or negative). The return value is an integer if *ndigits* is omitted or ``None``. - Otherwise the return value has the same type as *number*. + Otherwise, the return value has the same type as *number*. For a general Python object ``number``, ``round`` delegates to ``number.__round__``. @@ -1555,7 +1560,7 @@ are always available. They are listed here in alphabetical order. .. function:: setattr(object, name, value) This is the counterpart of :func:`getattr`. The arguments are an object, a - string and an arbitrary value. The string may name an existing attribute or a + string, and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, ``setattr(x, 'foobar', 123)`` is equivalent to ``x.foobar = 123``. @@ -1574,15 +1579,15 @@ are always available. They are listed here in alphabetical order. Return a :term:`slice` object representing the set of indices specified by ``range(start, stop, step)``. The *start* and *step* arguments default to ``None``. Slice objects have read-only data attributes :attr:`~slice.start`, - :attr:`~slice.stop` and :attr:`~slice.step` which merely return the argument + :attr:`~slice.stop`, and :attr:`~slice.step` which merely return the argument values (or their default). They have no other explicit functionality; - however they are used by NumPy and other third party packages. + however, they are used by NumPy and other third-party packages. Slice objects are also generated when extended indexing syntax is used. For example: ``a[start:stop:step]`` or ``a[start:stop, i]``. See :func:`itertools.islice` for an alternate version that returns an iterator. -.. function:: sorted(iterable, *, key=None, reverse=False) +.. function:: sorted(iterable, /, *, key=None, reverse=False) Return a new sorted list from the items in *iterable*. @@ -1603,6 +1608,15 @@ are always available. They are listed here in alphabetical order. compare equal --- this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). + The sort algorithm uses only ``<`` comparisons between items. While + defining an :meth:`~object.__lt__` method will suffice for sorting, + :PEP:`8` recommends that all six :ref:`rich comparisons + ` be implemented. This will help avoid bugs when using + the same data with other ordering tools such as :func:`max` that rely + on a different underlying method. Implementing all six comparisons + also helps avoid confusion for mixed type comparisons which can call + reflected the :meth:`~object.__gt__` method. + For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`. .. decorator:: staticmethod @@ -1623,7 +1637,7 @@ are always available. They are listed here in alphabetical order. an instance (such as ``C().f()``). Moreover, they can be called as regular functions (such as ``f()``). - Static methods in Python are similar to those found in Java or C++. Also see + Static methods in Python are similar to those found in Java or C++. Also, see :func:`classmethod` for a variant that is useful for creating alternate class constructors. @@ -1923,7 +1937,7 @@ are always available. They are listed here in alphabetical order. and *locals* to determine how to interpret the name in a package context. The *fromlist* gives the names of objects or submodules that should be imported from the module given by *name*. The standard implementation does - not use its *locals* argument at all, and uses its *globals* only to + not use its *locals* argument at all and uses its *globals* only to determine the package context of the :keyword:`import` statement. *level* specifies whether to use absolute or relative imports. ``0`` (the diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index e981bcdf6f2573..c78818bfab1a51 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -154,15 +154,22 @@ The :mod:`functools` module defines the following functions: @lru_cache def count_vowels(sentence): - sentence = sentence.casefold() - return sum(sentence.count(vowel) for vowel in 'aeiou') + return sum(sentence.count(vowel) for vowel in 'AEIOUaeiou') If *maxsize* is set to ``None``, the LRU feature is disabled and the cache can grow without bound. If *typed* is set to true, function arguments of different types will be - cached separately. For example, ``f(3)`` and ``f(3.0)`` will be treated - as distinct calls with distinct results. + cached separately. If *typed* is false, the implementation will usually + regard them as equivalent calls and only cache a single result. (Some + types such as *str* and *int* may be cached separately even when *typed* + is false.) + + Note, type specificity applies only to the function's immediate arguments + rather than their contents. The scalar arguments, ``Decimal(42)`` and + ``Fraction(42)`` are be treated as distinct calls with distinct results. + In contrast, the tuple arguments ``('answer', Decimal(42))`` and + ``('answer', Fraction(42))`` are treated as equivalent. The wrapped function is instrumented with a :func:`cache_parameters` function that returns a new :class:`dict` showing the values for *maxsize* @@ -172,8 +179,7 @@ The :mod:`functools` module defines the following functions: To help measure the effectiveness of the cache and tune the *maxsize* parameter, the wrapped function is instrumented with a :func:`cache_info` function that returns a :term:`named tuple` showing *hits*, *misses*, - *maxsize* and *currsize*. In a multi-threaded environment, the hits - and misses are approximate. + *maxsize* and *currsize*. The decorator also provides a :func:`cache_clear` function for clearing or invalidating the cache. @@ -182,6 +188,9 @@ The :mod:`functools` module defines the following functions: :attr:`__wrapped__` attribute. This is useful for introspection, for bypassing the cache, or for rewrapping the function with a different cache. + The cache keeps references to the arguments and return values until they age + out of the cache or until the cache is cleared. + An `LRU (least recently used) cache `_ works best when the most recent calls are the best predictors of upcoming @@ -199,7 +208,7 @@ The :mod:`functools` module defines the following functions: @lru_cache(maxsize=32) def get_pep(num): 'Retrieve text of a Python Enhancement Proposal' - resource = 'http://www.python.org/dev/peps/pep-%04d/' % num + resource = 'https://www.python.org/dev/peps/pep-%04d/' % num try: with urllib.request.urlopen(resource) as s: return s.read() @@ -399,8 +408,8 @@ The :mod:`functools` module defines the following functions: dispatch>` :term:`generic function`. To define a generic function, decorate it with the ``@singledispatch`` - decorator. Note that the dispatch happens on the type of the first argument, - create your function accordingly:: + decorator. When defining a function using ``@singledispatch``, note that the + dispatch happens on the type of the first argument:: >>> from functools import singledispatch >>> @singledispatch @@ -410,9 +419,9 @@ The :mod:`functools` module defines the following functions: ... print(arg) To add overloaded implementations to the function, use the :func:`register` - attribute of the generic function. It is a decorator. For functions - annotated with types, the decorator will infer the type of the first - argument automatically:: + attribute of the generic function, which can be used as a decorator. For + functions annotated with types, the decorator will infer the type of the + first argument automatically:: >>> @fun.register ... def _(arg: int, verbose=False): @@ -438,17 +447,17 @@ The :mod:`functools` module defines the following functions: ... - To enable registering lambdas and pre-existing functions, the - :func:`register` attribute can be used in a functional form:: + To enable registering :term:`lambdas` and pre-existing functions, + the :func:`register` attribute can also be used in a functional form:: >>> def nothing(arg, verbose=False): ... print("Nothing.") ... >>> fun.register(type(None), nothing) - The :func:`register` attribute returns the undecorated function which - enables decorator stacking, pickling, as well as creating unit tests for - each variant independently:: + The :func:`register` attribute returns the undecorated function. This + enables decorator stacking, :mod:`pickling`, and the creation + of unit tests for each variant independently:: >>> @fun.register(float) ... @fun.register(Decimal) @@ -483,11 +492,12 @@ The :mod:`functools` module defines the following functions: Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with ``@singledispatch`` is registered - for the base ``object`` type, which means it is used if no better + for the base :class:`object` type, which means it is used if no better implementation is found. - If an implementation registered to :term:`abstract base class`, virtual - subclasses will be dispatched to that implementation:: + If an implementation is registered to an :term:`abstract base class`, + virtual subclasses of the base class will be dispatched to that + implementation:: >>> from collections.abc import Mapping >>> @fun.register @@ -500,7 +510,7 @@ The :mod:`functools` module defines the following functions: >>> fun({"a": "b"}) a => b - To check which implementation will the generic function choose for + To check which implementation the generic function will choose for a given type, use the ``dispatch()`` attribute:: >>> fun.dispatch(float) @@ -523,7 +533,7 @@ The :mod:`functools` module defines the following functions: .. versionadded:: 3.4 .. versionchanged:: 3.7 - The :func:`register` attribute supports using type annotations. + The :func:`register` attribute now supports using type annotations. .. class:: singledispatchmethod(func) @@ -532,8 +542,9 @@ The :mod:`functools` module defines the following functions: dispatch>` :term:`generic function`. To define a generic method, decorate it with the ``@singledispatchmethod`` - decorator. Note that the dispatch happens on the type of the first non-self - or non-cls argument, create your function accordingly:: + decorator. When defining a function using ``@singledispatchmethod``, note + that the dispatch happens on the type of the first non-*self* or non-*cls* + argument:: class Negator: @singledispatchmethod @@ -549,9 +560,10 @@ The :mod:`functools` module defines the following functions: return not arg ``@singledispatchmethod`` supports nesting with other decorators such as - ``@classmethod``. Note that to allow for ``dispatcher.register``, - ``singledispatchmethod`` must be the *outer most* decorator. Here is the - ``Negator`` class with the ``neg`` methods being class bound:: + :func:`@classmethod`. Note that to allow for + ``dispatcher.register``, ``singledispatchmethod`` must be the *outer most* + decorator. Here is the ``Negator`` class with the ``neg`` methods bound to + the class, rather than an instance of the class:: class Negator: @singledispatchmethod @@ -569,8 +581,9 @@ The :mod:`functools` module defines the following functions: def _(cls, arg: bool): return not arg - The same pattern can be used for other similar decorators: ``staticmethod``, - ``abstractmethod``, and others. + The same pattern can be used for other similar decorators: + :func:`@staticmethod`, + :func:`@abstractmethod`, and others. .. versionadded:: 3.8 diff --git a/Doc/library/glob.rst b/Doc/library/glob.rst index 215f60d328c76a..4a290473d89242 100644 --- a/Doc/library/glob.rst +++ b/Doc/library/glob.rst @@ -23,8 +23,11 @@ according to the rules used by the Unix shell, although results are returned in arbitrary order. No tilde expansion is done, but ``*``, ``?``, and character ranges expressed with ``[]`` will be correctly matched. This is done by using the :func:`os.scandir` and :func:`fnmatch.fnmatch` functions in concert, and -not by actually invoking a subshell. Note that unlike :func:`fnmatch.fnmatch`, -:mod:`glob` treats filenames beginning with a dot (``.``) as special cases. +not by actually invoking a subshell. + +Note that files beginning with a dot (``.``) can only be matched by +patterns that also start with a dot, +unlike :func:`fnmatch.fnmatch` or :func:`pathlib.Path.glob`. (For tilde and shell variable expansion, use :func:`os.path.expanduser` and :func:`os.path.expandvars`.) diff --git a/Doc/library/graphlib.rst b/Doc/library/graphlib.rst index 0faca2186b268c..2bc80da4ead2a2 100644 --- a/Doc/library/graphlib.rst +++ b/Doc/library/graphlib.rst @@ -154,9 +154,10 @@ .. method:: static_order() - Returns an iterable of nodes in a topological order. Using this method - does not require to call :meth:`TopologicalSorter.prepare` or - :meth:`TopologicalSorter.done`. This method is equivalent to:: + Returns an iterator object which will iterate over nodes in a topological + order. When using this method, :meth:`~TopologicalSorter.prepare` and + :meth:`~TopologicalSorter.done` should not be called. This method is + equivalent to:: def static_order(self): self.prepare() @@ -206,4 +207,4 @@ The :mod:`graphlib` module defines the following exception classes: The detected cycle can be accessed via the second element in the :attr:`~CycleError.args` attribute of the exception instance and consists in a list of nodes, such that each node is, in the graph, an immediate predecessor of the next node in the list. In the reported list, - the first and the last node will be the same, to make it clear that it is cyclic. \ No newline at end of file + the first and the last node will be the same, to make it clear that it is cyclic. diff --git a/Doc/library/grp.rst b/Doc/library/grp.rst index 74de3f952005fd..fbfb922d3e0528 100644 --- a/Doc/library/grp.rst +++ b/Doc/library/grp.rst @@ -12,7 +12,7 @@ Unix versions. Group database entries are reported as a tuple-like object, whose attributes correspond to the members of the ``group`` structure (Attribute field below, see -````): +````): +-------+-----------+---------------------------------+ | Index | Attribute | Meaning | diff --git a/Doc/library/hashlib-blake2-tree.png b/Doc/library/hashlib-blake2-tree.png index 73e849444ed7d3..faef21b55f96b1 100644 Binary files a/Doc/library/hashlib-blake2-tree.png and b/Doc/library/hashlib-blake2-tree.png differ diff --git a/Doc/library/hashlib.rst b/Doc/library/hashlib.rst index d77a2e5912d5e6..aa24131f8bf444 100644 --- a/Doc/library/hashlib.rst +++ b/Doc/library/hashlib.rst @@ -23,7 +23,7 @@ This module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA's MD5 -algorithm (defined in Internet :rfc:`1321`). The terms "secure hash" and +algorithm (defined in internet :rfc:`1321`). The terms "secure hash" and "message digest" are interchangeable. Older algorithms were called message digests. The modern term is secure hash. @@ -80,6 +80,8 @@ library that Python uses on your platform. On most platforms the .. versionadded:: 3.6 :func:`blake2b` and :func:`blake2s` were added. +.. _hashlib-usedforsecurity: + .. versionchanged:: 3.9 All hashlib constructors take a keyword-only argument *usedforsecurity* with default value ``True``. A false value allows the use of insecure and @@ -118,10 +120,10 @@ More condensed: Using :func:`new` with an algorithm provided by OpenSSL: - >>> h = hashlib.new('sha512_256') + >>> h = hashlib.new('sha256') >>> h.update(b"Nobody inspects the spammish repetition") >>> h.hexdigest() - '19197dc4d03829df858011c6c87600f994a858103bbc19005f20987aa19a97e2' + '031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406' Hashlib provides the following constant attributes: @@ -247,16 +249,19 @@ include a `salt `_. a proper source, e.g. :func:`os.urandom`. The number of *iterations* should be chosen based on the hash algorithm and - computing power. As of 2013, at least 100,000 iterations of SHA-256 are - suggested. + computing power. As of 2022, hundreds of thousands of iterations of SHA-256 + are suggested. For rationale as to why and how to choose what is best for + your application, read *Appendix A.2.2* of NIST-SP-800-132_. The answers + on the `stackexchange pbkdf2 iterations question`_ explain in detail. *dklen* is the length of the derived key. If *dklen* is ``None`` then the digest size of the hash algorithm *hash_name* is used, e.g. 64 for SHA-512. - >>> import hashlib - >>> dk = hashlib.pbkdf2_hmac('sha256', b'password', b'salt', 100000) + >>> from hashlib import pbkdf2_hmac + >>> our_app_iters = 500_000 # Application specific, read above. + >>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters) >>> dk.hex() - '0394a2ede332c9a13eb82e9b24631604c31df978b4e2f0fbd2c549944f9d79a5' + '15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9' .. versionadded:: 3.4 @@ -374,10 +379,10 @@ Constructor functions also accept the following tree hashing parameters: * *depth*: maximal depth of tree (1 to 255, 255 if unlimited, 1 in sequential mode). -* *leaf_size*: maximal byte length of leaf (0 to 2**32-1, 0 if unlimited or in +* *leaf_size*: maximal byte length of leaf (0 to ``2**32-1``, 0 if unlimited or in sequential mode). -* *node_offset*: node offset (0 to 2**64-1 for BLAKE2b, 0 to 2**48-1 for +* *node_offset*: node offset (0 to ``2**64-1`` for BLAKE2b, 0 to ``2**48-1`` for BLAKE2s, 0 for the first, leftmost, leaf, or in sequential mode). * *node_depth*: node depth (0 to 255, 0 for leaves, or in sequential mode). @@ -498,7 +503,7 @@ Keyed hashing Keyed hashing can be used for authentication as a faster and simpler replacement for `Hash-based message authentication code -`_ (HMAC). +`_ (HMAC). BLAKE2 can be securely used in prefix-MAC mode thanks to the indifferentiability property inherited from BLAKE. @@ -729,7 +734,8 @@ Domain Dedication 1.0 Universal: .. _SHA-3: https://en.wikipedia.org/wiki/NIST_hash_function_competition .. _ChaCha: https://cr.yp.to/chacha.html .. _pyblake2: https://pythonhosted.org/pyblake2/ - +.. _NIST-SP-800-132: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf +.. _stackexchange pbkdf2 iterations question: https://security.stackexchange.com/questions/3959/recommended-of-iterations-when-using-pbkdf2-sha256/ .. seealso:: @@ -752,3 +758,6 @@ Domain Dedication 1.0 Universal: https://www.ietf.org/rfc/rfc8018.txt PKCS #5: Password-Based Cryptography Specification Version 2.1 + + https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf + NIST Recommendation for Password-Based Key Derivation. diff --git a/Doc/library/html.entities.rst b/Doc/library/html.entities.rst index 067e1b1e5adb3e..7d836fe7380245 100644 --- a/Doc/library/html.entities.rst +++ b/Doc/library/html.entities.rst @@ -44,4 +44,4 @@ This module defines four dictionaries, :data:`html5`, .. rubric:: Footnotes -.. [#] See https://www.w3.org/TR/html5/syntax.html#named-character-references +.. [#] See https://html.spec.whatwg.org/multipage/syntax.html#named-character-references diff --git a/Doc/library/html.parser.rst b/Doc/library/html.parser.rst index ac844a683bf7ac..03aff25ce6117a 100644 --- a/Doc/library/html.parser.rst +++ b/Doc/library/html.parser.rst @@ -126,7 +126,7 @@ implementations do nothing (except for :meth:`~HTMLParser.handle_startendtag`): .. method:: HTMLParser.handle_starttag(tag, attrs) - This method is called to handle the start of a tag (e.g. ``
``). + This method is called to handle the start tag of an element (e.g. ``
``). The *tag* argument is the name of the tag converted to lower case. The *attrs* argument is a list of ``(name, value)`` pairs containing the attributes found diff --git a/Doc/library/http.cookiejar.rst b/Doc/library/http.cookiejar.rst index 7b1aa80a01797c..3d59665be4f254 100644 --- a/Doc/library/http.cookiejar.rst +++ b/Doc/library/http.cookiejar.rst @@ -20,7 +20,7 @@ Both the regular Netscape cookie protocol and the protocol defined by :rfc:`2965` are handled. RFC 2965 handling is switched off by default. :rfc:`2109` cookies are parsed as Netscape cookies and subsequently treated either as Netscape or RFC 2965 cookies according to the 'policy' in effect. -Note that the great majority of cookies on the Internet are Netscape cookies. +Note that the great majority of cookies on the internet are Netscape cookies. :mod:`http.cookiejar` attempts to follow the de-facto Netscape cookie protocol (which differs substantially from that set out in the original Netscape specification), including taking note of the ``max-age`` and ``port`` cookie-attributes @@ -122,7 +122,7 @@ The following classes are provided: :mod:`http.cookiejar` and :mod:`http.cookies` modules do not depend on each other. - https://curl.haxx.se/rfc/cookie_spec.html + https://curl.se/rfc/cookie_spec.html The specification of the original Netscape cookie protocol. Though this is still the dominant protocol, the 'Netscape cookie protocol' implemented by all the major browsers (and :mod:`http.cookiejar`) only bears a passing resemblance to diff --git a/Doc/library/http.rst b/Doc/library/http.rst index 1569d504c7f92a..2466c84476c6c1 100644 --- a/Doc/library/http.rst +++ b/Doc/library/http.rst @@ -35,7 +35,7 @@ associated messages through the :class:`http.HTTPStatus` enum: >>> from http import HTTPStatus >>> HTTPStatus.OK - HTTPStatus.OK + >>> HTTPStatus.OK == 200 True >>> HTTPStatus.OK.value @@ -45,7 +45,7 @@ associated messages through the :class:`http.HTTPStatus` enum: >>> HTTPStatus.OK.description 'Request fulfilled, document follows' >>> list(HTTPStatus) - [HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...] + [, , ...] .. _http-status-codes: diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index 729d7e37137a81..0de02834401aa8 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -14,7 +14,7 @@ -------------- -This module defines classes for implementing HTTP servers (Web servers). +This module defines classes for implementing HTTP servers. .. warning:: @@ -197,7 +197,7 @@ provides three different variants: request header it responds back with a ``100 Continue`` followed by ``200 OK`` headers. This method can be overridden to raise an error if the server does not - want the client to continue. For e.g. server can chose to send ``417 + want the client to continue. For e.g. server can choose to send ``417 Expectation Failed`` as a response header and ``return False``. .. versionadded:: 3.2 @@ -320,9 +320,16 @@ provides three different variants: .. class:: SimpleHTTPRequestHandler(request, client_address, server, directory=None) - This class serves files from the current directory and below, directly + This class serves files from the directory *directory* and below, + or the current directory if *directory* is not provided, directly mapping the directory structure to HTTP requests. + .. versionadded:: 3.7 + The *directory* parameter. + + .. versionchanged:: 3.9 + The *directory* parameter accepts a :term:`path-like object`. + A lot of the work, such as parsing the request, is done by the base class :class:`BaseHTTPRequestHandler`. This class implements the :func:`do_GET` and :func:`do_HEAD` functions. @@ -345,13 +352,6 @@ provides three different variants: This dictionary is no longer filled with the default system mappings, but only contains overrides. - .. attribute:: directory - - If not specified, the directory to serve is the current working directory. - - .. versionchanged:: 3.9 - Accepts a :term:`path-like object`. - The :class:`SimpleHTTPRequestHandler` class defines the following methods: .. method:: do_HEAD() @@ -412,17 +412,22 @@ the current directory:: .. _http-server-cli: :mod:`http.server` can also be invoked directly using the :option:`-m` -switch of the interpreter with a ``port number`` argument. Similar to +switch of the interpreter. Similar to the previous example, this serves files relative to the current directory:: - python -m http.server 8000 + python -m http.server + +The server listens to port 8000 by default. The default can be overridden +by passing the desired port number as an argument:: + + python -m http.server 9000 -By default, server binds itself to all interfaces. The option ``-b/--bind`` +By default, the server binds itself to all interfaces. The option ``-b/--bind`` specifies a specific address to which it should bind. Both IPv4 and IPv6 addresses are supported. For example, the following command causes the server to bind to localhost only:: - python -m http.server 8000 --bind 127.0.0.1 + python -m http.server --bind 127.0.0.1 .. versionadded:: 3.4 ``--bind`` argument was introduced. @@ -430,14 +435,14 @@ to bind to localhost only:: .. versionadded:: 3.8 ``--bind`` argument enhanced to support IPv6 -By default, server uses the current directory. The option ``-d/--directory`` +By default, the server uses the current directory. The option ``-d/--directory`` specifies a directory to which it should serve the files. For example, the following command uses a specific directory:: python -m http.server --directory /tmp/ .. versionadded:: 3.7 - ``--directory`` specify alternate directory + ``--directory`` argument was introduced. .. class:: CGIHTTPRequestHandler(request, client_address, server) @@ -482,4 +487,4 @@ the following command uses a specific directory:: :class:`CGIHTTPRequestHandler` can be enabled in the command line by passing the ``--cgi`` option:: - python -m http.server --cgi 8000 + python -m http.server --cgi diff --git a/Doc/library/idle.rst b/Doc/library/idle.rst index 3c302115b5f408..d6021042c61166 100644 --- a/Doc/library/idle.rst +++ b/Doc/library/idle.rst @@ -96,11 +96,13 @@ Save Copy As... Print Window Print the current window to the default printer. -Close - Close the current window (ask to save if unsaved). +Close Window + Close the current window (if an unsaved editor, ask to save; if an unsaved + Shell, ask to quit execution). Calling ``exit()`` or ``close()`` in the Shell + window also closes Shell. If this is the only window, also exit IDLE. -Exit - Close all windows and quit IDLE (ask to save unsaved windows). +Exit IDLE + Close all windows and quit IDLE (ask to save unsaved edit windows). Edit menu (Shell and Editor) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -518,7 +520,7 @@ and not restarting the Shell thereafter. This is especially useful after adding imports at the top of a file. This also increases possible attribute completions. -Completion boxes intially exclude names beginning with '_' or, for +Completion boxes initially exclude names beginning with '_' or, for modules, not included in '__all__'. The hidden names can be accessed by typing '_' after '.', either before or after the box is opened. @@ -613,6 +615,12 @@ keywords, builtin class and function names, names following ``class`` and ``def``, strings, and comments. For any text window, these are the cursor (when present), found text (when possible), and selected text. +IDLE also highlights the :ref:`soft keywords ` :keyword:`match`, +:keyword:`case `, and :keyword:`_ ` in +pattern-matching statements. However, this highlighting is not perfect and +will be incorrect in some rare cases, including some ``_``-s in ``case`` +patterns. + Text coloring is done in the background, so uncolorized text is occasionally visible. To change the color scheme, use the Configure IDLE dialog Highlighting tab. The marking of debugger breakpoint lines in the editor and @@ -863,7 +871,7 @@ Running without a subprocess By default, IDLE executes user code in a separate subprocess via a socket, which uses the internal loopback interface. This connection is not -externally visible and no data is sent to or received from the Internet. +externally visible and no data is sent to or received from the internet. If firewall software complains anyway, you can ignore it. If the attempt to make the socket connection fails, Idle will notify you. diff --git a/Doc/library/imghdr.rst b/Doc/library/imghdr.rst index 3d7f6de7a1bec0..c17bf897b9be91 100644 --- a/Doc/library/imghdr.rst +++ b/Doc/library/imghdr.rst @@ -3,9 +3,14 @@ .. module:: imghdr :synopsis: Determine the type of image contained in a file or byte stream. + :deprecated: **Source code:** :source:`Lib/imghdr.py` +.. deprecated:: 3.11 + The :mod:`imghdr` module is deprecated + (see :pep:`PEP 594 <594#imghdr>` for details and alternatives). + -------------- The :mod:`imghdr` module determines the type of image contained in a file or diff --git a/Doc/library/importlib.metadata.rst b/Doc/library/importlib.metadata.rst index 9bedee5af28f69..a6caa994497ba8 100644 --- a/Doc/library/importlib.metadata.rst +++ b/Doc/library/importlib.metadata.rst @@ -8,12 +8,10 @@ :synopsis: The implementation of the importlib metadata. .. versionadded:: 3.8 +.. versionchanged:: 3.10 + ``importlib.metadata`` is no longer provisional. -**Source code:** :source:`Lib/importlib/metadata.py` - -.. note:: - This functionality is provisional and may deviate from the usual - version semantics of the standard library. +**Source code:** :source:`Lib/importlib/metadata/__init__.py` ``importlib.metadata`` is a library that provides for access to installed package metadata. Built in part on Python's import system, this library @@ -257,7 +255,7 @@ function:: Package distributions --------------------- -A convience method to resolve the distribution or +A convenience method to resolve the distribution or distributions (in the case of a namespace package) for top-level Python packages or modules:: @@ -266,6 +264,7 @@ Python packages or modules:: .. versionadded:: 3.10 +.. _distributions: Distributions ============= @@ -337,6 +336,3 @@ a custom finder, return instances of this derived ``Distribution`` in the .. _`entry point API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points .. _`metadata API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api .. _`importlib_resources`: https://importlib-resources.readthedocs.io/en/latest/index.html - - -.. rubric:: Footnotes diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst index 50297e0d73e7c8..c13d80e48232ec 100644 --- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -230,8 +230,8 @@ ABC hierarchy:: object +-- Finder (deprecated) - | +-- MetaPathFinder - | +-- PathEntryFinder + +-- MetaPathFinder + +-- PathEntryFinder +-- Loader +-- ResourceLoader --------+ +-- InspectLoader | @@ -264,8 +264,7 @@ ABC hierarchy:: .. class:: MetaPathFinder - An abstract base class representing a :term:`meta path finder`. For - compatibility, this is a subclass of :class:`Finder`. + An abstract base class representing a :term:`meta path finder`. .. versionadded:: 3.3 @@ -491,7 +490,7 @@ ABC hierarchy:: .. class:: ResourceReader - *Superseded by TraversableReader* + *Superseded by TraversableResources* An :term:`abstract base class` to provide the ability to read *resources*. @@ -622,7 +621,7 @@ ABC hierarchy:: .. method:: is_package(fullname) - An abstract method to return a true value if the module is a package, a + An optional method to return a true value if the module is a package, a false value otherwise. :exc:`ImportError` is raised if the :term:`loader` cannot find the module. @@ -815,8 +814,48 @@ ABC hierarchy:: .. versionadded:: 3.9 + .. attribute:: name -.. class:: TraversableReader + Abstract. The base name of this object without any parent references. + + .. abstractmethod:: iterdir() + + Yield Traversable objects in self. + + .. abstractmethod:: is_dir() + + Return True if self is a directory. + + .. abstractmethod:: is_file() + + Return True if self is a file. + + .. abstractmethod:: joinpath(child) + + Return Traversable child in self. + + .. abstractmethod:: __truediv__(child) + + Return Traversable child in self. + + .. abstractmethod:: open(mode='r', *args, **kwargs) + + *mode* may be 'r' or 'rb' to open as text or binary. Return a handle + suitable for reading (same as :attr:`pathlib.Path.open`). + + When opening as text, accepts encoding parameters such as those + accepted by :attr:`io.TextIOWrapper`. + + .. method:: read_bytes() + + Read contents of self as bytes. + + .. method:: read_text(encoding=None) + + Read contents of self as text. + + +.. class:: TraversableResources An abstract base class for resource readers capable of serving the ``files`` interface. Subclasses ResourceReader and provides @@ -891,7 +930,7 @@ The following functions are available. .. function:: files(package) - Returns an :class:`importlib.resources.abc.Traversable` object + Returns an :class:`importlib.abc.Traversable` object representing the resource container for the package (think directory) and its resources (think files). A Traversable may contain other containers (think subdirectories). @@ -903,7 +942,7 @@ The following functions are available. .. function:: as_file(traversable) - Given a :class:`importlib.resources.abc.Traversable` object representing + Given a :class:`importlib.abc.Traversable` object representing a file, typically from :func:`importlib.resources.files`, return a context manager for use in a :keyword:`with` statement. The context manager provides a :class:`pathlib.Path` object. @@ -1710,6 +1749,9 @@ Checking if a module can be imported If you need to find out if a module can be imported without actually doing the import, then you should use :func:`importlib.util.find_spec`. + +Note that if ``name`` is a submodule (contains a dot), +:func:`importlib.util.find_spec` will import the parent module. :: import importlib.util diff --git a/Doc/library/index.rst b/Doc/library/index.rst index 1a9e52ec5ccfba..7d2002b37df12c 100644 --- a/Doc/library/index.rst +++ b/Doc/library/index.rst @@ -74,4 +74,4 @@ the `Python Package Index `_. windows.rst unix.rst superseded.rst - undoc.rst + security_warnings.rst diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index b9e8be1234e259..44553afc33b75c 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -187,8 +187,9 @@ attributes: | | co_name | name with which this code | | | | object was defined | +-----------+-------------------+---------------------------+ -| | co_names | tuple of names of local | -| | | variables | +| | co_names | tuple of names other | +| | | than arguments and | +| | | function locals | +-----------+-------------------+---------------------------+ | | co_nlocals | number of local variables | +-----------+-------------------+---------------------------+ @@ -481,6 +482,7 @@ Retrieving source code If the documentation string for an object is not provided and the object is a class, a method, a property or a descriptor, retrieve the documentation string from the inheritance hierarchy. + Return ``None`` if the documentation string is invalid or missing. .. versionchanged:: 3.5 Documentation strings are now inherited if not overridden. @@ -504,12 +506,14 @@ Retrieving source code .. function:: getmodule(object) - Try to guess which module an object was defined in. + Try to guess which module an object was defined in. Return ``None`` + if the module cannot be determined. .. function:: getsourcefile(object) - Return the name of the Python source file in which an object was defined. This + Return the name of the Python source file in which an object was defined + or ``None`` if no way can be identified to get the source. This will fail with a :exc:`TypeError` if the object is a built-in module, class, or function. @@ -834,10 +838,10 @@ function. >>> str(param.replace(default=Parameter.empty, annotation='spam')) "foo:'spam'" - .. versionchanged:: 3.4 - In Python 3.3 Parameter objects were allowed to have ``name`` set - to ``None`` if their ``kind`` was set to ``POSITIONAL_ONLY``. - This is no longer permitted. + .. versionchanged:: 3.4 + In Python 3.3 Parameter objects were allowed to have ``name`` set + to ``None`` if their ``kind`` was set to ``POSITIONAL_ONLY``. + This is no longer permitted. .. class:: BoundArguments diff --git a/Doc/library/internet.rst b/Doc/library/internet.rst index b8950bb6cb8c2d..ff58dcf4d89c36 100644 --- a/Doc/library/internet.rst +++ b/Doc/library/internet.rst @@ -11,7 +11,7 @@ Internet Protocols and Support .. index:: module: socket -The modules described in this chapter implement Internet protocols and support +The modules described in this chapter implement internet protocols and support for related technology. They are all implemented in Python. Most of these modules require the presence of the system-dependent module :mod:`socket`, which is currently supported on most popular platforms. Here is an overview: @@ -20,8 +20,6 @@ is currently supported on most popular platforms. Here is an overview: .. toctree:: webbrowser.rst - cgi.rst - cgitb.rst wsgiref.rst urllib.rst urllib.request.rst @@ -33,10 +31,7 @@ is currently supported on most popular platforms. Here is an overview: ftplib.rst poplib.rst imaplib.rst - nntplib.rst smtplib.rst - smtpd.rst - telnetlib.rst uuid.rst socketserver.rst http.server.rst diff --git a/Doc/library/intro.rst b/Doc/library/intro.rst index 8567e4d1d08920..5bb33b9c10cc03 100644 --- a/Doc/library/intro.rst +++ b/Doc/library/intro.rst @@ -58,5 +58,5 @@ Notes on availability operating system. * If not separately noted, all functions that claim "Availability: Unix" are - supported on Mac OS X, which builds on a Unix core. + supported on macOS, which builds on a Unix core. diff --git a/Doc/library/io.rst b/Doc/library/io.rst index f9ffc19fac489d..c125ec2aa2e677 100644 --- a/Doc/library/io.rst +++ b/Doc/library/io.rst @@ -131,7 +131,7 @@ explicitly when opening text files. If you want to use UTF-8, pass ``encoding="utf-8"``. To use the current locale encoding, ``encoding="locale"`` is supported in Python 3.10. -When you need to run existing code on Windows that attempts to opens +When you need to run existing code on Windows that attempts to open UTF-8 files using the default locale encoding, you can enable the UTF-8 mode. See :ref:`UTF-8 mode on Windows `. @@ -193,7 +193,7 @@ High-level Module Interface .. versionadded:: 3.8 -.. function:: text_encoding(encoding, stacklevel=2) +.. function:: text_encoding(encoding, stacklevel=2, /) This is a helper function for callables that use :func:`open` or :class:`TextIOWrapper` and have an ``encoding=None`` parameter. @@ -306,8 +306,7 @@ I/O Base Classes .. class:: IOBase - The abstract base class for all I/O classes, acting on streams of bytes. - There is no public constructor. + The abstract base class for all I/O classes. This class provides empty abstract implementations for many methods that derived classes can override selectively; the default @@ -376,7 +375,7 @@ I/O Base Classes Return ``True`` if the stream can be read from. If ``False``, :meth:`read` will raise :exc:`OSError`. - .. method:: readline(size=-1) + .. method:: readline(size=-1, /) Read and return one line from the stream. If *size* is specified, at most *size* bytes will be read. @@ -385,16 +384,19 @@ I/O Base Classes the *newline* argument to :func:`open` can be used to select the line terminator(s) recognized. - .. method:: readlines(hint=-1) + .. method:: readlines(hint=-1, /) Read and return a list of lines from the stream. *hint* can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds *hint*. + *hint* values of ``0`` or less, as well as ``None``, are treated as no + hint. + Note that it's already possible to iterate on file objects using ``for line in file: ...`` without calling ``file.readlines()``. - .. method:: seek(offset, whence=SEEK_SET) + .. method:: seek(offset, whence=SEEK_SET, /) Change the stream position to the given byte *offset*. *offset* is interpreted relative to the position indicated by *whence*. The default @@ -426,7 +428,7 @@ I/O Base Classes Return the current stream position. - .. method:: truncate(size=None) + .. method:: truncate(size=None, /) Resize the stream to the given *size* in bytes (or the current position if *size* is not specified). The current stream position isn't changed. @@ -443,7 +445,7 @@ I/O Base Classes Return ``True`` if the stream supports writing. If ``False``, :meth:`write` and :meth:`truncate` will raise :exc:`OSError`. - .. method:: writelines(lines) + .. method:: writelines(lines, /) Write a list of lines to the stream. Line separators are not added, so it is usual for each of the lines provided to have a line separator at the @@ -458,8 +460,7 @@ I/O Base Classes .. class:: RawIOBase - Base class for raw binary streams. It inherits :class:`IOBase`. There is no - public constructor. + Base class for raw binary streams. It inherits :class:`IOBase`. Raw binary streams typically provide low-level access to an underlying OS device or API, and do not try to encapsulate it in high-level primitives @@ -469,7 +470,7 @@ I/O Base Classes :class:`RawIOBase` provides these methods in addition to those from :class:`IOBase`: - .. method:: read(size=-1) + .. method:: read(size=-1, /) Read up to *size* bytes from the object and return them. As a convenience, if *size* is unspecified or -1, all bytes until EOF are returned. @@ -488,7 +489,7 @@ I/O Base Classes Read and return all the bytes from the stream until EOF, using multiple calls to the stream if necessary. - .. method:: readinto(b) + .. method:: readinto(b, /) Read bytes into a pre-allocated, writable :term:`bytes-like object` *b*, and return the @@ -496,7 +497,7 @@ I/O Base Classes If the object is in non-blocking mode and no bytes are available, ``None`` is returned. - .. method:: write(b) + .. method:: write(b, /) Write the given :term:`bytes-like object`, *b*, to the underlying raw stream, and return the number of @@ -512,7 +513,7 @@ I/O Base Classes .. class:: BufferedIOBase Base class for binary streams that support some kind of buffering. - It inherits :class:`IOBase`. There is no public constructor. + It inherits :class:`IOBase`. The main difference with :class:`RawIOBase` is that methods :meth:`read`, :meth:`readinto` and :meth:`write` will try (respectively) to read as much @@ -553,7 +554,7 @@ I/O Base Classes .. versionadded:: 3.1 - .. method:: read(size=-1) + .. method:: read(size=-1, /) Read and return up to *size* bytes. If the argument is omitted, ``None``, or negative, data is read and returned until EOF is reached. An empty @@ -568,7 +569,7 @@ I/O Base Classes A :exc:`BlockingIOError` is raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment. - .. method:: read1([size]) + .. method:: read1(size=-1, /) Read and return up to *size* bytes, with at most one call to the underlying raw stream's :meth:`~RawIOBase.read` (or @@ -579,7 +580,7 @@ I/O Base Classes If *size* is ``-1`` (the default), an arbitrary number of bytes are returned (more than zero unless EOF is reached). - .. method:: readinto(b) + .. method:: readinto(b, /) Read bytes into a pre-allocated, writable :term:`bytes-like object` *b* and return the number of bytes read. @@ -591,7 +592,7 @@ I/O Base Classes A :exc:`BlockingIOError` is raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment. - .. method:: readinto1(b) + .. method:: readinto1(b, /) Read bytes into a pre-allocated, writable :term:`bytes-like object` *b*, using at most one call to @@ -603,7 +604,7 @@ I/O Base Classes .. versionadded:: 3.5 - .. method:: write(b) + .. method:: write(b, /) Write the given :term:`bytes-like object`, *b*, and return the number of bytes written (always equal to the length of *b* in bytes, since if @@ -686,7 +687,7 @@ Buffered Streams Buffered I/O streams provide a higher-level interface to an I/O device than raw I/O does. -.. class:: BytesIO([initial_bytes]) +.. class:: BytesIO(initial_bytes=b'') A binary stream using an in-memory bytes buffer. It inherits :class:`BufferedIOBase`. The buffer is discarded when the @@ -721,14 +722,14 @@ than raw I/O does. Return :class:`bytes` containing the entire contents of the buffer. - .. method:: read1([size]) + .. method:: read1(size=-1, /) In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.read`. .. versionchanged:: 3.7 The *size* argument is now optional. - .. method:: readinto1(b) + .. method:: readinto1(b, /) In :class:`BytesIO`, this is the same as :meth:`~BufferedIOBase.readinto`. @@ -751,18 +752,18 @@ than raw I/O does. :class:`BufferedReader` provides or overrides these methods in addition to those from :class:`BufferedIOBase` and :class:`IOBase`: - .. method:: peek([size]) + .. method:: peek(size=0, /) Return bytes from the stream without advancing the position. At most one single read on the raw stream is done to satisfy the call. The number of bytes returned may be less or more than requested. - .. method:: read([size]) + .. method:: read(size=-1, /) Read and return *size* bytes, or if *size* is not given or negative, until EOF or if the read call would block in non-blocking mode. - .. method:: read1([size]) + .. method:: read1(size=-1, /) Read and return up to *size* bytes with only one call on the raw stream. If at least one byte is buffered, only buffered bytes are returned. @@ -799,7 +800,7 @@ than raw I/O does. Force bytes held in the buffer into the raw stream. A :exc:`BlockingIOError` should be raised if the raw stream blocks. - .. method:: write(b) + .. method:: write(b, /) Write the :term:`bytes-like object`, *b*, and return the number of bytes written. When in non-blocking mode, a @@ -822,7 +823,7 @@ than raw I/O does. are guaranteed to be implemented. -.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE) +.. class:: BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE, /) A buffered binary stream providing higher-level access to two non seekable :class:`RawIOBase` raw binary streams---one readable, the other writeable. @@ -849,8 +850,7 @@ Text I/O .. class:: TextIOBase Base class for text streams. This class provides a character and line based - interface to stream I/O. It inherits :class:`IOBase`. There is no public - constructor. + interface to stream I/O. It inherits :class:`IOBase`. :class:`TextIOBase` provides or overrides these data attributes and methods in addition to those from :class:`IOBase`: @@ -890,19 +890,19 @@ Text I/O .. versionadded:: 3.1 - .. method:: read(size=-1) + .. method:: read(size=-1, /) Read and return at most *size* characters from the stream as a single :class:`str`. If *size* is negative or ``None``, reads until EOF. - .. method:: readline(size=-1) + .. method:: readline(size=-1, /) Read until newline or EOF and return a single ``str``. If the stream is already at EOF, an empty string is returned. If *size* is specified, at most *size* characters will be read. - .. method:: seek(offset, whence=SEEK_SET) + .. method:: seek(offset, whence=SEEK_SET, /) Change the stream position to the given *offset*. Behaviour depends on the *whence* parameter. The default value for *whence* is @@ -929,7 +929,7 @@ Text I/O does not usually represent a number of bytes in the underlying binary storage. - .. method:: write(s) + .. method:: write(s, /) Write the string *s* to the stream and return the number of characters written. diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst index 1c2263b128a8fe..9c2dff55703273 100644 --- a/Doc/library/ipaddress.rst +++ b/Doc/library/ipaddress.rst @@ -41,7 +41,7 @@ IP addresses, networks and interfaces: Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on the IP address passed as argument. Either IPv4 or IPv6 addresses may be - supplied; integers less than 2**32 will be considered to be IPv4 by default. + supplied; integers less than ``2**32`` will be considered to be IPv4 by default. A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or IPv6 address. @@ -56,7 +56,7 @@ IP addresses, networks and interfaces: Return an :class:`IPv4Network` or :class:`IPv6Network` object depending on the IP address passed as argument. *address* is a string or integer representing the IP network. Either IPv4 or IPv6 networks may be supplied; - integers less than 2**32 will be considered to be IPv4 by default. *strict* + integers less than ``2**32`` will be considered to be IPv4 by default. *strict* is passed to :class:`IPv4Network` or :class:`IPv6Network` constructor. A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or IPv6 address, or if the network has host bits set. @@ -70,7 +70,7 @@ IP addresses, networks and interfaces: Return an :class:`IPv4Interface` or :class:`IPv6Interface` object depending on the IP address passed as argument. *address* is a string or integer representing the IP address. Either IPv4 or IPv6 addresses may be supplied; - integers less than 2**32 will be considered to be IPv4 by default. A + integers less than ``2**32`` will be considered to be IPv4 by default. A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or IPv6 address. @@ -132,6 +132,11 @@ write code that handles both IP versions correctly. Address objects are The above change was also included in Python 3.9 starting with version 3.9.5. + .. versionchanged:: 3.8.12 + + The above change was also included in Python 3.8 starting with + version 3.8.12. + .. attribute:: version The appropriate version number: ``4`` for IPv4, ``6`` for IPv6. @@ -677,7 +682,7 @@ dictionaries. Note that currently expanded netmasks are not supported. That means ``2001:db00::0/24`` is a valid argument while ``2001:db00::0/ffff:ff00::`` - not. + is not. 2. An integer that fits into 128 bits. This is equivalent to a single-address network, with the network address being *address* and diff --git a/Doc/library/ipc.rst b/Doc/library/ipc.rst index b88a174eb97f15..4849c82f317d97 100644 --- a/Doc/library/ipc.rst +++ b/Doc/library/ipc.rst @@ -22,7 +22,5 @@ The list of modules described in this chapter is: ssl.rst select.rst selectors.rst - asyncore.rst - asynchat.rst signal.rst mmap.rst diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index fd77f99a88f577..6e1ba3c440124d 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -492,6 +492,8 @@ loops that truncate the stream. next(b, None) return zip(a, b) + .. versionadded:: 3.10 + .. function:: permutations(iterable, r=None) @@ -811,11 +813,36 @@ which incur interpreter overhead. return starmap(func, repeat(args)) return starmap(func, repeat(args, times)) - def grouper(iterable, n, fillvalue=None): - "Collect data into fixed-length chunks or blocks" - # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" + def grouper(iterable, n, *, incomplete='fill', fillvalue=None): + "Collect data into non-overlapping fixed-length chunks or blocks" + # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx + # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError + # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF args = [iter(iterable)] * n - return zip_longest(*args, fillvalue=fillvalue) + if incomplete == 'fill': + return zip_longest(*args, fillvalue=fillvalue) + if incomplete == 'strict': + return zip(*args, strict=True) + if incomplete == 'ignore': + return zip(*args) + else: + raise ValueError('Expected fill, strict, or ignore') + + def triplewise(iterable): + "Return overlapping triplets from an iterable" + # triplewise('ABCDEFG') -> ABC BCD CDE DEF EFG + for (a, _), (b, c) in pairwise(pairwise(iterable)): + yield a, b, c + + def sliding_window(iterable, n): + # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG + it = iter(iterable) + window = collections.deque(islice(it, n), maxlen=n) + if len(window) == n: + yield tuple(window) + for x in it: + window.append(x) + yield tuple(window) def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" @@ -837,6 +864,41 @@ which incur interpreter overhead. t1, t2 = tee(iterable) return filterfalse(pred, t1), filter(pred, t2) + def before_and_after(predicate, it): + """ Variant of takewhile() that allows complete + access to the remainder of the iterator. + + >>> it = iter('ABCdEfGhI') + >>> all_upper, remainder = before_and_after(str.isupper, it) + >>> ''.join(all_upper) + 'ABC' + >>> ''.join(remainder) # takewhile() would lose the 'd' + 'dEfGhI' + + Note that the first iterator must be fully + consumed before the second iterator can + generate valid results. + """ + it = iter(it) + transition = [] + def true_iterator(): + for elem in it: + if predicate(elem): + yield elem + else: + transition.append(elem) + return + def remainder_iterator(): + yield from transition + yield from it + return true_iterator(), remainder_iterator() + + def subslices(seq): + "Return all contiguous non-empty subslices of a sequence" + # subslices('ABCD') --> A AB ABC ABCD B BC BCD C CD D + slices = starmap(slice, combinations(range(len(seq) + 1), 2)) + return map(operator.getitem, repeat(seq), slices) + def powerset(iterable): "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" s = list(iterable) @@ -948,4 +1010,3 @@ which incur interpreter overhead. c, n = c*(n-r)//n, n-1 result.append(pool[-1-n]) return tuple(result) - diff --git a/Doc/library/json.rst b/Doc/library/json.rst index c8184da80fe43e..608e70df5b14c9 100644 --- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@ -11,9 +11,9 @@ -------------- -`JSON (JavaScript Object Notation) `_, specified by +`JSON (JavaScript Object Notation) `_, specified by :rfc:`7159` (which obsoletes :rfc:`4627`) and by -`ECMA-404 `_, +`ECMA-404 `_, is a lightweight data interchange format inspired by `JavaScript `_ object literal syntax (although it is not a strict subset of JavaScript [#rfc-errata]_ ). @@ -125,13 +125,6 @@ See :ref:`json-commandline` for detailed documentation. This module's encoders and decoders preserve input and output order by default. Order is only lost if the underlying containers are unordered. - Prior to Python 3.7, :class:`dict` was not guaranteed to be ordered, so - inputs and outputs were typically scrambled unless - :class:`collections.OrderedDict` was specifically requested. Starting - with Python 3.7, the regular :class:`dict` became order preserving, so - it is no longer necessary to specify :class:`collections.OrderedDict` for - JSON generation and parsing. - Basic Usage ----------- @@ -159,7 +152,7 @@ Basic Usage If *check_circular* is false (default: ``True``), then the circular reference check for container types will be skipped and a circular reference - will result in an :exc:`OverflowError` (or worse). + will result in an :exc:`RecursionError` (or worse). If *allow_nan* is false (default: ``True``), then it will be a :exc:`ValueError` to serialize out of range :class:`float` values (``nan``, @@ -432,7 +425,7 @@ Encoders and Decoders If *check_circular* is true (the default), then lists, dicts, and custom encoded objects will be checked for circular references during encoding to - prevent an infinite recursion (which would cause an :exc:`OverflowError`). + prevent an infinite recursion (which would cause an :exc:`RecursionError`). Otherwise, no such check takes place. If *allow_nan* is true (the default), then ``NaN``, ``Infinity``, and @@ -544,7 +537,7 @@ Standard Compliance and Interoperability ---------------------------------------- The JSON format is specified by :rfc:`7159` and by -`ECMA-404 `_. +`ECMA-404 `_. This section details this module's level of compliance with the RFC. For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and parameters other than those explicitly mentioned, are not considered. diff --git a/Doc/library/keyword.rst b/Doc/library/keyword.rst index 5cae79f5dc9dbc..c3b4699cb05af6 100644 --- a/Doc/library/keyword.rst +++ b/Doc/library/keyword.rst @@ -9,7 +9,7 @@ -------------- This module allows a Python program to determine if a string is a -:ref:`keyword `. +:ref:`keyword ` or :ref:`soft keyword `. .. function:: iskeyword(s) @@ -26,14 +26,14 @@ This module allows a Python program to determine if a string is a .. function:: issoftkeyword(s) - Return ``True`` if *s* is a Python soft :ref:`keyword `. + Return ``True`` if *s* is a Python :ref:`soft keyword `. .. versionadded:: 3.9 .. data:: softkwlist - Sequence containing all the soft :ref:`keywords ` defined for the + Sequence containing all the :ref:`soft keywords ` defined for the interpreter. If any soft keywords are defined to only be active when particular :mod:`__future__` statements are in effect, these will be included as well. diff --git a/Doc/library/locale.rst b/Doc/library/locale.rst index 60d0c59d017c73..dd14a379b0c6cc 100644 --- a/Doc/library/locale.rst +++ b/Doc/library/locale.rst @@ -435,10 +435,10 @@ The :mod:`locale` module defines the following exception and functions: .. versionadded:: 3.10 -.. function:: atof(string) +.. function:: atof(string, func=float) - Converts a string to a floating point number, following the :const:`LC_NUMERIC` - settings. + Converts a string to a number, following the :const:`LC_NUMERIC` settings, + by calling *func* on the result of calling :func:`delocalize` on *string*. .. function:: atoi(string) diff --git a/Doc/library/logging.config.rst b/Doc/library/logging.config.rst index f833bcdd1fcf5b..a1b8dc755ba6b0 100644 --- a/Doc/library/logging.config.rst +++ b/Doc/library/logging.config.rst @@ -152,6 +152,8 @@ in :mod:`logging` itself) and defining handlers which are declared either in send it to the socket as a sequence of bytes preceded by a four-byte length string packed in binary using ``struct.pack('>L', n)``. + .. _logging-eval-security: + .. note:: Because portions of the configuration are passed through @@ -166,7 +168,7 @@ in :mod:`logging` itself) and defining handlers which are declared either in :func:`listen` socket and sending a configuration which runs whatever code the attacker wants to have executed in the victim's process. This is especially easy to do if the default port is used, but not hard even if a - different port is used). To avoid the risk of this happening, use the + different port is used. To avoid the risk of this happening, use the ``verify`` argument to :func:`listen` to prevent unrecognised configurations from being applied. @@ -189,6 +191,20 @@ in :mod:`logging` itself) and defining handlers which are declared either in :func:`listen`. +Security considerations +^^^^^^^^^^^^^^^^^^^^^^^ + +The logging configuration functionality tries to offer convenience, and in part this +is done by offering the ability to convert text in configuration files into Python +objects used in logging configuration - for example, as described in +:ref:`logging-config-dict-userdef`. However, these same mechanisms (importing +callables from user-defined modules and calling them with parameters from the +configuration) could be used to invoke any code you like, and for this reason you +should treat configuration files from untrusted sources with *extreme caution* and +satisfy yourself that nothing bad can happen if you load them, before actually loading +them. + + .. _logging-config-dictschema: Configuration dictionary schema diff --git a/Doc/library/logging.handlers.rst b/Doc/library/logging.handlers.rst index 059ab3d3a34448..a5b181ee612d5f 100644 --- a/Doc/library/logging.handlers.rst +++ b/Doc/library/logging.handlers.rst @@ -117,6 +117,9 @@ sends logging output to a disk file. It inherits the output functionality from Outputs the record to the file. + Note that if the file was closed due to logging shutdown at exit and the file + mode is 'w', the record will not be emitted (see :issue:`42378`). + .. _null-handler: @@ -231,6 +234,19 @@ need to override. return the same output every time for a given input, otherwise the rollover behaviour may not work as expected. + It's also worth noting that care should be taken when using a namer to + preserve certain attributes in the filename which are used during rotation. + For example, :class:`RotatingFileHandler` expects to have a set of log files + whose names contain successive integers, so that rotation works as expected, + and :class:`TimedRotatingFileHandler` deletes old log files (based on the + ``backupCount`` parameter passed to the handler's initializer) by determining + the oldest files to delete. For this to happen, the filenames should be + sortable using the date/time portion of the filename, and a namer needs to + respect this. (If a namer is wanted that doesn't respect this scheme, it will + need to be used in a subclass of :class:`TimedRotatingFileHandler` which + overrides the :meth:`~TimedRotatingFileHandler.getFilesToDelete` method to + fit in with the custom naming scheme.) + .. versionadded:: 3.3 @@ -440,6 +456,10 @@ timed intervals. Outputs the record to the file, catering for rollover as described above. + .. method:: getFilesToDelete() + + Returns a list of filenames which should be deleted as part of rollover. These + are the absolute paths of the oldest backup log files written by the handler. .. _socket-handler: @@ -927,7 +947,7 @@ HTTPHandler ^^^^^^^^^^^ The :class:`HTTPHandler` class, located in the :mod:`logging.handlers` module, -supports sending logging messages to a Web server, using either ``GET`` or +supports sending logging messages to a web server, using either ``GET`` or ``POST`` semantics. @@ -957,17 +977,17 @@ supports sending logging messages to a Web server, using either ``GET`` or .. method:: emit(record) - Sends the record to the Web server as a URL-encoded dictionary. The + Sends the record to the web server as a URL-encoded dictionary. The :meth:`mapLogRecord` method is used to convert the record to the dictionary to be sent. - .. note:: Since preparing a record for sending it to a Web server is not + .. note:: Since preparing a record for sending it to a web server is not the same as a generic formatting operation, using :meth:`~logging.Handler.setFormatter` to specify a :class:`~logging.Formatter` for a :class:`HTTPHandler` has no effect. Instead of calling :meth:`~logging.Handler.format`, this handler calls :meth:`mapLogRecord` and then :func:`urllib.parse.urlencode` to encode the - dictionary in a form suitable for sending to a Web server. + dictionary in a form suitable for sending to a web server. .. _queue-handler: @@ -984,7 +1004,7 @@ supports sending logging messages to a queue, such as those implemented in the Along with the :class:`QueueListener` class, :class:`QueueHandler` can be used to let handlers do their work on a separate thread from the one which does the -logging. This is important in Web applications and also other service +logging. This is important in web applications and also other service applications where threads servicing clients need to respond as quickly as possible, while any potentially slow operations (such as sending an email via :class:`SMTPHandler`) are done on a separate thread. @@ -1046,7 +1066,7 @@ because it works hand-in-hand with :class:`QueueHandler`. Along with the :class:`QueueHandler` class, :class:`QueueListener` can be used to let handlers do their work on a separate thread from the one which does the -logging. This is important in Web applications and also other service +logging. This is important in web applications and also other service applications where threads servicing clients need to respond as quickly as possible, while any potentially slow operations (such as sending an email via :class:`SMTPHandler`) are done on a separate thread. diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index 70a703dde18a03..ed2d97698d3f6d 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -30,9 +30,17 @@ is that all Python modules can participate in logging, so your application log can include your own messages integrated with messages from third-party modules. +The simplest example: + +.. code-block:: none + + >>> import logging + >>> logging.warning('Watch out!') + WARNING:root:Watch out! + The module provides a lot of functionality and flexibility. If you are -unfamiliar with logging, the best way to get to grips with it is to see the -tutorials (see the links on the right). +unfamiliar with logging, the best way to get to grips with it is to view the +tutorials (**see the links above and on the right**). The basic classes defined by the module, together with their functions, are listed below. @@ -80,6 +88,15 @@ is the module's name in the Python package namespace. If this evaluates to false, logging messages are not passed to the handlers of ancestor loggers. + Spelling it out with an example: If the propagate attribute of the logger named + ``A.B.C`` evaluates to true, any event logged to ``A.B.C`` via a method call such as + ``logging.getLogger('A.B.C').error(...)`` will [subject to passing that logger's + level and filter settings] be passed in turn to any handlers attached to loggers + named ``A.B``, ``A`` and the root logger, after first being passed to any handlers + attached to ``A.B.C``. If any logger in the chain ``A.B.C``, ``A.B``, ``A`` has its + ``propagate`` attribute set to false, then that is the last logger whose handlers + are offered the event to handle, and propagation stops at that point. + The constructor sets this attribute to ``True``. .. note:: If you attach a handler to a logger *and* one or more of its @@ -203,7 +220,7 @@ is the module's name in the Python package namespace. attributes can then be used as you like. For example, they could be incorporated into logged messages. For example:: - FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s' + FORMAT = '%(asctime)s %(clientip)-15s %(user)-8s %(message)s' logging.basicConfig(format=FORMAT) d = {'clientip': '192.168.0.1', 'user': 'fbloggs'} logger = logging.getLogger('tcpserver') @@ -233,6 +250,10 @@ is the module's name in the Python package namespace. above example). In such circumstances, it is likely that specialized :class:`Formatter`\ s would be used with particular :class:`Handler`\ s. + If no handler is attached to this logger (or any of its ancestors, + taking into account the relevant :attr:`Logger.propagate` attributes), + the message will be sent to the handler set on :attr:`lastResort`. + .. versionchanged:: 3.2 The *stack_info* parameter was added. @@ -900,6 +921,10 @@ interchangeably. :meth:`~Logger.setLevel` and :meth:`~Logger.hasHandlers` methods were added to :class:`LoggerAdapter`. These methods delegate to the underlying logger. +.. versionchanged:: 3.6 + Attribute :attr:`manager` and method :meth:`_log` were added, which + delegate to the underlying logger and allow adapters to be nested. + Thread Safety ------------- @@ -996,7 +1021,7 @@ functions. be used as you like. For example, they could be incorporated into logged messages. For example:: - FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s' + FORMAT = '%(asctime)s %(clientip)-15s %(user)-8s %(message)s' logging.basicConfig(format=FORMAT) d = {'clientip': '192.168.0.1', 'user': 'fbloggs'} logging.warning('Protocol problem: %s', 'connection reset', extra=d) @@ -1025,6 +1050,10 @@ functions. above example). In such circumstances, it is likely that specialized :class:`Formatter`\ s would be used with particular :class:`Handler`\ s. + This function (as well as :func:`info`, :func:`warning`, :func:`error` and + :func:`critical`) will call :func:`basicConfig` if the root logger doesn't + have any handler attached. + .. versionchanged:: 3.2 The *stack_info* parameter was added. @@ -1067,16 +1096,6 @@ functions. Logs a message with level *level* on the root logger. The other arguments are interpreted as for :func:`debug`. - .. note:: The above module-level convenience functions, which delegate to the - root logger, call :func:`basicConfig` to ensure that at least one handler - is available. Because of this, they should *not* be used in threads, - in versions of Python earlier than 2.7.1 and 3.2, unless at least one - handler has been added to the root logger *before* the threads are - started. In earlier versions of Python, due to a thread safety shortcoming - in :func:`basicConfig`, this can (under rare circumstances) lead to - handlers being added multiple times to the root logger, which can in turn - lead to multiple messages for the same event. - .. function:: disable(level=CRITICAL) Provides an overriding level *level* for all loggers which takes precedence over @@ -1352,7 +1371,7 @@ with the :mod:`warnings` module. The proposal which described this feature for inclusion in the Python standard library. - `Original Python logging package `_ + `Original Python logging package `_ This is the original source for the :mod:`logging` package. The version of the package available from this site is suitable for use with Python 1.5.2, 2.1.x and 2.2.x, which do not include the :mod:`logging` package in the standard diff --git a/Doc/library/mailcap.rst b/Doc/library/mailcap.rst index bf9639bdaca50a..e2e5bb34456373 100644 --- a/Doc/library/mailcap.rst +++ b/Doc/library/mailcap.rst @@ -3,22 +3,28 @@ .. module:: mailcap :synopsis: Mailcap file handling. + :deprecated: **Source code:** :source:`Lib/mailcap.py` +.. deprecated:: 3.11 + The :mod:`mailcap` module is deprecated + (see :pep:`PEP 594 <594#mailcap>` for details). + The :mod:`mimetypes` module provides an alternative. + -------------- Mailcap files are used to configure how MIME-aware applications such as mail -readers and Web browsers react to files with different MIME types. (The name +readers and web browsers react to files with different MIME types. (The name "mailcap" is derived from the phrase "mail capability".) For example, a mailcap file might contain a line like ``video/mpeg; xmpeg %s``. Then, if the user -encounters an email message or Web document with the MIME type +encounters an email message or web document with the MIME type :mimetype:`video/mpeg`, ``%s`` will be replaced by a filename (usually one belonging to a temporary file) and the :program:`xmpeg` program can be automatically started to view the file. The mailcap format is documented in :rfc:`1524`, "A User Agent Configuration -Mechanism For Multimedia Mail Format Information", but is not an Internet +Mechanism For Multimedia Mail Format Information", but is not an internet standard. However, mailcap files are supported on most Unix systems. diff --git a/Doc/library/marshal.rst b/Doc/library/marshal.rst index d65afc20041133..24f9dc1689da4a 100644 --- a/Doc/library/marshal.rst +++ b/Doc/library/marshal.rst @@ -66,6 +66,8 @@ The module defines these functions: The *version* argument indicates the data format that ``dump`` should use (see below). + .. audit-event:: marshal.dumps value,version marshal.dump + .. function:: load(file) @@ -74,11 +76,18 @@ The module defines these functions: format), raise :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. The file must be a readable :term:`binary file`. + .. audit-event:: marshal.load "" marshal.load + .. note:: If an object containing an unsupported type was marshalled with :func:`dump`, :func:`load` will substitute ``None`` for the unmarshallable type. + .. versionchanged:: 3.10 + + This call used to raise a ``code.__new__`` audit event for each code object. Now + it raises a single ``marshal.load`` event for the entire load operation. + .. function:: dumps(value[, version]) @@ -89,6 +98,8 @@ The module defines these functions: The *version* argument indicates the data format that ``dumps`` should use (see below). + .. audit-event:: marshal.dumps value,version marshal.dump + .. function:: loads(bytes) @@ -96,6 +107,13 @@ The module defines these functions: :exc:`EOFError`, :exc:`ValueError` or :exc:`TypeError`. Extra bytes in the input are ignored. + .. audit-event:: marshal.loads bytes marshal.load + + .. versionchanged:: 3.10 + + This call used to raise a ``code.__new__`` audit event for each code object. Now + it raises a single ``marshal.loads`` event for the entire load operation. + In addition, the following constants are defined: diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 145bac4966e18c..7ba5fa4a0b4e7f 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -32,8 +32,8 @@ Number-theoretic and representation functions .. function:: ceil(x) Return the ceiling of *x*, the smallest integer greater than or equal to *x*. - If *x* is not a float, delegates to ``x.__ceil__()``, which should return an - :class:`~numbers.Integral` value. + If *x* is not a float, delegates to :meth:`x.__ceil__ `, + which should return an :class:`~numbers.Integral` value. .. function:: comb(n, k) @@ -77,9 +77,9 @@ Number-theoretic and representation functions .. function:: floor(x) - Return the floor of *x*, the largest integer less than or equal to *x*. - If *x* is not a float, delegates to ``x.__floor__()``, which should return an - :class:`~numbers.Integral` value. + Return the floor of *x*, the largest integer less than or equal to *x*. If + *x* is not a float, delegates to :meth:`x.__floor__ `, which + should return an :class:`~numbers.Integral` value. .. function:: fmod(x, y) @@ -298,9 +298,11 @@ Number-theoretic and representation functions .. function:: trunc(x) - Return the :class:`~numbers.Real` value *x* truncated to an - :class:`~numbers.Integral` (usually an integer). Delegates to - :meth:`x.__trunc__() `. + Return *x* with the fractional part + removed, leaving the integer part. This rounds toward 0: ``trunc()`` is + equivalent to :func:`floor` for positive *x*, and equivalent to :func:`ceil` + for negative *x*. If *x* is not a float, delegates to :meth:`x.__trunc__ + `, which should return an :class:`~numbers.Integral` value. .. function:: ulp(x) @@ -627,8 +629,23 @@ Constants .. data:: nan - A floating-point "not a number" (NaN) value. Equivalent to the output of - ``float('nan')``. + A floating-point "not a number" (NaN) value. Equivalent to the output of + ``float('nan')``. Due to the requirements of the `IEEE-754 standard + `_, ``math.nan`` and ``float('nan')`` are + not considered to equal to any other numeric value, including themselves. To check + whether a number is a NaN, use the :func:`isnan` function to test + for NaNs instead of ``is`` or ``==``. + Example:: + + >>> import math + >>> math.nan == math.nan + False + >>> float('nan') == float('nan') + False + >>> math.isnan(math.nan) + True + >>> math.isnan(float('nan')) + True .. versionadded:: 3.5 diff --git a/Doc/library/mm.rst b/Doc/library/mm.rst index c8f79c4de1cdf2..cd06e9385a2612 100644 --- a/Doc/library/mm.rst +++ b/Doc/library/mm.rst @@ -11,12 +11,5 @@ discretion of the installation. Here's an overview: .. toctree:: - audioop.rst - aifc.rst - sunau.rst wave.rst - chunk.rst colorsys.rst - imghdr.rst - sndhdr.rst - ossaudiodev.rst diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index d9825b47c71333..79733ed7d2d070 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -102,7 +102,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length To ensure validity of the created memory mapping the file specified by the descriptor *fileno* is internally automatically synchronized - with physical backing store on Mac OS X and OpenVMS. + with the physical backing store on macOS. This example shows a simple way of using :class:`~mmap.mmap`:: diff --git a/Doc/library/msilib.rst b/Doc/library/msilib.rst index 83b3d4973bf0d9..0eba2275c8e051 100644 --- a/Doc/library/msilib.rst +++ b/Doc/library/msilib.rst @@ -4,6 +4,7 @@ .. module:: msilib :platform: Windows :synopsis: Creation of Microsoft Installer files, and CAB files. + :deprecated: .. moduleauthor:: Martin v. Löwis .. sectionauthor:: Martin v. Löwis @@ -12,6 +13,10 @@ .. index:: single: msi +.. deprecated:: 3.11 + The :mod:`msilib` module is deprecated + (see :pep:`PEP 594 <594#msilib>` for details). + -------------- The :mod:`msilib` supports the creation of Microsoft Installer (``.msi``) files. diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index ae4f7bf3b9621d..de8f9718337d77 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -569,8 +569,15 @@ The :mod:`multiprocessing` package mostly replicates the API of the .. attribute:: exitcode The child's exit code. This will be ``None`` if the process has not yet - terminated. A negative value *-N* indicates that the child was terminated - by signal *N*. + terminated. + + If the child's :meth:`run` method returned normally, the exit code + will be 0. If it terminated via :func:`sys.exit` with an integer + argument *N*, the exit code will be *N*. + + If the child terminated due to an exception not caught within + :meth:`run`, the exit code will be 1. If it was terminated by + signal *N*, the exit code will be the negative value *-N*. .. attribute:: authkey @@ -783,7 +790,7 @@ For an example of the usage of queues for interprocess communication see multithreading/multiprocessing semantics, this number is not reliable. Note that this may raise :exc:`NotImplementedError` on Unix platforms like - Mac OS X where ``sem_getvalue()`` is not implemented. + macOS where ``sem_getvalue()`` is not implemented. .. method:: empty() @@ -951,7 +958,8 @@ Miscellaneous use. The number of usable CPUs can be obtained with ``len(os.sched_getaffinity(0))`` - May raise :exc:`NotImplementedError`. + When the number of CPUs cannot be determined a :exc:`NotImplementedError` + is raised. .. seealso:: :func:`os.cpu_count` @@ -1029,13 +1037,19 @@ Miscellaneous The return value can be ``'fork'``, ``'spawn'``, ``'forkserver'`` or ``None``. ``'fork'`` is the default on Unix, while ``'spawn'`` is - the default on Windows. + the default on Windows and macOS. + +.. versionchanged:: 3.8 + + On macOS, the *spawn* start method is now the default. The *fork* start + method should be considered unsafe as it can lead to crashes of the + subprocess. See :issue:`33725`. .. versionadded:: 3.4 -.. function:: set_executable() +.. function:: set_executable(executable) - Sets the path of the Python interpreter to use when starting a child process. + Set the path of the Python interpreter to use when starting a child process. (By default :data:`sys.executable` is used). Embedders will probably need to do some thing like :: @@ -1187,6 +1201,7 @@ For example: >>> arr2 array('i', [0, 1, 2, 3, 4, 0, 0, 0, 0, 0]) +.. _multiprocessing-recv-pickle-security: .. warning:: @@ -1233,7 +1248,7 @@ object -- see :ref:`multiprocessing-managers`. first argument is named *block*, as is consistent with :meth:`Lock.acquire`. .. note:: - On Mac OS X, this is indistinguishable from :class:`Semaphore` because + On macOS, this is indistinguishable from :class:`Semaphore` because ``sem_getvalue()`` is not implemented on that platform. .. class:: Condition([lock]) @@ -1372,7 +1387,7 @@ object -- see :ref:`multiprocessing-managers`. .. note:: - On Mac OS X, ``sem_timedwait`` is unsupported, so calling ``acquire()`` with + On macOS, ``sem_timedwait`` is unsupported, so calling ``acquire()`` with a timeout will emulate that function's behavior using a sleeping loop. .. note:: @@ -1633,6 +1648,7 @@ different machines. A manager object controls a server process which manages proxies. .. function:: multiprocessing.Manager() + :module: Returns a started :class:`~multiprocessing.managers.SyncManager` object which can be used for sharing objects between processes. The returned manager @@ -2242,8 +2258,9 @@ with the :class:`Pool` class. .. method:: starmap(func, iterable[, chunksize]) - Like :meth:`map` except that the elements of the *iterable* are expected - to be iterables that are unpacked as arguments. + Like :meth:`~multiprocessing.pool.Pool.map` except that the + elements of the *iterable* are expected to be iterables that are + unpacked as arguments. Hence an *iterable* of ``[(1,2), (3, 4)]`` results in ``[func(1,2), func(3,4)]``. @@ -2627,12 +2644,13 @@ handler type) for messages from different processes to get mixed up. inherited. .. currentmodule:: multiprocessing -.. function:: log_to_stderr() +.. function:: log_to_stderr(level=None) This function performs a call to :func:`get_logger` but in addition to returning the logger created by get_logger, it adds a handler which sends output to :data:`sys.stderr` using format ``'[%(levelname)s/%(processName)s] %(message)s'``. + You can modify ``levelname`` of the logger by passing a ``level`` argument. Below is an example session with logging turned on:: diff --git a/Doc/library/multiprocessing.shared_memory.rst b/Doc/library/multiprocessing.shared_memory.rst index cba576a29e2e2b..2ba42b7e579a77 100644 --- a/Doc/library/multiprocessing.shared_memory.rst +++ b/Doc/library/multiprocessing.shared_memory.rst @@ -342,3 +342,30 @@ behind it: >>> c.shm.close() >>> c.shm.unlink() +The following examples demonstrates that ``ShareableList`` +(and underlying ``SharedMemory``) objects +can be pickled and unpickled if needed. +Note, that it will still be the same shared object. +This happens, because the deserialized object has +the same unique name and is just attached to an existing +object with the same name (if the object is still alive): + + >>> import pickle + >>> from multiprocessing import shared_memory + >>> sl = shared_memory.ShareableList(range(10)) + >>> list(sl) + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + + >>> deserialized_sl = pickle.loads(pickle.dumps(sl)) + >>> list(deserialized_sl) + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + + >>> sl[0] = -1 + >>> deserialized_sl[1] = -2 + >>> list(sl) + [-1, -2, 2, 3, 4, 5, 6, 7, 8, 9] + >>> list(deserialized_sl) + [-1, -2, 2, 3, 4, 5, 6, 7, 8, 9] + + >>> sl.shm.close() + >>> sl.shm.unlink() diff --git a/Doc/library/netdata.rst b/Doc/library/netdata.rst index 491501665e3d71..1cf717332731ca 100644 --- a/Doc/library/netdata.rst +++ b/Doc/library/netdata.rst @@ -6,18 +6,16 @@ Internet Data Handling ********************** This chapter describes modules which support handling data formats commonly used -on the Internet. +on the internet. .. toctree:: email.rst json.rst - mailcap.rst mailbox.rst mimetypes.rst base64.rst binhex.rst binascii.rst quopri.rst - uu.rst diff --git a/Doc/library/nis.rst b/Doc/library/nis.rst index 10c67cbb81b2f0..49fe62954cce8a 100644 --- a/Doc/library/nis.rst +++ b/Doc/library/nis.rst @@ -5,10 +5,15 @@ .. module:: nis :platform: Unix :synopsis: Interface to Sun's NIS (Yellow Pages) library. + :deprecated: .. moduleauthor:: Fred Gansevles .. sectionauthor:: Moshe Zadka +.. deprecated:: 3.11 + The :mod:`nis` module is deprecated + (see :pep:`PEP 594 <594#nis>` for details). + -------------- The :mod:`nis` module gives a thin wrapper around the NIS library, useful for diff --git a/Doc/library/nntplib.rst b/Doc/library/nntplib.rst index e7ec9047e015e6..2a996e451bf7c5 100644 --- a/Doc/library/nntplib.rst +++ b/Doc/library/nntplib.rst @@ -3,6 +3,7 @@ .. module:: nntplib :synopsis: NNTP protocol client (requires sockets). + :deprecated: **Source code:** :source:`Lib/nntplib.py` @@ -10,6 +11,9 @@ pair: NNTP; protocol single: Network News Transfer Protocol +.. deprecated:: 3.11 + The :mod:`nntplib` module is deprecated (see :pep:`594` for details). + -------------- This module defines the class :class:`NNTP` which implements the client side of diff --git a/Doc/library/numbers.rst b/Doc/library/numbers.rst index 1b594952ead724..b77845ed0dee92 100644 --- a/Doc/library/numbers.rst +++ b/Doc/library/numbers.rst @@ -10,7 +10,7 @@ The :mod:`numbers` module (:pep:`3141`) defines a hierarchy of numeric :term:`abstract base classes ` which progressively define -more operations. None of the types defined in this module can be instantiated. +more operations. None of the types defined in this module are intended to be instantiated. .. class:: Number @@ -27,8 +27,8 @@ The numeric tower Subclasses of this type describe complex numbers and include the operations that work on the built-in :class:`complex` type. These are: conversions to :class:`complex` and :class:`bool`, :attr:`.real`, :attr:`.imag`, ``+``, - ``-``, ``*``, ``/``, :func:`abs`, :meth:`conjugate`, ``==``, and ``!=``. All - except ``-`` and ``!=`` are abstract. + ``-``, ``*``, ``/``, ``**``, :func:`abs`, :meth:`conjugate`, ``==``, and + ``!=``. All except ``-`` and ``!=`` are abstract. .. attribute:: real @@ -76,8 +76,9 @@ The numeric tower Subtypes :class:`Rational` and adds a conversion to :class:`int`. Provides defaults for :func:`float`, :attr:`~Rational.numerator`, and - :attr:`~Rational.denominator`. Adds abstract methods for ``**`` and - bit-string operations: ``<<``, ``>>``, ``&``, ``^``, ``|``, ``~``. + :attr:`~Rational.denominator`. Adds abstract methods for :func:`pow` with + modulus and bit-string operations: ``<<``, ``>>``, ``&``, ``^``, ``|``, + ``~``. Notes for type implementors diff --git a/Doc/library/optparse.rst b/Doc/library/optparse.rst index b1094198f4c844..0d686b10365a6d 100644 --- a/Doc/library/optparse.rst +++ b/Doc/library/optparse.rst @@ -131,7 +131,7 @@ option These option syntaxes are not supported by :mod:`optparse`, and they never will be. This is deliberate: the first three are non-standard on any environment, and the last only makes sense if you're exclusively targeting - VMS, MS-DOS, and/or Windows. + Windows or certain legacy platforms (e.g. VMS, MS-DOS). option argument an argument that follows an option, is closely associated with that option, diff --git a/Doc/library/os.path.rst b/Doc/library/os.path.rst index d06d9ce8c9e3d7..ce7913e3712d73 100644 --- a/Doc/library/os.path.rst +++ b/Doc/library/os.path.rst @@ -5,22 +5,16 @@ :synopsis: Operations on pathnames. **Source code:** :source:`Lib/posixpath.py` (for POSIX) and -:source:`Lib/ntpath.py` (for Windows NT). +:source:`Lib/ntpath.py` (for Windows). .. index:: single: path; operations -------------- -This module implements some useful functions on pathnames. To read or -write files see :func:`open`, and for accessing the filesystem see the -:mod:`os` module. The path parameters can be passed as either strings, -or bytes. Applications are encouraged to represent file names as -(Unicode) character strings. Unfortunately, some file names may not be -representable as strings on Unix, so applications that need to support -arbitrary file names on Unix should use bytes objects to represent -path names. Vice versa, using bytes objects cannot represent all file -names on Windows (in the standard ``mbcs`` encoding), hence Windows -applications should use string objects to access all files. +This module implements some useful functions on pathnames. To read or write +files see :func:`open`, and for accessing the filesystem see the :mod:`os` +module. The path parameters can be passed as strings, or bytes, or any object +implementing the :class:`os.PathLike` protocol. Unlike a unix shell, Python does not do any *automatic* path expansions. Functions such as :func:`expanduser` and :func:`expandvars` can be invoked @@ -38,7 +32,6 @@ the :mod:`glob` module.) their parameters. The result is an object of the same type, if a path or file name is returned. - .. note:: Since different operating systems have different path name conventions, there @@ -340,6 +333,14 @@ the :mod:`glob` module.) that contains symbolic links. On Windows, it converts forward slashes to backward slashes. To normalize case, use :func:`normcase`. + .. note:: + On POSIX systems, in accordance with `IEEE Std 1003.1 2013 Edition; 4.13 + Pathname Resolution `_, + if a pathname begins with exactly two slashes, the first component + following the leading characters may be interpreted in an implementation-defined + manner, although more than two leading characters shall be treated as a + single character. + .. versionchanged:: 3.6 Accepts a :term:`path-like object`. @@ -462,12 +463,16 @@ the :mod:`glob` module.) On Windows, splits a pathname into drive/UNC sharepoint and relative path. If the path contains a drive letter, drive will contain everything - up to and including the colon. - e.g. ``splitdrive("c:/dir")`` returns ``("c:", "/dir")`` + up to and including the colon:: + + >>> splitdrive("c:/dir") + ("c:", "/dir") If the path contains a UNC path, drive will contain the host name - and share, up to but not including the fourth separator. - e.g. ``splitdrive("//host/computer/dir")`` returns ``("//host/computer", "/dir")`` + and share, up to but not including the fourth separator:: + + >>> splitdrive("//host/computer/dir") + ("//host/computer", "/dir") .. versionchanged:: 3.6 Accepts a :term:`path-like object`. @@ -476,9 +481,29 @@ the :mod:`glob` module.) .. function:: splitext(path) Split the pathname *path* into a pair ``(root, ext)`` such that ``root + ext == - path``, and *ext* is empty or begins with a period and contains at most one - period. Leading periods on the basename are ignored; ``splitext('.cshrc')`` - returns ``('.cshrc', '')``. + path``, and the extension, *ext*, is empty or begins with a period and contains at + most one period. + + If the path contains no extension, *ext* will be ``''``:: + + >>> splitext('bar') + ('bar', '') + + If the path contains an extension, then *ext* will be set to this extension, + including the leading period. Note that previous periods will be ignored:: + + >>> splitext('foo.bar.exe') + ('foo.bar', '.exe') + >>> splitext('/foo/bar.exe') + ('/foo/bar', '.exe') + + Leading periods of the last component of the path are considered to + be part of the root:: + + >>> splitext('.cshrc') + ('.cshrc', '') + >>> splitext('/foo/....jpg') + ('/foo/....jpg', '') .. versionchanged:: 3.6 Accepts a :term:`path-like object`. diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 6f9f3219481c2b..efc5e4ce1e8373 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -113,7 +113,7 @@ of the UTF-8 encoding: :ref:`error handler ` 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 +* 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 @@ -170,14 +170,15 @@ process and user. .. data:: environ - A :term:`mapping` object representing the string environment. For example, - ``environ['HOME']`` is the pathname of your home directory (on some platforms), - and is equivalent to ``getenv("HOME")`` in C. + A :term:`mapping` object where keys and values are strings that represent + the process environment. For example, ``environ['HOME']`` is the pathname + of your home directory (on some platforms), and is equivalent to + ``getenv("HOME")`` in C. This mapping is captured the first time the :mod:`os` module is imported, typically during Python startup as part of processing :file:`site.py`. Changes - to the environment made after this time are not reflected in ``os.environ``, - except for changes made by modifying ``os.environ`` directly. + to the environment made after this time are not reflected in :data:`os.environ`, + except for changes made by modifying :data:`os.environ` directly. This mapping may be used to modify the environment as well as query the environment. :func:`putenv` will be called automatically when the mapping @@ -189,18 +190,18 @@ process and user. .. note:: - Calling :func:`putenv` directly does not change ``os.environ``, so it's better - to modify ``os.environ``. + Calling :func:`putenv` directly does not change :data:`os.environ`, so it's better + to modify :data:`os.environ`. .. note:: - On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may + On some platforms, including FreeBSD and macOS, setting ``environ`` may cause memory leaks. Refer to the system documentation for :c:func:`putenv`. You can delete items in this mapping to unset environment variables. :func:`unsetenv` will be called automatically when an item is deleted from - ``os.environ``, and when one of the :meth:`pop` or :meth:`clear` methods is + :data:`os.environ`, and when one of the :meth:`pop` or :meth:`clear` methods is called. .. versionchanged:: 3.9 @@ -209,10 +210,10 @@ process and user. .. data:: environb - Bytes version of :data:`environ`: a :term:`mapping` object representing the - environment as byte strings. :data:`environ` and :data:`environb` are - synchronized (modify :data:`environb` updates :data:`environ`, and vice - versa). + Bytes version of :data:`environ`: a :term:`mapping` object where both keys + and values are :class:`bytes` objects representing the process environment. + :data:`environ` and :data:`environb` are synchronized (modifying + :data:`environb` updates :data:`environ`, and vice versa). :data:`environb` is only available if :data:`supports_bytes_environ` is ``True``. @@ -291,7 +292,10 @@ process and user. .. function:: getenv(key, default=None) Return the value of the environment variable *key* if it exists, or - *default* if it doesn't. *key*, *default* and the result are str. + *default* if it doesn't. *key*, *default* and the result are str. Note that + since :func:`getenv` uses :data:`os.environ`, the mapping of :func:`getenv` is + similarly also captured on import, and the function may not reflect + future environment changes. On Unix, keys and values are decoded with :func:`sys.getfilesystemencoding` and ``'surrogateescape'`` error handler. Use :func:`os.getenvb` if you @@ -303,7 +307,11 @@ process and user. .. function:: getenvb(key, default=None) Return the value of the environment variable *key* if it exists, or - *default* if it doesn't. *key*, *default* and the result are bytes. + *default* if it doesn't. *key*, *default* and the result are bytes. Note that + since :func:`getenvb` uses :data:`os.environb`, the mapping of :func:`getenvb` is + similarly also captured on import, and the function may not reflect + future environment changes. + :func:`getenvb` is only available if :data:`supports_bytes_environ` is ``True``. @@ -369,7 +377,7 @@ process and user. .. note:: - On Mac OS X, :func:`getgroups` behavior differs somewhat from + On macOS, :func:`getgroups` behavior differs somewhat from other Unix platforms. If the Python interpreter was built with a deployment target of :const:`10.5` or earlier, :func:`getgroups` returns the list of effective group ids associated with the current user process; @@ -509,14 +517,15 @@ process and user. changes to the environment affect subprocesses started with :func:`os.system`, :func:`popen` or :func:`fork` and :func:`execv`. - Assignments to items in ``os.environ`` are automatically translated into + Assignments to items in :data:`os.environ` are automatically translated into corresponding calls to :func:`putenv`; however, calls to :func:`putenv` - don't update ``os.environ``, so it is actually preferable to assign to items - of ``os.environ``. + don't update :data:`os.environ`, so it is actually preferable to assign to items + of :data:`os.environ`. This also applies to :func:`getenv` and :func:`getenvb`, which + respectively use :data:`os.environ` and :data:`os.environb` in their implementations. .. note:: - On some platforms, including FreeBSD and Mac OS X, setting ``environ`` may + On some platforms, including FreeBSD and macOS, setting ``environ`` may cause memory leaks. Refer to the system documentation for :c:func:`putenv`. .. audit-event:: os.putenv key,value os.putenv @@ -554,7 +563,7 @@ process and user. .. availability:: Unix. - .. note:: On Mac OS X, the length of *groups* may not exceed the + .. note:: On macOS, the length of *groups* may not exceed the system-defined maximum number of effective group ids, typically 16. See the documentation for :func:`getgroups` for cases where it may not return the same group list set by calling setgroups(). @@ -711,10 +720,10 @@ process and user. environment affect subprocesses started with :func:`os.system`, :func:`popen` or :func:`fork` and :func:`execv`. - Deletion of items in ``os.environ`` is automatically translated into a + Deletion of items in :data:`os.environ` is automatically translated into a corresponding call to :func:`unsetenv`; however, calls to :func:`unsetenv` - don't update ``os.environ``, so it is actually preferable to delete items of - ``os.environ``. + don't update :data:`os.environ`, so it is actually preferable to delete items of + :data:`os.environ`. .. audit-event:: os.unsetenv key os.unsetenv @@ -1379,11 +1388,11 @@ or `the MSDN `_ on Windo On Linux, if *offset* is given as ``None``, the bytes are read from the current position of *in_fd* and the position of *in_fd* is updated. - The second case may be used on Mac OS X and FreeBSD where *headers* and + The second case may be used on macOS and FreeBSD where *headers* and *trailers* are arbitrary sequences of buffers that are written before and after the data from *in_fd* is written. It returns the same as the first case. - On Mac OS X and FreeBSD, a value of ``0`` for *count* specifies to send until + On macOS and FreeBSD, a value of ``0`` for *count* specifies to send until the end of *in_fd* is reached. All platforms support sockets as *out_fd* file descriptor, and some platforms @@ -2040,7 +2049,8 @@ features: Create a directory named *path* with numeric mode *mode*. - If the directory already exists, :exc:`FileExistsError` is raised. + If the directory already exists, :exc:`FileExistsError` is raised. If a parent + directory in the path does not exist, :exc:`FileNotFoundError` is raised. .. _mkdir_modebits: @@ -2297,7 +2307,7 @@ features: :exc:`IsADirectoryError` or a :exc:`NotADirectoryError` will be raised respectively. If both are directories and *dst* is empty, *dst* will be silently replaced. If *dst* is a non-empty directory, an :exc:`OSError` - is raised. If both are files, *dst* it will be replaced silently if the user + is raised. If both are files, *dst* will be replaced silently if the user has permission. The operation may fail on some Unix flavors if *src* and *dst* are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). @@ -2336,7 +2346,7 @@ features: .. function:: replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None) - Rename the file or directory *src* to *dst*. If *dst* is a directory, + Rename the file or directory *src* to *dst*. If *dst* is a non-empty directory, :exc:`OSError` will be raised. If *dst* exists and is a file, it will be replaced silently if the user has permission. The operation may fail if *src* and *dst* are on different filesystems. If successful, @@ -2789,7 +2799,7 @@ features: String that uniquely identifies the type of the filesystem that contains the file. - On Mac OS systems, the following attributes may also be available: + On macOS systems, the following attributes may also be available: .. attribute:: st_rsize @@ -3544,8 +3554,8 @@ to be ignored. Add a path to the DLL search path. This search path is used when resolving dependencies for imported - extension modules (the module itself is resolved through sys.path), - and also by :mod:`ctypes`. + extension modules (the module itself is resolved through + :data:`sys.path`), and also by :mod:`ctypes`. Remove the directory by calling **close()** on the returned object or using it in a :keyword:`with` statement. @@ -4211,12 +4221,12 @@ written in Python, such as a mail server's external command delivery program. the Standard C function :c:func:`system`, and has the same limitations. Changes to :data:`sys.stdin`, etc. are not reflected in the environment of the executed command. If *command* generates any output, it will be sent to - the interpreter standard output stream. + the interpreter standard output stream. The C standard does not + specify the meaning of the return value of the C function, so the return + value of the Python function is system-dependent. On Unix, the return value is the exit status of the process encoded in the - format specified for :func:`wait`. Note that POSIX does not specify the - meaning of the return value of the C :c:func:`system` function, so the return - value of the Python function is system-dependent. + format specified for :func:`wait`. On Windows, the return value is that returned by the system shell after running *command*. The shell is given by the Windows environment variable @@ -4243,20 +4253,20 @@ written in Python, such as a mail server's external command delivery program. Returns the current global process times. The return value is an object with five attributes: - * :attr:`user` - user time - * :attr:`system` - system time - * :attr:`children_user` - user time of all child processes - * :attr:`children_system` - system time of all child processes - * :attr:`elapsed` - elapsed real time since a fixed point in the past + * :attr:`!user` - user time + * :attr:`!system` - system time + * :attr:`!children_user` - user time of all child processes + * :attr:`!children_system` - system time of all child processes + * :attr:`!elapsed` - elapsed real time since a fixed point in the past For backwards compatibility, this object also behaves like a five-tuple - containing :attr:`user`, :attr:`system`, :attr:`children_user`, - :attr:`children_system`, and :attr:`elapsed` in that order. + containing :attr:`!user`, :attr:`!system`, :attr:`!children_user`, + :attr:`!children_system`, and :attr:`!elapsed` in that order. See the Unix manual page :manpage:`times(2)` and :manpage:`times(3)` manual page on Unix or `the GetProcessTimes MSDN `_ - on Windows. On Windows, only :attr:`user` and :attr:`system` are known; the other attributes are zero. + on Windows. On Windows, only :attr:`!user` and :attr:`!system` are known; the other attributes are zero. .. availability:: Unix, Windows. @@ -4635,7 +4645,7 @@ operating system. .. function:: sched_setparam(pid, param) - Set a scheduling parameters for the process with PID *pid*. A *pid* of 0 means + Set the scheduling parameters for the process with PID *pid*. A *pid* of 0 means the calling process. *param* is a :class:`sched_param` instance. @@ -4868,7 +4878,7 @@ Random numbers .. function:: urandom(size) - Return a string of *size* random bytes suitable for cryptographic use. + Return a bytestring of *size* random bytes suitable for cryptographic use. This function returns random bytes from an OS-specific randomness source. The returned data should be unpredictable enough for cryptographic applications, diff --git a/Doc/library/ossaudiodev.rst b/Doc/library/ossaudiodev.rst index a7d3dac363cdf2..728ee3036057dd 100644 --- a/Doc/library/ossaudiodev.rst +++ b/Doc/library/ossaudiodev.rst @@ -4,6 +4,11 @@ .. module:: ossaudiodev :platform: Linux, FreeBSD :synopsis: Access to OSS-compatible audio devices. + :deprecated: + +.. deprecated:: 3.11 + The :mod:`ossaudiodev` module is deprecated + (see :pep:`PEP 594 <594#ossaudiodev>` for details). -------------- diff --git a/Doc/library/othergui.rst b/Doc/library/othergui.rst deleted file mode 100644 index 48c1f2754111aa..00000000000000 --- a/Doc/library/othergui.rst +++ /dev/null @@ -1,57 +0,0 @@ -.. _other-gui-packages: - -Other Graphical User Interface Packages -======================================= - -Major cross-platform (Windows, Mac OS X, Unix-like) GUI toolkits are -available for Python: - -.. seealso:: - - `PyGObject `_ - PyGObject provides introspection bindings for C libraries using - `GObject `_. One of - these libraries is the `GTK+ 3 `_ widget set. - GTK+ comes with many more widgets than Tkinter provides. An online - `Python GTK+ 3 Tutorial `_ - is available. - - `PyGTK `_ - PyGTK provides bindings for an older version - of the library, GTK+ 2. It provides an object oriented interface that - is slightly higher level than the C one. There are also bindings to - `GNOME `_. An online `tutorial - `_ is available. - - `PyQt `_ - PyQt is a :program:`sip`\ -wrapped binding to the Qt toolkit. Qt is an - extensive C++ GUI application development framework that is - available for Unix, Windows and Mac OS X. :program:`sip` is a tool - for generating bindings for C++ libraries as Python classes, and - is specifically designed for Python. - - `PySide2 `_ - Also known as the Qt for Python project, PySide2 is a newer binding to the - Qt toolkit. It is provided by The Qt Company and aims to provide a - complete port of PySide to Qt 5. Compared to PyQt, its licensing scheme is - friendlier to non-open source applications. - - `wxPython `_ - wxPython is a cross-platform GUI toolkit for Python that is built around - the popular `wxWidgets `_ (formerly wxWindows) - C++ toolkit. It provides a native look and feel for applications on - Windows, Mac OS X, and Unix systems by using each platform's native - widgets where ever possible, (GTK+ on Unix-like systems). In addition to - an extensive set of widgets, wxPython provides classes for online - documentation and context sensitive help, printing, HTML viewing, - low-level device context drawing, drag and drop, system clipboard access, - an XML-based resource format and more, including an ever growing library - of user-contributed modules. - -PyGTK, PyQt, PySide2, and wxPython, all have a modern look and feel and more -widgets than Tkinter. In addition, there are many other GUI toolkits for -Python, both cross-platform, and platform-specific. See the `GUI Programming -`_ page in the Python Wiki for a -much more complete list, and also for links to documents where the -different GUI toolkits are compared. - diff --git a/Doc/library/pathlib-inheritance.svg b/Doc/library/pathlib-inheritance.svg index 49057f678fd7d0..01f8684a2b201c 100644 --- a/Doc/library/pathlib-inheritance.svg +++ b/Doc/library/pathlib-inheritance.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index b6507eb4d6fa2c..8077796ff6f6fd 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -914,7 +914,7 @@ call fails (for example because the path doesn't exist). The children are yielded in arbitrary order, and the special entries ``'.'`` and ``'..'`` are not included. If a file is removed from or added - to the directory after creating the iterator, whether an path object for + to the directory after creating the iterator, whether a path object for that file be included is unspecified. .. method:: Path.lchmod(mode) @@ -1018,8 +1018,9 @@ call fails (for example because the path doesn't exist). Rename this file or directory to the given *target*, and return a new Path instance pointing to *target*. On Unix, if *target* exists and is a file, - it will be replaced silently if the user has permission. *target* can be - either a string or another path object:: + it will be replaced silently if the user has permission. + On Windows, if *target* exists, :exc:`FileExistsError` will be raised. + *target* can be either a string or another path object:: >>> p = Path('foo') >>> p.open('w').write('some text') @@ -1042,7 +1043,7 @@ call fails (for example because the path doesn't exist). Rename this file or directory to the given *target*, and return a new Path instance pointing to *target*. If *target* points to an existing file or - directory, it will be unconditionally replaced. + empty directory, it will be unconditionally replaced. The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, *not* the directory of the Path @@ -1264,7 +1265,7 @@ Below is a table mapping various :mod:`os` functions to their corresponding :func:`os.link` :meth:`Path.hardlink_to` :func:`os.symlink` :meth:`Path.symlink_to` :func:`os.readlink` :meth:`Path.readlink` -:func:`os.path.relpath` :meth:`Path.relative_to` [#]_ +:func:`os.path.relpath` :meth:`PurePath.relative_to` [#]_ :func:`os.stat` :meth:`Path.stat`, :meth:`Path.owner`, :meth:`Path.group` @@ -1279,4 +1280,4 @@ Below is a table mapping various :mod:`os` functions to their corresponding .. rubric:: Footnotes .. [#] :func:`os.path.abspath` does not resolve symbolic links while :meth:`Path.resolve` does. -.. [#] :meth:`Path.relative_to` requires ``self`` to be the subpath of the argument, but :func:`os.path.relpath` does not. +.. [#] :meth:`PurePath.relative_to` requires ``self`` to be the subpath of the argument, but :func:`os.path.relpath` does not. diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index ed1e9712c0e3de..dcd509de56adc4 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -67,14 +67,13 @@ useful than quitting the debugger upon program's exit. before the first line of the module. -The typical usage to break into the debugger from a running program is to -insert :: +The typical usage to break into the debugger is to insert:: import pdb; pdb.set_trace() -at the location you want to break into the debugger. You can then step through -the code following this statement, and continue running without the debugger -using the :pdbcmd:`continue` command. +at the location you want to break into the debugger, and then run the program. +You can then step through the code following this statement, and continue +running without the debugger using the :pdbcmd:`continue` command. .. versionadded:: 3.7 The built-in :func:`breakpoint()`, when called with defaults, can be used @@ -234,7 +233,8 @@ Multiple commands may be entered on a single line, separated by ``;;``. (A single ``;`` is not used as it is the separator for multiple commands in a line that is passed to the Python parser.) No intelligence is applied to separating the commands; the input is split at the first ``;;`` pair, even if it is in the -middle of a quoted string. +middle of a quoted string. A workaround for strings with double semicolons +is to use implicit string concatenation ``';'';'`` or ``";"";"``. .. index:: pair: .pdbrc; file diff --git a/Doc/library/pickle.rst b/Doc/library/pickle.rst index be48561ed10ac8..920df712ea58f7 100644 --- a/Doc/library/pickle.rst +++ b/Doc/library/pickle.rst @@ -147,7 +147,7 @@ to read the pickle produced. earlier versions of Python. * Protocol version 2 was introduced in Python 2.3. It provides much more - efficient pickling of :term:`new-style class`\es. Refer to :pep:`307` for + efficient pickling of :term:`new-style classes `. Refer to :pep:`307` for information about improvements brought by protocol 2. * Protocol version 3 was added in Python 3.0. It has explicit support for @@ -261,7 +261,7 @@ process more convenient: protocol argument is needed. Bytes past the pickled representation of the object are ignored. - Arguments *file*, *fix_imports*, *encoding*, *errors*, *strict* and *buffers* + Arguments *fix_imports*, *encoding*, *errors*, *strict* and *buffers* have the same meaning as in the :class:`Unpickler` constructor. .. versionchanged:: 3.8 @@ -368,7 +368,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`, .. versionadded:: 3.3 - .. method:: reducer_override(self, obj) + .. method:: reducer_override(obj) Special reducer that can be defined in :class:`Pickler` subclasses. This method has priority over any reducer in the :attr:`dispatch_table`. It @@ -494,20 +494,18 @@ What can be pickled and unpickled? The following types can be pickled: -* ``None``, ``True``, and ``False`` - -* integers, floating point numbers, complex numbers +* ``None``, ``True``, and ``False``; -* strings, bytes, bytearrays +* integers, floating-point numbers, complex numbers; -* tuples, lists, sets, and dictionaries containing only picklable objects +* strings, bytes, bytearrays; -* functions defined at the top level of a module (using :keyword:`def`, not - :keyword:`lambda`) +* tuples, lists, sets, and dictionaries containing only picklable objects; -* built-in functions defined at the top level of a module +* functions (built-in and user-defined) accessible from the top level of a + module (using :keyword:`def`, not :keyword:`lambda`); -* classes that are defined at the top level of a module +* classes accessible from the top level of a module; * instances of such classes whose :attr:`~object.__dict__` or the result of calling :meth:`__getstate__` is picklable (see section :ref:`pickle-inst` for @@ -520,14 +518,14 @@ structure may exceed the maximum recursion depth, a :exc:`RecursionError` will b raised in this case. You can carefully raise this limit with :func:`sys.setrecursionlimit`. -Note that functions (built-in and user-defined) are pickled by "fully qualified" -name reference, not by value. [#]_ This means that only the function name is -pickled, along with the name of the module the function is defined in. Neither +Note that functions (built-in and user-defined) are pickled by fully +:term:`qualified name`, not by value. [#]_ This means that only the function name is +pickled, along with the name of the containing module and classes. Neither the function's code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised. [#]_ -Similarly, classes are pickled by named reference, so the same restrictions in +Similarly, classes are pickled by fully qualified name, so the same restrictions in the unpickling environment apply. Note that none of the class's code or data is pickled, so in the following example the class attribute ``attr`` is not restored in the unpickling environment:: @@ -537,7 +535,7 @@ restored in the unpickling environment:: picklestring = pickle.dumps(Foo) -These restrictions are why picklable functions and classes must be defined in +These restrictions are why picklable functions and classes must be defined at the top level of a module. Similarly, when class instances are pickled, their class's code and data are not @@ -569,7 +567,7 @@ implementation of this behaviour:: def save(obj): return (obj.__class__, obj.__dict__) - def load(cls, attributes): + def restore(cls, attributes): obj = cls.__new__(cls) obj.__dict__.update(attributes) return obj @@ -788,14 +786,15 @@ the code :: f = io.BytesIO() p = MyPickler(f) -does the same, but all instances of ``MyPickler`` will by default -share the same dispatch table. The equivalent code using the -:mod:`copyreg` module is :: +does the same but all instances of ``MyPickler`` will by default +share the private dispatch table. On the other hand, the code :: copyreg.pickle(SomeClass, reduce_SomeClass) f = io.BytesIO() p = pickle.Pickler(f) +modifies the global dispatch table shared by all users of the :mod:`copyreg` module. + .. _pickle-state: Handling Stateful Objects @@ -1098,7 +1097,7 @@ Here is an example of an unpickler allowing only few safe classes from the """Helper function analogous to pickle.loads().""" return RestrictedUnpickler(io.BytesIO(s)).load() -A sample usage of our unpickler working has intended:: +A sample usage of our unpickler working as intended:: >>> restricted_loads(pickle.dumps([1, 2, range(15)])) [1, 2, range(0, 15)] @@ -1142,7 +1141,7 @@ For the simplest code, use the :func:`dump` and :func:`load` functions. :: # An arbitrary collection of objects supported by pickle. data = { - 'a': [1, 2.0, 3, 4+6j], + 'a': [1, 2.0, 3+4j], 'b': ("character string", b"byte string"), 'c': {None, True, False} } @@ -1198,6 +1197,6 @@ The following example reads the resulting pickled data. :: operations. .. [#] The limitation on alphanumeric characters is due to the fact - the persistent IDs, in protocol 0, are delimited by the newline + that persistent IDs in protocol 0 are delimited by the newline character. Therefore if any kind of newline characters occurs in - persistent IDs, the resulting pickle will become unreadable. + persistent IDs, the resulting pickled data will become unreadable. diff --git a/Doc/library/pipes.rst b/Doc/library/pipes.rst index 57e27a6acf4b66..1c5bb8bddaafbc 100644 --- a/Doc/library/pipes.rst +++ b/Doc/library/pipes.rst @@ -4,11 +4,17 @@ .. module:: pipes :platform: Unix :synopsis: A Python interface to Unix shell pipelines. + :deprecated: .. sectionauthor:: Moshe Zadka **Source code:** :source:`Lib/pipes.py` +.. deprecated:: 3.11 + The :mod:`pipes` module is deprecated + (see :pep:`PEP 594 <594#pipes>` for details). + Please use the :mod:`subprocess` module instead. + -------------- The :mod:`pipes` module defines a class to abstract the concept of a *pipeline* diff --git a/Doc/library/pkgutil.rst b/Doc/library/pkgutil.rst index 3b17b9a6219870..788a02dcb8922f 100644 --- a/Doc/library/pkgutil.rst +++ b/Doc/library/pkgutil.rst @@ -26,7 +26,7 @@ support. __path__ = extend_path(__path__, __name__) This will add to the package's ``__path__`` all subdirectories of directories - on ``sys.path`` named after the package. This is useful if one wants to + on :data:`sys.path` named after the package. This is useful if one wants to distribute different parts of a single logical package as multiple directories. @@ -128,9 +128,9 @@ support. Yield :term:`finder` objects for the given module name. - If fullname contains a '.', the finders will be for the package + If fullname contains a ``'.'``, the finders will be for the package containing fullname, otherwise they will be all registered top level - finders (i.e. those on both sys.meta_path and sys.path_hooks). + finders (i.e. those on both :data:`sys.meta_path` and :data:`sys.path_hooks`). If the named module is in a package, that package is imported as a side effect of invoking this function. @@ -145,7 +145,7 @@ support. .. function:: iter_modules(path=None, prefix='') Yields :class:`ModuleInfo` for all submodules on *path*, or, if - *path* is ``None``, all top-level modules on ``sys.path``. + *path* is ``None``, all top-level modules on :data:`sys.path`. *path* should be either ``None`` or a list of paths to look for modules in. diff --git a/Doc/library/platform.rst b/Doc/library/platform.rst index be86e568c180ba..dc2d871b47d5ef 100644 --- a/Doc/library/platform.rst +++ b/Doc/library/platform.rst @@ -42,7 +42,7 @@ Cross Platform .. note:: - On Mac OS X (and perhaps other platforms), executable files may be + On macOS (and perhaps other platforms), executable files may be universal files containing multiple architectures. To get at the "64-bitness" of the current interpreter, it is more @@ -53,7 +53,7 @@ Cross Platform .. function:: machine() - Returns the machine type, e.g. ``'i386'``. An empty string is returned if the + Returns the machine type, e.g. ``'AMD64'``. An empty string is returned if the value cannot be determined. @@ -139,7 +139,7 @@ Cross Platform .. function:: release() - Returns the system's release, e.g. ``'2.2.0'`` or ``'NT'`` An empty string is + Returns the system's release, e.g. ``'2.2.0'`` or ``'NT'``. An empty string is returned if the value cannot be determined. @@ -176,7 +176,7 @@ Cross Platform Entries which cannot be determined are set to ``''``. .. versionchanged:: 3.3 - Result changed from a tuple to a namedtuple. + Result changed from a tuple to a :func:`~collections.namedtuple`. Java Platform @@ -201,7 +201,9 @@ Windows Platform Get additional version information from the Windows Registry and return a tuple ``(release, version, csd, ptype)`` referring to OS release, version number, - CSD level (service pack) and OS type (multi/single processor). + CSD level (service pack) and OS type (multi/single processor). Values which + cannot be determined are set to the defaults given as parameters (which all + default to an empty string). As a hint: *ptype* is ``'Uniprocessor Free'`` on single processor NT machines and ``'Multiprocessor Free'`` on multi processor machines. The *'Free'* refers @@ -211,9 +213,9 @@ Windows Platform .. function:: win32_edition() - Returns a string representing the current Windows edition. Possible - values include but are not limited to ``'Enterprise'``, ``'IoTUAP'``, - ``'ServerStandard'``, and ``'nanoserver'``. + Returns a string representing the current Windows edition, or ``None`` if the + value cannot be determined. Possible values include but are not limited to + ``'Enterprise'``, ``'IoTUAP'``, ``'ServerStandard'``, and ``'nanoserver'``. .. versionadded:: 3.8 @@ -225,13 +227,13 @@ Windows Platform .. versionadded:: 3.8 -Mac OS Platform ---------------- +macOS Platform +-------------- .. function:: mac_ver(release='', versioninfo=('','',''), machine='') - Get Mac OS version information and return it as tuple ``(release, versioninfo, + Get macOS version information and return it as tuple ``(release, versioninfo, machine)`` with *versioninfo* being a tuple ``(version, dev_stage, non_release_version)``. diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index ce6d4a85bf5e9d..5ded9661f08014 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -133,7 +133,7 @@ The following classes are available: encoded data, which contains UID (see PList manual). It has one attribute, :attr:`data`, which can be used to retrieve the int value - of the UID. :attr:`data` must be in the range `0 <= data < 2**64`. + of the UID. :attr:`data` must be in the range ``0 <= data < 2**64``. .. versionadded:: 3.8 diff --git a/Doc/library/posix.rst b/Doc/library/posix.rst index ad417a17879c1f..90be191aa2f8d7 100644 --- a/Doc/library/posix.rst +++ b/Doc/library/posix.rst @@ -37,7 +37,7 @@ Large File Support .. sectionauthor:: Steve Clift -Several operating systems (including AIX, HP-UX and Solaris) provide +Several operating systems (including AIX 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 diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index f45c66fd9f46fb..8f374503725ad1 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -42,24 +42,36 @@ The :mod:`pprint` module defines one class: compact=False, sort_dicts=True, underscore_numbers=False) Construct a :class:`PrettyPrinter` instance. This constructor understands - several keyword parameters. An output stream may be set using the *stream* - keyword; the only method used on the stream object is the file protocol's - :meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts - ``sys.stdout``. The - amount of indentation added for each recursive level is specified by *indent*; - the default is one. Other values can cause output to look a little odd, but can - make nesting easier to spot. The number of levels which may be printed is - controlled by *depth*; if the data structure being printed is too deep, the next - contained level is replaced by ``...``. By default, there is no constraint on - the depth of the objects being formatted. The desired output width is - constrained using the *width* parameter; the default is 80 characters. If a - structure cannot be formatted within the constrained width, a best effort will - be made. If *compact* is false (the default) each item of a long sequence - will be formatted on a separate line. If *compact* is true, as many items - as will fit within the *width* will be formatted on each output line. If - *sort_dicts* is true (the default), dictionaries will be formatted with their - keys sorted, otherwise they will display in insertion order. If - *underscore_numbers* is true, integers will be formatted with the + several keyword parameters. + + *stream* (default ``sys.stdout``) is a :term:`file-like object` to + which the output will be written by calling its :meth:`write` method. + + Other values configure the manner in which nesting of complex data + structures is displayed. + + *indent* (default 1) specifies the amount of indentation added for + each nesting level. + + *depth* controls the number of nesting levels which may be printed; if + the data structure being printed is too deep, the next contained level + is replaced by ``...``. By default, there is no constraint on the + depth of the objects being formatted. + + *width* (default 80) specifies the desired maximum number of characters per + line in the output. If a structure cannot be formatted within the width + constraint, a best effort will be made. + + *compact* impacts the way that long sequences (lists, tuples, sets, etc) + are formatted. If *compact* is false (the default) then each item of a + sequence will be formatted on a separate line. If *compact* is true, as + many items as will fit within the *width* will be formatted on each output + line. + + If *sort_dicts* is true (the default), dictionaries will be formatted with + their keys sorted, otherwise they will display in insertion order. + + If *underscore_numbers* is true, integers will be formatted with the ``_`` character for a thousands separator, otherwise underscores are not displayed (the default). diff --git a/Doc/library/profile.rst b/Doc/library/profile.rst index 774d46d0e96247..cf324a57e79fdf 100644 --- a/Doc/library/profile.rst +++ b/Doc/library/profile.rst @@ -622,7 +622,7 @@ procedure can be used to obtain a better constant for a given platform (see The method executes the number of Python calls given by the argument, directly and again under the profiler, measuring the time for both. It then computes the hidden overhead per profiler event, and returns that as a float. For example, -on a 1.8Ghz Intel Core i5 running Mac OS X, and using Python's time.process_time() as +on a 1.8Ghz Intel Core i5 running macOS, and using Python's time.process_time() as the timer, the magical number is about 4.04e-6. The object of this exercise is to get a fairly consistent result. If your diff --git a/Doc/library/pty.rst b/Doc/library/pty.rst index 73d4f102fd4d8e..7f4da41e93802d 100644 --- a/Doc/library/pty.rst +++ b/Doc/library/pty.rst @@ -2,8 +2,8 @@ ======================================== .. module:: pty - :platform: Linux - :synopsis: Pseudo-Terminal Handling for Linux. + :platform: Unix + :synopsis: Pseudo-Terminal Handling for Unix. .. moduleauthor:: Steen Lumholt .. sectionauthor:: Moshe Zadka @@ -16,9 +16,9 @@ The :mod:`pty` module defines operations for handling the pseudo-terminal concept: starting another process and being able to write to and read from its controlling terminal programmatically. -Because pseudo-terminal handling is highly platform dependent, there is code to -do it only for Linux. (The Linux code is supposed to work on other platforms, -but hasn't been tested yet.) +Pseudo-terminal handling is highly platform dependent. This code is mainly +tested on Linux, FreeBSD, and macOS (it is supposed to work on other POSIX +platforms but it's not been thoroughly tested). The :mod:`pty` module defines the following functions: @@ -47,10 +47,14 @@ The :mod:`pty` module defines the following functions: spawned behind the pty will eventually terminate, and when it does *spawn* will return. + A loop copies STDIN of the current process to the child and data received + from the child to STDOUT of the current process. It is not signaled to the + child if STDIN of the current process closes down. + The functions *master_read* and *stdin_read* are passed a file descriptor which they should read from, and they should always return a byte string. In order to force spawn to return before the child process exits an - :exc:`OSError` should be thrown. + empty byte array should be returned to signal end of file. The default implementation for both functions will read and return up to 1024 bytes each time the function is called. The *master_read* callback is passed @@ -65,10 +69,6 @@ The :mod:`pty` module defines the following functions: process will quit without any input, *spawn* will then loop forever. If *master_read* signals EOF the same behavior results (on linux at least). - If both callbacks signal EOF then *spawn* will probably never return, unless - *select* throws an error on your platform when passed three empty lists. This - is a bug, documented in `issue 26228 `_. - Return the exit status value from :func:`os.waitpid` on the child process. :func:`waitstatus_to_exitcode` can be used to convert the exit status into diff --git a/Doc/library/pydoc.rst b/Doc/library/pydoc.rst index f956b9d2176187..94daf4a58f9c24 100644 --- a/Doc/library/pydoc.rst +++ b/Doc/library/pydoc.rst @@ -18,7 +18,7 @@ The :mod:`pydoc` module automatically generates documentation from Python modules. The documentation can be presented as pages of text on the console, -served to a Web browser, or saved to HTML files. +served to a web browser, or saved to HTML files. For modules, classes, functions and methods, the displayed documentation is derived from the docstring (i.e. the :attr:`__doc__` attribute) of the object, @@ -65,9 +65,9 @@ manner similar to the Unix :program:`man` command. The synopsis line of a module is the first line of its documentation string. You can also use :program:`pydoc` to start an HTTP server on the local machine -that will serve documentation to visiting Web browsers. :program:`pydoc -p 1234` +that will serve documentation to visiting web browsers. :program:`pydoc -p 1234` will start a HTTP server on port 1234, allowing you to browse the -documentation at ``http://localhost:1234/`` in your preferred Web browser. +documentation at ``http://localhost:1234/`` in your preferred web browser. Specifying ``0`` as the port number will select an arbitrary unused port. :program:`pydoc -n ` will start the server listening at the given diff --git a/Doc/library/queue.rst b/Doc/library/queue.rst index 0ec5900bef5bbf..540dde9e154e80 100644 --- a/Doc/library/queue.rst +++ b/Doc/library/queue.rst @@ -138,7 +138,7 @@ provide the public methods described below. .. method:: Queue.put_nowait(item) - Equivalent to ``put(item, False)``. + Equivalent to ``put(item, block=False)``. .. method:: Queue.get(block=True, timeout=None) @@ -201,15 +201,14 @@ Example of how to wait for enqueued tasks to be completed:: print(f'Finished {item}') q.task_done() - # turn-on the worker thread + # Turn-on the worker thread. threading.Thread(target=worker, daemon=True).start() - # send thirty task requests to the worker + # Send thirty task requests to the worker. for item in range(30): q.put(item) - print('All task requests sent\n', end='') - # block until all tasks are done + # Block until all tasks are done. q.join() print('All work completed') @@ -249,7 +248,7 @@ SimpleQueue Objects .. method:: SimpleQueue.put_nowait(item) - Equivalent to ``put(item)``, provided for compatibility with + Equivalent to ``put(item, block=False)``, provided for compatibility with :meth:`Queue.put_nowait`. diff --git a/Doc/library/random.rst b/Doc/library/random.rst index e924127d8b828d..b9c33af59c8fa7 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -512,7 +512,7 @@ between the effects of a drug versus a placebo:: Simulation of arrival times and service deliveries for a multiserver queue:: - from heapq import heappush, heappop + from heapq import heapify, heapreplace from random import expovariate, gauss from statistics import mean, quantiles @@ -524,14 +524,15 @@ Simulation of arrival times and service deliveries for a multiserver queue:: waits = [] arrival_time = 0.0 servers = [0.0] * num_servers # time when each server becomes available - for i in range(100_000): + heapify(servers) + for i in range(1_000_000): arrival_time += expovariate(1.0 / average_arrival_interval) - next_server_available = heappop(servers) + next_server_available = servers[0] wait = max(0.0, next_server_available - arrival_time) waits.append(wait) - service_duration = gauss(average_service_time, stdev_service_time) + service_duration = max(0.0, gauss(average_service_time, stdev_service_time)) service_completed = arrival_time + wait + service_duration - heappush(servers, service_completed) + heapreplace(servers, service_completed) print(f'Mean wait: {mean(waits):.1f} Max wait: {max(waits):.1f}') print('Quartiles:', [round(q, 1) for q in quantiles(waits)]) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 9abbd8ba73616e..fd12241312211d 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -602,40 +602,14 @@ functions are simplified versions of the full featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form. + +Flags +^^^^^ + .. versionchanged:: 3.6 Flag constants are now instances of :class:`RegexFlag`, which is a subclass of :class:`enum.IntFlag`. -.. function:: compile(pattern, flags=0) - - Compile a regular expression pattern into a :ref:`regular expression object - `, which can be used for matching using its - :func:`~Pattern.match`, :func:`~Pattern.search` and other methods, described - below. - - The expression's behaviour can be modified by specifying a *flags* value. - Values can be any of the following variables, combined using bitwise OR (the - ``|`` operator). - - The sequence :: - - prog = re.compile(pattern) - result = prog.match(string) - - is equivalent to :: - - result = re.match(pattern, string) - - but using :func:`re.compile` and saving the resulting regular expression - object for reuse is more efficient when the expression will be used several - times in a single program. - - .. note:: - - The compiled versions of the most recent patterns passed to - :func:`re.compile` and the module-level matching functions are cached, so - programs that use only a few regular expressions at a time needn't worry - about compiling regular expressions. .. data:: A @@ -744,6 +718,41 @@ form. Corresponds to the inline flag ``(?x)``. +Functions +^^^^^^^^^ + +.. function:: compile(pattern, flags=0) + + Compile a regular expression pattern into a :ref:`regular expression object + `, which can be used for matching using its + :func:`~Pattern.match`, :func:`~Pattern.search` and other methods, described + below. + + The expression's behaviour can be modified by specifying a *flags* value. + Values can be any of the following variables, combined using bitwise OR (the + ``|`` operator). + + The sequence :: + + prog = re.compile(pattern) + result = prog.match(string) + + is equivalent to :: + + result = re.match(pattern, string) + + but using :func:`re.compile` and saving the resulting regular expression + object for reuse is more efficient when the expression will be used several + times in a single program. + + .. note:: + + The compiled versions of the most recent patterns passed to + :func:`re.compile` and the module-level matching functions are cached, so + programs that use only a few regular expressions at a time needn't worry + about compiling regular expressions. + + .. function:: search(pattern, string, flags=0) Scan through *string* looking for the first location where the regular expression @@ -824,10 +833,20 @@ form. .. function:: findall(pattern, string, flags=0) Return all non-overlapping matches of *pattern* in *string*, as a list of - strings. The *string* is scanned left-to-right, and matches are returned in - the order found. If one or more groups are present in the pattern, return a - list of groups; this will be a list of tuples if the pattern has more than - one group. Empty matches are included in the result. + strings or tuples. The *string* is scanned left-to-right, and matches + are returned in the order found. Empty matches are included in the result. + + The result depends on the number of capturing groups in the pattern. + If there are no groups, return a list of strings matching the whole + pattern. If there is exactly one group, return a list of strings + matching that group. If multiple groups are present, return a list + of tuples of strings matching the groups. Non-capturing groups do not + affect the form of the result. + + >>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') + ['foot', 'fell', 'fastest'] + >>> re.findall(r'(\w+)=(\d+)', 'set width=20 and height=10') + [('width', '20'), ('height', '10')] .. versionchanged:: 3.7 Non-empty matches can now start just after a previous empty match. @@ -931,8 +950,8 @@ form. This is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it. For example:: - >>> print(re.escape('http://www.python.org')) - http://www\.python\.org + >>> print(re.escape('https://www.python.org')) + https://www\.python\.org >>> legal_chars = string.ascii_lowercase + string.digits + "!#$%&'*+-.^_`|~:" >>> print('[%s]+' % re.escape(legal_chars)) @@ -965,6 +984,9 @@ form. Clear the regular expression cache. +Exceptions +^^^^^^^^^^ + .. exception:: error(msg, pattern=None, pos=None) Exception raised when a string passed to one of the functions here is not a @@ -1562,7 +1584,7 @@ find all of the adverbs in some text, they might use :func:`findall` in the following manner:: >>> text = "He was carefully disguised but captured quickly by police." - >>> re.findall(r"\w+ly", text) + >>> re.findall(r"\w+ly\b", text) ['carefully', 'quickly'] @@ -1576,7 +1598,7 @@ a writer wanted to find all of the adverbs *and their positions* in some text, they would use :func:`finditer` in the following manner:: >>> text = "He was carefully disguised but captured quickly by police." - >>> for m in re.finditer(r"\w+ly", text): + >>> for m in re.finditer(r"\w+ly\b", text): ... print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0))) 07-16: carefully 40-47: quickly diff --git a/Doc/library/runpy.rst b/Doc/library/runpy.rst index af35e81a2d4523..26a4f1435214e8 100644 --- a/Doc/library/runpy.rst +++ b/Doc/library/runpy.rst @@ -93,7 +93,7 @@ The :mod:`runpy` module provides two functions: run this way, as well as ensuring the real module name is always accessible as ``__spec__.name``. -.. function:: run_path(file_path, init_globals=None, run_name=None) +.. function:: run_path(path_name, init_globals=None, run_name=None) .. index:: module: __main__ @@ -140,7 +140,7 @@ The :mod:`runpy` module provides two functions: A number of alterations are also made to the :mod:`sys` module. Firstly, ``sys.path`` may be altered as described above. ``sys.argv[0]`` is updated - with the value of ``file_path`` and ``sys.modules[__name__]`` is updated + with the value of ``path_name`` and ``sys.modules[__name__]`` is updated with a temporary module object for the module being executed. All modifications to items in :mod:`sys` are reverted before the function returns. diff --git a/Doc/library/secrets.rst b/Doc/library/secrets.rst index afa8e2d385fa46..eda4616da5ec91 100644 --- a/Doc/library/secrets.rst +++ b/Doc/library/secrets.rst @@ -129,7 +129,7 @@ Other functions .. function:: compare_digest(a, b) Return ``True`` if strings *a* and *b* are equal, otherwise ``False``, - in such a way as to reduce the risk of + using a "constant-time compare" to reduce the risk of `timing attacks `_. See :func:`hmac.compare_digest` for additional details. @@ -193,7 +193,7 @@ suitable for password recovery applications: .. testcode:: import secrets - url = 'https://mydomain.com/reset=' + secrets.token_urlsafe() + url = 'https://example.com/reset=' + secrets.token_urlsafe() diff --git a/Doc/library/security_warnings.rst b/Doc/library/security_warnings.rst new file mode 100644 index 00000000000000..26b015c0f8fc7a --- /dev/null +++ b/Doc/library/security_warnings.rst @@ -0,0 +1,34 @@ +.. _security-warnings: + +.. index:: single: security considerations + +Security Considerations +======================= + +The following modules have specific security considerations: + +* :mod:`base64`: :ref:`base64 security considerations ` in + :rfc:`4648` +* :mod:`cgi`: :ref:`CGI security considerations ` +* :mod:`hashlib`: :ref:`all constructors take a "usedforsecurity" keyword-only + argument disabling known insecure and blocked algorithms + ` +* :mod:`http.server` is not suitable for production use, only implementing + basic security checks +* :mod:`logging`: :ref:`Logging configuration uses eval() + ` +* :mod:`multiprocessing`: :ref:`Connection.recv() uses pickle + ` +* :mod:`pickle`: :ref:`Restricting globals in pickle ` +* :mod:`random` shouldn't be used for security purposes, use :mod:`secrets` + instead +* :mod:`shelve`: :ref:`shelve is based on pickle and thus unsuitable for + dealing with untrusted sources ` +* :mod:`ssl`: :ref:`SSL/TLS security considerations ` +* :mod:`subprocess`: :ref:`Subprocess security considerations + ` +* :mod:`tempfile`: :ref:`mktemp is deprecated due to vulnerability to race + conditions ` +* :mod:`xml`: :ref:`XML vulnerabilities ` +* :mod:`zipfile`: :ref:`maliciously prepared .zip files can cause disk volume + exhaustion ` diff --git a/Doc/library/select.rst b/Doc/library/select.rst index a354187c266c7f..46b5ff8b6d5863 100644 --- a/Doc/library/select.rst +++ b/Doc/library/select.rst @@ -534,7 +534,7 @@ https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2 | :const:`KQ_FILTER_PROC` | Watch for events on a process id | +---------------------------+---------------------------------------------+ | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device | - | | [not available on Mac OS X] | + | | [not available on macOS] | +---------------------------+---------------------------------------------+ | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is | | | delivered to the process | @@ -626,7 +626,7 @@ https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child | +----------------------------+--------------------------------------------+ - :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X): + :const:`KQ_FILTER_NETDEV` filter flags (not available on macOS): +----------------------------+--------------------------------------------+ | Constant | Meaning | diff --git a/Doc/library/shelve.rst b/Doc/library/shelve.rst index 07caf91d5b7d99..684f239ef06fa8 100644 --- a/Doc/library/shelve.rst +++ b/Doc/library/shelve.rst @@ -54,13 +54,16 @@ lots of shared sub-objects. The keys are ordinary strings. with shelve.open('spam') as db: db['eggs'] = 'eggs' +.. _shelve-security: + .. warning:: Because the :mod:`shelve` module is backed by :mod:`pickle`, it is insecure to load a shelf from an untrusted source. Like with pickle, loading a shelf can execute arbitrary code. -Shelf objects support all methods supported by dictionaries. This eases the +Shelf objects support most of methods and operations supported by dictionaries +(except copying, constructors and operators ``|`` and ``|=``). This eases the transition from dictionary based scripts to those requiring persistent storage. Two additional methods are supported: diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst index d5080da15bba41..193c01006171e2 100644 --- a/Doc/library/shutil.rst +++ b/Doc/library/shutil.rst @@ -160,7 +160,8 @@ Directory and files operations Copies the file *src* to the file or directory *dst*. *src* and *dst* should be :term:`path-like objects ` or strings. If *dst* specifies a directory, the file will be copied into *dst* using the - base filename from *src*. Returns the path to the newly created file. + base filename from *src*. If *dst* specifies a file that already exists, + it will be replaced. Returns the path to the newly created file. If *follow_symlinks* is false, and *src* is a symbolic link, *dst* will be created as a symbolic link. If *follow_symlinks* @@ -230,9 +231,8 @@ Directory and files operations dirs_exist_ok=False) Recursively copy an entire directory tree rooted at *src* to a directory - named *dst* and return the destination directory. *dirs_exist_ok* dictates - whether to raise an exception in case *dst* or any missing parent directory - already exists. + named *dst* and return the destination directory. All intermediate + directories needed to contain *dst* will also be created by default. Permissions and times of directories are copied with :func:`copystat`, individual files are copied using :func:`~shutil.copy2`. @@ -263,8 +263,14 @@ Directory and files operations If *copy_function* is given, it must be a callable that will be used to copy each file. It will be called with the source path and the destination path -   as arguments. By default, :func:`~shutil.copy2` is used, but any function -   that supports the same signature (like :func:`~shutil.copy`) can be used. + as arguments. By default, :func:`~shutil.copy2` is used, but any function + that supports the same signature (like :func:`~shutil.copy`) can be used. + + If *dirs_exist_ok* is false (the default) and *dst* already exists, a + :exc:`FileExistsError` is raised. If *dirs_exist_ok* is true, the copying + operation will continue if it encounters existing directories, and files + within the *dst* tree will be overwritten by corresponding files from the + *src* tree. .. audit-event:: shutil.copytree src,dst shutil.copytree @@ -275,7 +281,7 @@ Directory and files operations .. versionchanged:: 3.2 Added the *copy_function* argument to be able to provide a custom copy function. - Added the *ignore_dangling_symlinks* argument to silent dangling symlinks + Added the *ignore_dangling_symlinks* argument to silence dangling symlinks errors when *symlinks* is false. .. versionchanged:: 3.8 @@ -470,42 +476,7 @@ file then shutil will silently fallback on using less efficient copytree example ~~~~~~~~~~~~~~~~ -This example is the implementation of the :func:`copytree` function, described -above, with the docstring omitted. It demonstrates many of the other functions -provided by this module. :: - - def copytree(src, dst, symlinks=False): - names = os.listdir(src) - os.makedirs(dst) - errors = [] - for name in names: - srcname = os.path.join(src, name) - dstname = os.path.join(dst, name) - try: - if symlinks and os.path.islink(srcname): - linkto = os.readlink(srcname) - os.symlink(linkto, dstname) - elif os.path.isdir(srcname): - copytree(srcname, dstname, symlinks) - else: - copy2(srcname, dstname) - # XXX What about devices, sockets etc.? - except OSError as why: - errors.append((srcname, dstname, str(why))) - # catch the Error from the recursive copytree so that we can - # continue with other files - except Error as err: - errors.extend(err.args[0]) - try: - copystat(src, dst) - except OSError as why: - # can't copy file access times on Windows - if why.winerror is None: - errors.extend((src, dst, str(why))) - if errors: - raise Error(errors) - -Another example that uses the :func:`ignore_patterns` helper:: +An example that uses the :func:`ignore_patterns` helper:: from shutil import copytree, ignore_patterns @@ -595,6 +566,10 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. .. audit-event:: shutil.make_archive base_name,format,root_dir,base_dir shutil.make_archive + .. note:: + + This function is not thread-safe. + .. versionchanged:: 3.8 The modern pax (POSIX.1-2001) format is now used instead of the legacy GNU format for archives created with ``format="tar"``. @@ -655,10 +630,16 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. .. audit-event:: shutil.unpack_archive filename,extract_dir,format shutil.unpack_archive + .. warning:: + + Never extract archives from untrusted sources without prior inspection. + It is possible that files are created outside of the path specified in + the *extract_dir* argument, e.g. members that have absolute filenames + starting with "/" or filenames with two dots "..". + .. versionchanged:: 3.7 Accepts a :term:`path-like object` for *filename* and *extract_dir*. - .. function:: register_unpack_format(name, extensions, function[, extra_args[, description]]) Registers an unpack format. *name* is the name of the format and diff --git a/Doc/library/signal.rst b/Doc/library/signal.rst index 84a569d03eb293..0aa5996b89423b 100644 --- a/Doc/library/signal.rst +++ b/Doc/library/signal.rst @@ -46,6 +46,9 @@ This has consequences: arbitrary amount of time, regardless of any signals received. The Python signal handlers will be called when the calculation finishes. +* If the handler raises an exception, it will be raised "out of thin air" in + the main thread. See the :ref:`note below ` for a + discussion. .. _signals-and-threads: @@ -673,7 +676,75 @@ case, wrap your entry point to catch this exception as follows:: if __name__ == '__main__': main() -Do not set :const:`SIGPIPE`'s disposition to :const:`SIG_DFL` -in order to avoid :exc:`BrokenPipeError`. Doing that would cause -your program to exit unexpectedly also whenever any socket connection -is interrupted while your program is still writing to it. +Do not set :const:`SIGPIPE`'s disposition to :const:`SIG_DFL` in +order to avoid :exc:`BrokenPipeError`. Doing that would cause +your program to exit unexpectedly whenever any socket +connection is interrupted while your program is still writing to +it. + +.. _handlers-and-exceptions: + +Note on Signal Handlers and Exceptions +-------------------------------------- + +If a signal handler raises an exception, the exception will be propagated to +the main thread and may be raised after any :term:`bytecode` instruction. Most +notably, a :exc:`KeyboardInterrupt` may appear at any point during execution. +Most Python code, including the standard library, cannot be made robust against +this, and so a :exc:`KeyboardInterrupt` (or any other exception resulting from +a signal handler) may on rare occasions put the program in an unexpected state. + +To illustrate this issue, consider the following code:: + + class SpamContext: + def __init__(self): + self.lock = threading.Lock() + + def __enter__(self): + # If KeyboardInterrupt occurs here, everything is fine + self.lock.acquire() + # If KeyboardInterrupt occcurs here, __exit__ will not be called + ... + # KeyboardInterrupt could occur just before the function returns + + def __exit__(self, exc_type, exc_val, exc_tb): + ... + self.lock.release() + +For many programs, especially those that merely want to exit on +:exc:`KeyboardInterrupt`, this is not a problem, but applications that are +complex or require high reliability should avoid raising exceptions from signal +handlers. They should also avoid catching :exc:`KeyboardInterrupt` as a means +of gracefully shutting down. Instead, they should install their own +:const:`SIGINT` handler. Below is an example of an HTTP server that avoids +:exc:`KeyboardInterrupt`:: + + import signal + import socket + from selectors import DefaultSelector, EVENT_READ + from http.server import HTTPServer, SimpleHTTPRequestHandler + + interrupt_read, interrupt_write = socket.socketpair() + + def handler(signum, frame): + print('Signal handler called with signal', signum) + interrupt_write.send(b'\0') + signal.signal(signal.SIGINT, handler) + + def serve_forever(httpd): + sel = DefaultSelector() + sel.register(interrupt_read, EVENT_READ) + sel.register(httpd, EVENT_READ) + + while True: + for key, _ in sel.select(): + if key.fileobj == interrupt_read: + interrupt_read.recv(1) + return + if key.fileobj == httpd: + httpd.handle_request() + + print("Serving on port 8000") + httpd = HTTPServer(('', 8000), SimpleHTTPRequestHandler) + serve_forever(httpd) + print("Shutdown...") diff --git a/Doc/library/site.rst b/Doc/library/site.rst index 2e3646f6a74f80..e2ad3c48f9754e 100644 --- a/Doc/library/site.rst +++ b/Doc/library/site.rst @@ -32,7 +32,7 @@ It starts by constructing up to four directories from a head and a tail part. For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads are skipped. For the tail part, it uses the empty string and then :file:`lib/site-packages` (on Windows) or -:file:`lib/python{X.Y}/site-packages` (on Unix and Macintosh). For each +:file:`lib/python{X.Y}/site-packages` (on Unix and macOS). For each of the distinct head-tail combinations, it sees if it refers to an existing directory, and if so, adds it to ``sys.path`` and also inspects the newly added path for configuration files. @@ -176,8 +176,8 @@ Module contents Path to the user site-packages for the running Python. Can be ``None`` if :func:`getusersitepackages` hasn't been called yet. Default value is - :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac - OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac + :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework + macOS builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for macOS framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages` on Windows. This directory is a site directory, which means that :file:`.pth` files in it will be processed. @@ -187,8 +187,8 @@ Module contents Path to the base directory for the user site-packages. Can be ``None`` if :func:`getuserbase` hasn't been called yet. Default value is - :file:`~/.local` for UNIX and Mac OS X non-framework builds, - :file:`~/Library/Python/{X.Y}` for Mac framework builds, and + :file:`~/.local` for UNIX and macOS non-framework builds, + :file:`~/Library/Python/{X.Y}` for macOS framework builds, and :file:`{%APPDATA%}\\Python` for Windows. This value is used by Distutils to compute the installation directories for scripts, data files, Python modules, etc. for the :ref:`user installation scheme `. diff --git a/Doc/library/smtpd.rst b/Doc/library/smtpd.rst index d84e74a8ceaaff..8fd009e5b23659 100644 --- a/Doc/library/smtpd.rst +++ b/Doc/library/smtpd.rst @@ -3,6 +3,7 @@ .. module:: smtpd :synopsis: A SMTP server implementation in Python. + :deprecated: .. moduleauthor:: Barry Warsaw .. sectionauthor:: Moshe Zadka @@ -13,11 +14,12 @@ This module offers several classes to implement SMTP (email) servers. -.. seealso:: - - The `aiosmtpd `_ package is a recommended - replacement for this module. It is based on :mod:`asyncio` and provides a - more straightforward API. :mod:`smtpd` should be considered deprecated. +.. deprecated:: 3.6 + :mod:`smtpd` will be removed in Python 3.12 + (see :pep:`PEP 594 <594#smtpd>` for details). + The `aiosmtpd `_ package is a recommended + replacement for this module. It is based on :mod:`asyncio` and provides a + more straightforward API. Several server implementations are present; one is a generic do-nothing implementation, which can be overridden, while the other two offer diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index 52220f7a7f30ac..aaab6b11d3bbe5 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -15,7 +15,7 @@ -------------- The :mod:`smtplib` module defines an SMTP client session object that can be used -to send mail to any Internet machine with an SMTP or ESMTP listener daemon. For +to send mail to any internet machine with an SMTP or ESMTP listener daemon. For details of SMTP and ESMTP operation, consult :rfc:`821` (Simple Mail Transfer Protocol) and :rfc:`1869` (SMTP Service Extensions). diff --git a/Doc/library/sndhdr.rst b/Doc/library/sndhdr.rst index 6bfa9a9fd210be..3ca36f270dade1 100644 --- a/Doc/library/sndhdr.rst +++ b/Doc/library/sndhdr.rst @@ -3,6 +3,7 @@ .. module:: sndhdr :synopsis: Determine type of a sound file. + :deprecated: .. sectionauthor:: Fred L. Drake, Jr. .. Based on comments in the module source file. @@ -13,6 +14,10 @@ single: A-LAW single: u-LAW +.. deprecated:: 3.11 + The :mod:`sndhdr` module is deprecated + (see :pep:`PEP 594 <594#sndhdr>` for details and alternatives). + -------------- The :mod:`sndhdr` provides utility functions which attempt to determine the type diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst old mode 100755 new mode 100644 index 8a1fc96e0362e2..4af78e415f3d71 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -66,7 +66,7 @@ created. Socket addresses are represented as follows: .. _host_port: - A pair ``(host, port)`` is used for the :const:`AF_INET` address family, - where *host* is a string representing either a hostname in Internet domain + where *host* is a string representing either a hostname in internet domain notation like ``'daring.cwi.nl'`` or an IPv4 address like ``'100.50.200.5'``, and *port* is an integer. @@ -197,11 +197,15 @@ created. Socket addresses are represented as follows: - *addr* - Optional bytes-like object specifying the hardware physical address, whose interpretation depends on the device. + .. availability:: Linux >= 2.2. + - :const:`AF_QIPCRTR` is a Linux-only socket based interface for communicating with services running on co-processors in Qualcomm platforms. The address family is represented as a ``(node, port)`` tuple where the *node* and *port* are non-negative integers. + .. availability:: Linux >= 4.7. + .. versionadded:: 3.8 - :const:`IPPROTO_UDPLITE` is a variant of UDP which allows you to specify @@ -229,9 +233,9 @@ resolution and/or the host configuration. For deterministic behavior use a numeric address in *host* portion. All errors raise exceptions. The normal exceptions for invalid argument types -and out-of-memory conditions can be raised; starting from Python 3.3, errors +and out-of-memory conditions can be raised. Errors related to socket or address semantics raise :exc:`OSError` or one of its -subclasses (they used to raise :exc:`socket.error`). +subclasses. Non-blocking mode is supported through :meth:`~socket.setblocking`. A generalization of this based on timeouts is supported through @@ -379,6 +383,11 @@ Constants On Windows, ``TCP_KEEPIDLE``, ``TCP_KEEPINTVL`` appear if run-time Windows supports. + .. versionchanged:: 3.10 + ``IP_RECVTOS`` was added. + Added ``TCP_KEEPALIVE``. On MacOS this constant can be used in the same + way that ``TCP_KEEPIDLE`` is used on Linux. + .. data:: AF_CAN PF_CAN SOL_CAN_* @@ -511,7 +520,7 @@ Constants .. data:: AF_LINK - .. availability:: BSD, OSX. + .. availability:: BSD, macOS. .. versionadded:: 3.4 @@ -553,7 +562,7 @@ Creating sockets The following functions all create :ref:`socket objects `. -.. function:: socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) +.. class:: socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) Create a new socket using the given address family, socket type and protocol number. The address family should be :const:`AF_INET` (the default), @@ -635,7 +644,7 @@ The following functions all create :ref:`socket objects `. .. function:: create_connection(address[, timeout[, source_address]]) - Connect to a TCP service listening on the Internet *address* (a 2-tuple + Connect to a TCP service listening on the internet *address* (a 2-tuple ``(host, port)``), and return the socket object. This is a higher-level function than :meth:`socket.connect`: if *host* is a non-numeric hostname, it will try to resolve it for both :data:`AF_INET` and :data:`AF_INET6`, @@ -785,9 +794,9 @@ The :mod:`socket` module also offers various network-related services: system if IPv6 isn't enabled):: >>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP) - [(socket.AF_INET6, socket.SOCK_STREAM, + [(, , 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)), - (socket.AF_INET, socket.SOCK_STREAM, + (, , 6, '', ('93.184.216.34', 80))] .. versionchanged:: 3.2 @@ -803,8 +812,9 @@ The :mod:`socket` module also offers various network-related services: it is interpreted as the local host. To find the fully qualified name, the hostname returned by :func:`gethostbyaddr` is checked, followed by aliases for the host, if available. The first name which includes a period is selected. In - case no fully qualified domain name is available, the hostname as returned by - :func:`gethostname` is returned. + case no fully qualified domain name is available and *name* was provided, + it is returned unchanged. If *name* was empty or equal to ``'0.0.0.0'``, + the hostname from :func:`gethostname` is returned. .. function:: gethostbyname(hostname) @@ -821,8 +831,8 @@ The :mod:`socket` module also offers various network-related services: .. function:: gethostbyname_ex(hostname) Translate a host name to IPv4 address format, extended interface. Return a - triple ``(hostname, aliaslist, ipaddrlist)`` where *hostname* is the primary - host name responding to the given *ip_address*, *aliaslist* is a (possibly + triple ``(hostname, aliaslist, ipaddrlist)`` where *hostname* is the host's + primary host name, *aliaslist* is a (possibly empty) list of alternative host names for the same address, and *ipaddrlist* is a list of IPv4 addresses for the same interface on the same host (often but not always a single address). :func:`gethostbyname_ex` does not support IPv6 name @@ -872,7 +882,7 @@ The :mod:`socket` module also offers various network-related services: .. function:: getprotobyname(protocolname) - Translate an Internet protocol name (for example, ``'icmp'``) to a constant + Translate an internet protocol name (for example, ``'icmp'``) to a constant suitable for passing as the (optional) third argument to the :func:`.socket` function. This is usually only needed for sockets opened in "raw" mode (:const:`SOCK_RAW`); for the normal socket modes, the correct protocol is chosen @@ -881,7 +891,7 @@ The :mod:`socket` module also offers various network-related services: .. function:: getservbyname(servicename[, protocolname]) - Translate an Internet service name and protocol name to a port number for that + Translate an internet service name and protocol name to a port number for that service. The optional protocol name, if given, should be ``'tcp'`` or ``'udp'``, otherwise any protocol will match. @@ -890,7 +900,7 @@ The :mod:`socket` module also offers various network-related services: .. function:: getservbyport(port[, protocolname]) - Translate an Internet port number and protocol name to a service name for that + Translate an internet port number and protocol name to a service name for that service. The optional protocol name, if given, should be ``'tcp'`` or ``'udp'``, otherwise any protocol will match. diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst index 232c0616d925dc..b65a3e8fb2b975 100644 --- a/Doc/library/socketserver.rst +++ b/Doc/library/socketserver.rst @@ -15,7 +15,7 @@ There are four basic concrete server classes: .. class:: TCPServer(server_address, RequestHandlerClass, bind_and_activate=True) - This uses the Internet TCP protocol, which provides for + This uses the internet TCP protocol, which provides for continuous streams of data between the client and server. If *bind_and_activate* is true, the constructor automatically attempts to invoke :meth:`~BaseServer.server_bind` and @@ -263,7 +263,7 @@ Server Objects The address on which the server is listening. The format of addresses varies depending on the protocol family; see the documentation for the :mod:`socket` module - for details. For Internet protocols, this is a tuple containing a string giving + for details. For internet protocols, this is a tuple containing a string giving the address, and an integer port number: ``('127.0.0.1', 80)``, for example. diff --git a/Doc/library/spwd.rst b/Doc/library/spwd.rst index c6cad2a3c32849..40f50de07babff 100644 --- a/Doc/library/spwd.rst +++ b/Doc/library/spwd.rst @@ -4,6 +4,11 @@ .. module:: spwd :platform: Unix :synopsis: The shadow password database (getspnam() and friends). + :deprecated: + +.. deprecated:: 3.11 + The :mod:`spwd` module is deprecated + (see :pep:`PEP 594 <594#spwd>` for details and alternatives). -------------- diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index d0f28db12fda16..a430144bd5e750 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -17,20 +17,21 @@ SQLite for internal data storage. It's also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle. -The sqlite3 module was written by Gerhard Häring. It provides a SQL interface +The sqlite3 module was written by Gerhard Häring. It provides an SQL interface compliant with the DB-API 2.0 specification described by :pep:`249`, and requires SQLite 3.7.15 or newer. -To use the module, you must first create a :class:`Connection` object that +To use the module, start by creating a :class:`Connection` object that represents the database. Here the data will be stored in the :file:`example.db` file:: import sqlite3 con = sqlite3.connect('example.db') -You can also supply the special name ``:memory:`` to create a database in RAM. +The special path name ``:memory:`` can be provided to create a temporary +database in RAM. -Once you have a :class:`Connection`, you can create a :class:`Cursor` object +Once a :class:`Connection` has been established, create a :class:`Cursor` object and call its :meth:`~Cursor.execute` method to perform SQL commands:: cur = con.cursor() @@ -49,16 +50,17 @@ and call its :meth:`~Cursor.execute` method to perform SQL commands:: # Just be sure any changes have been committed or they will be lost. con.close() -The data you've saved is persistent and is available in subsequent sessions:: +The saved data is persistent: it can be reloaded in a subsequent session even +after restarting the Python interpreter:: import sqlite3 con = sqlite3.connect('example.db') cur = con.cursor() -To retrieve data after executing a SELECT statement, you can either treat the -cursor as an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to -retrieve a single matching row, or call :meth:`~Cursor.fetchall` to get a list of the -matching rows. +To retrieve data after executing a SELECT statement, either treat the cursor as +an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to +retrieve a single matching row, or call :meth:`~Cursor.fetchall` to get a list +of the matching rows. This example uses the iterator form:: @@ -73,27 +75,27 @@ This example uses the iterator form:: .. _sqlite3-placeholders: -Usually your SQL operations will need to use values from Python variables. You -shouldn't assemble your query using Python's string operations because doing so -is insecure; it makes your program vulnerable to an SQL injection attack -(see the `xkcd webcomic `_ for a humorous example of -what can go wrong):: +SQL operations usually need to use values from Python variables. However, +beware of using Python's string operations to assemble queries, as they +are vulnerable to SQL injection attacks (see the `xkcd webcomic +`_ for a humorous example of what can go wrong):: # Never do this -- insecure! symbol = 'RHAT' cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol) -Instead, use the DB-API's parameter substitution. Put a placeholder wherever -you want to use a value, and then provide a tuple of values as the second -argument to the cursor's :meth:`~Cursor.execute` method. An SQL statement may +Instead, use the DB-API's parameter substitution. To insert a variable into a +query string, use a placeholder in the string, and substitute the actual values +into the query by providing them as a :class:`tuple` of values to the second +argument of the cursor's :meth:`~Cursor.execute` method. An SQL statement may use one of two kinds of placeholders: question marks (qmark style) or named placeholders (named style). For the qmark style, ``parameters`` must be a :term:`sequence `. For the named style, it can be either a :term:`sequence ` or :class:`dict` instance. The length of the :term:`sequence ` must match the number of placeholders, or a :exc:`ProgrammingError` is raised. If a :class:`dict` is given, it must contain -keys for all named parameters. Any extra items are ignored. Here's an example -of both styles: +keys for all named parameters. Any extra items are ignored. Here's an example of +both styles: .. literalinclude:: ../includes/sqlite3/execute_1.py @@ -117,6 +119,24 @@ Module functions and constants ------------------------------ +.. data:: apilevel + + String constant stating the supported DB-API level. Required by the DB-API. + Hard-coded to ``"2.0"``. + +.. data:: paramstyle + + String constant stating the type of parameter marker formatting expected by + the :mod:`sqlite3` module. Required by the DB-API. Hard-coded to + ``"qmark"``. + + .. note:: + + The :mod:`sqlite3` module supports both ``qmark`` and ``numeric`` DB-API + parameter styles, because that is what the underlying SQLite library + supports. However, the DB-API does not allow multiple values for + the ``paramstyle`` attribute. + .. data:: version The version number of this module, as a string. This is not the version of @@ -139,6 +159,26 @@ Module functions and constants The version number of the run-time SQLite library, as a tuple of integers. +.. data:: threadsafety + + Integer constant required by the DB-API, stating the level of thread safety + the :mod:`sqlite3` module supports. Currently hard-coded to ``1``, meaning + *"Threads may share the module, but not connections."* However, this may not + always be true. You can check the underlying SQLite library's compile-time + threaded mode using the following query:: + + import sqlite3 + con = sqlite3.connect(":memory:") + con.execute(""" + select * from pragma_compile_options + where compile_options like 'THREADSAFE=%' + """).fetchall() + + Note that the `SQLITE_THREADSAFE levels + `_ do not match the DB-API 2.0 + ``threadsafety`` levels. + + .. data:: PARSE_DECLTYPES This constant is meant to be used with the *detect_types* parameter of the @@ -165,7 +205,7 @@ Module functions and constants does not include the type, i. e. if you use something like ``'as "Expiration date [datetime]"'`` in your SQL, then we will parse out everything until the first ``'['`` for the column name and strip - the preceeding space: the column name would simply be "Expiration date". + the preceding space: the column name would simply be "Expiration date". .. function:: connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri]) @@ -215,14 +255,28 @@ Module functions and constants for the connection, you can set the *cached_statements* parameter. The currently implemented default is to cache 100 statements. - If *uri* is true, *database* is interpreted as a URI. This allows you - to specify options. For example, to open a database in read-only mode - you can use:: + If *uri* is :const:`True`, *database* is interpreted as a + :abbr:`URI (Uniform Resource Identifier)` with a file path and an optional + query string. The scheme part *must* be ``"file:"``. The path can be a + relative or absolute file path. The query string allows us to pass + parameters to SQLite. Some useful URI tricks include:: + + # Open a database in read-only mode. + con = sqlite3.connect("file:template.db?mode=ro", uri=True) - db = sqlite3.connect('file:path/to/database?mode=ro', uri=True) + # Don't implicitly create a new database file if it does not already exist. + # Will raise sqlite3.OperationalError if unable to open a database file. + con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True) - More information about this feature, including a list of recognized options, can - be found in the `SQLite URI documentation `_. + # Create a shared named in-memory database. + con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True) + con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True) + con1.executescript("create table t(t); insert into t values(28);") + rows = con2.execute("select * from t").fetchall() + + More information about this feature, including a list of recognized + parameters, can be found in the + `SQLite URI documentation `_. .. audit-event:: sqlite3.connect database sqlite3.connect .. audit-event:: sqlite3.connect/handle connection_handle sqlite3.connect @@ -283,7 +337,7 @@ Connection Objects .. class:: Connection - A SQLite database connection has the following attributes and methods: + An SQLite database connection has the following attributes and methods: .. attribute:: isolation_level @@ -324,24 +378,21 @@ Connection Objects .. method:: execute(sql[, parameters]) - This is a nonstandard shortcut that creates a cursor object by calling - the :meth:`~Connection.cursor` method, calls the cursor's - :meth:`~Cursor.execute` method with the *parameters* given, and returns - the cursor. + Create a new :class:`Cursor` object and call + :meth:`~Cursor.execute` on it with the given *sql* and *parameters*. + Return the new cursor object. .. method:: executemany(sql[, parameters]) - This is a nonstandard shortcut that creates a cursor object by - calling the :meth:`~Connection.cursor` method, calls the cursor's - :meth:`~Cursor.executemany` method with the *parameters* given, and - returns the cursor. + Create a new :class:`Cursor` object and call + :meth:`~Cursor.executemany` on it with the given *sql* and *parameters*. + Return the new cursor object. .. method:: executescript(sql_script) - This is a nonstandard shortcut that creates a cursor object by - calling the :meth:`~Connection.cursor` method, calls the cursor's - :meth:`~Cursor.executescript` method with the given *sql_script*, and - returns the cursor. + Create a new :class:`Cursor` object and call + :meth:`~Cursor.executescript` on it with the given *sql_script*. + Return the new cursor object. .. method:: create_function(name, num_params, func, *, deterministic=False) @@ -385,22 +436,19 @@ Connection Objects .. method:: create_collation(name, callable) - Creates a collation with the specified *name* and *callable*. The callable will - be passed two string arguments. It should return -1 if the first is ordered - lower than the second, 0 if they are ordered equal and 1 if the first is ordered - higher than the second. Note that this controls sorting (ORDER BY in SQL) so - your comparisons don't affect other SQL operations. + Create a collation named *name* using the collating function *callable*. + *callable* is passed two :class:`string ` arguments, + and it should return an :class:`integer `: - Note that the callable will get its parameters as Python bytestrings, which will - normally be encoded in UTF-8. + * ``1`` if the first is ordered higher than the second + * ``-1`` if the first is ordered lower than the second + * ``0`` if they are ordered equal - The following example shows a custom collation that sorts "the wrong way": + The following example shows a reverse sorting collation: .. literalinclude:: ../includes/sqlite3/collation_reverse.py - To remove a collation, call ``create_collation`` with ``None`` as callable:: - - con.create_collation("reverse", None) + Remove a collation function by setting *callable* to :const:`None`. .. method:: interrupt() @@ -451,14 +499,22 @@ Connection Objects Registers *trace_callback* to be called for each SQL statement that is actually executed by the SQLite backend. - The only argument passed to the callback is the statement (as string) that - is being executed. The return value of the callback is ignored. Note that - the backend does not only run statements passed to the :meth:`Cursor.execute` - methods. Other sources include the transaction management of the Python - module and the execution of triggers defined in the current database. + The only argument passed to the callback is the statement (as + :class:`str`) that is being executed. The return value of the callback is + ignored. Note that the backend does not only run statements passed to the + :meth:`Cursor.execute` methods. Other sources include the + :ref:`transaction management ` of the + sqlite3 module and the execution of triggers defined in the current + database. Passing :const:`None` as *trace_callback* will disable the trace callback. + .. note:: + Exceptions raised in the trace callback are not propagated. As a + development and debugging aid, use + :meth:`~sqlite3.enable_callback_tracebacks` to enable printing + tracebacks from exceptions raised in the trace callback. + .. versionadded:: 3.3 @@ -482,7 +538,7 @@ Connection Objects .. method:: load_extension(path) - This routine loads a SQLite extension from a shared library. You have to + This routine loads an SQLite extension from a shared library. You have to enable extension loading with :meth:`enable_load_extension` before you can use this routine. @@ -520,8 +576,8 @@ Connection Objects Using this attribute you can control what objects are returned for the ``TEXT`` data type. By default, this attribute is set to :class:`str` and the - :mod:`sqlite3` module will return Unicode objects for ``TEXT``. If you want to - return bytestrings instead, you can set it to :class:`bytes`. + :mod:`sqlite3` module will return :class:`str` objects for ``TEXT``. + If you want to return :class:`bytes` instead, you can set it to :class:`bytes`. You can also set it to any other callable that accepts a single bytestring parameter and returns the resulting object. @@ -558,7 +614,7 @@ Connection Objects .. method:: backup(target, *, pages=-1, progress=None, name="main", sleep=0.250) - This method makes a backup of a SQLite database even while it's being accessed + This method makes a backup of an SQLite database even while it's being accessed by other clients, or concurrently by the same connection. The copy will be written into the mandatory argument *target*, that must be another :class:`Connection` instance. @@ -648,7 +704,8 @@ Cursor Objects This is a nonstandard convenience method for executing multiple SQL statements at once. It issues a ``COMMIT`` statement first, then executes the SQL script it - gets as a parameter. + gets as a parameter. This method disregards :attr:`isolation_level`; any + transaction control must be added to *sql_script*. *sql_script* can be an instance of :class:`str`. @@ -692,6 +749,14 @@ Cursor Objects The cursor will be unusable from this point forward; a :exc:`ProgrammingError` exception will be raised if any operation is attempted with the cursor. + .. method:: setinputsizes(sizes) + + Required by the DB-API. Does nothing in :mod:`sqlite3`. + + .. method:: setoutputsize(size [, column]) + + Required by the DB-API. Does nothing in :mod:`sqlite3`. + .. attribute:: rowcount Although the :class:`Cursor` class of the :mod:`sqlite3` module implements this @@ -709,14 +774,15 @@ Cursor Objects .. attribute:: lastrowid - This read-only attribute provides the rowid of the last modified row. It is - only set if you issued an ``INSERT`` or a ``REPLACE`` statement using the - :meth:`execute` method. For operations other than ``INSERT`` or - ``REPLACE`` or when :meth:`executemany` is called, :attr:`lastrowid` is - set to :const:`None`. + This read-only attribute provides the row id of the last inserted row. It + is only updated after successful ``INSERT`` or ``REPLACE`` statements + using the :meth:`execute` method. For other statements, after + :meth:`executemany` or :meth:`executescript`, or if the insertion failed, + the value of ``lastrowid`` is left unchanged. The initial value of + ``lastrowid`` is :const:`None`. - If the ``INSERT`` or ``REPLACE`` statement failed to insert the previous - successful rowid is returned. + .. note:: + Inserts into ``WITHOUT ROWID`` tables are not recorded. .. versionchanged:: 3.6 Added support for the ``REPLACE`` statement. @@ -903,7 +969,7 @@ This is how SQLite types are converted to Python types by default: +-------------+----------------------------------------------+ The type system of the :mod:`sqlite3` module is extensible in two ways: you can -store additional Python types in a SQLite database via object adaptation, and +store additional Python types in an SQLite database via object adaptation, and you can let the :mod:`sqlite3` module convert SQLite types to different Python types via converters. @@ -1017,6 +1083,12 @@ If a timestamp stored in SQLite has a fractional part longer than 6 numbers, its value will be truncated to microsecond precision by the timestamp converter. +.. note:: + + The default "timestamp" converter ignores UTC offsets in the database and + always returns a naive :class:`datetime.datetime` object. To preserve UTC + offsets in timestamps, either leave converters disabled, or register an + offset-aware converter with :func:`register_converter`. .. _sqlite3-controlling-transactions: @@ -1048,6 +1120,9 @@ setting :attr:`isolation_level` to ``None``. This will leave the underlying control the transaction state by explicitly issuing ``BEGIN``, ``ROLLBACK``, ``SAVEPOINT``, and ``RELEASE`` statements in your code. +Note that :meth:`~Cursor.executescript` disregards +:attr:`isolation_level`; any transaction control must be added explicitly. + .. versionchanged:: 3.6 :mod:`sqlite3` used to implicitly commit an open transaction before DDL statements. This is no longer the case. @@ -1097,7 +1172,7 @@ committed: .. rubric:: Footnotes .. [#f1] The sqlite3 module is not built with loadable extension support by - default, because some platforms (notably Mac OS X) have SQLite + default, because some platforms (notably macOS) have SQLite libraries which are compiled without this feature. To get loadable extension support, you must pass the :option:`--enable-loadable-sqlite-extensions` option to configure. diff --git a/Doc/library/ssl.rst b/Doc/library/ssl.rst index afa3d87f5767a1..afb3e356db89ae 100644 --- a/Doc/library/ssl.rst +++ b/Doc/library/ssl.rst @@ -18,7 +18,7 @@ This module provides access to Transport Layer Security (often known as "Secure Sockets Layer") encryption and peer authentication facilities for network sockets, both client-side and server-side. This module uses the OpenSSL -library. It is available on all modern Unix systems, Windows, Mac OS X, and +library. It is available on all modern Unix systems, Windows, macOS, and probably additional platforms, as long as OpenSSL is installed on that platform. .. note:: @@ -681,19 +681,23 @@ Constants .. deprecated:: 3.10 + TLS clients and servers require different default settings for secure + communication. The generic TLS protocol constant is deprecated in + favor of :data:`PROTOCOL_TLS_CLIENT` and :data:`PROTOCOL_TLS_SERVER`. + .. data:: PROTOCOL_TLS_CLIENT - Auto-negotiate the highest protocol version like :data:`PROTOCOL_TLS`, - but only support client-side :class:`SSLSocket` connections. The protocol - enables :data:`CERT_REQUIRED` and :attr:`~SSLContext.check_hostname` by - default. + Auto-negotiate the highest protocol version that both the client and + server support, and configure the context client-side connections. The + protocol enables :data:`CERT_REQUIRED` and + :attr:`~SSLContext.check_hostname` by default. .. versionadded:: 3.6 .. data:: PROTOCOL_TLS_SERVER - Auto-negotiate the highest protocol version like :data:`PROTOCOL_TLS`, - but only support server-side :class:`SSLSocket` connections. + Auto-negotiate the highest protocol version that both the client and + server support, and configure the context server-side connections. .. versionadded:: 3.6 @@ -710,7 +714,7 @@ Constants Selects SSL version 2 as the channel encryption protocol. This protocol is not available if OpenSSL is compiled with the - ``OPENSSL_NO_SSL2`` flag. + ``no-ssl2`` option. .. warning:: @@ -724,8 +728,8 @@ Constants Selects SSL version 3 as the channel encryption protocol. - This protocol is not be available if OpenSSL is compiled with the - ``OPENSSL_NO_SSLv3`` flag. + This protocol is not available if OpenSSL is compiled with the + ``no-ssl3`` option. .. warning:: @@ -1052,7 +1056,7 @@ Constants Option for :func:`create_default_context` and :meth:`SSLContext.load_default_certs`. This value indicates that the - context may be used to authenticate Web servers (therefore, it will + context may be used to authenticate web servers (therefore, it will be used to create client-side sockets). .. versionadded:: 3.4 @@ -1061,7 +1065,7 @@ Constants Option for :func:`create_default_context` and :meth:`SSLContext.load_default_certs`. This value indicates that the - context may be used to authenticate Web clients (therefore, it will + context may be used to authenticate web clients (therefore, it will be used to create server-side sockets). .. versionadded:: 3.4 @@ -1393,7 +1397,7 @@ SSL sockets also have the following additional methods and attributes: .. method:: SSLSocket.version() Return the actual SSL protocol version negotiated by the connection - as a string, or ``None`` is no secure connection is established. + as a string, or ``None`` if no secure connection is established. As of this writing, possible return values include ``"SSLv2"``, ``"SSLv3"``, ``"TLSv1"``, ``"TLSv1.1"`` and ``"TLSv1.2"``. Recent OpenSSL versions may define more return values. @@ -1544,7 +1548,7 @@ to speed up repeated connections from the same clients. string must be the path to a single file in PEM format containing the certificate as well as any number of CA certificates needed to establish the certificate's authenticity. The *keyfile* string, if present, must - point to a file containing the private key in. Otherwise the private + point to a file containing the private key. Otherwise the private key will be taken from *certfile* as well. See the discussion of :ref:`ssl-certificates` for more information on how the certificate is stored in the *certfile*. @@ -1572,7 +1576,7 @@ to speed up repeated connections from the same clients. Load a set of default "certification authority" (CA) certificates from default locations. On Windows it loads CA certs from the ``CA`` and - ``ROOT`` system stores. On other systems it calls + ``ROOT`` system stores. On all systems it calls :meth:`SSLContext.set_default_verify_paths`. In the future the method may load CA certificates from other locations, too. @@ -1749,10 +1753,10 @@ to speed up repeated connections from the same clients. Due to the early negotiation phase of the TLS connection, only limited methods and attributes are usable like :meth:`SSLSocket.selected_alpn_protocol` and :attr:`SSLSocket.context`. - :meth:`SSLSocket.getpeercert`, :meth:`SSLSocket.getpeercert`, - :meth:`SSLSocket.cipher` and :meth:`SSLSocket.compress` methods require that + The :meth:`SSLSocket.getpeercert`, + :meth:`SSLSocket.cipher` and :meth:`SSLSocket.compression` methods require that the TLS connection has progressed beyond the TLS Client Hello and therefore - will not contain return meaningful values nor can they be called safely. + will not return meaningful values nor can they be called safely. The *sni_callback* function must return ``None`` to allow the TLS negotiation to continue. If a TLS failure is required, a constant @@ -2066,7 +2070,7 @@ to speed up repeated connections from the same clients. :attr:`SSLContext.verify_flags` returns :class:`VerifyFlags` flags: >>> ssl.create_default_context().verify_flags # doctest: +SKIP - ssl.VERIFY_X509_TRUSTED_FIRST + .. attribute:: SSLContext.verify_mode @@ -2078,7 +2082,7 @@ to speed up repeated connections from the same clients. :attr:`SSLContext.verify_mode` returns :class:`VerifyMode` enum: >>> ssl.create_default_context().verify_mode - ssl.CERT_REQUIRED + .. index:: single: certificates @@ -2353,7 +2357,7 @@ waiting for clients to connect:: context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile") bindsocket = socket.socket() - bindsocket.bind(('myaddr.mydomain.com', 10023)) + bindsocket.bind(('myaddr.example.com', 10023)) bindsocket.listen(5) When a client connects, you'll call :meth:`accept` on the socket to get the diff --git a/Doc/library/stat.rst b/Doc/library/stat.rst index f48a0a9faa6b0c..083dc5e3bcfd68 100644 --- a/Doc/library/stat.rst +++ b/Doc/library/stat.rst @@ -109,7 +109,7 @@ Example:: for f in os.listdir(top): pathname = os.path.join(top, f) - mode = os.stat(pathname).st_mode + mode = os.lstat(pathname).st_mode if S_ISDIR(mode): # It's a directory, recurse into it walktree(pathname, callback) @@ -372,11 +372,11 @@ The following flags can be used in the *flags* argument of :func:`os.chflags`: .. data:: UF_COMPRESSED - The file is stored compressed (Mac OS X 10.6+). + The file is stored compressed (macOS 10.6+). .. data:: UF_HIDDEN - The file should not be displayed in a GUI (Mac OS X 10.5+). + The file should not be displayed in a GUI (macOS 10.5+). .. data:: SF_ARCHIVED @@ -398,7 +398,7 @@ The following flags can be used in the *flags* argument of :func:`os.chflags`: The file is a snapshot file. -See the \*BSD or Mac OS systems man page :manpage:`chflags(2)` for more information. +See the \*BSD or macOS systems man page :manpage:`chflags(2)` for more information. On Windows, the following file attribute constants are available for use when testing bits in the ``st_file_attributes`` member returned by :func:`os.stat`. diff --git a/Doc/library/statistics.rst b/Doc/library/statistics.rst index 117d2b63cbea15..fd486191c01900 100644 --- a/Doc/library/statistics.rst +++ b/Doc/library/statistics.rst @@ -76,7 +76,7 @@ These functions calculate statistics regarding relations between two inputs. ========================= ===================================================== :func:`covariance` Sample covariance for two variables. :func:`correlation` Pearson's correlation coefficient for two variables. -:func:`linear_regression` Intercept and slope for simple linear regression. +:func:`linear_regression` Slope and intercept for simple linear regression. ========================= ===================================================== @@ -116,10 +116,11 @@ However, for reading convenience, most of the examples show sorted sequences. .. note:: - The mean is strongly affected by outliers and is not a robust estimator - for central location: the mean is not necessarily a typical example of - the data points. For more robust measures of central location, see - :func:`median` and :func:`mode`. + The mean is strongly affected by `outliers + `_ and is not necessarily a + typical example of the data points. For a more robust, although less + efficient, measure of `central tendency + `_, see :func:`median`. The sample mean gives an unbiased estimate of the true population mean, so that when taken on average over all the possible samples, @@ -626,47 +627,40 @@ However, for reading convenience, most of the examples show sorted sequences. .. versionadded:: 3.10 -.. function:: linear_regression(regressor, dependent_variable) +.. function:: linear_regression(x, y, /) - Return the intercept and slope of `simple linear regression + Return the slope and intercept of `simple linear regression `_ parameters estimated using ordinary least squares. Simple linear - regression describes relationship between *regressor* and - *dependent variable* in terms of linear function: + regression describes the relationship between an independent variable *x* and + a dependent variable *y* in terms of this linear function: - *dependent_variable = intercept + slope \* regressor + noise* + *y = slope \* x + intercept + noise* - where ``intercept`` and ``slope`` are the regression parameters that are - estimated, and noise term is an unobserved random variable, for the + where ``slope`` and ``intercept`` are the regression parameters that are + estimated, and ``noise`` represents the variability of the data that was not explained by the linear regression - (it is equal to the difference between prediction and the actual values - of dependent variable). + (it is equal to the difference between predicted and actual values + of the dependent variable). - Both inputs must be of the same length (no less than two), and regressor - needs not to be constant, otherwise :exc:`StatisticsError` is raised. + Both inputs must be of the same length (no less than two), and + the independent variable *x* cannot be constant; + otherwise a :exc:`StatisticsError` is raised. - For example, if we took the data on the data on `release dates of the Monty - Python films `_, and used - it to predict the cumulative number of Monty Python films produced, we could - predict what would be the number of films they could have made till year - 2019, assuming that they kept the pace. + For example, we can use the `release dates of the Monty + Python films `_ + to predict the cumulative number of Monty Python films + that would have been produced by 2019 + assuming that they had kept the pace. .. doctest:: >>> year = [1971, 1975, 1979, 1982, 1983] >>> films_total = [1, 2, 3, 4, 5] - >>> intercept, slope = linear_regression(year, films_total) - >>> round(intercept + slope * 2019) + >>> slope, intercept = linear_regression(year, films_total) + >>> round(slope * 2019 + intercept) 16 - We could also use it to "predict" how many Monty Python films existed when - Brian Cohen was born. - - .. doctest:: - - >>> round(intercept + slope * 1) - -610 - .. versionadded:: 3.10 diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index c4e6b4d1186d04..dd7c1ab5c9d1ef 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -178,13 +178,14 @@ operators are only defined where they make sense; for example, they raise a single: __ge__() (instance method) Non-identical instances of a class normally compare as non-equal unless the -class defines the :meth:`__eq__` method. +class defines the :meth:`~object.__eq__` method. Instances of a class cannot be ordered with respect to other instances of the same class, or other types of object, unless the class defines enough of the -methods :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in -general, :meth:`__lt__` and :meth:`__eq__` are sufficient, if you want the -conventional meanings of the comparison operators). +methods :meth:`~object.__lt__`, :meth:`~object.__le__`, :meth:`~object.__gt__`, and +:meth:`~object.__ge__` (in general, :meth:`~object.__lt__` and +:meth:`~object.__eq__` are sufficient, if you want the conventional meanings of the +comparison operators). The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be customized; also they can be applied to any two objects and never raise an @@ -660,7 +661,7 @@ Hashing of numeric types ------------------------ For numbers ``x`` and ``y``, possibly of different types, it's a requirement -that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`__hash__` +that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`~object.__hash__` method documentation for more details). For ease of implementation and efficiency across a variety of numeric types (including :class:`int`, :class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`) @@ -739,7 +740,7 @@ number, :class:`float`, or :class:`complex`:: """Compute the hash of a float x.""" if math.isnan(x): - return super().__hash__() + return object.__hash__(x) elif math.isinf(x): return sys.hash_info.inf if x > 0 else -sys.hash_info.inf else: @@ -772,21 +773,21 @@ using two distinct methods; these are used to allow user-defined classes to support iteration. Sequences, described below in more detail, always support the iteration methods. -One method needs to be defined for container objects to provide iteration +One method needs to be defined for container objects to provide :term:`iterable` support: .. XXX duplicated in reference/datamodel! .. method:: container.__iter__() - Return an iterator object. The object is required to support the iterator - protocol described below. If a container supports different types of - iteration, additional methods can be provided to specifically request + Return an :term:`iterator` object. The object is required to support the + iterator protocol described below. If a container supports different types + of iteration, additional methods can be provided to specifically request iterators for those iteration types. (An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.) This method corresponds to the - :c:member:`~PyTypeObject.tp_iter` slot of the type structure for Python objects in the Python/C - API. + :c:member:`~PyTypeObject.tp_iter` slot of the type structure for Python + objects in the Python/C API. The iterator objects themselves are required to support the following two methods, which together form the :dfn:`iterator protocol`: @@ -794,18 +795,19 @@ methods, which together form the :dfn:`iterator protocol`: .. method:: iterator.__iter__() - Return the iterator object itself. This is required to allow both containers - and iterators to be used with the :keyword:`for` and :keyword:`in` statements. - This method corresponds to the :c:member:`~PyTypeObject.tp_iter` slot of the type structure for - Python objects in the Python/C API. + Return the :term:`iterator` object itself. This is required to allow both + containers and iterators to be used with the :keyword:`for` and + :keyword:`in` statements. This method corresponds to the + :c:member:`~PyTypeObject.tp_iter` slot of the type structure for Python + objects in the Python/C API. .. method:: iterator.__next__() - Return the next item from the container. If there are no further items, raise - the :exc:`StopIteration` exception. This method corresponds to the - :c:member:`~PyTypeObject.tp_iternext` slot of the type structure for Python objects in the - Python/C API. + Return the next item from the :term:`iterator`. If there are no further + items, raise the :exc:`StopIteration` exception. This method corresponds to + the :c:member:`~PyTypeObject.tp_iternext` slot of the type structure for + Python objects in the Python/C API. Python defines several iterator objects to support iteration over general and specific sequence types, dictionaries, and other more specialized forms. The @@ -919,6 +921,16 @@ This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see :ref:`comparisons` in the language reference.) +.. index:: + single: loop; over mutable sequence + single: mutable sequence; loop over + +Forward and reversed iterators over mutable sequences access values using an +index. That index will continue to march forward (or backward) even if the +underlying sequence is mutated. The iterator terminates only when an +:exc:`IndexError` or a :exc:`StopIteration` is encountered (or when the index +drops below zero). + Notes: (1) @@ -1112,7 +1124,7 @@ accepts integers that meet the value restriction ``0 <= x <= 255``). | | index given by *i* | | | | (same as ``s[i:i] = [x]``) | | +------------------------------+--------------------------------+---------------------+ -| ``s.pop([i])`` | retrieves the item at *i* and | \(2) | +| ``s.pop()`` or ``s.pop(i)`` | retrieves the item at *i* and | \(2) | | | also removes it from *s* | | +------------------------------+--------------------------------+---------------------+ | ``s.remove(x)`` | remove the first item from *s* | \(3) | @@ -1295,7 +1307,7 @@ loops. range(start, stop[, step]) The arguments to the range constructor must be integers (either built-in - :class:`int` or any object that implements the ``__index__`` special + :class:`int` or any object that implements the :meth:`~object.__index__` special method). If the *step* argument is omitted, it defaults to ``1``. If the *start* argument is omitted, it defaults to ``0``. If *step* is zero, :exc:`ValueError` is raised. @@ -1423,7 +1435,7 @@ Strings are immutable written in a variety of ways: * Single quotes: ``'allows embedded "double" quotes'`` -* Double quotes: ``"allows embedded 'single' quotes"``. +* Double quotes: ``"allows embedded 'single' quotes"`` * Triple quoted: ``'''Three single quotes'''``, ``"""Three double quotes"""`` Triple quoted strings may span multiple lines - all associated whitespace will @@ -1467,7 +1479,8 @@ multiple fragments. depends on whether *encoding* or *errors* is given, as follows. If neither *encoding* nor *errors* is given, ``str(object)`` returns - :meth:`object.__str__() `, which is the "informal" or nicely + :meth:`type(object).__str__(object) `, + which is the "informal" or nicely printable string representation of *object*. For string objects, this is the string itself. If *object* does not have a :meth:`~object.__str__` method, then :func:`str` falls back to returning @@ -1749,9 +1762,9 @@ expression support in the :mod:`re` module). >>> from keyword import iskeyword >>> 'hello'.isidentifier(), iskeyword('hello') - True, False + (True, False) >>> 'def'.isidentifier(), iskeyword('def') - True, True + (True, True) .. method:: str.islower() @@ -2018,7 +2031,7 @@ expression support in the :mod:`re` module). .. index:: single: universal newlines; str.splitlines method -.. method:: str.splitlines([keepends]) +.. method:: str.splitlines(keepends=False) Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless *keepends* is given and @@ -2138,7 +2151,11 @@ expression support in the :mod:`re` module). >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk" - A workaround for apostrophes can be constructed using regular expressions:: + The :func:`string.capwords` function does not have this problem, as it + splits words on spaces only. + + Alternatively, a workaround for apostrophes can be constructed using regular + expressions:: >>> import re >>> def titlecase(s): @@ -2431,7 +2448,7 @@ data and are closely related to string objects in a variety of other ways. literals, except that a ``b`` prefix is added: * Single quotes: ``b'still allows embedded "double" quotes'`` - * Double quotes: ``b"still allows embedded 'single' quotes"``. + * Double quotes: ``b"still allows embedded 'single' quotes"`` * Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""`` Only ASCII characters are permitted in bytes literals (regardless of the @@ -2518,16 +2535,6 @@ The representation of bytes objects uses the literal format (``b'...'``) since it is often more useful than e.g. ``bytes([46, 46, 46])``. You can always convert a bytes object into a list of integers using ``list(b)``. -.. note:: - For Python 2.x users: In the Python 2.x series, a variety of implicit - conversions between 8-bit strings (the closest thing 2.x offers to a - built-in binary data type) and Unicode strings were permitted. This was a - backwards compatibility workaround to account for the fact that Python - originally only supported 8-bit text, and Unicode text was a later - addition. In Python 3.x, those implicit conversions are gone - conversions - between 8-bit binary data and Unicode text must be explicit, and bytes and - string objects will always compare unequal. - .. _typebytearray: @@ -3567,7 +3574,7 @@ The conversion types are: | | be used for Python2/3 code bases. | | +------------+-----------------------------------------------------+-------+ | ``'a'`` | Bytes (converts any Python object using | \(5) | -| | ``repr(obj).encode('ascii','backslashreplace)``). | | +| | ``repr(obj).encode('ascii', 'backslashreplace')``). | | +------------+-----------------------------------------------------+-------+ | ``'r'`` | ``'r'`` is an alias for ``'a'`` and should only | \(7) | | | be used for Python2/3 code bases. | | @@ -3632,17 +3639,16 @@ Memory Views of an object that supports the :ref:`buffer protocol ` without copying. -.. class:: memoryview(obj) +.. class:: memoryview(object) - Create a :class:`memoryview` that references *obj*. *obj* must support the - buffer protocol. Built-in objects that support the buffer protocol include - :class:`bytes` and :class:`bytearray`. + Create a :class:`memoryview` that references *object*. *object* must + support the buffer protocol. Built-in objects that support the buffer + protocol include :class:`bytes` and :class:`bytearray`. A :class:`memoryview` has the notion of an *element*, which is the - atomic memory unit handled by the originating object *obj*. For many - simple types such as :class:`bytes` and :class:`bytearray`, an element - is a single byte, but other types such as :class:`array.array` may have - bigger elements. + atomic memory unit handled by the originating *object*. For many simple + types such as :class:`bytes` and :class:`bytearray`, an element is a single + byte, but other types such as :class:`array.array` may have bigger elements. ``len(view)`` is equal to the length of :class:`~memoryview.tolist`. If ``view.ndim = 0``, the length is 1. If ``view.ndim = 1``, the length @@ -4329,13 +4335,9 @@ then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.) -Dictionaries can be created by placing a comma-separated list of ``key: value`` -pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: -'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor. - -.. class:: dict(**kwarg) - dict(mapping, **kwarg) - dict(iterable, **kwarg) +.. class:: dict(**kwargs) + dict(mapping, **kwargs) + dict(iterable, **kwargs) Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments. @@ -4755,9 +4757,9 @@ their implementation of the context management protocol. See the Python's :term:`generator`\s and the :class:`contextlib.contextmanager` decorator provide a convenient way to implement these protocols. If a generator function is decorated with the :class:`contextlib.contextmanager` decorator, it will return a -context manager implementing the necessary :meth:`__enter__` and -:meth:`__exit__` methods, rather than the iterator produced by an undecorated -generator function. +context manager implementing the necessary :meth:`~contextmanager.__enter__` and +:meth:`~contextmanager.__exit__` methods, rather than the iterator produced by an +undecorated generator function. Note that there is no specific slot for any of these methods in the type structure for Python objects in the Python/C API. Extension types wanting to @@ -4785,33 +4787,54 @@ Generic Alias Type 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 +``GenericAlias`` objects are generally created by +:ref:`subscripting ` a class. They are most often used with +:ref:`container classes `, such as :class:`list` or +:class:`dict`. For example, ``list[int]`` is a ``GenericAlias`` object created +by subscripting the ``list`` class with the argument :class:`int`. +``GenericAlias`` objects are intended primarily for use with :term:`type annotations `. -Usually, the :ref:`subscription ` 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 -`, implementing *parameterized generics* - a specific instance -of a generic which provides the types for container elements. + It is generally only possible to subscript a class if the class implements + the special method :meth:`~object.__class_getitem__`. + +A ``GenericAlias`` object acts as a proxy for a :term:`generic type`, +implementing *parameterized generics*. + +For a container class, the +argument(s) supplied to a :ref:`subscription ` of the class may +indicate the type(s) of the elements an object contains. For example, +``set[bytes]`` can be used in type annotations to signify a :class:`set` in +which all the elements are of type :class:`bytes`. + +For a class which defines :meth:`~object.__class_getitem__` but is not a +container, the argument(s) supplied to a subscription of the class will often +indicate the return type(s) of one or more methods defined on an object. For +example, :mod:`regular expressions ` can be used on both the :class:`str` data +type and the :class:`bytes` data type: + +* If ``x = re.search('foo', 'foo')``, ``x`` will be a + :ref:`re.Match ` object where the return values of + ``x.group(0)`` and ``x[0]`` will both be of type :class:`str`. We can + represent this kind of object in type annotations with the ``GenericAlias`` + ``re.Match[str]``. -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. +* If ``y = re.search(b'bar', b'bar')``, (note the ``b`` for :class:`bytes`), + ``y`` will also be an instance of ``re.Match``, but the return + values of ``y.group(0)`` and ``y[0]`` will both be of type + :class:`bytes`. In type annotations, we would represent this + variety of :ref:`re.Match ` objects with ``re.Match[bytes]``. + +``GenericAlias`` objects are instances of the class +:class:`types.GenericAlias`, which 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. + Creates a ``GenericAlias`` representing a type ``T`` parameterized by types + *X*, *Y*, and more depending on the ``T`` used. For example, a function expecting a :class:`list` containing :class:`float` elements:: @@ -4836,7 +4859,7 @@ The builtin functions :func:`isinstance` and :func:`issubclass` do not accept The Python runtime does not enforce :term:`type annotations `. This extends to generic types and their type parameters. When creating -an object from a ``GenericAlias``, container elements are not checked +a container object from a ``GenericAlias``, the elements in the container are not checked against their type. For example, the following code is discouraged, but will run without errors:: @@ -4863,8 +4886,8 @@ Calling :func:`repr` or :func:`str` on a generic shows the parameterized type:: >>> str(list[int]) 'list[int]' -The :meth:`__getitem__` method of generics will raise an exception to disallow -mistakes like ``dict[str][str]``:: +The :meth:`~object.__getitem__` method of generic containers will raise an +exception to disallow mistakes like ``dict[str][str]``:: >>> dict[str][str] Traceback (most recent call last): @@ -4873,7 +4896,7 @@ mistakes like ``dict[str][str]``:: However, such expressions are valid when :ref:`type variables ` are used. The index must have as many elements as there are type variable items -in the ``GenericAlias`` object's :attr:`__args__ `. :: +in the ``GenericAlias`` object's :attr:`~genericalias.__args__`. :: >>> from typing import TypeVar >>> Y = TypeVar('Y') @@ -4881,10 +4904,11 @@ in the ``GenericAlias`` object's :attr:`__args__ `. :: dict[str, int] -Standard Generic Collections -^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Standard Generic Classes +^^^^^^^^^^^^^^^^^^^^^^^^ -These standard library collections support parameterized generics. +The following standard library classes support parameterized generics. This +list is non-exhaustive. * :class:`tuple` * :class:`list` @@ -4922,12 +4946,29 @@ These standard library collections support parameterized generics. * :class:`collections.abc.ValuesView` * :class:`contextlib.AbstractContextManager` * :class:`contextlib.AbstractAsyncContextManager` +* :class:`dataclasses.Field` +* :class:`functools.cached_property` +* :class:`functools.partialmethod` +* :class:`os.PathLike` +* :class:`queue.LifoQueue` +* :class:`queue.Queue` +* :class:`queue.PriorityQueue` +* :class:`queue.SimpleQueue` * :ref:`re.Pattern ` * :ref:`re.Match ` +* :class:`shelve.BsdDbShelf` +* :class:`shelve.DbfilenameShelf` +* :class:`shelve.Shelf` +* :class:`types.MappingProxyType` +* :class:`weakref.WeakKeyDictionary` +* :class:`weakref.WeakMethod` +* :class:`weakref.WeakSet` +* :class:`weakref.WeakValueDictionary` + -Special Attributes of Generic Alias -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Special Attributes of ``GenericAlias`` objects +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ All parameterized generics implement special read-only attributes. @@ -4942,8 +4983,8 @@ All parameterized generics implement special read-only attributes. .. 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:: + types passed to the original :meth:`~object.__class_getitem__` of the + generic class:: >>> dict[str, list[int]].__args__ (, list[int]) @@ -4968,9 +5009,17 @@ All parameterized generics implement special read-only attributes. .. 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. + :pep:`484` - Type Hints + Introducing Python's framework for type annotations. + + :pep:`585` - Type Hinting Generics In Standard Collections + Introducing the ability to natively parameterize standard-library + classes, provided they implement the special class method + :meth:`~object.__class_getitem__`. + + :ref:`Generics`, :ref:`user-defined generics ` and :class:`typing.Generic` + Documentation on how to implement generic classes that can be + parameterized at runtime and understood by static type-checkers. .. versionadded:: 3.9 @@ -5041,16 +5090,16 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`. TypeError: isinstance() argument 2 cannot contain a parameterized generic 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 +:data:`types.UnionType` and used for :func:`isinstance` checks. An object cannot be instantiated from the type:: >>> import types - >>> isinstance(int | str, types.Union) + >>> isinstance(int | str, types.UnionType) True - >>> types.Union() + >>> types.UnionType() Traceback (most recent call last): File "", line 1, in - TypeError: cannot create 'types.Union' instances + TypeError: cannot create 'types.UnionType' instances .. note:: The :meth:`__or__` method for type objects was added to support the syntax diff --git a/Doc/library/string.rst b/Doc/library/string.rst index d935419f7b75e7..b7108641805e65 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -386,8 +386,8 @@ The ``'#'`` option causes the "alternate form" to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float and complex types. For integers, when binary, octal, or hexadecimal output -is used, this option adds the prefix respective ``'0b'``, ``'0o'``, or -``'0x'`` to the output value. For float and complex the +is used, this option adds the respective prefix ``'0b'``, ``'0o'``, +``'0x'``, or ``'0X'`` to the output value. For float and complex the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions @@ -428,12 +428,13 @@ character of ``'0'`` with an *alignment* type of ``'='``. Preceding the *width* field by ``'0'`` no longer affects the default alignment for strings. -The *precision* is a decimal number indicating how many digits should be -displayed after the decimal point for a floating point value formatted with -``'f'`` and ``'F'``, or before and after the decimal point for a floating point -value formatted with ``'g'`` or ``'G'``. For non-number types the field +The *precision* is a decimal integer indicating how many digits should be +displayed after the decimal point for presentation types +``'f'`` and ``'F'``, or before and after the decimal point for presentation +types ``'g'`` or ``'G'``. For string presentation types the field indicates the maximum field size - in other words, how many characters will be -used from the field content. The *precision* is not allowed for integer values. +used from the field content. The *precision* is not allowed for integer +presentation types. Finally, the *type* determines how the data should be presented. @@ -467,6 +468,8 @@ The available integer presentation types are: +---------+----------------------------------------------------------+ | ``'X'`` | Hex format. Outputs the number in base 16, using | | | upper-case letters for the digits above 9. | + | | In case ``'#'`` is specified, the prefix ``'0x'`` will | + | | be upper-cased to ``'0X'`` as well. | +---------+----------------------------------------------------------+ | ``'n'`` | Number. This is the same as ``'d'``, except that it uses | | | the current locale setting to insert the appropriate | diff --git a/Doc/library/struct.rst b/Doc/library/struct.rst index eccba20fb8fe7e..978eeabe37b1a3 100644 --- a/Doc/library/struct.rst +++ b/Doc/library/struct.rst @@ -146,9 +146,10 @@ If the first character is not one of these, ``'@'`` is assumed. Native byte order is big-endian or little-endian, depending on the host system. For example, Intel x86 and AMD64 (x86-64) are little-endian; -Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature -switchable endianness (bi-endian). Use ``sys.byteorder`` to check the -endianness of your system. +IBM z and most legacy architectures are big-endian; +and ARM, RISC-V and IBM Power feature switchable endianness +(bi-endian, though the former two are nearly always little-endian in practice). +Use ``sys.byteorder`` to check the endianness of your system. Native size and alignment are determined using the C compiler's ``sizeof`` expression. This is always combined with native byte order. diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst index b60db58d182e6c..d0f8864493ed02 100644 --- a/Doc/library/subprocess.rst +++ b/Doc/library/subprocess.rst @@ -33,9 +33,6 @@ The recommended approach to invoking subprocesses is to use the :func:`run` function for all use cases it can handle. For more advanced use cases, the underlying :class:`Popen` interface can be used directly. -The :func:`run` function was added in Python 3.5; if you need to retain -compatibility with older versions, see the :ref:`call-function-trio` section. - .. function:: run(args, *, stdin=None, input=None, stdout=None, stderr=None,\ capture_output=False, shell=False, cwd=None, timeout=None, \ @@ -214,7 +211,9 @@ compatibility with older versions, see the :ref:`call-function-trio` section. .. exception:: CalledProcessError Subclass of :exc:`SubprocessError`, raised when a process run by - :func:`check_call` or :func:`check_output` returns a non-zero exit status. + :func:`check_call`, :func:`check_output`, or :func:`run` (with ``check=True``) + returns a non-zero exit status. + .. attribute:: returncode @@ -264,13 +263,14 @@ default values. The arguments that are most commonly needed are: *stdin*, *stdout* and *stderr* specify the executed program's standard input, standard output and standard error file handles, respectively. Valid values are :data:`PIPE`, :data:`DEVNULL`, an existing file descriptor (a positive - integer), an existing file object, and ``None``. :data:`PIPE` indicates - that a new pipe to the child should be created. :data:`DEVNULL` indicates - that the special file :data:`os.devnull` will be used. With the default - settings of ``None``, no redirection will occur; the child's file handles - will be inherited from the parent. Additionally, *stderr* can be - :data:`STDOUT`, which indicates that the stderr data from the child - process should be captured into the same file handle as for *stdout*. + integer), an existing file object with a valid file descriptor, and ``None``. + :data:`PIPE` indicates that a new pipe to the child should be created. + :data:`DEVNULL` indicates that the special file :data:`os.devnull` will + be used. With the default settings of ``None``, no redirection will occur; + the child's file handles will be inherited from the parent. + Additionally, *stderr* can be :data:`STDOUT`, which indicates that the + stderr data from the child process should be captured into the same file + handle as for *stdout*. .. index:: single: universal newlines; subprocess module @@ -482,13 +482,14 @@ functions. *stdin*, *stdout* and *stderr* specify the executed program's standard input, standard output and standard error file handles, respectively. Valid values are :data:`PIPE`, :data:`DEVNULL`, an existing file descriptor (a positive - integer), an existing :term:`file object`, and ``None``. :data:`PIPE` - indicates that a new pipe to the child should be created. :data:`DEVNULL` - indicates that the special file :data:`os.devnull` will be used. With the - default settings of ``None``, no redirection will occur; the child's file - handles will be inherited from the parent. Additionally, *stderr* can be - :data:`STDOUT`, which indicates that the stderr data from the applications - should be captured into the same file handle as for stdout. + integer), an existing :term:`file object` with a valid file descriptor, + and ``None``. :data:`PIPE` indicates that a new pipe to the child should + be created. :data:`DEVNULL` indicates that the special file + :data:`os.devnull` will be used. With the default settings of ``None``, + no redirection will occur; the child's file handles will be inherited from + the parent. Additionally, *stderr* can be :data:`STDOUT`, which indicates + that the stderr data from the applications should be captured into the same + file handle as for stdout. If *preexec_fn* is set to a callable object, this object will be called in the child process just before the child is executed. @@ -689,7 +690,10 @@ execute, will be re-raised in the parent. The most common exception raised is :exc:`OSError`. This occurs, for example, when trying to execute a non-existent file. Applications should prepare for -:exc:`OSError` exceptions. +:exc:`OSError` exceptions. Note that, when ``shell=True``, :exc:`OSError` +will be raised by the child only if the selected shell itself was not found. +To determine if the shell failed to find the requested application, it is +necessary to check the return code or output from the subprocess. A :exc:`ValueError` will be raised if :class:`Popen` is called with invalid arguments. @@ -707,6 +711,7 @@ Exceptions defined in this module all inherit from :exc:`SubprocessError`. .. versionadded:: 3.3 The :exc:`SubprocessError` base class was added. +.. _subprocess-security: Security Considerations ----------------------- @@ -1145,6 +1150,8 @@ calls these functions. code was zero then return, otherwise raise :exc:`CalledProcessError`. The :exc:`CalledProcessError` object will have the return code in the :attr:`~CalledProcessError.returncode` attribute. + If :func:`check_call` was unable to start the process it will propagate the exception + that was raised. Code needing to capture stdout or stderr should use :func:`run` instead:: @@ -1292,11 +1299,17 @@ Replacing :func:`os.system` sts = os.system("mycmd" + " myarg") # becomes - sts = call("mycmd" + " myarg", shell=True) + retcode = call("mycmd" + " myarg", shell=True) Notes: * Calling the program through the shell is usually not required. +* The :func:`call` return value is encoded differently to that of + :func:`os.system`. + +* The :func:`os.system` function ignores SIGINT and SIGQUIT signals while + the command is running, but the caller must do this separately when + using the :mod:`subprocess` module. A more realistic example would look like this:: diff --git a/Doc/library/sunau.rst b/Doc/library/sunau.rst index aad6f93b6bff18..b4d996e67e17cf 100644 --- a/Doc/library/sunau.rst +++ b/Doc/library/sunau.rst @@ -3,11 +3,16 @@ .. module:: sunau :synopsis: Provide an interface to the Sun AU sound format. + :deprecated: .. sectionauthor:: Moshe Zadka **Source code:** :source:`Lib/sunau.py` +.. deprecated:: 3.11 + The :mod:`sunau` module is deprecated + (see :pep:`PEP 594 <594#sunau>` for details). + -------------- The :mod:`sunau` module provides a convenient interface to the Sun AU sound diff --git a/Doc/library/superseded.rst b/Doc/library/superseded.rst index 50a5983236e76a..b38f16691f6ea9 100644 --- a/Doc/library/superseded.rst +++ b/Doc/library/superseded.rst @@ -10,5 +10,27 @@ backwards compatibility. They have been superseded by other modules. .. toctree:: - optparse.rst + aifc.rst + asynchat.rst + asyncore.rst + audioop.rst + cgi.rst + cgitb.rst + chunk.rst + crypt.rst + imghdr.rst imp.rst + mailcap.rst + msilib.rst + nis.rst + nntplib.rst + optparse.rst + ossaudiodev.rst + pipes.rst + smtpd.rst + sndhdr.rst + spwd.rst + sunau.rst + telnetlib.rst + uu.rst + xdrlib.rst diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index ec12e02fb37d45..b59fa2c9d7cf3b 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -429,10 +429,7 @@ always available. .. function:: exit([arg]) - Exit from Python. This is implemented by raising the :exc:`SystemExit` - exception, so cleanup actions specified by finally clauses of :keyword:`try` - statements are honored, and it is possible to intercept the exit attempt at - an outer level. + Raise a :exc:`SystemExit` exception, signaling an intention to exit the interpreter. The optional argument *arg* can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero @@ -449,7 +446,8 @@ always available. Since :func:`exit` ultimately "only" raises an exception, it will only exit the process when called from the main thread, and the exception is not - intercepted. + intercepted. Cleanup actions specified by finally clauses of :keyword:`try` statements + are honored, and it is possible to intercept the exit attempt at an outer level. .. versionchanged:: 3.6 If an error occurs in the cleanup after the Python interpreter @@ -1073,7 +1071,11 @@ always available. This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks. However, replacing the dictionary will not necessarily work as expected and - deleting essential items from the dictionary may cause Python to fail. + deleting essential items from the dictionary may cause Python to fail. If + you want to iterate over this global dictionary always use + ``sys.modules.copy()`` or ``tuple(sys.modules)`` to avoid exceptions as its + size may change during iteration as a side effect of code or activity in + other threads. .. data:: orig_argv @@ -1212,13 +1214,10 @@ always available. .. data:: prefix A string giving the site-specific directory prefix where the platform - independent Python files are installed; by default, this is the string + independent Python files are installed; on Unix, the default is ``'/usr/local'``. This can be set at build time with the ``--prefix`` - argument to the :program:`configure` script. The main collection of Python - library modules is installed in the directory :file:`{prefix}/lib/python{X.Y}` - while the platform independent header files (all except :file:`pyconfig.h`) are - stored in :file:`{prefix}/include/python{X.Y}`, where *X.Y* is the version - number of Python, for example ``3.2``. + argument to the :program:`configure` script. See + :ref:`installation_paths` for derived paths. .. note:: If a :ref:`virtual environment ` is in effect, this value will be changed in ``site.py`` to point to the virtual diff --git a/Doc/library/sysconfig.rst b/Doc/library/sysconfig.rst index bb8e2d88c0fc95..7ef3b2489673e6 100644 --- a/Doc/library/sysconfig.rst +++ b/Doc/library/sysconfig.rst @@ -60,6 +60,7 @@ Example of usage:: >>> sysconfig.get_config_vars('AR', 'CXX') ['ar', 'g++'] +.. _installation_paths: Installation paths ------------------ @@ -72,9 +73,9 @@ Every new component that is installed using :mod:`distutils` or a Distutils-based system will follow the same scheme to copy its file in the right places. -Python currently supports seven schemes: +Python currently supports six schemes: -- *posix_prefix*: scheme for POSIX platforms like Linux or Mac OS X. This is +- *posix_prefix*: scheme for POSIX platforms like Linux or macOS. This is the default scheme used when Python or a component is installed. - *posix_home*: scheme for POSIX platforms used when a *home* option is used upon installation. This scheme is used when a component is installed through @@ -84,6 +85,7 @@ Python currently supports seven schemes: located under the user home directory. - *nt*: scheme for NT platforms like Windows. - *nt_user*: scheme for NT platforms, when the *user* option is used. +- *osx_framework_user*: scheme for macOS, when the *user* option is used. Each scheme is itself composed of a series of paths and each path has a unique identifier. Python currently uses eight paths: @@ -175,7 +177,7 @@ identifier. Python currently uses eight paths: If *expand* is set to ``False``, the path will not be expanded using the variables. - If *name* is not found, return ``None``. + If *name* is not found, raise a :exc:`KeyError`. .. function:: get_paths([scheme, [vars, [expand]]]) @@ -225,7 +227,7 @@ Other functions - win-amd64 (64bit Windows on AMD64, aka x86_64, Intel64, and EM64T) - win32 (all others - specifically, sys.platform is returned) - Mac OS X can return: + macOS can return: - macosx-10.6-ppc - macosx-10.4-ppc64 diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst index 13088a10d77c57..6afb8397b7866d 100644 --- a/Doc/library/tarfile.rst +++ b/Doc/library/tarfile.rst @@ -102,6 +102,9 @@ Some facts and figures: ``'x:bz2'``, :func:`tarfile.open` accepts the keyword argument *compresslevel* (default ``9``) to specify the compression level of the file. + For modes ``'w:xz'`` and ``'x:xz'``, :func:`tarfile.open` accepts the + keyword argument *preset* to specify the compression level of the file. + For special purposes, there is a second format for *mode*: ``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile` object that processes its data as a stream of blocks. No random seeking will diff --git a/Doc/library/telnetlib.rst b/Doc/library/telnetlib.rst index 48a9aea50dddd1..48a927c8ac96b2 100644 --- a/Doc/library/telnetlib.rst +++ b/Doc/library/telnetlib.rst @@ -3,6 +3,7 @@ .. module:: telnetlib :synopsis: Telnet client class. + :deprecated: .. sectionauthor:: Skip Montanaro @@ -10,6 +11,10 @@ .. index:: single: protocol; Telnet +.. deprecated:: 3.11 + The :mod:`telnetlib` module is deprecated + (see :pep:`PEP 594 <594#telnetlib>` for details and alternatives). + -------------- The :mod:`telnetlib` module provides a :class:`Telnet` class that implements the diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index 2970252036780d..f0f9f297461955 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -62,6 +62,9 @@ The module defines the following user-callable items: The :py:data:`os.O_TMPFILE` flag is used if it is available and works (Linux-specific, requires Linux kernel 3.11 or later). + On platforms that are neither Posix nor Cygwin, TemporaryFile is an alias + for NamedTemporaryFile. + .. audit-event:: tempfile.mkstemp fullpath tempfile.TemporaryFile .. versionchanged:: 3.5 @@ -81,7 +84,7 @@ The module defines the following user-callable items: file-like object. Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot - on Windows NT or later). If *delete* is true (the default), the file is + on Windows). If *delete* is true (the default), the file is deleted as soon as it is closed. The returned object is always a file-like object whose :attr:`!file` attribute is the underlying true file object. This file-like object can @@ -93,9 +96,9 @@ The module defines the following user-callable items: Added *errors* parameter. -.. function:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None) +.. class:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None) - This function operates exactly as :func:`TemporaryFile` does, except that + This class operates exactly as :func:`TemporaryFile` does, except that data is spooled in memory until the file size exceeds *max_size*, or until the file's :func:`fileno` method is called, at which point the contents are written to disk and operation proceeds as with @@ -118,9 +121,9 @@ The module defines the following user-callable items: Added *errors* parameter. -.. function:: TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False) +.. class:: TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False) - This function securely creates a temporary directory using the same rules as :func:`mkdtemp`. + This class securely creates a temporary directory using the same rules as :func:`mkdtemp`. The resulting object can be used as a context manager (see :ref:`tempfile-examples`). On completion of the context or destruction of the temporary directory object, the newly created temporary directory @@ -341,6 +344,7 @@ Here are some examples of typical usage of the :mod:`tempfile` module:: >>> # directory and contents have been removed +.. _tempfile-mktemp-deprecated: Deprecated functions and variables ---------------------------------- diff --git a/Doc/library/test.rst b/Doc/library/test.rst index e4f779bd83eb87..07c3339661ad5b 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -267,10 +267,10 @@ The :mod:`test.support` module defines the following constants: .. data:: INTERNET_TIMEOUT - Timeout in seconds for network requests going to the Internet. + Timeout in seconds for network requests going to the internet. The timeout is short enough to prevent a test to wait for too long if the - Internet request is blocked for whatever reason. + internet request is blocked for whatever reason. Usually, a timeout using :data:`INTERNET_TIMEOUT` should not mark a test as failed, but skip the test instead: see @@ -359,13 +359,19 @@ The :mod:`test.support` module defines the following constants: .. data:: MISSING_C_DOCSTRINGS - Return ``True`` if running on CPython, not on Windows, and configuration - not set with ``WITH_DOC_STRINGS``. + Set to ``True`` if Python is built without docstrings (the + :c:macro:`WITH_DOC_STRINGS` macro is not defined). + See the :option:`configure --without-doc-strings <--without-doc-strings>` option. + + See also the :data:`HAVE_DOCSTRINGS` variable. .. data:: HAVE_DOCSTRINGS - Check for presence of docstrings. + Set to ``True`` if function docstrings are available. + See the :option:`python -OO <-O>` option, which strips docstrings of functions implemented in Python. + + See also the :data:`MISSING_C_DOCSTRINGS` variable. .. data:: TEST_HTTP_URL @@ -423,11 +429,6 @@ The :mod:`test.support` module defines the following functions: Used when tests are executed by :mod:`test.regrtest`. -.. function:: system_must_validate_cert(f) - - Raise :exc:`unittest.SkipTest` on TLS certification validation failures. - - .. function:: sortdict(dict) Return a repr of *dict* with keys sorted. @@ -445,12 +446,12 @@ The :mod:`test.support` module defines the following functions: .. function:: match_test(test) - Match *test* to patterns set in :func:`set_match_tests`. + Determine whether *test* matches the patterns set in :func:`set_match_tests`. -.. function:: set_match_tests(patterns) +.. function:: set_match_tests(accept_patterns=None, ignore_patterns=None) - Define match test with regular expression *patterns*. + Define match patterns on test filenames and test method names for filtering tests. .. function:: run_unittest(*classes) @@ -490,7 +491,9 @@ The :mod:`test.support` module defines the following functions: .. function:: check_impl_detail(**guards) Use this check to guard CPython's implementation-specific tests or to - run them only on the implementations guarded by the arguments:: + run them only on the implementations guarded by the arguments. This + function returns ``True`` or ``False`` depending on the host platform. + Example usage:: check_impl_detail() # Only on CPython (default). check_impl_detail(jython=True) # Only on Jython. @@ -509,7 +512,7 @@ The :mod:`test.support` module defines the following functions: time the regrtest began. -.. function:: get_original_stdout +.. function:: get_original_stdout() Return the original stdout set by :func:`record_original_stdout` or ``sys.stdout`` if it's not set. @@ -554,7 +557,7 @@ The :mod:`test.support` module defines the following functions: .. function:: disable_faulthandler() - A context manager that replaces ``sys.stderr`` with ``sys.__stderr__``. + A context manager that temporary disables :mod:`faulthandler`. .. function:: gc_collect() @@ -567,8 +570,8 @@ The :mod:`test.support` module defines the following functions: .. function:: disable_gc() - A context manager that disables the garbage collector upon entry and - reenables it upon exit. + A context manager that disables the garbage collector on entry. On + exit, the garbage collector is restored to its prior state. .. function:: swap_attr(obj, attr, new_val) @@ -633,14 +636,14 @@ The :mod:`test.support` module defines the following functions: .. function:: calcobjsize(fmt) - Return :func:`struct.calcsize` for ``nP{fmt}0n`` or, if ``gettotalrefcount`` - exists, ``2PnP{fmt}0P``. + Return the size of the :c:type:`PyObject` whose structure members are + defined by *fmt*. The returned value includes the size of the Python object header and alignment. .. function:: calcvobjsize(fmt) - Return :func:`struct.calcsize` for ``nPn{fmt}0n`` or, if ``gettotalrefcount`` - exists, ``2PnPn{fmt}0P``. + Return the size of the :c:type:`PyVarObject` whose structure members are + defined by *fmt*. The returned value includes the size of the Python object header and alignment. .. function:: checksizeof(test, o, size) @@ -656,6 +659,11 @@ The :mod:`test.support` module defines the following functions: have an associated comment identifying the relevant tracker issue. +.. function:: system_must_validate_cert(f) + + A decorator that skips the decorated test on TLS certification validation failures. + + .. decorator:: run_with_locale(catstr, *locales) A decorator for running a function in a different locale, correctly @@ -673,19 +681,19 @@ The :mod:`test.support` module defines the following functions: .. decorator:: requires_freebsd_version(*min_version) Decorator for the minimum version when running test on FreeBSD. If the - FreeBSD version is less than the minimum, raise :exc:`unittest.SkipTest`. + FreeBSD version is less than the minimum, the test is skipped. .. decorator:: requires_linux_version(*min_version) Decorator for the minimum version when running test on Linux. If the - Linux version is less than the minimum, raise :exc:`unittest.SkipTest`. + Linux version is less than the minimum, the test is skipped. .. decorator:: requires_mac_version(*min_version) - Decorator for the minimum version when running test on Mac OS X. If the - MAC OS X version is less than the minimum, raise :exc:`unittest.SkipTest`. + Decorator for the minimum version when running test on macOS. If the + macOS version is less than the minimum, the test is skipped. .. decorator:: requires_IEEE_754 @@ -723,7 +731,7 @@ The :mod:`test.support` module defines the following functions: Decorator for only running the test if :data:`HAVE_DOCSTRINGS`. -.. decorator:: cpython_only(test) +.. decorator:: cpython_only Decorator for tests only applicable to CPython. @@ -734,12 +742,12 @@ The :mod:`test.support` module defines the following functions: returns ``False``, then uses *msg* as the reason for skipping the test. -.. decorator:: no_tracing(func) +.. decorator:: no_tracing Decorator to temporarily turn off tracing for the duration of the test. -.. decorator:: refcount_test(test) +.. decorator:: refcount_test Decorator for tests which involve reference counting. The decorator does not run the test if it is not run by CPython. Any trace function is unset @@ -762,10 +770,9 @@ The :mod:`test.support` module defines the following functions: means the test doesn't support dummy runs when ``-M`` is not specified. -.. decorator:: bigaddrspacetest(f) +.. decorator:: bigaddrspacetest - Decorator for tests that fill the address space. *f* is the function to - wrap. + Decorator for tests that fill the address space. .. function:: check_syntax_error(testcase, statement, errtext='', *, lineno=None, offset=None) @@ -867,7 +874,7 @@ The :mod:`test.support` module defines the following functions: .. function:: check_free_after_iterating(test, iter, cls, args=()) - Assert that *iter* is deallocated after iterating. + Assert instances of *cls* are deallocated after iterating. .. function:: missing_compiler_executable(cmd_names=[]) @@ -928,8 +935,16 @@ The :mod:`test.support` module defines the following functions: .. versionadded:: 3.10 +.. function:: check_disallow_instantiation(test_case, tp, *args, **kwds) + + Assert that type *tp* cannot be instantiated using *args* and *kwds*. + + .. versionadded:: 3.10 + + The :mod:`test.support` module defines the following classes: + .. class:: SuppressCrashReport() A context manager used to try to prevent crash dialog popups on tests that @@ -950,6 +965,16 @@ The :mod:`test.support` module defines the following classes: Class to save and restore signal handlers registered by the Python signal handler. + .. method:: save(self) + + Save the signal handlers to a dictionary mapping signal numbers to the + current signal handler. + + .. method:: restore(self) + + Set the signal numbers from the :meth:`save` dictionary to the saved + handler. + .. class:: Matcher() @@ -1093,11 +1118,11 @@ script execution tests. variables *env_vars* succeeds (``rc == 0``) and return a ``(return code, stdout, stderr)`` tuple. - If the ``__cleanenv`` keyword is set, *env_vars* is used as a fresh + If the *__cleanenv* keyword-only parameter is set, *env_vars* is used as a fresh environment. Python is started in isolated mode (command line option ``-I``), - except if the ``__isolated`` keyword is set to ``False``. + except if the *__isolated* keyword-only parameter is set to ``False``. .. versionchanged:: 3.9 The function no longer strips whitespaces from *stderr*. @@ -1208,15 +1233,17 @@ The :mod:`test.support.threading_helper` module provides support for threading t is still alive after *timeout* seconds. -.. decorator:: reap_threads(func) +.. decorator:: reap_threads Decorator to ensure the threads are cleaned up even if the test fails. .. function:: start_threads(threads, unlock=None) - Context manager to start *threads*. It attempts to join the threads upon - exit. + Context manager to start *threads*, which is a sequence of threads. + *unlock* is a function called after the threads are started, even if an + exception was raised; an example would be :meth:`threading.Event.set`. + ``start_threads`` will attempt to join the started threads upon exit. .. function:: threading_cleanup(*original_values) @@ -1241,7 +1268,7 @@ The :mod:`test.support.threading_helper` module provides support for threading t Context manager catching :class:`threading.Thread` exception using :func:`threading.excepthook`. - Attributes set when an exception is catched: + Attributes set when an exception is caught: * ``exc_type`` * ``exc_value`` @@ -1298,7 +1325,10 @@ The :mod:`test.support.os_helper` module provides support for os tests. .. data:: TESTFN_NONASCII - Set to a filename containing the :data:`FS_NONASCII` character. + Set to a filename containing the :data:`FS_NONASCII` character, if it exists. + This guarantees that if the filename exists, it can be encoded and decoded + with the default filesystem encoding. This allows tests that require a + non-ASCII filename to be easily skipped on platforms where they can't work. .. data:: TESTFN_UNENCODABLE @@ -1396,13 +1426,16 @@ The :mod:`test.support.os_helper` module provides support for os tests. .. function:: rmdir(filename) Call :func:`os.rmdir` on *filename*. On Windows platforms, this is - wrapped with a wait loop that checks for the existence of the file. + wrapped with a wait loop that checks for the existence of the file, + which is needed due to antivirus programs that can hold files open and prevent + deletion. .. function:: rmtree(path) Call :func:`shutil.rmtree` on *path* or call :func:`os.lstat` and - :func:`os.rmdir` to remove a path and its contents. On Windows platforms, + :func:`os.rmdir` to remove a path and its contents. As with :func:`rmdir`, + on Windows platforms this is wrapped with a wait loop that checks for the existence of the files. @@ -1449,8 +1482,9 @@ The :mod:`test.support.os_helper` module provides support for os tests. .. function:: unlink(filename) - Call :func:`os.unlink` on *filename*. On Windows platforms, this is - wrapped with a wait loop that checks for the existence fo the file. + Call :func:`os.unlink` on *filename*. As with :func:`rmdir`, + on Windows platforms, this is + wrapped with a wait loop that checks for the existence of the file. :mod:`test.support.import_helper` --- Utilities for import tests @@ -1506,7 +1540,7 @@ The :mod:`test.support.import_helper` module provides support for import tests. .. versionadded:: 3.1 -.. function:: import_module(name, deprecated=False, *, required_on()) +.. function:: import_module(name, deprecated=False, *, required_on=()) This function imports and returns the named module. Unlike a normal import, this function raises :exc:`unittest.SkipTest` if the module @@ -1548,7 +1582,7 @@ The :mod:`test.support.import_helper` module provides support for import tests. A context manager to force import to return a new module reference. This is useful for testing module-level behaviors, such as the emission of a - DeprecationWarning on import. Example usage:: + :exc:`DeprecationWarning` on import. Example usage:: with CleanImport('foo'): importlib.import_module('foo') # New reference. @@ -1556,7 +1590,7 @@ The :mod:`test.support.import_helper` module provides support for import tests. .. class:: DirsOnSysPath(*paths) - A context manager to temporarily add directories to sys.path. + A context manager to temporarily add directories to :data:`sys.path`. This makes a copy of :data:`sys.path`, appends any directories given as positional arguments, then reverts :data:`sys.path` to the copied diff --git a/Doc/library/textwrap.rst b/Doc/library/textwrap.rst index 16837104b6cebc..1a9d5f98f78a7e 100644 --- a/Doc/library/textwrap.rst +++ b/Doc/library/textwrap.rst @@ -17,20 +17,30 @@ If you're just wrapping or filling one or two text strings, the convenience functions should be good enough; otherwise, you should use an instance of :class:`TextWrapper` for efficiency. -.. function:: wrap(text, width=70, **kwargs) +.. function:: wrap(text, width=70, *, initial_indent="", \ + subsequent_indent="", expand_tabs=True, \ + replace_whitespace=True, fix_sentence_endings=False, \ + break_long_words=True, drop_whitespace=True, \ + break_on_hyphens=True, tabsize=8, max_lines=None, \ + placeholder=' [...]') Wraps the single paragraph in *text* (a string) so every line is at most *width* characters long. Returns a list of output lines, without final newlines. Optional keyword arguments correspond to the instance attributes of - :class:`TextWrapper`, documented below. *width* defaults to ``70``. + :class:`TextWrapper`, documented below. See the :meth:`TextWrapper.wrap` method for additional details on how :func:`wrap` behaves. -.. function:: fill(text, width=70, **kwargs) +.. function:: fill(text, width=70, *, initial_indent="", \ + subsequent_indent="", expand_tabs=True, \ + replace_whitespace=True, fix_sentence_endings=False, \ + break_long_words=True, drop_whitespace=True, \ + break_on_hyphens=True, tabsize=8, \ + max_lines=None, placeholder=' [...]') Wraps the single paragraph in *text*, and returns a single string containing the wrapped paragraph. :func:`fill` is shorthand for :: @@ -41,7 +51,9 @@ functions should be good enough; otherwise, you should use an instance of :func:`wrap`. -.. function:: shorten(text, width, **kwargs) +.. function:: shorten(text, width, *, fix_sentence_endings=False, \ + break_long_words=True, break_on_hyphens=True, \ + placeholder=' [...]') Collapse and truncate the given *text* to fit in the given *width*. @@ -65,7 +77,6 @@ functions should be good enough; otherwise, you should use an instance of .. versionadded:: 3.4 - .. function:: dedent(text) Remove any common leading whitespace from every line in *text*. diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst index 16f23c3a0c3548..14434fbd52d082 100644 --- a/Doc/library/threading.rst +++ b/Doc/library/threading.rst @@ -121,10 +121,11 @@ This module defines the following functions: .. function:: enumerate() - Return a list of all :class:`Thread` objects currently alive. The list - includes daemonic threads, dummy thread objects created by - :func:`current_thread`, and the main thread. It excludes terminated threads - and threads that have not yet been started. + Return a list of all :class:`Thread` objects currently active. The list + includes daemonic threads and dummy thread objects created by + :func:`current_thread`. It excludes terminated threads and threads + that have not yet been started. However, the main thread is always part + of the result, even when terminated. .. function:: main_thread() @@ -427,8 +428,8 @@ since it is impossible to detect the termination of alien threads. .. attribute:: daemon - A boolean value indicating whether this thread is a daemon thread (True) - or not (False). This must be set before :meth:`~Thread.start` is called, + A boolean value indicating whether this thread is a daemon thread (``True``) + or not (``False``). This must be set before :meth:`~Thread.start` is called, otherwise :exc:`RuntimeError` is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to @@ -502,7 +503,7 @@ All methods are executed atomically. value, block for at most the number of seconds specified by *timeout* and as long as the lock cannot be acquired. A *timeout* argument of ``-1`` specifies an unbounded wait. It is forbidden to specify a *timeout* - when *blocking* is false. + when *blocking* is ``False``. The return value is ``True`` if the lock is acquired successfully, ``False`` if not (for example if the *timeout* expired). @@ -530,7 +531,7 @@ All methods are executed atomically. .. method:: locked() - Return true if the lock is acquired. + Return ``True`` if the lock is acquired. @@ -579,17 +580,17 @@ Reentrant locks also support the :ref:`context management protocol ` is unlocked, only one at a time will be able to grab ownership of the lock. There is no return value in this case. - When invoked with the *blocking* argument set to true, do the same thing as when + When invoked with the *blocking* argument set to ``True``, do the same thing as when called without arguments, and return ``True``. - When invoked with the *blocking* argument set to false, do not block. If a call + When invoked with the *blocking* argument set to ``False``, do not block. If a call without an argument would block, return ``False`` immediately; otherwise, do the same thing as when called without arguments, and return ``True``. When invoked with the floating-point *timeout* argument set to a positive value, block for at most the number of seconds specified by *timeout* and as long as the lock cannot be acquired. Return ``True`` if the lock has - been acquired, false if the timeout has elapsed. + been acquired, ``False`` if the timeout has elapsed. .. versionchanged:: 3.2 The *timeout* parameter is new. @@ -830,7 +831,7 @@ Semaphores also support the :ref:`context management protocol `. thread will be awoken by each call to :meth:`~Semaphore.release`. The order in which threads are awoken should not be relied on. - When invoked with *blocking* set to false, do not block. If a call + When invoked with *blocking* set to ``False``, do not block. If a call without an argument would block, return ``False`` immediately; otherwise, do the same thing as when called without arguments, and return ``True``. diff --git a/Doc/library/time.rst b/Doc/library/time.rst index cfd67e87501cd4..ed1f71dea4a0dc 100644 --- a/Doc/library/time.rst +++ b/Doc/library/time.rst @@ -259,6 +259,12 @@ Functions :const:`None`, the current time as returned by :func:`.time` is used. The dst flag is set to ``1`` when DST applies to the given time. + :func:`localtime` may raise :exc:`OverflowError`, if the timestamp is + outside the range of values supported by the platform C :c:func:`localtime` + or :c:func:`gmtime` functions, and :exc:`OSError` on :c:func:`localtime` or + :c:func:`gmtime` failure. It's common for this to be restricted to years + between 1970 and 2038. + .. function:: mktime(t) @@ -456,10 +462,10 @@ Functions | | negative time difference from UTC/GMT of the | | | | form +HHMM or -HHMM, where H represents decimal| | | | hour digits and M represents decimal minute | | - | | digits [-23:59, +23:59]. | | + | | digits [-23:59, +23:59]. [1]_ | | +-----------+------------------------------------------------+-------+ | ``%Z`` | Time zone name (no characters if no time zone | | - | | exists). | | + | | exists). Deprecated. [1]_ | | +-----------+------------------------------------------------+-------+ | ``%%`` | A literal ``'%'`` character. | | +-----------+------------------------------------------------+-------+ @@ -480,7 +486,7 @@ Functions calculations when the day of the week and the year are specified. Here is an example, a format for dates compatible with that specified in the - :rfc:`2822` Internet email standard. [#]_ :: + :rfc:`2822` Internet email standard. [1]_ :: >>> from time import gmtime, strftime >>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) @@ -908,10 +914,9 @@ Timezone Constants .. rubric:: Footnotes -.. [#] The use of ``%Z`` is now deprecated, but the ``%z`` escape that expands to the - preferred hour/minute offset is not supported by all ANSI C libraries. Also, a +.. [1] The use of ``%Z`` is now deprecated, but the ``%z`` escape that expands to the + preferred hour/minute offset is not supported by all ANSI C libraries. Also, a strict reading of the original 1982 :rfc:`822` standard calls for a two-digit - year (%y rather than %Y), but practice moved to 4-digit years long before the + year (``%y`` rather than ``%Y``), but practice moved to 4-digit years long before the year 2000. After that, :rfc:`822` became obsolete and the 4-digit year has been first recommended by :rfc:`1123` and then mandated by :rfc:`2822`. - diff --git a/Doc/library/timeit.rst b/Doc/library/timeit.rst index 668fcb860cea87..ca21fe622323ff 100644 --- a/Doc/library/timeit.rst +++ b/Doc/library/timeit.rst @@ -15,8 +15,8 @@ This module provides a simple way to time small bits of Python code. It has both a :ref:`timeit-command-line-interface` as well as a :ref:`callable ` one. It avoids a number of common traps for measuring execution times. -See also Tim Peters' introduction to the "Algorithms" chapter in the *Python -Cookbook*, published by O'Reilly. +See also Tim Peters' introduction to the "Algorithms" chapter in the second +edition of *Python Cookbook*, published by O'Reilly. Basic Examples @@ -282,6 +282,13 @@ It is possible to provide a setup statement that is executed only once at the be $ python -m timeit -s 'text = "sample string"; char = "g"' 'text.find(char)' 1000000 loops, best of 5: 0.342 usec per loop +In the output, there are three fields. The loop count, which tells you how many +times the statement body was run per timing loop repetition. The repetition +count ('best of 5') which tells you how many times the timing loop was +repeated, and finally the time the statement body took on average within the +best repetition of the timing loop. That is, the time the fastest repetition +took divided by the loop count. + :: >>> import timeit diff --git a/Doc/library/tk.rst b/Doc/library/tk.rst index c6c73f057cab16..0cb8fda4e32ebb 100644 --- a/Doc/library/tk.rst +++ b/Doc/library/tk.rst @@ -19,16 +19,15 @@ The :mod:`tkinter` package is a thin object-oriented layer on top of Tcl/Tk. To use :mod:`tkinter`, you don't need to write Tcl code, but you will need to consult the Tk documentation, and occasionally the Tcl documentation. :mod:`tkinter` is a set of wrappers that implement the Tk widgets as Python -classes. In addition, the internal module :mod:`_tkinter` provides a threadsafe -mechanism which allows Python and Tcl to interact. +classes. :mod:`tkinter`'s chief virtues are that it is fast, and that it usually comes bundled with Python. Although its standard documentation is weak, good material is available, which includes: references, tutorials, a book and others. :mod:`tkinter` is also famous for having an outdated look and feel, which has been vastly improved in Tk 8.5. Nevertheless, there are many other -GUI libraries that you could be interested in. For more information about -alternatives, see the :ref:`other-gui-packages` section. +GUI libraries that you could be interested in. The Python wiki lists several +alternative `GUI frameworks and tools `_. .. toctree:: @@ -42,7 +41,6 @@ alternatives, see the :ref:`other-gui-packages` section. tkinter.ttk.rst tkinter.tix.rst idle.rst - othergui.rst .. Other sections I have in mind are Tkinter internals diff --git a/Doc/library/tk_msg.png b/Doc/library/tk_msg.png index c122d8f8ae5ba9..6495e63e84650d 100644 Binary files a/Doc/library/tk_msg.png and b/Doc/library/tk_msg.png differ diff --git a/Doc/library/tkinter.rst b/Doc/library/tkinter.rst index 7739f2f60a7980..096a343bd95589 100644 --- a/Doc/library/tkinter.rst +++ b/Doc/library/tkinter.rst @@ -11,92 +11,185 @@ -------------- The :mod:`tkinter` package ("Tk interface") is the standard Python interface to -the Tk GUI toolkit. Both Tk and :mod:`tkinter` are available on most Unix -platforms, as well as on Windows systems. (Tk itself is not part of Python; it -is maintained at ActiveState.) +the Tcl/Tk GUI toolkit. Both Tk and :mod:`tkinter` are available on most Unix +platforms, including macOS, as well as on Windows systems. Running ``python -m tkinter`` from the command line should open a window demonstrating a simple Tk interface, letting you know that :mod:`tkinter` is properly installed on your system, and also showing what version of Tcl/Tk is installed, so you can read the Tcl/Tk documentation specific to that version. -.. seealso:: +Tkinter supports a range of Tcl/Tk versions, built either with or +without thread support. The official Python binary release bundles Tcl/Tk 8.6 +threaded. See the source code for the :mod:`_tkinter` module +for more information about supported versions. - Tkinter documentation: +Tkinter is not a thin wrapper, but adds a fair amount of its own logic to +make the experience more pythonic. This documentation will concentrate on these +additions and changes, and refer to the official Tcl/Tk documentation for +details that are unchanged. - `Python Tkinter Resources `_ - The Python Tkinter Topic Guide provides a great deal of information on using Tk - from Python and links to other sources of information on Tk. +.. note:: - `TKDocs `_ - Extensive tutorial plus friendlier widget pages for some of the widgets. + Tcl/Tk 8.5 (2007) introduced a modern set of themed user interface components + along with a new API to use them. Both old and new APIs are still available. + Most documentation you will find online still uses the old API and + can be woefully outdated. - `Tkinter 8.5 reference: a GUI for Python `_ - On-line reference material. +.. seealso:: - `Tkinter docs from effbot `_ - Online reference for tkinter supported by effbot.org. + * `TkDocs `_ + Extensive tutorial on creating user interfaces with Tkinter. Explains key concepts, + and illustrates recommended approaches using the modern API. - `Programming Python `_ - Book by Mark Lutz, has excellent coverage of Tkinter. + * `Tkinter 8.5 reference: a GUI for Python `_ + Reference documentation for Tkinter 8.5 detailing available classes, methods, and options. - `Modern Tkinter for Busy Python Developers `_ - Book by Mark Roseman about building attractive and modern graphical user interfaces with Python and Tkinter. + Tcl/Tk Resources: - `Python and Tkinter Programming `_ - Book by John Grayson (ISBN 1-884777-81-3). + * `Tk commands `_ + Comprehensive reference to each of the underlying Tcl/Tk commands used by Tkinter. - Tcl/Tk documentation: + * `Tcl/Tk Home Page `_ + Additional documentation, and links to Tcl/Tk core development. - `Tk commands `_ - Most commands are available as :mod:`tkinter` or :mod:`tkinter.ttk` classes. - Change '8.6' to match the version of your Tcl/Tk installation. + Books: - `Tcl/Tk recent man pages `_ - Recent Tcl/Tk manuals on www.tcl.tk. + * `Modern Tkinter for Busy Python Developers `_ + By Mark Roseman. (ISBN 978-1999149567) - `ActiveState Tcl Home Page `_ - The Tk/Tcl development is largely taking place at ActiveState. + * `Python and Tkinter Programming `_ + By Alan Moore. (ISBN 978-1788835886) - `Tcl and the Tk Toolkit `_ - Book by John Ousterhout, the inventor of Tcl. + * `Programming Python `_ + By Mark Lutz; has excellent coverage of Tkinter. (ISBN 978-0596158101) - `Practical Programming in Tcl and Tk `_ - Brent Welch's encyclopedic book. + * `Tcl and the Tk Toolkit (2nd edition) `_ + By John Ousterhout, inventor of Tcl/Tk, and Ken Jones; does not cover Tkinter. (ISBN 978-0321336330) -Tkinter Modules ---------------- +Architecture +------------ -Most of the time, :mod:`tkinter` is all you really need, but a number of -additional modules are available as well. The Tk interface is located in a -binary module named :mod:`_tkinter`. This module contains the low-level -interface to Tk, and should never be used directly by application programmers. -It is usually a shared library (or DLL), but might in some cases be statically -linked with the Python interpreter. +Tcl/Tk is not a single library but rather consists of a few distinct +modules, each with separate functionality and its own official +documentation. Python's binary releases also ship an add-on module +together with it. -In addition to the Tk interface module, :mod:`tkinter` includes a number of -Python modules, :mod:`tkinter.constants` being one of the most important. -Importing :mod:`tkinter` will automatically import :mod:`tkinter.constants`, -so, usually, to use Tkinter all you need is a simple import statement:: +Tcl + Tcl is a dynamic interpreted programming language, just like Python. Though + it can be used on its own as a general-purpose programming language, it is + most commonly embedded into C applications as a scripting engine or an + interface to the Tk toolkit. The Tcl library has a C interface to + create and manage one or more instances of a Tcl interpreter, run Tcl + commands and scripts in those instances, and add custom commands + implemented in either Tcl or C. Each interpreter has an event queue, + and there are facilities to send events to it and process them. + Unlike Python, Tcl's execution model is designed around cooperative + multitasking, and Tkinter bridges this difference + (see `Threading model`_ for details). - import tkinter +Tk + Tk is a `Tcl package `_ implemented in C + that adds custom commands to create and manipulate GUI widgets. Each + :class:`Tk` object embeds its own Tcl interpreter instance with Tk loaded into + it. Tk's widgets are very customizable, though at the cost of a dated appearance. + Tk uses Tcl's event queue to generate and process GUI events. -Or, more often:: +Ttk + Themed Tk (Ttk) is a newer family of Tk widgets that provide a much better + appearance on different platforms than many of the classic Tk widgets. + Ttk is distributed as part of Tk, starting with Tk version 8.5. Python + bindings are provided in a separate module, :mod:`tkinter.ttk`. - from tkinter import * +Internally, Tk and Ttk use facilities of the underlying operating system, +i.e., Xlib on Unix/X11, Cocoa on macOS, GDI on Windows. +When your Python application uses a class in Tkinter, e.g., to create a widget, +the :mod:`tkinter` module first assembles a Tcl/Tk command string. It passes that +Tcl command string to an internal :mod:`_tkinter` binary module, which then +calls the Tcl interpreter to evaluate it. The Tcl interpreter will then call into the +Tk and/or Ttk packages, which will in turn make calls to Xlib, Cocoa, or GDI. -.. class:: Tk(screenName=None, baseName=None, className='Tk', useTk=1) - The :class:`Tk` class is instantiated without arguments. This creates a toplevel - widget of Tk which usually is the main window of an application. Each instance - has its own associated Tcl interpreter. +Tkinter Modules +--------------- - .. FIXME: The following keyword arguments are currently recognized: +Support for Tkinter is spread across several modules. Most applications will need the +main :mod:`tkinter` module, as well as the :mod:`tkinter.ttk` module, which provides +the modern themed widget set and API:: -.. function:: Tcl(screenName=None, baseName=None, className='Tk', useTk=0) + from tkinter import * + from tkinter import ttk + + +.. class:: Tk(screenName=None, baseName=None, className='Tk', useTk=True, sync=False, use=None) + + Construct a toplevel Tk widget, which is usually the main window of an + application, and initialize a Tcl interpreter for this widget. Each + instance has its own associated Tcl interpreter. + + The :class:`Tk` class is typically instantiated using all default values. + However, the following keyword arguments are currently recognized: + + *screenName* + When given (as a string), sets the :envvar:`DISPLAY` environment + variable. (X11 only) + *baseName* + Name of the profile file. By default, *baseName* is derived from the + program name (``sys.argv[0]``). + *className* + Name of the widget class. Used as a profile file and also as the name + with which Tcl is invoked (*argv0* in *interp*). + *useTk* + If ``True``, initialize the Tk subsystem. The :func:`tkinter.Tcl() ` + function sets this to ``False``. + *sync* + If ``True``, execute all X server commands synchronously, so that errors + are reported immediately. Can be used for debugging. (X11 only) + *use* + Specifies the *id* of the window in which to embed the application, + instead of it being created as an independent toplevel window. *id* must + be specified in the same way as the value for the -use option for + toplevel widgets (that is, it has a form like that returned by + :meth:`winfo_id`). + + Note that on some platforms this will only work correctly if *id* refers + to a Tk frame or toplevel that has its -container option enabled. + + :class:`Tk` reads and interprets profile files, named + :file:`.{className}.tcl` and :file:`.{baseName}.tcl`, into the Tcl + interpreter and calls :func:`exec` on the contents of + :file:`.{className}.py` and :file:`.{baseName}.py`. The path for the + profile files is the :envvar:`HOME` environment variable or, if that + isn't defined, then :attr:`os.curdir`. + + .. attribute:: tk + + The Tk application object created by instantiating :class:`Tk`. This + provides access to the Tcl interpreter. Each widget that is attached + the same instance of :class:`Tk` has the same value for its :attr:`tk` + attribute. + + .. attribute:: master + + The widget object that contains this widget. For :class:`Tk`, the + *master* is :const:`None` because it is the main window. The terms + *master* and *parent* are similar and sometimes used interchangeably + as argument names; however, calling :meth:`winfo_parent` returns a + string of the widget name whereas :attr:`master` returns the object. + *parent*/*child* reflects the tree-like relationship while + *master*/*slave* reflects the container structure. + + .. attribute:: children + + The immediate descendants of this widget as a :class:`dict` with the + child widget names as the keys and the child instance objects as the + values. + + +.. function:: Tcl(screenName=None, baseName=None, className='Tk', useTk=False) The :func:`Tcl` function is a factory function which creates an object much like that created by the :class:`Tk` class, except that it does not initialize the Tk @@ -107,7 +200,10 @@ Or, more often:: subsystem initialized) by calling its :meth:`loadtk` method. -Other modules that provide Tk support include: +The modules that provide Tk support include: + +:mod:`tkinter` + Main Tkinter module. :mod:`tkinter.colorchooser` Dialog to let the user choose a color. @@ -130,9 +226,35 @@ Other modules that provide Tk support include: :mod:`tkinter.simpledialog` Basic dialogs and convenience functions. +:mod:`tkinter.ttk` + Themed widget set introduced in Tk 8.5, providing modern alternatives + for many of the classic widgets in the main :mod:`tkinter` module. + +Additional modules: + +:mod:`_tkinter` + A binary module that contains the low-level interface to Tcl/Tk. + It is automatically imported by the main :mod:`tkinter` module, + and should never be used directly by application programmers. + It is usually a shared library (or DLL), but might in some cases be + statically linked with the Python interpreter. + +:mod:`idlelib` + Python's Integrated Development and Learning Environment (IDLE). Based + on :mod:`tkinter`. + +:mod:`tkinter.constants` + Symbolic constants that can be used in place of strings when passing + various parameters to Tkinter calls. Automatically imported by the + main :mod:`tkinter` module. + :mod:`tkinter.dnd` - Drag-and-drop support for :mod:`tkinter`. This is experimental and should - become deprecated when it is replaced with the Tk DND. + (experimental) Drag-and-drop support for :mod:`tkinter`. This will + become deprecated when it is replaced with the Tk DND. + +:mod:`tkinter.tix` + (deprecated) An older third-party Tcl/Tk package that adds several new + widgets. Better alternatives for most can be found in :mod:`tkinter.ttk`. :mod:`turtle` Turtle graphics in a Tk window. @@ -141,243 +263,294 @@ Other modules that provide Tk support include: Tkinter Life Preserver ---------------------- -.. sectionauthor:: Matt Conway +This section is not designed to be an exhaustive tutorial on either Tk or +Tkinter. For that, refer to one of the external resources noted earlier. +Instead, this section provides a very quick orientation to what a Tkinter +application looks like, identifies foundational Tk concepts, and +explains how the Tkinter wrapper is structured. + +The remainder of this section will help you to identify the classes, +methods, and options you'll need in your Tkinter application, and where to +find more detailed documentation on them, including in the official Tcl/Tk +reference manual. -This section is not designed to be an exhaustive tutorial on either Tk or -Tkinter. Rather, it is intended as a stop gap, providing some introductory -orientation on the system. +A Hello World Program +^^^^^^^^^^^^^^^^^^^^^ -Credits: +We'll start by walking through a "Hello World" application in Tkinter. This +isn't the smallest one we could write, but has enough to illustrate some +key concepts you'll need to know. -* Tk was written by John Ousterhout while at Berkeley. +:: -* Tkinter was written by Steen Lumholt and Guido van Rossum. + from tkinter import * + from tkinter import ttk + root = Tk() + frm = ttk.Frame(root, padding=10) + frm.grid() + ttk.Label(frm, text="Hello World!").grid(column=0, row=0) + ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0) + root.mainloop() -* This Life Preserver was written by Matt Conway at the University of Virginia. -* The HTML rendering, and some liberal editing, was produced from a FrameMaker - version by Ken Manheimer. +After the imports, the next line creates an instance of the :class:`Tk` class, +which initializes Tk and creates its associated Tcl interpreter. It also +creates a toplevel window, known as the root window, which serves as the main +window of the application. -* Fredrik Lundh elaborated and revised the class interface descriptions, to get - them current with Tk 4.2. +The following line creates a frame widget, which in this case will contain +a label and a button we'll create next. The frame is fit inside the root +window. -* Mike Clarkson converted the documentation to LaTeX, and compiled the User - Interface chapter of the reference manual. +The next line creates a label widget holding a static text string. The +:meth:`grid` method is used to specify the relative layout (position) of the +label within its containing frame widget, similar to how tables in HTML work. +A button widget is then created, and placed to the right of the label. When +pressed, it will call the :meth:`destroy` method of the root window. -How To Use This Section -^^^^^^^^^^^^^^^^^^^^^^^ +Finally, the :meth:`mainloop` method puts everything on the display, and +responds to user input until the program terminates. -This section is designed in two parts: the first half (roughly) covers -background material, while the second half can be taken to the keyboard as a -handy reference. -When trying to answer questions of the form "how do I do blah", it is often best -to find out how to do "blah" in straight Tk, and then convert this back into the -corresponding :mod:`tkinter` call. Python programmers can often guess at the -correct Python command by looking at the Tk documentation. This means that in -order to use Tkinter, you will have to know a little bit about Tk. This document -can't fulfill that role, so the best we can do is point you to the best -documentation that exists. Here are some hints: -* The authors strongly suggest getting a copy of the Tk man pages. - Specifically, the man pages in the ``manN`` directory are most useful. - The ``man3`` man pages describe the C interface to the Tk library and thus - are not especially helpful for script writers. +Important Tk Concepts +^^^^^^^^^^^^^^^^^^^^^ -* Addison-Wesley publishes a book called Tcl and the Tk Toolkit by John - Ousterhout (ISBN 0-201-63337-X) which is a good introduction to Tcl and Tk for - the novice. The book is not exhaustive, and for many details it defers to the - man pages. +Even this simple program illustrates the following key Tk concepts: -* :file:`tkinter/__init__.py` is a last resort for most, but can be a good - place to go when nothing else makes sense. +widgets + A Tkinter user interface is made up of individual *widgets*. Each widget is + represented as a Python object, instantiated from classes like + :class:`ttk.Frame`, :class:`ttk.Label`, and :class:`ttk.Button`. +widget hierarchy + Widgets are arranged in a *hierarchy*. The label and button were contained + within a frame, which in turn was contained within the root window. When + creating each *child* widget, its *parent* widget is passed as the first + argument to the widget constructor. -A Simple Hello World Program -^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +configuration options + Widgets have *configuration options*, which modify their appearance and + behavior, such as the text to display in a label or button. Different + classes of widgets will have different sets of options. -:: +geometry management + Widgets aren't automatically added to the user interface when they are + created. A *geometry manager* like ``grid`` controls where in the + user interface they are placed. - import tkinter as tk +event loop + Tkinter reacts to user input, changes from your program, and even refreshes + the display only when actively running an *event loop*. If your program + isn't running the event loop, your user interface won't update. - class Application(tk.Frame): - def __init__(self, master=None): - super().__init__(master) - self.master = master - self.pack() - self.create_widgets() - def create_widgets(self): - self.hi_there = tk.Button(self) - self.hi_there["text"] = "Hello World\n(click me)" - self.hi_there["command"] = self.say_hi - self.hi_there.pack(side="top") +Understanding How Tkinter Wraps Tcl/Tk +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - self.quit = tk.Button(self, text="QUIT", fg="red", - command=self.master.destroy) - self.quit.pack(side="bottom") +When your application uses Tkinter's classes and methods, internally Tkinter +is assembling strings representing Tcl/Tk commands, and executing those +commands in the Tcl interpreter attached to your applicaton's :class:`Tk` +instance. - def say_hi(self): - print("hi there, everyone!") +Whether it's trying to navigate reference documentation, trying to find +the right method or option, adapting some existing code, or debugging your +Tkinter application, there are times that it will be useful to understand +what those underlying Tcl/Tk commands look like. - root = tk.Tk() - app = Application(master=root) - app.mainloop() +To illustrate, here is the Tcl/Tk equivalent of the main part of the Tkinter +script above. +:: -A (Very) Quick Look at Tcl/Tk ------------------------------ + ttk::frame .frm -padding 10 + grid .frm + grid [ttk::label .frm.lbl -text "Hello World!"] -column 0 -row 0 + grid [ttk::button .frm.btn -text "Quit" -command "destroy ."] -column 1 -row 0 -The class hierarchy looks complicated, but in actual practice, application -programmers almost always refer to the classes at the very bottom of the -hierarchy. -Notes: +Tcl's syntax is similar to many shell languages, where the first word is the +command to be executed, with arguments to that command following it, separated +by spaces. Without getting into too many details, notice the following: -* These classes are provided for the purposes of organizing certain functions - under one namespace. They aren't meant to be instantiated independently. +* The commands used to create widgets (like ``ttk::frame``) correspond to + widget classes in Tkinter. -* The :class:`Tk` class is meant to be instantiated only once in an application. - Application programmers need not instantiate one explicitly, the system creates - one whenever any of the other classes are instantiated. +* Tcl widget options (like ``-text``) correspond to keyword arguments in + Tkinter. -* The :class:`Widget` class is not meant to be instantiated, it is meant only - for subclassing to make "real" widgets (in C++, this is called an 'abstract - class'). +* Widgets are referred to by a *pathname* in Tcl (like ``.frm.btn``), + whereas Tkinter doesn't use names but object references. -To make use of this reference material, there will be times when you will need -to know how to read short passages of Tk and how to identify the various parts -of a Tk command. (See section :ref:`tkinter-basic-mapping` for the -:mod:`tkinter` equivalents of what's below.) +* A widget's place in the widget hierarchy is encoded in its (hierarchical) + pathname, which uses a ``.`` (dot) as a path separator. The pathname for + the root window is just ``.`` (dot). In Tkinter, the hierarchy is defined + not by pathname but by specifying the parent widget when creating each + child widget. -Tk scripts are Tcl programs. Like all Tcl programs, Tk scripts are just lists -of tokens separated by spaces. A Tk widget is just its *class*, the *options* -that help configure it, and the *actions* that make it do useful things. +* Operations which are implemented as separate *commands* in Tcl (like + ``grid`` or ``destroy``) are represented as *methods* on Tkinter widget + objects. As you'll see shortly, at other times Tcl uses what appear to be + method calls on widget objects, which more closely mirror what would is + used in Tkinter. -To make a widget in Tk, the command is always of the form:: - classCommand newPathname options +How do I...? What option does...? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -*classCommand* - denotes which kind of widget to make (a button, a label, a menu...) +If you're not sure how to do something in Tkinter, and you can't immediately +find it in the tutorial or reference documentation you're using, there are a +few strategies that can be helpful. -.. index:: single: . (dot); in Tkinter +First, remember that the details of how individual widgets work may vary +across different versions of both Tkinter and Tcl/Tk. If you're searching +documentation, make sure it corresponds to the Python and Tcl/Tk versions +installed on your system. -*newPathname* - is the new name for this widget. All names in Tk must be unique. To help - enforce this, widgets in Tk are named with *pathnames*, just like files in a - file system. The top level widget, the *root*, is called ``.`` (period) and - children are delimited by more periods. For example, - ``.myApp.controlPanel.okButton`` might be the name of a widget. +When searching for how to use an API, it helps to know the exact name of the +class, option, or method that you're using. Introspection, either in an +interactive Python shell or with :func:`print`, can help you identify what +you need. -*options* - configure the widget's appearance and in some cases, its behavior. The options - come in the form of a list of flags and values. Flags are preceded by a '-', - like Unix shell command flags, and values are put in quotes if they are more - than one word. +To find out what configuration options are available on any widget, call its +:meth:`configure` method, which returns a dictionary containing a variety of +information about each object, including its default and current values. Use +:meth:`keys` to get just the names of each option. -For example:: +:: - button .fred -fg red -text "hi there" - ^ ^ \______________________/ - | | | - class new options - command widget (-opt val -opt val ...) + btn = ttk.Button(frm, ...) + print(btn.configure().keys()) -Once created, the pathname to the widget becomes a new command. This new -*widget command* is the programmer's handle for getting the new widget to -perform some *action*. In C, you'd express this as someAction(fred, -someOptions), in C++, you would express this as fred.someAction(someOptions), -and in Tk, you say:: +As most widgets have many configuration options in common, it can be useful +to find out which are specific to a particular widget class. Comparing the +list of options to that of a simpler widget, like a frame, is one way to +do that. - .fred someAction someOptions +:: -Note that the object name, ``.fred``, starts with a dot. + print(set(btn.configure().keys()) - set(frm.configure().keys())) -As you'd expect, the legal values for *someAction* will depend on the widget's -class: ``.fred disable`` works if fred is a button (fred gets greyed out), but -does not work if fred is a label (disabling of labels is not supported in Tk). +Similarly, you can find the available methods for a widget object using the +standard :func:`dir` function. If you try it, you'll see there are over 200 +common widget methods, so again identifying those specific to a widget class +is helpful. -The legal values of *someOptions* is action dependent. Some actions, like -``disable``, require no arguments, others, like a text-entry box's ``delete`` -command, would need arguments to specify what range of text to delete. +:: + print(dir(btn)) + print(set(dir(btn)) - set(dir(frm))) -.. _tkinter-basic-mapping: -Mapping Basic Tk into Tkinter ------------------------------ +Navigating the Tcl/Tk Reference Manual +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Class commands in Tk correspond to class constructors in Tkinter. :: +As noted, the official `Tk commands `_ +reference manual (man pages) is often the most accurate description of what +specific operations on widgets do. Even when you know the name of the option +or method that you need, you may still have a few places to look. - button .fred =====> fred = Button() +While all operations in Tkinter are implemented as method calls on widget +objects, you've seen that many Tcl/Tk operations appear as commands that +take a widget pathname as its first parameter, followed by optional +parameters, e.g. -The master of an object is implicit in the new name given to it at creation -time. In Tkinter, masters are specified explicitly. :: +:: + + destroy . + grid .frm.btn -column 0 -row 0 + +Others, however, look more like methods called on a widget object (in fact, +when you create a widget in Tcl/Tk, it creates a Tcl command with the name +of the widget pathname, with the first parameter to that command being the +name of a method to call). + +:: + + .frm.btn invoke + .frm.lbl configure -text "Goodbye" - button .panel.fred =====> fred = Button(panel) -The configuration options in Tk are given in lists of hyphened tags followed by -values. In Tkinter, options are specified as keyword-arguments in the instance -constructor, and keyword-args for configure calls or as instance indices, in -dictionary style, for established instances. See section -:ref:`tkinter-setting-options` on setting options. :: +In the official Tcl/Tk reference documentation, you'll find most operations +that look like method calls on the man page for a specific widget (e.g., +you'll find the :meth:`invoke` method on the +`ttk::button `_ +man page), while functions that take a widget as a parameter often have +their own man page (e.g., +`grid `_). - button .fred -fg red =====> fred = Button(panel, fg="red") - .fred configure -fg red =====> fred["fg"] = red - OR ==> fred.config(fg="red") +You'll find many common options and methods in the +`options `_ or +`ttk::widget `_ man +pages, while others are found in the man page for a specific widget class. -In Tk, to perform an action on a widget, use the widget name as a command, and -follow it with an action name, possibly with arguments (options). In Tkinter, -you call methods on the class instance to invoke actions on the widget. The -actions (methods) that a given widget can perform are listed in -:file:`tkinter/__init__.py`. :: +You'll also find that many Tkinter methods have compound names, e.g., +:func:`winfo_x`, :func:`winfo_height`, :func:`winfo_viewable`. You'd find +documentation for all of these in the +`winfo `_ man page. - .fred invoke =====> fred.invoke() +.. note:: + Somewhat confusingly, there are also methods on all Tkinter widgets + that don't actually operate on the widget, but operate at a global + scope, independent of any widget. Examples are methods for accessing + the clipboard or the system bell. (They happen to be implemented as + methods in the base :class:`Widget` class that all Tkinter widgets + inherit from). -To give a widget to the packer (geometry manager), you call pack with optional -arguments. In Tkinter, the Pack class holds all this functionality, and the -various forms of the pack command are implemented as methods. All widgets in -:mod:`tkinter` are subclassed from the Packer, and so inherit all the packing -methods. See the :mod:`tkinter.tix` module documentation for additional -information on the Form geometry manager. :: - pack .fred -side left =====> fred.pack(side="left") +Threading model +--------------- + +Python and Tcl/Tk have very different threading models, which :mod:`tkinter` +tries to bridge. If you use threads, you may need to be aware of this. +A Python interpreter may have many threads associated with it. In Tcl, multiple +threads can be created, but each thread has a separate Tcl interpreter instance +associated with it. Threads can also create more than one interpreter instance, +though each interpreter instance can be used only by the one thread that created it. -How Tk and Tkinter are Related ------------------------------- +Each :class:`Tk` object created by :mod:`tkinter` contains a Tcl interpreter. +It also keeps track of which thread created that interpreter. Calls to +:mod:`tkinter` can be made from any Python thread. Internally, if a call comes +from a thread other than the one that created the :class:`Tk` object, an event +is posted to the interpreter's event queue, and when executed, the result is +returned to the calling Python thread. -From the top down: +Tcl/Tk applications are normally event-driven, meaning that after initialization, +the interpreter runs an event loop (i.e. :func:`Tk.mainloop`) and responds to events. +Because it is single-threaded, event handlers must respond quickly, otherwise they +will block other events from being processed. To avoid this, any long-running +computations should not run in an event handler, but are either broken into smaller +pieces using timers, or run in another thread. This is different from many GUI +toolkits where the GUI runs in a completely separate thread from all application +code including event handlers. -Your App Here (Python) - A Python application makes a :mod:`tkinter` call. +If the Tcl interpreter is not running the event loop and processing events, any +:mod:`tkinter` calls made from threads other than the one running the Tcl +interpreter will fail. -tkinter (Python Package) - This call (say, for example, creating a button widget), is implemented in - the :mod:`tkinter` package, which is written in Python. This Python - function will parse the commands and the arguments and convert them into a - form that makes them look as if they had come from a Tk script instead of - a Python script. +A number of special cases exist: -_tkinter (C) - These commands and their arguments will be passed to a C function in the - :mod:`_tkinter` - note the underscore - extension module. + * Tcl/Tk libraries can be built so they are not thread-aware. In this case, + :mod:`tkinter` calls the library from the originating Python thread, even + if this is different than the thread that created the Tcl interpreter. A global + lock ensures only one call occurs at a time. -Tk Widgets (C and Tcl) - This C function is able to make calls into other C modules, including the C - functions that make up the Tk library. Tk is implemented in C and some Tcl. - The Tcl part of the Tk widgets is used to bind certain default behaviors to - widgets, and is executed once at the point where the Python :mod:`tkinter` - package is imported. (The user never sees this stage). + * While :mod:`tkinter` allows you to create more than one instance of a :class:`Tk` + object (with its own interpreter), all interpreters that are part of the same + thread share a common event queue, which gets ugly fast. In practice, don't create + more than one instance of :class:`Tk` at a time. Otherwise, it's best to create + them in separate threads and ensure you're running a thread-aware Tcl/Tk build. -Tk (C) - The Tk part of the Tk Widgets implement the final mapping to ... + * Blocking event handlers are not the only way to prevent the Tcl interpreter from + reentering the event loop. It is even possible to run multiple nested event loops + or abandon the event loop entirely. If you're doing anything tricky when it comes + to events or threads, be aware of these possibilities. -Xlib (C) - the Xlib library to draw graphics on the screen. + * There are a few select :mod:`tkinter` functions that presently work only when + called from the thread that created the Tcl interpreter. Handy Reference @@ -639,8 +812,8 @@ callback color Colors can be given as the names of X colors in the rgb.txt file, or as strings - representing RGB values in 4 bit: ``"#RGB"``, 8 bit: ``"#RRGGBB"``, 12 bit" - ``"#RRRGGGBBB"``, or 16 bit ``"#RRRRGGGGBBBB"`` ranges, where R,G,B here + representing RGB values in 4 bit: ``"#RGB"``, 8 bit: ``"#RRGGBB"``, 12 bit: + ``"#RRRGGGBBB"``, or 16 bit: ``"#RRRRGGGGBBBB"`` ranges, where R,G,B here represent any legal hex digit. See page 160 of Ousterhout's book for details. cursor @@ -704,8 +877,9 @@ of the bind method is:: where: sequence - is a string that denotes the target kind of event. (See the bind man page and - page 201 of John Ousterhout's book for details). + is a string that denotes the target kind of event. (See the + :manpage:`bind(3tk)` man page, and page 201 of John Ousterhout's book, + :title-reference:`Tcl and the Tk Toolkit (2nd edition)`, for details). func is a Python function, taking one argument, to be invoked when the event occurs. diff --git a/Doc/library/tkinter.ttk.rst b/Doc/library/tkinter.ttk.rst index 6967d7509657b3..2db4c0f9143f64 100644 --- a/Doc/library/tkinter.ttk.rst +++ b/Doc/library/tkinter.ttk.rst @@ -759,7 +759,7 @@ ones inherited from :class:`ttk.Widget`. Platform-specific notes ^^^^^^^^^^^^^^^^^^^^^^^ -* On MacOS X, toplevel windows automatically include a built-in size grip +* On macOS, toplevel windows automatically include a built-in size grip by default. Adding a :class:`Sizegrip` is harmless, since the built-in grip will just mask the widget. diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst index 20f668c7282028..68432aeaecbcc1 100644 --- a/Doc/library/tracemalloc.rst +++ b/Doc/library/tracemalloc.rst @@ -740,12 +740,12 @@ Traceback .. method:: format(limit=None, most_recent_first=False) - Format the traceback as a list of lines with newlines. Use the - :mod:`linecache` module to retrieve lines from the source code. - If *limit* is set, format the *limit* most recent frames if *limit* - is positive. Otherwise, format the ``abs(limit)`` oldest frames. - If *most_recent_first* is ``True``, the order of the formatted frames - is reversed, returning the most recent frame first instead of last. + Format the traceback as a list of lines. Use the :mod:`linecache` module to + retrieve lines from the source code. If *limit* is set, format the *limit* + most recent frames if *limit* is positive. Otherwise, format the + ``abs(limit)`` oldest frames. If *most_recent_first* is ``True``, the order + of the formatted frames is reversed, returning the most recent frame first + instead of last. Similar to the :func:`traceback.format_tb` function, except that :meth:`.format` does not include newlines. diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst index 6a9d61916ad1a5..228cf1e01e74ab 100644 --- a/Doc/library/turtle.rst +++ b/Doc/library/turtle.rst @@ -193,8 +193,8 @@ Methods of TurtleScreen/Screen Window control | :func:`bgcolor` | :func:`bgpic` - | :func:`clear` | :func:`clearscreen` - | :func:`reset` | :func:`resetscreen` + | :func:`clearscreen` + | :func:`resetscreen` | :func:`screensize` | :func:`setworldcoordinates` @@ -1069,7 +1069,6 @@ More drawing control ~~~~~~~~~~~~~~~~~~~~ .. function:: reset() - :noindex: Delete the turtle's drawings from the screen, re-center the turtle and set variables to the default values. @@ -1091,7 +1090,6 @@ More drawing control .. function:: clear() - :noindex: Delete the turtle's drawings from the screen. Do not move turtle. State and position of the turtle as well as drawings of other turtles are not affected. @@ -1627,11 +1625,7 @@ Window control .. function:: clear() - clearscreen() - - Delete all drawings and all turtles from the TurtleScreen. Reset the now - empty TurtleScreen to its initial state: white background, no background - image, no event bindings and tracing on. + :noindex: .. note:: This TurtleScreen method is available as a global function only under the @@ -1639,10 +1633,15 @@ Window control derived from the Turtle method ``clear``. -.. function:: reset() - resetscreen() +.. function:: clearscreen() - Reset all Turtles on the Screen to their initial state. + Delete all drawings and all turtles from the TurtleScreen. Reset the now + empty TurtleScreen to its initial state: white background, no background + image, no event bindings and tracing on. + + +.. function:: reset() + :noindex: .. note:: This TurtleScreen method is available as a global function only under the @@ -1650,6 +1649,11 @@ Window control derived from the Turtle method ``reset``. +.. function:: resetscreen() + + Reset all Turtles on the Screen to their initial state. + + .. function:: screensize(canvwidth=None, canvheight=None, bg=None) :param canvwidth: positive integer, new width of canvas in pixels diff --git a/Doc/library/types.rst b/Doc/library/types.rst index b3fac293ecc9e1..5cd42f1fc2928d 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -243,7 +243,7 @@ Standard names are defined for the following types: .. note:: A future version of Python may stop setting this attribute by default. - To guard against this potential change, preferrably read from the + To guard against this potential change, preferably read from the :attr:`__spec__` attribute instead or use ``getattr(module, "__loader__", None)`` if you explicitly need to use this attribute. @@ -268,7 +268,7 @@ Standard names are defined for the following types: .. note:: A future version of Python may stop setting this attribute by default. - To guard against this potential change, preferrably read from the + To guard against this potential change, preferably read from the :attr:`__spec__` attribute instead or use ``getattr(module, "__package__", None)`` if you explicitly need to use this attribute. @@ -278,8 +278,8 @@ Standard names are defined for the following types: .. attribute:: __spec__ - A record of the the module's import-system-related state. Expected to be - an instance of :class:`importlib.machinery.ModuleSpec`. + A record of the module's import-system-related state. Expected to be an + instance of :class:`importlib.machinery.ModuleSpec`. .. versionadded:: 3.4 @@ -312,7 +312,7 @@ Standard names are defined for the following types: This type can now be subclassed. -.. data:: Union +.. class:: UnionType The type of :ref:`union type expressions`. diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 8b1ce34d98ec66..9936f1a3e57c37 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -17,13 +17,10 @@ -------------- -This module provides runtime support for type hints as specified by -:pep:`484`, :pep:`526`, :pep:`544`, :pep:`586`, :pep:`589`, :pep:`591`, -:pep:`612` and :pep:`613`. -The most fundamental support consists of the types :data:`Any`, :data:`Union`, -:data:`Tuple`, :data:`Callable`, :class:`TypeVar`, and -:class:`Generic`. For full specification please see :pep:`484`. For -a simplified introduction to type hints see :pep:`483`. +This module provides runtime support for type hints. The most fundamental +support consists of the types :data:`Any`, :data:`Union`, :data:`Callable`, +:class:`TypeVar`, and :class:`Generic`. For a full specification, please see +:pep:`484`. For a simplified introduction to type hints, see :pep:`483`. The function below takes and returns a string and is annotated as follows:: @@ -35,6 +32,47 @@ In the function ``greeting``, the argument ``name`` is expected to be of type :class:`str` and the return type :class:`str`. Subtypes are accepted as arguments. +New features are frequently added to the ``typing`` module. +The `typing_extensions `_ package +provides backports of these new features to older versions of Python. + +.. _relevant-peps: + +Relevant PEPs +============= + +Since the initial introduction of type hints in :pep:`484` and :pep:`483`, a +number of PEPs have modified and enhanced Python's framework for type +annotations. These include: + +* :pep:`526`: Syntax for Variable Annotations + *Introducing* syntax for annotating variables outside of function + definitions, and :data:`ClassVar` +* :pep:`544`: Protocols: Structural subtyping (static duck typing) + *Introducing* :class:`Protocol` and the + :func:`@runtime_checkable` decorator +* :pep:`585`: Type Hinting Generics In Standard Collections + *Introducing* :class:`types.GenericAlias` and the ability to use standard + library classes as :ref:`generic types` +* :pep:`586`: Literal Types + *Introducing* :data:`Literal` +* :pep:`589`: TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys + *Introducing* :class:`TypedDict` +* :pep:`591`: Adding a final qualifier to typing + *Introducing* :data:`Final` and the :func:`@final` decorator +* :pep:`593`: Flexible function and variable annotations + *Introducing* :data:`Annotated` +* :pep:`604`: Allow writing union types as ``X | Y`` + *Introducing* :data:`types.UnionType` and the ability to use + the binary-or operator ``|`` to signify a + :ref:`union of types` +* :pep:`612`: Parameter Specification Variables + *Introducing* :class:`ParamSpec` and :data:`Concatenate` +* :pep:`613`: Explicit Type Aliases + *Introducing* :data:`TypeAlias` +* :pep:`647`: User-Defined Type Guards + *Introducing* :data:`TypeGuard` + .. _type-aliases: Type aliases @@ -77,7 +115,7 @@ Note that ``None`` as a type hint is a special case and is replaced by NewType ======= -Use the :func:`NewType` helper function to create distinct types:: +Use the :class:`NewType` helper to create distinct types:: from typing import NewType @@ -106,15 +144,14 @@ accidentally creating a ``UserId`` in an invalid way:: Note that these checks are enforced only by the static type checker. At runtime, the statement ``Derived = NewType('Derived', Base)`` will make ``Derived`` a -function that immediately returns whatever parameter you pass it. That means +callable that immediately returns whatever parameter you pass it. That means the expression ``Derived(some_value)`` does not create a new class or introduce -any overhead beyond that of a regular function call. +much overhead beyond that of a regular function call. More precisely, the expression ``some_value is Derived(some_value)`` is always true at runtime. -This also means that it is not possible to create a subtype of ``Derived`` -since it is an identity function at runtime, not an actual type:: +It is invalid to create a subtype of ``Derived``:: from typing import NewType @@ -123,7 +160,7 @@ since it is an identity function at runtime, not an actual type:: # Fails at runtime and does not typecheck class AdminUserId(UserId): pass -However, it is possible to create a :func:`NewType` based on a 'derived' ``NewType``:: +However, it is possible to create a :class:`NewType` based on a 'derived' ``NewType``:: from typing import NewType @@ -151,6 +188,12 @@ See :pep:`484` for more details. .. versionadded:: 3.5.2 +.. versionchanged:: 3.10 + ``NewType`` is now a class rather than a function. There is some additional + runtime cost when calling ``NewType`` over a regular function. However, this + cost will be reduced in 3.11.0. + + Callable ======== @@ -168,6 +211,10 @@ For example:: on_error: Callable[[int, Exception], None]) -> None: # Body + async def on_update(value: str) -> None: + # Body + callback: Callable[[str], Awaitable[None]] = on_update + It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis for the list of arguments in the type hint: ``Callable[..., ReturnType]``. @@ -185,7 +232,7 @@ respectively. See :pep:`612` for more information. .. seealso:: - The documentation for :class:`ParamSpec` and :class:`Concatenate` provide + The documentation for :class:`ParamSpec` and :class:`Concatenate` provides examples of usage in ``Callable``. .. _generics: @@ -204,7 +251,7 @@ subscription to denote expected types for container elements. def notify_by_email(employees: Sequence[Employee], overrides: Mapping[str, str]) -> None: ... -Generics can be parameterized by using a new factory available in typing +Generics can be parameterized by using a factory available in typing called :class:`TypeVar`. :: @@ -217,6 +264,7 @@ called :class:`TypeVar`. def first(l: Sequence[T]) -> T: # Generic function return l[0] +.. _user-defined-generics: User-defined generic types ========================== @@ -251,8 +299,8 @@ A user-defined class can be defined as a generic class. single type parameter ``T`` . This also makes ``T`` valid as a type within the class body. -The :class:`Generic` base class defines :meth:`__class_getitem__` so that -``LoggedVar[t]`` is valid as a type:: +The :class:`Generic` base class defines :meth:`~object.__class_getitem__` so +that ``LoggedVar[t]`` is valid as a type:: from collections.abc import Iterable @@ -260,16 +308,16 @@ The :class:`Generic` base class defines :meth:`__class_getitem__` so that for var in vars: var.set(0) -A generic type can have any number of type variables, and type variables may -be constrained:: +A generic type can have any number of type variables. All varieties of +:class:`TypeVar` are permissible as parameters for a generic type:: - from typing import TypeVar, Generic - ... + from typing import TypeVar, Generic, Sequence - T = TypeVar('T') + T = TypeVar('T', contravariant=True) + B = TypeVar('B', bound=Sequence[bytes], covariant=True) S = TypeVar('S', int, str) - class StrangePair(Generic[T, S]): + class WeirdTrio(Generic[T, B, S]): ... Each type variable argument to :class:`Generic` must be distinct. @@ -316,11 +364,11 @@ not generic but implicitly inherits from ``Iterable[Any]``:: User defined generic type aliases are also supported. Examples:: from collections.abc import Iterable - from typing import TypeVar, Union + from typing import TypeVar S = TypeVar('S') - Response = Union[Iterable[S], int] + Response = Iterable[S] | int - # Return type here is same as Union[Iterable[str], int] + # Return type here is same as Iterable[str] | int def response(query: str) -> Response[str]: ... @@ -353,7 +401,7 @@ to this is that a list of types can be used to substitute a :class:`ParamSpec`:: Furthermore, a generic with only one parameter specification variable will accept parameter lists in the forms ``X[[Type1, Type2, ...]]`` and also ``X[Type1, Type2, ...]`` for aesthetic reasons. Internally, the latter is converted -to the former and are thus equivalent:: +to the former, so the following are equivalent:: >>> class X(Generic[P]): ... ... @@ -388,12 +436,12 @@ value of type :data:`Any` and assign it to any variable:: from typing import Any - a = None # type: Any - a = [] # OK - a = 2 # OK + a: Any = None + a = [] # OK + a = 2 # OK - s = '' # type: str - s = a # OK + s: str = '' + s = a # OK def foo(item: Any) -> int: # Typechecks; 'item' could be any type, @@ -457,7 +505,7 @@ manner. Use :data:`Any` to indicate that a value is dynamically typed. Nominal vs structural subtyping =============================== -Initially :pep:`484` defined Python static type system as using +Initially :pep:`484` defined the Python static type system as using *nominal subtyping*. This means that a class ``A`` is allowed where a class ``B`` is expected if and only if ``A`` is a subclass of ``B``. @@ -583,9 +631,9 @@ These can be used as types in annotations using ``[]``, each having a unique syn .. data:: Union - Union type; ``Union[X, Y]`` means either X or Y. + Union type; ``Union[X, Y]`` is equivalent to ``X | Y`` and means either X or Y. - To define a union, use e.g. ``Union[int, str]``. Details: + To define a union, use e.g. ``Union[int, str]`` or the shorthand ``int | str``. Using that shorthand is recommended. Details: * The arguments must be types and there must be at least one. @@ -599,18 +647,16 @@ These can be used as types in annotations using ``[]``, each having a unique syn * Redundant arguments are skipped, e.g.:: - Union[int, str, int] == Union[int, str] + Union[int, str, int] == Union[int, str] == int | str * When comparing unions, the argument order is ignored, e.g.:: Union[int, str] == Union[str, int] - * You cannot subclass or instantiate a union. + * You cannot subclass or instantiate a ``Union``. * You cannot write ``Union[X][Y]``. - * You can use ``Optional[X]`` as a shorthand for ``Union[X, None]``. - .. versionchanged:: 3.7 Don't remove explicit subclasses from unions at runtime. @@ -622,7 +668,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn Optional type. - ``Optional[X]`` is equivalent to ``Union[X, None]``. + ``Optional[X]`` is equivalent to ``X | None`` (or ``Union[X, None]``). Note that this is not the same concept as an optional argument, which is one that has a default. An optional argument with a @@ -639,6 +685,10 @@ These can be used as types in annotations using ``[]``, each having a unique syn def foo(arg: Optional[int] = None) -> None: ... + .. versionchanged:: 3.10 + Optional can now be written as ``X | None``. See + :ref:`union type expressions`. + .. data:: Callable Callable type; ``Callable[[int], str]`` is a function of (int) -> str. @@ -695,7 +745,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn from collections.abc import Callable from threading import Lock - from typing import Any, Concatenate, ParamSpec, TypeVar + from typing import Concatenate, ParamSpec, TypeVar P = ParamSpec('P') R = TypeVar('R') @@ -706,7 +756,6 @@ These can be used as types in annotations using ``[]``, each having a unique syn def with_lock(f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]: '''A type-safe decorator which provides a lock.''' - global my_lock def inner(*args: P.args, **kwargs: P.kwargs) -> R: # Provide the lock as the first argument. return f(my_lock, *args, **kwargs) @@ -765,7 +814,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn :ref:`type variables `, and unions of any of these types. For example:: - def new_non_team_user(user_class: Type[Union[BasicUser, ProUser]]): ... + def new_non_team_user(user_class: Type[BasicUser | ProUser]): ... ``Type[Any]`` is equivalent to ``Type`` which in turn is equivalent to ``type``, which is the root of Python's metaclass hierarchy. @@ -799,10 +848,10 @@ These can be used as types in annotations using ``[]``, each having a unique syn .. versionadded:: 3.8 .. versionchanged:: 3.9.1 - ``Literal`` now de-duplicates parameters. Equality comparison of + ``Literal`` now de-duplicates parameters. Equality comparisons of ``Literal`` objects are no longer order dependent. ``Literal`` objects will now raise a :exc:`TypeError` exception during equality comparisons - if one of their parameters are not :term:`immutable`. + if one of their parameters are not :term:`hashable`. .. data:: ClassVar @@ -862,7 +911,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn ``no_type_check`` functionality that currently exists in the ``typing`` module which completely disables typechecking annotations on a function or a class, the ``Annotated`` type allows for both static typechecking - of ``T`` (e.g., via mypy or Pyre, which can safely ignore ``x``) + of ``T`` (which can safely ignore ``x``) together with runtime access to ``x`` within a specific application. Ultimately, the responsibility of how to interpret the annotations (if @@ -946,7 +995,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn conditional code flow and applying the narrowing to a block of code. The conditional expression here is sometimes referred to as a "type guard":: - def is_str(val: Union[str, float]): + def is_str(val: str | float): # "isinstance" type guard if isinstance(val, str): # Type of ``val`` is narrowed to ``str`` @@ -966,7 +1015,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn 2. If the return value is ``True``, the type of its argument is the type inside ``TypeGuard``. - For example:: + For example:: def is_str_list(val: List[object]) -> TypeGuard[List[str]]: '''Determines whether all objects in the list are strings''' @@ -1038,7 +1087,8 @@ These are not used in annotations. They are building blocks for creating generic Usage:: T = TypeVar('T') # Can be anything - A = TypeVar('A', str, bytes) # Must be str or bytes + S = TypeVar('S', bound=str) # Can be any subtype of str + A = TypeVar('A', str, bytes) # Must be exactly str or bytes Type variables exist primarily for the benefit of static type checkers. They serve as the parameters for generic types as well @@ -1049,25 +1099,91 @@ These are not used in annotations. They are building blocks for creating generic """Return a list containing n references to x.""" return [x]*n - def longest(x: A, y: A) -> A: - """Return the longest of two strings.""" - return x if len(x) >= len(y) else y - The latter example's signature is essentially the overloading - of ``(str, str) -> str`` and ``(bytes, bytes) -> bytes``. Also note - that if the arguments are instances of some subclass of :class:`str`, - the return type is still plain :class:`str`. + def print_capitalized(x: S) -> S: + """Print x capitalized, and return x.""" + print(x.capitalize()) + return x + + + def concatenate(x: A, y: A) -> A: + """Add two strings or bytes objects together.""" + return x + y + + Note that type variables can be *bound*, *constrained*, or neither, but + cannot be both bound *and* constrained. + + Constrained type variables and bound type variables have different + semantics in several important ways. Using a *constrained* type variable + means that the ``TypeVar`` can only ever be solved as being exactly one of + the constraints given:: + + a = concatenate('one', 'two') # Ok, variable 'a' has type 'str' + b = concatenate(StringSubclass('one'), StringSubclass('two')) # Inferred type of variable 'b' is 'str', + # despite 'StringSubclass' being passed in + c = concatenate('one', b'two') # error: type variable 'A' can be either 'str' or 'bytes' in a function call, but not both + + Using a *bound* type variable, however, means that the ``TypeVar`` will be + solved using the most specific type possible:: + + print_capitalized('a string') # Ok, output has type 'str' + + class StringSubclass(str): + pass + + print_capitalized(StringSubclass('another string')) # Ok, output has type 'StringSubclass' + print_capitalized(45) # error: int is not a subtype of str + + Type variables can be bound to concrete types, abstract types (ABCs or + protocols), and even unions of types:: + + U = TypeVar('U', bound=str|bytes) # Can be any subtype of the union str|bytes + V = TypeVar('V', bound=SupportsAbs) # Can be anything with an __abs__ method + + Bound type variables are particularly useful for annotating + :func:`classmethods ` that serve as alternative constructors. + In the following example (© + `Raymond Hettinger `_), the + type variable ``C`` is bound to the ``Circle`` class through the use of a + forward reference. Using this type variable to annotate the + ``with_circumference`` classmethod, rather than hardcoding the return type + as ``Circle``, means that a type checker can correctly infer the return + type even if the method is called on a subclass:: + + import math + + C = TypeVar('C', bound='Circle') + + class Circle: + """An abstract circle""" + + def __init__(self, radius: float) -> None: + self.radius = radius + + # Use a type variable to show that the return type + # will always be an instance of whatever ``cls`` is + @classmethod + def with_circumference(cls: type[C], circumference: float) -> C: + """Create a circle with the specified circumference""" + radius = circumference / (math.pi * 2) + return cls(radius) + + + class Tire(Circle): + """A specialised circle (made out of rubber)""" + + MATERIAL = 'rubber' + + + c = Circle.with_circumference(3) # Ok, variable 'c' has type 'Circle' + t = Tire.with_circumference(4) # Ok, variable 't' has type 'Tire' (not 'Circle') At runtime, ``isinstance(x, T)`` will raise :exc:`TypeError`. In general, :func:`isinstance` and :func:`issubclass` should not be used with types. Type variables may be marked covariant or contravariant by passing ``covariant=True`` or ``contravariant=True``. See :pep:`484` for more - details. By default type variables are invariant. Alternatively, - a type variable may specify an upper bound using ``bound=``. - This means that an actual type substituted (explicitly or implicitly) - for the type variable must be a subclass of the boundary type, - see :pep:`484`. + details. By default, type variables are invariant. .. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False) @@ -1113,11 +1229,11 @@ These are not used in annotations. They are building blocks for creating generic use a :class:`TypeVar` with bound ``Callable[..., Any]``. However this causes two problems: - 1. The type checker can't type check the ``inner`` function because - ``*args`` and ``**kwargs`` have to be typed :data:`Any`. - 2. :func:`~cast` may be required in the body of the ``add_logging`` - decorator when returning the ``inner`` function, or the static type - checker must be told to ignore the ``return inner``. + 1. The type checker can't type check the ``inner`` function because + ``*args`` and ``**kwargs`` have to be typed :data:`Any`. + 2. :func:`~cast` may be required in the body of the ``add_logging`` + decorator when returning the ``inner`` function, or the static type + checker must be told to ignore the ``return inner``. .. attribute:: args .. attribute:: kwargs @@ -1169,7 +1285,7 @@ These are not used in annotations. They are building blocks for creating generic .. data:: AnyStr - ``AnyStr`` is a type variable defined as + ``AnyStr`` is a :class:`constrained type variable ` defined as ``AnyStr = TypeVar('AnyStr', str, bytes)``. It is meant to be used for functions that may accept any kind of string @@ -1232,8 +1348,13 @@ These are not used in annotations. They are building blocks for creating generic .. note:: - :func:`runtime_checkable` will check only the presence of the required methods, - not their type signatures. + :func:`runtime_checkable` will check only the presence of the required + methods, not their type signatures. For example, :class:`ssl.SSLObject` + is a class, therefore it passes an :func:`issubclass` + check against :data:`Callable`. However, the + :meth:`ssl.SSLObject.__init__` method exists only to raise a + :exc:`TypeError` with a more informative message, therefore making + it impossible to call (instantiate) :class:`ssl.SSLObject`. .. versionadded:: 3.8 @@ -1270,7 +1391,7 @@ These are not used in annotations. They are building blocks for declaring types. The resulting class has an extra attribute ``__annotations__`` giving a dict that maps the field names to the field types. (The field names are in the ``_fields`` attribute and the default values are in the - ``_field_defaults`` attribute both of which are part of the namedtuple + ``_field_defaults`` attribute, both of which are part of the :func:`~collections.namedtuple` API.) ``NamedTuple`` subclasses can also have docstrings and methods:: @@ -1301,17 +1422,21 @@ These are not used in annotations. They are building blocks for declaring types. Removed the ``_field_types`` attribute in favor of the more standard ``__annotations__`` attribute which has the same information. -.. function:: NewType(name, tp) +.. class:: NewType(name, tp) - A helper function to indicate a distinct type to a typechecker, - see :ref:`distinct`. At runtime it returns a function that returns - its argument. Usage:: + A helper class to indicate a distinct type to a typechecker, + see :ref:`distinct`. At runtime it returns an object that returns + its argument when called. + Usage:: UserId = NewType('UserId', int) first_user = UserId(1) .. versionadded:: 3.5.2 + .. versionchanged:: 3.10 + ``NewType`` is now a class rather than a function. + .. class:: TypedDict(dict) Special construct to add type hints to a dictionary. @@ -1333,9 +1458,6 @@ These are not used in annotations. They are building blocks for declaring types. assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') - The type info for introspection can be accessed via ``Point2D.__annotations__``, - ``Point2D.__total__``, ``Point2D.__required_keys__``, and - ``Point2D.__optional_keys__``. To allow using this feature with older versions of Python that do not support :pep:`526`, ``TypedDict`` supports two additional equivalent syntactic forms:: @@ -1343,6 +1465,18 @@ These are not used in annotations. They are building blocks for declaring types. Point2D = TypedDict('Point2D', x=int, y=int, label=str) Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) + The functional syntax should also be used when any of the keys are not valid + :ref:`identifiers `, for example because they are keywords or contain hyphens. + Example:: + + # raises SyntaxError + class Point2D(TypedDict): + in: int # 'in' is a keyword + x-y: int # name with hyphens + + # OK, functional syntax + Point2D = TypedDict('Point2D', {'in': int, 'x-y': int}) + By default, all keys must be present in a ``TypedDict``. It is possible to override this by specifying totality. Usage:: @@ -1356,6 +1490,82 @@ These are not used in annotations. They are building blocks for declaring types. ``True`` as the value of the ``total`` argument. ``True`` is the default, and makes all items defined in the class body required. + It is possible for a ``TypedDict`` type to inherit from one or more other ``TypedDict`` types + using the class-based syntax. + Usage:: + + class Point3D(Point2D): + z: int + + ``Point3D`` has three items: ``x``, ``y`` and ``z``. It is equivalent to this + definition:: + + class Point3D(TypedDict): + x: int + y: int + z: int + + A ``TypedDict`` cannot inherit from a non-\ ``TypedDict`` class, + notably including :class:`Generic`. For example:: + + class X(TypedDict): + x: int + + class Y(TypedDict): + y: int + + class Z(object): pass # A non-TypedDict class + + class XY(X, Y): pass # OK + + class XZ(X, Z): pass # raises TypeError + + T = TypeVar('T') + class XT(X, Generic[T]): pass # raises TypeError + + A ``TypedDict`` can be introspected via annotations dicts + (see :ref:`annotations-howto` for more information on annotations best practices), + :attr:`__total__`, :attr:`__required_keys__`, and :attr:`__optional_keys__`. + + .. attribute:: __total__ + + ``Point2D.__total__`` gives the value of the ``total`` argument. + Example:: + + >>> from typing import TypedDict + >>> class Point2D(TypedDict): pass + >>> Point2D.__total__ + True + >>> class Point2D(TypedDict, total=False): pass + >>> Point2D.__total__ + False + >>> class Point3D(Point2D): pass + >>> Point3D.__total__ + True + + .. attribute:: __required_keys__ + .. attribute:: __optional_keys__ + + ``Point2D.__required_keys__`` and ``Point2D.__optional_keys__`` return + :class:`frozenset` objects containing required and non-required keys, respectively. + Currently the only way to declare both required and non-required keys in the + same ``TypedDict`` is mixed inheritance, declaring a ``TypedDict`` with one value + for the ``total`` argument and then inheriting it from another ``TypedDict`` with + a different value for ``total``. + Usage:: + + >>> class Point2D(TypedDict, total=False): + ... x: int + ... y: int + ... + >>> class Point3D(Point2D): + ... z: int + ... + >>> Point3D.__required_keys__ == frozenset({'z'}) + True + >>> Point3D.__optional_keys__ == frozenset({'x', 'y'}) + True + See :pep:`589` for more examples and detailed rules of using ``TypedDict``. .. versionadded:: 3.8 @@ -1488,7 +1698,11 @@ Other concrete types Generic type ``IO[AnyStr]`` and its subclasses ``TextIO(IO[str])`` and ``BinaryIO(IO[bytes])`` represent the types of I/O streams such as returned by - :func:`open`. These types are also in the ``typing.io`` namespace. + :func:`open`. + + .. deprecated-removed:: 3.8 3.12 + The ``typing.io`` namespace is deprecated and will be removed. + These types should be directly imported from ``typing`` instead. .. class:: Pattern Match @@ -1498,7 +1712,11 @@ Other concrete types :func:`re.match`. These types (and the corresponding functions) are generic in ``AnyStr`` and can be made specific by writing ``Pattern[str]``, ``Pattern[bytes]``, ``Match[str]``, or - ``Match[bytes]``. These types are also in the ``typing.re`` namespace. + ``Match[bytes]``. + + .. deprecated-removed:: 3.8 3.12 + The ``typing.re`` namespace is deprecated and will be removed. + These types should be directly imported from ``typing`` instead. .. deprecated:: 3.9 Classes ``Pattern`` and ``Match`` from :mod:`re` now support ``[]``. @@ -1696,7 +1914,7 @@ Corresponding to other types in :mod:`collections.abc` .. class:: Hashable - An alias to :class:`collections.abc.Hashable` + An alias to :class:`collections.abc.Hashable`. .. class:: Reversible(Iterable[T_co]) @@ -1708,7 +1926,7 @@ Corresponding to other types in :mod:`collections.abc` .. class:: Sized - An alias to :class:`collections.abc.Sized` + An alias to :class:`collections.abc.Sized`. Asynchronous programming """""""""""""""""""""""" @@ -1720,11 +1938,10 @@ Asynchronous programming correspond to those of :class:`Generator`, for example:: from collections.abc import Coroutine - c = None # type: Coroutine[list[str], str, int] - ... - x = c.send('hi') # type: list[str] + c: Coroutine[list[str], str, int] # Some coroutine defined elsewhere + x = c.send('hi') # Inferred type of 'x' is list[str] async def bar() -> None: - x = await c # type: int + y = await c # Inferred type of 'y' is int .. versionadded:: 3.5.3 @@ -1914,7 +2131,7 @@ Functions and decorators ... class Sub(Base): def done(self) -> None: # Error reported by type checker - ... + ... @final class Leaf: @@ -1991,6 +2208,13 @@ Introspection helpers 'name': Annotated[str, 'some marker'] } + .. note:: + + :func:`get_type_hints` does not work with imported + :ref:`type aliases ` that include forward references. + Enabling postponed evaluation of annotations (:pep:`563`) may remove + the need for most forward references. + .. versionchanged:: 3.9 Added ``include_extras`` parameter as part of :pep:`593`. @@ -2002,7 +2226,7 @@ Introspection helpers For a typing object of the form ``X[Y, Z, ...]`` these functions return ``X`` and ``(Y, Z, ...)``. If ``X`` is a generic alias for a builtin or :mod:`collections` class, it gets normalized to the original class. - If ``X`` is a :class:`Union` or :class:`Literal` contained in another + If ``X`` is a union or :class:`Literal` contained in another generic type, the order of ``(Y, Z, ...)`` may be different from the order of the original arguments ``[Y, Z, ...]`` due to type caching. For unsupported objects return ``None`` and ``()`` correspondingly. @@ -2027,7 +2251,7 @@ Introspection helpers year: int is_typeddict(Film) # => True - is_typeddict(Union[list, str]) # => False + is_typeddict(list | str) # => False .. versionadded:: 3.10 @@ -2066,10 +2290,10 @@ Constant .. note:: - If ``from __future__ import annotations`` is used in Python 3.7 or later, + If ``from __future__ import annotations`` is used, annotations are not evaluated at function definition time. - Instead, they are stored as strings in ``__annotations__``, - This makes it unnecessary to use quotes around the annotation. + Instead, they are stored as strings in ``__annotations__``. + This makes it unnecessary to use quotes around the annotation (see :pep:`563`). .. versionadded:: 3.5.2 diff --git a/Doc/library/undoc.rst b/Doc/library/undoc.rst deleted file mode 100644 index 2444080d6b9d90..00000000000000 --- a/Doc/library/undoc.rst +++ /dev/null @@ -1,26 +0,0 @@ -.. _undoc: - -******************** -Undocumented Modules -******************** - -Here's a quick listing of modules that are currently undocumented, but that -should be documented. Feel free to contribute documentation for them! (Send -via email to docs@python.org.) - -The idea and original contents for this chapter were taken from a posting by -Fredrik Lundh; the specific contents of this chapter have been substantially -revised. - - -Platform specific modules -========================= - -These modules are used to implement the :mod:`os.path` module, and are not -documented beyond this mention. There's little need to document these. - -:mod:`ntpath` - --- Implementation of :mod:`os.path` on Win32 and Win64 platforms. - -:mod:`posixpath` - --- Implementation of :mod:`os.path` on POSIX. diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 1604731b99e3fe..86a848edab8895 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -328,8 +328,8 @@ the *new_callable* argument to :func:`patch`. .. method:: assert_called_once_with(*args, **kwargs) - Assert that the mock was called exactly once and that that call was - with the specified arguments. + Assert that the mock was called exactly once and that call was with the + specified arguments. >>> mock = Mock(return_value=None) >>> mock('foo', bar='baz') @@ -1516,7 +1516,7 @@ attribute in a class) that does not exist will fail with :exc:`AttributeError`:: >>> test() Traceback (most recent call last): ... - AttributeError: does not have the attribute 'non_existing' + AttributeError: does not have the attribute 'non_existing_attribute' but adding ``create=True`` in the call to :func:`patch` will make the previous example work as expected:: @@ -2208,7 +2208,7 @@ In this example we monkey patch ``method`` to return ``sentinel.some_object``: >>> real.method.return_value = sentinel.some_object >>> result = real.method() >>> assert result is sentinel.some_object - >>> sentinel.some_object + >>> result sentinel.some_object @@ -2550,7 +2550,7 @@ your assertion is gone: >>> mock = Mock(name='Thing', return_value=None) >>> mock(1, 2, 3) - >>> mock.assret_called_once_with(4, 5, 6) + >>> mock.assret_called_once_with(4, 5, 6) # Intentional typo! Your tests can pass silently and incorrectly because of the typo. @@ -2570,7 +2570,7 @@ attributes on the mock that exist on the real class: >>> from urllib import request >>> mock = Mock(spec=request.Request) - >>> mock.assret_called_with + >>> mock.assret_called_with # Intentional typo! Traceback (most recent call last): ... AttributeError: Mock object has no attribute 'assret_called_with' @@ -2582,7 +2582,7 @@ with any methods on the mock: >>> mock.has_data() - >>> mock.has_data.assret_called_with() + >>> mock.has_data.assret_called_with() # Intentional typo! Auto-speccing solves this problem. You can either pass ``autospec=True`` to :func:`patch` / :func:`patch.object` or use the :func:`create_autospec` function to create a @@ -2625,7 +2625,7 @@ any typos in our asserts will raise the correct error:: >>> req.add_header('spam', 'eggs') - >>> req.add_header.assret_called_with + >>> req.add_header.assret_called_with # Intentional typo! Traceback (most recent call last): ... AttributeError: Mock object has no attribute 'assret_called_with' diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index 0c29408cb19214..67fec60263a4c6 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -223,7 +223,7 @@ Command-line options Only run test methods and classes that match the pattern or substring. This option may be used multiple times, in which case all test cases that - match of the given patterns are included. + match any of the given patterns are included. Patterns that contain a wildcard character (``*``) are matched against the test name using :meth:`fnmatch.fnmatchcase`; otherwise simple case-sensitive @@ -597,8 +597,9 @@ The following decorators and exception implement test skipping and expected fail .. decorator:: expectedFailure Mark the test as an expected failure or error. If the test fails or errors - it will be considered a success. If the test passes, it will be considered - a failure. + in the test function itself (rather than in one of the :dfn:`test fixture` + methods) then it will be considered a success. If the test passes, it will + be considered a failure. .. exception:: SkipTest(reason) @@ -2438,7 +2439,7 @@ To add cleanup code that must be run even in the case of an exception, use after :func:`setUpModule` if :func:`setUpModule` raises an exception. It is responsible for calling all the cleanup functions added by - :func:`addCleanupModule`. If you need cleanup functions to be called + :func:`addModuleCleanup`. If you need cleanup functions to be called *prior* to :func:`tearDownModule` then you can call :func:`doModuleCleanups` yourself. diff --git a/Doc/library/unix.rst b/Doc/library/unix.rst index 04d4081f4a0736..4553a104d15a24 100644 --- a/Doc/library/unix.rst +++ b/Doc/library/unix.rst @@ -13,14 +13,10 @@ of it. Here's an overview: posix.rst pwd.rst - spwd.rst grp.rst - crypt.rst termios.rst tty.rst pty.rst fcntl.rst - pipes.rst resource.rst - nis.rst syslog.rst diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst index 0aaac562883f18..1478b34bc95514 100644 --- a/Doc/library/urllib.parse.rst +++ b/Doc/library/urllib.parse.rst @@ -20,7 +20,7 @@ strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a "relative URL" to an absolute URL given a "base URL." -The module has been designed to match the Internet RFC on Relative Uniform +The module has been designed to match the internet RFC on Relative Uniform Resource Locators. It supports the following URL schemes: ``file``, ``ftp``, ``gopher``, ``hdl``, ``http``, ``https``, ``imap``, ``mailto``, ``mms``, ``news``, ``nntp``, ``prospero``, ``rsync``, ``rtsp``, ``rtspu``, ``sftp``, @@ -48,17 +48,29 @@ or on combining URL components into a URL string. result, except for a leading slash in the *path* component, which is retained if present. For example: + .. doctest:: + :options: +NORMALIZE_WHITESPACE + >>> from urllib.parse import urlparse - >>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html') - >>> o # doctest: +NORMALIZE_WHITESPACE - ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html', - params='', query='', fragment='') + >>> urlparse("scheme://netloc/path;parameters?query#fragment") + ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='', + query='query', fragment='fragment') + >>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?" + ... "highlight=params#url-parsing") + >>> o + ParseResult(scheme='http', netloc='docs.python.org:80', + path='/3/library/urllib.parse.html', params='', + query='highlight=params', fragment='url-parsing') >>> o.scheme 'http' + >>> o.netloc + 'docs.python.org:80' + >>> o.hostname + 'docs.python.org' >>> o.port 80 - >>> o.geturl() - 'http://www.cwi.nl:80/%7Eguido/Python.html' + >>> o._replace(fragment="").geturl() + 'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params' Following the syntax specifications in :rfc:`1808`, urlparse recognizes a netloc only if it is properly introduced by '//'. Otherwise the @@ -92,31 +104,30 @@ or on combining URL components into a URL string. The return value is a :term:`named tuple`, which means that its items can be accessed by index or as named attributes, which are: - +------------------+-------+--------------------------+----------------------+ - | Attribute | Index | Value | Value if not present | - +==================+=======+==========================+======================+ - | :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter | - +------------------+-------+--------------------------+----------------------+ - | :attr:`netloc` | 1 | Network location part | empty string | - +------------------+-------+--------------------------+----------------------+ - | :attr:`path` | 2 | Hierarchical path | empty string | - +------------------+-------+--------------------------+----------------------+ - | :attr:`params` | 3 | Parameters for last path | empty string | - | | | element | | - +------------------+-------+--------------------------+----------------------+ - | :attr:`query` | 4 | Query component | empty string | - +------------------+-------+--------------------------+----------------------+ - | :attr:`fragment` | 5 | Fragment identifier | empty string | - +------------------+-------+--------------------------+----------------------+ - | :attr:`username` | | User name | :const:`None` | - +------------------+-------+--------------------------+----------------------+ - | :attr:`password` | | Password | :const:`None` | - +------------------+-------+--------------------------+----------------------+ - | :attr:`hostname` | | Host name (lower case) | :const:`None` | - +------------------+-------+--------------------------+----------------------+ - | :attr:`port` | | Port number as integer, | :const:`None` | - | | | if present | | - +------------------+-------+--------------------------+----------------------+ + +------------------+-------+-------------------------+------------------------+ + | Attribute | Index | Value | Value if not present | + +==================+=======+=========================+========================+ + | :attr:`scheme` | 0 | URL scheme specifier | *scheme* parameter | + +------------------+-------+-------------------------+------------------------+ + | :attr:`netloc` | 1 | Network location part | empty string | + +------------------+-------+-------------------------+------------------------+ + | :attr:`path` | 2 | Hierarchical path | empty string | + +------------------+-------+-------------------------+------------------------+ + | :attr:`params` | 3 | No longer used | always an empty string | + +------------------+-------+-------------------------+------------------------+ + | :attr:`query` | 4 | Query component | empty string | + +------------------+-------+-------------------------+------------------------+ + | :attr:`fragment` | 5 | Fragment identifier | empty string | + +------------------+-------+-------------------------+------------------------+ + | :attr:`username` | | User name | :const:`None` | + +------------------+-------+-------------------------+------------------------+ + | :attr:`password` | | Password | :const:`None` | + +------------------+-------+-------------------------+------------------------+ + | :attr:`hostname` | | Host name (lower case) | :const:`None` | + +------------------+-------+-------------------------+------------------------+ + | :attr:`port` | | Port number as integer, | :const:`None` | + | | | if present | | + +------------------+-------+-------------------------+------------------------+ Reading the :attr:`port` attribute will raise a :exc:`ValueError` if an invalid port is specified in the URL. See section diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index b4435a62ad43d9..63248b3d94f212 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -169,8 +169,8 @@ The :mod:`urllib.request` module defines the following functions: This helper function returns a dictionary of scheme to proxy server URL mappings. It scans the environment for variables named ``_proxy``, in a case insensitive approach, for all operating systems first, and when it - cannot find it, looks for proxy information from Mac OSX System - Configuration for Mac OS X and Windows Systems Registry for Windows. + cannot find it, looks for proxy information from System + Configuration for macOS and Windows Systems Registry for Windows. If both lowercase and uppercase environment variables exist (and disagree), lowercase is preferred. @@ -218,6 +218,7 @@ The following classes are provided: (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"``, while :mod:`urllib`'s default user agent string is ``"Python-urllib/2.6"`` (on Python 2.6). + All header keys are sent in camel case. An appropriate ``Content-Type`` header should be included if the *data* argument is present. If this header has not been provided and *data* @@ -303,8 +304,8 @@ The following classes are provided: the list of proxies from the environment variables ``_proxy``. If no proxy environment variables are set, then in a Windows environment proxy settings are obtained from the registry's - Internet Settings section, and in a Mac OS X environment proxy information - is retrieved from the OS X System Configuration Framework. + Internet Settings section, and in a macOS environment proxy information + is retrieved from the System Configuration Framework. To disable autodetected proxy pass an empty dictionary. @@ -546,7 +547,8 @@ request. name, and later calls will overwrite previous calls in case the *key* collides. Currently, this is no loss of HTTP functionality, since all headers which have meaning when used more than once have a (header-specific) way of gaining the - same functionality using only one header. + same functionality using only one header. Note that headers added using + this method are also added to redirected requests. .. method:: Request.add_unredirected_header(key, header) @@ -655,7 +657,7 @@ OpenerDirector Objects optional *timeout* parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). The timeout feature actually works only for - HTTP, HTTPS and FTP connections). + HTTP, HTTPS and FTP connections. .. method:: OpenerDirector.error(proto, *args) @@ -737,7 +739,7 @@ The following attribute and methods should only be used by classes derived from This method, if implemented, will be called by the parent :class:`OpenerDirector`. It should return a file-like object as described in - the return value of the :meth:`open` of :class:`OpenerDirector`, or ``None``. + the return value of the :meth:`~OpenerDirector.open` method of :class:`OpenerDirector`, or ``None``. It should raise :exc:`~urllib.error.URLError`, unless a truly exceptional thing happens (for example, :exc:`MemoryError` should not be mapped to :exc:`URLError`). @@ -1543,7 +1545,7 @@ some point in the future. * The :func:`urlopen` and :func:`urlretrieve` functions can cause arbitrarily long delays while waiting for a network connection to be set up. This means - that it is difficult to build an interactive Web client using these functions + that it is difficult to build an interactive web client using these functions without using threads. .. index:: diff --git a/Doc/library/urllib.robotparser.rst b/Doc/library/urllib.robotparser.rst index 544f50273dd17c..f063e463753e0b 100644 --- a/Doc/library/urllib.robotparser.rst +++ b/Doc/library/urllib.robotparser.rst @@ -19,7 +19,7 @@ This module provides a single class, :class:`RobotFileParser`, which answers questions about whether or not a particular user agent can fetch a URL on the -Web site that published the :file:`robots.txt` file. For more details on the +web site that published the :file:`robots.txt` file. For more details on the structure of :file:`robots.txt` files, see http://www.robotstxt.org/orig.html. diff --git a/Doc/library/uu.rst b/Doc/library/uu.rst index 0bc8021e1bdfc7..026ec415c9da65 100644 --- a/Doc/library/uu.rst +++ b/Doc/library/uu.rst @@ -3,11 +3,17 @@ .. module:: uu :synopsis: Encode and decode files in uuencode format. + :deprecated: .. moduleauthor:: Lance Ellinghouse **Source code:** :source:`Lib/uu.py` +.. deprecated:: 3.11 + The :mod:`uu` module is deprecated + (see :pep:`PEP 594 <594#uu-and-the-uu-encoding>` for details). + :mod:`base64` is a modern alternative. + -------------- This module encodes and decodes files in uuencode format, allowing arbitrary diff --git a/Doc/library/uuid.rst b/Doc/library/uuid.rst index 415e25bef6de7d..ddfbbda279e35d 100644 --- a/Doc/library/uuid.rst +++ b/Doc/library/uuid.rst @@ -118,7 +118,7 @@ which relays any information about the UUID's safety, using this enumeration: .. attribute:: UUID.hex - The UUID as a 32-character hexadecimal string. + The UUID as a 32-character lowercase hexadecimal string. .. attribute:: UUID.int diff --git a/Doc/library/venv.rst b/Doc/library/venv.rst index 2a4eede91a7c17..127a8717cf7d12 100644 --- a/Doc/library/venv.rst +++ b/Doc/library/venv.rst @@ -425,7 +425,7 @@ subclass which installs setuptools and pip into a created virtual environment:: 'more target ' 'directories.') parser.add_argument('dirs', metavar='ENV_DIR', nargs='+', - help='A directory in which to create the + help='A directory in which to create the ' 'virtual environment.') parser.add_argument('--no-setuptools', default=False, action='store_true', dest='nodist', diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index fe11aabbcbdd6b..8f2a5accd472be 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -105,7 +105,7 @@ The following warnings category classes are currently defined: | | :class:`bytes` and :class:`bytearray`. | +----------------------------------+-----------------------------------------------+ | :exc:`ResourceWarning` | Base category for warnings related to | -| | resource usage. | +| | resource usage (ignored by default). | +----------------------------------+-----------------------------------------------+ .. versionchanged:: 3.7 @@ -154,14 +154,19 @@ the disposition of the match. Each entry is a tuple of the form (*action*, +---------------+----------------------------------------------+ * *message* is a string containing a regular expression that the start of - the warning message must match. The expression is compiled to always be - case-insensitive. + the warning message must match, case-insensitively. In :option:`-W` and + :envvar:`PYTHONWARNINGS`, *message* is a literal string that the start of the + warning message must contain (case-insensitively), ignoring any whitespace at + the start or end of *message*. * *category* is a class (a subclass of :exc:`Warning`) of which the warning category must be a subclass in order to match. -* *module* is a string containing a regular expression that the module name must - match. The expression is compiled to be case-sensitive. +* *module* is a string containing a regular expression that the start of the + fully-qualified module name must match, case-sensitively. In :option:`-W` and + :envvar:`PYTHONWARNINGS`, *module* is a literal string that the + fully-qualified module name must be equal to (case-sensitively), ignoring any + whitespace at the start or end of *module*. * *lineno* is an integer that the line number where the warning occurred must match, or ``0`` to match all line numbers. @@ -207,8 +212,7 @@ Some examples:: error::ResourceWarning # Treat ResourceWarning messages as errors default::DeprecationWarning # Show DeprecationWarning messages ignore,default:::mymodule # Only report warnings triggered by "mymodule" - error:::mymodule[.*] # Convert warnings to errors in "mymodule" - # and any subpackages of "mymodule" + error:::mymodule # Convert warnings to errors in "mymodule" .. _default-warning-filter: diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index b88543e4453723..4b0945c020f843 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -88,6 +88,10 @@ support weak references but can add support through subclassing:: Extension types can easily be made to support weak references; see :ref:`weakref-support`. +When ``__slots__`` are defined for a given type, weak reference support is +disabled unless a ``'__weakref__'`` string is also present in the sequence of +strings in the ``__slots__`` declaration. +See :ref:`__slots__ documentation ` for details. .. class:: ref(object[, callback]) diff --git a/Doc/library/webbrowser.rst b/Doc/library/webbrowser.rst index b7bfb655a71579..c0990882e58fe9 100644 --- a/Doc/library/webbrowser.rst +++ b/Doc/library/webbrowser.rst @@ -1,8 +1,8 @@ -:mod:`webbrowser` --- Convenient Web-browser controller +:mod:`webbrowser` --- Convenient web-browser controller ======================================================= .. module:: webbrowser - :synopsis: Easy-to-use controller for Web browsers. + :synopsis: Easy-to-use controller for web browsers. .. moduleauthor:: Fred L. Drake, Jr. .. sectionauthor:: Fred L. Drake, Jr. @@ -12,7 +12,7 @@ -------------- The :mod:`webbrowser` module provides a high-level interface to allow displaying -Web-based documents to users. Under most circumstances, simply calling the +web-based documents to users. Under most circumstances, simply calling the :func:`.open` function from this module will do the right thing. Under Unix, graphical browsers are preferred under X11, but text-mode browsers @@ -39,7 +39,7 @@ parameters: ``-n`` opens the URL in a new browser window, if possible; ``-t`` opens the URL in a new browser page ("tab"). The options are, naturally, mutually exclusive. Usage example:: - python -m webbrowser -t "http://www.python.org" + python -m webbrowser -t "https://www.python.org" The following exception is defined: @@ -143,9 +143,9 @@ for the controller classes, all defined in this module. +------------------------+-----------------------------------------+-------+ | ``'windows-default'`` | :class:`WindowsDefault` | \(2) | +------------------------+-----------------------------------------+-------+ -| ``'macosx'`` | :class:`MacOSX('default')` | \(3) | +| ``'macosx'`` | :class:`MacOSXOSAScript('default')` | \(3) | +------------------------+-----------------------------------------+-------+ -| ``'safari'`` | :class:`MacOSX('safari')` | \(3) | +| ``'safari'`` | :class:`MacOSXOSAScript('safari')` | \(3) | +------------------------+-----------------------------------------+-------+ | ``'google-chrome'`` | :class:`Chrome('google-chrome')` | | +------------------------+-----------------------------------------+-------+ @@ -169,14 +169,14 @@ Notes: Only on Windows platforms. (3) - Only on Mac OS X platform. + Only on macOS platform. .. versionadded:: 3.3 Support for Chrome/Chromium has been added. Here are some simple examples:: - url = 'http://docs.python.org/' + url = 'https://docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab(url) diff --git a/Doc/library/windows.rst b/Doc/library/windows.rst index b60d4e4ccf51d9..4d72ead12aadc7 100644 --- a/Doc/library/windows.rst +++ b/Doc/library/windows.rst @@ -9,7 +9,6 @@ This chapter describes modules that are only available on MS Windows platforms. .. toctree:: - msilib.rst msvcrt.rst winreg.rst winsound.rst diff --git a/Doc/library/xdrlib.rst b/Doc/library/xdrlib.rst index 42a03a46754d03..a3124a986524bd 100644 --- a/Doc/library/xdrlib.rst +++ b/Doc/library/xdrlib.rst @@ -3,6 +3,7 @@ .. module:: xdrlib :synopsis: Encoders and decoders for the External Data Representation (XDR). + :deprecated: **Source code:** :source:`Lib/xdrlib.py` @@ -10,6 +11,10 @@ single: XDR single: External Data Representation +.. deprecated:: 3.11 + The :mod:`xdrlib` module is deprecated + (see :pep:`PEP 594 <594#xdrlib>` for details). + -------------- The :mod:`xdrlib` module supports the External Data Representation Standard as diff --git a/Doc/library/xml.dom.minidom.rst b/Doc/library/xml.dom.minidom.rst index bf72c46561b7c7..20984b98b1778c 100644 --- a/Doc/library/xml.dom.minidom.rst +++ b/Doc/library/xml.dom.minidom.rst @@ -145,7 +145,7 @@ module documentation. This section lists the differences between the API and For the :class:`Document` node, an additional keyword argument *encoding* can be used to specify the encoding field of the XML header. - Silimarly, explicitly stating the *standalone* argument causes the + Similarly, explicitly stating the *standalone* argument causes the standalone document declarations to be added to the prologue of the XML document. If the value is set to `True`, `standalone="yes"` is added, @@ -156,6 +156,9 @@ module documentation. This section lists the differences between the API and The :meth:`writexml` method now preserves the attribute order specified by the user. + .. versionchanged:: 3.9 + The *standalone* parameter was added. + .. method:: Node.toxml(encoding=None, standalone=None) Return a string or byte string containing the XML represented by @@ -174,6 +177,9 @@ module documentation. This section lists the differences between the API and The :meth:`toxml` method now preserves the attribute order specified by the user. + .. versionchanged:: 3.9 + The *standalone* parameter was added. + .. method:: Node.toprettyxml(indent="\\t", newl="\\n", encoding=None, \ standalone=None) @@ -190,6 +196,8 @@ module documentation. This section lists the differences between the API and The :meth:`toprettyxml` method now preserves the attribute order specified by the user. + .. versionchanged:: 3.9 + The *standalone* parameter was added. .. _dom-example: diff --git a/Doc/library/xml.dom.rst b/Doc/library/xml.dom.rst index 98454e18ff1f87..b387240a3716cc 100644 --- a/Doc/library/xml.dom.rst +++ b/Doc/library/xml.dom.rst @@ -73,7 +73,7 @@ implementations are free to support the strict mapping from IDL). See section `Document Object Model (DOM) Level 1 Specification `_ The W3C recommendation for the DOM supported by :mod:`xml.dom.minidom`. - `Python Language Mapping Specification `_ + `Python Language Mapping Specification `_ This specifies the mapping from OMG IDL to Python. diff --git a/Doc/library/xml.etree.elementtree.rst b/Doc/library/xml.etree.elementtree.rst index 87f4ee347d604d..2fe0d2e082fb3a 100644 --- a/Doc/library/xml.etree.elementtree.rst +++ b/Doc/library/xml.etree.elementtree.rst @@ -363,13 +363,6 @@ These two approaches both output:: |--> Commander Clement -Additional resources -^^^^^^^^^^^^^^^^^^^^ - -See http://effbot.org/zone/element-index.htm for tutorials and links to other -docs. - - .. _elementtree-xpath: XPath support @@ -833,6 +826,7 @@ Functions ^^^^^^^^^ .. function:: xml.etree.ElementInclude.default_loader( href, parse, encoding=None) + :module: Default loader. This default loader reads an included resource from disk. *href* is a URL. *parse* is for parse mode either "xml" or "text". *encoding* @@ -844,6 +838,7 @@ Functions .. function:: xml.etree.ElementInclude.include( elem, loader=None, base_url=None, \ max_depth=6) + :module: This function expands XInclude directives. *elem* is the root element. *loader* is an optional resource loader. If omitted, it defaults to :func:`default_loader`. diff --git a/Doc/library/xml.rst b/Doc/library/xml.rst index 1981cab7cd438d..e3b35162961147 100644 --- a/Doc/library/xml.rst +++ b/Doc/library/xml.rst @@ -60,22 +60,26 @@ circumvent firewalls. The following table gives an overview of the known attacks and whether the various modules are vulnerable to them. -========================= ============== =============== ============== ============== ============== -kind sax etree minidom pulldom xmlrpc -========================= ============== =============== ============== ============== ============== -billion laughs **Vulnerable** **Vulnerable** **Vulnerable** **Vulnerable** **Vulnerable** -quadratic blowup **Vulnerable** **Vulnerable** **Vulnerable** **Vulnerable** **Vulnerable** -external entity expansion Safe (4) Safe (1) Safe (2) Safe (4) Safe (3) -`DTD`_ retrieval Safe (4) Safe Safe Safe (4) Safe -decompression bomb Safe Safe Safe Safe **Vulnerable** -========================= ============== =============== ============== ============== ============== - -1. :mod:`xml.etree.ElementTree` doesn't expand external entities and raises a +========================= ================== ================== ================== ================== ================== +kind sax etree minidom pulldom xmlrpc +========================= ================== ================== ================== ================== ================== +billion laughs **Vulnerable** (1) **Vulnerable** (1) **Vulnerable** (1) **Vulnerable** (1) **Vulnerable** (1) +quadratic blowup **Vulnerable** (1) **Vulnerable** (1) **Vulnerable** (1) **Vulnerable** (1) **Vulnerable** (1) +external entity expansion Safe (5) Safe (2) Safe (3) Safe (5) Safe (4) +`DTD`_ retrieval Safe (5) Safe Safe Safe (5) Safe +decompression bomb Safe Safe Safe Safe **Vulnerable** +========================= ================== ================== ================== ================== ================== + +1. Expat 2.4.1 and newer is not vulnerable to the "billion laughs" and + "quadratic blowup" vulnerabilities. Items still listed as vulnerable due to + potential reliance on system-provided libraries. Check + :data:`pyexpat.EXPAT_VERSION`. +2. :mod:`xml.etree.ElementTree` doesn't expand external entities and raises a :exc:`ParserError` when an entity occurs. -2. :mod:`xml.dom.minidom` doesn't expand external entities and simply returns +3. :mod:`xml.dom.minidom` doesn't expand external entities and simply returns the unexpanded entity verbatim. -3. :mod:`xmlrpclib` doesn't expand external entities and omits them. -4. Since Python 3.7.1, external general entities are no longer processed by +4. :mod:`xmlrpclib` doesn't expand external entities and omits them. +5. Since Python 3.7.1, external general entities are no longer processed by default. diff --git a/Doc/library/xml.sax.handler.rst b/Doc/library/xml.sax.handler.rst index 59d0d1b3b01175..719ce5ab1bcf65 100644 --- a/Doc/library/xml.sax.handler.rst +++ b/Doc/library/xml.sax.handler.rst @@ -147,7 +147,7 @@ for the feature and property names. .. data:: property_xml_string | value: ``"http://xml.org/sax/properties/xml-string"`` - | data type: String + | data type: Bytes | description: The literal string of characters that was the source for the current event. | access: read-only diff --git a/Doc/library/xmlrpc.client.rst b/Doc/library/xmlrpc.client.rst index 51279b35fd71b7..8d9db53ef1f0da 100644 --- a/Doc/library/xmlrpc.client.rst +++ b/Doc/library/xmlrpc.client.rst @@ -169,12 +169,6 @@ between conformable Python objects and XML on the wire. `XML-RPC Specification `_ The official specification. - `Unofficial XML-RPC Errata `_ - Fredrik Lundh's "unofficial errata, intended to clarify certain - details in the XML-RPC specification, as well as hint at - 'best practices' to use when designing your own XML-RPC - implementations." - .. _serverproxy-objects: ServerProxy Objects diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst index 3db55e646c47cc..f6288709fec1aa 100644 --- a/Doc/library/zipfile.rst +++ b/Doc/library/zipfile.rst @@ -78,7 +78,6 @@ The module defines the following items: of the last modification to the file; the fields are described in section :ref:`zipinfo-objects`. - .. function:: is_zipfile(filename) Returns ``True`` if *filename* is a valid ZIP file based on its magic number, @@ -290,7 +289,7 @@ ZipFile Objects compressed text files in :term:`universal newlines` mode. .. versionchanged:: 3.6 - :meth:`open` can now be used to write files into the archive with the + :meth:`ZipFile.open` can now be used to write files into the archive with the ``mode='w'`` option. .. versionchanged:: 3.6 @@ -406,6 +405,11 @@ ZipFile Objects If ``arcname`` (or ``filename``, if ``arcname`` is not given) contains a null byte, the name of the file in the archive will be truncated at the null byte. + .. note:: + + A leading slash in the filename may lead to the archive being impossible to + open in some zip programs on Windows systems. + .. versionchanged:: 3.6 Calling :meth:`write` on a ZipFile created with mode ``'r'`` or a closed ZipFile will raise a :exc:`ValueError`. Previously, @@ -857,6 +861,8 @@ Exceeding limitations on different file systems can cause decompression failed. Such as allowable characters in the directory entries, length of the file name, length of the pathname, size of a single file, and number of files, etc. +.. _zipfile-resources-limitations: + Resources limitations ~~~~~~~~~~~~~~~~~~~~~ diff --git a/Doc/library/zipimport.rst b/Doc/library/zipimport.rst index cd7030fed01732..fe1adcae163c23 100644 --- a/Doc/library/zipimport.rst +++ b/Doc/library/zipimport.rst @@ -23,8 +23,8 @@ and a path within the archive can be specified to only import from a subdirectory. For example, the path :file:`example.zip/lib/` would only import from the :file:`lib/` subdirectory within the archive. -Any files may be present in the ZIP archive, but only files :file:`.py` and -:file:`.pyc` are available for import. ZIP import of dynamic modules +Any files may be present in the ZIP archive, but importers are only invoked for +:file:`.py` and :file:`.pyc` files. ZIP import of dynamic modules (:file:`.pyd`, :file:`.so`) is disallowed. Note that if an archive only contains :file:`.py` files, Python will not attempt to modify the archive by adding the corresponding :file:`.pyc` file, meaning that if a ZIP archive diff --git a/Doc/library/zlib.rst b/Doc/library/zlib.rst index ec60ea24db6627..6758c615d74db8 100644 --- a/Doc/library/zlib.rst +++ b/Doc/library/zlib.rst @@ -42,10 +42,9 @@ The available exception and functions in this module are: for use as a general hash algorithm. .. versionchanged:: 3.0 - Always returns an unsigned value. - To generate the same numeric value across all Python versions and - platforms, use ``adler32(data) & 0xffffffff``. - + The result is always unsigned. + To generate the same numeric value when using Python 2 or earlier, + use ``adler32(data) & 0xffffffff``. .. function:: compress(data, /, level=-1) @@ -127,10 +126,9 @@ The available exception and functions in this module are: for use as a general hash algorithm. .. versionchanged:: 3.0 - Always returns an unsigned value. - To generate the same numeric value across all Python versions and - platforms, use ``crc32(data) & 0xffffffff``. - + The result is always unsigned. + To generate the same numeric value when using Python 2 or earlier, + use ``crc32(data) & 0xffffffff``. .. function:: decompress(data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE) diff --git a/Doc/license.rst b/Doc/license.rst index f487d98b2b43a3..e0ca5f2662dc1c 100644 --- a/Doc/license.rst +++ b/Doc/license.rst @@ -100,7 +100,7 @@ PSF LICENSE AGREEMENT FOR PYTHON |release| analyze, test, perform and/or display publicly, prepare derivative works, distribute, and otherwise use Python |release| alone or in any derivative version, provided, however, that PSF's License Agreement and PSF's notice of - copyright, i.e., "Copyright © 2001-2021 Python Software Foundation; All Rights + copyright, i.e., "Copyright © 2001-2022 Python Software Foundation; All Rights Reserved" are retained in Python |release| alone or in any derivative version prepared by Licensee. @@ -203,9 +203,9 @@ CNRI LICENSE AGREEMENT FOR PYTHON 1.6.1 Licensee may substitute the following text (omitting the quotes): "Python 1.6.1 is made available subject to the terms and conditions in CNRI's License Agreement. This Agreement together with Python 1.6.1 may be located on the - Internet using the following unique, persistent identifier (known as a handle): + internet using the following unique, persistent identifier (known as a handle): 1895.22/1013. This Agreement may also be obtained from a proxy server on the - Internet using the following URL: http://hdl.handle.net/1895.22/1013." + internet using the following URL: http://hdl.handle.net/1895.22/1013." 3. In the event Licensee prepares a derivative work that is based on or incorporates Python 1.6.1 or any part thereof, and wants to make the derivative @@ -655,7 +655,7 @@ OpenSSL The modules :mod:`hashlib`, :mod:`posix`, :mod:`ssl`, :mod:`crypt` use the OpenSSL library for added performance if made available by the -operating system. Additionally, the Windows and Mac OS X installers for +operating system. Additionally, the Windows and macOS installers for Python may include a copy of the OpenSSL libraries, so we include a copy of the OpenSSL license here:: @@ -955,7 +955,7 @@ W3C C14N test suite The C14N 2.0 test suite in the :mod:`test` package (``Lib/test/xmltestdata/c14n-20/``) was retrieved from the W3C website at https://www.w3.org/TR/xml-c14n2-testcases/ and is distributed under the -3-clause BSD license: +3-clause BSD license:: Copyright (c) 2013 W3C(R) (MIT, ERCIM, Keio, Beihang), All Rights Reserved. diff --git a/Doc/make.bat b/Doc/make.bat index 7fde0636427713..ac66b56e9a3ef7 100644 --- a/Doc/make.bat +++ b/Doc/make.bat @@ -13,7 +13,7 @@ if not defined SPHINXBUILD ( %PYTHON% -c "import sphinx" > nul 2> nul if errorlevel 1 ( echo Installing sphinx with %PYTHON% - %PYTHON% -m pip install sphinx==2.2.0 + %PYTHON% -m pip install -r requirements.txt if errorlevel 1 exit /B ) set SPHINXBUILD=%PYTHON% -c "import sphinx.cmd.build, sys; sys.exit(sphinx.cmd.build.main())" @@ -30,6 +30,7 @@ if not defined BLURB ( %PYTHON% -c "import blurb" > nul 2> nul if errorlevel 1 ( echo Installing blurb with %PYTHON% + rem Should have been installed with Sphinx earlier %PYTHON% -m pip install blurb if errorlevel 1 exit /B ) diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index 96bd9b028d5044..871c101996b155 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -196,27 +196,6 @@ the built-in function :func:`range` returns an iterator of integers suitable to emulate the effect of Pascal's ``for i := a to b do``; e.g., ``list(range(3))`` returns the list ``[0, 1, 2]``. -.. note:: - - .. index:: - single: loop; over mutable sequence - single: mutable sequence; loop over - - There is a subtlety when the sequence is being modified by the loop (this can - only occur for mutable sequences, e.g. lists). An internal counter is used - to keep track of which item is used next, and this is incremented on each - iteration. When this counter has reached the length of the sequence the loop - terminates. This means that if the suite deletes the current (or a previous) - item from the sequence, the next item will be skipped (since it gets the - index of the current item which has already been treated). Likewise, if the - suite inserts an item in the sequence before the current item, the current - item will be treated again the next time through the loop. This can lead to - nasty bugs that can be avoided by making a temporary copy using a slice of - the whole sequence, e.g., :: - - for x in a[:]: - if x < 0: a.remove(x) - .. _try: .. _except: @@ -254,9 +233,10 @@ is found that matches the exception. An expression-less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is "compatible" with the exception. An object is -compatible with an exception if it is the class or a base class of the exception -object, or a tuple containing an item that is the class or a base class of -the exception object. +compatible with an exception if the object is the class or a +:term:`non-virtual base class ` of the exception object, +or a tuple containing an item that is the class or a non-virtual base class +of the exception object. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [#]_ @@ -418,8 +398,8 @@ usage patterns to be encapsulated for convenient reuse. The execution of the :keyword:`with` statement with one "item" proceeds as follows: -#. The context expression (the expression given in the :token:`with_item`) is - evaluated to obtain a context manager. +#. The context expression (the expression given in the + :token:`~python-grammar:with_item`) is evaluated to obtain a context manager. #. The context manager's :meth:`__enter__` is loaded for later use. @@ -585,8 +565,8 @@ Here's an overview of the logical flow of a match statement: #. If the pattern succeeds, the corresponding guard (if present) is evaluated. In this case all name bindings are guaranteed to have happened. - * If the guard evaluates as truthy or missing, the ``block`` inside ``case_block`` is - executed. + * If the guard evaluates as true or is missing, the ``block`` inside + ``case_block`` is executed. * Otherwise, the next ``case_block`` is attempted as described above. @@ -637,10 +617,10 @@ The logical flow of a ``case`` block with a ``guard`` follows: #. If the pattern succeeded, evaluate the ``guard``. - * If the ``guard`` condition evaluates to "truthy", the case block is + * If the ``guard`` condition evaluates as true, the case block is selected. - * If the ``guard`` condition evaluates to "falsy", the case block is not + * If the ``guard`` condition evaluates as false, the case block is not selected. * If the ``guard`` raises an exception during evaluation, the exception @@ -797,7 +777,8 @@ Syntax: capture_pattern: !'_' NAME A single underscore ``_`` is not a capture pattern (this is what ``!'_'`` -expresses). And is instead treated as a :token:`wildcard_pattern`. +expresses). It is instead treated as a +:token:`~python-grammar:wildcard_pattern`. In a given pattern, a given name can only be bound once. E.g. ``case x, x: ...`` is invalid while ``case [x] | x: ...`` is allowed. @@ -820,7 +801,9 @@ and binds no name. Syntax: .. productionlist:: python-grammar wildcard_pattern: '_' -``_`` is a :ref:`soft keyword `. +``_`` is a :ref:`soft keyword ` within any pattern, +but only within patterns. It is an identifier, as usual, even within +``match`` subject expressions, ``guard``\ s, and ``case`` blocks. In simple terms, ``_`` will always succeed. @@ -898,8 +881,8 @@ sequence pattern. The following is the logical flow for matching a sequence pattern against a subject value: -#. If the subject value is not an instance of a - :class:`collections.abc.Sequence` the sequence pattern fails. +#. If the subject value is not a sequence [#]_, the sequence pattern + fails. #. If the subject value is an instance of ``str``, ``bytes`` or ``bytearray`` the sequence pattern fails. @@ -941,7 +924,7 @@ subject value: In simple terms ``[P1, P2, P3,`` ... ``, P]`` matches only if all the following happens: -* ``isinstance(, collections.abc.Sequence)`` +* check ```` is a sequence * ``len(subject) == `` * ``P1`` matches ``[0]`` (note that this match can also bind names) * ``P2`` matches ``[1]`` (note that this match can also bind names) @@ -966,22 +949,22 @@ Syntax: At most one double star pattern may be in a mapping pattern. The double star pattern must be the last subpattern in the mapping pattern. -Duplicate key values in mapping patterns are disallowed. (If all key patterns -are literal patterns this is considered a syntax error; otherwise this is a -runtime error and will raise :exc:`ValueError`.) +Duplicate keys in mapping patterns are disallowed. Duplicate literal keys will +raise a :exc:`SyntaxError`. Two keys that otherwise have the same value will +raise a :exc:`ValueError` at runtime. The following is the logical flow for matching a mapping pattern against a subject value: -#. If the subject value is not an instance of :class:`collections.abc.Mapping`, - the mapping pattern fails. +#. If the subject value is not a mapping [#]_,the mapping pattern fails. #. If every key given in the mapping pattern is present in the subject mapping, and the pattern for each key matches the corresponding item of the subject mapping, the mapping pattern succeeds. #. If duplicate keys are detected in the mapping pattern, the pattern is - considered invalid and :exc:`ValueError` is raised. + considered invalid. A :exc:`SyntaxError` is raised for duplicate literal + values; or a :exc:`ValueError` for named keys of the same value. .. note:: Key-value pairs are matched using the two-argument form of the mapping subject's ``get()`` method. Matched key-value pairs must already be present @@ -991,7 +974,7 @@ subject value: In simple terms ``{KEY1: P1, KEY2: P2, ... }`` matches only if all the following happens: -* ``isinstance(, collections.abc.Mapping)`` +* check ```` is a mapping * ``KEY1 in `` * ``P1`` matches ``[KEY1]`` * ... and so on for the corresponding KEY/pattern pair. @@ -1015,7 +998,7 @@ A class pattern represents a class and its positional and keyword arguments The same keyword should not be repeated in class patterns. -The following is the logical flow for matching a mapping pattern against a +The following is the logical flow for matching a class pattern against a subject value: #. If ``name_or_attr`` is not an instance of the builtin :class:`type` , raise @@ -1053,7 +1036,7 @@ subject value: patterns using the :data:`~object.__match_args__` attribute on the class ``name_or_attr`` before matching: - I. The equivalent of ``getattr(cls, "__match_args__", ()))`` is called. + I. The equivalent of ``getattr(cls, "__match_args__", ())`` is called. * If this raises an exception, the exception bubbles up. @@ -1180,9 +1163,9 @@ is roughly equivalent to :: except that the original function is not temporarily bound to the name ``func``. .. versionchanged:: 3.9 - Functions may be decorated with any valid :token:`assignment_expression`. - Previously, the grammar was much more restrictive; see :pep:`614` for - details. + Functions may be decorated with any valid + :token:`~python-grammar:assignment_expression`. Previously, the grammar was + much more restrictive; see :pep:`614` for details. .. index:: triple: default; parameter; value @@ -1358,9 +1341,9 @@ The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. .. versionchanged:: 3.9 - Classes may be decorated with any valid :token:`assignment_expression`. - Previously, the grammar was much more restrictive; see :pep:`614` for - details. + Classes may be decorated with any valid + :token:`~python-grammar:assignment_expression`. Previously, the grammar was + much more restrictive; see :pep:`614` for details. **Programmer's note:** Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a @@ -1524,6 +1507,35 @@ body of a coroutine function. there is a :keyword:`finally` clause which happens to raise another exception. That new exception causes the old one to be lost. +.. [#] In pattern matching, a sequence is defined as one of the following: + + * a class that inherits from :class:`collections.abc.Sequence` + * a Python class that has been registered as :class:`collections.abc.Sequence` + * a builtin class that has its (CPython) :data:`Py_TPFLAGS_SEQUENCE` bit set + * a class that inherits from any of the above + + The following standard library classes are sequences: + + * :class:`array.array` + * :class:`collections.deque` + * :class:`list` + * :class:`memoryview` + * :class:`range` + * :class:`tuple` + + .. note:: Subject values of type ``str``, ``bytes``, and ``bytearray`` + do not match sequence patterns. + +.. [#] In pattern matching, a mapping is defined as one of the following: + + * a class that inherits from :class:`collections.abc.Mapping` + * a Python class that has been registered as :class:`collections.abc.Mapping` + * a builtin class that has its (CPython) :data:`Py_TPFLAGS_MAPPING` bit set + * a class that inherits from any of the above + + The standard library classes :class:`dict` and :class:`types.MappingProxyType` + are mappings. + .. [#] A string literal appearing as the first statement in the function body is transformed into the function's ``__doc__`` attribute and therefore the function's :term:`docstring`. diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index eefdc3d5100b56..a5739e6e5e4723 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -188,7 +188,7 @@ Ellipsis representation in computers. The string representations of the numeric classes, computed by - :meth:`__repr__` and :meth:`__str__`, have the following + :meth:`~object.__repr__` and :meth:`~object.__str__`, have the following properties: * They are valid numeric literals which, when passed to their @@ -648,13 +648,13 @@ Callable types A function or method which uses the :keyword:`yield` statement (see section :ref:`yield`) is called a :dfn:`generator function`. Such a function, when - called, always returns an iterator object which can be used to execute the - body of the function: calling the iterator's :meth:`iterator.__next__` - method will cause the function to execute until it provides a value - using the :keyword:`!yield` statement. When the function executes a - :keyword:`return` statement or falls off the end, a :exc:`StopIteration` - exception is raised and the iterator will have reached the end of the set of - values to be returned. + called, always returns an :term:`iterator` object which can be used to + execute the body of the function: calling the iterator's + :meth:`iterator.__next__` method will cause the function to execute until + it provides a value using the :keyword:`!yield` statement. When the + function executes a :keyword:`return` statement or falls off the end, a + :exc:`StopIteration` exception is raised and the iterator will have + reached the end of the set of values to be returned. Coroutine functions .. index:: @@ -674,10 +674,11 @@ Callable types A function or method which is defined using :keyword:`async def` and which uses the :keyword:`yield` statement is called a :dfn:`asynchronous generator function`. Such a function, when called, - returns an asynchronous iterator object which can be used in an + returns an :term:`asynchronous iterator` object which can be used in an :keyword:`async for` statement to execute the body of the function. - Calling the asynchronous iterator's :meth:`aiterator.__anext__` method + Calling the asynchronous iterator's + :meth:`aiterator.__anext__ ` method will return an :term:`awaitable` which when awaited will execute until it provides a value using the :keyword:`yield` expression. When the function executes an empty :keyword:`return` @@ -715,13 +716,13 @@ Callable types Classes Classes are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that - override :meth:`__new__`. The arguments of the call are passed to - :meth:`__new__` and, in the typical case, to :meth:`__init__` to + override :meth:`~object.__new__`. The arguments of the call are passed to + :meth:`__new__` and, in the typical case, to :meth:`~object.__init__` to initialize the new instance. Class Instances Instances of arbitrary classes can be made callable by defining a - :meth:`__call__` method in their class. + :meth:`~object.__call__` method in their class. Modules @@ -880,14 +881,14 @@ Class instances section :ref:`descriptors` for another way in which attributes of a class retrieved via its instances may differ from the objects actually stored in the class's :attr:`~object.__dict__`. If no class attribute is found, and the - object's class has a :meth:`__getattr__` method, that is called to satisfy + object's class has a :meth:`~object.__getattr__` method, that is called to satisfy the lookup. .. index:: triple: class instance; attribute; assignment Attribute assignments and deletions update the instance's dictionary, never a - class's dictionary. If the class has a :meth:`__setattr__` or - :meth:`__delattr__` method, this is called instead of updating the instance + class's dictionary. If the class has a :meth:`~object.__setattr__` or + :meth:`~object.__delattr__` method, this is called instead of updating the instance dictionary directly. .. index:: @@ -1141,7 +1142,8 @@ Internal types Slice objects .. index:: builtin: slice - Slice objects are used to represent slices for :meth:`__getitem__` + Slice objects are used to represent slices for + :meth:`~object.__getitem__` methods. They are also created by the built-in :func:`slice` function. .. index:: @@ -1194,7 +1196,8 @@ A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python's approach to :dfn:`operator overloading`, allowing classes to define their own behavior with respect to language -operators. For instance, if a class defines a method named :meth:`__getitem__`, +operators. For instance, if a class defines a method named +:meth:`~object.__getitem__`, and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined (typically @@ -1202,9 +1205,9 @@ operation raise an exception when no appropriate method is defined (typically Setting a special method to ``None`` indicates that the corresponding operation is not available. For example, if a class sets -:meth:`__iter__` to ``None``, the class is not iterable, so calling +:meth:`~object.__iter__` to ``None``, the class is not iterable, so calling :func:`iter` on its instances will raise a :exc:`TypeError` (without -falling back to :meth:`__getitem__`). [#]_ +falling back to :meth:`~object.__getitem__`). [#]_ When implementing a class that emulates any built-in type, it is important that the emulation only be implemented to the degree that it makes sense for the @@ -1236,7 +1239,7 @@ Basic customization as necessary before returning it. If :meth:`__new__` is invoked during object construction and it returns an - instance or subclass of *cls*, then the new instance’s :meth:`__init__` method + instance of *cls*, then the new instance’s :meth:`__init__` method will be invoked like ``__init__(self[, ...])``, where *self* is the new instance and the remaining arguments are the same as were passed to the object constructor. @@ -1463,7 +1466,7 @@ Basic customization Called by built-in function :func:`hash` and for operations on members of hashed collections including :class:`set`, :class:`frozenset`, and - :class:`dict`. :meth:`__hash__` should return an integer. The only required + :class:`dict`. The ``__hash__()`` method should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and @@ -1523,7 +1526,7 @@ Basic customization This is intended to provide protection against a denial-of-service caused by carefully-chosen inputs that exploit the worst case performance of a - dict insertion, O(n^2) complexity. See + dict insertion, O(n\ :sup:`2`) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details. Changing hash values affects the iteration order of sets. @@ -1739,28 +1742,6 @@ class' :attr:`~object.__dict__`. Called to delete the attribute on an instance *instance* of the owner class. -.. method:: object.__set_name__(self, owner, name) - - Called at the time the owning class *owner* is created. The - descriptor has been assigned to *name*. - - .. note:: - - :meth:`__set_name__` is only called implicitly as part of the - :class:`type` constructor, so it will need to be called explicitly with - the appropriate parameters when a descriptor is added to a class after - initial creation:: - - class A: - pass - descr = custom_descriptor() - A.attr = descr - descr.__set_name__(A, 'attr') - - See :ref:`class-object-creation` for more details. - - .. versionadded:: 3.6 - The attribute :attr:`__objclass__` is interpreted by the :mod:`inspect` module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). @@ -1776,7 +1757,8 @@ Invoking Descriptors In general, a descriptor is an object attribute with "binding behavior", one whose attribute access has been overridden by methods in the descriptor -protocol: :meth:`__get__`, :meth:`__set__`, and :meth:`__delete__`. If any of +protocol: :meth:`~object.__get__`, :meth:`~object.__set__`, and +:meth:`~object.__delete__`. If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the @@ -1807,12 +1789,13 @@ Class Binding Super Binding If ``a`` is an instance of :class:`super`, then the binding ``super(B, obj).m()`` searches ``obj.__class__.__mro__`` for the base class ``A`` - immediately preceding ``B`` and then invokes the descriptor with the call: + immediately following ``B`` and then invokes the descriptor with the call: ``A.__dict__['m'].__get__(obj, obj.__class__)``. For instance bindings, the precedence of descriptor invocation depends on which descriptor methods are defined. A descriptor can define any combination -of :meth:`__get__`, :meth:`__set__` and :meth:`__delete__`. If it does not +of :meth:`~object.__get__`, :meth:`~object.__set__` and +:meth:`~object.__delete__`. If it does not define :meth:`__get__`, then accessing the attribute will return the descriptor object itself unless there is a value in the object's instance dictionary. If the descriptor defines :meth:`__set__` and/or :meth:`__delete__`, it is a data @@ -1823,7 +1806,8 @@ descriptors have just the :meth:`__get__` method. Data descriptors with instance dictionary. In contrast, non-data descriptors can be overridden by instances. -Python methods (including :func:`staticmethod` and :func:`classmethod`) are +Python methods (including those decorated with +:func:`@staticmethod ` and :func:`@classmethod `) are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. @@ -1838,46 +1822,50 @@ __slots__ ^^^^^^^^^ *__slots__* allow us to explicitly declare data members (like -properties) and deny the creation of *__dict__* and *__weakref__* +properties) and deny the creation of :attr:`~object.__dict__` and *__weakref__* (unless explicitly declared in *__slots__* or available in a parent.) -The space saved over using *__dict__* can be significant. +The space saved over using :attr:`~object.__dict__` can be significant. Attribute lookup speed can be significantly improved as well. .. data:: object.__slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. *__slots__* reserves space - for the declared variables and prevents the automatic creation of *__dict__* + for the declared variables and prevents the automatic creation of + :attr:`~object.__dict__` and *__weakref__* for each instance. Notes on using *__slots__* """""""""""""""""""""""""" -* When inheriting from a class without *__slots__*, the *__dict__* and +* When inheriting from a class without *__slots__*, the + :attr:`~object.__dict__` and *__weakref__* attribute of the instances will always be accessible. -* Without a *__dict__* variable, instances cannot be assigned new variables not +* Without a :attr:`~object.__dict__` variable, instances cannot be assigned new + variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises :exc:`AttributeError`. If dynamic assignment of new variables is desired, then add ``'__dict__'`` to the sequence of strings in the *__slots__* declaration. * Without a *__weakref__* variable for each instance, classes defining - *__slots__* do not support weak references to its instances. If weak reference + *__slots__* do not support :mod:`weak references ` to its instances. + If weak reference support is needed, then add ``'__weakref__'`` to the sequence of strings in the *__slots__* declaration. -* *__slots__* are implemented at the class level by creating descriptors - (:ref:`descriptors`) for each variable name. As a result, class attributes +* *__slots__* are implemented at the class level by creating :ref:`descriptors ` + for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is not limited to the class where it is defined. *__slots__* declared in parents are available in - child classes. However, child subclasses will get a *__dict__* and + child classes. However, child subclasses will get a :attr:`~object.__dict__` and *__weakref__* unless they also define *__slots__* (which should only contain names of any *additional* slots). @@ -1889,18 +1877,24 @@ Notes on using *__slots__* * Nonempty *__slots__* does not work for classes derived from "variable-length" built-in types such as :class:`int`, :class:`bytes` and :class:`tuple`. -* Any non-string iterable may be assigned to *__slots__*. Mappings may also be - used; however, in the future, special meaning may be assigned to the values - corresponding to each key. +* Any non-string :term:`iterable` may be assigned to *__slots__*. -* *__class__* assignment works only if both classes have the same *__slots__*. +* If a :class:`dictionary ` is used to assign *__slots__*, the dictionary + keys will be used as the slot names. The values of the dictionary can be used + to provide per-attribute docstrings that will be recognised by + :func:`inspect.getdoc` and displayed in the output of :func:`help`. -* Multiple inheritance with multiple slotted parent classes can be used, +* :attr:`~instance.__class__` assignment works only if both classes have the + same *__slots__*. + +* :ref:`Multiple inheritance ` with multiple slotted parent + classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise :exc:`TypeError`. -* If an iterator is used for *__slots__* then a descriptor is created for each +* If an :term:`iterator` is used for *__slots__* then a :term:`descriptor` is + created for each of the iterator's values. However, the *__slots__* attribute will be an empty iterator. @@ -1909,8 +1903,8 @@ Notes on using *__slots__* Customizing class creation -------------------------- -Whenever a class inherits from another class, *__init_subclass__* is -called on that class. This way, it is possible to write classes which +Whenever a class inherits from another class, :meth:`~object.__init_subclass__` is +called on the parent class. This way, it is possible to write classes which change the behavior of subclasses. This is closely related to class decorators, but where class decorators only affect the specific class they're applied to, ``__init_subclass__`` solely applies to future subclasses of the @@ -1949,6 +1943,33 @@ class defining the method. .. versionadded:: 3.6 +When a class is created, :meth:`type.__new__` scans the class variables +and makes callbacks to those with a :meth:`~object.__set_name__` hook. + +.. method:: object.__set_name__(self, owner, name) + + Automatically called at the time the owning class *owner* is + created. The object has been assigned to *name* in that class:: + + class A: + x = C() # Automatically calls: x.__set_name__(A, 'x') + + If the class variable is assigned after the class is created, + :meth:`__set_name__` will not be called automatically. + If needed, :meth:`__set_name__` can be called directly:: + + class A: + pass + + c = C() + A.x = c # The hook is not called + c.__set_name__(A, 'x') # Manually invoke the hook + + See :ref:`class-object-creation` for more details. + + .. versionadded:: 3.6 + + .. _metaclasses: Metaclasses @@ -2035,7 +2056,8 @@ Once the appropriate metaclass has been identified, then the class namespace is prepared. If the metaclass has a ``__prepare__`` attribute, it is called as ``namespace = metaclass.__prepare__(name, bases, **kwds)`` (where the additional keyword arguments, if any, come from the class definition). The -``__prepare__`` method should be implemented as a :func:`classmethod`. The +``__prepare__`` method should be implemented as a +:func:`classmethod `. The namespace returned by ``__prepare__`` is passed in to ``__new__``, but when the final class object is created the namespace is copied into a new ``dict``. @@ -2098,15 +2120,15 @@ current call is identified based on the first argument passed to the method. Failing to do so will result in a :exc:`RuntimeError` in Python 3.8. When using the default metaclass :class:`type`, or any metaclass that ultimately -calls ``type.__new__``, the following additional customisation steps are +calls ``type.__new__``, the following additional customization steps are invoked after creating the class object: -* first, ``type.__new__`` collects all of the descriptors in the class - namespace that define a :meth:`~object.__set_name__` method; -* second, all of these ``__set_name__`` methods are called with the class - being defined and the assigned name of that particular descriptor; -* finally, the :meth:`~object.__init_subclass__` hook is called on the - immediate parent of the new class in its method resolution order. +1) The ``type.__new__`` method collects all of the attributes in the class + namespace that define a :meth:`~object.__set_name__` method; +2) Those ``__set_name__`` methods are called with the class + being defined and the assigned name of that particular attribute; +3) The :meth:`~object.__init_subclass__` hook is called on the + immediate parent of the new class in its method resolution order. After the class object is created, it is passed to the class decorators included in the class definition (if any) and the resulting object is bound @@ -2175,22 +2197,142 @@ case the instance is itself a class. Emulating generic types ----------------------- -One can implement the generic class syntax as specified by :pep:`484` -(for example ``List[int]``) by defining a special method: +When using :term:`type annotations`, it is often useful to +*parameterize* a :term:`generic type` using Python's square-brackets notation. +For example, the annotation ``list[int]`` might be used to signify a +:class:`list` in which all the elements are of type :class:`int`. + +.. seealso:: + + :pep:`484` - Type Hints + Introducing Python's framework for type annotations + + :ref:`Generic Alias Types` + Documentation for objects representing parameterized generic classes + + :ref:`Generics`, :ref:`user-defined generics` and :class:`typing.Generic` + Documentation on how to implement generic classes that can be + parameterized at runtime and understood by static type-checkers. + +A class can *generally* only be parameterized if it defines the special +class method ``__class_getitem__()``. .. classmethod:: object.__class_getitem__(cls, key) Return an object representing the specialization of a generic class by type arguments found in *key*. -This method is looked up on the class object itself, and when defined in -the class body, this method is implicitly a class method. Note, this -mechanism is primarily reserved for use with static type hints, other usage -is discouraged. + When defined on a class, ``__class_getitem__()`` is automatically a class + method. As such, there is no need for it to be decorated with + :func:`@classmethod` when it is defined. -.. seealso:: - :pep:`560` - Core support for typing module and generic types +The purpose of *__class_getitem__* +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The purpose of :meth:`~object.__class_getitem__` is to allow runtime +parameterization of standard-library generic classes in order to more easily +apply :term:`type hints` to these classes. + +To implement custom generic classes that can be parameterized at runtime and +understood by static type-checkers, users should either inherit from a standard +library class that already implements :meth:`~object.__class_getitem__`, or +inherit from :class:`typing.Generic`, which has its own implementation of +``__class_getitem__()``. + +Custom implementations of :meth:`~object.__class_getitem__` on classes defined +outside of the standard library may not be understood by third-party +type-checkers such as mypy. Using ``__class_getitem__()`` on any class for +purposes other than type hinting is discouraged. + + +.. _classgetitem-versus-getitem: + + +*__class_getitem__* versus *__getitem__* +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Usually, the :ref:`subscription` of an object using square +brackets will call the :meth:`~object.__getitem__` instance method defined on +the object's class. However, if the object being subscribed is itself a class, +the class method :meth:`~object.__class_getitem__` may be called instead. +``__class_getitem__()`` should return a :ref:`GenericAlias` +object if it is properly defined. + +Presented with the :term:`expression` ``obj[x]``, the Python interpreter +follows something like the following process to decide whether +:meth:`~object.__getitem__` or :meth:`~object.__class_getitem__` should be +called:: + + from inspect import isclass + + def subscribe(obj, x): + """Return the result of the expression `obj[x]`""" + + class_of_obj = type(obj) + + # If the class of obj defines __getitem__, + # call class_of_obj.__getitem__(obj, x) + if hasattr(class_of_obj, '__getitem__'): + return class_of_obj.__getitem__(obj, x) + + # Else, if obj is a class and defines __class_getitem__, + # call obj.__class_getitem__(x) + elif isclass(obj) and hasattr(obj, '__class_getitem__'): + return obj.__class_getitem__(x) + + # Else, raise an exception + else: + raise TypeError( + f"'{class_of_obj.__name__}' object is not subscriptable" + ) + +In Python, all classes are themselves instances of other classes. The class of +a class is known as that class's :term:`metaclass`, and most classes have the +:class:`type` class as their metaclass. :class:`type` does not define +:meth:`~object.__getitem__`, meaning that expressions such as ``list[int]``, +``dict[str, float]`` and ``tuple[str, bytes]`` all result in +:meth:`~object.__class_getitem__` being called:: + + >>> # list has class "type" as its metaclass, like most classes: + >>> type(list) + + >>> type(dict) == type(list) == type(tuple) == type(str) == type(bytes) + True + >>> # "list[int]" calls "list.__class_getitem__(int)" + >>> list[int] + list[int] + >>> # list.__class_getitem__ returns a GenericAlias object: + >>> type(list[int]) + + +However, if a class has a custom metaclass that defines +:meth:`~object.__getitem__`, subscribing the class may result in different +behaviour. An example of this can be found in the :mod:`enum` module:: + + >>> from enum import Enum + >>> class Menu(Enum): + ... """A breakfast menu""" + ... SPAM = 'spam' + ... BACON = 'bacon' + ... + >>> # Enum classes have a custom metaclass: + >>> type(Menu) + + >>> # EnumMeta defines __getitem__, + >>> # so __class_getitem__ is not called, + >>> # and the result is not a GenericAlias object: + >>> Menu['SPAM'] + + >>> type(Menu['SPAM']) + + + +.. seealso:: + :pep:`560` - Core Support for typing module and generic types + Introducing :meth:`~object.__class_getitem__`, and outlining when a + :ref:`subscription` results in ``__class_getitem__()`` + being called instead of :meth:`~object.__getitem__` .. _callable-types: @@ -2213,31 +2355,36 @@ Emulating container types ------------------------- The following methods can be defined to implement container objects. Containers -usually are sequences (such as lists or tuples) or mappings (like dictionaries), +usually are :term:`sequences ` (such as :class:`lists ` or +:class:`tuples `) or :term:`mappings ` (like +:class:`dictionaries `), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which ``0 <= k < -N`` where *N* is the length of the sequence, or slice objects, which define a +N`` where *N* is the length of the sequence, or :class:`slice` objects, which define a range of items. It is also recommended that mappings provide the methods :meth:`keys`, :meth:`values`, :meth:`items`, :meth:`get`, :meth:`clear`, :meth:`setdefault`, :meth:`pop`, :meth:`popitem`, :meth:`!copy`, and -:meth:`update` behaving similar to those for Python's standard dictionary +:meth:`update` behaving similar to those for Python's standard :class:`dictionary ` objects. The :mod:`collections.abc` module provides a :class:`~collections.abc.MutableMapping` -abstract base class to help create those methods from a base set of -:meth:`__getitem__`, :meth:`__setitem__`, :meth:`__delitem__`, and :meth:`keys`. +:term:`abstract base class` to help create those methods from a base set of +:meth:`~object.__getitem__`, :meth:`~object.__setitem__`, :meth:`~object.__delitem__`, and :meth:`keys`. Mutable sequences should provide methods :meth:`append`, :meth:`count`, :meth:`index`, :meth:`extend`, :meth:`insert`, :meth:`pop`, :meth:`remove`, -:meth:`reverse` and :meth:`sort`, like Python standard list objects. Finally, +:meth:`reverse` and :meth:`sort`, like Python standard :class:`list` +objects. Finally, sequence types should implement addition (meaning concatenation) and -multiplication (meaning repetition) by defining the methods :meth:`__add__`, -:meth:`__radd__`, :meth:`__iadd__`, :meth:`__mul__`, :meth:`__rmul__` and -:meth:`__imul__` described below; they should not define other numerical +multiplication (meaning repetition) by defining the methods +:meth:`~object.__add__`, :meth:`~object.__radd__`, :meth:`~object.__iadd__`, +:meth:`~object.__mul__`, :meth:`~object.__rmul__` and :meth:`~object.__imul__` +described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the -:meth:`__contains__` method to allow efficient use of the ``in`` operator; for +:meth:`~object.__contains__` method to allow efficient use of the ``in`` +operator; for mappings, ``in`` should search the mapping's keys; for sequences, it should search through the values. It is further recommended that both mappings and -sequences implement the :meth:`__iter__` method to allow efficient iteration +sequences implement the :meth:`~object.__iter__` method to allow efficient iteration through the container; for mappings, :meth:`__iter__` should iterate through the object's keys; for sequences, it should iterate through the values. @@ -2290,19 +2437,27 @@ through the object's keys; for sequences, it should iterate through the values. .. method:: object.__getitem__(self, key) - Called to implement evaluation of ``self[key]``. For sequence types, the - accepted keys should be integers and slice objects. Note that the special - interpretation of negative indexes (if the class wishes to emulate a sequence - type) is up to the :meth:`__getitem__` method. If *key* is of an inappropriate - type, :exc:`TypeError` may be raised; if of a value outside the set of indexes - for the sequence (after any special interpretation of negative values), - :exc:`IndexError` should be raised. For mapping types, if *key* is missing (not - in the container), :exc:`KeyError` should be raised. + Called to implement evaluation of ``self[key]``. For :term:`sequence` types, + the accepted keys should be integers and slice objects. Note that the + special interpretation of negative indexes (if the class wishes to emulate a + :term:`sequence` type) is up to the :meth:`__getitem__` method. If *key* is + of an inappropriate type, :exc:`TypeError` may be raised; if of a value + outside the set of indexes for the sequence (after any special + interpretation of negative values), :exc:`IndexError` should be raised. For + :term:`mapping` types, if *key* is missing (not in the container), + :exc:`KeyError` should be raised. .. note:: - :keyword:`for` loops expect that an :exc:`IndexError` will be raised for illegal - indexes to allow proper detection of the end of the sequence. + :keyword:`for` loops expect that an :exc:`IndexError` will be raised for + illegal indexes to allow proper detection of the end of the sequence. + + .. note:: + + When :ref:`subscripting` a *class*, the special + class method :meth:`~object.__class_getitem__` may be called instead of + ``__getitem__()``. See :ref:`classgetitem-versus-getitem` for more + details. .. method:: object.__setitem__(self, key, value) @@ -2331,12 +2486,10 @@ through the object's keys; for sequences, it should iterate through the values. .. method:: object.__iter__(self) - This method is called when an iterator is required for a container. This method - should return a new iterator object that can iterate over all the objects in the - container. For mappings, it should iterate over the keys of the container. - - Iterator objects also need to implement this method; they are required to return - themselves. For more information on iterator objects, see :ref:`typeiter`. + This method is called when an :term:`iterator` is required for a container. + This method should return a new iterator object that can iterate over all the + objects in the container. For mappings, it should iterate over the keys of + the container. .. method:: object.__reversed__(self) @@ -2536,8 +2689,8 @@ left undefined. return the value of the object truncated to an :class:`~numbers.Integral` (typically an :class:`int`). - If :meth:`__int__` is not defined then the built-in function :func:`int` - falls back to :meth:`__trunc__`. + The built-in function :func:`int` falls back to :meth:`__trunc__` if neither + :meth:`__int__` nor :meth:`__index__` is defined. .. _context-managers: @@ -2643,7 +2796,8 @@ exception:: TypeError: object of type 'C' has no len() The rationale behind this behaviour lies with a number of special methods such -as :meth:`__hash__` and :meth:`__repr__` that are implemented by all objects, +as :meth:`~object.__hash__` and :meth:`~object.__repr__` that are implemented +by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself:: @@ -2666,7 +2820,7 @@ the instance when looking up special methods:: In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the -:meth:`__getattribute__` method even of the object's metaclass:: +:meth:`~object.__getattribute__` method even of the object's metaclass:: >>> class Meta(type): ... def __getattribute__(*args): @@ -2690,7 +2844,7 @@ correctness, implicit special method lookup generally also bypasses the >>> len(c) # Implicit lookup 10 -Bypassing the :meth:`__getattribute__` machinery in this fashion +Bypassing the :meth:`~object.__getattribute__` machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method *must* be set on the class @@ -2707,7 +2861,7 @@ Coroutines Awaitable Objects ----------------- -An :term:`awaitable` object generally implements an :meth:`__await__` method. +An :term:`awaitable` object generally implements an :meth:`~object.__await__` method. :term:`Coroutine objects ` returned from :keyword:`async def` functions are awaitable. @@ -2715,7 +2869,7 @@ are awaitable. The :term:`generator iterator` objects returned from generators decorated with :func:`types.coroutine` or :func:`asyncio.coroutine` - are also awaitable, but they do not implement :meth:`__await__`. + are also awaitable, but they do not implement :meth:`~object.__await__`. .. method:: object.__await__(self) @@ -2734,7 +2888,7 @@ Coroutine Objects ----------------- :term:`Coroutine objects ` are :term:`awaitable` objects. -A coroutine's execution can be controlled by calling :meth:`__await__` and +A coroutine's execution can be controlled by calling :meth:`~object.__await__` and iterating over the result. When the coroutine has finished executing and returns, the iterator raises :exc:`StopIteration`, and the exception's :attr:`~StopIteration.value` attribute holds the return value. If the @@ -2753,20 +2907,21 @@ generators, coroutines do not directly support iteration. Starts or resumes execution of the coroutine. If *value* is ``None``, this is equivalent to advancing the iterator returned by - :meth:`__await__`. If *value* is not ``None``, this method delegates + :meth:`~object.__await__`. If *value* is not ``None``, this method delegates to the :meth:`~generator.send` method of the iterator that caused the coroutine to suspend. The result (return value, :exc:`StopIteration`, or other exception) is the same as when iterating over the :meth:`__await__` return value, described above. -.. method:: coroutine.throw(type[, value[, traceback]]) +.. method:: coroutine.throw(value) + coroutine.throw(type[, value[, traceback]]) Raises the specified exception in the coroutine. This method delegates to the :meth:`~generator.throw` method of the iterator that caused the coroutine to suspend, if it has such a method. Otherwise, the exception is raised at the suspension point. The result (return value, :exc:`StopIteration`, or other exception) is the same as - when iterating over the :meth:`__await__` return value, described + when iterating over the :meth:`~object.__await__` return value, described above. If the exception is not caught in the coroutine, it propagates back to the caller. @@ -2820,11 +2975,11 @@ An example of an asynchronous iterable object:: .. versionadded:: 3.5 .. versionchanged:: 3.7 - Prior to Python 3.7, ``__aiter__`` could return an *awaitable* + Prior to Python 3.7, :meth:`~object.__aiter__` could return an *awaitable* that would resolve to an :term:`asynchronous iterator `. - Starting with Python 3.7, ``__aiter__`` must return an + Starting with Python 3.7, :meth:`~object.__aiter__` must return an asynchronous iterator object. Returning anything else will result in a :exc:`TypeError` error. @@ -2867,8 +3022,9 @@ An example of an asynchronous context manager class:: controlled conditions. It generally isn't a good idea though, since it can lead to some very strange behaviour if it is handled incorrectly. -.. [#] The :meth:`__hash__`, :meth:`__iter__`, :meth:`__reversed__`, and - :meth:`__contains__` methods have special handling for this; others +.. [#] The :meth:`~object.__hash__`, :meth:`~object.__iter__`, + :meth:`~object.__reversed__`, and :meth:`~object.__contains__` methods have + special handling for this; others will still raise a :exc:`TypeError`, but may do so by relying on the behavior that ``None`` is not callable. @@ -2879,5 +3035,6 @@ An example of an asynchronous context manager class:: *blocking* such fallback. .. [#] For operands of the same type, it is assumed that if the non-reflected - method -- such as :meth:`__add__` -- fails then the overall operation is not + method -- such as :meth:`~object.__add__` -- fails then the overall + operation is not supported, which is why the reflected method is not called. diff --git a/Doc/reference/executionmodel.rst b/Doc/reference/executionmodel.rst index 55ac01b6a844dc..d9183561820b2b 100644 --- a/Doc/reference/executionmodel.rst +++ b/Doc/reference/executionmodel.rst @@ -56,15 +56,25 @@ Binding of names .. index:: single: from; import statement -The following constructs bind names: formal parameters to functions, -:keyword:`import` statements, class and function definitions (these bind the -class or function name in the defining block), and targets that are identifiers -if occurring in an assignment, :keyword:`for` loop header, or after -:keyword:`!as` in a :keyword:`with` statement or :keyword:`except` clause. -The :keyword:`!import` statement -of the form ``from ... import *`` binds all names defined in the imported -module, except those beginning with an underscore. This form may only be used -at the module level. +The following constructs bind names: + +* formal parameters to functions, +* class definitions, +* function definitions, +* assignment expressions, +* :ref:`targets ` that are identifiers if occurring in + an assignment: + + + :keyword:`for` loop header, + + after :keyword:`!as` in a :keyword:`with` statement, :keyword:`except` + clause or in the as-pattern in structural pattern matching, + + in a capture pattern in structural pattern matching + +* :keyword:`import` statements. + +The :keyword:`!import` statement of the form ``from ... import *`` binds all +names defined in the imported module, except those beginning with an underscore. +This form may only be used at the module level. A target occurring in a :keyword:`del` statement is also considered bound for this purpose (though the actual semantics are to unbind the name). @@ -119,14 +129,14 @@ is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. -If the :keyword:`global` statement occurs within a block, all uses of the name -specified in the statement refer to the binding of that name in the top-level +If the :keyword:`global` statement occurs within a block, all uses of the names +specified in the statement refer to the bindings of those names in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module :mod:`builtins`. The -global namespace is searched first. If the name is not found there, the +global namespace is searched first. If the names are not found there, the builtins namespace is searched. The :keyword:`!global` statement must precede -all uses of the name. +all uses of the listed names. The :keyword:`global` statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains @@ -249,8 +259,9 @@ a stack traceback, except when the exception is :exc:`SystemExit`. Exceptions are identified by class instances. The :keyword:`except` clause is selected depending on the class of the instance: it must reference the class of -the instance or a base class thereof. The instance can be received by the -handler and can carry additional information about the exceptional condition. +the instance or a :term:`non-virtual base class ` thereof. +The instance can be received by the handler and can carry additional information +about the exceptional condition. .. note:: diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 5ad640ad35d615..1f8b08f70d504e 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -422,9 +422,9 @@ Yield expressions The yield expression is used when defining a :term:`generator` function or an :term:`asynchronous generator` function and thus can only be used in the body of a function definition. Using a yield -expression in a function's body causes that function to be a generator, +expression in a function's body causes that function to be a generator function, and using it in an :keyword:`async def` function's body causes that -coroutine function to be an asynchronous generator. For example:: +coroutine function to be an asynchronous generator function. For example:: def gen(): # defines a generator function yield 123 @@ -445,21 +445,20 @@ functions are described separately in section :ref:`asynchronous-generator-functions`. When a generator function is called, it returns an iterator known as a -generator. That generator then controls the execution of the generator function. -The execution starts when one of the generator's methods is called. At that -time, the execution proceeds to the first yield expression, where it is -suspended again, returning the value of :token:`expression_list` to the generator's -caller. By suspended, we mean that all local state is retained, including the -current bindings of local variables, the instruction pointer, the internal -evaluation stack, and the state of any exception handling. When the execution -is resumed by calling one of the -generator's methods, the function can proceed exactly as if the yield expression -were just another external call. The value of the yield expression after -resuming depends on the method which resumed the execution. If -:meth:`~generator.__next__` is used (typically via either a :keyword:`for` or -the :func:`next` builtin) then the result is :const:`None`. Otherwise, if -:meth:`~generator.send` is used, then the result will be the value passed in to -that method. +generator. That generator then controls the execution of the generator +function. The execution starts when one of the generator's methods is called. +At that time, the execution proceeds to the first yield expression, where it is +suspended again, returning the value of :token:`~python-grammar:expression_list` +to the generator's caller. By suspended, we mean that all local state is +retained, including the current bindings of local variables, the instruction +pointer, the internal evaluation stack, and the state of any exception handling. +When the execution is resumed by calling one of the generator's methods, the +function can proceed exactly as if the yield expression were just another +external call. The value of the yield expression after resuming depends on the +method which resumed the execution. If :meth:`~generator.__next__` is used +(typically via either a :keyword:`for` or the :func:`next` builtin) then the +result is :const:`None`. Otherwise, if :meth:`~generator.send` is used, then +the result will be the value passed in to that method. .. index:: single: coroutine @@ -509,8 +508,8 @@ on the right hand side of an assignment statement. usable as simple coroutines. :pep:`380` - Syntax for Delegating to a Subgenerator - The proposal to introduce the :token:`yield_from` syntax, making delegation - to subgenerators easy. + The proposal to introduce the :token:`~python-grammar:yield_from` syntax, + making delegation to subgenerators easy. :pep:`525` - Asynchronous Generators The proposal that expanded on :pep:`492` by adding generator capabilities to @@ -538,9 +537,9 @@ is already executing raises a :exc:`ValueError` exception. :meth:`~generator.__next__` method, the current yield expression always evaluates to :const:`None`. The execution then continues to the next yield expression, where the generator is suspended again, and the value of the - :token:`expression_list` is returned to :meth:`__next__`'s caller. If the - generator exits without yielding another value, a :exc:`StopIteration` - exception is raised. + :token:`~python-grammar:expression_list` is returned to :meth:`__next__`'s + caller. If the generator exits without yielding another value, a + :exc:`StopIteration` exception is raised. This method is normally called implicitly, e.g. by a :keyword:`for` loop, or by the built-in :func:`next` function. @@ -557,14 +556,27 @@ is already executing raises a :exc:`ValueError` exception. could receive the value. -.. method:: generator.throw(type[, value[, traceback]]) +.. method:: generator.throw(value) + generator.throw(type[, value[, traceback]]) - Raises an exception of type ``type`` at the point where the generator was paused, + Raises an exception at the point where the generator was paused, and returns the next value yielded by the generator function. If the generator exits without yielding another value, a :exc:`StopIteration` exception is raised. If the generator function does not catch the passed-in exception, or raises a different exception, then that exception propagates to the caller. + In typical use, this is called with a single exception instance similar to the + way the :keyword:`raise` keyword is used. + + For backwards compatability, however, the second signature is + supported, following a convention from older versions of Python. + The *type* argument should be an exception class, and *value* + should be an exception instance. If the *value* is not provided, the + *type* constructor is called to get an instance. If *traceback* + is provided, it is set on the exception, otherwise any existing + :attr:`~BaseException.__traceback__` attribute stored in *value* may + be cleared. + .. index:: exception: GeneratorExit @@ -629,21 +641,20 @@ An asynchronous generator object is typically used in an :keyword:`async for` statement in a coroutine function analogously to how a generator object would be used in a :keyword:`for` statement. -Calling one of the asynchronous generator's methods returns an -:term:`awaitable` object, and the execution starts when this object -is awaited on. At that time, the execution proceeds to the first yield -expression, where it is suspended again, returning the value of -:token:`expression_list` to the awaiting coroutine. As with a generator, -suspension means that all local state is retained, including the -current bindings of local variables, the instruction pointer, the internal -evaluation stack, and the state of any exception handling. When the execution -is resumed by awaiting on the next object returned by the asynchronous -generator's methods, the function can proceed exactly as if the yield -expression were just another external call. The value of the yield expression -after resuming depends on the method which resumed the execution. If +Calling one of the asynchronous generator's methods returns an :term:`awaitable` +object, and the execution starts when this object is awaited on. At that time, +the execution proceeds to the first yield expression, where it is suspended +again, returning the value of :token:`~python-grammar:expression_list` to the +awaiting coroutine. As with a generator, suspension means that all local state +is retained, including the current bindings of local variables, the instruction +pointer, the internal evaluation stack, and the state of any exception handling. +When the execution is resumed by awaiting on the next object returned by the +asynchronous generator's methods, the function can proceed exactly as if the +yield expression were just another external call. The value of the yield +expression after resuming depends on the method which resumed the execution. If :meth:`~agen.__anext__` is used then the result is :const:`None`. Otherwise, if -:meth:`~agen.asend` is used, then the result will be the value passed in to -that method. +: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 @@ -695,10 +706,10 @@ which are used to control the execution of a generator function. Returns an awaitable which when run starts to execute the asynchronous generator or resumes it at the last executed yield expression. When an asynchronous generator function is resumed with an :meth:`~agen.__anext__` - method, the current yield expression always evaluates to :const:`None` in - the returned awaitable, which when run will continue to the next yield - expression. The value of the :token:`expression_list` of the yield - expression is the value of the :exc:`StopIteration` exception raised by + method, the current yield expression always evaluates to :const:`None` in the + returned awaitable, which when run will continue to the next yield + expression. The value of the :token:`~python-grammar:expression_list` of the + yield expression is the value of the :exc:`StopIteration` exception raised by the completing coroutine. If the asynchronous generator exits without yielding another value, the awaitable instead raises a :exc:`StopAsyncIteration` exception, signalling that the asynchronous @@ -812,30 +823,44 @@ Subscriptions object: dictionary pair: sequence; item -Subscription of a sequence (string, tuple or list) or mapping (dictionary) -object usually selects an item from the collection: +The subscription of an instance of a :ref:`container class ` +will generally select an element from the container. The subscription of a +:term:`generic class ` will generally return a +:ref:`GenericAlias ` object. .. productionlist:: python-grammar subscription: `primary` "[" `expression_list` "]" -The primary must evaluate to an object that supports subscription (lists or -dictionaries for example). User-defined objects can support subscription by -defining a :meth:`__getitem__` method. +When an object is subscripted, the interpreter will evaluate the primary and +the expression list. + +The primary must evaluate to an object that supports subscription. An object +may support subscription through defining one or both of +:meth:`~object.__getitem__` and :meth:`~object.__class_getitem__`. When the +primary is subscripted, the evaluated result of the expression list will be +passed to one of these methods. For more details on when ``__class_getitem__`` +is called instead of ``__getitem__``, see :ref:`classgetitem-versus-getitem`. -For built-in objects, there are two types of objects that support subscription: +If the expression list contains at least one comma, it will evaluate to a +:class:`tuple` containing the items of the expression list. Otherwise, the +expression list will evaluate to the value of the list's sole member. -If the primary is a mapping, the expression list must evaluate to an object -whose value is one of the keys of the mapping, and the subscription selects the -value in the mapping that corresponds to that key. (The expression list is a -tuple except if it has exactly one item.) +For built-in objects, there are two types of objects that support subscription +via :meth:`~object.__getitem__`: -If the primary is a sequence, the expression list must evaluate to an integer -or a slice (as discussed in the following section). +1. Mappings. If the primary is a :term:`mapping`, the expression list must + evaluate to an object whose value is one of the keys of the mapping, and the + subscription selects the value in the mapping that corresponds to that key. + An example of a builtin mapping class is the :class:`dict` class. +2. Sequences. If the primary is a :term:`sequence`, the expression list must + evaluate to an :class:`int` or a :class:`slice` (as discussed in the + following section). Examples of builtin sequence classes include the + :class:`str`, :class:`list` and :class:`tuple` classes. The formal syntax makes no special provision for negative indices in -sequences; however, built-in sequences all provide a :meth:`__getitem__` +:term:`sequences `. However, built-in sequences all provide a :meth:`~object.__getitem__` method that interprets negative indices by adding the length of the sequence -to the index (so that ``x[-1]`` selects the last item of ``x``). The +to the index so that, for example, ``x[-1]`` selects the last item of ``x``. The resulting value must be a nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is that value (counting from zero). Since the support for negative indices and slicing @@ -846,14 +871,10 @@ this method will need to explicitly add that support. single: character pair: string; item -A string's items are characters. A character is not a separate data type but a +A :class:`string ` is a special kind of sequence whose items are +*characters*. A character is not a separate data type but a string of exactly one character. -Subscription of certain :term:`classes ` or :term:`types ` -creates a :ref:`generic alias `. -In this case, user-defined classes can support subscription by providing a -:meth:`__class_getitem__` classmethod. - .. _slicings: @@ -1148,6 +1169,7 @@ Raising ``0.0`` to a negative power results in a :exc:`ZeroDivisionError`. Raising a negative number to a fractional power results in a :class:`complex` number. (In earlier versions it raised a :exc:`ValueError`.) +This operation can be customized using the special :meth:`__pow__` method. .. _unary: @@ -1169,14 +1191,16 @@ All unary arithmetic and bitwise operations have the same priority: single: operator; - (minus) single: - (minus); unary operator -The unary ``-`` (minus) operator yields the negation of its numeric argument. +The unary ``-`` (minus) operator yields the negation of its numeric argument; the +operation can be overridden with the :meth:`__neg__` special method. .. index:: single: plus single: operator; + (plus) single: + (plus); unary operator -The unary ``+`` (plus) operator yields its numeric argument unchanged. +The unary ``+`` (plus) operator yields its numeric argument unchanged; the +operation can be overridden with the :meth:`__pos__` special method. .. index:: single: inversion @@ -1184,7 +1208,10 @@ The unary ``+`` (plus) operator yields its numeric argument unchanged. The unary ``~`` (invert) operator yields the bitwise inversion of its integer argument. The bitwise inversion of ``x`` is defined as ``-(x+1)``. It only -applies to integral numbers. +applies to integral numbers or to custom objects that override the +:meth:`__invert__` special method. + + .. index:: exception: TypeError @@ -1220,6 +1247,9 @@ the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; a negative repetition factor yields an empty sequence. +This operation can be customized using the special :meth:`__mul__` and +:meth:`__rmul__` methods. + .. index:: single: matrix multiplication operator: @ (at) @@ -1242,6 +1272,9 @@ integer; the result is that of mathematical division with the 'floor' function applied to the result. Division by zero raises the :exc:`ZeroDivisionError` exception. +This operation can be customized using the special :meth:`__truediv__` and +:meth:`__floordiv__` methods. + .. index:: single: modulo operator: % (percent) @@ -1265,6 +1298,8 @@ also overloaded by string objects to perform old-style string formatting (also known as interpolation). The syntax for string formatting is described in the Python Library Reference, section :ref:`old-string-formatting`. +The *modulo* operation can be customized using the special :meth:`__mod__` method. + The floor division operator, the modulo operator, and the :func:`divmod` function are not defined for complex numbers. Instead, convert to a floating point number using the :func:`abs` function if appropriate. @@ -1279,6 +1314,9 @@ must either both be numbers or both be sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated. +This operation can be customized using the special :meth:`__add__` and +:meth:`__radd__` methods. + .. index:: single: subtraction single: operator; - (minus) @@ -1287,6 +1325,8 @@ In the latter case, the sequences are concatenated. The ``-`` (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. +This operation can be customized using the special :meth:`__sub__` method. + .. _shifting: @@ -1306,6 +1346,9 @@ The shifting operations have lower priority than the arithmetic operations: These operators accept integers as arguments. They shift the first argument to the left or right by the number of bits given by the second argument. +This operation can be customized using the special :meth:`__lshift__` and +:meth:`__rshift__` methods. + .. index:: exception: ValueError A right shift by *n* bits is defined as floor division by ``pow(2,n)``. A left @@ -1331,7 +1374,8 @@ Each of the three bitwise operations has a different priority level: operator: & (ampersand) The ``&`` operator yields the bitwise AND of its arguments, which must be -integers. +integers or one of them must be a custom object overriding :meth:`__and__` or +:meth:`__rand__` special methods. .. index:: pair: bitwise; xor @@ -1339,7 +1383,8 @@ integers. operator: ^ (caret) The ``^`` operator yields the bitwise XOR (exclusive OR) of its arguments, which -must be integers. +must be integers or one of them must be a custom object overriding :meth:`__xor__` or +:meth:`__rxor__` special methods. .. index:: pair: bitwise; or @@ -1347,7 +1392,8 @@ must be integers. operator: | (vertical bar) The ``|`` operator yields the bitwise (inclusive) OR of its arguments, which -must be integers. +must be integers or one of them must be a custom object overriding :meth:`__or__` or +:meth:`__ror__` special methods. .. _comparisons: @@ -1375,7 +1421,9 @@ in mathematics: comp_operator: "<" | ">" | "==" | ">=" | "<=" | "!=" : | "is" ["not"] | ["not"] "in" -Comparisons yield boolean values: ``True`` or ``False``. +Comparisons yield boolean values: ``True`` or ``False``. Custom +:dfn:`rich comparison methods` may return non-boolean values. In this case +Python will call :func:`bool` on such value in boolean contexts. .. index:: pair: chaining; comparisons @@ -1672,8 +1720,9 @@ Assignment expressions assignment_expression: [`identifier` ":="] `expression` An assignment expression (sometimes also called a "named expression" or -"walrus") assigns an :token:`expression` to an :token:`identifier`, while also -returning the value of the :token:`expression`. +"walrus") assigns an :token:`~python-grammar:expression` to an +:token:`~python-grammar:identifier`, while also returning the value of the +:token:`~python-grammar:expression`. One common use case is when handling matched regular expressions: @@ -1842,7 +1891,7 @@ precedence and have a left-to-right chaining feature as described in the | ``x[index]``, ``x[index:index]``, | Subscription, slicing, | | ``x(arguments...)``, ``x.attribute`` | call, attribute reference | +-----------------------------------------------+-------------------------------------+ -| :keyword:`await` ``x`` | Await expression | +| :keyword:`await x ` | Await expression | +-----------------------------------------------+-------------------------------------+ | ``**`` | Exponentiation [#]_ | +-----------------------------------------------+-------------------------------------+ @@ -1866,7 +1915,7 @@ precedence and have a left-to-right chaining feature as described in the | :keyword:`is`, :keyword:`is not`, ``<``, | tests and identity tests | | ``<=``, ``>``, ``>=``, ``!=``, ``==`` | | +-----------------------------------------------+-------------------------------------+ -| :keyword:`not` ``x`` | Boolean NOT | +| :keyword:`not x ` | Boolean NOT | +-----------------------------------------------+-------------------------------------+ | :keyword:`and` | Boolean AND | +-----------------------------------------------+-------------------------------------+ diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst index 2ef5b901b930bf..9c3c86649f555b 100644 --- a/Doc/reference/import.rst +++ b/Doc/reference/import.rst @@ -84,9 +84,9 @@ considered a package. All modules have a name. Subpackage names are separated from their parent package name by a dot, akin to Python's standard attribute access syntax. Thus -you might have a module called :mod:`sys` and a package called :mod:`email`, -which in turn has a subpackage called :mod:`email.mime` and a module within -that subpackage called :mod:`email.mime.text`. +you might have a package called :mod:`email`, which in turn has a subpackage +called :mod:`email.mime` and a module within that subpackage called +:mod:`email.mime.text`. Regular packages @@ -490,21 +490,19 @@ submodule. Let's say you have the following directory structure:: spam/ __init__.py foo.py - bar.py -and ``spam/__init__.py`` has the following lines in it:: +and ``spam/__init__.py`` has the following line in it:: from .foo import Foo - from .bar import Bar -then executing the following puts a name binding to ``foo`` and ``bar`` in the +then executing the following puts name bindings for ``foo`` and ``Foo`` in the ``spam`` module:: >>> import spam >>> spam.foo - >>> spam.bar - + >>> spam.Foo + Given Python's familiar name binding rules this might seem surprising, but it's actually a fundamental feature of the import system. The invariant @@ -970,6 +968,8 @@ should expose ``XXX.YYY.ZZZ`` as a usable expression, but .moduleY is not a valid expression. +.. _import-dunder-main: + Special considerations for __main__ =================================== diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst index 4ad8f8be1e7ddf..0dad31fd370e25 100644 --- a/Doc/reference/lexical_analysis.rst +++ b/Doc/reference/lexical_analysis.rst @@ -101,12 +101,11 @@ addition, if the first bytes of the file are the UTF-8 byte-order mark (``b'\xef\xbb\xbf'``), the declared file encoding is UTF-8 (this is supported, among others, by Microsoft's :program:`notepad`). -If an encoding is declared, the encoding name must be recognized by Python. The +If an encoding is declared, the encoding name must be recognized by Python +(see :ref:`standard-encodings`). The encoding is used for all lexical analysis, including string literals, comments and identifiers. -.. XXX there should be a list of supported encodings. - .. _explicit-joining: @@ -385,10 +384,20 @@ classes are identified by the patterns of leading and trailing underscore characters: ``_*`` - Not imported by ``from module import *``. The special identifier ``_`` is used - in the interactive interpreter to store the result of the last evaluation; it is - stored in the :mod:`builtins` module. When not in interactive mode, ``_`` - has no special meaning and is not defined. See section :ref:`import`. + Not imported by ``from module import *``. + +``_`` + In a ``case`` pattern within a :keyword:`match` statement, ``_`` is a + :ref:`soft keyword ` that denotes a + :ref:`wildcard `. + + Separately, the interactive interpreter makes the result of the last evaluation + available in the variable ``_``. + (It is stored in the :mod:`builtins` module, alongside built-in + functions like ``print``.) + + Elsewhere, ``_`` is a regular identifier. It is often used to name + "special" items, but it is not special to Python itself. .. note:: @@ -396,6 +405,8 @@ characters: refer to the documentation for the :mod:`gettext` module for more information on this convention. + It is also commonly used for unused variables. + ``__*__`` System-defined names, informally known as "dunder" names. These names are defined by the interpreter and its implementation (including the standard library). @@ -457,10 +468,10 @@ String literals are described by the following lexical definitions: bytesescapeseq: "\" One syntactic restriction not indicated by these productions is that whitespace -is not allowed between the :token:`stringprefix` or :token:`bytesprefix` and the -rest of the literal. The source character set is defined by the encoding -declaration; it is UTF-8 if no encoding declaration is given in the source file; -see section :ref:`encodings`. +is not allowed between the :token:`~python-grammar:stringprefix` or +:token:`~python-grammar:bytesprefix` and the rest of the literal. The source +character set is defined by the encoding declaration; it is UTF-8 if no encoding +declaration is given in the source file; see section :ref:`encodings`. .. index:: triple-quoted string, Unicode Consortium, raw string single: """; string literal @@ -469,9 +480,11 @@ see section :ref:`encodings`. In plain English: Both types of literals can be enclosed in matching single quotes (``'``) or double quotes (``"``). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as -*triple-quoted strings*). The backslash (``\``) character is used to escape -characters that otherwise have a special meaning, such as newline, backslash -itself, or the quote character. +*triple-quoted strings*). The backslash (``\``) character is used to give special +meaning to otherwise ordinary characters like ``n``, which means 'newline' when +escaped (``\n``). It can also be used to escape characters that otherwise have a +special meaning, such as newline, backslash itself, or the quote character. +See :ref:`escape sequences ` below for examples. .. index:: single: b'; bytes literal @@ -530,6 +543,8 @@ retained), except that three unescaped quotes in a row terminate the literal. ( single: \u; escape sequence single: \U; escape sequence +.. _escape-sequences: + Unless an ``'r'`` or ``'R'`` prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are: diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst index 3aba2700048bc0..d5f1e045e980cb 100644 --- a/Doc/reference/simple_stmts.rst +++ b/Doc/reference/simple_stmts.rst @@ -124,9 +124,7 @@ square brackets, is recursively defined as follows. * If the target list is a single target with no trailing comma, optionally in parentheses, the object is assigned to that target. -* Else: The object must be an iterable with the same number of - items as there are targets in the target list, and the items are assigned, - from left to right, to the corresponding targets. +* Else: * If the target list contains one target prefixed with an asterisk, called a "starred" target: The object must be an iterable with at least as many items @@ -563,10 +561,10 @@ The :keyword:`!raise` statement .. productionlist:: python-grammar raise_stmt: "raise" [`expression` ["from" `expression`]] -If no expressions are present, :keyword:`raise` re-raises the last exception -that was active in the current scope. If no exception is active in the current -scope, a :exc:`RuntimeError` exception is raised indicating that this is an -error. +If no expressions are present, :keyword:`raise` re-raises the +exception that is currently being handled, which is also known as the *active exception*. +If there isn't currently an active exception, a :exc:`RuntimeError` exception is raised +indicating that this is an error. Otherwise, :keyword:`raise` evaluates the first expression as the exception object. It must be either a subclass or an instance of :class:`BaseException`. @@ -581,8 +579,8 @@ The :dfn:`type` of the exception is the exception instance's class, the A traceback object is normally created automatically when an exception is raised and attached to it as the :attr:`__traceback__` attribute, which is writable. You can create an exception and set your own traceback in one step using the -:meth:`with_traceback` exception method (which returns the same exception -instance, with its traceback set to its argument), like so:: +:meth:`~BaseException.with_traceback` exception method (which returns the +same exception instance, with its traceback set to its argument), like so:: raise Exception("foo occurred").with_traceback(tracebackobj) @@ -614,8 +612,10 @@ exceptions will be printed:: File "", line 4, in RuntimeError: Something bad happened -A similar mechanism works implicitly if an exception is raised inside an -exception handler or a :keyword:`finally` clause: the previous exception is then +A similar mechanism works implicitly if a new exception is raised when +an exception is already being handled. An exception may be handled +when an :keyword:`except` or :keyword:`finally` clause, or a +:keyword:`with` statement, is used. The previous exception is then attached as the new exception's :attr:`__context__` attribute:: >>> try: @@ -734,7 +734,7 @@ The :keyword:`!import` statement : ("," `identifier` ["as" `identifier`])* : | "from" `relative_module` "import" "(" `identifier` ["as" `identifier`] : ("," `identifier` ["as" `identifier`])* [","] ")" - : | "from" `module` "import" "*" + : | "from" `relative_module` "import" "*" module: (`identifier` ".")* `identifier` relative_module: "."* `module` | "."+ @@ -795,9 +795,9 @@ The :keyword:`from` form uses a slightly more complex process: Examples:: import foo # foo imported and bound locally - import foo.bar.baz # foo.bar.baz imported, foo bound locally - import foo.bar.baz as fbb # foo.bar.baz imported and bound as fbb - from foo.bar import baz # foo.bar.baz imported and bound as baz + import foo.bar.baz # foo, foo.bar, and foo.bar.baz imported, foo bound locally + import foo.bar.baz as fbb # foo, foo.bar, and foo.bar.baz imported, foo.bar.baz bound as fbb + from foo.bar import baz # foo, foo.bar, and foo.bar.baz imported, foo.bar.baz bound as baz from foo import attr # foo imported and foo.attr bound as attr .. index:: single: * (asterisk); import statement diff --git a/Doc/requirements.txt b/Doc/requirements.txt index 2b70af3a4fc6b9..f43ce2c6e4efd8 100644 --- a/Doc/requirements.txt +++ b/Doc/requirements.txt @@ -3,10 +3,16 @@ # Sphinx version is pinned so that new versions that introduce new warnings # won't suddenly cause build failures. Updating the version is fine as long # as no warnings are raised by doing so. -sphinx==3.2.1 +sphinx==3.4.3 +# Docutils version is pinned to a version compatible with Sphinx +# version <3.5.4. It can be removed after bumping Sphinx version to at +# least 3.5.4. +docutils==0.17.1 +# Jinja version is pinned to a version compatible with Sphinx version <4.5. +jinja2==3.0.3 blurb # The theme used by the documentation is stored separately, so we need # to install that as well. -python-docs-theme +python-docs-theme>=2022.1 diff --git a/Doc/tools/extensions/c_annotations.py b/Doc/tools/extensions/c_annotations.py index 76c9d920cbe31f..489f06613eb310 100644 --- a/Doc/tools/extensions/c_annotations.py +++ b/Doc/tools/extensions/c_annotations.py @@ -10,8 +10,10 @@ * stable API annotations - Usage: Set the `refcount_file` config value to the path to the reference + Usage: + * Set the `refcount_file` config value to the path to the reference count data file. + * Set the `stable_abi_file` config value to the path to stable ABI list. :copyright: Copyright 2007-2014 by Georg Brandl. :license: Python license. @@ -20,11 +22,23 @@ from os import path from docutils import nodes from docutils.parsers.rst import directives +from docutils.parsers.rst import Directive +from docutils.statemachine import StringList +import csv from sphinx import addnodes from sphinx.domains.c import CObject +REST_ROLE_MAP = { + 'function': 'func', + 'var': 'data', + 'type': 'type', + 'macro': 'macro', + 'type': 'type', +} + + class RCEntry: def __init__(self, name): self.name = name @@ -33,12 +47,10 @@ def __init__(self, name): self.result_refs = None -class Annotations(dict): - @classmethod - def fromfile(cls, filename): - d = cls() - fp = open(filename, 'r') - try: +class Annotations: + def __init__(self, refcount_filename, stable_abi_file): + self.refcount_data = {} + with open(refcount_filename, 'r') as fp: for line in fp: line = line.strip() if line[:1] in ("", "#"): @@ -50,9 +62,9 @@ def fromfile(cls, filename): function, type, arg, refcount, comment = parts # Get the entry, creating it if needed: try: - entry = d[function] + entry = self.refcount_data[function] except KeyError: - entry = d[function] = RCEntry(function) + entry = self.refcount_data[function] = RCEntry(function) if not refcount or refcount == "null": refcount = None else: @@ -64,27 +76,58 @@ def fromfile(cls, filename): else: entry.result_type = type entry.result_refs = refcount - finally: - fp.close() - return d + + self.stable_abi_data = {} + with open(stable_abi_file, 'r') as fp: + for record in csv.DictReader(fp): + role = record['role'] + name = record['name'] + self.stable_abi_data[name] = record def add_annotations(self, app, doctree): for node in doctree.traverse(addnodes.desc_content): par = node.parent if par['domain'] != 'c': continue - if par['stableabi']: - node.insert(0, nodes.emphasis(' Part of the stable ABI.', - ' Part of the stable ABI.', - classes=['stableabi'])) - if par['objtype'] != 'function': - continue if not par[0].has_key('ids') or not par[0]['ids']: continue name = par[0]['ids'][0] if name.startswith("c."): name = name[2:] - entry = self.get(name) + + objtype = par['objtype'] + + # Stable ABI annotation. These have two forms: + # Part of the [Stable ABI](link). + # Part of the [Stable ABI](link) since version X.Y. + record = self.stable_abi_data.get(name) + if record: + if record['role'] != objtype: + raise ValueError( + f"Object type mismatch in limited API annotation " + f"for {name}: {record['role']!r} != {objtype!r}") + stable_added = record['added'] + message = ' Part of the ' + emph_node = nodes.emphasis(message, message, + classes=['stableabi']) + ref_node = addnodes.pending_xref( + 'Stable ABI', refdomain="std", reftarget='stable', + reftype='ref', refexplicit="False") + ref_node += nodes.Text('Stable ABI') + emph_node += ref_node + if record['ifdef_note']: + emph_node += nodes.Text(' ' + record['ifdef_note']) + if stable_added == '3.2': + # Stable ABI was introduced in 3.2. + emph_node += nodes.Text('.') + else: + emph_node += nodes.Text(f' since version {stable_added}.') + node.insert(0, emph_node) + + # Return value annotation + if objtype != 'function': + continue + entry = self.refcount_data.get(name) if not entry: continue elif not entry.result_type.endswith("Object*"): @@ -99,13 +142,36 @@ def add_annotations(self, app, doctree): def init_annotations(app): - refcounts = Annotations.fromfile( - path.join(app.srcdir, app.config.refcount_file)) - app.connect('doctree-read', refcounts.add_annotations) + annotations = Annotations( + path.join(app.srcdir, app.config.refcount_file), + path.join(app.srcdir, app.config.stable_abi_file), + ) + app.connect('doctree-read', annotations.add_annotations) + + class LimitedAPIList(Directive): + + has_content = False + required_arguments = 0 + optional_arguments = 0 + final_argument_whitespace = True + + def run(self): + content = [] + for record in annotations.stable_abi_data.values(): + role = REST_ROLE_MAP[record['role']] + name = record['name'] + content.append(f'* :c:{role}:`{name}`') + + pnode = nodes.paragraph() + self.state.nested_parse(StringList(content), 0, pnode) + return [pnode] + + app.add_directive('limited-api-list', LimitedAPIList) def setup(app): app.add_config_value('refcount_file', '', True) + app.add_config_value('stable_abi_file', '', True) app.connect('builder-inited', init_annotations) # monkey-patch C object... diff --git a/Doc/tools/extensions/escape4chm.py b/Doc/tools/extensions/escape4chm.py index e9999716251734..89970975b9032b 100644 --- a/Doc/tools/extensions/escape4chm.py +++ b/Doc/tools/extensions/escape4chm.py @@ -5,6 +5,7 @@ https://bugs.python.org/issue32174 """ +import pathlib import re from html.entities import codepoint2name @@ -39,12 +40,12 @@ def fixup_keywords(app, exception): return getLogger(__name__).info('fixing HTML escapes in keywords file...') - outdir = app.builder.outdir + outdir = pathlib.Path(app.builder.outdir) outname = app.builder.config.htmlhelp_basename - with app.builder.open_file(outdir, outname + '.hhk', 'r') as f: + with open(outdir / (outname + '.hhk'), 'rb') as f: index = f.read() - with app.builder.open_file(outdir, outname + '.hhk', 'w') as f: - f.write(index.replace(''', ''')) + with open(outdir / (outname + '.hhk'), 'wb') as f: + f.write(index.replace(b''', b''')) def setup(app): # `html-page-context` event emitted when the HTML builder has diff --git a/Doc/tools/extensions/peg_highlight.py b/Doc/tools/extensions/peg_highlight.py index 42101be10ea9b6..27f54cdf593c87 100644 --- a/Doc/tools/extensions/peg_highlight.py +++ b/Doc/tools/extensions/peg_highlight.py @@ -56,8 +56,8 @@ class PEGLexer(RegexLexer): (_name + _text_ws + r"(\[[\w\d_\*]+?\])" + _text_ws + "(=)", bygroups(None, None, None, None, None),), ], "invalids": [ - (r"^(\s+\|\s+invalid_\w+\s*\n)", bygroups(None)), - (r"^(\s+\|\s+incorrect_\w+\s*\n)", bygroups(None)), + (r"^(\s+\|\s+.*invalid_\w+.*\n)", bygroups(None)), + (r"^(\s+\|\s+.*incorrect_\w+.*\n)", bygroups(None)), (r"^(#.*invalid syntax.*(?:.|\n)*)", bygroups(None),), ], "root": [ diff --git a/Doc/tools/extensions/pyspecific.py b/Doc/tools/extensions/pyspecific.py index bcd9d26b36846f..27004afc0bab99 100644 --- a/Doc/tools/extensions/pyspecific.py +++ b/Doc/tools/extensions/pyspecific.py @@ -43,8 +43,9 @@ import suspicious -ISSUE_URI = 'https://bugs.python.org/issue%s' -SOURCE_URI = 'https://github.com/python/cpython/tree/master/%s' +ISSUE_URI = 'https://bugs.python.org/issue?@action=redirect&bpo=%s' +GH_ISSUE_URI = 'https://github.com/python/cpython/issues/%s' +SOURCE_URI = 'https://github.com/python/cpython/tree/3.10/%s' # monkey-patch reST parser to disable alphabetic and roman enumerated lists from docutils.parsers.rst.states import Body @@ -58,11 +59,33 @@ def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): issue = utils.unescape(text) + # sanity check: there are no bpo issues within these two values + if 47261 < int(issue) < 400000: + msg = inliner.reporter.error(f'The BPO ID {text!r} seems too high -- ' + 'use :gh:`...` for GitHub IDs', line=lineno) + prb = inliner.problematic(rawtext, rawtext, msg) + return [prb], [msg] text = 'bpo-' + issue refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue) return [refnode], [] +# Support for marking up and linking to GitHub issues + +def gh_issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): + issue = utils.unescape(text) + # sanity check: all GitHub issues have ID >= 32426 + # even though some of them are also valid BPO IDs + if int(issue) < 32426: + msg = inliner.reporter.error(f'The GitHub ID {text!r} seems too low -- ' + 'use :issue:`...` for BPO IDs', line=lineno) + prb = inliner.problematic(rawtext, rawtext, msg) + return [prb], [msg] + text = 'gh-' + issue + refnode = nodes.reference(text, text, refuri=GH_ISSUE_URI % issue) + return [refnode], [] + + # Support for linking to Python source files easily def source_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): @@ -406,7 +429,8 @@ def run(self): # Support for including Misc/NEWS -issue_re = re.compile('(?:[Ii]ssue #|bpo-)([0-9]+)') +issue_re = re.compile('(?:[Ii]ssue #|bpo-)([0-9]+)', re.I) +gh_issue_re = re.compile('(?:gh-issue-|gh-)([0-9]+)', re.I) whatsnew_re = re.compile(r"(?im)^what's new in (.*?)\??$") @@ -433,8 +457,9 @@ def run(self): text = 'The NEWS file is not available.' node = nodes.strong(text, text) return [node] - content = issue_re.sub(r'`bpo-\1 `__', - content) + content = issue_re.sub(r':issue:`\1`', content) + # Fallback handling for the GitHub issue + content = gh_issue_re.sub(r':gh:`\1`', content) content = whatsnew_re.sub(r'\1', content) # remove first 3 lines as they are the main heading lines = ['.. default-role:: obj', ''] + content.splitlines()[3:] @@ -614,6 +639,7 @@ def process_audit_events(app, doctree, fromdocname): def setup(app): app.add_role('issue', issue_role) + app.add_role('gh', gh_issue_role) app.add_role('source', source_role) app.add_directive('impl-detail', ImplementationDetail) app.add_directive('availability', Availability) diff --git a/Doc/tools/rstlint.py b/Doc/tools/rstlint.py index cbcb8eb801b135..3092a3b2d81b78 100755 --- a/Doc/tools/rstlint.py +++ b/Doc/tools/rstlint.py @@ -165,7 +165,7 @@ def hide_comments(lines): """Tool to remove comments from given lines. It yields empty lines in place of comments, so line numbers are - still meaningfull. + still meaningful. """ in_multiline_comment = False for line in lines: diff --git a/Doc/tools/susp-ignored.csv b/Doc/tools/susp-ignored.csv index d56a2b9fd0bfb9..7167f059333e93 100644 --- a/Doc/tools/susp-ignored.csv +++ b/Doc/tools/susp-ignored.csv @@ -110,6 +110,7 @@ howto/pyporting,,::,Programming Language :: Python :: 3 howto/regex,,::, howto/regex,,:foo,(?:foo) howto/urllib2,,:password,"""joe:password@example.com""" +library/__main__,,`, library/ast,,:upper,lower:upper library/ast,,:step,lower:upper:step library/audioop,,:ipos,"# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)]," @@ -170,6 +171,8 @@ library/ipaddress,,::,2001:db00::0/ffff:ff00:: library/itertools,,:step,elements from seq[start:stop:step] library/itertools,,::,kernel = tuple(kernel)[::-1] library/itertools,,:stop,elements from seq[start:stop:step] +library/logging,,:root,WARNING:root:Watch out! +library/logging,,:Watch,WARNING:root:Watch out! library/logging.handlers,,:port,host:port library/mmap,,:i2,obj[i1:i2] library/multiprocessing,,`,# Add more tasks using `put()` @@ -209,10 +212,12 @@ library/smtplib,,:port,method must support that as well as a regular host:port library/socket,,::,'5aef:2b::8' library/socket,,:can,"return (can_id, can_dlc, data[:can_dlc])" library/socket,,:len,fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) -library/sqlite3,,:name,"cur.execute(""select * from lang where lang_name=:name and lang_age=:age""," -library/sqlite3,,:age,"cur.execute(""select * from lang where lang_name=:name and lang_age=:age""," +library/sqlite3,,:year,"cur.execute(""select * from lang where first_appeared=:year"", {""year"": 1972})" library/sqlite3,,:memory, -library/sqlite3,,:path,"db = sqlite3.connect('file:path/to/database?mode=ro', uri=True)" +library/sqlite3,,:template,"con = sqlite3.connect(""file:template.db?mode=ro"", uri=True)" +library/sqlite3,,:nosuchdb,"con = sqlite3.connect(""file:nosuchdb.db?mode=rw"", uri=True)" +library/sqlite3,,:mem1,"con1 = sqlite3.connect(""file:mem1?mode=memory&cache=shared"", uri=True)" +library/sqlite3,,:mem1,"con2 = sqlite3.connect(""file:mem1?mode=memory&cache=shared"", uri=True)" library/ssl,,:My,"Organizational Unit Name (eg, section) []:My Group" library/ssl,,:My,"Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc." library/ssl,,:myserver,"Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com" @@ -234,6 +239,12 @@ library/tarfile,,:xz,'r:xz' library/tarfile,,:xz,'w:xz' library/time,,:mm, library/time,,:ss, +library/tkinter,,::,ttk::frame .frm -padding 10 +library/tkinter,,::,"grid [ttk::label .frm.lbl -text ""Hello World!""] -column 0 -row 0" +library/tkinter,,::,"grid [ttk::button .frm.btn -text ""Quit"" -command ""destroy .""] -column 1 -row 0" +library/tkinter,,::,ttk::frame +library/tkinter,,::,ttk::button +library/tkinter,,::,ttk::widget library/tracemalloc,,:limit,"for index, stat in enumerate(top_stats[:limit], 1):" library/turtle,,::,Example:: library/unittest,,:foo,"self.assertEqual(cm.output, ['INFO:foo:first message'," @@ -265,6 +276,7 @@ license,,:zooko,mailto:zooko@zooko.com reference/expressions,,:index,x[index:index] reference/lexical_analysis,,`,$ ? ` reference/lexical_analysis,,:fileencoding,# vim:fileencoding= +reference/datamodel,,`, """Return the result of the expression `obj[x]`""" tutorial/datastructures,,:value,It is also possible to delete a key:value tutorial/datastructures,,:value,key:value pairs within the braces adds initial key:value pairs tutorial/stdlib2,,:config,"logging.warning('Warning:config file %s not found', 'server.conf')" @@ -370,7 +382,8 @@ library/importlib.metadata,,:main,"EntryPoint(name='wheel', value='wheel.cli:mai library/importlib.metadata,,`,loading the metadata for packages for the indicated ``context``. library/re,,`,"`" using/configure,84,:db2,=db1:db2:... -library/typing,1004,`,# Type of ``val`` is narrowed to ``str`` -library/typing,1004,`,"# Else, type of ``val`` is narrowed to ``float``." -library/typing,1004,`,# Type of ``val`` is narrowed to ``List[str]``. -library/typing,1004,`,# Type of ``val`` remains as ``List[object]``. +library/typing,,`,# Type of ``val`` is narrowed to ``str`` +library/typing,,`,"# Else, type of ``val`` is narrowed to ``float``." +library/typing,,`,# Type of ``val`` is narrowed to ``List[str]``. +library/typing,,`,# Type of ``val`` remains as ``List[object]``. +library/typing,,`, # will always be an instance of whatever ``cls`` is diff --git a/Doc/tools/templates/customsourcelink.html b/Doc/tools/templates/customsourcelink.html index fca44e9163cac7..21af621efce95b 100644 --- a/Doc/tools/templates/customsourcelink.html +++ b/Doc/tools/templates/customsourcelink.html @@ -4,7 +4,7 @@

{{ _('This Page') }}