diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index d6d06555dd26d3..a95eb943e4a4e7 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -498,6 +498,11 @@ Lib/_markupbase.py @ezio-melotti Lib/test/test_html*.py @ezio-melotti Tools/build/parse_html5_entities.py @ezio-melotti +# HTTP +Doc/library/http* @orsenthil +Lib/http/ @orsenthil +Lib/test/test_http*.py @orsenthil + # IDLE Doc/library/idle.rst @terryjreedy Lib/idlelib/ @terryjreedy @@ -633,6 +638,11 @@ Modules/_typesmodule.c @AA-Turner Lib/unittest/mock.py @cjw296 Lib/test/test_unittest/testmock/ @cjw296 +# Urllib +Doc/library/urllib* @orsenthil +Lib/urllib/ @orsenthil +Lib/test/test_urllib*.py @orsenthil + # Venv **/*venv* @vsajip @FFY00 diff --git a/.github/ISSUE_TEMPLATE/documentation.yml b/.github/ISSUE_TEMPLATE/documentation.yml index d720cf9c4de91e..d21e2a88604087 100644 --- a/.github/ISSUE_TEMPLATE/documentation.yml +++ b/.github/ISSUE_TEMPLATE/documentation.yml @@ -7,10 +7,16 @@ body: value: | > [!NOTE] > Trivial changes (for example typos) don’t require an issue before opening a PR. + - type: input + id: url + attributes: + label: "Documentation URL" + description: "A link to the relevant documentation page, if applicable." + placeholder: "https://docs.python.org/3/..." - type: textarea id: description attributes: label: "Documentation" - description: "A clear and concise description of the issue. Include a link to the page." + description: "A clear and concise description of the issue." validations: required: true diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml index 61ee08db9919bb..37d9121dadfc8f 100644 --- a/.github/actionlint.yaml +++ b/.github/actionlint.yaml @@ -12,3 +12,6 @@ paths: ignore: - 1st argument of function call is not assignable - SC2(015|038|086|091|097|098|129|155) + # Pending release of actionlint > 1.7.12 for `$/` self-repository syntax: + # https://github.com/rhysd/actionlint/issues/711 + - reusable workflow call "\$/[^"]+" at "uses" is not following the format diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock new file mode 100644 index 00000000000000..9e4ec30ba9d254 --- /dev/null +++ b/.github/workflows/actions.lock @@ -0,0 +1,161 @@ +# This file is machine-generated by `gh actions-lock`. +# Do not edit by hand; run `gh actions-lock` to update. +# Docs: https://gh.io/actions-lockfile +version: 'v0.0.3' +workflows: + '.github/workflows/build.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.0.0' + - 'actions/upload-artifact@v7.0.1' + - 're-actors/alls-green@v1.3.0' + '.github/workflows/cache-cleanup-codeql.yml': [] + '.github/workflows/jit.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.0.0' + '.github/workflows/lint.yml': + - 'actions/checkout@v7.0.1' + - 'j178/prek-action@v3.0.0' + '.github/workflows/mypy.yml': + - 'actions/checkout@v7.0.1' + - 'astral-sh/setup-uv@v10.2.0' + '.github/workflows/new-bugs-announce-notifier.yml': + - 'actions/github-script@v9.0.0' + - 'actions/setup-node@v7.0.0' + '.github/workflows/require-pr-label.yml': + - 'mheap/github-action-required-labels@v5.6.0' + '.github/workflows/reusable-check-c-api-docs.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.0.0' + '.github/workflows/reusable-check-html-ids.yml': + - 'actions/checkout@v7.0.1' + - 'actions/download-artifact@v8.0.1' + - 'actions/setup-python@v7.0.0' + '.github/workflows/reusable-cifuzz.yml': + - 'actions/upload-artifact@v7.0.1' + - 'github/codeql-action@v4.38.2' + - 'google/oss-fuzz@ed23f8af80ff82b25ca67cd9b101e690b8897b3f' + '.github/workflows/reusable-context.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.0.0' + '.github/workflows/reusable-docs.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.0.0' + - 'actions/upload-artifact@v7.0.1' + '.github/workflows/reusable-emscripten.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'actions/setup-node@v7.0.0' + - 'actions/setup-python@v7.0.0' + '.github/workflows/reusable-install.yml': + - 'actions/checkout@v7.0.1' + '.github/workflows/reusable-macos.yml': + - 'actions/checkout@v7.0.1' + '.github/workflows/reusable-san.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'actions/upload-artifact@v7.0.1' + '.github/workflows/reusable-test-lazy-imports-all.yml': + - 'actions/checkout@v7.0.1' + '.github/workflows/reusable-ubuntu.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + '.github/workflows/reusable-wasi.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.0.0' + - 'bytecodealliance/actions@v1.1.3' + - 'bytecodealliance/setup-wasi-sdk-action@v1.0.0' + '.github/workflows/reusable-windows.yml': + - 'actions/checkout@v7.0.1' + '.github/workflows/stale.yml': + - 'actions/stale@v11.0.0' + '.github/workflows/tail-call.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.0.0' + '.github/workflows/verify-ensurepip-wheels.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.0.0' + '.github/workflows/verify-expat.yml': + - 'actions/checkout@v7.0.1' +dependencies: + 'actions/cache@v6.1.0': + ref: 'v6.1.0' + commit: 'sha1-55cc8345863c7cc4c66a329aec7e433d2d1c52a9' + owner_id: 44036562 + repo_id: 215566462 + 'actions/checkout@v7.0.1': + ref: 'v7.0.1' + commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1' + owner_id: 44036562 + repo_id: 197814629 + 'actions/download-artifact@v8.0.1': + ref: 'v8.0.1' + commit: 'sha1-3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c' + owner_id: 44036562 + repo_id: 192626254 + 'actions/github-script@v9.0.0': + ref: 'v9.0.0' + commit: 'sha1-3a2844b7e9c422d3c10d287c895573f7108da1b3' + owner_id: 44036562 + repo_id: 205262760 + 'actions/setup-node@v7.0.0': + ref: 'v7.0.0' + commit: 'sha1-820762786026740c76f36085b0efc47a31fe5020' + owner_id: 44036562 + repo_id: 189476904 + 'actions/setup-python@v7.0.0': + ref: 'v7.0.0' + commit: 'sha1-5fda3b95a4ea91299a34e894583c3862153e4b97' + owner_id: 44036562 + repo_id: 192625525 + 'actions/stale@v11.0.0': + ref: 'v11.0.0' + commit: 'sha1-4391f3da665fdf50b6810c1a66712fb9ba21aa93' + owner_id: 44036562 + repo_id: 200437066 + 'actions/upload-artifact@v7.0.1': + ref: 'v7.0.1' + commit: 'sha1-043fb46d1a93c77aae656e7c1c64a875d1fc6a0a' + owner_id: 44036562 + repo_id: 192625955 + 'astral-sh/setup-uv@v10.2.0': + ref: 'v10.2.0' + commit: 'sha1-c18668ad3cf93ea998bef934396af7bb5c839dc7' + owner_id: 115962839 + repo_id: 846733598 + 'bytecodealliance/actions@v1.1.3': + ref: 'v1.1.3' + commit: 'sha1-9152e710e9f7182e4c29ad218e4f335a7b203613' + owner_id: 54038801 + repo_id: 697422232 + 'bytecodealliance/setup-wasi-sdk-action@v1.0.0': + ref: 'v1.0.0' + commit: 'sha1-77d09f3df1e8da344d8403cf8631af8977938b4c' + owner_id: 54038801 + repo_id: 1113975359 + 'github/codeql-action@v4.38.2': + ref: 'v4.38.2' + commit: 'sha1-2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2' + owner_id: 9919 + repo_id: 259445878 + 'google/oss-fuzz@ed23f8af80ff82b25ca67cd9b101e690b8897b3f': + ref: 'master' + commit: 'sha1-ed23f8af80ff82b25ca67cd9b101e690b8897b3f' + owner_id: 1342004 + repo_id: 63809205 + 'j178/prek-action@v3.0.0': + ref: 'v3.0.0' + commit: 'sha1-4e14d07f9231acabce116ccfca13b13dd9755ece' + owner_id: 10510431 + repo_id: 1052387486 + 'mheap/github-action-required-labels@v5.6.0': + ref: 'v5.6.0' + commit: 'sha1-23e10fde7e062233401931a0eece796cd9bf3177' + owner_id: 59130 + repo_id: 272521528 + 're-actors/alls-green@v1.3.0': + ref: 'v1.3.0' + commit: 'sha1-b5b5b37504aa4183270bd3d855c52a67f212be35' + owner_id: 46427759 + repo_id: 434447646 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 07087b836cb597..97971504cb70c9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -41,13 +41,13 @@ jobs: # || 'falsy-branch' # }} # - uses: ./.github/workflows/reusable-context.yml + uses: $/.github/workflows/reusable-context.yml check-docs: name: Docs needs: build-context if: fromJSON(needs.build-context.outputs.run-docs) - uses: ./.github/workflows/reusable-docs.yml + uses: $/.github/workflows/reusable-docs.yml check-autoconf-regen: name: 'Check if Autoconf files are up to date' @@ -64,7 +64,7 @@ jobs: run: | apt update && apt install git -yq git config --global --add safe.directory "$GITHUB_WORKSPACE" - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: fetch-depth: 1 persist-credentials: false @@ -101,10 +101,10 @@ jobs: needs: build-context if: needs.build-context.outputs.run-tests == 'true' steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@v7.0.0 with: python-version: '3.x' - name: Runner image version @@ -147,7 +147,7 @@ jobs: if: >- needs.build-context.outputs.run-tests == 'true' || needs.build-context.outputs.run-docs == 'true' - uses: ./.github/workflows/reusable-check-c-api-docs.yml + uses: $/.github/workflows/reusable-check-c-api-docs.yml build-windows: name: >- @@ -167,15 +167,13 @@ jobs: - true interpreter: - switch-case + - tail-call exclude: # Skip Win32 on free-threaded builds - { arch: Win32, free-threading: true } - include: - # msvc::musttail is currently only supported on x64, - # and only supported on 3.15+. - - { arch: x64, free-threading: false, interpreter: tail-call } - - { arch: x64, free-threading: true, interpreter: tail-call } - uses: ./.github/workflows/reusable-windows.yml + # Skip Win32 on tail-call interpreter builds + - { arch: Win32, interpreter: tail-call } + uses: $/.github/workflows/reusable-windows.yml with: arch: ${{ matrix.arch }} free-threading: ${{ matrix.free-threading }} @@ -201,7 +199,7 @@ jobs: exclude: - os: macos-26-intel free-threading: true - uses: ./.github/workflows/reusable-macos.yml + uses: $/.github/workflows/reusable-macos.yml with: free-threading: ${{ matrix.free-threading }} os: ${{ matrix.os }} @@ -238,7 +236,7 @@ jobs: bolt: false free-threading: false test-opts: '-u cpu' - uses: ./.github/workflows/reusable-ubuntu.yml + uses: $/.github/workflows/reusable-ubuntu.yml with: bolt-optimizations: ${{ matrix.bolt }} free-threading: ${{ matrix.free-threading }} @@ -249,7 +247,7 @@ jobs: name: 'Ubuntu (installed)' needs: build-context if: needs.build-context.outputs.run-ubuntu == 'true' - uses: ./.github/workflows/reusable-install.yml + uses: $/.github/workflows/reusable-install.yml build-ubuntu-ssltests: name: 'Ubuntu SSL tests' @@ -280,7 +278,7 @@ jobs: SSLLIB_DIR: ${{ github.workspace }}/multissl/${{ matrix.ssllib.name }}/${{ matrix.ssllib.version }} LD_LIBRARY_PATH: ${{ github.workspace }}/multissl/${{ matrix.ssllib.name }}/${{ matrix.ssllib.version }}/lib steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Runner image version @@ -291,7 +289,7 @@ jobs: run: sudo ./.github/workflows/posix-deps-apt.sh - name: 'Restore SSL library build' id: cache-ssl-lib - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ./multissl/${{ matrix.ssllib.name }}/${{ matrix.ssllib.version }} key: ${{ matrix.os }}-multissl-${{ matrix.ssllib.name }}-${{ matrix.ssllib.version }} @@ -339,7 +337,7 @@ jobs: runs-on: ${{ matrix.runs-on }} steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Build and test @@ -360,7 +358,7 @@ jobs: - false - true steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false @@ -373,13 +371,13 @@ jobs: name: 'Emscripten' needs: build-context if: needs.build-context.outputs.run-emscripten == 'true' - uses: ./.github/workflows/reusable-emscripten.yml + uses: $/.github/workflows/reusable-emscripten.yml build-wasi: name: 'WASI' needs: build-context if: needs.build-context.outputs.run-wasi == 'true' - uses: ./.github/workflows/reusable-wasi.yml + uses: $/.github/workflows/reusable-wasi.yml test-hypothesis: name: "Hypothesis tests on Ubuntu" @@ -391,7 +389,7 @@ jobs: OPENSSL_VER: 3.5.9 PYTHONSTRICTEXTENSIONBUILD: 1 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Register gcc problem matcher @@ -405,7 +403,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@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ./multissl/openssl/${{ env.OPENSSL_VER }} key: ${{ runner.os }}-multissl-openssl-${{ env.OPENSSL_VER }} @@ -452,7 +450,7 @@ jobs: ./python -m venv "$VENV_LOC" && "$VENV_PYTHON" -m pip install -r "${GITHUB_WORKSPACE}/Tools/requirements-hypothesis.txt" - name: 'Restore Hypothesis database' id: cache-hypothesis-database - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ${{ env.CPYTHON_BUILDDIR }}/.hypothesis/ key: hypothesis-database-${{ github.head_ref || github.run_id }} @@ -479,7 +477,7 @@ jobs: -x test_subprocess \ -x test_signal \ -x test_sysconfig - - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + - uses: actions/upload-artifact@v7.0.1 if: always() with: name: hypothesis-example-db @@ -489,7 +487,7 @@ jobs: name: 'Lazy imports enabled' needs: build-context if: fromJSON(needs.build-context.outputs.run-tests) - uses: ./.github/workflows/reusable-test-lazy-imports-all.yml + uses: $/.github/workflows/reusable-test-lazy-imports-all.yml build-san: # ${{ '' } is a hack to nest jobs under the same sidebar category. @@ -516,7 +514,7 @@ jobs: - check-name: Memory sanitizer: MSan free-threading: false - uses: ./.github/workflows/reusable-san.yml + uses: $/.github/workflows/reusable-san.yml with: sanitizer: ${{ matrix.sanitizer }} free-threading: ${{ matrix.free-threading }} @@ -528,7 +526,7 @@ jobs: needs: build-context if: needs.build-context.outputs.run-ubuntu == 'true' steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Runner image version @@ -549,9 +547,17 @@ jobs: run: | "$BUILD_DIR/host-python/bin/python3" -m test test_sysconfig test_site test_embed - name: Configure cross build - run: ./configure --prefix="$BUILD_DIR/cross-python" --with-build-python="$BUILD_DIR/host-python/bin/python3" + run: | + # Exercise a native build whose builtin modules differ from the build Python. + echo '_bisect _bisectmodule.c' > Modules/Setup.local + ./configure --prefix="$BUILD_DIR/cross-python" --with-build-python="$BUILD_DIR/host-python/bin/python3" - name: Install cross Python run: make -j8 install + - name: Check native extension modules with a build Python + run: | + "$BUILD_DIR/host-python/bin/python3" -c "import sys; assert '_bisect' not in sys.builtin_module_names" + "$BUILD_DIR/cross-python/bin/python3" -c "import _bisect, sys; assert '_bisect' in sys.builtin_module_names" + PYTHONSTRICTEXTENSIONBUILD=1 make checksharedmods - name: Display build info run: | "$BUILD_DIR/cross-python/bin/python3" -m test.pythoninfo @@ -598,7 +604,7 @@ jobs: && 'no-exclude' || 'python3-libraries' }} - uses: ./.github/workflows/reusable-cifuzz.yml + uses: $/.github/workflows/reusable-cifuzz.yml with: oss-fuzz-project-name: ${{ matrix.oss-fuzz-project-name }} sanitizer: ${{ matrix.sanitizer }} @@ -630,7 +636,7 @@ jobs: steps: - name: Check whether the needed jobs succeeded or failed - uses: re-actors/alls-green@b5b5b37504aa4183270bd3d855c52a67f212be35 + uses: re-actors/alls-green@v1.3.0 with: allowed-failures: >- build-android, diff --git a/.github/workflows/jit.yml b/.github/workflows/jit.yml index 381b00185b1ae8..21ecb2c958c12d 100644 --- a/.github/workflows/jit.yml +++ b/.github/workflows/jit.yml @@ -32,7 +32,7 @@ jobs: runs-on: ubuntu-26.04 timeout-minutes: 60 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Install dependencies @@ -72,10 +72,10 @@ jobs: architecture: ARM64 runner: windows-11-arm steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@v7.0.0 with: python-version: '3.11' # PCbuild downloads LLVM automatically: @@ -98,10 +98,10 @@ jobs: - true - false steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@v7.0.0 with: python-version: '3.11' - name: Install LLVM @@ -141,10 +141,10 @@ jobs: - target: aarch64-unknown-linux-gnu/gcc runner: ubuntu-26.04-arm steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@v7.0.0 with: python-version: '3.11' - name: Install dependencies @@ -182,10 +182,10 @@ jobs: use_clang: true run_tests: false steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@v7.0.0 with: python-version: '3.11' - name: Install dependencies diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index cd413cd4cfbaa0..0b89ebe92d367d 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -19,7 +19,22 @@ jobs: timeout-minutes: 10 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: j178/prek-action@4e14d07f9231acabce116ccfca13b13dd9755ece # v3.0.0 + - uses: j178/prek-action@v3.0.0 + + # Fails when .github/workflows/actions.lock is out of sync with the + # workflows or a locked pin no longer verifies upstream. + lockfile: + runs-on: ubuntu-slim + timeout-minutes: 10 + env: + GH_TOKEN: ${{ github.token }} + + steps: + - uses: actions/checkout@v7.0.1 + with: + persist-credentials: false + - run: gh extension install github/gh-actions-lock --pin v0.4.1 + - run: gh actions-lock --verify diff --git a/.github/workflows/mypy.yml b/.github/workflows/mypy.yml index a8c95627c2417d..6109870724ea59 100644 --- a/.github/workflows/mypy.yml +++ b/.github/workflows/mypy.yml @@ -66,10 +66,10 @@ jobs: "Tools/peg_generator", ] steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0 + - uses: astral-sh/setup-uv@v10.2.0 with: python-version: "3.15" activate-environment: true diff --git a/.github/workflows/new-bugs-announce-notifier.yml b/.github/workflows/new-bugs-announce-notifier.yml index 640474273a9de3..8a1c59f50d8d1d 100644 --- a/.github/workflows/new-bugs-announce-notifier.yml +++ b/.github/workflows/new-bugs-announce-notifier.yml @@ -15,12 +15,14 @@ jobs: issues: read timeout-minutes: 10 steps: - - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 + - uses: actions/setup-node@v7.0.0 with: node-version: 20 - - run: npm install mailgun.js form-data + # TODO: Replace with a committed lockfile and `npm ci`, or drop npm + # and call the Mailgun REST API from the standard library. + - run: npm install mailgun.js form-data # zizmor: ignore[adhoc-packages] - name: Send notification - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + uses: actions/github-script@v9.0.0 env: MAILGUN_API_KEY: ${{ secrets.MAILGUN_PYTHON_ORG_MAILGUN_KEY }} with: diff --git a/.github/workflows/require-pr-label.yml b/.github/workflows/require-pr-label.yml index 05c862c5918122..58d223bb34f203 100644 --- a/.github/workflows/require-pr-label.yml +++ b/.github/workflows/require-pr-label.yml @@ -18,7 +18,7 @@ jobs: steps: - name: Check there's no DO-NOT-MERGE - uses: mheap/github-action-required-labels@23e10fde7e062233401931a0eece796cd9bf3177 # v5.6.0 + uses: mheap/github-action-required-labels@v5.6.0 with: mode: exactly count: 0 @@ -36,7 +36,7 @@ jobs: steps: # Check that the PR is not awaiting changes from the author due to previous review. - name: Check there's no required changes - uses: mheap/github-action-required-labels@23e10fde7e062233401931a0eece796cd9bf3177 # v5.6.0 + uses: mheap/github-action-required-labels@v5.6.0 with: mode: exactly count: 0 @@ -45,7 +45,7 @@ jobs: awaiting change review - id: is-feature name: Check whether this PR is a feature (contains a "type-feature" label) - uses: mheap/github-action-required-labels@23e10fde7e062233401931a0eece796cd9bf3177 # v5.6.0 + uses: mheap/github-action-required-labels@v5.6.0 with: mode: exactly count: 1 @@ -56,7 +56,7 @@ jobs: - id: awaiting-merge if: steps.is-feature.outputs.status == 'success' name: Check for complete review - uses: mheap/github-action-required-labels@23e10fde7e062233401931a0eece796cd9bf3177 # v5.6.0 + uses: mheap/github-action-required-labels@v5.6.0 with: mode: exactly count: 1 diff --git a/.github/workflows/reusable-check-c-api-docs.yml b/.github/workflows/reusable-check-c-api-docs.yml index 636c960df4a086..ed4b3f86df4e00 100644 --- a/.github/workflows/reusable-check-c-api-docs.yml +++ b/.github/workflows/reusable-check-c-api-docs.yml @@ -15,10 +15,10 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 5 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@v7.0.0 with: python-version: '3.x' - name: Check for undocumented C APIs diff --git a/.github/workflows/reusable-check-html-ids.yml b/.github/workflows/reusable-check-html-ids.yml index d07659c96a20b0..bd258ecd971a7a 100644 --- a/.github/workflows/reusable-check-html-ids.yml +++ b/.github/workflows/reusable-check-html-ids.yml @@ -16,7 +16,7 @@ jobs: timeout-minutes: 30 steps: - name: 'Check out PR head' - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v7.0.1 with: persist-credentials: false ref: ${{ github.event.pull_request.head.sha }} @@ -43,7 +43,7 @@ jobs: MERGE_BASE: ${{ steps.merge-base.outputs.sha }} run: git worktree add /tmp/merge-base "$MERGE_BASE" --detach - name: 'Set up Python' - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + uses: actions/setup-python@v7.0.0 with: python-version: '3' cache: 'pip' @@ -55,7 +55,7 @@ jobs: - name: 'Collect HTML IDs' run: python Doc/tools/check-html-ids.py collect /tmp/merge-base/Doc/build/html -o /tmp/html-ids-base.json.gz - name: 'Download PR head HTML IDs' - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + uses: actions/download-artifact@v8.0.1 with: name: html-ids-head.json.gz path: /tmp diff --git a/.github/workflows/reusable-cifuzz.yml b/.github/workflows/reusable-cifuzz.yml index a70b75eebc4a98..e14c842d7a8daf 100644 --- a/.github/workflows/reusable-cifuzz.yml +++ b/.github/workflows/reusable-cifuzz.yml @@ -37,13 +37,13 @@ jobs: sanitizer: ${{ inputs.sanitizer }} - name: Upload crash if: failure() && steps.build.outcome == 'success' - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + uses: actions/upload-artifact@v7.0.1 with: name: ${{ inputs.sanitizer }}-artifacts path: ./out/artifacts - name: Upload SARIF if: always() && steps.build.outcome == 'success' - uses: github/codeql-action/upload-sarif@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2 + uses: github/codeql-action/upload-sarif@v4.38.2 with: sarif_file: cifuzz-sarif/results.sarif checkout_path: cifuzz-sarif diff --git a/.github/workflows/reusable-context.yml b/.github/workflows/reusable-context.yml index dedfdb3e552f2f..6c3b454d1a4099 100644 --- a/.github/workflows/reusable-context.yml +++ b/.github/workflows/reusable-context.yml @@ -73,14 +73,14 @@ jobs: run-windows-tests: ${{ steps.changes.outputs.run-windows-tests }} steps: - name: Set up Python - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + uses: actions/setup-python@v7.0.0 with: python-version: "3" - run: >- echo '${{ github.event_name }}' - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false ref: >- diff --git a/.github/workflows/reusable-docs.yml b/.github/workflows/reusable-docs.yml index e13c43526bc190..b60bdfd302db8f 100644 --- a/.github/workflows/reusable-docs.yml +++ b/.github/workflows/reusable-docs.yml @@ -38,7 +38,7 @@ jobs: refspec_pr: '+${{ github.event.pull_request.head.sha }}:remotes/origin/${{ github.event.pull_request.head.ref }}' steps: - name: 'Check out latest PR branch commit' - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v7.0.1 with: persist-credentials: false ref: >- @@ -63,7 +63,7 @@ jobs: git fetch origin "${refspec_base}" --shallow-since="${DATE}" \ --no-tags --prune --no-recurse-submodules - name: 'Set up Python' - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + uses: actions/setup-python@v7.0.0 with: python-version: '3' cache: 'pip' @@ -94,7 +94,7 @@ jobs: run: python Doc/tools/check-html-ids.py collect Doc/build/html -o Doc/build/html-ids-head.json.gz - name: 'Upload HTML IDs' if: github.event_name == 'pull_request' - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + uses: actions/upload-artifact@v7.0.1 with: name: html-ids-head path: Doc/build/html-ids-head.json.gz @@ -104,7 +104,7 @@ jobs: name: 'Check for removed HTML IDs' needs: build-doc if: github.event_name == 'pull_request' - uses: ./.github/workflows/reusable-check-html-ids.yml + uses: $/.github/workflows/reusable-check-html-ids.yml # Run "doctest" on HEAD as new syntax doesn't exist in the latest stable release doctest: @@ -112,10 +112,10 @@ jobs: runs-on: ubuntu-26.04 timeout-minutes: 60 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + - uses: actions/cache@v6.1.0 with: path: ~/.cache/pip key: ubuntu-doc-${{ hashFiles('Doc/requirements.txt') }} @@ -138,11 +138,11 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 30 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: 'Set up Python' - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + uses: actions/setup-python@v7.0.0 with: python-version: '3' cache: 'pip' diff --git a/.github/workflows/reusable-emscripten.yml b/.github/workflows/reusable-emscripten.yml index d093c86eb2db96..6565e671420f09 100644 --- a/.github/workflows/reusable-emscripten.yml +++ b/.github/workflows/reusable-emscripten.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-26.04 timeout-minutes: 40 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: "Read Emscripten config" @@ -41,18 +41,18 @@ jobs: with open(os.environ["GITHUB_ENV"], "a") as f: f.write(f"PY_EMSDK_CACHE={emsdk_cache}\n") - name: "Install Node.js" - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 + uses: actions/setup-node@v7.0.0 with: node-version: ${{ steps.emscripten-config.outputs.node-version }} - name: "Cache Emscripten SDK" id: emsdk-cache - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ${{ env.PY_EMSDK_CACHE }} key: emsdk-${{ steps.emscripten-config.outputs.emscripten-version }}-${{ steps.emscripten-config.outputs.deps-hash }} restore-keys: emsdk-${{ steps.emscripten-config.outputs.emscripten-version }} - name: "Install Python" - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + uses: actions/setup-python@v7.0.0 with: python-version: '3.x' - name: "Runner image version" diff --git a/.github/workflows/reusable-install.yml b/.github/workflows/reusable-install.yml index b8acf1a0fb1771..fa09a9a4f34c58 100644 --- a/.github/workflows/reusable-install.yml +++ b/.github/workflows/reusable-install.yml @@ -17,7 +17,7 @@ jobs: env: PYTHONSTRICTEXTENSIONBUILD: 1 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Register gcc problem matcher diff --git a/.github/workflows/reusable-macos.yml b/.github/workflows/reusable-macos.yml index 2883a20b612f85..ec9fd64b282aa1 100644 --- a/.github/workflows/reusable-macos.yml +++ b/.github/workflows/reusable-macos.yml @@ -31,7 +31,7 @@ jobs: PYTHONSTRICTEXTENSIONBUILD: 1 TERM: linux steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Runner image version diff --git a/.github/workflows/reusable-san.yml b/.github/workflows/reusable-san.yml index 9c31241498a75e..fb7d84e889e118 100644 --- a/.github/workflows/reusable-san.yml +++ b/.github/workflows/reusable-san.yml @@ -30,7 +30,7 @@ jobs: runs-on: ubuntu-26.04 timeout-minutes: 60 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Runner image version @@ -100,7 +100,7 @@ jobs: - name: 'Restore OpenSSL build' id: cache-openssl if: inputs.sanitizer == 'TSan' || inputs.sanitizer == 'ASan' - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ./multissl/openssl/${{ env.OPENSSL_VER }} key: >- @@ -163,7 +163,7 @@ jobs: run: find "${GITHUB_WORKSPACE}" -name 'san_log.*' | xargs head -n 1000 - name: Archive logs if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + uses: actions/upload-artifact@v7.0.1 with: name: >- ${{ inputs.sanitizer }}-logs-${{ diff --git a/.github/workflows/reusable-test-lazy-imports-all.yml b/.github/workflows/reusable-test-lazy-imports-all.yml index b30a776f1a9858..ea2615b880558b 100644 --- a/.github/workflows/reusable-test-lazy-imports-all.yml +++ b/.github/workflows/reusable-test-lazy-imports-all.yml @@ -26,7 +26,7 @@ jobs: env: EXCLUDE_FILE: Lib/test/lazy_imports_all_exclude.txt steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Register gcc problem matcher diff --git a/.github/workflows/reusable-ubuntu.yml b/.github/workflows/reusable-ubuntu.yml index 1b10078b8409f4..f6e42e1861349c 100644 --- a/.github/workflows/reusable-ubuntu.yml +++ b/.github/workflows/reusable-ubuntu.yml @@ -39,7 +39,7 @@ jobs: PYTHONSTRICTEXTENSIONBUILD: 1 TERM: linux steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Register gcc problem matcher @@ -59,7 +59,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@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ./multissl/openssl/${{ env.OPENSSL_VER }} key: ${{ inputs.os }}-multissl-openssl-${{ env.OPENSSL_VER }} diff --git a/.github/workflows/reusable-wasi.yml b/.github/workflows/reusable-wasi.yml index 12dd790897ad39..d4d51fbd1f3029 100644 --- a/.github/workflows/reusable-wasi.yml +++ b/.github/workflows/reusable-wasi.yml @@ -17,12 +17,12 @@ jobs: env: WASMTIME_VERSION: 48.0.2 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false # No problem resolver registered as one doesn't currently exist for Clang. - name: "Install wasmtime" - uses: bytecodealliance/actions/wasmtime/setup@9152e710e9f7182e4c29ad218e4f335a7b203613 # v1.1.3 + uses: bytecodealliance/actions/wasmtime/setup@v1.1.3 with: version: ${{ env.WASMTIME_VERSION }} - name: "Read WASI SDK version" @@ -38,12 +38,12 @@ jobs: shell: python - name: "Install WASI SDK" id: install-wasi-sdk - uses: bytecodealliance/setup-wasi-sdk-action@77d09f3df1e8da344d8403cf8631af8977938b4c + uses: bytecodealliance/setup-wasi-sdk-action@v1.0.0 with: version: ${{ steps.wasi-sdk-version.outputs.version }} add-to-path: false - name: "Install Python" - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + uses: actions/setup-python@v7.0.0 with: python-version: '3.x' - name: "Runner image version" @@ -64,6 +64,4 @@ jobs: - name: "Display build info of the host/WASI Python" run: python3 Platforms/WASI pythoninfo-host - name: "Test" - run: | - CROSS_BUILD_WASI=$(python3 Platforms/WASI path) - make --directory "${CROSS_BUILD_WASI}" test + run: python3 Platforms/WASI test diff --git a/.github/workflows/reusable-windows.yml b/.github/workflows/reusable-windows.yml index 4be4bc05fa6d0d..eb6c22f2c4cf43 100644 --- a/.github/workflows/reusable-windows.yml +++ b/.github/workflows/reusable-windows.yml @@ -31,7 +31,7 @@ jobs: env: ARCH: ${{ inputs.arch }} steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Register MSVC problem matcher diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index c6094947cf98f6..4b528a6f986177 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -18,7 +18,7 @@ jobs: steps: - name: "Check PRs" - uses: actions/stale@4391f3da665fdf50b6810c1a66712fb9ba21aa93 # v11.0.0 + uses: actions/stale@v11.0.0 with: repo-token: ${{ secrets.GITHUB_TOKEN }} stale-pr-message: 'This PR is stale because it has been open for 90 days with no activity.' diff --git a/.github/workflows/tail-call.yml b/.github/workflows/tail-call.yml index 1dc5ea1bc51356..c302f543a8fd74 100644 --- a/.github/workflows/tail-call.yml +++ b/.github/workflows/tail-call.yml @@ -41,10 +41,10 @@ jobs: runner: ubuntu-26.04-arm configure_flags: --with-pydebug steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@v7.0.0 with: python-version: '3.11' - name: Build diff --git a/.github/workflows/verify-ensurepip-wheels.yml b/.github/workflows/verify-ensurepip-wheels.yml index 4d0c5d21d5def2..89083b65d71bfa 100644 --- a/.github/workflows/verify-ensurepip-wheels.yml +++ b/.github/workflows/verify-ensurepip-wheels.yml @@ -25,10 +25,10 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@v7.0.0 with: python-version: '3' - name: Compare checksum of bundled wheels to the ones published on PyPI diff --git a/.github/workflows/verify-expat.yml b/.github/workflows/verify-expat.yml index 7a1046059e0ba8..ce1ae0ee8a77fc 100644 --- a/.github/workflows/verify-expat.yml +++ b/.github/workflows/verify-expat.yml @@ -23,7 +23,7 @@ jobs: runs-on: ubuntu-slim timeout-minutes: 5 steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Download and verify bundled libexpat files diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 45528d9f32d6c9..bda616bad11c54 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: 3b3f7c3f57fe9925356faf5fe6230835138be230 # frozen: v0.15.17 + rev: c0f278f1254ccc8fb09676bcdb8d847e7db493e5 # frozen: v0.17.4 hooks: - id: ruff-check name: Run Ruff (lint) on Platforms/Apple/ @@ -70,7 +70,7 @@ repos: files: ^Tools/wasm/ - repo: https://github.com/psf/black-pre-commit-mirror - rev: ea488cebbfd88a5f50b8bd95d5c829d0bb76feb8 # frozen: 26.1.0 + rev: a5439e9af2ff9371297bb1a76a01b8ad17989b88 # frozen: 26.10.1 hooks: - id: black name: Run Black on Tools/jit/ @@ -104,7 +104,7 @@ repos: files: '^\.github/CODEOWNERS|\.(gram)$' - repo: https://github.com/python-jsonschema/check-jsonschema - rev: 9f48a48aa91a6040d749ad68ec70907d907a5a7f # frozen: 0.37.0 + rev: a76b92851fc9eede9f9bc4073eb55202f34eb0df # frozen: 0.39.0 hooks: - id: check-dependabot - id: check-github-workflows @@ -116,7 +116,7 @@ repos: - id: actionlint - repo: https://github.com/zizmorcore/zizmor-pre-commit - rev: b546b77c44c466a54a42af5499dcc0dcc1a3193f # frozen: v1.22.0 + rev: 06a39750ec31860bb44f2acf67182c40016cd1e7 # frozen: v1.32.0 hooks: - id: zizmor diff --git a/AGENTS.md b/AGENTS.md index d23d0c9315d3f3..ce4489146a9ee9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -14,3 +14,16 @@ When acting on this repository, apply the policy's core principles: - Follow existing coding style and patterns. - Write tests that exercise the change. - Keep backwards compatibility with prior releases in mind. + +## Opening issues + +Do not auto-generate issues with AI tools. + +Search GitHub for duplicate issues before opening a new issue. Do not open +duplicates. + +```bash +gh search issues --repo python/cpython "" +gh search issues --repo python/cpython "" +gh issue list --repo python/cpython --state all --label "" --search "" +``` diff --git a/Doc/builtins/functions.rst b/Doc/builtins/functions.rst index efd6bb9e28c89a..8186eae5662621 100644 --- a/Doc/builtins/functions.rst +++ b/Doc/builtins/functions.rst @@ -740,7 +740,7 @@ are always available. They are listed here in alphabetical order. This function supports dynamic execution of Python code. *source* must be either a string or a code object. If it is a string, the string is parsed as 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, + 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 :ref:`file-input` in the Reference Manual). Be aware that the :keyword:`nonlocal`, :keyword:`yield`, and :keyword:`return` @@ -1181,6 +1181,11 @@ are always available. They are listed here in alphabetical order. a :exc:`TypeError` exception is raised. :exc:`TypeError` may not be raised for an invalid type if an earlier check succeeds. + If *object* has a ``__class__`` attribute that differs from + :func:`type(object) ` (for example a proxy, or a :mod:`unittest.mock` + object created with a *spec*), ``True`` is also returned when that class + is a subclass of *classinfo*. + .. versionchanged:: 3.10 *classinfo* can be a :ref:`types-union`. @@ -1950,7 +1955,7 @@ are always available. They are listed here in alphabetical order. >>> MISSING - Sentinel objects are truthy and compare equal only to themselves. They are + Sentinel objects are true and compare equal only to themselves. They are intended to be compared with the :keyword:`is` operator. ``sentinel`` does not support subclassing. @@ -2470,9 +2475,3 @@ are always available. They are listed here in alphabetical order. .. versionchanged:: 3.9 When the command line options :option:`-E` or :option:`-I` are being used, the environment variable :envvar:`PYTHONCASEOK` is now ignored. - -.. rubric:: Footnotes - -.. [#] Note that the parser only accepts the Unix-style end of line convention. - If you are reading the code from a file, make sure to use newline conversion - mode to convert Windows or Mac-style newlines. diff --git a/Doc/builtins/stdtypes.rst b/Doc/builtins/stdtypes.rst index b998331cb60c5b..7072f01e09aa25 100644 --- a/Doc/builtins/stdtypes.rst +++ b/Doc/builtins/stdtypes.rst @@ -1225,8 +1225,8 @@ the underlying generator function: If the asynchronous generator function then exits gracefully, is already closed, or raises :exc:`GeneratorExit` (by not catching the exception), then the returned awaitable will raise a :exc:`StopIteration` exception. - Any further awaitables returned by subsequent calls to the asynchronous - generator will raise a :exc:`StopAsyncIteration` exception. + Awaitables returned by subsequent calls to :meth:`~agen.__anext__` + or :meth:`~agen.asend` will raise a :exc:`StopAsyncIteration` exception. If the asynchronous generator yields a value, a :exc:`RuntimeError` is raised by the awaitable. @@ -2313,19 +2313,19 @@ expression support in the :mod:`re` module). Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces - ``{}``. Each replacement field contains either the numeric index of a - positional argument, or the name of a keyword argument. Returns a copy of + ``{}``. Each replacement field refers to a positional argument (numbered + explicitly or automatically) or a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. For example: .. doctest:: - >>> "The sum of 1 + 2 is {0}".format(1+2) - 'The sum of 1 + 2 is 3' - >>> "The sum of {a} + {b} is {answer}".format(answer=1+2, a=1, b=2) + >>> "The sum of {} + {} is {}".format(1, 2, 1+2) 'The sum of 1 + 2 is 3' >>> "{1} expects the {0} Inquisition!".format("Spanish", "Nobody") 'Nobody expects the Spanish Inquisition!' + >>> "The sum of {a} + {b} is {answer}".format(answer=1+2, a=1, b=2) + 'The sum of 1 + 2 is 3' See :ref:`formatstrings` for a description of the various formatting options that can be specified in format strings. @@ -3180,7 +3180,7 @@ expression support in the :mod:`re` module). Return a copy of the string with all the cased characters [4]_ converted to uppercase. Note that ``s.upper().isupper()`` might be ``False`` if ``s`` - contains uncased characters or if the Unicode category of the resulting + contains no cased characters or if the Unicode category of the resulting character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, titlecase). @@ -3537,9 +3537,8 @@ The conversion types are: | | character in the result. | | +------------+-----------------------------------------------------+-------+ -For floating-point formats, the result should be correctly rounded to a given -precision ``p`` of digits after the decimal point. The rounding mode matches -that of the :func:`round` builtin. +For floating-point formats, the result should be correctly rounded to the +specified precision. The rounding mode matches that of the :func:`round` builtin. Notes: @@ -5928,8 +5927,9 @@ can be used interchangeably to index the same dictionary entry. .. seealso:: - :class:`frozendict` and :class:`types.MappingProxyType` can be used to - create a read-only view of a :class:`dict`. + :class:`types.MappingProxyType` can be used to create a read-only view + of a :class:`dict`. :class:`frozendict` is an immutable version of + :class:`dict`. .. seealso:: @@ -6082,8 +6082,7 @@ Frozen dictionaries Similar to :meth:`dict.fromkeys`, but call again the type constructor with an initialized :class:`frozendict` if the type is a - :class:`frozendict` subclass or if the constructor returned a - :class:`frozendict`. + :class:`frozendict` subclass. .. versionadded:: 3.15 @@ -6532,8 +6531,8 @@ The user-exposed type for the union object can be accessed from .. note:: The :meth:`!__or__` method for type objects was added to support the syntax - ``X | Y``. If a metaclass implements :meth:`!__or__`, the Union may - override it: + ``X | Y``. If a metaclass implements :meth:`!__or__`, it may override + the default behavior of creating a union: .. doctest:: diff --git a/Doc/c-api/code.rst b/Doc/c-api/code.rst index 48d9b83f437ff7..efd0bc79cbd780 100644 --- a/Doc/c-api/code.rst +++ b/Doc/c-api/code.rst @@ -50,7 +50,7 @@ bound into a function. free variables of a code object. The names are in the same order as the values returned by :c:func:`PyUnstable_InterpreterFrame_GetLocal`. - .. versionadded:: next + .. versionadded:: 3.16 .. c:function:: PyCodeObject* PyUnstable_Code_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, PyObject *qualname, int firstlineno, PyObject *linetable, PyObject *exceptiontable) diff --git a/Doc/c-api/conversion.rst b/Doc/c-api/conversion.rst index 0b176cdb118234..84cb777692ce3e 100644 --- a/Doc/c-api/conversion.rst +++ b/Doc/c-api/conversion.rst @@ -277,4 +277,4 @@ macros above: Values used internally by the ``Py_IS*`` classification macros above. Exposed for backwards compatibility only. - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 diff --git a/Doc/c-api/dict.rst b/Doc/c-api/dict.rst index f375af2925b5fd..cdb3d8fba5af80 100644 --- a/Doc/c-api/dict.rst +++ b/Doc/c-api/dict.rst @@ -472,11 +472,14 @@ Dictionary objects .. c:function:: int PyDict_Update(PyObject *a, PyObject *b) - This is the same as ``PyDict_Merge(a, b, 1)`` in C, and is similar to - ``a.update(b)`` in Python except that :c:func:`PyDict_Update` doesn't fall - back to the iterating over a sequence of key value pairs if the second - argument has no "keys" attribute. Return ``0`` on success or ``-1`` if an - exception was raised. + This is the same as ``PyDict_Merge(a, b, 1)`` in C. + + Similar to ``a.update(b)`` in Python, except that this function + does not fall back to the iterating over a sequence of key-value pairs + if *b* is not a mapping. + Use :c:func:`PyDict_MergeFromSeq2` for this case. + + Return ``0`` on success or ``-1`` if an exception was raised. .. note:: @@ -523,11 +526,13 @@ Dictionary objects as an argument, prefer :c:func:`PyFrozenDict_New` to leave the source intact. + Does not work for dicts with embedded values. + .. impl-detail:: Works with O(1) complexity. - .. versionadded:: next + .. versionadded:: 3.16 .. c:function:: int PyDict_AddWatcher(PyDict_WatchCallback callback) diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst index ba4e2a418b2a49..dd67222bebc3db 100644 --- a/Doc/c-api/exceptions.rst +++ b/Doc/c-api/exceptions.rst @@ -1350,19 +1350,19 @@ to :c:data:`PyExc_OSError`. .. c:var:: PyObject *PyExc_EnvironmentError - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 Use :c:data:`PyExc_OSError` instead. .. c:var:: PyObject *PyExc_IOError - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 Use :c:data:`PyExc_OSError` instead. .. c:var:: PyObject *PyExc_WindowsError .. availability:: Windows - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 Use :c:data:`PyExc_OSError` instead. diff --git a/Doc/c-api/file.rst b/Doc/c-api/file.rst index a6bfa8358e3aac..3f5b3bdbb26f4e 100644 --- a/Doc/c-api/file.rst +++ b/Doc/c-api/file.rst @@ -188,4 +188,4 @@ by mistake. They are documented solely for completeness; use other The string literal ``"b"``, used to open files in binary mode. Exposed for backwards compatibility only. - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 diff --git a/Doc/c-api/frame.rst b/Doc/c-api/frame.rst index ea684630627355..a086c60905402e 100644 --- a/Doc/c-api/frame.rst +++ b/Doc/c-api/frame.rst @@ -258,4 +258,4 @@ Unless using :pep:`523`, you will not need this. * If the slot is unset or hidden, store ``NULL`` in *result* and return ``0``. * On error, raise an exception, store ``NULL`` in *result* and return ``-1``. - .. versionadded:: next + .. versionadded:: 3.16 diff --git a/Doc/c-api/gcsupport.rst b/Doc/c-api/gcsupport.rst index 2afade177674a2..0ebe719af4ff4d 100644 --- a/Doc/c-api/gcsupport.rst +++ b/Doc/c-api/gcsupport.rst @@ -253,6 +253,13 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type: returned immediately. Otherwise, return zero. + The garbage collector can call :c:member:`~PyTypeObject.tp_traverse` more + than once for the same object during a collection. If the object's + references do not change, each traversal must visit the same references. + In particular, do not skip a reference because a lock is busy. + If one traversal visits a reference and a later traversal skips it, the + collector can clear an object that is still reachable. + A typical :c:member:`!tp_traverse` function calls the :c:func:`Py_VISIT` convenience macro on each of the instance's members that are Python objects that the instance owns. @@ -318,14 +325,6 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type: For a list of safe functions, see a :ref:`separate section ` below. - If an error occurs during traversal, ignore it and return 0. - If ``tp_traverse`` fails to visit some strong references, garbage reference - cycles involving the skipped references will not be reliably broken, - but correctness won't be affected. - For example, if traversal requires a lock that cannot be acquired promptly, - skip the traversal. - The garbage collector will visit your object again in a future pass. - .. note:: The :c:func:`Py_VISIT` call may be skipped for those members that provably diff --git a/Doc/c-api/init_config.rst b/Doc/c-api/init_config.rst index f9190cbafc6a24..cc96ba67c4e85b 100644 --- a/Doc/c-api/init_config.rst +++ b/Doc/c-api/init_config.rst @@ -1227,8 +1227,8 @@ PyConfig .. c:member:: int safe_path - If equals to zero, ``Py_RunMain()`` prepends a potentially unsafe path to - :data:`sys.path` at startup: + If equals to zero, ``Py_RunMain()`` prepends a potentially unsafe + :data:`sys._path0` to :data:`sys.path` at startup: * If :c:member:`argv[0] ` is equal to ``L"-m"`` (``python -m module``), prepend the current working directory. diff --git a/Doc/c-api/module.rst b/Doc/c-api/module.rst index 6e4e3c48c08429..9ec1407c88d01b 100644 --- a/Doc/c-api/module.rst +++ b/Doc/c-api/module.rst @@ -552,19 +552,19 @@ This means that if you have a module object, but you are not sure if it .. code-block:: c - PyObject *module = + PyObject *module = ; void *module_token; if (PyModule_GetToken(module, &module_token) < 0) { return NULL; } if (module_token != your_token) { - PyErr_SetString(PyExc_ValueError, "unexpected module") + PyErr_SetString(PyExc_ValueError, "unexpected module"); return NULL; } // This module's state has the expected memory layout; it's safe to cast - struct my_state state = (struct my_state*)PyModule_GetState(module) + struct my_state *state = (struct my_state*)PyModule_GetState(module); A module's token -- and the *your_token* value to use in the above code -- is: diff --git a/Doc/c-api/profiling.rst b/Doc/c-api/profiling.rst index 8393858c827e2d..c87c03e96f83ae 100644 --- a/Doc/c-api/profiling.rst +++ b/Doc/c-api/profiling.rst @@ -251,4 +251,4 @@ Performance statistics This constant is intended for internal use only and is not useful to extension modules. - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst index 2f2c8e06f668b9..19b9265eccf746 100644 --- a/Doc/c-api/structures.rst +++ b/Doc/c-api/structures.rst @@ -792,7 +792,7 @@ The following flags can be used with :c:member:`PyMemberDef.flags`: A member flag that has no effect, retained solely for backwards compatibility. This is only available with ``#include "structmember.h"``. - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 Omit this flag instead. diff --git a/Doc/c-api/synchronization.rst b/Doc/c-api/synchronization.rst index a74a92ca7cfa24..26712549f285f1 100644 --- a/Doc/c-api/synchronization.rst +++ b/Doc/c-api/synchronization.rst @@ -269,7 +269,7 @@ These APIs are obsolete since Python 3.13 with the introduction of A :term:`soft deprecated` alias to :c:expr:`long long`, the integer type used for the timeout argument of :c:func:`PyThread_acquire_lock_timed`. - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 .. c:var:: const long long PY_TIMEOUT_MAX diff --git a/Doc/c-api/tuple.rst b/Doc/c-api/tuple.rst index 10929b233d6840..2105571eee5ff0 100644 --- a/Doc/c-api/tuple.rst +++ b/Doc/c-api/tuple.rst @@ -161,7 +161,7 @@ Tuple Objects :c:func:`PyTuple_SetItem`; except if *newsize* is zero in which case it returns the immutable empty tuple. - .. versionchanged:: next + .. versionchanged:: 3.16 The function now fails if the tuple hash value was already computed. diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index 56dd0bd32470cb..c2b9124c9201ba 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -3343,7 +3343,7 @@ A type that supports weakrefs, instance dicts, and hashing:: .tp_new = myobj_new, .tp_traverse = (traverseproc)myobj_traverse, .tp_clear = (inquiry)myobj_clear, - .tp_alloc = PyType_GenericNew, + .tp_alloc = PyType_GenericAlloc, .tp_dealloc = (destructor)myobj_dealloc, .tp_repr = (reprfunc)myobj_repr, .tp_hash = (hashfunc)myobj_hash, diff --git a/Doc/c-api/weakref.rst b/Doc/c-api/weakref.rst index 83ca45ec251e2b..40c9f1151112c8 100644 --- a/Doc/c-api/weakref.rst +++ b/Doc/c-api/weakref.rst @@ -114,7 +114,7 @@ as much as it can. This function is intended for internal use only. Do not access or modify the weak reference list through the returned pointer. - .. soft-deprecated:: next + .. soft-deprecated:: 3.16 .. c:function:: void PyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject *object) diff --git a/Doc/data/refcounts.dat b/Doc/data/refcounts.dat index c19a8a3083a430..b488237b31b642 100644 --- a/Doc/data/refcounts.dat +++ b/Doc/data/refcounts.dat @@ -2417,6 +2417,9 @@ PyType_GenericNew:PyObject*:type:0: PyType_GenericNew:PyObject*:args:0: PyType_GenericNew:PyObject*:kwds:0: +PyType_GetDict:PyObject*::+1: +PyType_GetDict:PyTypeObject*:type:0: + PyType_GetFlags:unsigned long::: PyType_GetFlags:PyTypeObject*:type:0: diff --git a/Doc/deprecations/pending-removal-in-3.18.rst b/Doc/deprecations/pending-removal-in-3.18.rst index 19113aab981bbc..fb10420ab38c52 100644 --- a/Doc/deprecations/pending-removal-in-3.18.rst +++ b/Doc/deprecations/pending-removal-in-3.18.rst @@ -11,6 +11,13 @@ Pending removal in Python 3.18 C implementation, has been deprecated since Python 3.13. (Contributed by Serhiy Storchaka in :gh:`89902`.) +* :mod:`urllib.parse`: + + * Accepting objects with false values (like ``0`` and ``[]``) except empty + strings, byte-like objects and ``None`` in the URL parsing functions has + been deprecated since Python 3.16. + (Contributed by Jacob Walls in :gh:`63293`.) + * Deprecations defined by :pep:`829`: * ``import`` lines in :file:`{name}.pth` files are silently ignored. diff --git a/Doc/extending/embedding.rst b/Doc/extending/embedding.rst index cb41889437c8b0..8a87d7cb1c3b4c 100644 --- a/Doc/extending/embedding.rst +++ b/Doc/extending/embedding.rst @@ -95,15 +95,16 @@ perform some operation on a file. :: See :ref:`arg-parsing-string-and-buffers` for a description of this macro. Setting :c:member:`PyConfig.program_name` should be called before -:c:func:`Py_InitializeFromConfig` to inform the interpreter about paths to Python run-time -libraries. Next, the Python interpreter is initialized with -:c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script -that prints the date and time. Afterwards, the :c:func:`Py_FinalizeEx` call shuts -the interpreter down, followed by the end of the program. In a real program, -you may want to get the Python script from another source, perhaps a text-editor -routine, a file, or a database. Getting the Python code from a file can better -be done by using the :c:func:`PyRun_SimpleFile` function, which saves you the -trouble of allocating memory space and loading the file contents. +:c:func:`Py_InitializeFromConfig` to inform the interpreter about paths to +Python run-time libraries. Next, the Python interpreter is initialized with +:c:func:`Py_InitializeFromConfig`, followed by the execution of a hard-coded +Python script that prints the date and time. Afterwards, the +:c:func:`Py_FinalizeEx` call shuts the interpreter down, followed by the end of +the program. In a real program, you may want to get the Python script from +another source, perhaps a text-editor routine, a file, or a database. Getting +the Python code from a file can better be done by using the +:c:func:`PyRun_SimpleFile` function, which saves you the trouble of allocating +memory space and loading the file contents. .. _lower-level-embedding: diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst index 6bf27ce33c3182..98ec3f7f993efb 100644 --- a/Doc/extending/extending.rst +++ b/Doc/extending/extending.rst @@ -1040,7 +1040,7 @@ exporting module, not a client module. Finally, the module's :c:data:`mod_exec } Note that ``PySpam_API`` is declared ``static``; otherwise the pointer -array would disappear when :c:func:`!PyInit_spam` terminates! +array would disappear when :c:func:`!spam_module_exec` terminates! The bulk of the work is in the header file :file:`spammodule.h`, which looks like this:: diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 02a3a3a22070da..ca5fcc154f7312 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -5,6 +5,7 @@ Design and History FAQ .. only:: html .. contents:: + :local: Why does Python use indentation for grouping of statements? @@ -400,7 +401,7 @@ library that are impossible to free (e.g. a tool like Purify will complain about these). Python is, however, aggressive about cleaning up memory on exit and does try to destroy every single object. -If you want to force Python to delete certain things on deallocation use the +If you want to force Python to delete certain things when Python exits, use the :mod:`atexit` module to run a function that will force those deletions. diff --git a/Doc/faq/extending.rst b/Doc/faq/extending.rst index 1d5abed2317b0c..555c312b913769 100644 --- a/Doc/faq/extending.rst +++ b/Doc/faq/extending.rst @@ -5,6 +5,7 @@ Extending/Embedding FAQ .. only:: html .. contents:: + :local: .. highlight:: c @@ -208,25 +209,20 @@ minor that it doesn't seem worth the effort.) How do I debug an extension? ---------------------------- -When using GDB with dynamically loaded extensions, you can't set a breakpoint in -your extension until your extension is loaded. - -In your ``.gdbinit`` file (or interactively), add the command: - -.. code-block:: none - - br _PyImport_LoadDynamicModule - -Then, when you run GDB: +When using GDB with dynamically loaded extensions, enable pending breakpoints +to set a breakpoint before the extension is loaded. For example, to stop in +a function named ``myfunction``: .. code-block:: shell-session $ gdb /local/bin/python - gdb) run myscript.py - gdb) continue # repeat until your extension is loaded - gdb) finish # so that your extension is loaded - gdb) br myfunction.c:50 - gdb) continue + (gdb) set breakpoint pending on + (gdb) break myfunction + (gdb) run myscript.py + +GDB resolves the breakpoint when the extension is loaded and stops when the +function is called. You can also set a pending breakpoint directly in your +extension's source code, for example with ``break myfunction.c:50``. I want to compile a Python module on my Linux system, but some files are missing. Why? -------------------------------------------------------------------------------------- diff --git a/Doc/faq/general.rst b/Doc/faq/general.rst index ff38ba395fba22..ba4fb89713c66d 100644 --- a/Doc/faq/general.rst +++ b/Doc/faq/general.rst @@ -7,6 +7,7 @@ General Python FAQ .. only:: html .. contents:: + :local: General Information diff --git a/Doc/faq/gui.rst b/Doc/faq/gui.rst index cfa60feceb31b7..37e77ee08fc8ec 100644 --- a/Doc/faq/gui.rst +++ b/Doc/faq/gui.rst @@ -7,6 +7,7 @@ Graphic User Interface FAQ .. only:: html .. contents:: + :local: .. XXX need review for Python 3. diff --git a/Doc/faq/library.rst b/Doc/faq/library.rst index d8d75ca6f2ec96..abfd35ea884641 100644 --- a/Doc/faq/library.rst +++ b/Doc/faq/library.rst @@ -7,6 +7,7 @@ Library and Extension FAQ .. only:: html .. contents:: + :local: General Library Questions ========================= @@ -241,51 +242,6 @@ The :mod:`threading` module builds convenient abstractions on top of the low-level primitives provided by the :mod:`_thread` module. -None of my threads seem to run: why? ------------------------------------- - -As soon as the main thread exits, all threads are killed. Your main thread is -running too quickly, giving the threads no time to do any work. - -A simple fix is to add a sleep to the end of the program that's long enough for -all the threads to finish:: - - import threading, time - - def thread_task(name, n): - for i in range(n): - print(name, i) - - for i in range(10): - T = threading.Thread(target=thread_task, args=(str(i), i)) - T.start() - - time.sleep(10) # <---------------------------! - -But now (on many platforms) the threads don't run in parallel, but appear to run -sequentially, one at a time! The reason is that the OS thread scheduler doesn't -start a new thread until the previous thread is blocked. - -A simple fix is to add a tiny sleep to the start of the run function:: - - def thread_task(name, n): - time.sleep(0.001) # <--------------------! - for i in range(n): - print(name, i) - - for i in range(10): - T = threading.Thread(target=thread_task, args=(str(i), i)) - T.start() - - time.sleep(10) - -Instead of trying to guess a good delay value for :func:`time.sleep`, -it's better to use some kind of semaphore mechanism. One idea is to use the -:mod:`queue` module to create a queue object, let each thread append a token to -the queue when it finishes, and let the main thread read as many tokens from the -queue as there are threads. - - How do I parcel out work among a bunch of worker threads? --------------------------------------------------------- @@ -410,35 +366,17 @@ deployment on high-end multiprocessor server machines, because a multi-threaded Python program effectively only uses one CPU, due to the insistence that (almost) all Python code can only run while the GIL is held. -With the approval of :pep:`703` work is now underway to remove the GIL from the -CPython implementation of Python. Initially it will be implemented as an -optional compiler flag when building the interpreter, and so separate -builds will be available with and without the GIL. Long-term, the hope is -to settle on a single build, once the performance implications of removing the -GIL are fully understood. Python 3.13 is likely to be the first release -containing this work, although it may not be completely functional in this -release. - -The current work to remove the GIL is based on a -`fork of Python 3.9 with the GIL removed `_ -by Sam Gross. -Prior to that, -in the days of Python 1.5, Greg Stein actually implemented a comprehensive -patch set (the "free threading" patches) that removed the GIL and replaced it -with fine-grained locking. Adam Olsen did a similar experiment -in his `python-safethread `_ -project. Unfortunately, both of these earlier experiments exhibited a sharp -drop in single-thread -performance (at least 30% slower), due to the amount of fine-grained locking -necessary to compensate for the removal of the GIL. The Python 3.9 fork -is the first attempt at removing the GIL with an acceptable performance -impact. +Since Python 3.13, CPython provides an optional free-threaded build that can run +with the GIL disabled, allowing Python threads to run in parallel on multiple +CPU cores (see :pep:`703`). Free-threaded Python is officially supported as of +Python 3.14 (see :pep:`779`), but is not the default build. See +:doc:`/howto/free-threading-python` for details. The presence of the GIL in current Python releases doesn't mean that you can't make good use of Python on multi-CPU machines! You just have to be creative with dividing the work up between multiple *processes* rather than multiple *threads*. The -:class:`~concurrent.futures.ProcessPoolExecutor` class in the new +:class:`~concurrent.futures.ProcessPoolExecutor` class in the :mod:`concurrent.futures` module provides an easy way of doing so; the :mod:`multiprocessing` module provides a lower-level API in case you want more control over dispatching of tasks. @@ -452,7 +390,9 @@ already do this. An alternative approach to reducing the impact of the GIL is to make the GIL a per-interpreter-state lock rather than truly global. This was :ref:`first implemented in Python 3.12 ` and is -available in the C API. A Python interface to it is expected in Python 3.13. +available in the C API. Python interfaces were added in Python 3.14 through the +:mod:`concurrent.interpreters` module and the +:class:`~concurrent.futures.InterpreterPoolExecutor` class. The main limitation to it at the moment is likely to be 3rd party extension modules, since these must be written with multiple interpreters in mind in order to be usable, so many older extension modules will not be usable. diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 4e1157e6ebe729..fe2e8587eb201b 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -7,6 +7,7 @@ Programming FAQ .. only:: html .. contents:: + :local: General questions ================= @@ -1197,8 +1198,7 @@ Use the :func:`reversed` built-in function:: for x in reversed(sequence): ... # do something with x ... -This won't touch your original sequence, but build a new copy with reversed -order to iterate over. +This returns a reverse iterator without modifying your original sequence. How do you remove duplicates from a list? @@ -1641,8 +1641,8 @@ Use the built-in :func:`super` function:: In the example, :func:`super` will automatically determine the instance from which it was called (the ``self`` value), look up the :term:`method resolution -order` (MRO) with ``type(self).__mro__``, and return the next in line after -``Derived`` in the MRO: ``Base``. +order` (MRO) with ``type(self).__mro__``, and return a proxy that searches for +methods starting with the next class after ``Derived`` in the MRO: ``Base``. How can I organize my code to make it easier to change the base class? @@ -1685,7 +1685,7 @@ C)`` holds, unless overridden by ``c`` itself or by some class on the base-class search path from ``c.__class__`` back to ``C``. Caution: within a method of C, an assignment like ``self.count = 42`` creates a -new and unrelated instance named "count" in ``self``'s own dict. Rebinding of a +new and unrelated attribute named "count" in ``self``'s own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not:: @@ -2054,30 +2054,6 @@ relevant instance attributes are mutable, the ``cached_property`` approach can't be made to work because it cannot detect changes to the attributes. -To make the ``lru_cache`` approach work when the *station_id* is mutable, -the class needs to define the :meth:`~object.__eq__` and :meth:`~object.__hash__` -methods so that 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 ======= diff --git a/Doc/faq/windows.rst b/Doc/faq/windows.rst index c0c92fdbbc84d1..7f6170f8ade99e 100644 --- a/Doc/faq/windows.rst +++ b/Doc/faq/windows.rst @@ -11,6 +11,7 @@ Python on Windows FAQ .. only:: html .. contents:: + :local: .. XXX need review for Python 3. XXX need review for Windows Vista/Seven? diff --git a/Doc/glossary.rst b/Doc/glossary.rst index a31bd53f06294d..f0934dc2e13877 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -29,9 +29,10 @@ Glossary Abstract base classes complement :term:`duck-typing` by providing a way to define interfaces when other techniques like :func:`hasattr` would be clumsy or subtly wrong (for example with - :ref:`magic methods `). ABCs introduce virtual - subclasses, which are classes that don't inherit from a class but are - still recognized by :func:`isinstance` and :func:`issubclass`; see the + :ref:`magic methods `). ABCs introduce + :term:`virtual subclasses `, which are classes that + don't inherit from a class but are still recognized by + :func:`isinstance` and :func:`issubclass`; see the :mod:`abc` module documentation. Python comes with many built-in ABCs for data structures (in the :mod:`collections.abc` module), numbers (in the :mod:`numbers` module), streams (in the :mod:`io` module), import finders @@ -1727,6 +1728,12 @@ Glossary A computer defined entirely in software. Python's virtual machine executes the :term:`bytecode` emitted by the bytecode compiler. + virtual subclass + A class that :func:`issubclass` and :func:`isinstance` treat as a + subclass of an :term:`abstract base class` even though it does not + inherit from it. A class is made a virtual subclass by registering + it with the ABC's :meth:`~abc.ABCMeta.register` method. + walrus operator A light-hearted way to refer to the :ref:`assignment expression ` operator ``:=`` because it looks a bit like a diff --git a/Doc/howto/abi3t-migration.rst b/Doc/howto/abi3t-migration.rst index 5faf05b89d388f..afa3d255306292 100644 --- a/Doc/howto/abi3t-migration.rst +++ b/Doc/howto/abi3t-migration.rst @@ -256,7 +256,7 @@ See the :ref:`PyInit documentation ` for related information. The function references a ``PyModuleDef`` object (```` in the code above). Its definition should be similar to the following, with different values -and perhaps some fields unnnamed or left out: +and perhaps some fields unnamed or left out: .. code-block:: :class: bad diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst index 801d76dd80aabf..3962b3e3467a45 100644 --- a/Doc/howto/descriptor.rst +++ b/Doc/howto/descriptor.rst @@ -1678,7 +1678,7 @@ variables: def __new__(mcls, clsname, bases, mapping, **kwargs): 'Emulate type_new() in Objects/typeobject.c' - # type_new() calls PyTypeReady() which calls add_methods() + # type_new() calls PyType_Ready() which calls add_methods() slot_names = mapping.get('slot_names', []) for offset, name in enumerate(slot_names): mapping[name] = Member(name, clsname, offset) diff --git a/Doc/howto/instrumentation.rst b/Doc/howto/instrumentation.rst index c7ce5785047a6d..e0d08b19db7a88 100644 --- a/Doc/howto/instrumentation.rst +++ b/Doc/howto/instrumentation.rst @@ -430,7 +430,7 @@ Here is a tapset file, based on a non-shared build of CPython: /* Provide a higher-level wrapping around the function__entry and function__return markers: - \*/ + */ probe python.function.entry = process("python").mark("function__entry") { filename = user_string($arg1); @@ -498,7 +498,7 @@ frames, each second, across the whole system: } probe timer.ms(1000) { - printf("\033[2J\033[1;1H") /* clear screen \*/ + printf("\033[2J\033[1;1H") /* clear screen */ printf("%6s %80s %6s %30s %6s\n", "PID", "FILENAME", "LINE", "FUNCTION", "CALLS") foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) { diff --git a/Doc/howto/isolating-extensions.rst b/Doc/howto/isolating-extensions.rst index 302c15528d5ef3..fcf2abb8157fa4 100644 --- a/Doc/howto/isolating-extensions.rst +++ b/Doc/howto/isolating-extensions.rst @@ -557,9 +557,9 @@ For example:: {"example_method", (PyCFunction)(void(*)(void))example_method, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, - example_method_doc} + example_method_doc}, {NULL}, - } + }; Module State Access from Slot Methods, Getters and Setters diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst index 687a0c516f6717..d8a177020b921b 100644 --- a/Doc/howto/logging-cookbook.rst +++ b/Doc/howto/logging-cookbook.rst @@ -330,11 +330,10 @@ Suppose you configure logging with the following JSON: } } -This configuration does *almost* what we want, except that ``sys.stdout`` would show messages -of severity ``ERROR`` and only events of this severity and higher will be tracked -as well as ``INFO`` and ``WARNING`` messages. To prevent this, we can set up a filter which -excludes those messages and add it to the relevant handler. This can be configured by -adding a ``filters`` section parallel to ``formatters`` and ``handlers``: +This configuration does *almost* what we want, except that ``sys.stdout`` would +also show messages of severity ``ERROR`` and above. To exclude those messages, +we can add a filter to the stdout handler. This can be configured by adding a +``filters`` section parallel to ``formatters`` and ``handlers``: .. code-block:: json @@ -562,9 +561,9 @@ One solution is to use a two-part approach. For the first part, attach only a :class:`QueueHandler` to those loggers which are accessed from performance-critical threads. They simply write to their queue, which can be sized to a large enough capacity or initialized with no upper bound to their -size. The write to the queue will typically be accepted quickly, though you -will probably need to catch the :exc:`queue.Full` exception as a precaution -in your code. If you are a library developer who has performance-critical +size. The write to the queue will typically be accepted quickly. If a bounded +queue is full, :class:`QueueHandler` calls :meth:`~logging.Handler.handleError` +with the record. If you are a library developer who has performance-critical threads in their code, be sure to document this (together with a suggestion to attach only ``QueueHandlers`` to your loggers) for the benefit of other developers who will use your code. @@ -1085,7 +1084,7 @@ each request is handled by a thread: # A dummy set of requests which will be used in the simulation - we'll just pick # from this list randomly. Note that all GET requests are from 192.168.2.XXX - # addresses, whereas POST requests are from 192.16.3.XXX addresses. Three users + # addresses, whereas POST requests are from 192.168.3.XXX addresses. Three users # are represented in the sample requests. REQUESTS = [ @@ -1608,8 +1607,8 @@ application: The most current file is always :file:`logging_rotatingfile_example.out`, and each time it reaches the size limit it is renamed with the suffix -``.1``. Each of the existing backup files is renamed to increment the suffix -(``.1`` becomes ``.2``, etc.) and the ``.6`` file is erased. +``.1``. Before this happens, the oldest backup (``.5``), if present, is +deleted and the remaining backups are renamed (``.4`` becomes ``.5``, etc.). Obviously this example sets the log length much too small as an extreme example. You would want to set *maxBytes* to an appropriate value. @@ -1801,7 +1800,7 @@ Before Python 3.2, there were only two places where this creation was done: * :func:`makeLogRecord`, which is called with a dictionary containing attributes to be added to the LogRecord. This is typically invoked when a suitable dictionary has been received over the network (e.g. in pickle form - via a :class:`~handlers.SocketHandler`, or in JSON form via an + via a :class:`~handlers.SocketHandler`, or as URL-encoded data via an :class:`~handlers.HTTPHandler`). This has usually meant that if you need to do anything special with a @@ -1827,11 +1826,7 @@ do simply by adding new packages or modules and doing :: logger = logging.getLogger(__name__) -at module level). It's probably one too many things to think about. Developers -could also add the filter to a :class:`~logging.NullHandler` attached to their -top-level logger, but this would not be invoked if an application developer -attached a handler to a lower-level library logger --- so output from that -handler would not reflect the intentions of the library developer. +at module level). It's probably one too many things to think about. In Python 3.2 and later, :class:`~logging.LogRecord` creation is done through a factory, which you can specify. The factory is just a callable you can set with @@ -1913,15 +1908,15 @@ You can also subclass :class:`QueueListener` to get messages from other kinds of queues, for example a ZeroMQ 'subscribe' socket. Here's an example:: class ZeroMQSocketListener(QueueListener): - def __init__(self, uri, /, *handlers, **kwargs): - self.ctx = kwargs.get('ctx') or zmq.Context() + def __init__(self, uri, /, *handlers, ctx=None, **kwargs): + self.ctx = ctx or zmq.Context() socket = zmq.Socket(self.ctx, zmq.SUB) socket.setsockopt_string(zmq.SUBSCRIBE, '') # subscribe to everything socket.connect(uri) super().__init__(socket, *handlers, **kwargs) - def dequeue(self): - msg = self.queue.recv_json() + def dequeue(self, block): + msg = self.queue.recv_json(flags=0 if block else zmq.NOBLOCK) return logging.makeLogRecord(msg) .. _pynng-handlers: @@ -2245,7 +2240,7 @@ works:: import logging.config import logging.handlers from multiprocessing import Process, Queue, Event, current_process - import os + from multiprocessing import get_start_method import random import time @@ -2281,13 +2276,8 @@ works:: logging.config.dictConfig(config) listener = logging.handlers.QueueListener(q, MyHandler()) listener.start() - if os.name == 'posix': - # On POSIX, the setup logger will have been configured in the - # parent process, but should have been disabled following the - # dictConfig call. - # On Windows, since fork isn't used, the setup logger won't - # exist in the child, so it would be created and the message - # would appear - hence the "if posix" clause. + if get_start_method() == 'fork': + # dictConfig disables the setup logger inherited from the parent. logger = logging.getLogger('setup') logger.critical('Should not appear, because of disabled logger ...') stop_event.wait() @@ -2312,13 +2302,8 @@ works:: logging.CRITICAL] loggers = ['foo', 'foo.bar', 'foo.bar.baz', 'spam', 'spam.ham', 'spam.ham.eggs'] - if os.name == 'posix': - # On POSIX, the setup logger will have been configured in the - # parent process, but should have been disabled following the - # dictConfig call. - # On Windows, since fork isn't used, the setup logger won't - # exist in the child, so it would be created and the message - # would appear - hence the "if posix" clause. + if get_start_method() == 'fork': + # dictConfig disables the setup logger inherited from the parent. logger = logging.getLogger('setup') logger.critical('Should not appear, because of disabled logger ...') for i in range(100): @@ -2345,9 +2330,7 @@ works:: } # The worker process configuration is just a QueueHandler attached to the # root logger, which allows all messages to be sent to the queue. - # We disable existing loggers to disable the "setup" logger used in the - # parent process. This is needed on POSIX because the logger will - # be there in the child following a fork(). + # Disable the parent's "setup" logger if it was inherited via fork. config_worker = { 'version': 1, 'disable_existing_loggers': True, @@ -2365,9 +2348,7 @@ works:: # The listener process configuration shows that the full flexibility of # logging configuration is available to dispatch events to handlers however # you want. - # We disable existing loggers to disable the "setup" logger used in the - # parent process. This is needed on POSIX because the logger will - # be there in the child following a fork(). + # Disable the parent's "setup" logger if it was inherited via fork. config_listener = { 'version': 1, 'disable_existing_loggers': True, @@ -4077,11 +4058,9 @@ How to uniformly handle newlines in logging output Usually, messages that are logged (say to console or file) consist of a single line of text. However, sometimes there is a need to handle messages with -multiple lines - whether because a logging format string contains newlines, or -logged data contains newlines. If you want to handle such messages uniformly, so -that each line in the logged message appears uniformly formatted as if it was -logged separately, you can do this using a handler mixin, as in the following -snippet: +multiple lines because the message or its arguments contain newlines. +To format each line as if it were logged separately, you can use a handler +mixin, as in the following snippet: .. code-block:: python diff --git a/Doc/howto/logging.rst b/Doc/howto/logging.rst index c8ce0df9e937f8..584f0901b8a69f 100644 --- a/Doc/howto/logging.rst +++ b/Doc/howto/logging.rst @@ -146,13 +146,6 @@ interpreter, and don't just continue from the session described above:: logger.warning('And this, too') logger.error('And non-ASCII stuff, too, like Øresund and Malmö') -.. versionchanged:: 3.9 - The *encoding* argument was added. In earlier Python versions, or if not - specified, the encoding used is the default value used by :func:`open`. While - not shown in the above example, an *errors* argument can also now be passed, - which determines how encoding errors are handled. For available values and - the default, see the documentation for :func:`open`. - And now if we open the file and look at what we have, we should find the log messages: @@ -465,10 +458,9 @@ With the logger object configured, the following methods create log messages: a message and a level that corresponds to their respective method names. The message is actually a format string, which may contain the standard string substitution syntax of ``%s``, ``%d``, ``%f``, and so on. The - rest of their arguments is a list of objects that correspond with the - substitution fields in the message. With regard to ``**kwargs``, the - logging methods care only about a keyword of ``exc_info`` and use it to - determine whether to log exception information. + remaining positional arguments correspond to the substitution fields in the + message. These methods also accept the keyword arguments *exc_info*, *extra*, + *stack_info*, and *stacklevel*, described in :meth:`Logger.debug`. * :meth:`Logger.exception` creates a log message similar to :meth:`Logger.error`. The difference is that :meth:`Logger.exception` dumps a @@ -1006,9 +998,9 @@ Logged messages are formatted for presentation through instances of the use with the % operator and a dictionary. For formatting multiple messages in a batch, instances of -:class:`BufferingFormatter` can be used. In addition to the format -string (which is applied to each message in the batch), there is provision for -header and trailer format strings. +:class:`BufferingFormatter` can be used. A line formatter formats each record +in the batch. To add a header or footer, override +:meth:`~BufferingFormatter.formatHeader` or :meth:`~BufferingFormatter.formatFooter`. When filtering based on logger level and/or handler level is not enough, instances of :class:`Filter` can be added to both :class:`Logger` and @@ -1059,10 +1051,7 @@ In the preceding sections and examples, it has been assumed that the message passed when logging the event is a string. However, this is not the only possibility. You can pass an arbitrary object as a message, and its :meth:`~object.__str__` method will be called when the logging system needs to -convert it to a string representation. In fact, if you want to, you can avoid -computing a string representation altogether - for example, the -:class:`~handlers.SocketHandler` emits an event by pickling it and sending it -over the wire. +convert it to a string representation. Optimization diff --git a/Doc/library/annotationlib.rst b/Doc/library/annotationlib.rst index 5b22353ea08e64..1755a6582bf957 100644 --- a/Doc/library/annotationlib.rst +++ b/Doc/library/annotationlib.rst @@ -581,7 +581,7 @@ Or used as the annotate function for an object: >>> class C: ... pass >>> C.__annotate__ = Annotate() - >>> get_annotations(Annotate(), format=Format.STRING) + >>> get_annotations(C, format=Format.STRING) {'x': 'MyType'} diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 02d6940ad06d2c..0c69c00950c5b5 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -746,11 +746,11 @@ the upper-cased name. For example:: >>> parser.parse_args(['--foo-bar', 'FOO-BAR']) Namespace(foo_bar='FOO-BAR') >>> parser.print_help() - usage: [-h] [--foo-bar FOO-BAR] + usage: [-h] [--foo-bar FOO_BAR] optional arguments: -h, --help show this help message and exit - --foo-bar FOO-BAR + --foo-bar FOO_BAR .. _action: @@ -977,6 +977,53 @@ See also :ref:`specifying-ambiguous-arguments`. The supported values are: usage: PROG [-h] foo [foo ...] PROG: error: the following arguments are required: foo +.. _argparse.REMAINDER: + +* ``argparse.REMAINDER``. Remaining command-line arguments are gathered into a + list, including strings that look like options. This is useful for forwarding + arguments to another command. A common pattern is a final positional argument + after a command name; options for the outer parser must precede that command:: + + >>> parser = argparse.ArgumentParser() + >>> parser.add_argument('--foo') + >>> parser.add_argument('command') + >>> parser.add_argument('args', nargs=argparse.REMAINDER) + >>> parser.parse_args('--foo B cmd --foo C'.split()) + Namespace(foo='B', command='cmd', args=['--foo', 'C']) + >>> parser.parse_args(['cmd']) + Namespace(foo=None, command='cmd', args=[]) + + When the remainder is the first positional argument, leading options are still + parsed normally. It starts with the first positional value, or with ``--``. + In a subparser, the subcommand name has already been consumed by the parent + parser and does not start the remainder. Any ``--`` consumed by the remainder + is retained:: + + >>> parser = argparse.ArgumentParser() + >>> parser.add_argument('args', nargs=argparse.REMAINDER) + >>> parser.parse_args(['--', '--foo', 'B']) + Namespace(args=['--', '--foo', 'B']) + + For an optional argument, supply the option separately from its values:: + + >>> parser = argparse.ArgumentParser() + >>> parser.add_argument('--forward', nargs=argparse.REMAINDER) + >>> parser.parse_args('--forward --foo B --'.split()) + Namespace(forward=['--foo', 'B', '--']) + >>> parser.parse_args(['--forward']) + Namespace(forward=[]) + + An attached value such as ``--forward=value`` supplies only that value; + subsequent arguments are parsed normally. + + .. versionchanged:: 3.15 + Optional arguments with ``nargs=argparse.REMAINDER`` consume ``--`` and + subsequent arguments instead of stopping at ``--``. + + With the default ``'store'`` action, an empty positional remainder ignores + default_ and produces ``[]``. The choices_ restriction is not applied to the + remainder. + If the ``nargs`` keyword argument is not provided, the number of arguments consumed is determined by the action_. Generally this means a single command-line argument will be consumed and a single item (not a list) will be produced. @@ -1375,7 +1422,7 @@ behavior:: >>> parser.parse_args('-f 1 -q 2 -x 3'.split()) Namespace(foo_bar='1', quz='2', x='3') >>> parser.parse_args('--foo 1 -quz 2 -y 3'.split()) - Namespace(foo_bar='1', quz='2', x='2') + Namespace(foo_bar='1', quz='2', x='3') ``dest`` allows a custom attribute name to be provided:: @@ -2088,7 +2135,7 @@ Argument groups to create instances. Only the methods documented here are part of the public API; other attributes and methods are implementation details. - .. versionadded:: next + .. versionadded:: 3.16 The private ``_ArgumentGroup`` name remains as an alias. @@ -2165,7 +2212,7 @@ Mutual exclusion Only the ``add_argument`` method documented here is part of the public API; other attributes and methods are implementation details. - .. versionadded:: next + .. versionadded:: 3.16 The private ``_MutuallyExclusiveGroup`` name remains as an alias. @@ -2334,8 +2381,9 @@ Intermixed parsing These parsers do not support all the :mod:`!argparse` features, and will raise exceptions if unsupported features are used. In particular, subparsers, - and mutually exclusive groups that include both - optionals and positionals are not supported. + positional arguments with + :ref:`nargs=argparse.REMAINDER `, and mutually exclusive + groups that include both optionals and positionals are not supported. The following example shows the difference between :meth:`~ArgumentParser.parse_known_args` and diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index b847d73ae09b4d..10feca3203aec5 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -2029,7 +2029,7 @@ Display the current date with call_later() An example of a callback displaying the current date every second. The callback uses the :meth:`loop.call_later` method to reschedule itself -after 5 seconds, and then stops the event loop:: +after 1 second, and eventually stops the event loop:: import asyncio import datetime as dt diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 6df87d4d71a365..bc79927b985627 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -666,6 +666,10 @@ Eager task factory changes to existing applications. For example, the application's task execution order is likely to change. + During the eager step, call graph introspection reports the creating + task as an awaiter, even for a plain :func:`create_task` call. + This relationship is removed when the eager step finishes. + .. versionadded:: 3.12 .. function:: create_eager_task_factory(custom_task_constructor) diff --git a/Doc/library/binascii.rst b/Doc/library/binascii.rst index be9ad897bcf2ee..a725df99fdda2a 100644 --- a/Doc/library/binascii.rst +++ b/Doc/library/binascii.rst @@ -291,7 +291,7 @@ The :mod:`!binascii` module defines the following functions: If the optional argument *strip_ws* is true, trailing whitespace is stripped from each line, as required by :rfc:`2045`. - .. versionchanged:: next + .. versionchanged:: 3.16 Added the *strip_ws* parameter. @@ -301,11 +301,11 @@ The :mod:`!binascii` module defines the following functions: encoding. The return value is the converted line(s). If the optional argument *quotetabs* is present and true, all tabs and spaces will be encoded. If the optional argument *istext* is present and true, newlines are not encoded but - trailing whitespace will be encoded. If the optional argument *header* is - present and true, spaces will be encoded as underscores per :rfc:`1522`. If the - optional argument *header* is present and false, newline characters will be - encoded as well; otherwise linefeed conversion might corrupt the binary data - stream. + trailing whitespace will be encoded. If the optional argument *istext* is + present and false, newline characters will be encoded as well; otherwise + linefeed conversion might corrupt the binary data stream. If the optional + argument *header* is present and true, spaces will be encoded as underscores + per :rfc:`1522`. .. function:: crc_hqx(data, value) diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index 18e0319d91fad9..69578e4a4846ce 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -162,10 +162,10 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is final newline. - .. method:: formatday(theday, weekday, width) + .. method:: formatday(day, weekday, width) Return a string representing a single day formatted with the given *width*. - If *theday* is ``0``, return a string of spaces of + If *day* is ``0``, return a string of spaces of the specified width, representing an empty day. The *weekday* parameter is unused. @@ -177,10 +177,10 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is :meth:`setfirstweekday` method. - .. method:: formatweekday(weekday, width) + .. method:: formatweekday(day, width) Return a string representing the name of a single weekday formatted to - the specified *width*. The *weekday* parameter is an integer representing + the specified *width*. The *day* parameter is an integer representing the day of the week, where ``0`` is Monday and ``6`` is Sunday. diff --git a/Doc/library/cgi.rst b/Doc/library/cgi.rst index f9108fa954a906..6e4d277fcefe75 100644 --- a/Doc/library/cgi.rst +++ b/Doc/library/cgi.rst @@ -12,8 +12,8 @@ It was :ref:`removed in Python 3.13 ` after being deprecated in Python 3.11. The removal was decided in :pep:`594`. A fork of the module on PyPI can be used instead: :pypi:`legacy-cgi`. -This is a copy of the cgi module, no longer maintained or supported by the core -Python team. +This package contains copies of the :mod:`!cgi` and :mod:`!cgitb` modules, +which are no longer maintained or supported by the core Python team. The last version of Python that provided the :mod:`!cgi` module was `Python 3.12 `_. diff --git a/Doc/library/cgitb.rst b/Doc/library/cgitb.rst index fc646aa4c48acd..081a4f561bd366 100644 --- a/Doc/library/cgitb.rst +++ b/Doc/library/cgitb.rst @@ -12,8 +12,8 @@ It was :ref:`removed in Python 3.13 ` after being deprecated in Python 3.11. The removal was decided in :pep:`594`. A fork of the module on PyPI can now be used instead: :pypi:`legacy-cgi`. -This is a copy of the cgi module, no longer maintained or supported by the core -Python team. +This package contains copies of the :mod:`!cgi` and :mod:`!cgitb` modules, +which are no longer maintained or supported by the core Python team. The last version of Python that provided the :mod:`!cgitb` module was `Python 3.12 `_. diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index ca4f5f6dfea4f4..f19407818528e2 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -1642,7 +1642,7 @@ This module implements the following exception: :synopsis: Internationalized Domain Names implementation This module implements :rfc:`3490` (Internationalized Domain Names in -Applications) and :rfc:`3492` (Nameprep: A Stringprep Profile for +Applications) and :rfc:`3491` (Nameprep: A Stringprep Profile for Internationalized Domain Names (IDN)). It builds upon the ``punycode`` encoding and :mod:`stringprep`. diff --git a/Doc/library/concurrent.interpreters.rst b/Doc/library/concurrent.interpreters.rst index 058e59b32fd15a..db6b8c444e0287 100644 --- a/Doc/library/concurrent.interpreters.rst +++ b/Doc/library/concurrent.interpreters.rst @@ -250,7 +250,7 @@ Interpreter objects with interpreters.create() as interp: interp.exec('print("hello")') - .. versionchanged:: next + .. versionchanged:: 3.16 Added support for using :class:`!Interpreter` objects in a :term:`context manager`. diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 6f3524e0253bca..b62bd94893c285 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1117,7 +1117,7 @@ an object passed for a pointer argument (see ``c_int`` instance is passed by reference, and one whose :attr:`!_as_parameter_` is a ``c_int`` array is accepted as well. -.. versionchanged:: next +.. versionchanged:: 3.16 Objects with an :attr:`!_as_parameter_` attribute now receive the same implicit conversions for pointer arguments as the object they wrap. @@ -1358,7 +1358,7 @@ the simple types: same requirement C itself imposes. Returning a pointer to memory owned by a temporary Python object will leave the C caller with a dangling pointer. -.. versionchanged:: next +.. versionchanged:: 3.16 Callback functions can now return structures and unions. .. _ctypes-accessing-values-exported-from-dlls: diff --git a/Doc/library/curses.rst b/Doc/library/curses.rst index ac34bf2b8ed3ff..740374ee743478 100644 --- a/Doc/library/curses.rst +++ b/Doc/library/curses.rst @@ -1032,10 +1032,15 @@ Terminfo database Instantiate the bytes object *str* with the supplied parameters, where *str* should be a parameterized byte string obtained from the terminfo database. For example, ``tparm(tigetstr("cup"), 5, 3)`` could result in ``b'\033[6;4H'``, the exact - result depending on terminal type. Up to nine integer parameters may be supplied. + result depending on terminal type. Up to nine parameters may be supplied. + They are integers, except in a few cases, such as ``Ms`` of xterm, + where they must be bytes objects. :func:`setupterm` (or :func:`initscr`) must be called first. + .. versionchanged:: next + Added support of bytes parameters. + .. function:: putp(str) Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 552626cbb8ac88..20d6f0cb9d4abf 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2480,7 +2480,7 @@ where historical changes have been made to civil time. The *name* passed to the constructor, or ``None`` if no name was passed. - .. versionadded:: next + .. versionadded:: 3.16 Class attributes: @@ -2724,7 +2724,7 @@ differences between platforms in handling of unsupported format specifiers. ``%D``, ``%F``, ``%n``, ``%t``, and ``%:z`` were added for :meth:`~.datetime.strptime`. -.. versionadded:: next +.. versionadded:: 3.16 ``%Nf`` was added. diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index 5a93a328d48728..1b5f0c92faf02c 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -621,10 +621,14 @@ operations on it as if it was a Python list. The top of the stack corresponds to .. opcode:: POP_ITER - Removes the iterator from the top of the stack. + Removes the iterator and the value pushed with it by :opcode:`GET_ITER` + from the top of the stack. .. versionadded:: 3.14 + .. versionchanged:: 3.15 + Two values are now removed. + .. opcode:: POP_TOP @@ -644,11 +648,15 @@ operations on it as if it was a Python list. The top of the stack corresponds to .. opcode:: END_SEND - Implements ``del STACK[-2]``. + Implements ``del STACK[-3:-1]``. Used to clean up when a generator exits. .. versionadded:: 3.12 + .. versionchanged:: 3.15 + The receiver and the value pushed with it by :opcode:`GET_ITER` are + removed, instead of only the receiver. + .. opcode:: COPY (i) @@ -712,17 +720,23 @@ result back on the stack. Implements ``STACK[-1] = ~STACK[-1]``. -.. opcode:: GET_ITER +.. opcode:: GET_ITER (yield_from) - Implements ``STACK[-1] = iter(STACK[-1])``. + Replaces ``STACK[-1]`` with an iterator for it and pushes a second value, + which is used by :opcode:`FOR_ITER`, :opcode:`SEND` and :opcode:`POP_ITER`. + For some built-in sequence types, such as :class:`list` and :class:`tuple`, + the object itself is left on the stack as a "virtual iterator" and the + index to start iterating from is pushed. Otherwise, implements + ``STACK[-1] = iter(STACK[-1])`` and pushes ``NULL``. -.. opcode:: GET_YIELD_FROM_ITER + If *yield_from* is non-zero, the instruction is used for ``yield from``: + a :term:`coroutine` object is left as is, unless the current code is + a regular generator, in which case :exc:`TypeError` is raised. - If ``STACK[-1]`` is a :term:`generator iterator` or :term:`coroutine` object - it is left as is. Otherwise, implements ``STACK[-1] = iter(STACK[-1])``. - - .. versionadded:: 3.5 + .. versionchanged:: 3.15 + A second value is now pushed after the iterator. The *yield_from* + argument was added, replacing the ``GET_YIELD_FROM_ITER`` instruction. .. opcode:: TO_BOOL @@ -803,7 +817,8 @@ not have to be) the original ``STACK[-2]``. .. opcode:: GET_AWAITABLE (where) - Implements ``STACK[-1] = get_awaitable(STACK[-1])``, where ``get_awaitable(o)`` + Replaces ``STACK[-1]`` with ``get_awaitable(STACK[-1])``, ``NULL`` and + ``None``, ready for :opcode:`SEND`. ``get_awaitable(o)`` returns ``o`` if ``o`` is a coroutine object or a generator object with the :data:`~inspect.CO_ITERABLE_COROUTINE` flag, or resolves ``o.__await__``. @@ -819,6 +834,10 @@ not have to be) the original ``STACK[-2]``. .. versionchanged:: 3.11 Previously, this instruction did not have an oparg. + .. versionchanged:: next + Also pushes ``NULL`` and ``None``, which were previously pushed by + separate instructions. + .. opcode:: GET_AITER @@ -832,11 +851,16 @@ not have to be) the original ``STACK[-2]``. .. opcode:: GET_ANEXT - Implement ``STACK.append(get_awaitable(STACK[-1].__anext__()))`` to the stack. + Implement ``STACK.append(get_awaitable(STACK[-1].__anext__()))`` to the stack, + then push ``NULL`` and ``None``, ready for :opcode:`SEND`. See ``GET_AWAITABLE`` for details about ``get_awaitable``. .. versionadded:: 3.5 + .. versionchanged:: next + Also pushes ``NULL`` and ``None``, which were previously pushed by + separate instructions. + .. opcode:: END_ASYNC_FOR @@ -1425,7 +1449,10 @@ iterations of the loop. .. opcode:: FOR_ITER (delta) - ``STACK[-1]`` is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. + ``STACK[-2]`` is an :term:`iterator` (or a virtual iterator, see + :opcode:`GET_ITER`) and ``STACK[-1]`` is the value pushed with it by + :opcode:`GET_ITER`. Get the next item, calling the iterator's + :meth:`~iterator.__next__` method if needed. If this yields a new value, push it on the stack (leaving the iterator below it). If the iterator indicates it is :term:`exhausted` then the byte code counter is incremented by *delta*. @@ -1433,6 +1460,10 @@ iterations of the loop. .. versionchanged:: 3.12 Up until 3.11 the iterator was popped when it was exhausted. + .. versionchanged:: 3.15 + The iterator is now followed on the stack by the value pushed with it + by :opcode:`GET_ITER`. + .. opcode:: LOAD_GLOBAL (namei) Loads the global named ``co_names[namei>>1]`` onto the stack. @@ -1810,8 +1841,10 @@ iterations of the loop. .. opcode:: SEND (delta) - Equivalent to ``STACK[-1] = STACK[-2].send(STACK[-1])``. Used in ``yield from`` - and ``await`` statements. + Equivalent to ``STACK[-1] = STACK[-3].send(STACK[-1])``. Used in ``yield from`` + and ``await`` statements. ``STACK[-2]`` is the value pushed with the + receiver by :opcode:`GET_ITER`; if the receiver is a virtual iterator, + it is advanced as by :opcode:`FOR_ITER`. If the call raises :exc:`StopIteration`, pop the top value from the stack, push the exception's ``value`` attribute, and increment the bytecode counter @@ -1819,6 +1852,10 @@ iterations of the loop. .. versionadded:: 3.11 + .. versionchanged:: 3.15 + The receiver is now at ``STACK[-3]``, followed by the value pushed with + it by :opcode:`GET_ITER`. + .. opcode:: HAVE_ARGUMENT diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst index 894929ac99634a..de4ac60101a273 100644 --- a/Doc/library/doctest.rst +++ b/Doc/library/doctest.rst @@ -1642,7 +1642,7 @@ DocTestRunner objects verbosity is not specified, then the :class:`DocTestRunner`'s verbosity is used. - :class:`DocTestParser` has the following attributes: + :class:`DocTestRunner` has the following attributes: .. attribute:: tries diff --git a/Doc/library/fileinput.rst b/Doc/library/fileinput.rst index 24132ee39f130c..b175591ddea2b6 100644 --- a/Doc/library/fileinput.rst +++ b/Doc/library/fileinput.rst @@ -195,7 +195,7 @@ backup file, :exc:`FileExistsError` is raised. If the output file cannot be created, the backup file is kept. In-place filtering of files in a directory that other, untrusted users can modify is not supported. -.. versionchanged:: next +.. versionchanged:: 3.16 The output file is now created exclusively, and the backup file is no longer deleted if the output file cannot be created. diff --git a/Doc/library/fnmatch.rst b/Doc/library/fnmatch.rst index 2b0163bd4ce353..4f1486fefe98a0 100644 --- a/Doc/library/fnmatch.rst +++ b/Doc/library/fnmatch.rst @@ -46,8 +46,10 @@ module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses a period are not special for this module, and are matched by the ``*`` and ``?`` patterns. -Unless stated otherwise, "filename string" and "pattern string" either refer to -:class:`str` or ``ISO-8859-1`` encoded :class:`bytes` objects. Note that the +Unless stated otherwise, "filename string" and "pattern string" refer to +:class:`str` or ``ISO-8859-1`` encoded :class:`bytes` objects. +In addition to filename strings, :func:`.filter` and :func:`.filterfalse` +accept :term:`path-like objects ` in *names*. Note that the functions documented below do not allow to mix a :class:`!bytes` pattern with a :class:`!str` filename, and vice-versa. @@ -90,6 +92,9 @@ functions: :func:`~fnmatch.fnmatch`, :func:`fnmatchcase`, :func:`.filter`, It is the same as ``[n for n in names if fnmatch(n, pat)]``, but implemented more efficiently. + .. versionchanged:: next + Added support for path-like objects in *names*. + .. function:: filterfalse(names, pat) @@ -100,6 +105,9 @@ functions: :func:`~fnmatch.fnmatch`, :func:`fnmatchcase`, :func:`.filter`, .. versionadded:: 3.14 + .. versionchanged:: next + Added support for path-like objects in *names*. + .. function:: translate(pat) diff --git a/Doc/library/ftplib.rst b/Doc/library/ftplib.rst index a0a59868a5ace9..15041a125ba92a 100644 --- a/Doc/library/ftplib.rst +++ b/Doc/library/ftplib.rst @@ -92,7 +92,7 @@ FTP objects :param str user: |param_doc_user| - If given, :code:`login(host, passwd, acct)` is implicitly called + If given, :code:`login(user, passwd, acct)` is implicitly called by the constructor. :param str passwd: @@ -464,7 +464,7 @@ FTP_TLS objects :param str user: |param_doc_user| - If given, :code:`login(host, passwd, acct)` is implicitly called + If given, :code:`login(user, passwd, acct)` is implicitly called by the constructor. :param str passwd: diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 1bbe30158749c4..2ce07976c39531 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -339,7 +339,7 @@ The :mod:`!functools` module defines the following functions: .. versionadded:: 3.14 - .. versionchanged:: next + .. versionchanged:: 3.16 ``Placeholder`` is now a :class:`sentinel`. .. function:: partial(func, /, *args, **keywords) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 3a050873b79b9d..cd55deccbe34c7 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -307,7 +307,7 @@ Once executed the :mod:`!gzip` module keeps the input file(s). Add a new command line interface with a usage. By default, when you will execute the CLI, the default compression level is 6. -.. versionchanged:: next +.. versionchanged:: 3.16 An existing output file is removed and created anew instead of being opened for writing, so a symbolic link at that name is replaced rather diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst index ab557f1805e104..50a11ff18ab9bb 100644 --- a/Doc/library/http.client.rst +++ b/Doc/library/http.client.rst @@ -230,6 +230,29 @@ The following exceptions are raised as appropriate: Previously, :exc:`BadStatusLine`\ ``('')`` was raised. +.. exception:: ProxyTunnelError + + A subclass of :exc:`OSError`. Raised when the proxy server answers the + CONNECT request sent for :meth:`HTTPConnection.set_tunnel` with a status + other than 200. + + .. attribute:: code + + The HTTP status code of the proxy's response, such as ``407``. + + .. attribute:: reason + + The reason phrase of the proxy's response. + + .. attribute:: headers + + The headers of the proxy's response, as an + :class:`http.client.HTTPMessage` instance. + + .. versionadded:: next + Previously, :exc:`OSError` was raised. + + The constants defined in this module are: .. data:: HTTP_PORT @@ -259,7 +282,7 @@ HTTPConnection Objects .. method:: HTTPConnection.request(method, url, body=None, headers={}, *, \ - encode_chunked=False) + encode_chunked=False, continue_timeout=2.5) This will send a request to the server using the HTTP request method *method* and the request URI *url*. The provided *url* must be @@ -289,7 +312,7 @@ HTTPConnection Objects but there is a request body, one of those header fields will be added automatically. If *body* is ``None``, the Content-Length header is set to ``0`` for - methods that expect a body (``PUT``, ``POST``, and ``PATCH``). If + methods that expect a body (``PUT``, ``POST``, ``PATCH``, and ``QUERY``). If *body* is a string or a bytes-like object that is not also a :term:`file `, the Content-Length header is set to its length. Any other type of *body* (files @@ -335,11 +358,36 @@ HTTPConnection Objects No attempt is made to determine the Content-Length for file objects. -.. method:: HTTPConnection.getresponse() + .. versionchanged:: next + The *continue_timeout* parameter was added. + + If the headers include ``Expect: 100-Continue`` and *body* is set, the + body will not be sent until either a ``100 Continue`` response is + received from the server or *continue_timeout* seconds have elapsed; if a + response code other than 100 is received, the body will not be sent at all. + + *continue_timeout* value must be greater than zero, otherwise + :exc:`ValueError` is raised. + + .. versionchanged:: next + ``QUERY`` was added to the methods that expect a body. + +.. method:: HTTPConnection.getresponse(support_continue=False) Should be called after a request is sent to get the response from the server. Returns an :class:`HTTPResponse` instance. + By default, ``100 Continue`` responses are skipped and the method returns + the first response with a status other than 100. If *support_continue* + is true, a ``100 Continue`` response is returned to the caller, which must + then send the request body and call :meth:`getresponse` again to obtain + the final response. + + .. note:: + + The whole response must be read before a new request can be sent to + the server. + .. versionchanged:: 3.5 If a :exc:`ConnectionError` or subclass is raised, the :class:`HTTPConnection` object will be ready to reconnect when @@ -349,6 +397,10 @@ HTTPConnection Objects socket. Instead the caller is responsible to call :meth:`close` on the existing connection. + .. versionchanged:: next + Added the *support_continue* parameter. (In prior versions + a ``Continue`` response was always ignored.) + .. method:: HTTPConnection.set_debuglevel(level) @@ -398,6 +450,9 @@ HTTPConnection Objects one will be automatically generated and transmitted if not provided in the headers argument. + .. versionchanged:: next + :exc:`ProxyTunnelError` is raised if the proxy rejects the CONNECT request. + .. method:: HTTPConnection.get_proxy_response_headers() @@ -444,7 +499,8 @@ also send your request step by step, by using the four functions below. .. method:: HTTPConnection.putrequest(method, url, skip_host=False, \ - skip_accept_encoding=False) + skip_accept_encoding=False, \ + continue_timeout=2.5) This should be the first call after the connection to the server has been made. It sends a line to the server consisting of the *method* string, @@ -453,6 +509,14 @@ also send your request step by step, by using the four functions below. additional content encodings), specify *skip_host* or *skip_accept_encoding* with non-False values. + If an ``Expect: 100-Continue`` header is sent, the optional + *continue_timeout* parameter sets how many seconds the client will wait for + a ``100 Continue`` response before sending the message body, It must be + greater than zero, otherwise :exc:`ValueError` is raised. + + .. versionchanged:: next + *continue_timeout* parameter was added. + .. method:: HTTPConnection.putheader(header, argument[, ...]) @@ -466,7 +530,12 @@ also send your request step by step, by using the four functions below. Send a blank line to the server, signalling the end of the headers. The optional *message_body* argument can be used to pass a message body - associated with the request. + associated with the request. If a body is provided and an + ``Expect: 100-Continue`` header has been sent, the body will not be sent + until a ``100 Continue`` response is received from the server; if another + response code is received, the body is not sent at all. If no response + is received within the *continue_timeout* given to :meth:`putrequest`, + the body will be sent regardless. If *encode_chunked* is ``True``, the result of each iteration of *message_body* will be chunk-encoded as specified in :rfc:`7230`, @@ -487,6 +556,12 @@ also send your request step by step, by using the four functions below. .. versionchanged:: 3.6 Added chunked encoding support and the *encode_chunked* parameter. + .. versionchanged:: next + If an ``Expect: 100-Continue`` header has been sent, the body will not be + sent until a ``100 Continue`` response is received from the server, or a + contine_timeout has elapsed. If the response code is not 100, the body + will not be sent at all. + .. method:: HTTPConnection.send(data) @@ -663,6 +738,46 @@ method attribute. Here is an example session that uses the ``PUT`` method:: >>> conn.request("PUT", "/file", BODY) >>> response = conn.getresponse() >>> print(response.status, response.reason) + 200 OK + +Conditional transmission of the body is supported when an ``Expect: 100-Continue`` +header is set. To use this in a simple case, just set the header, and +optionally the time for which the client should wait for a ``100 Continue`` +response before sending the body regardless:: + + >>> import http.client + >>> BODY = "***filecontents***" + >>> conn = http.client.HTTPConnection("localhost", 8080) + >>> conn.request("PUT", "/file", BODY, headers={'Expect': '100-Continue'}, + ... continue_timeout=1.0) + >>> response = conn.getresponse() + >>> # You will not see the '100' response, as it is handled internally + >>> print(response.status, response.reason) + 200, OK + +Here is a more complex example in which we manually check the response and +decide whether to send the body. This may be useful if the body must be +generated by some resource-intensive process which should be skipped if the +server will not accept it. *continue_timeout* does not apply here, so this +should only be used with servers known to support ``Expect: 100-Continue``; +otherwise :meth:`~HTTPConnection.getresponse` may block until the +connection's *timeout* expires. :: + + >>> import http.client + >>> conn = http.client.HTTPConnection("localhost", 8080) + >>> conn.putrequest("PUT", "/file") + >>> conn.putheader('Expect', '100-Continue') + >>> # Assuming you know in advance what the length will be + >>> # If not, you will need to encode it as chunked + >>> conn.putheader('Content-Length', '42') + >>> conn.endheaders() + >>> response = conn.getresponse(support_continue=True) + >>> print(response.status, response.reason) + 100, Continue + >>> BODY = resource_intensive_calculation() + >>> conn.send(BODY) + >>> response = conn.getresponse() + >>> print(response.status, response.reason) 200, OK .. _httpmessage-objects: diff --git a/Doc/library/http.cookies.rst b/Doc/library/http.cookies.rst index 4965c5fc3ba1d8..5ac1a318be80f8 100644 --- a/Doc/library/http.cookies.rst +++ b/Doc/library/http.cookies.rst @@ -163,7 +163,7 @@ Morsel Objects that the cookie was first set in. For this to be accepted by the user agent, you **must** also set ``Secure``. - In addition, it is recommended to use the ``__Host`` prefix when setting + In addition, it is recommended to use the ``__Host-`` prefix when setting partitioned cookies to make them bound to the hostname and not the registrable domain. Read `CHIPS (Cookies Having Independent Partitioned State)`_ @@ -188,6 +188,11 @@ Morsel Objects .. versionchanged:: 3.14 Added support for the :attr:`partitioned` attribute. + .. versionchanged:: next + :meth:`!__setitem__` and :meth:`!__ior__` now raise :exc:`CookieError` if + a value string for any attribute except :attr:`comment` contains the + reserved separator character ``;``. (If :attr:`comment` contains ``;`` + it will be escaped in the output.) .. attribute:: Morsel.value @@ -253,6 +258,10 @@ Morsel Objects .. versionchanged:: 3.5 an error is raised for invalid keys. + .. versionchanged:: next + :exc:`CookieError` is now raised if a value string for any attribute + except :attr:`comment` contains the reserved separator character ``;``. + (If :attr:`comment` contains ``;`` it will be escaped in the output.) .. method:: Morsel.copy(value) @@ -267,6 +276,10 @@ Morsel Objects Raise an error if key is not a valid :rfc:`2109` attribute, otherwise behave the same as :meth:`dict.setdefault`. + .. versionchanged:: next + :exc:`CookieError` is now raised if the value string for any attribute + except :attr:`comment` contains the reserved separator character ``;``. + (If :attr:`comment` contains ``;`` it will be escaped in the output.) .. _cookie-example: diff --git a/Doc/library/http.rst b/Doc/library/http.rst index 43a801416e24f9..934f67436ae5fc 100644 --- a/Doc/library/http.rst +++ b/Doc/library/http.rst @@ -35,7 +35,7 @@ The :mod:`!http` module also defines the following enums that help you work with >>> from http import HTTPStatus >>> HTTPStatus.OK - HTTPStatus.OK + >>> HTTPStatus.OK == 200 True >>> HTTPStatus.OK.value @@ -45,7 +45,7 @@ The :mod:`!http` module also defines the following enums that help you work with >>> HTTPStatus.OK.description 'Request fulfilled, document follows' >>> list(HTTPStatus) - [HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...] + [, , ...] .. _http-status-codes: @@ -194,6 +194,7 @@ Property Indicates that Details , , , + , ] .. _http-methods: @@ -217,4 +218,5 @@ Method Enum Name Details ``OPTIONS`` ``OPTIONS`` HTTP Semantics :rfc:`9110`, Section 9.3.7 ``TRACE`` ``TRACE`` HTTP Semantics :rfc:`9110`, Section 9.3.8 ``PATCH`` ``PATCH`` HTTP/1.1 :rfc:`5789` +``QUERY`` ``QUERY`` The HTTP QUERY Method :rfc:`10008` =========== =================================== ================================================================== diff --git a/Doc/library/io.rst b/Doc/library/io.rst index 09e9e42565a99e..7a74f3c8b2c9d7 100644 --- a/Doc/library/io.rst +++ b/Doc/library/io.rst @@ -205,7 +205,7 @@ High-level module interface def read_text(path, encoding=None): encoding = io.text_encoding(encoding) # stacklevel=2 - with open(path, encoding) as f: + with open(path, encoding=encoding) as f: return f.read() In this example, an :class:`EncodingWarning` is emitted for the caller of @@ -526,6 +526,81 @@ I/O base classes write calls until all unwritten bytes are written. High-level I/O objects like :ref:`binary-io` and :ref:`text-io` implement retry behavior. + +Raw file I/O +^^^^^^^^^^^^ + +.. class:: FileIO(name, mode='r', closefd=True, opener=None) + + A raw binary stream representing an OS-level file containing bytes data. It + inherits from :class:`RawIOBase` and implements its low-level access design. + This means :meth:`~RawIOBase.write` does not guarantee all bytes are written + and :meth:`~RawIOBase.read` may read less bytes than requested even when more + bytes may be present in the underlying file. To get "write all" and + "read at least" behavior, use :ref:`binary-io`. + + The *name* can be one of two things: + + * a character string or :class:`bytes` object representing the path to the + file which will be opened. In this case closefd must be ``True`` (the default) + otherwise an error will be raised. + * an integer representing the number of an existing OS-level file descriptor + to which the resulting :class:`FileIO` object will give access. When the + FileIO object is closed this fd will be closed as well, unless *closefd* + is set to ``False``. + + The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading + (default), writing, exclusive creation or appending. The file will be + created if it doesn't exist when opened for writing or appending; it will be + truncated when opened for writing. :exc:`FileExistsError` will be raised if + it already exists when opened for creating. Opening a file for creating + implies writing, so this mode behaves in a similar way to ``'w'``. Add a + ``'+'`` to the mode to allow simultaneous reading and writing. + + 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 + (*name*, *flags*). *opener* must return an open file descriptor (passing + :mod:`os.open` as *opener* results in functionality similar to passing + ``None``). + + The newly created file is :ref:`non-inheritable `. + + See the :func:`open` built-in function for examples on using the *opener* + parameter. + + .. warning:: + :class:`FileIO` is a low-level I/O object and members, such as + :meth:`~RawIOBase.read` and :meth:`~RawIOBase.write`, need to have their + return values checked explicitly in a retry loop to implement "write all" + and "read at least" behavior. High-level I/O objects :ref:`binary-io` and + :ref:`text-io` implement retry behavior. + + .. versionchanged:: 3.3 + The *opener* parameter was added. + The ``'x'`` mode was added. + + .. versionchanged:: 3.4 + The file is now non-inheritable. + + :class:`FileIO` provides these data attributes in addition to those from + :class:`RawIOBase` and :class:`IOBase`: + + .. attribute:: mode + + The mode as given in the constructor. + + .. attribute:: name + + The file name. This is the file descriptor of the file when no name is + given in the constructor. + + +Buffered streams +^^^^^^^^^^^^^^^^ + +Buffered I/O streams provide a higher-level interface to an I/O device +than raw I/O does. + .. class:: BufferedIOBase Base class for binary streams that support some kind of buffering. @@ -647,80 +722,6 @@ I/O base classes so the implementation should only access *b* during the method call. -Raw file I/O -^^^^^^^^^^^^ - -.. class:: FileIO(name, mode='r', closefd=True, opener=None) - - A raw binary stream representing an OS-level file containing bytes data. It - inherits from :class:`RawIOBase` and implements its low-level access design. - This means :meth:`~RawIOBase.write` does not guarantee all bytes are written - and :meth:`~RawIOBase.read` may read less bytes than requested even when more - bytes may be present in the underlying file. To get "write all" and - "read at least" behavior, use :ref:`binary-io`. - - The *name* can be one of two things: - - * a character string or :class:`bytes` object representing the path to the - file which will be opened. In this case closefd must be ``True`` (the default) - otherwise an error will be raised. - * an integer representing the number of an existing OS-level file descriptor - to which the resulting :class:`FileIO` object will give access. When the - FileIO object is closed this fd will be closed as well, unless *closefd* - is set to ``False``. - - The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading - (default), writing, exclusive creation or appending. The file will be - created if it doesn't exist when opened for writing or appending; it will be - truncated when opened for writing. :exc:`FileExistsError` will be raised if - it already exists when opened for creating. Opening a file for creating - implies writing, so this mode behaves in a similar way to ``'w'``. Add a - ``'+'`` to the mode to allow simultaneous reading and writing. - - 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 - (*name*, *flags*). *opener* must return an open file descriptor (passing - :mod:`os.open` as *opener* results in functionality similar to passing - ``None``). - - The newly created file is :ref:`non-inheritable `. - - See the :func:`open` built-in function for examples on using the *opener* - parameter. - - .. warning:: - :class:`FileIO` is a low-level I/O object and members, such as - :meth:`~RawIOBase.read` and :meth:`~RawIOBase.write`, need to have their - return values checked explicitly in a retry loop to implement "write all" - and "read at least" behavior. High-level I/O objects :ref:`binary-io` and - :ref:`text-io` implement retry behavior. - - .. versionchanged:: 3.3 - The *opener* parameter was added. - The ``'x'`` mode was added. - - .. versionchanged:: 3.4 - The file is now non-inheritable. - - :class:`FileIO` provides these data attributes in addition to those from - :class:`RawIOBase` and :class:`IOBase`: - - .. attribute:: mode - - The mode as given in the constructor. - - .. attribute:: name - - The file name. This is the file descriptor of the file when no name is - given in the constructor. - - -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=b'') A binary stream using an in-memory bytes buffer. It inherits from diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst index 2c7deb40e9044c..92c2bbd5f97b9c 100644 --- a/Doc/library/ipaddress.rst +++ b/Doc/library/ipaddress.rst @@ -739,7 +739,7 @@ dictionaries. Traceback (most recent call last): File "", line 1, in raise ValueError( - ValueError: next prefix must be between 1 and 32 + ValueError: prefix must be between 1 and 32 >>> IPv4Network('255.255.255.0/24').next_network() Traceback (most recent call last): File "", line 1, in @@ -748,6 +748,36 @@ dictionaries. .. versionadded:: 3.16 + .. method:: prev_network(prev_prefix=None) + + Finds the previous closest network of prefix size *prev_prefix*. If + *prev_prefix=None*, then the current network prefix will be used. + Returns a single network object. + + Raises :exc:`ValueError` if *prev_prefix* is out of range or no further + network of the requested size exists. + + >>> IPv4Network('192.0.2.0/24').prev_network() + IPv4Network('192.0.1.0/24') + >>> IPv4Network('192.0.2.0/24').prev_network(prev_prefix=25) + IPv4Network('192.0.1.128/25') + >>> IPv4Network('192.0.2.0/24').prev_network(prev_prefix=23) + IPv4Network('192.0.0.0/23') + >>> IPv4Network('192.0.80.0/22').prev_network(prev_prefix=18) + IPv4Network('192.0.0.0/18') + >>> IPv4Network('192.0.80.0/22').prev_network(prev_prefix=50) + Traceback (most recent call last): + File "", line 1, in + raise ValueError( + ValueError: prefix must be between 1 and 32 + >>> IPv4Network('0.0.0.0/24').prev_network() + Traceback (most recent call last): + File "", line 1, in + raise ValueError( + ValueError: out of address space, cannot make another /24 network + + .. versionadded:: 3.16 + .. class:: IPv6Network(address, strict=True) Construct an IPv6 network definition. *address* can be one of the following: @@ -820,6 +850,7 @@ dictionaries. .. method:: subnet_of(other) .. method:: supernet_of(other) .. method:: next_network(next_prefix=None) + .. method:: prev_network(prev_prefix=None) .. method:: compare_networks(other) Refer to the corresponding attribute documentation in diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index a3d117c1070241..3038843573de3a 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -747,14 +747,14 @@ Formatter Objects .. versionchanged:: 3.3 Previously, the default format was hard-coded as in this example: ``2010-09-06 22:38:15,292`` where the part before the comma is - handled by a strptime format string (``'%Y-%m-%d %H:%M:%S'``), and the - part after the comma is a millisecond value. Because strptime does not + handled by a strftime format string (``'%Y-%m-%d %H:%M:%S'``), and the + part after the comma is a millisecond value. Because strftime does not have a format placeholder for milliseconds, the millisecond value is appended using another format string, ``'%s,%03d'`` --- and both of these format strings have been hardcoded into this method. With the change, these strings are defined as class-level attributes which can be overridden at the instance level when desired. The names of the - attributes are ``default_time_format`` (for the strptime format string) + attributes are ``default_time_format`` (for the strftime format string) and ``default_msec_format`` (for appending the millisecond value). .. versionchanged:: 3.9 @@ -919,7 +919,7 @@ wire). :param exc_info: An exception tuple with the current exception information, as returned by :func:`sys.exc_info`, or ``None`` if no exception information is available. - :type exc_info: tuple[type[BaseException], BaseException, types.TracebackType] | None + :type exc_info: tuple[type[BaseException], BaseException, types.TracebackType | None] | tuple[None, None, None] | None :param func: The name of the function or method from which the logging call was invoked. diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index cd376bd49f43ed..dc45f8401541f4 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -2750,7 +2750,7 @@ multiple connections at the same time. assumed to be the fastest available. See :ref:`multiprocessing-address-formats`. Note that if *family* is ``'AF_UNIX'`` and address is ``None`` then the socket will be created in a - private temporary directory created using :func:`tempfile.mkstemp`. + private temporary directory created using :func:`tempfile.mkdtemp`. If the listener object uses a socket then *backlog* (1 by default) is passed to the :meth:`~socket.socket.listen` method of the socket once it has been diff --git a/Doc/library/optparse.rst b/Doc/library/optparse.rst index 4271e65e142cad..3486c83a657643 100644 --- a/Doc/library/optparse.rst +++ b/Doc/library/optparse.rst @@ -2003,7 +2003,7 @@ To add new types, you need to define your own subclass of :mod:`!optparse`'s (e.g., ``-f``), and ``value`` is the string from the command line that must be checked and converted to your desired type. ``check_mytype()`` should return an object of the hypothetical type ``mytype``. The value returned by - a type-checking function will wind up in the OptionValues instance returned + a type-checking function will wind up in the Values instance returned by :meth:`OptionParser.parse_args`, or be passed to a callback as the ``value`` parameter. @@ -2070,7 +2070,7 @@ Adding new actions is a bit trickier, because you have to understand that "store" actions actions that result in :mod:`!optparse` storing a value to an attribute of the - current OptionValues instance; these options require a :attr:`~Option.dest` + current Values instance; these options require a :attr:`~Option.dest` attribute to be supplied to the Option constructor. "typed" actions @@ -2152,8 +2152,8 @@ Features of note: control back to :meth:`Option.take_action` for the standard :mod:`!optparse` actions. -* ``values`` is an instance of the optparse_parser.Values class, which provides - the very useful :meth:`ensure_value` method. :meth:`ensure_value` is +* ``values`` is an instance of the optparse.Values class, which provides + the very useful :meth:`!ensure_value` method. :meth:`!ensure_value` is essentially :func:`getattr` with a safety valve; it is called as :: values.ensure_value(attr, value) @@ -2163,9 +2163,9 @@ Features of note: very handy for actions like ``"extend"``, ``"append"``, and ``"count"``, all of which accumulate data in a variable and expect that variable to be of a certain type (a list for the first two, an integer for the latter). Using - :meth:`ensure_value` means that scripts using your action don't have to worry + :meth:`!ensure_value` means that scripts using your action don't have to worry about setting a default value for the option destinations in question; they - can just leave the default as ``None`` and :meth:`ensure_value` will take care of + can just leave the default as ``None`` and :meth:`!ensure_value` will take care of getting it right when it's needed. Exceptions diff --git a/Doc/library/os.rst b/Doc/library/os.rst index ca8b60d57ead76..fff27b48ae989c 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -1576,7 +1576,7 @@ or `the MSDN `_ on Windo Combine the functionality of :func:`os.readv` and :func:`os.pread`. - .. availability:: Linux >= 2.6.30, FreeBSD >= 6.0, OpenBSD >= 2.7, AIX >= 7.1. + .. availability:: Unix. Using flags requires Linux >= 4.6. @@ -1682,7 +1682,7 @@ or `the MSDN `_ on Windo Combine the functionality of :func:`os.writev` and :func:`os.pwrite`. - .. availability:: Linux >= 2.6.30, FreeBSD >= 6.0, OpenBSD >= 2.7, AIX >= 7.1. + .. availability:: Unix. Using flags requires Linux >= 4.6. @@ -2838,7 +2838,7 @@ features: substitution path (which typically includes ``\\?\`` prefix) rather than the optional "print name" field that was previously returned. - .. versionchanged:: next + .. versionchanged:: 3.16 Accepts file descriptors pointing to symbolic links as *path* on Linux, Android, macOS and iOS. @@ -3374,7 +3374,7 @@ features: .. versionchanged:: 3.12 ``st_ctime_ns`` is deprecated on Windows. Use ``st_birthtime_ns`` - for the file creation time. In the future, ``st_ctime`` will contain + for the file creation time. In the future, ``st_ctime_ns`` will contain the time of the most recent metadata change, as for other platforms. .. attribute:: st_birthtime @@ -3448,20 +3448,6 @@ features: String that uniquely identifies the type of the filesystem that contains the file. - On macOS systems, the following attributes may also be available: - - .. attribute:: st_rsize - - Real size of the file. - - .. attribute:: st_creator - - Creator of the file. - - .. attribute:: st_type - - File type. - On Windows systems, the following attributes are also available: .. attribute:: st_file_attributes @@ -5456,7 +5442,7 @@ written in Python, such as a mail server's external command delivery program. parent's signal mask. This argument corresponds to the C library :c:macro:`!POSIX_SPAWN_SETSIGMASK` flag. - The *sigdef* argument will reset the disposition of all signals in the set + The *setsigdef* argument will reset the disposition of all signals in the set specified. This argument corresponds to the C library :c:macro:`!POSIX_SPAWN_SETSIGDEF` flag. @@ -6466,9 +6452,12 @@ Random numbers random bytes in non-blocking mode (using the :data:`GRND_NONBLOCK` flag) or to poll until the system urandom entropy pool is initialized. - On a Unix-like system, random bytes are read from the ``/dev/urandom`` - device. If the ``/dev/urandom`` device is not available or not readable, the - :exc:`NotImplementedError` exception is raised. + On other Unix-like systems, the C ``getrandom()`` function is used if it is + available, otherwise the C ``getentropy()`` function. + + If none of these functions can be used, random bytes are read from the + ``/dev/urandom`` device. If the ``/dev/urandom`` device is not available or + not readable, the :exc:`NotImplementedError` exception is raised. On Windows, it will use ``BCryptGenRandom()``. diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index 995a9756a4ef53..1a494e4a0f9fa5 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -278,7 +278,8 @@ access further features, you have to do this yourself: that matches one of these patterns. [1]_ By default, Pdb sets a handler for the SIGINT signal (which is sent when the - user presses :kbd:`Ctrl-C` on the console) when you give a :pdbcmd:`continue` command. + user presses :kbd:`Ctrl-C` on the console) when you resume execution with + :pdbcmd:`continue`, :pdbcmd:`next`, :pdbcmd:`until`, or :pdbcmd:`return`. This allows you to break into the debugger again by pressing :kbd:`Ctrl-C`. If you want Pdb not to touch the SIGINT handler, set *nosigint* to true. @@ -328,6 +329,10 @@ access further features, you have to do this yourself: Inline breakpoints like :func:`breakpoint` or :func:`pdb.set_trace` will always stop the program at calling frame, ignoring the *skip* pattern (if any). + .. versionchanged:: 3.16 + SIGINT handling also applies to :pdbcmd:`next`, :pdbcmd:`until`, and + :pdbcmd:`return`. + .. method:: run(statement, globals=None, locals=None) runeval(expression, globals=None, locals=None) runcall(function, *args, **kwds) @@ -812,7 +817,7 @@ can be overridden by the local file. List or jump between chained exceptions. - When using ``pdb.pm()`` or ``Pdb.post_mortem(...)`` with a chained exception + When using ``pdb.pm()`` or ``pdb.post_mortem(...)`` with a chained exception instead of a traceback, it allows the user to move between the chained exceptions using ``exceptions`` command to list exceptions, and ``exceptions `` to switch to that exception. diff --git a/Doc/library/platform.rst b/Doc/library/platform.rst index f728a7b32e789f..a80a653a615236 100644 --- a/Doc/library/platform.rst +++ b/Doc/library/platform.rst @@ -325,7 +325,7 @@ Android platform * ``release`` - Android version, as a string (e.g. ``"14"``). - * ``api_level`` - API level of the running device, as an integer (e.g. ``34`` + * ``api_level`` - Major component of the API level, as an integer (e.g. ``34`` for Android 14). To get the API level which Python was built against, see :func:`sys.getandroidapilevel`. diff --git a/Doc/library/profile.rst b/Doc/library/profile.rst index f7e85d1598727f..324f69ef9d31c5 100644 --- a/Doc/library/profile.rst +++ b/Doc/library/profile.rst @@ -92,7 +92,7 @@ following functions: the *timer* argument. This must be a function that returns a single number representing the current time. If the number is an integer, the *timeunit* specifies a multiplier that specifies the duration of each unit of time. For - example, if the timer returns times measured in thousands of seconds, the + example, if the timer returns times measured in thousandths of seconds, the time unit would be ``.001``. Directly using the :class:`Profile` class allows formatting profile results @@ -345,7 +345,7 @@ you are using :class:`profile.Profile` or :class:`profiling.tracing.Profile`, ``your_time_func`` should return a single number. If it returns integers, you can also invoke the class constructor with a second argument specifying the real duration of one unit of time. For example, if - ``your_integer_time_func`` returns times measured in thousands of seconds, + ``your_integer_time_func`` returns times measured in thousandths of seconds, you would construct the :class:`Profile` instance as follows:: pr = profiling.tracing.Profile(your_integer_time_func, 0.001) diff --git a/Doc/library/profiling.sampling.rst b/Doc/library/profiling.sampling.rst index 7bb0e966583bcb..b4dfd344299f4a 100644 --- a/Doc/library/profiling.sampling.rst +++ b/Doc/library/profiling.sampling.rst @@ -1051,7 +1051,7 @@ an interactive flame graph visualization:: `Try the interactive example <../_static/tachyon-example-flamegraph.html>`__! If no output file is specified, the profiler generates a filename based on -the process ID (for example, ``flamegraph.12345.html``). +the process ID (for example, ``flamegraph_12345.html``). The generated HTML file requires no external dependencies and can be opened directly in a web browser. The visualization displays call stacks as nested @@ -1321,11 +1321,14 @@ data, similar to the ``top`` command for system processes:: .. only:: not latex - .. figure:: tachyon-live-mode-2.gif + .. figure:: tachyon-live-mode-2.svg :alt: Tachyon live mode showing all threads :align: center :width: 100% + .. This screenshot has been rendered with this one-liner CLI: + uvx 'click-extra==10.3.1' screenshot --record --columns 120 --rows 47 --start 6 --end 10 --hold 0 --blank 0 --preset plain --prompt '' --watermark '' --margin 0 --padding 0 --shadow none --border none --output tachyon-live-mode-2.svg -- python3.15 -m profiling.sampling run --live --all-threads --sampling-rate 10khz -m test test_gc test_threading test_list test_dict + Live mode displays real-time profiling statistics, showing combined data from multiple threads in a multi-threaded application. @@ -1346,11 +1349,14 @@ frequently, including specialized variants and their base opcodes. .. only:: not latex - .. figure:: tachyon-live-mode-1.gif + .. figure:: tachyon-live-mode-1.svg :alt: Tachyon live mode with opcode panel :align: center :width: 100% + .. This screenshot has been rendered with this one-liner CLI: + uvx 'click-extra==10.3.1' screenshot --record --columns 120 --rows 47 --start 6 --end 10 --hold 0 --blank 0 --preset plain --prompt '' --watermark '' --margin 0 --padding 0 --shadow none --border none --output tachyon-live-mode-1.svg -- python3.15 -m profiling.sampling run --live --all-threads --sampling-rate 10khz --opcodes -m test test_gc test_threading test_list test_dict + Live mode with ``--opcodes`` enabled shows an opcode panel with a bytecode instruction breakdown for the selected function. diff --git a/Doc/library/pyexpat.rst b/Doc/library/pyexpat.rst index f6d4aca383481f..712b31367577f7 100644 --- a/Doc/library/pyexpat.rst +++ b/Doc/library/pyexpat.rst @@ -603,7 +603,7 @@ otherwise stated. Called for each declared attribute for an element type. If an attribute list declaration declares three attributes, this handler is called three times, once for each attribute. *elname* is the name of the element to which the - declaration applies and *attname* is the name of the attribute declared. The + declaration applies and *attname* is the name of the attribute declared. The attribute type is a string passed as *type*: ``'CDATA'``, ``'ID'``, ``'IDREF'``, ``'IDREFS'``, ``'ENTITY'``, ``'ENTITIES'``, ``'NMTOKEN'`` or ``'NMTOKENS'``, diff --git a/Doc/library/quopri.rst b/Doc/library/quopri.rst index 60b4d1c182d641..ee21ad65543a3a 100644 --- a/Doc/library/quopri.rst +++ b/Doc/library/quopri.rst @@ -31,7 +31,7 @@ sending a graphics file. If the optional argument *strip_ws* is true, trailing whitespace is stripped from each line, as required by :rfc:`2045`. - .. versionchanged:: next + .. versionchanged:: 3.16 Added the *strip_ws* parameter. @@ -53,7 +53,7 @@ sending a graphics file. Like :func:`decode`, except that it accepts a source :class:`bytes` and returns the corresponding decoded :class:`bytes`. - .. versionchanged:: next + .. versionchanged:: 3.16 Added the *strip_ws* parameter. diff --git a/Doc/library/reprlib.rst b/Doc/library/reprlib.rst index 75d0df481c79b4..563ad2b3195e95 100644 --- a/Doc/library/reprlib.rst +++ b/Doc/library/reprlib.rst @@ -40,7 +40,7 @@ This module provides a class, an instance, and a function: .. versionchanged:: 3.12 Allow attributes to be set via keyword arguments. - .. versionchanged:: next + .. versionchanged:: 3.16 :class:`!Repr` instances now have a string representation that includes their attribute values. diff --git a/Doc/library/runpy.rst b/Doc/library/runpy.rst index 9e1ec4691872f0..aed6d7ef58ad86 100644 --- a/Doc/library/runpy.rst +++ b/Doc/library/runpy.rst @@ -114,10 +114,9 @@ The :mod:`!runpy` module provides two functions: For a simple script, the specified code is simply executed in a fresh module namespace. For a valid :data:`sys.path` entry (typically a zipfile or directory), the entry is first added to the beginning of ``sys.path``. The - function then looks for and executes a :mod:`__main__` module using the - updated path. Note that there is no special protection against invoking - an existing ``__main__`` entry located elsewhere on ``sys.path`` if - there is no such module at the specified location. + function then looks for and executes a :mod:`__main__` module in that + entry. :exc:`ImportError` is raised if there is no such module at the + specified location. The optional dictionary argument *init_globals* may be used to pre-populate the module's globals dictionary before the code is executed. @@ -177,6 +176,9 @@ The :mod:`!runpy` module provides two functions: .. versionchanged:: 3.15 The global variable ``__cached__`` is no longer set. + .. versionchanged:: next + The ``__main__`` module is now searched only in *path_name*. + .. seealso:: :pep:`338` -- Executing modules as scripts diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst index 2d3edd8e5aebfc..db484beaf4b2ab 100644 --- a/Doc/library/shutil.rst +++ b/Doc/library/shutil.rst @@ -532,6 +532,9 @@ Directory and files operations or ends with an extension that is in ``PATHEXT``; and filenames that have no extension can now be found. + .. versionchanged:: next + Accepts a :term:`path-like object` for *cmd*. + .. exception:: Error Subclass of :exc:`OSError` collecting exceptions raised during a multi-file @@ -786,7 +789,7 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. .. versionchanged:: 3.12 Added the *filter* argument. - .. versionchanged:: next + .. versionchanged:: 3.16 Added the "whl" format. .. function:: register_unpack_format(name, extensions, function[, extra_args[, description]]) @@ -837,7 +840,7 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules. You can register new formats or provide your own unpacker for any existing formats, by using :func:`register_unpack_format`. - .. versionchanged:: next + .. versionchanged:: 3.16 Added the *whl* format. diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index 5c97199bc453e8..fe58c7fd2414fe 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -24,7 +24,7 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions). An :class:`SMTP` instance encapsulates an SMTP connection. It has methods that support a full repertoire of SMTP and ESMTP operations. - If the host parameter is set to a truthy value, :meth:`SMTP.connect` is called with + If the host parameter is set to a true value, :meth:`SMTP.connect` is called with host and port automatically when the object is created; otherwise, :meth:`!connect` must be called manually. @@ -88,7 +88,7 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions). required from the beginning of the connection and using :meth:`SMTP.starttls` is not appropriate. - If the host parameter is set to a truthy value, :meth:`SMTP.connect` is called with host + If the host parameter is set to a true value, :meth:`SMTP.connect` is called with host and port automatically when the object is created; otherwise, :meth:`!SMTP.connect` must be called manually. diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index 5a1cd95da527ea..5aaad9a1bcc5ba 100644 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -1065,7 +1065,7 @@ The :mod:`!socket` module also offers various network-related services: for IPv6 multicast addresses, string representing an address will not contain ``%scope_id`` part. - .. versionchanged:: next + .. versionchanged:: 3.16 Null characters are now rejected in the *host* and *port* parameters. .. _Happy Eyeballs: https://en.wikipedia.org/wiki/Happy_Eyeballs diff --git a/Doc/library/ssl.rst b/Doc/library/ssl.rst index 139dc2c24eab76..a1ed46e2b6e610 100644 --- a/Doc/library/ssl.rst +++ b/Doc/library/ssl.rst @@ -101,6 +101,7 @@ Client socket example with custom context and IPv4:: with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock: with context.wrap_socket(sock, server_hostname=hostname) as ssock: + ssock.connect((hostname, 443)) print(ssock.version()) @@ -341,7 +342,7 @@ Exceptions The server's ECH retry configuration as an ECHConfigList, or ``b""`` if the server did not provide one. - .. versionadded:: next + .. versionadded:: 3.16 .. exception:: CertificateError @@ -432,7 +433,8 @@ Certificate handling same format as used for the *cafile* parameter in :meth:`SSLContext.load_verify_locations`. The call will attempt to validate the server certificate against that set of root certificates, and will fail - if the validation attempt fails. A timeout can be specified with the + if the validation attempt fails or the certificate hostname does not match + the server hostname. A timeout can be specified with the ``timeout`` parameter. .. versionchanged:: 3.3 @@ -445,6 +447,11 @@ Certificate handling .. versionchanged:: 3.10 The *timeout* parameter was added. + .. versionchanged:: 3.16 + The function now raises :exc:`SSLCertVerificationError` if *ca_certs* is + specified but the certificate hostname does not match the server + hostname. + .. function:: DER_cert_to_PEM_cert(der_cert_bytes) Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded @@ -900,7 +907,7 @@ Constants Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. data:: OP_ECH_TRIALDECRYPT @@ -909,7 +916,7 @@ Constants Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. data:: OP_ECH_IGNORE_CID @@ -917,7 +924,7 @@ Constants Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. data:: OP_ECH_GREASE_RETRY_CONFIG @@ -925,7 +932,7 @@ Constants Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. data:: HAS_ALPN @@ -1027,7 +1034,7 @@ Constants Whether the OpenSSL library has built-in support for Encrypted Client Hello (ECH). This requires OpenSSL 4.0 or later with ECH enabled. - .. versionadded:: next + .. versionadded:: 3.16 .. data:: CHANNEL_BINDING_TYPES @@ -1104,33 +1111,33 @@ Constants Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_SUCCESS ECH was accepted without a certificate verification error. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_FAILED An internal or protocol error occurred, or ECH was rejected without a retry configuration. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_FAILED_ECH ECH was rejected and the server supplied an authenticated retry configuration. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_BAD_NAME ECH was accepted, but verification of the peer's certificate failed. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_FAILED_ECH_BAD_NAME @@ -1138,45 +1145,45 @@ Constants verification for the outer server name failed. The retry configuration must not be used. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_NOT_TRIED ECH is configured but has not been attempted (such as before the handshake starts). - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_NOT_CONFIGURED No ECH configuration is set for this connection. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_GREASE ECH GREASE is enabled or was used, but no retry configuration was received. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_GREASE_ECH The client sent a GREASE ECH extension and received a retry configuration. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_BACKEND The server received a ClientHello marked as an inner ClientHello, as used by a backend server in an ECH split deployment. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: ECHStatus.ECH_STATUS_BAD_CALL Invalid arguments were supplied to OpenSSL's ECH status API. - .. versionadded:: next + .. versionadded:: 3.16 .. data:: Purpose.SERVER_AUTH @@ -1532,7 +1539,7 @@ SSL sockets also have the following additional methods and attributes: Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. method:: SSLSocket.get_ech_status() @@ -1544,7 +1551,7 @@ SSL sockets also have the following additional methods and attributes: Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. method:: SSLSocket.set_ech_config(ech_config) @@ -1559,7 +1566,7 @@ SSL sockets also have the following additional methods and attributes: Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. method:: SSLSocket.selected_alpn_protocol() @@ -1641,7 +1648,7 @@ SSL sockets also have the following additional methods and attributes: Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: SSLSocket.server_side @@ -1843,7 +1850,7 @@ to speed up repeated connections from the same clients. Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. method:: SSLContext.load_verify_locations(cafile=None, capath=None, cadata=None) @@ -1958,7 +1965,7 @@ to speed up repeated connections from the same clients. Set the allowed ciphers for sockets created with this context when connecting using TLS 1.2 and earlier. The *ciphers* argument should be a string in the `OpenSSL cipher list format - `_. + `_. To set allowed TLS 1.3 ciphers, use :meth:`SSLContext.set_ciphersuites`. If no cipher can be selected (because compile-time options or other @@ -2072,7 +2079,7 @@ to speed up repeated connections from the same clients. Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. method:: SSLContext.set_npn_protocols(npn_protocols) @@ -2093,10 +2100,10 @@ to speed up repeated connections from the same clients. NPN has been superseded by ALPN -.. method:: SSLContext.set_outer_alpn_protocols(protocols) +.. method:: SSLContext.set_outer_alpn_protocols(alpn_protocols) Specify which protocols the socket should advertise during the TLS - handshake in the outer ClientHello when ECH is used. The *protocols* + handshake in the outer ClientHello when ECH is used. The *alpn_protocols* argument accepts the same values as for :meth:`~SSLContext.set_alpn_protocols`. @@ -2110,7 +2117,7 @@ to speed up repeated connections from the same clients. Only available when :data:`HAS_ECH` is ``True``. - .. versionadded:: next + .. versionadded:: 3.16 .. attribute:: SSLContext.sni_callback @@ -2230,13 +2237,12 @@ to speed up repeated connections from the same clients. The parameter ``server_side`` is a boolean which identifies whether server-side or client-side behavior is desired from this socket. - For client-side sockets, the context construction is lazy; if the - underlying socket isn't connected yet, the context construction will be - performed after :meth:`connect` is called on the socket. For - server-side sockets, if the socket has no remote peer, it is assumed + For client-side sockets, if *sock* is not yet connected, the underlying + SSL object is created when :meth:`~socket.socket.connect` is called. + For server-side sockets, if the socket has no remote peer, it is assumed to be a listening socket, and the server-side SSL wrapping is automatically performed on client connections accepted via the - :meth:`accept` method. The method may raise :exc:`SSLError`. + :meth:`~socket.socket.accept` method. The method may raise :exc:`SSLError`. On client connections, the optional parameter *server_hostname* specifies the hostname of the service which we are connecting to. This allows a @@ -2278,7 +2284,7 @@ to speed up repeated connections from the same clients. The method returns an instance of :attr:`SSLContext.sslsocket_class` instead of hard-coded :class:`SSLSocket`. - .. versionchanged:: next + .. versionchanged:: 3.16 *ech_config* argument was added. .. attribute:: SSLContext.sslsocket_class @@ -2320,7 +2326,7 @@ to speed up repeated connections from the same clients. *server_hostname* was accepted, and verified the certificate chain but never the peer's identity. - .. versionchanged:: next + .. versionchanged:: 3.16 *ech_config* argument was added. .. attribute:: SSLContext.sslobject_class @@ -2860,7 +2866,7 @@ Visual inspection shows that the certificate does identify the desired service Now the SSL channel is established and the certificate verified, you can proceed to talk with the server:: - >>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n") + >>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: www.python.org\r\n\r\n") >>> pprint.pprint(conn.recv(1024).split(b"\r\n")) [b'HTTP/1.1 200 OK', b'Date: Sat, 18 Oct 2014 18:27:20 GMT', @@ -3072,7 +3078,7 @@ provided. When compared to :class:`SSLSocket`, this object lacks the following features: - - Any form of network IO; ``recv()`` and ``send()`` read and write only to + - Any form of network IO; ``read()`` and ``write()`` read and write only to the underlying :class:`MemoryBIO` buffers. - There is no *do_handshake_on_connect* machinery. You must always manually @@ -3296,7 +3302,7 @@ of TLS/SSL. Some new TLS 1.3 features are not yet available. Encrypted Client Hello ^^^^^^^^^^^^^^^^^^^^^^ -.. versionadded:: next +.. versionadded:: 3.16 Encrypted Client Hello (ECH) encrypts values in the ClientHello record sent when establishing a TLS connection. It requires an ECHConfigList containing algorithm diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst index 2a31213560c92d..189b0818276879 100644 --- a/Doc/library/subprocess.rst +++ b/Doc/library/subprocess.rst @@ -1326,7 +1326,7 @@ calls these functions. ... "ls non_existent_file; exit 0", ... stderr=subprocess.STDOUT, ... shell=True) - 'ls: non_existent_file: No such file or directory\n' + b'ls: non_existent_file: No such file or directory\n' .. versionadded:: 3.1 diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 33de85b04f4b0e..322c0862b511ab 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -438,7 +438,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only .. availability:: WASI. - .. versionadded:: next + .. versionadded:: 3.16 .. data:: pycache_prefix @@ -862,9 +862,9 @@ always available. Unless explicitly noted otherwise, all variables are read-only .. function:: getandroidapilevel() - Return the build-time API level of Android as an integer. This represents the - minimum version of Android this build of Python can run on. For runtime - version information, see :func:`platform.android_ver`. + Return the major component of the Android API level that Python was built + against, as an integer. For runtime version information, see + :func:`platform.android_ver`. .. availability:: Android. @@ -1651,6 +1651,24 @@ always available. Unless explicitly noted otherwise, all variables are read-only * Module :mod:`site` This describes how to use .pth files to extend :data:`sys.path`. +.. data:: _path0 + + Path prepended to :data:`sys.path` at Python startup: + + * Current directory if running a module + with ``python -m module`` command line. + * Script's directory if running a script + with ``python script.py``. + * Otherwise an empty string, which means the current working directory, + for ``python -c code`` or ``python`` (REPL) command line. + + If :option:`-P` or :option:`-I` command line options are used or if + :envvar:`PYTHONSAFEPATH` environment variable is used, :data:`sys._path0` is + ``None`` and :data:`sys.path` is left unchanged. + + .. versionadded:: 3.16 + + .. data:: path_hooks A list of callables that take a path argument to try to create a diff --git a/Doc/library/tachyon-live-mode-1.gif b/Doc/library/tachyon-live-mode-1.gif deleted file mode 100644 index 02a56d1767f7dc..00000000000000 Binary files a/Doc/library/tachyon-live-mode-1.gif and /dev/null differ diff --git a/Doc/library/tachyon-live-mode-1.svg b/Doc/library/tachyon-live-mode-1.svg new file mode 100644 index 00000000000000..43be7a309ce5cc --- /dev/null +++ b/Doc/library/tachyon-live-mode-1.svg @@ -0,0 +1,359 @@ + + + + + + + +Tachyon ProfilerPID:12999│Thread:ALL│Uptime:│Time:│Interval:100µs│Display:10.0HzSamples:[]10.0KHz/10.0KHz (100%)Efficiency:[]Threads:% on gil│% off gil│% waiting for gil│% exc│% GCFunctions:total│exec│stack│34shownTop 3:││───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────nsamples%▼tottime%functionfile:line►0.2─────────────────────────────────────────────────────────────────────────────────────────[████████████████████nsamples: direct/cumulative (direct=executing, cumulative=on stack)Sort: Total Time | 't':mode 'x':trends ←→:thread 'h':help 'q':quit0m06s727/7270.073Popen._try_waitsubprocess.py:2208344/3440.034Popen._execute_childsubprocess.py:20860.026gc_collecttest/support/__init__.py:9620.022205/2050.021subprocess.py:21090.020test/support/__init__.py:964163/48830.016GCTests.test_trashcan_threads.<loca...test/test_gc.py:500151/16740.015GCTests.test_trashcan.<locals>.Ouch...test/test_gc.py:434107/1230.011test/test_gc.py:49996/960.010_FuncBuilder.add_fns_to_classdataclasses.py:49953/530.005GCTests.test_resurrection_only_happ...test/test_gc.py:990test/test_gc.py:47210:31:07<GC>~:00.4% (█████████████████████████████░████████████████████████████████████████████████████████████2511/25110.251GCTests.test_trashcan_threadstest/test_gc.py:5162420/24200.242threading.py:363755(50.5%)50.550.54.64.63.2🥇 sleeping_retry🥈 Condition.waitsleeping_retrytest/support/__init__.py:2782Condition.waitthreading.py:370Opcodes for sleeping_retry()] CALL_BUILTIN_O (CALL)[░░░░░░░░░░░░░░░░░░░░] CALL104)🥉 BarrierTests.test_time...9994/99940.999BarrierTests.test_timeout.<locals>.ftest/lock_tests.py:122523785/237852.3796425/64250.643_PollLikeSelector.selectselectors.py:398Condition.wait0.50.4gc_collecttest/support/__init__.py:9630.20.40.4Popen._execute_child0.4gc_collect0.30.30.2GCTests.test_trashcan_threads.<loca...0.253/530.0050.10.10.10.1GCTests.test_trashcan_threads.<loca...4414569/45740.457GCTests.test_trashcan_threads.<loca...test/test_gc.py:474wait_threads_blockedtest/lock_tests.py:25860/11460.086_call_with_frames_removed<frozen importlib._bootstrap>:4910.60.6total (10000.5/s)756315262/2018218/1396203/1436██████████████████████████████1221/12210.1221.31.38091/81290.8092.22.52.63.06381/63810.638BaseLockTests.test_acquire_contende...test/lock_tests.py:1746425/64250.643_PollLikeSelector.selectselectors.py:39810:31:08BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2498091/81290.809<GC>~:00.3% (0.50.3🥉 BaseLockTests.test_tim...BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2499994/99940.999BarrierTests.test_timeout.<locals>.ftest/lock_tests.py:12251.90.40.31.20.310026/100261.003total (10000.4/s)████████████████████████████████████████████████████████████0.20m07s0.026test/support/__init__.py:9620.022test/support/__init__.py:963205/2050.0210.3subprocess.py:21090.020163/48830.016GCTests.test_trashcan_threads.<loca...test/test_gc.py:500151/16740.015GCTests.test_trashcan.<locals>.Ouch...test/test_gc.py:434107/1230.011test/test_gc.py:49996/960.010_FuncBuilder.add_fns_to_classdataclasses.py:499GCTests.test_resurrection_only_happ...test/test_gc.py:990860/11460.086_call_with_frames_removed<frozen importlib._bootstrap>:491727/7270.073Popen._try_waitsubprocess.py:2208344/3440.034Popen._execute_childsubprocess.py:2086gc_collect0.60.6Popen._execute_childtest/support/__init__.py:964gc_collectGCTests.test_trashcan_threads.<loca...0.2Condition.wait4569/45740.457GCTests.test_trashcan_threads.<loca...test/test_gc.py:4742511/25110.251GCTests.test_trashcan_threadstest/test_gc.py:5160.5Condition.waitthreading.py:3731.11.1774319455262/2232218/1549203/1590Condition.waitthreading.py:373threading.py:363<GC>~:0Condition.wait6425/64250.643_PollLikeSelector.selectselectors.py:3986381/63810.638BaseLockTests.test_acquire_contende...test/lock_tests.py:1740.49402/94020.9403982/39820.3988638/86760.8646555/65550.65610:31:096.86.8🥇 Condition.wait🥈 sleeping_retryCondition.waitthreading.py:370sleeping_retrytest/support/__init__.py:2782Opcodes for Condition.wait()2.348765/487654.877] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 48765)1.01.00m08s████████████████████████████████████████████████████████████(67.6%)67.667.66.36.30.50.5gc_collectConditionTests.test_waitfortest/lock_tests.py:786163/48830.016test/test_gc.py:500151/16740.015GCTests.test_trashcan.<locals>.Ouch...test/test_gc.py:434363/3630.036ConditionTests.test_waitfor_timeouttest/lock_tests.py:807344/3440.034subprocess.py:20860.026test/support/__init__.py:9620.022test/support/__init__.py:963205/2050.021Popen._execute_childsubprocess.py:21090.020gc_collecttest/support/__init__.py:9640.50.50.30.30.2107/1230.011test/test_gc.py:499462/4620.0460.50.5total (10000.3/s)🥉 Condition.waitCondition.waitthreading.py:36310026/100261.0039994/99940.999BarrierTests.test_timeout.<locals>.ftest/lock_tests.py:12259402/94020.940<GC>~:0wait_threads_blockedtest/lock_tests.py:254569/45740.457GCTests.test_trashcan_threads.<loca...test/test_gc.py:4743.33.32.21.10.332210:31:1079447213550/135501.355262/2609218/1757203/17670.90.99400/94385552/55520.5550.9400m09s77951/779517.795] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 77951)1.06425/64250.643_PollLikeSelector.selectselectors.py:398363/3630.036ConditionTests.test_waitfor_timeouttest/lock_tests.py:807344/3440.0340.4subprocess.py:2086test/support/__init__.py:962205/2050.021Popen._execute_childsubprocess.py:2109(59.4%)7.57.50.43.55.713614/136141.3619402/94020.940threading.py:3730.0370.0310.0308433415029994/99940.999BarrierTests.test_timeout.<locals>.ftest/lock_tests.py:12259701/97010.9706097/60970.6100.80.8371/2113306/2043302/3033LockTests.test_reacquire.<locals>.ftest/lock_tests.py:28310:31:1110160/101981.016125/1250.013████████████████████████████████████████████████████████████1.913614/136141.361Condition.waitthreading.py:36360041total ( 9997.0/s)86.75% good, 13.25% failed11.688.44.413.6312443(45.7%)(19.2%)26311/263112.63145.745.719.219.27442/748014.30.74414.45008/50089.60.5019.64569/45748.80.4578.8GCTests.test_trashcan_threads.<loca...test/test_gc.py:4744360/43608.40.4368.44.84.8860/11461.70.086_call_with_frames_removed<frozen importlib._bootstrap>:491818/8181.60.0821.60.70.7262/17753.4218/1220202/12052.39.499.6% ( 26207)1.42.31.40.612.312.3BaseLockTests.test_acquire_contende...test/lock_tests.py:1742.2wait_threads_blockedtest/lock_tests.py:25BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:24961059total ( 9999.9/s)86.97% good, 13.03% failed4.3314(44.8%)(18.8%)26834/268342.68344.844.818.818.87720/775814.50.77214.612.112.16381/638112.00.63812.05008/50080.5014.74.71221/12212.30.1222.32.2262/19313.6218/1286203/12622.49.299.6% ( 26730)2.41.411.588.51.48.68.61.60.69.49.4BaseLockTests.test_acquire_contende...test/lock_tests.py:174BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:24913.86206387.17% good, 12.83% failed11.788.314.2(50.8%)(44.0%)(18.5%)27457/2745750.82.74650.844.044.018.518.58091/81290.8096381/638111.80.63811.85403/540310.00.54010.08.54.54.52.32.33.72.62.79.03.199.6% ( 27353)11.911.91.68.40.6BaseLockTests.test_acquire_contende...test/lock_tests.py:174BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:24915.015.04.24.64.62.16306387.37% good, 12.63% failed14.0(51.6%)(43.2%)(18.1%)28457/2845751.62.84651.643.243.218.118.16403/640311.60.64011.64.44.43.78.999.6% ( 28353)14.811.588.51.60.614.78.38.32.211.711.74.24.64.62.1BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:24911.611.66406487.57% good, 12.43% failed11.388.74.1(52.5%)(42.4%)(17.8%)29458/2945852.52.94652.542.442.417.817.814.47404/740413.20.74013.2BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:24911.44.54.54.34.33.68.799.6% ( 29354)2.011.511.58.111.40.614.58.22.213.81.56506487.76% good, 12.24% failed11.188.94.013.5(53.3%)(41.7%)(17.5%)30458/3045853.33.04653.341.741.717.517.58404/840414.70.84014.74.44.48.699.7% ( 30354)8.08.014.214.211.211.22.011.311.32.12.12.40.64.24.23.52.52.91.56606587.94% good, 12.06% failed10.989.1(54.1%)(40.9%)(17.2%)31459/3145954.13.14654.140.940.99405/940516.20.94116.213.94.34.38.499.7% ( 31355)17.217.211.011.02.02.12.12.47.97.911.111.114.04.24.23.52.52.93.91.513.36706688.12% good, 11.88% failed10.789.3(54.9%)(40.2%)(17.0%)32459/3245954.93.24654.940.240.210026/1002617.01.00317.016.916.913.78.399.7% ( 32355)13.113.82.12.12.41.24.14.110.810.87.77.710.910.93.91.54.24.23.42.42.86806688.30% good, 11.70% failed10.689.4(55.7%)(39.6%)(16.7%)33459/3345955.73.34655.739.639.616.616.613.513.510.710.77.67.64.04.08.199.7% ( 33355)3.82.02.01.410.610.61.216.716.712.92.34.24.23.42.42.86906688.46% good, 11.54% failed10.889.2761316445(55.8%)(38.9%)34087/3408755.83.40955.838.938.916.416.48454/849213.80.84513.910.510.510.410.47.57.52740/27404.50.2744.52511/25110.251GCTests.test_trashcan_threadstest/test_gc.py:516262/21583.5218/1499203/15302.58.02.799.7% ( 33983)3.82.02.01.4threading.py:3631.24.14.12.5Condition.wait(16.4%)16.416.413.37006688.62% good, 11.38% failed10.389.73.6766317449(55.2%)(38.3%)(16.1%)34273/3427355.23.42755.238.338.316.116.116.116.18454/84920.84513.710.310.310.310.37.47.44300/43006.90.4306.93440/34405.50.3445.5threading.py:3732511/25114.00.2514.0GCTests.test_trashcan_threadstest/test_gc.py:5162035/20353.30.2043.3262/21583.5218/1499203/15302.57.999.7% ( 34169)13.113.62.4threading.py:3631.41.8Condition.wait0.21.21.22.7wait_threads_blockedtest/lock_tests.py:257106788.78% good, 11.22% failed10.189.93.513.0770452(54.8%)(37.7%)(15.9%)34576/3457654.83.45854.837.737.715.915.915.815.88552/85900.85510.210.210.110.14889/48897.70.4897.7threading.py:3634800/48007.60.4807.6threading.py:3737.27.22630/26304.20.2634.24.04.03.52.42.57.799.7% ( 34472)13.61.41.8Condition.wait13.60.21.21.22.7318262/2196218/1527203/15627206788.93% good, 11.07% failed9.690.43.4771453(54.2%)(37.1%)(15.6%)34729/3472954.23.47354.237.137.115.615.68552/85900.8558389/838913.10.83913.16425/642510.00.64310.0_PollLikeSelector.selectselectors.py:3986381/638110.00.63810.06299/62999.80.6309.83476/34765.40.3485.47.699.7% ( 34625)13.43.93.92.62.4threading.py:3632.41.815.615.6BaseLockTests.test_acquire_contende...test/lock_tests.py:1747.17.13.413.3Condition.wait12.9318262/2196218/1527203/15627306789.08% good, 10.92% failed9.590.53.312.8(54.0%)(37.6%)(15.4%)35132/3513254.03.51354.024450/2445037.62.44537.615.415.415.415.49402/940214.40.94014.48638/867613.30.86413.36555/655510.10.65610.19.99.99.89.83982/39826.10.3986.13.47.599.7% ( 35028)3.93.92.62.41.82.47.07.074004total ( 9991.8/s)89.22% good, 10.78% failed9.190.93.112.6(54.6%)(44.1%)(15.2%)36069/3606954.63.60754.629135/2913544.12.91344.115.215.213.113.19.99.99.79.79.79.73.83.87.499.7% ( 35965)6.96.96.06.01.714.214.22.43.42.32.515.115.17506889.37% good, 10.63% failed8.791.33.012.5(55.3%)(51.4%)(14.9%)37133/3713355.33.71355.334455/3445551.43.44651.414.914.914.012.912.99.89.89.69.69.59.57.399.7% ( 37029)14.914.91.73.73.73.32.45.95.914.02.32.57606989.51% good, 10.49% failed8.491.62.912.3(58.0%)(56.0%)(14.7%)39460/3946058.03.94658.038134/3813456.03.81356.014.714.714.713.812.712.79.69.69.49.49.49.47.2] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 39460)6.76.71.73.73.73.313.814.75.85.82.32.57706989.65% good, 10.35% failed8.092.02.812.1(64.3%)(56.6%)(14.5%)44460/4446064.34.44664.339134/3913456.63.91356.614.514.514.512.69.59.59.39.39.29.27.1] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 44460)13.612.51.73.63.63.214.55.85.86.66.613.62.22.478053total ( 9998.0/s)89.78% good, 10.22% failed(69.6%)(57.2%)(14.3%)69.669.640116/4011657.24.01257.214.314.314.314.313.412.39.29.29.19.16.56.57.02.75.75.713.41.612.03.63.63.29.49.47.892.22.22.412.479047total ( 9996.6/s)89.90% good, 10.10% failed(68.6%)(14.1%)68.668.641110/411104.11114.114.114.114.113.213.29.29.29.09.09.09.06.46.43.16.92.71.63.53.5(57.8%)57.857.85.65.67.792.32.211.82.22.412.212.28007890.03% good,9.97% failed777320457(57.9%)(13.9%)41716/4171657.94.17257.913.913.913.913.98719/87570.8727022/70229.70.7029.78.98.98.98.9342/3420.034ConditionTests.test_waitfortest/lock_tests.py:786262/22680.0263.1test/support/__init__.py:962218/15700.0222.2test/support/__init__.py:963205/2050.0210.3Popen._execute_childsubprocess.py:2109203/16140.0202.2test/support/__init__.py:964163/48830.0166.8test/test_gc.py:500151/16740.0152.3GCTests.test_trashcan.<locals>.Ouch...test/test_gc.py:434107/1230.011GCTests.test_trashcan_threads.<loca...test/test_gc.py:49996/960.010_FuncBuilder.add_fns_to_classdataclasses.py:4995.55.51.612.13.53.52.67.792.313.013.011.80.212.18107890.16% good,9.84% failed780459(57.7%)(13.7%)49438/494384.94442152/4215257.74.21557.713.713.713.713.712.912.98799/883712.00.8807143/71439.80.7149.88.88.88.78.7462/4620.60.0460.6262/23003.1218/1594203/16382.26.7] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 49438)5.45.42.21.63213.43.42.32.67.792.311.712.18207890.27% good,9.73% failed784463(67.2%)(13.5%)67.267.242808/428084.28113.513.513.513.59140/917812.30.91412.47698/769810.40.77010.48.78.78.68.66.26.2262/2499218/1678203/16966.61.55.45.449784/497844.978] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 49784)32112.712.7(57.8%)57.857.812.03.43.42.32.63.47.892.22.32.38307890.39% good,9.61% failed7.692.42.5786465(66.3%)(57.3%)66.366.343040/4304057.34.30457.311680/1168015.61.16815.613.413.413.312.59234/92720.9238.58.54653/46536.20.4656.26.16.1262/2541218/1707203/17192.36.512.51.549784/497844.978] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 49784)32112.312.38.68.6(15.6%)12.03.413.32.384077total (10000.2/s)90.49% good,9.51% failed7.592.5792470(67.8%)(17.8%)51569/5156967.85.15767.843339/4333957.04.33457.013550/1355017.81.35517.813.213.213.113.112.49322/93600.9328.48.48.45259/52590.526262/25743.4218/1735203/17466.4] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 51569)6.06.01.512.312.32.4(57.0%)8.411.96.96.912.42.32.38507990.61% good,9.39% failed7.392.7(72.1%)(17.6%)55587/5558772.15.55972.143968/4396857.04.39757.017.617.613.013.09400/943812.20.94012.28.38.35552/55520.555] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 55587)1.55.95.92.4(57.0%)7.27.22.36.38.38.311.93.413.013.012.212.285958total ( 9986.3/s)90.70% good,9.30% failed7.192.92.3(76.9%)(57.5%)(17.4%)59982/5998276.95.99876.944847/4484757.54.48557.517.417.412.912.912.812.812.112.18.28.28.2] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 59982)1.512.15.95.92.36.33.23.23.38.27.17.111.82.112.186982total ( 9989.2/s)90.81% good,9.19% failed6.993.1(82.4%)(58.1%)(17.2%)65102/6510282.46.51082.445871/4587158.14.58758.112.712.711.911.911.911.98.18.18.16.2] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 65102)2.21.511.617.217.28.112.712.73.23.22.23.32.25.85.82.17.07.08808090.93% good,9.07% failed(88.1%)(58.6%)(16.9%)70592/7059288.17.05988.146969/4696958.64.69758.616.916.912.512.512.512.511.711.711.711.88.08.06.1] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 70592)6.96.98.08.02.25.75.71.46.793.32.23.33.13.12.22.111.58908191.03% good,8.97% failed(93.2%)(59.2%)(16.7%)75597/7559793.27.56093.247970/4797059.24.79759.212.412.47.97.96.0] CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS (CALL) 100.0% ( 75597)2.211.611.66.593.51.47.97.916.716.711.45.65.612.312.32.10.43.13.12.23.22.111.611.69008291.13% good,8.87% failed(95.0%)(59.7%)(16.5%)95.095.048970/4897059.74.89759.716.516.512.212.212.212.211.511.57.87.87.87.82.26.493.611.511.51.42.10.25.911.25.65.62.10.43.13.12.03.290986total ( 9984.6/s)91.21% good,8.79% failed11.1(93.9%)(60.1%)(16.3%)93.993.949874/4987460.14.98760.116.316.312.112.112.012.07.77.73.12.15.55.56.76.76.393.71.411.311.311.42.10.25.92.10.42.07.77.711.33.03.09213091.30% good,8.70% failed832335497(92.7%)(59.8%)(16.2%)92.792.750261/5026159.85.02659.813614/1361416.21.36116.211.911.99502/95400.9509402/9402threading.py:3738276/82769.80.8289.87.67.65812/58120.581371/19100.40.037306/18700.40.031302/27510.40.0303.35.811.911.95.45.46.593.52.211.211.21.411.311.32.311.46.96.9BaseLockTests.test_acquire_contende...test/lock_tests.py:1742.02.03.03.09313191.39% good,8.61% failed836338498(91.6%)(16.0%)91.691.650533/5053359.45.05359.416.016.011.811.89938/997611.70.99411.79701/970111.40.97011.411.011.06097/60970.610371/2039306/1971302/29572.45.45.40.46.693.47.27.22.011.811.711.72.02.33.03.094035total ( 9989.9/s)91.46% good,8.54% failed(90.6%)(15.8%)90.690.651089/5108959.45.10959.415.815.810237/1023711.91.02411.910160/1019811.81.01611.911.310.910.95.3125/1250.10.0130.111.611.62.92.90.41.96.793.311.97.17.12.52.42.011.35.39513391.56% good,8.44% failed(89.5%)(59.9%)89.589.552187/5218759.95.21959.911335/1133513.01.13313.011.111.110.810.87.47.43.55.62.92.90.411.511.56.693.42.415.615.6(15.6%)1.911.711.711.75.22.07.07.05.39613491.65% good,8.35% failed(88.5%)(60.4%)(15.5%)88.588.553188/5318860.45.31960.415.515.512336/1233614.01.23414.011.511.610.710.77.37.35.26.96.911.66.593.52.92.911.011.011.311.32.41.93.45.22.05.59713491.73% good,8.27% failed(87.5%)(60.8%)(15.3%)87.587.554188/5418860.85.41960.815.315.313336/133361.33411.411.411.211.210.910.910.610.67.27.26.593.52.41.93.415.015.02.82.85.55.15.111.598044total ( 9991.1/s)91.81% good,8.19% failed(86.6%)(61.2%)(15.8%)86.686.655097/5509761.25.51061.214245/1424515.81.42515.811.311.310.410.46.493.67.17.15.411.111.11.911.43.42.82.810.810.82.35.15.115.115.19913591.90% good,8.10% failed11.3(85.6%)(61.7%)(16.5%)85.685.656188/5618861.75.61961.715075/1507516.51.50816.511.211.211.011.010.310.314.914.910.610.66.493.66.76.77.17.15.42.22.82.82.35.05.00.93.31.80m10s10013691.98% good,8.02% failed(84.6%)(62.1%)84.684.657189/5718962.15.71962.115075/150751.50814.811.011.110.510.510.210.27.07.02.72.75.314.86.393.71.211.22.26.66.62.310.910.9(16.4%)16.416.45.05.00.93.31.8 + diff --git a/Doc/library/tachyon-live-mode-2.gif b/Doc/library/tachyon-live-mode-2.gif deleted file mode 100644 index 7f98edd10f9933..00000000000000 Binary files a/Doc/library/tachyon-live-mode-2.gif and /dev/null differ diff --git a/Doc/library/tachyon-live-mode-2.svg b/Doc/library/tachyon-live-mode-2.svg new file mode 100644 index 00000000000000..39cedcd8027f72 --- /dev/null +++ b/Doc/library/tachyon-live-mode-2.svg @@ -0,0 +1,422 @@ + + + + + + + +Tachyon ProfilerPID:12964│Thread:ALL│Uptime:│Time:│Interval:100µs│Display:10.0HzSamples:[]Efficiency:[]Threads:% on gil│% off gil│% waiting for gil│% exc│% GCFunctions:total│exec│stack│34shownTop 3:││───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────nsamples%▼tottime%functionfile:line0.003nsamples: direct/cumulative (direct=executing, cumulative=on stack)Sort: Total Time | 't':mode 'x':trends ←→:thread 'h':help 'q':quit0m06stotal (10000.6/s)15.915.9██████████████████████████████10.0KHz/10.0KHz (100%)████████████████████████████████████████████████████████████0.98513934583.0Popen._execute_child151/1510.015FileLoader.get_data<frozen importlib._bootstrap_external🥇 sleeping_retry🥈 Condition.waitsleeping_retrytest/support/__init__.py:2782Condition.waitthreading.py:370🥉 BarrierTests.test_time...9969/99690.997BarrierTests.test_timeout.<locals>.ftest/lock_tests.py:122521527/215272.153<GC>~:06401/64010.640_PollLikeSelector.selectselectors.py:3981092/15850.109_call_with_frames_removed<frozen importlib._bootstrap>:491114/1160.011GCTests.test_trashcan_threads.<local...test/test_gc.py:49094/940.009_compile_bytecode60/600.006test/test_gc.py:47259/59GCTests.test_resurrection_is_transitivetest/test_gc.py:102638/380.004_FuncBuilder.add_fns_to_classdataclasses.py:49934/340.003FileLoader.get_data<frozen importlib._bootstrap_external30/30GCTests.test_resurrection_does_not_b...test/test_gc.py:107828/28findteststest/libregrtest/findtests.py:4728/28GCTests.test_legacy_finalizertest/test_gc.py:185GCTests.test_resurrection_only_happe...test/test_gc.py:99027/27FileFinder._fill_cache27/27GCTests.test_get_objects_generationstest/test_gc.py:93626/26GCCallbackTests.test_collect_garbagetest/test_gc.py:14040.50.50.50.4GCTests.test_trashcan_threads.<local...0.40.40.30.3gc_collect0.20.10.10.0060.00328/28test/support/__init__.py:963test/support/__init__.py:964gc_collect0.20.20.20.003<frozen importlib._bootstrap_externalGCTests.test_trashcan_threads.<local...0.10.10.10.10.10.10.10.10.0030.0030.0030.003<frozen importlib._bootstrap_external10:30:57445/4450.045Popen._try_waitsubprocess.py:2208Popen._execute_child0.024225/2680.023test/test_gc.py:499BaseLockTests.test_acquire_contended...test/lock_tests.py:1745049/50490.505BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2494618/46220.462GCTests.test_trashcan_threads.<local...test/test_gc.py:4742686/26860.269GCTests.test_trashcan_threadstest/test_gc.py:5162321/23210.232Condition.waitthreading.py:3632.00.10.1wait_threads_blockedtest/lock_tests.py:250.80.80.50.60.60.50.5gc_collecttest/support/__init__.py:962GCTests.test_trashcan_threads.<local...0.10.10.10.10.00.013.087.04.215.32.91.51.53.2853459394819/8190.082gc_collecttest/support/__init__.py:962318/3180.032subprocess.py:2086277/2770.028subprocess.py:2109244/5081test/test_gc.py:500gc_collecttest/support/__init__.py:963test/support/__init__.py:964151/1510.015FileLoader.get_data<frozen importlib._bootstrap_external0.60.60.5Popen._execute_child0.30.30.00.00.00.0█████████████████████████████░5460/54600.546BaseLockTests.test_acquire_contended...test/lock_tests.py:1740.70.0400.0220.020222/1797203/16940.40.10.18869/88860.8871.9BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2496401/64010.640_PollLikeSelector.selectselectors.py:3981.41.40.00.00.00.00.00.00.10.10.3████████████████████████████████████████████████████████████<GC>~:00.70.7🥉 BaseLockTests.test_tim...BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2499969/99690.997BarrierTests.test_timeout.<locals>.ftest/lock_tests.py:12250.00.010100/101001.0100m07s10:30:589486/95030.9494.24.22.5397/24913.9222/1970203/18782686/26860.269GCTests.test_trashcan_threadstest/test_gc.py:5161.734/34FileLoader.get_data1092/15850.109_call_with_frames_removed<frozen importlib._bootstrap>:491445/4450.045Popen._try_waitsubprocess.py:22080.040gc_collecttest/support/__init__.py:962318/3180.032subprocess.py:2086277/2770.028subprocess.py:2109244/50810.024test/test_gc.py:500225/2680.023test/test_gc.py:4990.0220.020151/1510.015FileLoader.get_data<frozen importlib._bootstrap_external114/1160.011GCTests.test_trashcan_threads.<local...test/test_gc.py:49094/940.009_compile_bytecode60/60test/test_gc.py:47259/59GCTests.test_resurrection_is_transitivetest/test_gc.py:102638/380.004_FuncBuilder.add_fns_to_classdataclasses.py:49930/30GCTests.test_resurrection_does_not_b...test/test_gc.py:1078findteststest/libregrtest/findtests.py:47GCTests.test_legacy_finalizertest/test_gc.py:185GCTests.test_resurrection_only_happe...test/test_gc.py:99027/27FileFinder._fill_cache0.4Popen._execute_childGCTests.test_trashcan_threads.<local...0.00628/280.4<frozen importlib._bootstrap_externalGCTests.test_trashcan_threads.<local...<frozen importlib._bootstrap_externalConditionTests._check_notify.<locals>.ftest/lock_tests.py:66128/28<frozen importlib._bootstrap_external398Condition.waitCondition.waitthreading.py:373BaseLockTests.test_acquire_contended...test/lock_tests.py:1744618/46220.462GCTests.test_trashcan_threads.<local...test/test_gc.py:474Condition.waitthreading.py:3736401/64010.640_PollLikeSelector.selectselectors.py:398threading.py:363ConditionTests._check_notify.<locals>.ftest/lock_tests.py:66134/34FileLoader.get_data<frozen importlib._bootstrap_external5460/54600.546BaseLockTests.test_acquire_contended...test/lock_tests.py:174wait_threads_blockedtest/lock_tests.py:252686/26860.269GCTests.test_trashcan_threadstest/test_gc.py:5160.0044735840/58400.584397/2626222/2071203/19819632/96320.9633683/36830.36835/359825/98420.9830.6████████████████████████████████████████████████████████████🥇 Condition.wait🥈 sleeping_retryCondition.waitthreading.py:370sleeping_retrytest/support/__init__.py:27827.77.76.53.83.83.70m08s10:30:599969/99690.997BarrierTests.test_timeout.<locals>.ftest/lock_tests.py:1225threading.py:3632.16401/64010.640_PollLikeSelector.selectselectors.py:398491/4910.049ConditionTests.test_waitfortest/lock_tests.py:786445/4450.045Popen._try_waitsubprocess.py:2208Popen._execute_childGCTests.test_trashcan_threads.<local...gc_collect0.20.20.30.0060.00428/284031.40.3417/4170.042ConditionTests.test_waitfor_timeouttest/lock_tests.py:8070.040318/3180.032subprocess.py:2086277/2770.028Popen._execute_childsubprocess.py:2109244/50810.024test/test_gc.py:500225/2680.023GCTests.test_trashcan_threads.<local...test/test_gc.py:4990.022test/support/__init__.py:9630.020gc_collecttest/support/__init__.py:964151/1510.015FileLoader.get_dataGCTests.test_trashcan_threads.<local...0.5114/1160.011test/test_gc.py:49094/940.009_compile_bytecode<frozen importlib._bootstrap_external60/60test/test_gc.py:47259/590.006GCTests.test_resurrection_is_transitivetest/test_gc.py:102638/38_FuncBuilder.add_fns_to_classdataclasses.py:4990.00435/35ConditionTests._check_notify.<locals>.ftest/lock_tests.py:66134/34FileLoader.get_data<frozen importlib._bootstrap_external30/30GCTests.test_resurrection_does_not_b...test/test_gc.py:1078findteststest/libregrtest/findtests.py:47GCTests.test_legacy_finalizertest/test_gc.py:1852.545544/455444.55413.013.03.53.56.60.59969/99690.997BarrierTests.test_timeout.<locals>.ftest/lock_tests.py:12259633/96330.963🥉 Condition.waitCondition.waitthreading.py:36310100/101001.01012187/121871.219wait_threads_blockedtest/lock_tests.py:254618/46220.462GCTests.test_trashcan_threads.<local...test/test_gc.py:474397/29774915125/51250.513222/2313203/222110658/106751.0660m09s10:31:0074519/745197.4526401/64010.640_PollLikeSelector.selectselectors.py:3980.004████████████████████████████████████████████████████████████5241.2125/1250.013LockTests.test_reacquire.<locals>.ftest/lock_tests.py:283125/1250.013LockTests.test_state_after_timeouttest/lock_tests.py:311114/1160.011test/test_gc.py:49094/940.009_compile_bytecode60/600.006GCTests.test_trashcan_threads.<local...test/test_gc.py:47259/590.006GCTests.test_resurrection_is_transitivetest/test_gc.py:102638/38_FuncBuilder.add_fns_to_classdataclasses.py:49936/360.004InterruptMainTests.test_can_interrup...test/test_threading.py:278235/350.004ConditionTests._check_notify.<locals>.ftest/lock_tests.py:66134/34FileLoader.get_data9474238717/87170.8725677/56770.568397/3238222/2494203/237411253/112701.1251.912255/122551.226Condition.waitthreading.py:36310:30:566029787.07% good, 12.93% failed13.186.94.5(51.9%)(41.0%)(19.0%)27265/2726551.92.72751.941.041.019.019.08330/83470.8335049/50499.60.5059.64618/46220.462GCTests.test_trashcan_threads.<local...test/test_gc.py:4742686/26865.10.2695.1GCTests.test_trashcan_threadstest/test_gc.py:5162321/23210.2321555/15553.00.1553.0BaseLockTests.test_acquire_contended...test/lock_tests.py:1742.1445/4450.80.0450.8Popen._try_waitsubprocess.py:2208318/3180.60.0320.6Popen._execute_childsubprocess.py:2086277/2770.028subprocess.py:2109244/50810.0249.7GCTests.test_trashcan_threads.<local...test/test_gc.py:500237/19400.50.024225/2680.0230.5test/test_gc.py:499223/2230.022wait_threads_blockedtest/lock_tests.py:25128/15100.20.0132.9125/14330.20.0130.10.10.10.10.10.10.10.10.00.00.10.1Condition.waitthreading.py:3638.88.83.7gc_collecttest/support/__init__.py:96212.212.214.7BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2492.74.44.46129887.26% good, 12.74% failed12.687.44.3(51.4%)(40.2%)(18.6%)27488/2748851.42.74951.440.240.218.618.68498/85150.85012.012.05460/546010.20.54610.25.05.0819/8191.50.0821.5318/3180.032subprocess.py:2086277/2770.028subprocess.py:2109244/50810.0249.5GCTests.test_trashcan_threads.<local...test/test_gc.py:500237/1941128/15880.20.013125/15220.0132.80.10.10.10.13.64.34.33.09.49.40.214.78.68.66229987.46% good, 12.54% failed(51.7%)(39.5%)(18.3%)28158/2815851.72.81651.739.539.518.318.38669/86860.8675460/546010.00.54610.04.94.9397/21770.70.0409.3222/17400.40.022203/16370.40.02011.711.74.34.33.09.39.34.08.58.563264total ( 9988.1/s)87.64% good, 12.36% failed(52.2%)(38.8%)(18.0%)28916/2891652.22.89252.238.838.818.018.08869/888616.00.88716.05686/568610.30.56910.39.89.84.24.24.19.28.38.33.111.511.50.4397/22634.84.8BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2496434387.85% good, 12.15% failed12.787.34.115.1(53.1%)(38.1%)(17.6%)29995/2999553.13.00053.138.138.117.617.66765/676512.00.67612.09.79.74.14.14.09.03.20.411.311.315.715.73.0total (10000.5/s)397/22634.84.88.28.22.86527688.02% good, 11.98% failed12.587.54.014.9(53.8%)(37.5%)(17.3%)30928/3092853.83.09353.837.537.517.317.315.47698/769813.40.77013.44.74.78.811.111.115.50.4total ( 9990.2/s)4.04.03.93.12.98.08.0397/22632.89.59.56627188.20% good, 11.80% failed12.387.7(36.8%)(17.1%)31923/3192354.63.19254.636.836.817.117.115.215.28693/86930.8694.64.68.7total ( 9983.9/s)14.914.97.97.911.011.09.39.34.04.03.93.12.9397/2263(54.6%)14.73.92.76737988.40% good, 11.60% failed12.088.014.4(55.5%)(36.1%)33031/3303155.53.30355.536.136.116.716.79801/980116.50.98016.514.914.910.710.74.54.53.93.93.88.59.29.27.87.80.81.82.8total (10000.5/s)397/2263BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2493.92.7(16.7%)8869/88860.8873.06838088.57% good, 11.43% failed11.988.1(56.2%)(35.5%)34032/3403256.23.40356.235.535.510100/1010016.71.01016.716.516.514.710.610.68.43.83.83.82.63.77.67.60.81.82.89.09.0total (10000.5/s)397/22630.20.214.214.64.44.4(16.7%)8869/88860.8873.06930988.72% good, 11.28% failed11.888.2(56.8%)(35.0%)(16.4%)34944/3494456.83.49456.835.035.016.416.416.216.28886/890314.50.88914.510.410.48.98.9397/22808.32.93.83.83.82.63.70.10.11.31.30.8total ( 9990.2/s)14.01.82.80.27.57.50.24.44.47038088.88% good, 11.12% failed12.287.83.7856462(56.6%)(34.4%)35405/3540556.63.54156.634.434.416.116.115.915.99486/950315.20.94915.210.210.27.47.42751/27514.40.2754.42686/26860.269GCTests.test_trashcan_threadstest/test_gc.py:5161.72.5397/24918.1222/19703.1203/18781.31.30.8Condition.waitthreading.py:363(16.1%)4.34.33.00.40.48.78.74.0total (10000.5/s)0.20.214.77126289.02% good, 10.98% failed11.688.43.6863397466(56.0%)(33.9%)(15.9%)35500/3550056.03.55056.033.933.915.915.915.015.010.110.18.68.67.37.33696/36965.80.3705.83432/34325.40.3435.4threading.py:3731606/16062.50.1612.58.03.032/32total ( 9983.9/s)0.8Condition.waitthreading.py:36315.715.714.53.10.772208total ( 9976.6/s)89.16% good, 10.84% failed11.488.63.514.3866468(55.8%)(33.4%)(15.7%)35952/3595255.83.59555.833.433.415.715.715.514.714.89.99.94781/47817.40.4787.4threading.py:3734618/46220.462GCTests.test_trashcan_threads.<local...test/test_gc.py:4744130/41300.4132098/20983.30.2103.37.933/332.96.46.415.50.87.27.28.58.53.10.772923total ( 9937.6/s)9.9KHz/10.0KHz (99.4%)89.26% good, 10.74% failed11.288.83.4868470(55.4%)(33.1%)(15.5%)36090/3609055.43.60955.433.133.115.315.39701/97180.9709.89.85950/59509.10.5959.15460/54600.5465416/54168.30.5428.37.17.14.14.12450/24503.80.2453.87.83.033/330.10.115.515.53.114.914.98.48.4Condition.waitthreading.py:3632.4397/2561222/2041203/19520.714.50.73.97438389.47% good, 10.53% failed10.889.23.2869471(32.3%)36316/3631654.63.63254.632.332.315.215.215.015.09701/97180.9709632/963214.50.96314.59.69.65840/58408.80.5848.88.28.26.96.93683/36830.3687.635/350.10.10.00.02.90.10.114.64.04.05.55.51.62.4397/2561222/2041203/19520.7(15.2%)3.83.1total (10000.5/s)(54.6%)14.214.60.775310total ( 9990.8/s)89.60% good, 10.40% failed10.589.53.1(55.0%)(37.0%)(15.0%)37117/3711755.03.71255.024967/2496737.02.49737.014.814.89825/98420.98314.314.38.78.78.18.17.53.114.64.04.01.66.86.85.55.50.715.015.08712.914.214.69.59.52.30.73.976196total ( 9975.9/s)89.72% good, 10.28% failed10.189.93.0(55.6%)(43.0%)(14.8%)38003/3800355.63.80055.629397/2939743.02.94043.014.614.614.414.414.114.18.08.07.42.91.66.86.85.45.43.93.914.03.014.814.89.49.40.78713.88.58.52.30.77738489.88% good, 10.12% failed9.690.42.913.8(56.4%)(50.8%)(14.5%)39191/3919156.43.91956.435337/3533750.83.53450.814.514.514.314.314.114.213.813.87.97.96.67.39.29.21.68.48.43.93.95.35.3total (10000.4/s)3.06.68713.82.82.37838490.00% good, 10.00% failed9.390.72.813.6(57.2%)(57.0%)40337/4033757.24.03457.240191/4019157.04.01957.014.314.314.114.113.914.013.713.79.19.17.28.38.32.20.3total (10000.4/s)1.56.68712.9(14.3%)2.85.25.279009total ( 9953.1/s)90.08% good,9.92% failed9.091.013.5(61.1%)(57.3%)(14.2%)43462/4346261.14.34661.140816/4081657.34.08257.314.014.013.813.813.513.56.57.114.214.20.10.12.20.32.70.40.41.58712.90.10.19.09.08.28.22.85.25.280307total ( 9988.4/s)90.24% good,9.76% failed872399(61.3%)(58.1%)(13.9%)44406/4440661.34.44161.342114/4211458.14.21158.113.913.913.813.813.613.613.313.38.18.17.57.53.67.02.75.15.16.46.413.32.20.32.70.40.43.73.71.58.991.18.88.82.98140390.38% good,9.62% failed877401476(60.4%)(58.6%)(13.7%)44406/4440660.44.44160.443106/4310658.64.31158.613.613.69927/994413.50.99313.513.113.15871/58710.587397/26663.66.9222/2095203/201913.713.72.20.37.47.4total (10000.4/s)2.60.40.43.73.71.513.28.991.18.78.78.08.02.86.36.32.75.05.082274total ( 9984.8/s)90.47% good,9.53% failed880402(59.7%)(58.2%)(13.6%)44418/4441859.74.44259.743337/4333758.24.33458.213.613.610074/1009113.51.00713.613.413.46456/64568.70.6468.70.70.7397/27560.040318/3180.032subprocess.py:2086277/2770.028subprocess.py:2109244/50810.0246.8test/test_gc.py:500225/2680.0230.4test/test_gc.py:499222/21390.022test/support/__init__.py:963203/20320.020test/support/__init__.py:964151/1510.015FileLoader.get_data114/1160.011test/test_gc.py:49094/940.009_compile_bytecode<frozen importlib._bootstrap_external60/60GCTests.test_trashcan_threads.<local...test/test_gc.py:47259/59GCTests.test_resurrection_is_transitivetest/test_gc.py:102638/38_FuncBuilder.add_fns_to_classdataclasses.py:49935/3534/34FileLoader.get_data<frozen importlib._bootstrap_external30/30GCTests.test_resurrection_does_not_b...test/test_gc.py:1078findteststest/libregrtest/findtests.py:47GCTests.test_legacy_finalizertest/test_gc.py:185GCTests.test_resurrection_only_happe...test/test_gc.py:9907.37.30.10.113.34784.94.93.63.62.612.912.91.58.991.12.76.26.23.7gc_collecttest/support/__init__.py:9622.98.68.683403total ( 9998.9/s)90.60% good,9.40% failed8.891.2881(60.3%)(13.4%)45544/4554460.34.55460.344049/440494.40513.413.410074/100911.00713.213.29633/96336456/64560.6460.60.6397/27566.70.4222/2139203/20320.23.613.46.16.14784.94.93.63.62.67.27.20.212.712.7(58.3%)58.358.313.32.72.88.58.58.58.513.18441690.71% good,9.29% failed8.791.3886483(59.5%)(57.9%)🥉 <GC>(13.4%)59.559.544364/4436457.94.43657.910266/1028313.41.02713.4<GC>~:010100/101001.01013.29633/963312.612.69591/959112.50.95912.54185/41850.419397/2871222/2182203/20663.75.55.513.28.48.4total (10000.4/s)2.70.27.17.12.813.2BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2496.06.085396total ( 9998.0/s)90.81% good,9.19% failed8.691.4889486(58.7%)(15.7%)58.758.744855/4485557.84.48657.812187/1218715.71.21915.710351/103681.03512.412.44581/45815.90.4585.9397/2906222/2205203/20932.78.38.3(57.8%)13.412.912.97.07.013.32.813.13.72.06.06.086288total ( 9984.9/s)90.90% good,9.10% failed8.491.62.4892489(62.3%)(57.4%)(15.5%)48896/4889662.34.89062.345058/4505857.44.50657.410491/105081.04912.912.912.312.35125/51250.5135.95.93.86.5222/2240203/212715.515.52.712.712.77.07.013.413.43.43.42.913.12.08.28.26.56.58742291.01% good,8.99% failed8.391.7(67.5%)(15.3%)53686/5368667.55.36967.546016/4601657.84.60257.815.315.310658/106751.06612.712.712.512.512.112.15.85.86.42.86.46.4(57.8%)total (10000.4/s)6.96.989413.413.43.43.42.913.22.38.08.03.72.088349total ( 9992.1/s)91.10% good,8.90% failed8.092.013.0(72.5%)(15.1%)58316/5831672.55.83272.546942/469424.69415.115.112.512.512.412.412.012.06.86.86.32.92.86.46.413.2894(58.3%)58.358.32.33.33.35.75.78.08.03.713.32.089242total ( 9980.2/s)91.19% good,8.81% failed7.892.22.212.9(77.1%)(58.8%)(15.0%)62781/6278177.16.27877.147835/4783558.84.78458.813.113.112.212.211.811.86.76.71.91.312.412.47.97.989415.015.03.33.32.75.75.72.86.26.36.33.790197total ( 9975.3/s)91.29% good,8.71% failed7.692.412.7(82.0%)(59.3%)(14.8%)67556/6755682.06.75682.048790/4879059.34.87959.312.913.012.112.111.711.76.66.65.65.63.612.312.31.91.37.87.814.814.88942.13.33.36.26.22.72.86.291377total ( 9995.3/s)91.40% good,8.60% failed(88.0%)(59.8%)(14.6%)73456/7345688.07.34688.049970/4997059.84.99759.814.614.612.812.812.112.111.911.911.511.57.77.76.13.61.97.392.71.36.16.112.65.55.58942.16.56.53.23.22.72.89242491.49% good,8.51% failed(88.1%)(14.4%)74519/7451988.17.45288.151015/5101560.35.10260.314.414.412.612.611.911.911.811.811.411.46.03.51.91.37.67.67.292.86.16.1total (10000.4/s)5.55.52.62.0894(60.3%)6.56.53.23.212.42.79342491.58% good,8.42% failed895404(87.1%)(60.8%)87.187.151992/5199260.85.19960.810680/1069712.51.06812.511.811.811.711.711.311.36.46.4397/299914.214.212.33.51.91.37.292.85.45.43.13.1total (10000.4/s)2.62.02.77.57.56.06.0(14.2%)5.994412total ( 9999.1/s)91.65% good,8.35% failed938419519(86.1%)86.186.152214/5221460.35.22160.312255/122551.22610900/1091712.61.09012.611.511.511.111.18190/81909.50.8199.55573/55730.557397/30923.6222/2390203/22712.636/36InterruptMainTests.test_can_interrup...test/test_threading.py:278235/35ConditionTests._check_notify.<locals>.ftest/lock_tests.py:66134/34FileLoader.get_data30/30GCTests.test_resurrection_does_not_b...test/test_gc.py:1078findteststest/libregrtest/findtests.py:4714.214.26.46.41.37.292.811.711.73.13.15.35.37.47.42.82.01.8(60.3%)12.4(14.2%)5.99542591.73% good,8.27% failed946422(85.1%)(60.2%)(14.0%)85.185.152716/5271660.25.27260.214.014.011169/1118612.81.11712.811.411.411.011.08717/871710.00.87210.05677/56770.568397/32025.8222/2471203/23497.37.30.10.13.77.392.711.511.53.13.15.35.312.6total (10000.4/s)2.72.82.01.812255/122551.2260.10.16.56.596424total (10000.1/s)91.82% good,8.18% failed12.5(84.2%)(60.6%)(13.8%)84.284.253628/5362860.65.36360.613.813.811253/1127012.71.12512.710921/1092112.31.09212.310.910.99.89.83.76.46.47.392.711.311.37.27.22.01.83.03.012255/122551.2262.72.85.75.25.29742691.90% good,8.10% failed(83.2%)(61.0%)(13.7%)83.283.254630/5463061.05.46361.011923/1192313.31.19213.312.612.610.810.89.79.73.613.713.711.111.17.292.8total (10000.4/s)7.17.12.01.812.43.03.012255/122551.2262.72.86.36.3BaseLockTests.test_timeout.<locals>.ftest/lock_tests.py:2495.75.25.298426total (10000.3/s)91.99% good,8.01% failed(82.3%)(61.4%)82.382.355630/5563061.45.56361.412923/1292314.31.29214.313.513.510.610.69.69.63.65.15.112.37.192.95.612.412.411.011.07.17.12.61.83.03.02.86.36.3(14.3%)0.29942792.07% good,7.93% failed12.2(81.4%)(61.9%)81.481.456631/5663161.95.66361.913924/1392415.21.39215.213.410.910.910.510.59.59.512.312.33.513.47.192.95.60.3total (10000.4/s)2.67.07.02.76.26.2(15.2%)0.22.92.91.70.35.05.00m10s100364total ( 9994.0/s)92.14% good,7.86% failed7.093.012.1(80.6%)(62.3%)80.680.657568/5756862.35.75762.314861/1486116.11.48616.110.810.810.410.49.49.45.53.56.16.10.3(16.1%)6.96.92.613.32.712.212.213.30.22.92.91.70.35.05.0 + diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst index 98d1ebfd7f603e..3131107be97189 100644 --- a/Doc/library/tarfile.rst +++ b/Doc/library/tarfile.rst @@ -781,7 +781,7 @@ call the :meth:`~TarInfo.replace` method to create a modified copy in one step. Several attributes can be set to ``None`` to indicate that a piece of metadata is unused or unknown. -Different :class:`TarInfo` methods handle ``None`` differently: +Different :class:`TarFile` methods handle ``None`` differently: - The :meth:`~TarFile.extract` or :meth:`~TarFile.extractall` methods will ignore the corresponding metadata, leaving it set to a default. @@ -1149,11 +1149,11 @@ reused in custom filters: links (symbolic and hard) to arbitrary files on the filesystem, potentially granting unintended access to these files. - .. versionchanged:: next + .. versionchanged:: 3.16 Filenames containing ``..`` components are now normalized. - .. versionchanged:: next + .. versionchanged:: 3.16 Filenames with a drive that are not absolute are now refused. @@ -1204,7 +1204,7 @@ reused in custom filters: Link targets are now normalized. - .. versionchanged:: next + .. versionchanged:: 3.16 Link targets with a drive that are not absolute are now refused. @@ -1476,7 +1476,7 @@ The same example using the :keyword:`with` statement:: How to create and write an archive to stdout using :data:`sys.stdout.buffer ` in the *fileobj* parameter -in :meth:`TarFile.add`:: +in :func:`~tarfile.open`:: import sys import tarfile diff --git a/Doc/library/test.rst b/Doc/library/test.rst index 20810a537d4fe1..309f112b986d13 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -517,6 +517,8 @@ The :mod:`!test.support` module defines the following functions: ``True`` if called by a function whose ``__name__`` is ``'__main__'``. Used when tests are executed by :mod:`test.regrtest`. + If called at the top level, sets label "requires\_\ *resource*" on the module. + .. function:: sortdict(dict) @@ -533,6 +535,29 @@ The :mod:`!test.support` module defines the following functions: rather than looking directly in the path directories. +.. function:: mark(label, value=True, /) + + Add a label to a test. Use ``@mark('label')`` as a decorator of a test + method or class. + + The optional *value* (``True`` by default) is matched on the command line + by ``--label label=value``, whereas ``--label label`` matches any value. + + Many :mod:`test.support` decorators like :func:`requires_resource`, + :func:`~test.support.cpython_only` or :func:`bigmemtest` add labels + automatically. + + +.. function:: mark_module(label, value=True, /, *, globals=None) + + Add a label to every test of a module. Call it at the module level:: + + test.support.mark_module('pickletest') + + The module is the caller, unless its :func:`globals` dict is passed as the + *globals* argument. + + .. function:: get_pagesize() Get size of a page in bytes. @@ -777,26 +802,31 @@ The :mod:`!test.support` module defines the following functions: .. decorator:: requires_zlib Decorator for skipping tests if :mod:`zlib` doesn't exist. + Adds label "requires_zlib". .. decorator:: requires_gzip Decorator for skipping tests if :mod:`gzip` doesn't exist. + Adds label "requires_gzip". .. decorator:: requires_bz2 Decorator for skipping tests if :mod:`bz2` doesn't exist. + Adds label "requires_bz2". .. decorator:: requires_lzma Decorator for skipping tests if :mod:`lzma` doesn't exist. + Adds label "requires_lzma". .. decorator:: requires_resource(resource) Decorator for skipping tests if *resource* is not available. + Adds label "requires\_\ *resource*". .. decorator:: requires_docstrings @@ -813,12 +843,17 @@ The :mod:`!test.support` module defines the following functions: .. decorator:: cpython_only Decorator for tests only applicable to CPython. + Adds label "impl_detail_cpython". .. decorator:: impl_detail(msg=None, **guards) Decorator for invoking :func:`check_impl_detail` on *guards*. If that returns ``False``, then uses *msg* as the reason for skipping the test. + For every keyword argument *name* adds a label + "impl_detail\_\ *name*" if its value is true or + "impl_detail_no\_\ *name*" otherwise. + .. decorator:: thread_unsafe(reason=None) @@ -853,10 +888,13 @@ The :mod:`!test.support` module defines the following functions: method may be less than the requested value. If *dry_run* is ``False``, it means the test doesn't support dummy runs when ``-M`` is not specified. + Adds label "bigmemtest". + .. decorator:: bigaddrspacetest Decorator for tests that fill the address space. + Adds label "bigaddrspacetest". .. function:: linked_to_musl() @@ -1706,22 +1744,113 @@ The :mod:`!test.support.import_helper` module provides support for import tests. .. versionadded:: 3.10 +Creating module objects +----------------------- + +.. function:: create_module(name, loader=None, *, ispkg=False) + + Create a new, empty :term:`module` or :term:`package` and return it. + + *name*, *loader* and *ispkg* (as ``is_package``) are passed to + :class:`importlib.machinery.ModuleSpec`. + + +.. function:: add_module(spec, *, parents=True) + + Create a :term:`module` from a name or spec and add it to :data:`sys.modules`. + + If *parents* is ``True``, create any missing parent modules. + + Return the new module. + + +.. function:: add_package(spec, *, parents=True) + + Create a :term:`package` from a name or spec and add it to :data:`sys.modules`. + + If *parents* is ``True``, create any missing parent modules. + + Return the new package. + + +Manipulating sys.modules +------------------------ -.. function:: forget(module_name) - Remove the module named *module_name* from ``sys.modules`` and delete any +.. function:: unload(name) + + Remove the module named *name* from :data:`sys.modules`. + + +.. function:: forget(modname) + + Remove the module named *modname* from :data:`sys.modules` and delete any byte-compiled files of the module. -.. function:: import_fresh_module(name, fresh=(), blocked=(), deprecated=False) +.. function:: modules_setup() + + Return a tuple containing a copy of :data:`sys.modules`. + + +.. function:: modules_cleanup(oldmodules) + + Remove modules except for *oldmodules* and ``encodings`` from :data:`sys.modules` + in order to preserve the internal cache. + + +.. function:: isolated_modules() + + A context manager that makes a copy of :data:`sys.modules` on entry and restores + it on exit. + + +.. class:: CleanImport(*module_names, usefrozen=False) + + A context manager to force imports to return new module references. This + is useful for testing module-level behaviors, such as the emission of a + :exc:`DeprecationWarning` on import. + + When created, this makes a copy of :data:`sys.modules` and removes names from + *module_names* from the original. On exit, the original module references are + restored. + + If *usefrozen* is False (the default) then the frozen importer is + disabled (except for essential modules like ``importlib._bootstrap``). + + Example usage:: + + with CleanImport('foo'): + importlib.import_module('foo') # New reference. + + +Importing modules +----------------- + +.. 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 + cannot be imported. + + Module and package deprecation messages are suppressed during this import + if *deprecated* is ``True``. If a module is required on a platform but + optional for others, set *required_on* to an iterable of platform prefixes + which will be compared against :data:`sys.platform`. + + If called at the top level, sets label "requires\_\ *name*" on the module. + + .. versionadded:: 3.1 + +.. function:: import_fresh_module(name, fresh=(), blocked=(), *, deprecated=False, usefrozen=False) This function imports and returns a fresh copy of the named Python module - by removing the named module from ``sys.modules`` before doing the import. - Note that unlike :func:`importlib.reload`, the original module is not + by removing the named module from :data:`sys.modules` before doing the import. + Note that unlike :func:`reload `, the original module is not affected by this operation. *fresh* is an iterable of additional module names that are also removed - from the ``sys.modules`` cache before doing the import. + from the :data:`sys.modules` cache before doing the import. *blocked* is an iterable of module names that are replaced with ``None`` in the module cache during the import to ensure that attempts to import @@ -1729,11 +1858,14 @@ The :mod:`!test.support.import_helper` module provides support for import tests. The named module and any modules named in the *fresh* and *blocked* parameters are saved before starting the import and then reinserted into - ``sys.modules`` when the fresh import is complete. + :data:`sys.modules` when the fresh import is complete. Module and package deprecation messages are suppressed during this import if *deprecated* is ``True``. + If *usefrozen* is False (the default) then the frozen importer is + disabled (except for essential modules like ``importlib._bootstrap``). + This function will raise :exc:`ImportError` if the named module cannot be imported. @@ -1749,65 +1881,91 @@ The :mod:`!test.support.import_helper` module provides support for import tests. .. versionadded:: 3.1 -.. function:: import_module(name, deprecated=False, *, required_on=()) +.. function:: ensure_module_imported(name, *, clearnone=True) - This function imports and returns the named module. Unlike a normal - import, this function raises :exc:`unittest.SkipTest` if the module - cannot be imported. + Import and return the named module. - Module and package deprecation messages are suppressed during this import - if *deprecated* is ``True``. If a module is required on a platform but - optional for others, set *required_on* to an iterable of platform prefixes - which will be compared against :data:`sys.platform`. + If the module is already imported, this is returned. Otherwise, import + the module and return it. If the module is not found a new, empty module + will be created. - .. versionadded:: 3.1 + If *clearnone* is ``True``, this will first remove any ``None`` values that + would block the import. -.. function:: modules_setup() +Manipulating sys.path +--------------------- - Return a copy of :data:`sys.modules`. +.. class:: DirsOnSysPath(*paths) + A context manager to temporarily add directories to :data:`sys.path`. -.. function:: modules_cleanup(oldmodules) + This makes a copy of :data:`sys.path`, appends any directories given + as positional arguments, then reverts :data:`sys.path` to the copied + settings when the context ends. - Remove modules except for *oldmodules* and ``encodings`` in order to - preserve internal cache. + Note that *all* :data:`sys.path` modifications in the body of the + context manager, including replacement of the object, + will be reverted at the end of the block. -.. function:: unload(name) +.. function:: ready_to_import(name=None, source="") - Delete *name* from ``sys.modules``. + A context manager that will create a new Python module *name* with *source* + source code in a temporary directory and inserts this directory at the start + of :data:`sys.path`. The context manager yields a tuple with the name and the path + to the script in the temporary directory. + If the name matches an existing module, this will be cleared from :data:`sys.modules` + on entry and restored on exit. -.. function:: make_legacy_pyc(source) + If no name is given, the module will be named "spam". + + + +Manipulating internals +---------------------- + +.. function:: make_legacy_pyc(source, allow_compile=False) Move a :pep:`3147`/:pep:`488` pyc file to its legacy pyc location and return the file system path to the legacy pyc file. The *source* value is the file system path to the source file. It does not need to exist, however the PEP - 3147/488 pyc file must exist. + 3147/488 pyc file must exist or *allow_compile* must be set. + If *allow_compile* is ``True`` this will create a .pyc file if it does not exist. -.. class:: CleanImport(*module_names) - 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 - :exc:`DeprecationWarning` on import. Example usage:: +.. function:: frozen_modules(enabled=True) - with CleanImport('foo'): - importlib.import_module('foo') # New reference. + A context manager that forces frozen modules to be used or excluded. + If *enabled* is ``True`` frozen modules are used, otherwise they are excluded. -.. class:: DirsOnSysPath(*paths) + This only applies to modules that have not yet been imported. Some essential + modules will always be imported frozen. - 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 - settings when the context ends. +.. function:: multi_interp_extensions_check(enabled=True) - Note that *all* :data:`sys.path` modifications in the body of the - context manager, including replacement of the object, - will be reverted at the end of the block. + A context manager that forces (if *enabled* is ``True``) or prevents legacy (single-phase init) + modules from being allowed in subinterpreters. + + This only applies to modules that haven't been imported yet. + It overrides the :c:member:`PyInterpreterConfig.check_multi_interp_extensions` + setting. + + +Lazy imports +------------ + +.. function:: ensure_lazy_imports(imported_module, modules_to_block, *, additional_code=None) + + Test that when *imported_module* is imported, none of the modules named in *modules_to_block* + are imported as a side effect. + + *additional_code* if given should be additional Python source code to execute before + checking that the blocked modules still haven't been imported. :mod:`!test.support.warnings_helper` --- Utilities for warnings tests diff --git a/Doc/library/this.rst b/Doc/library/this.rst new file mode 100644 index 00000000000000..2893d5584addc4 --- /dev/null +++ b/Doc/library/this.rst @@ -0,0 +1,42 @@ +:orphan: + +:mod:`!this` --- The Zen of Python +================================== + +.. module:: this + :synopsis: The Zen of Python + +.. + This easter egg module is deliberately missing from the table of contents. + +:pep:`20` -- The Zen of Python, by Tim Peters (RIP). + + | Beautiful is better than ugly. + | Explicit is better than implicit. + | Simple is better than complex. + | Complex is better than complicated. + | Flat is better than nested. + | Sparse is better than dense. + | Readability counts. + | Special cases aren't special enough to break the rules. + | Although practicality beats purity. + | Errors should never pass silently. + | Unless explicitly silenced. + | In the face of ambiguity, refuse the temptation to guess. + | There should be one-\- and preferably only one -\-obvious way to do it. + | Although that way may not be obvious at first unless you're Dutch. + | Now is better than never. + | Although never is often better than *right* now. + | If the implementation is hard to explain, it's a bad idea. + | If the implementation is easy to explain, it may be a good idea. + | Namespaces are one honking great idea -\- let's do more of those! + +`In Memoriam: Tim Peters 1952-2026 `__. + + +Usage +----- + +.. code-block:: shell-session + + $ python3 -m this diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst index 5d9a7b6314b166..5e80c30953955d 100644 --- a/Doc/library/threading.rst +++ b/Doc/library/threading.rst @@ -821,7 +821,7 @@ locks. In the locked state, some thread owns the lock; in the unlocked state, no thread owns it. Threads call a lock's :meth:`~RLock.acquire` method to lock it, -and its :meth:`~Lock.release` method to unlock it. +and its :meth:`~RLock.release` method to unlock it. .. note:: @@ -832,7 +832,7 @@ and its :meth:`~Lock.release` method to unlock it. RLock's :meth:`~RLock.acquire`/:meth:`~RLock.release` call pairs may be nested, unlike Lock's :meth:`~Lock.acquire`/:meth:`~Lock.release`. Only the final -:meth:`~RLock.release` (the :meth:`~Lock.release` of the outermost pair) resets +:meth:`~RLock.release` (the :meth:`~RLock.release` of the outermost pair) resets the lock to an unlocked state and allows another thread blocked in :meth:`~RLock.acquire` to proceed. diff --git a/Doc/library/timeit.rst b/Doc/library/timeit.rst index dd574fce09f5fc..09da385bfdf79a 100644 --- a/Doc/library/timeit.rst +++ b/Doc/library/timeit.rst @@ -148,7 +148,7 @@ The module defines three convenience functions and a public class: Automatically determine how many times to call :meth:`.timeit`. This is a convenience function that calls :meth:`.timeit` repeatedly - so that the total time >= *Timer.target_time* seconds, returning the eventual + so that the total time >= *target_time* seconds, returning the eventual (number of loops, time taken for that number of loops). It calls :meth:`.timeit` with increasing numbers from the sequence 1, 2, 5, 10, 20, 50, ... until the time taken is at least *target_time* seconds. diff --git a/Doc/library/tkinter.rst b/Doc/library/tkinter.rst index 653a91c5cbedba..31c0124d3597a9 100644 --- a/Doc/library/tkinter.rst +++ b/Doc/library/tkinter.rst @@ -17,7 +17,7 @@ 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. -Tkinter supports a range of Tcl/Tk versions, built either with or without +Tkinter supports a range of Tcl/Tk versions, which must be built with thread support. Tcl/Tk 8.5.12 is the minimum supported version; the official Python binary release bundles Tcl/Tk 9.0. @@ -27,6 +27,9 @@ supported versions. .. versionchanged:: 3.11 Support for Tcl/Tk versions older than 8.5.12 was removed. +.. versionchanged:: next + Support for Tcl/Tk built without thread support was removed. + 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 @@ -476,18 +479,11 @@ interpreter will fail. A number of special cases exist: -* Tcl/Tk libraries built without thread support are now rare: Tcl/Tk 9.0 (the - bundled version) is always thread-aware, so this case only arises with some - older 8.x builds. When the library is not thread-aware, - :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. - * 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. + them in separate threads. * 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 @@ -2023,6 +2019,31 @@ Base and mixin classes .. versionadded:: 3.16 + The following members support assistive technologies such as screen + readers. + They require Tk 9.1 or newer. + Tk automatically exposes the standard widgets to the platform + accessibility API; + the :attr:`!accessible` attributes allow overriding what is exposed, + and the :class:`Accessible` methods allow registering custom widgets. + If no assistive technology is active when the application starts, + setting the attributes has no effect, + reading them returns ``0``, + and the methods do nothing (except :meth:`!tk_check_screenreader`). + + .. method:: tk_check_screenreader() + + Return whether a screen reader is currently running. + + .. versionadded:: next + + .. attribute:: accessible + + The accessible attributes of the widget, + as an :class:`Accessible` object. + + .. versionadded:: next + The methods with the ``busy_`` prefix manage the busy state of a window, which shows a busy cursor and ignores user input. @@ -2455,6 +2476,55 @@ Base and mixin classes .. versionadded:: 3.11 +.. class:: Accessible() + + The accessible attributes of a widget, + returned by the :attr:`~Misc.accessible` property of the widget. + The attributes are exposed to assistive technologies + such as screen readers. + Requires Tk 9.1 or newer. + + Setting an attribute overrides what Tk exposes for the widget; + reading an attribute that has not been set raises + :exc:`~tkinter.TclError`. + A widget must have a role before any other attribute is set or read, + otherwise :exc:`~tkinter.TclError` is raised. + Tk sets the role of most standard widgets when they are mapped; + for other widgets, or before that, set :attr:`!role` first. + The supported roles are ``Button``, ``Canvas``, ``Checkbutton``, + ``Combobox``, ``Entry``, ``Label``, ``Listbox``, ``Menu``, + ``Notebook``, ``Progressbar``, ``Scale``, ``Scrollbar``, + ``Spinbox``, ``Table``, ``Text`` and ``Tree``. + + .. versionadded:: next + + .. attribute:: role + name + description + value + state + action + help + + The accessible attributes of the widget. + + .. method:: add_object() + + Register the widget with the platform accessibility API. + This is only needed for custom widgets; + the standard widgets are registered automatically. + + .. method:: emit_selection_change() + + Notify the platform accessibility API that the selection in the + widget has changed. + + .. method:: emit_focus_change() + + Notify the platform accessibility API that the widget has received + focus. + Not available on macOS. + .. class:: Wm() @@ -6820,13 +6890,13 @@ and :mod:`!tkinter.constants` namespaces. YES ON - Truthy values, all equal to the integer ``1``. + Values that are true, all equal to the integer ``1``. .. data:: FALSE NO OFF - Falsy values, all equal to the integer ``0``. + Values that are false, all equal to the integer ``0``. .. data:: N S @@ -6845,6 +6915,17 @@ and :mod:`!tkinter.constants` namespaces. edges) plus ``CENTER`` (``'center'``), used as values for the *anchor* and *sticky* options and by methods such as :meth:`Misc.grid_anchor`. +.. data:: EN + ES + WN + WS + + Additional values for the *labelanchor* option of :class:`LabelFrame` + and :class:`ttk.LabelFrame `, + which also accept the compass directions above. + + .. versionadded:: next + .. data:: LEFT RIGHT TOP @@ -6881,6 +6962,13 @@ and :mod:`!tkinter.constants` namespaces. Values for the *wrap* option of the :class:`Text` widget, selecting line wrapping on character or word boundaries. +.. data:: TABULAR + WORDPROCESSOR + + Values for the *tabstyle* option of the :class:`Text` widget. + + .. versionadded:: next + .. data:: BASELINE The text-alignment value ``'baseline'``. @@ -6891,6 +6979,12 @@ and :mod:`!tkinter.constants` namespaces. Values for the *bordermode* option of the placer (see :meth:`Place.place_configure`). +.. data:: IGNORE + + An additional value for the *bordermode* option of the placer. + + .. versionadded:: next + .. data:: INSERT CURRENT END @@ -6916,6 +7010,38 @@ and :mod:`!tkinter.constants` namespaces. Values for the *state* option of various widgets and items. +.. data:: READONLY + + An additional value for the *state* option of :class:`Entry`, + :class:`Spinbox` and the themed widgets. + + .. versionadded:: next + +.. data:: ALTERNATE + BACKGROUND + FOCUS + HOVER + INVALID + PRESSED + SELECTED + + Names of the themed widget states + (see :meth:`ttk.Widget.state `), + in addition to ``ACTIVE``, ``DISABLED`` and ``READONLY``. + + .. versionadded:: next + +.. data:: LEAF + OPEN + + Names of the themed widget states used by + :class:`ttk.Treeview `, + as are ``FIRST`` and ``LAST`` for the tabs of + :class:`ttk.Notebook `. + They require Tk 9.1 or newer. + + .. versionadded:: next + .. data:: CASCADE CHECKBUTTON COMMAND @@ -6925,6 +7051,14 @@ and :mod:`!tkinter.constants` namespaces. Menu entry types, used as the *itemType* argument of :meth:`Menu.add` and :meth:`Menu.insert`. +.. data:: MENUBAR + TEAROFF + + Values for the *type* option of the :class:`Menu` widget, + in addition to ``NORMAL``. + + .. versionadded:: next + .. data:: SINGLE BROWSE MULTIPLE @@ -6932,6 +7066,23 @@ and :mod:`!tkinter.constants` namespaces. Values for the *selectmode* option of the :class:`Listbox` widget. +.. data:: ITEM + CELL + + Values for the *selecttype* option of + :class:`ttk.Treeview `. + It requires Tk 9.0 or newer. + + .. versionadded:: next + +.. data:: TREE + HEADINGS + + Values for the *show* option of + :class:`ttk.Treeview `. + + .. versionadded:: next + .. data:: PIESLICE CHORD ARC @@ -6966,6 +7117,74 @@ and :mod:`!tkinter.constants` namespaces. Values for the *what* argument of :meth:`XView.xview_scroll` and :meth:`YView.yview_scroll`. +.. data:: PIXELS + + An additional value for the *what* argument of + :meth:`YView.yview_scroll` of the :class:`Text` widget. + + .. versionadded:: next + +.. data:: TEXT + IMAGE + + Values for the *compound* option of the themed widgets, + in addition to ``CENTER``, ``LEFT``, ``RIGHT``, ``TOP``, ``BOTTOM`` + and ``NONE``. + + .. versionadded:: next + +.. data:: ABOVE + BELOW + FLUSH + + Values for the *direction* option of :class:`Menubutton` and + :class:`ttk.Menubutton `, + in addition to ``LEFT`` and ``RIGHT``. + + .. versionadded:: next + +.. data:: ALWAYS + MIDDLE + NEVER + + Values for the *stretch* option of :meth:`PanedWindow.paneconfigure`, + in addition to ``FIRST`` and ``LAST``. + + .. versionadded:: next + +.. data:: DETERMINATE + INDETERMINATE + + Values for the *mode* option of + :class:`ttk.Progressbar `. + + .. versionadded:: next + +.. data:: HOLLOW + + An additional value for the *insertunfocussed* option of the + :class:`Text` widget, which also accepts ``NONE`` and ``SOLID``. + The option requires Tk 8.6 or newer. + + .. versionadded:: next + +.. data:: KEY + FOCUSIN + FOCUSOUT + + Values for the *validate* option of :class:`Entry`, :class:`Spinbox` + and :class:`ttk.Entry `, + in addition to ``ALL``, ``FOCUS`` and ``NONE``. + + .. versionadded:: next + +.. data:: FORCED + + An additional validation reason reported by the ``%V`` substitution + of the *validatecommand* option. + + .. versionadded:: next + .. data:: UNDERLINE NUMERIC DOTBOX diff --git a/Doc/library/traceback.rst b/Doc/library/traceback.rst index 8f167eb3524db2..8baf4b7d34905a 100644 --- a/Doc/library/traceback.rst +++ b/Doc/library/traceback.rst @@ -157,16 +157,19 @@ Module-Level Functions .. function:: print_list(extracted_list, file=None) - Print the list of tuples as returned by :func:`extract_tb` or - :func:`extract_stack` as a formatted stack trace to the given file. + Print a list of :class:`FrameSummary` objects as returned by + :func:`extract_tb` or :func:`extract_stack`, or a list of old-style tuples + (see :meth:`StackSummary.from_list`), as a formatted stack trace to the + given file. If *file* is ``None``, the output is written to :data:`sys.stderr`. .. function:: format_list(extracted_list) - Given a list of tuples or :class:`FrameSummary` objects as returned by - :func:`extract_tb` or :func:`extract_stack`, return a list of strings ready - for printing. Each string in the resulting list corresponds to the item with + Given a list of :class:`FrameSummary` objects as returned by + :func:`extract_tb` or :func:`extract_stack`, or a list of old-style tuples + (see :meth:`StackSummary.from_list`), return a list of strings ready for + printing. Each string in the resulting list corresponds to the item with the same index in the argument list. Each string ends in a newline; the strings may contain internal newlines as well, for those items whose source text line is not ``None``. @@ -330,8 +333,8 @@ the module-level functions described above. The :attr:`~BaseException.__notes__` value from the original exception, or ``None`` - if the exception does not have any notes. If it is not ``None`` - is it formatted in the traceback after the exception string. + if the exception does not have any notes. If it is not ``None``, + it is formatted in the traceback after the exception string. .. versionadded:: 3.11 @@ -536,8 +539,8 @@ in a :ref:`traceback `. .. attribute:: FrameSummary.line - A string representing the source code for this frame, with leading and - trailing whitespace stripped. + The source line at :attr:`~FrameSummary.lineno`, + with leading and trailing whitespace stripped. If the source is not available, it is ``None``. .. attribute:: FrameSummary.end_lineno diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst index 4a19acb430b0d1..6ce677b15a51b4 100644 --- a/Doc/library/tracemalloc.rst +++ b/Doc/library/tracemalloc.rst @@ -750,7 +750,7 @@ Traceback Example:: print("Traceback (most recent call first):") - for line in traceback: + for line in traceback.format(most_recent_first=True): print(line) Output:: diff --git a/Doc/library/turtle.rst b/Doc/library/turtle.rst index 39f8e5c6f226ee..dbc0cbb8fd09cc 100644 --- a/Doc/library/turtle.rst +++ b/Doc/library/turtle.rst @@ -496,9 +496,9 @@ Move and draw of the turtle; *extent*, an angle, determines which part of the circle is drawn. If *extent* is not given, draw the entire circle. If *extent* is not a full circle, one endpoint of the arc is the current pen position. - Draw the arc in counterclockwise direction if *radius* is positive, - otherwise in clockwise direction. Finally, the turtle's heading is changed - by *extent*. + Draw the arc in the counterclockwise direction if *radius* is positive, + otherwise in the clockwise direction. Finally, the turtle's heading is + changed by *extent*. As the circle is approximated by an inscribed regular polygon, *steps* determines the number of steps to use. If not given, it will be calculated @@ -551,9 +551,9 @@ Move and draw .. function:: stamp() - Stamp a copy of the turtle shape onto the canvas at the current turtle - position. Return a stamp_id for that stamp, which can be used to delete - it by calling ``clearstamp(stamp_id)``. + Stamp a copy of the turtle shape onto the canvas and return its id. The + stamp is placed at the current turtle position. The returned id can be + used to delete the stamp with :func:`clearstamp`. .. doctest:: :skipif: _tkinter is None @@ -565,10 +565,10 @@ Move and draw .. function:: clearstamp(stampid) - :param stampid: an integer, must be return value of previous - :func:`stamp` call + :param stampid: an integer - Delete stamp with given *stampid*. + Delete the stamp with the given *stampid*. *stampid* must be the return + value of a previous :func:`stamp` call. .. doctest:: :skipif: _tkinter is None @@ -587,11 +587,11 @@ Move and draw .. function:: clearstamps(n=None) - :param n: an integer (or ``None``) + :param n: an integer or ``None`` - Delete all or first/last *n* of turtle's stamps. If *n* is ``None``, delete - all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete - last *n* stamps. + Delete all or the first or last *n* of the turtle's stamps. If *n* is not + given, delete all stamps. If *n* is positive, delete the first *n* stamps. + If *n* is negative, delete the last *n* stamps, so -2 deletes the last two. .. doctest:: :skipif: _tkinter is None @@ -606,8 +606,9 @@ Move and draw .. function:: undo() - Undo (repeatedly) the last turtle action(s). Number of available - undo actions is determined by the size of the undobuffer. + Undo the last turtle action. Calling :func:`!undo` repeatedly undoes one + action at a time. The number of actions that can be undone is set by the + size of the undo buffer, see :func:`setundobuffer`. .. doctest:: :skipif: _tkinter is None @@ -625,8 +626,9 @@ Move and draw Return or set the turtle's speed. If *speed* is not given, return the current speed. Otherwise set the turtle's speed to an integer from 0 to 10. - If *speed* is a number greater than 10 or smaller than 0.5, the speed is - set to 0. You can also use these names instead of numbers: + A number above 0.5 and below 10.5 is rounded to the nearest integer. Any + other number sets the speed to 0. You can also use these names instead of + numbers: * "fastest": 0 * "fast": 10 @@ -845,46 +847,49 @@ Drawing state .. function:: pen(pen=None, **pendict) - :param pen: a dictionary with some or all of the below listed keys - :param pendict: one or more keyword-arguments with the below listed keys as keywords + :param pen: a dictionary or ``None`` + :param pendict: keyword arguments with the keys below - Return or set the pen's attributes in a "pen-dictionary" with the following - key/value pairs: + Return or set the pen's attributes. The attributes are returned as a + dictionary with the following keys and values: - * "shown": True/False - * "pendown": True/False - * "pencolor": color-string or color-tuple - * "fillcolor": color-string or color-tuple - * "pensize": positive number - * "speed": number in range 0..10 - * "resizemode": "auto" or "user" or "noresize" - * "stretchfactor": (positive number, positive number) - * "outline": positive number - * "tilt": number + * "shown": ``True`` or ``False`` + * "pendown": ``True`` or ``False`` + * "pencolor": a color string or a color tuple + * "fillcolor": a color string or a color tuple + * "pensize": a positive number + * "speed": a number from 0 to 10 + * "resizemode": "auto", "user" or "noresize" + * "stretchfactor": a pair of positive numbers + * "shearfactor": a number + * "outline": a positive number + * "tilt": a number - This dictionary can be used as argument for a subsequent call to :func:`pen` - to restore the former pen-state. Moreover one or more of these attributes - can be provided as keyword-arguments. This can be used to set several pen - attributes in one statement. + Such a dictionary can be passed as *pen* to restore a former pen state. The + attributes can also be given as keyword arguments, which can be used to set + several of them in one statement. .. doctest:: :skipif: _tkinter is None :options: +NORMALIZE_WHITESPACE >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10) - >>> sorted(turtle.pen().items()) - [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'), - ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'), - ('shearfactor', 0.0), ('shown', True), ('speed', 9), - ('stretchfactor', (1.0, 1.0)), ('tilt', 0.0)] - >>> penstate=turtle.pen() + >>> turtle.pen() + {'shown': True, 'pendown': True, 'pencolor': 'red', 'fillcolor': 'black', + 'pensize': 10, 'speed': 9, 'resizemode': 'noresize', + 'stretchfactor': (1.0, 1.0), 'shearfactor': 0.0, 'outline': 1, + 'tilt': 0.0} + >>> penstate = turtle.pen() >>> turtle.color("yellow", "") >>> turtle.penup() - >>> sorted(turtle.pen().items())[:3] - [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow')] + >>> turtle.pen()["pencolor"] + 'yellow' + >>> # restore the saved pen state, but with a green fill >>> turtle.pen(penstate, fillcolor="green") - >>> sorted(turtle.pen().items())[:3] - [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red')] + >>> turtle.pen()["pencolor"] + 'red' + >>> turtle.pen()["fillcolor"] + 'green' .. function:: isdown() @@ -1051,6 +1056,7 @@ Filling >>> turtle.home() + .. function:: filling() Return ``True`` if the turtle is filling, ``False`` otherwise. @@ -1064,9 +1070,10 @@ Filling ... else: ... turtle.pensize(3) + .. function:: fill() - Fill the shape drawn in the ``with turtle.fill():`` block. + Fill the shape drawn in the :keyword:`with` block. .. doctest:: :skipif: _tkinter is None @@ -1075,8 +1082,8 @@ Filling >>> with turtle.fill(): ... turtle.circle(80) - Using :func:`!fill` is equivalent to adding the :func:`begin_fill` before the - fill-block and :func:`end_fill` after the fill-block: + :func:`fill` is a :term:`context manager`, so using it is the same as + calling :func:`begin_fill` before the block and :func:`end_fill` after it: .. doctest:: :skipif: _tkinter is None @@ -1091,17 +1098,18 @@ Filling .. function:: begin_fill() - To be called just before drawing a shape to be filled. + Call just before drawing a shape to be filled. The shape is filled by the + next call to :func:`end_fill`. .. function:: end_fill() Fill the shape drawn after the last call to :func:`begin_fill`. - Whether or not overlap regions for self-intersecting polygons - or multiple shapes are filled depends on the operating system graphics, - type of overlap, and number of overlaps. For example, the Turtle star - above may be either all yellow or have some white regions. + If the shape crosses itself or several shapes overlap, whether the + overlapping parts are filled depends on the operating system, the type of + overlap and how many there are. For example, the turtle star above may be + either all yellow or have some white regions. .. doctest:: :skipif: _tkinter is None @@ -1144,15 +1152,16 @@ More drawing control .. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal")) - :param arg: object to be written to the TurtleScreen - :param move: True/False - :param align: one of the strings "left", "center" or right" - :param font: a triple (fontname, fontsize, fonttype) + :param arg: an object + :param move: a boolean + :param align: a string + :param font: a tuple of a string, an integer and a string - Write text - the string representation of *arg* - at the current turtle - position according to *align* ("left", "center" or "right") and with the given - font. If *move* is true, the pen is moved to the bottom-right corner of the - text. By default, *move* is ``False``. + Write text at the current turtle position. The text is the string + representation of *arg*. *align* is one of the strings "left", "center" or + "right" and says how the text is placed relative to the turtle position. + *font* is a triple of font name, font size and font type. If *move* is + ``True``, the pen is moved to the bottom-right corner of the text. .. doctest:: :skipif: _tkinter is None @@ -1203,7 +1212,7 @@ Appearance Set the turtle shape to the named shape. If *name* is not given, return the name of the current shape. The shape must exist in the - :class:`TurtleScreen`'s shape dictionary. Initially there are the following + :class:`TurtleScreen`'s shape list. Initially there are the following shapes: "arrow", "turtle", "circle", "square", "triangle" and "classic". To add shapes, see the :class:`Screen` method :func:`register_shape`. @@ -1280,14 +1289,15 @@ Appearance .. function:: shearfactor(shear=None) - :param shear: number (optional) + :param shear: a number or ``None`` - Set or return the current shearfactor. Shear the turtleshape according to - the given shearfactor shear, which is the tangent of the shear angle. - Do *not* change the turtle's heading (direction of movement). - If shear is not given: return the current shearfactor, i. e. the - tangent of the shear angle, by which lines parallel to the - heading of the turtle are sheared. + Return or set the current shear factor. Shearing slants the turtle shape, + like the slant of italic letters. The shear factor says how much it is + slanted: 0 is not slanted at all, and the larger the number, the stronger + the slant. Negative numbers slant the other way. If *shear* is not given, + return the current shear factor. Otherwise slant the turtle shape + accordingly and set resizemode to "user". The turtle's heading does not + change. Precisely, the shear factor is the tangent of the shear angle. .. doctest:: :skipif: _tkinter is None @@ -1303,8 +1313,9 @@ Appearance :param angle: a number - Rotate the turtleshape by *angle* from its current tilt-angle, but do *not* - change the turtle's heading (direction of movement). + Turn the turtle shape by *angle* from its current tilt angle. The turtle's + heading does not change, so the turtle keeps moving in the same direction. + See :func:`tiltangle`. .. doctest:: :skipif: _tkinter is None @@ -1320,15 +1331,15 @@ Appearance .. function:: tiltangle(angle=None) - :param angle: a number (optional) + :param angle: a number or ``None`` - Set or return the current tilt-angle. If angle is given, rotate the - turtleshape to point in the direction specified by angle, - regardless of its current tilt-angle. Do *not* change the turtle's - heading (direction of movement). - If angle is not given: return the current tilt-angle, i. e. the angle - between the orientation of the turtleshape and the heading of the - turtle (its direction of movement). + Return or set the current tilt angle. The tilt angle is the angle between + the direction the turtle shape points in and the direction the turtle moves + in. Normally the shape points in the direction of movement, so the tilt + angle is 0. If *angle* is given, turn the turtle shape to that tilt angle, + no matter what it was before, and set resizemode to "user". The turtle's + heading does not change. If *angle* is not given, return the current tilt + angle. .. doctest:: :skipif: _tkinter is None @@ -1343,21 +1354,18 @@ Appearance .. function:: shapetransform(t11=None, t12=None, t21=None, t22=None) - :param t11: a number (optional) - :param t12: a number (optional) - :param t21: a number (optional) - :param t12: a number (optional) + :param t11: a number or ``None`` + :param t12: a number or ``None`` + :param t21: a number or ``None`` + :param t22: a number or ``None`` - Set or return the current transformation matrix of the turtle shape. - - If none of the matrix elements are given, return the transformation - matrix as a tuple of 4 elements. - Otherwise set the given elements and transform the turtleshape - according to the matrix consisting of first row t11, t12 and - second row t21, t22. The determinant t11 * t22 - t12 * t21 must not be - zero, otherwise an error is raised. - Modify stretchfactor, shearfactor and tiltangle according to the - given matrix. + Return or set the transformation matrix of the turtle shape. The matrix + combines the stretch factors, the shear factor and the tilt angle of the + turtle shape into four numbers: *t11* and *t12* form its first row, *t21* + and *t22* its second. If none of them are given, return the current matrix + as a tuple of four numbers. Otherwise set the given elements and update the + stretch factors, shear factor and tilt angle accordingly. + ``t11 * t22 - t12 * t21`` must not be zero, otherwise an error is raised. .. doctest:: :skipif: _tkinter is None @@ -1372,8 +1380,9 @@ Appearance .. function:: get_shapepoly() - Return the current shape polygon as tuple of coordinate pairs. This - can be used to define a new shape or components of a compound shape. + Return the points that form the outline of the current turtle shape + as a tuple of ``(x, y)`` coordinate pairs. These points can be used + to define a new shape or parts of a :ref:`compound shape `. .. doctest:: :skipif: _tkinter is None @@ -1398,7 +1407,7 @@ Using events coordinates of the clicked point on the canvas. If *fun* is ``None``, existing bindings are removed. *btn* is the number of the mouse button and defaults to 1, the left mouse button. If *add* is ``True``, a new binding - is added, otherwise it replaces a former binding. Example for the + is added, otherwise it replaces a previous binding. Example for the anonymous turtle, that is, the procedural way: .. doctest:: @@ -1413,14 +1422,16 @@ Using events .. function:: onrelease(fun, btn=1, add=None) - :param fun: a function with two arguments which will be called with the - coordinates of the clicked point on the canvas - :param btn: number of the mouse-button, defaults to 1 (left mouse button) - :param add: ``True`` or ``False`` -- if ``True``, a new binding will be - added, otherwise it will replace a former binding + :param fun: a function with two arguments or ``None`` + :param btn: an integer + :param add: a boolean or ``None`` - Bind *fun* to mouse-button-release events on this turtle. If *fun* is - ``None``, existing bindings are removed. + Bind *fun* to mouse-button-release events on this turtle. *fun* is called + with the coordinates of the point on the canvas where the mouse button is + released. If *fun* is ``None``, existing bindings are removed. *btn* is the + number of the mouse button and defaults to 1, the left mouse button. If + *add* is ``True``, a new binding is added, otherwise it replaces a previous + binding. .. doctest:: :skipif: _tkinter is None @@ -1428,6 +1439,7 @@ Using events >>> class MyTurtle(Turtle): ... def glow(self,x,y): ... self.fillcolor("red") + ... ... def unglow(self,x,y): ... self.fillcolor("") ... @@ -1438,25 +1450,25 @@ Using events .. function:: ondrag(fun, btn=1, add=None) - :param fun: a function with two arguments which will be called with the - coordinates of the clicked point on the canvas - :param btn: number of the mouse-button, defaults to 1 (left mouse button) - :param add: ``True`` or ``False`` -- if ``True``, a new binding will be - added, otherwise it will replace a former binding - - Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``, - existing bindings are removed. + :param fun: a function with two arguments or ``None`` + :param btn: an integer + :param add: a boolean or ``None`` - Remark: Every sequence of mouse-move-events on a turtle is preceded by a - mouse-click event on that turtle. + Bind *fun* to mouse-move events on this turtle. *fun* is called with the + coordinates of the mouse pointer on the canvas. If *fun* is ``None``, + existing bindings are removed. *btn* is the number of the mouse button and + defaults to 1, the left mouse button. If *add* is ``True``, a new binding + is added, otherwise it replaces a previous binding. Every sequence of + mouse-move events on a turtle is preceded by a mouse-click event on that + turtle. .. doctest:: :skipif: _tkinter is None >>> turtle.ondrag(turtle.goto) - Subsequently, clicking and dragging the Turtle will move it across - the screen thereby producing handdrawings (if pen is down). + Clicking and dragging the turtle then moves it across the screen, drawing + by hand if the pen is down. Special Turtle methods @@ -1464,8 +1476,8 @@ Special Turtle methods .. function:: poly() - Record the vertices of a polygon drawn in the ``with turtle.poly():`` block. - The first and last vertices will be connected. + Record the vertices of a polygon drawn in the :keyword:`with` block. The + first and last vertices will be connected. .. doctest:: :skipif: _tkinter is None @@ -1475,6 +1487,18 @@ Special Turtle methods ... turtle.right(60) ... turtle.forward(100) + :func:`poly` is a :term:`context manager`, so using it is the same as + calling :func:`begin_poly` before the block and :func:`end_poly` after it: + + .. doctest:: + :skipif: _tkinter is None + + >>> turtle.begin_poly() + >>> turtle.forward(100) + >>> turtle.right(60) + >>> turtle.forward(100) + >>> turtle.end_poly() + .. versionadded:: 3.14 @@ -1574,8 +1598,8 @@ Special Turtle methods .. doctest:: :skipif: _tkinter is None - >>> while undobufferentries(): - ... undo() + >>> while turtle.undobufferentries(): + ... turtle.undo() .. _methods-of-turtlescreen-screen: @@ -1638,19 +1662,19 @@ Window control .. function:: bgpic(picname=None) - :param picname: a string, name of an image file (PNG, GIF, PGM, and PPM) - or ``"nopic"``, or ``None`` + :param picname: a string or ``None`` - Set background image or return name of current backgroundimage. If *picname* - is a filename, set the corresponding image as background. If *picname* is - ``"nopic"``, delete background image, if present. If *picname* is ``None``, - return the filename of the current backgroundimage. :: + Set the background image or return the name of the current one. If + *picname* is the name of an image file (PNG, GIF, PGM, and PPM), set that + image as the background. If *picname* is ``"nopic"``, delete the background + image, if present. If *picname* is ``None``, return the filename of the + current background image. :: >>> screen.bgpic() 'nopic' >>> screen.bgpic("landscape.gif") >>> screen.bgpic() - "landscape.gif" + 'landscape.gif' .. function:: clear() @@ -1685,15 +1709,19 @@ Window control .. function:: screensize(canvwidth=None, canvheight=None, bg=None) - :param canvwidth: positive integer, new width of canvas in pixels - :param canvheight: positive integer, new height of canvas in pixels - :param bg: colorstring or color-tuple, new background color + :param canvwidth: a positive integer or ``None`` + :param canvheight: a positive integer or ``None`` + :param bg: a colorstring or ``None`` - If no arguments are given, return current (canvaswidth, canvasheight). Else - resize the canvas the turtles are drawing on. Do not alter the drawing - window. To observe hidden parts of the canvas, use the scrollbars. With this - method, one can make visible those parts of a drawing which were outside the - canvas before. + Resize the canvas the turtles are drawing on. *canvwidth* and *canvheight* + are the new width and height of the canvas in pixels, and *bg* is the new + background color. If no arguments are given, return the current canvas + width and height. + + The drawing window is not altered. To see hidden parts of the canvas, use + the scrollbars. This can make visible those parts of a drawing which were + outside the canvas before, for example to find a turtle that has wandered + off the canvas. .. doctest:: :skipif: _tkinter is None @@ -1704,8 +1732,6 @@ Window control >>> screen.screensize() (2000, 1500) - e.g. to search for an erroneously escaped turtle ;-) - .. function:: setworldcoordinates(llx, lly, urx, ury) @@ -1805,7 +1831,7 @@ Animation control .. function:: update() - Perform a TurtleScreen update. To be used when tracer is turned off. + Perform a TurtleScreen update. To be used when :func:`tracer` is turned off. See also the RawTurtle/Turtle method :func:`speed`. @@ -1815,19 +1841,24 @@ Using screen events .. function:: listen(xdummy=None, ydummy=None) - Set focus on TurtleScreen (in order to collect key-events). Dummy arguments - are provided in order to be able to pass :func:`listen` to the onclick method. + :param xdummy: ignored + :param ydummy: ignored + + Set focus on the :class:`TurtleScreen` so that it receives key events. The + two dummy arguments are only there so that :func:`listen` can be passed to + :func:`onclick`. .. function:: onkey(fun, key) onkeyrelease(fun, key) :param fun: a function with no arguments or ``None`` - :param key: a string: key (e.g. "a") or key-symbol (e.g. "space") + :param key: a string - Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings - are removed. Remark: in order to be able to register key-events, TurtleScreen - must have the focus. (See method :func:`listen`.) + Bind *fun* to the key-release event of *key*. *key* is a key, such as + "a", or a key symbol, such as "space". If *fun* is ``None``, the event + binding is removed. To be able to receive key events, the + :class:`TurtleScreen` must have focus, see :func:`listen`. .. doctest:: :skipif: _tkinter is None @@ -1839,26 +1870,34 @@ Using screen events >>> screen.onkey(f, "Up") >>> screen.listen() + The turtle can then be moved by repeatedly pressing the up-arrow key, + drawing a hexagon. + .. function:: onkeypress(fun, key=None) :param fun: a function with no arguments or ``None`` - :param key: a string: key (e.g. "a") or key-symbol (e.g. "space") + :param key: a string or ``None`` - Bind *fun* to key-press event of key if key is given, - or to any key-press-event if no key is given. - Remark: in order to be able to register key-events, TurtleScreen - must have focus. (See method :func:`listen`.) + Bind *fun* to the key-press event of *key*. *key* is a key, such as "a", + or a key symbol, such as "space". If *key* is not given, *fun* is bound to + the key-press event of any key. If *fun* is ``None``, the event binding is + removed. To be able to receive key events, the :class:`TurtleScreen` must + have focus, see :func:`listen`. .. doctest:: :skipif: _tkinter is None >>> def f(): ... fd(50) + ... lt(60) ... - >>> screen.onkey(f, "Up") + >>> screen.onkeypress(f, "Up") >>> screen.listen() + The turtle can then be moved by pressing or holding down the up-arrow key, + drawing a hexagon. + .. function:: onclick(fun, btn=1, add=None) onscreenclick(fun, btn=1, add=None) @@ -1871,7 +1910,7 @@ Using screen events coordinates of the clicked point on the canvas. If *fun* is ``None``, existing bindings are removed. *btn* is the number of the mouse button and defaults to 1, the left mouse button. If *add* is ``True``, a new binding - is added, otherwise it replaces a former binding. + is added, otherwise it replaces a previous binding. Example for a TurtleScreen instance named ``screen`` and a Turtle instance named ``turtle``: @@ -1892,7 +1931,7 @@ Using screen events .. function:: ontimer(fun, t=0) :param fun: a function with no arguments - :param t: a number >= 0 + :param t: a nonnegative number Install a timer that calls *fun* after *t* milliseconds. @@ -1905,7 +1944,7 @@ Using screen events ... fd(50) ... lt(60) ... screen.ontimer(f, 250) - >>> f() ### makes the turtle march around + >>> f() # makes the turtle march around >>> running = False @@ -1944,10 +1983,10 @@ Input methods Pop up a dialog window for input of a number. *title* is the title of the dialog window and *prompt* is a text describing what numerical information to input. *default* is the default value, *minval* and *maxval* are the - minimum and maximum values for input. If these are given, the number - entered must be between *minval* and *maxval*. If not, a hint is issued - and the dialog remains open for correction. Return the number entered. If - the dialog is canceled, return ``None``. :: + minimum and maximum values for input. If these are given and the number + entered is not between *minval* and *maxval*, a hint is issued and the + dialog remains open for correction. Return the number entered. If the + dialog is canceled, return ``None``. :: >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000) @@ -1997,7 +2036,7 @@ Settings and special methods >>> turtle.pencolor(240, 160, 80) Traceback (most recent call last): ... - TurtleGraphicsError: bad color sequence: (240, 160, 80) + turtle.TurtleGraphicsError: bad color sequence: (240, 160, 80) >>> screen.colormode() 1.0 >>> screen.colormode(255) @@ -2097,6 +2136,26 @@ Settings and special methods 640 +.. function:: save(filename, *, overwrite=False) + + :param filename: a string + :param overwrite: a boolean + + Save the current drawing and the turtles as a PostScript file. *filename* + is the path of the file to create. It must end with ``".ps"`` or + ``".eps"``. If a file with that name already exists, a + :exc:`FileExistsError` is raised, unless *overwrite* is ``True``. + *overwrite* must be given by name, as in ``overwrite=True``. + + .. doctest:: + :skipif: _tkinter is None + + >>> screen.save("my_drawing.ps") + >>> screen.save("my_drawing.ps", overwrite=True) + + .. versionadded:: 3.14 + + .. _screenspecific: .. _methods-specific-to-screen-not-inherited-from-turtlescreen: @@ -2110,59 +2169,40 @@ Screen-only methods .. function:: exitonclick() - Bind ``bye()`` method to mouse clicks on the Screen. + Bind :func:`bye` to mouse clicks on the screen and start the event loop. + Clicking on the screen then closes the turtle graphics window. If the + configuration value ``using_IDLE`` is ``True``, the event loop is not + started, see :func:`mainloop`. - If the value "using_IDLE" in the configuration dictionary is ``False`` - (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch - (no subprocess) is used, this value should be set to ``True`` in - :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the - client script. - - -.. function:: save(filename, overwrite=False) - - Save the current turtle drawing (and turtles) as a PostScript file. - - :param filename: the path of the saved PostScript file - :param overwrite: if ``False`` and there already exists a file with the given - filename, then the function will raise a - ``FileExistsError``. If it is ``True``, the file will be - overwritten. - - .. doctest:: - :skipif: _tkinter is None - - >>> screen.save("my_drawing.ps") - >>> screen.save("my_drawing.ps", overwrite=True) +.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"]) - .. versionadded:: 3.14 + :param width: an integer or a float + :param height: an integer or a float + :param startx: an integer or ``None`` + :param starty: an integer or ``None`` + Set the size and position of the main window. -.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"]) + *width* and *height* are the size of the window. As integers they are sizes + in pixels, as floats they are fractions of the screen, so 0.5 is half the + screen. The defaults are 50% of the screen width and 75% of its height. - Set the size and position of the main window. Default values of arguments - are stored in the configuration dictionary and can be changed via a - :file:`turtle.cfg` file. + *startx* and *starty* are the position of the window. If positive, they are + the distance in pixels from the left and top edges of the screen, if + negative from the right and bottom edges. If ``None``, the window is + centered in that direction. - :param width: if an integer, a size in pixels, if a float, a fraction of the - screen; default is 50% of screen - :param height: if an integer, the height in pixels, if a float, a fraction of - the screen; default is 75% of screen - :param startx: if positive, starting position in pixels from the left - edge of the screen, if negative from the right edge, if ``None``, - center window horizontally - :param starty: if positive, starting position in pixels from the top - edge of the screen, if negative from the bottom edge, if ``None``, - center window vertically + The default values of all arguments are stored in the configuration + dictionary and can be changed with a :file:`turtle.cfg` file. .. doctest:: :skipif: _tkinter is None - >>> screen.setup (width=200, height=200, startx=0, starty=0) - >>> # sets window to 200x200 pixels, in upper left of screen - >>> screen.setup(width=.75, height=0.5, startx=None, starty=None) - >>> # sets window to 75% of screen by 50% of screen and centers + >>> screen.setup(width=200, height=200, startx=0, starty=0) + >>> # sets window to 200x200 pixels, in upper left of screen + >>> screen.setup(width=0.75, height=0.5) + >>> # sets window to 75% of screen by 50% of screen and centers .. function:: title(titlestring) @@ -2187,8 +2227,8 @@ Public classes :param canvas: a :class:`!tkinter.Canvas`, a :class:`ScrolledCanvas` or a :class:`TurtleScreen` - Create a turtle. The turtle has all methods described above as "methods of - Turtle/RawTurtle". + Create a turtle. The turtle has all the methods described in + :ref:`turtle-methods`. .. class:: Turtle() @@ -2201,8 +2241,8 @@ Public classes :param cv: a :class:`!tkinter.Canvas` - Provides screen oriented methods like :func:`bgcolor` etc. that are described - above. + Provides the screen-oriented methods, such as :func:`bgcolor`, that are + described above. .. class:: Screen() @@ -2215,13 +2255,13 @@ Public classes .. class:: ScrolledCanvas(master) - :param master: some Tkinter widget to contain the ScrolledCanvas, i.e. - a Tkinter-canvas with scrollbars added + :param master: a Tkinter widget - Used by class Screen, which thus automatically provides a ScrolledCanvas as - playground for the turtles. + A Tkinter canvas with scrollbars added. *master* is the widget that contains + it. Used by class :class:`Screen`, which thus automatically provides a + ScrolledCanvas as playground for the turtles. -.. class:: Shape(type_, data) +.. class:: Shape(type_, data=None) :param type\_: a string :param data: depends on *type_* @@ -2353,7 +2393,7 @@ below: >>> poly2 = ((0,0), (10,-5), (-10,-5)) >>> s.addcomponent(poly2, "blue", "red") -3. Now add the Shape to the Screen's shapelist and use it: +3. Now add the Shape to the Screen's shape list and use it: .. doctest:: :skipif: _tkinter is None @@ -2456,38 +2496,52 @@ via docstrings. So these can be used as online-help via the Python help facilities: - When using IDLE, tooltips show the signatures and first lines of the - docstrings of typed in function-/method calls. + docstrings of typed in function/method calls. - Calling :func:`help` on methods or functions displays the docstrings:: - >>> help(Screen.bgcolor) - Help on method bgcolor in module turtle: - - bgcolor(self, *args) unbound turtle.Screen method - Set or return backgroundcolor of the TurtleScreen. - - Arguments (if given): a color string or three numbers - in the range 0..colormode or a 3-tuple of such numbers. - + >>> help(TurtleScreen.bgcolor) + Help on function bgcolor in module turtle: - >>> screen.bgcolor("orange") - >>> screen.bgcolor() - "orange" - >>> screen.bgcolor(0.5,0,0.5) - >>> screen.bgcolor() - "#800080" + bgcolor(self, *args) + Return or set the background color of the TurtleScreen. + + Four input formats are allowed: + - bgcolor() + Return the current background color as a color specification + string or as a tuple (see example). May be used as input + to another color(), pencolor(), fillcolor() or bgcolor() call. + - bgcolor(colorstring) + Set the background color to colorstring, which is a Tk color + specification string, such as "red", "yellow", or "#33cc8c". + - bgcolor((r, g, b)) + Set the background color to the RGB color represented by + the tuple of r, g, and b. Each of r, g, and b must be + between 0 and colormode, where colormode is either 1.0 or 255 + (see colormode()). + - bgcolor(r, g, b) + Set the background color to the RGB color represented by + r, g, and b. Each of r, g, and b must be between 0 and + colormode. + + Example (for a TurtleScreen instance named screen): + >>> screen.bgcolor("orange") + >>> screen.bgcolor() + 'orange' + >>> screen.colormode(255) + >>> screen.bgcolor("#800080") + >>> screen.bgcolor() + (128.0, 0.0, 128.0) >>> help(Turtle.penup) - Help on method penup in module turtle: + Help on function penup in module turtle: - penup(self) unbound turtle.Turtle method - Pull the pen up -- no drawing when moving. + penup(self) + Lift the pen. Aliases: penup | pu | up - No argument - - >>> turtle.penup() + The turtle does not draw when it moves. - The docstrings of the functions which are derived from methods have a modified form:: @@ -2496,32 +2550,44 @@ facilities: Help on function bgcolor in module turtle: bgcolor(*args) - Set or return backgroundcolor of the TurtleScreen. - - Arguments (if given): a color string or three numbers - in the range 0..colormode or a 3-tuple of such numbers. + Return or set the background color of the TurtleScreen. + + Four input formats are allowed: + - bgcolor() + Return the current background color as a color specification + string or as a tuple (see example). May be used as input + to another color(), pencolor(), fillcolor() or bgcolor() call. + - bgcolor(colorstring) + Set the background color to colorstring, which is a Tk color + specification string, such as "red", "yellow", or "#33cc8c". + - bgcolor((r, g, b)) + Set the background color to the RGB color represented by + the tuple of r, g, and b. Each of r, g, and b must be + between 0 and colormode, where colormode is either 1.0 or 255 + (see colormode()). + - bgcolor(r, g, b) + Set the background color to the RGB color represented by + r, g, and b. Each of r, g, and b must be between 0 and + colormode. - Example:: - - >>> bgcolor("orange") - >>> bgcolor() - "orange" - >>> bgcolor(0.5,0,0.5) - >>> bgcolor() - "#800080" + Example: + >>> bgcolor("orange") + >>> bgcolor() + 'orange' + >>> colormode(255) + >>> bgcolor("#800080") + >>> bgcolor() + (128.0, 0.0, 128.0) >>> help(penup) Help on function penup in module turtle: penup() - Pull the pen up -- no drawing when moving. + Lift the pen. Aliases: penup | pu | up - No argument - - Example: - >>> penup() + The turtle does not draw when it moves. These modified docstrings are created automatically together with the function definitions that are derived from the methods at import time. @@ -2575,13 +2641,12 @@ running version are ignored. .. function:: write_docstringdict(filename="turtle_docstringdict") - :param filename: a string, used as filename + :param filename: a string - Create and write docstring-dictionary to a Python script with the given - filename. This function has to be called explicitly (it is not used by the - turtle graphics classes). The docstring dictionary will be written to the - Python script :file:`{filename}.py`. It is intended to serve as a template - for translation of the docstrings into different languages. + Write the docstring dictionary to a Python script. The dictionary is written + to the file :file:`{filename}.py`. It is intended to serve as a template for + translating the docstrings into different languages. This function has to + be called explicitly. It is not used by the turtle graphics classes. .. _turtle-configuration: @@ -2593,11 +2658,11 @@ The built-in default configuration mimics the appearance and behaviour of the old turtle module in order to retain best possible compatibility with it. If you want to use a different configuration which better reflects the features -of this module or which better fits to your needs, e.g. for use in a classroom, -you can prepare a configuration file ``turtle.cfg`` which will be read at import -time and modify the configuration according to its settings. +of this module or which better fits to your needs, for example for use in a +classroom, you can prepare a configuration file ``turtle.cfg`` which will be +read at import time and modify the configuration according to its settings. -The built in configuration would correspond to the following ``turtle.cfg``: +The built-in configuration would correspond to the following ``turtle.cfg``: .. code-block:: ini @@ -2628,13 +2693,13 @@ Short explanation of selected entries: method. - Line 5 and 6 correspond to the arguments of the method :func:`Screen.screensize `. -- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more - info try ``help(shape)``. -- If you want to use no fill color (i.e. make the turtle transparent), you have - to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in - the cfg file). -- If you want to reflect the turtle its state, you have to use ``resizemode = - auto``. +- *shape* can be any of the built-in shapes, such as arrow or turtle. For more + information, see :func:`shape`. +- If you want to use no fill color (that is, make the turtle transparent), you + have to write ``fillcolor = ""`` (but all nonempty strings must not have + quotes in the cfg file). +- If you want the turtle's appearance to reflect its state, you have to set + ``resizemode = auto``. - The *language* entry selects the language of the docstrings, unless the :envvar:`PYTHON_TURTLE_LANG` environment variable is set. See :ref:`turtle-docstring-translation` for more information. @@ -2643,8 +2708,8 @@ Short explanation of selected entries: method-docstrings to function-docstrings will delete these names from the docstrings. - *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its ``-n`` - switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the - mainloop. + switch ("no subprocess"). This will prevent :func:`exitonclick` from + entering the mainloop. There can be a :file:`turtle.cfg` file in the directory where :mod:`!turtle` is stored and an additional one in the current working directory. The latter will @@ -2660,7 +2725,7 @@ not from within the demo-viewer). Explanation =========== -A turtle object draws on a screen object, and there a number of key classes in +A turtle object draws on a screen object, and there are a number of key classes in the turtle object-oriented interface that can be used to create them and relate them to each other. @@ -2743,7 +2808,7 @@ The demo scripts are: | ``fractalcurves`` | Hilbert & Koch curves | recursion | +------------------------+------------------------------+--------------------------------------+ | ``lindenmayer`` | ethnomathematics | L-System | -| | (indian kolams) | | +| | (Indian kolams) | | +------------------------+------------------------------+--------------------------------------+ | ``minimal_hanoi`` | Towers of Hanoi | Rectangular Turtles | | | | as Hanoi discs | @@ -2765,10 +2830,10 @@ The demo scripts are: | ``planet_and_moon`` | simulation of | compound shapes, | | | gravitational system | :class:`Vec2D` | +------------------------+------------------------------+--------------------------------------+ -| ``rosette`` | a pattern from the wikipedia | :func:`clone`, | +| ``rosette`` | a pattern from the Wikipedia | :func:`clone`, | | | article on turtle graphics | :func:`undo` | +------------------------+------------------------------+--------------------------------------+ -| ``round_dance`` | dancing turtles rotating | compound shapes, :func:`clone` | +| ``round_dance`` | dancing turtles rotating | compound shapes, :func:`clone`, | | | pairwise in opposite | :func:`shapesize`, :func:`tilt`, | | | direction | :func:`get_shapepoly`, :func:`update`| +------------------------+------------------------------+--------------------------------------+ diff --git a/Doc/library/types.rst b/Doc/library/types.rst index 9e928e6dd3637b..7a6e7aa3fae6a5 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -515,14 +515,14 @@ Additional Utility Classes and Functions .. versionchanged:: 3.13 Added support for an optional positional argument. -.. function:: DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None) +.. class:: DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None) - Route attribute access on a class to __getattr__. + Route attribute access on a class to :meth:`~object.__getattr__`. This is a descriptor, used to define attributes that act differently when accessed through an instance and through a class. Instance access remains normal, but access to an attribute through a class will be routed to the - class's __getattr__ method; this is done by raising AttributeError. + class's ``__getattr__`` method; this is done by raising :exc:`AttributeError`. This allows one to have properties active on an instance, and have virtual attributes on the class with the same name (see :class:`enum.Enum` for an example). diff --git a/Doc/library/unittest.mock-examples.rst b/Doc/library/unittest.mock-examples.rst index da57f05edcf78f..7b736b5aecb641 100644 --- a/Doc/library/unittest.mock-examples.rst +++ b/Doc/library/unittest.mock-examples.rst @@ -642,7 +642,7 @@ Mocking a generator method ~~~~~~~~~~~~~~~~~~~~~~~~~~ A Python generator is a function or method that uses the :keyword:`yield` statement -to return a series of values when iterated over [#]_. +to return a series of values when iterated over. A generator method / function is called to return the generator object. It is the generator object that is then iterated over. The protocol method for @@ -671,12 +671,6 @@ To configure the values returned from the iteration (implicit in the call to >>> list(mock_foo.iter()) [1, 2, 3] -.. [#] There are also generator expressions and more `advanced uses - `_ of generators, but we aren't - concerned about them here. A very good introduction to generators and how - powerful they are is: `Generator Tricks for Systems Programmers - `_. - Applying the same patch to every test method ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/Doc/library/urllib.error.rst b/Doc/library/urllib.error.rst index 0079699e06ca27..0ff20efcd3a2b7 100644 --- a/Doc/library/urllib.error.rst +++ b/Doc/library/urllib.error.rst @@ -11,8 +11,6 @@ The :mod:`!urllib.error` module defines the exception classes for exceptions raised by :mod:`urllib.request`. The base exception class is :exc:`URLError`. -The following exceptions are raised by :mod:`!urllib.error` as appropriate: - .. exception:: URLError The handlers raise this exception (or derived exceptions) when they run into diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst index 26e159306c8107..9a8f8cf9bab012 100644 --- a/Doc/library/urllib.parse.rst +++ b/Doc/library/urllib.parse.rst @@ -225,6 +225,16 @@ or on combining URL components into a URL string. .. versionchanged:: 3.15 Added the *missing_as_none* parameter. + .. versionchanged:: 3.16 + Passing an object that is not a string, bytes, or ``None`` as + *urlstring* or *scheme* now raises :exc:`TypeError` instead of + :exc:`AttributeError`. + + .. deprecated:: 3.16 + Accepting objects with false values (like ``0`` and ``[]``) except empty + strings, byte-like objects and ``None`` as *urlstring* or *scheme* is + now deprecated. They will raise :exc:`TypeError` in Python 3.18. + .. _WHATWG spec: https://url.spec.whatwg.org/#concept-basic-url-parser @@ -341,6 +351,15 @@ or on combining URL components into a URL string. .. versionchanged:: 3.15 Added the *keep_empty* parameter. + .. versionchanged:: 3.16 + Passing an object that is not a string, bytes, or ``None`` in *parts* + now raises :exc:`TypeError` instead of :exc:`AttributeError`. + + .. deprecated:: 3.16 + Accepting objects with false values (like ``0`` and ``[]``) except empty + strings, byte-like objects and ``None`` in *parts* is now deprecated. + They will raise :exc:`TypeError` in Python 3.18. + .. function:: urlparse(urlstring, scheme=None, allow_fragments=True, *, missing_as_none=False) @@ -358,6 +377,16 @@ or on combining URL components into a URL string. :func:`urlsplit` should generally be used instead of :func:`urlparse`. A separate function is needed to separate the path segments and parameters. + .. versionchanged:: 3.16 + Passing an object that is not a string, bytes, or ``None`` as + *urlstring* or *scheme* now raises :exc:`TypeError` instead of + :exc:`AttributeError`. + + .. deprecated:: 3.16 + Accepting objects with false values (like ``0`` and ``[]``) except empty + strings, byte-like objects and ``None`` as *urlstring* or *scheme* is + now deprecated. They will raise :exc:`TypeError` in Python 3.18. + .. function:: urlunparse(parts) urlunparse(parts, *, keep_empty) @@ -379,6 +408,15 @@ or on combining URL components into a URL string. .. versionchanged:: 3.15 Added the *keep_empty* parameter. + .. versionchanged:: 3.16 + Passing an object that is not a string, bytes, or ``None`` in *parts* + now raises :exc:`TypeError` instead of :exc:`AttributeError`. + + .. deprecated:: 3.16 + Accepting objects with false values (like ``0`` and ``[]``) except empty + strings, byte-like objects and ``None`` in *parts* is now deprecated. + They will raise :exc:`TypeError` in Python 3.18. + .. function:: urljoin(base, url, allow_fragments=True) @@ -422,6 +460,15 @@ or on combining URL components into a URL string. Behavior updated to match the semantics defined in :rfc:`3986`. + .. versionchanged:: 3.16 + Passing an object that is not a string, bytes, or ``None`` as *base* or + *url* now raises :exc:`TypeError` instead of :exc:`AttributeError`. + + .. deprecated:: 3.16 + Accepting objects with false values (like ``0`` and ``[]``) except empty + strings, byte-like objects and ``None`` as *base* or *url* is now + deprecated. They will raise :exc:`TypeError` in Python 3.18. + .. function:: urldefrag(url, *, missing_as_none=False) @@ -452,6 +499,15 @@ or on combining URL components into a URL string. .. versionchanged:: 3.15 Added the *missing_as_none* parameter. + .. versionchanged:: 3.16 + Passing an object that is not a string, bytes, or ``None`` as *url* now + raises :exc:`TypeError` instead of :exc:`AttributeError`. + + .. deprecated:: 3.16 + Accepting objects with false values (like ``0`` and ``[]``) except empty + strings, byte-like objects and ``None`` as *url* is now deprecated. + They will raise :exc:`TypeError` in Python 3.18. + .. function:: unwrap(url) Extract the url from a wrapped URL (that is, a string formatted as diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 9a75f4986e1a3a..d032ab16250f26 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -378,6 +378,11 @@ The following classes are provided: ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set; see the documentation on :func:`~urllib.request.getproxies`. + Because *proxies* holds at most one proxy for each protocol, it cannot + use a proxy auto-config (PAC) file, which selects a proxy for each + URL. See :ref:`this example ` for one way to use a + PAC file. + .. class:: HTTPPasswordMgr() @@ -417,7 +422,7 @@ The following classes are provided: received the request is re-sent with the authentication credentials. If authentication succeeds, ``update_authenticated`` is called to set ``is_authenticated`` ``True`` for the URI, so that subsequent requests to - the URI or any of its super-URIs will automatically include the + the URI itself or any path below it will automatically include the authentication credentials. .. versionadded:: 3.5 @@ -440,6 +445,10 @@ The following classes are provided: :ref:`http-password-mgr` for information on the interface that must be supported. + .. versionchanged:: next + Also handles a proxy that requires authentication for the CONNECT + request used to tunnel ``https`` URLs. + .. class:: AbstractDigestAuthHandler(password_mgr=None) @@ -477,6 +486,10 @@ The following classes are provided: :ref:`http-password-mgr` for information on the interface that must be supported. + .. versionchanged:: next + Also handles a proxy that requires authentication for the CONNECT + request used to tunnel ``https`` URLs. + .. class:: HTTPHandler() @@ -842,7 +855,7 @@ The following attribute and methods should only be used by classes derived from 1. a :class:`Request` object, #. a file-like object with the HTTP error body, - #. the three-digit code of the error, as a string, + #. the three-digit code of the error, as an integer, #. the user-visible explanation of the code, as a string, and #. the headers of the error, as a mapping object. @@ -996,11 +1009,10 @@ These methods are available on :class:`HTTPPasswordMgr` and .. method:: HTTPPasswordMgr.add_password(realm, uri, user, passwd) *uri* can be either a single URI, or a sequence of URIs. *realm*, *user* and - *passwd* must be strings. This causes ``(user, passwd)`` to be used as - authentication tokens when authentication for *realm* and a super-URI of any - of the given URIs is given. If a URI includes a scheme, its credentials only - match authentication URIs with the same scheme or no scheme. A URI without a - scheme matches authentication URIs with any scheme. + *passwd* must be strings. The credentials for *realm* apply to any registered + URI and to paths below it. If a URI includes a scheme, + its credentials only match authentication URIs with the same scheme or no + scheme. A URI without a scheme matches authentication URIs with any scheme. .. versionchanged:: 3.16 Authentication credentials for URIs with a scheme are now scoped by @@ -1097,6 +1109,14 @@ ProxyBasicAuthHandler Objects Retry the request with authentication information, if available. + For an ``https`` URL, the request goes through a tunnel that the proxy + sets up in reply to a CONNECT request. A 407 response can then come + either from the proxy, rejecting the CONNECT, or from the origin server, + through the tunnel. Both are handled the same way: the request is retried + with the credentials for the proxy, and those credentials are sent only in + the CONNECT request, never to the origin server. If the origin server + keeps answering 407, :exc:`~urllib.error.HTTPError` is raised. + .. _abstract-digest-auth-handler: @@ -1136,6 +1156,14 @@ ProxyDigestAuthHandler Objects Retry the request with authentication information, if available. + For an ``https`` URL, the request goes through a tunnel that the proxy + sets up in reply to a CONNECT request. A 407 response can then come + either from the proxy, rejecting the CONNECT, or from the origin server, + through the tunnel. Both are handled the same way: the request is retried + with the credentials for the proxy, and those credentials are sent only in + the CONNECT request, never to the origin server. If the origin server + keeps answering 407, :exc:`~urllib.error.HTTPError` is raised. + .. _http-handler-objects: @@ -1200,8 +1228,9 @@ FTPHandler Objects .. method:: FTPHandler.ftp_open(req) - Open the FTP file indicated by *req*. The login is always done with empty - username and password. + Open the FTP file indicated by *req*. The username and password are taken + from the URL, with missing values defaulting to empty strings. See + :meth:`ftplib.FTP.login` for the login defaults. .. _cacheftp-handler-objects: @@ -1243,9 +1272,9 @@ HTTPErrorProcessor Objects Process HTTP error responses. - For 200 error codes, the response object is returned immediately. + For 2xx status codes, the response object is returned immediately. - For non-200 error codes, this simply passes the job on to the + For other status codes, this simply passes the job on to the :meth:`!http_error_\` handler methods, via :meth:`OpenerDirector.error`. Eventually, :class:`HTTPDefaultErrorHandler` will raise an :exc:`~urllib.error.HTTPError` if no other handler handles the error. @@ -1279,11 +1308,11 @@ it:: ... data = gzip.decompress(data) ... print(data[:300].decode('utf-8', errors='replace')) -Note that urlopen returns a bytes object. This is because there is no way -for urlopen to automatically determine the encoding of the byte stream -it receives from the HTTP server. In general, a program will decode -the returned bytes object to string once it determines or guesses -the appropriate encoding. +Note that calling ``read()`` on the response returned by :func:`urlopen` returns +a :class:`bytes` object. This is because there is no way for :func:`urlopen` to +automatically determine the encoding of the byte stream it receives from the +HTTP server. In general, a program will decode the returned bytes object to a +string once it determines or guesses the appropriate encoding. The following HTML spec document, https://html.spec.whatwg.org/#charset, lists the various ways in which an HTML or an XML document could have specified its @@ -1382,6 +1411,51 @@ programmatically supplied proxy URLs, and adds proxy authorization support with with opener.open('http://www.example.com/login.html') as f: print(f.read().decode('utf-8')) +.. _urllib-request-pac: + +Proxy auto-config (PAC) files select a proxy for each URL, so they cannot be +described by the *proxies* mapping that :class:`ProxyHandler` expects. The +:pypi:`pypac` package finds and evaluates PAC files; its ``ProxyResolver`` +can be used to write a handler that consults the PAC file once per request:: + + import urllib.error + import urllib.request + from pypac import get_pac + from pypac.resolver import ProxyResolver + + class PACProxyHandler(urllib.request.ProxyHandler): + + def __init__(self, resolver): + self.resolver = resolver + # An empty mapping suppresses the _open methods that + # ProxyHandler would make from a static proxies mapping. + super().__init__({}) + + def _pac_open(self, req, protocol): + proxy = self.resolver.get_proxy(req.full_url) + if proxy is None: + raise urllib.error.URLError('no proxy available') + if proxy == 'DIRECT': + return None # continue with the remaining handlers + if proxy.startswith('socks'): + raise urllib.error.URLError('SOCKS proxies are not supported') + return self.proxy_open(req, proxy, protocol) + + def http_open(self, req): + return self._pac_open(req, 'http') + + def https_open(self, req): + return self._pac_open(req, 'https') + + opener = urllib.request.build_opener(PACProxyHandler(ProxyResolver(get_pac()))) + with opener.open('http://www.example.com/') as f: + print(f.read().decode('utf-8')) + +Note that a PAC file may also select a SOCKS proxy, which :mod:`urllib.request` +cannot use, and that the :envvar:`no_proxy` environment variable and the +operating system's proxy bypass list still apply to requests the PAC file +assigned to a proxy. + Adding HTTP headers: Use the *headers* argument to the :class:`Request` constructor, or:: @@ -1437,7 +1511,7 @@ The following example uses an explicitly specified HTTP proxy, overriding environment settings:: >>> import urllib.request - >>> proxies = {'http': 'http://proxy.example.com:8080/'} + >>> proxies = {'https': 'http://proxy.example.com:8080/'} >>> opener = urllib.request.build_opener(urllib.request.ProxyHandler(proxies)) >>> with opener.open("https://www.python.org") as f: ... f.read().decode('utf-8') @@ -1586,7 +1660,8 @@ The typical response object is a :class:`urllib.response.addinfourl` instance: .. attribute:: headers - Returns the headers of the response in the form of an :class:`~email.message.EmailMessage` instance. + Returns the headers of the response in the form of an + :class:`~email.message.Message` instance. .. attribute:: status diff --git a/Doc/library/uuid.rst b/Doc/library/uuid.rst index 2d266c2d433c60..6e21b6f99f877d 100644 --- a/Doc/library/uuid.rst +++ b/Doc/library/uuid.rst @@ -69,7 +69,7 @@ which relays any information about the UUID's safety, using this enumeration: variant and version number set according to :rfc:`9562`, overriding bits in the given *hex*, *bytes*, *bytes_le*, *fields*, or *int*. - .. versionchanged:: next + .. versionchanged:: 3.16 Passing an object other than :class:`bytes` as *bytes* or *bytes_le* now consistently raises :exc:`TypeError`. Previously, some invalid inputs raised :exc:`AssertionError` or :exc:`ValueError`. diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index 1e8acb0cb51c91..028e6c5817e50b 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -188,6 +188,8 @@ class, to turn a warning into an error we simply raise ``category(message)``. If a warning is reported and doesn't match any registered filter then the "default" action is applied (hence its name). +.. versionchanged:: 3.15 + Added regular expression support for *message* and *module*. .. _repeated-warning-suppression-criteria: diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 56c06ae609a215..e51a9bdcb870d5 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -149,7 +149,7 @@ See :ref:`__slots__ documentation ` for details. .. versionchanged:: 3.16 Raise :exc:`!TypeError` if *callback* is not callable or ``None``. - .. versionchanged:: next + .. versionchanged:: 3.16 Weak references can now be pickled. @@ -224,7 +224,7 @@ See :ref:`__slots__ documentation ` for details. .. versionchanged:: 3.9 Added support for ``|`` and ``|=`` operators, as specified in :pep:`584`. - .. versionchanged:: next + .. versionchanged:: 3.16 :class:`!WeakKeyDictionary` can now be pickled. :class:`WeakKeyDictionary` objects have an additional method that @@ -252,7 +252,7 @@ than needed. .. versionchanged:: 3.9 Added support for ``|`` and ``|=`` operators, as specified in :pep:`584`. - .. versionchanged:: next + .. versionchanged:: 3.16 :class:`!WeakValueDictionary` can now be pickled. :class:`WeakValueDictionary` objects have the following additional methods. @@ -279,7 +279,7 @@ These have the same issues as the :meth:`WeakKeyDictionary.keyrefs` method. A copy or a deep copy shares the elements with the original; user-defined attributes are copied or deep-copied. - .. versionchanged:: next + .. versionchanged:: 3.16 :class:`!WeakSet` can now be pickled and copied. @@ -318,7 +318,7 @@ These have the same issues as the :meth:`WeakKeyDictionary.keyrefs` method. .. versionadded:: 3.4 - .. versionchanged:: next + .. versionchanged:: 3.16 :class:`!WeakMethod` can now be pickled and copied. .. class:: finalize(obj, func, /, *args, **kwargs) diff --git a/Doc/library/xml.dom.minidom.rst b/Doc/library/xml.dom.minidom.rst index 34f3412d447212..ad33627b61f412 100644 --- a/Doc/library/xml.dom.minidom.rst +++ b/Doc/library/xml.dom.minidom.rst @@ -147,6 +147,15 @@ module documentation. This section lists the differences between the API and otherwise it is set to ``"no"``. Not stating the argument will omit the declaration from the document. + :exc:`ValueError` is raised if the content of the node cannot be + represented in XML, for example if a comment contains ``"--"`` or + a CDATA section contains ``"]]>"``. + The characters are not checked: + the text, the attribute values and the content of comments, + processing instructions and CDATA sections are written as is. + If they can contain characters which are not allowed in XML, + check them with :func:`xml.is_valid_text` before serialization. + .. versionchanged:: 3.8 The :meth:`writexml` method now preserves the attribute order specified by the user. diff --git a/Doc/library/xml.dom.rst b/Doc/library/xml.dom.rst index e5b5147720802e..05166214c7fc31 100644 --- a/Doc/library/xml.dom.rst +++ b/Doc/library/xml.dom.rst @@ -515,7 +515,7 @@ NodeList Objects A :class:`NodeList` represents a sequence of nodes. These objects are used in two ways in the DOM Core recommendation: an :class:`Element` object provides one as its list of child nodes, and the :meth:`~Element.getElementsByTagName` -and :meth:`~Element.getElementsByTagNameNS` methods of :class:`Node` return +and :meth:`~Element.getElementsByTagNameNS` methods of :class:`Element` return objects with this interface to represent query results. :class:`NodeList` does *not* inherit from :class:`Node`. @@ -759,7 +759,7 @@ inherits properties from :class:`Node`. If several elements have the same ID, the first one in document order is returned. - .. versionchanged:: next + .. versionchanged:: 3.16 Previously, which of the elements with the same ID was returned was unspecified and could change between calls. diff --git a/Doc/tools/extensions/pyspecific.py b/Doc/tools/extensions/pyspecific.py index 9b335f2976a2fa..ac956d66efd423 100644 --- a/Doc/tools/extensions/pyspecific.py +++ b/Doc/tools/extensions/pyspecific.py @@ -11,6 +11,7 @@ import re +from docutils import nodes from sphinx import addnodes from sphinx.domains.python import PyFunction, PyMethod @@ -78,6 +79,13 @@ def parse_pdb_command(env, sig, signode): return fullname +def parse_make_target(env, sig, signode): + """Transform a make target signature into RST nodes.""" + signode += addnodes.desc_name(sig, sig) + signode['ids'].append(nodes.make_id(sig)) + return sig + + def parse_monitoring_event(env, sig, signode): """Transform a monitoring event signature into RST nodes.""" signode += addnodes.desc_addname('sys.monitoring.events.', 'sys.monitoring.events.') @@ -113,6 +121,7 @@ def setup(app): app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature) app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command) app.add_object_type('monitoring-event', 'monitoring-event', '%s (monitoring event)', parse_monitoring_event) + app.add_object_type('make-target', 'make-target', '%s (make target)', parse_make_target) app.add_directive_to_domain('py', 'awaitablefunction', PyAwaitableFunction) app.add_directive_to_domain('py', 'awaitablemethod', PyAwaitableMethod) app.connect('env-check-consistency', patch_pairindextypes) diff --git a/Doc/tools/removed-ids.txt b/Doc/tools/removed-ids.txt index cc5ec51b541ab3..386018a026a16d 100644 --- a/Doc/tools/removed-ids.txt +++ b/Doc/tools/removed-ids.txt @@ -29,10 +29,12 @@ c-api/interp-lifecycle.html: global-configuration-variables c-api/threads.html: c.PyEval_InitThreads # Removed sections +faq/library.html: none-of-my-threads-seem-to-run-why library/asyncio-task.html: terminating-a-task-group library/dis.html: opcode-DELETE_ATTR library/dis.html: opcode-DELETE_GLOBAL library/dis.html: opcode-DELETE_NAME +library/dis.html: opcode-GET_YIELD_FROM_ITER library/asyncio-llapi-index.html: event-loop-policies deprecations/index.html: pending-removal-in-python-3-15 @@ -124,3 +126,8 @@ library/concurrent.interpreters.html: concurrent.interpreters.QueueFullError # Removed footnote c-api/exceptions.html: win + +# Classic Mac OS attributes that 'os.stat_result' does not have +library/os.html: os.stat_result.st_creator +library/os.html: os.stat_result.st_rsize +library/os.html: os.stat_result.st_type diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index e0ad7945a8ecef..ffb2587840be5a 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -322,10 +322,12 @@ Miscellaneous options Run Python in isolated mode. This also implies :option:`-E`, :option:`-P` and :option:`-s` options. - In isolated mode :data:`sys.path` contains neither the script's directory nor - the user's site-packages directory. All ``PYTHON*`` environment - variables are ignored, too. Further restrictions may be imposed to prevent - the user from injecting malicious code. + In isolated mode, Python does not prepend the potentially unsafe path + :data:`sys._path0` to :data:`sys.path`, and ignores all ``PYTHON*`` + environment variables, e.g. :envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, + that might be set. + Further restrictions may be imposed to prevent the user from injecting + malicious code. .. versionadded:: 3.4 @@ -353,14 +355,8 @@ Miscellaneous options .. option:: -P - Don't prepend a potentially unsafe path to :data:`sys.path`: - - * ``python -m module`` command line: Don't prepend the current working - directory. - * ``python script.py`` command line: Don't prepend the script's directory. - If it's a symbolic link, resolve symbolic links. - * ``python -c code`` and ``python`` (REPL) command lines: Don't prepend an - empty string, which means the current working directory. + Don't prepend the potentially unsafe path :data:`sys._path0` to + :data:`sys.path`. See also the :envvar:`PYTHONSAFEPATH` environment variable, and :option:`-E` and :option:`-I` (isolated) options. @@ -820,7 +816,9 @@ conflict. .. envvar:: PYTHONSAFEPATH If this is set to a non-empty string, don't prepend a potentially unsafe - path to :data:`sys.path`: see the :option:`-P` option for details. + path :data:`sys._path0` to :data:`sys.path`. + + See also the :option:`-P` option. .. versionadded:: 3.11 diff --git a/Doc/using/configure.rst b/Doc/using/configure.rst index 602cf9fb6f082d..4f2e4dfc88d386 100644 --- a/Doc/using/configure.rst +++ b/Doc/using/configure.rst @@ -7,7 +7,7 @@ Configure Python .. _build-requirements: -Build Requirements +Build requirements ================== To build CPython, you will need: @@ -171,6 +171,8 @@ platform-specific modules like :mod:`winreg` are not listed here. SQLite 3.15.2 is now required for :mod:`sqlite3`. +.. _generated-files: + Generated files =============== @@ -181,9 +183,11 @@ files. Commands to regenerate all generated files:: make regen-stdlib-module-names make regen-limited-abi make regen-configure + make regen-sbom + make regen-unicodedata -The ``Makefile.pre.in`` file documents generated files, their inputs, and tools used -to regenerate them. Search for ``regen-*`` make targets. +The :source:`Makefile.pre.in` file documents generated files, their inputs, and +tools used to regenerate them. Search for ``regen-*`` make targets. configure script ---------------- @@ -214,7 +218,7 @@ and `pkg-config `_ 1.8.1. .. _configure-options: -Configure Options +Configure options ================= List all :file:`configure` script options using:: @@ -223,7 +227,7 @@ List all :file:`configure` script options using:: See also the :file:`Misc/SpecialBuilds.txt` in the Python source distribution. -General Options +General options --------------- .. option:: --enable-loadable-sqlite-extensions @@ -628,7 +632,7 @@ Options for third-party dependencies overriding ``pkg-config``. -WebAssembly Options +WebAssembly options ------------------- .. option:: --enable-wasm-dynamic-linking @@ -647,7 +651,7 @@ WebAssembly Options .. versionadded:: 3.11 -Install Options +Install options --------------- .. option:: --prefix=PREFIX @@ -735,7 +739,14 @@ also be used to improve performance. Enable Link Time Optimization (LTO) in any build (disabled by default). The C compiler Clang requires ``llvm-ar`` for LTO (``ar`` on macOS), as well - as an LTO-aware linker (``ld.gold`` or ``lld``). + as an LTO-aware linker (``ld.gold`` or ``lld``). GNU linkers require the + ``LLVMgold.so`` plugin for Clang LTO; ``lld`` does not. On platforms other + than macOS, configure tries ``-fuse-ld=lld`` if the configured linker cannot + link a Clang LTO program. + + .. versionchanged:: next + Fall back to ``lld`` when the configured linker cannot link Clang LTO + programs. .. versionadded:: 3.6 @@ -934,7 +945,7 @@ also be used to improve performance. .. _debug-build: -Python Debug Build +Python debug build ------------------ A debug build is Python built with the :option:`--with-pydebug` configure @@ -1248,7 +1259,7 @@ Libraries options .. versionadded:: 3.10 -Security Options +Security options ---------------- .. option:: --with-hash-algorithm=[fnv|siphash13|siphash24] @@ -1326,7 +1337,7 @@ Security Options .. versionadded:: 3.14 -macOS Options +macOS options ------------- See :source:`Mac/README.rst`. @@ -1381,7 +1392,7 @@ See :source:`Mac/README.rst`. .. versionadded:: 3.13 -iOS Options +iOS options ----------- See :source:`Platforms/Apple/iOS/README.md`. @@ -1406,7 +1417,7 @@ rejects :option:`--enable-shared`. See iOS builds may be configured without a framework. -Cross Compiling Options +Cross compiling options ----------------------- Cross compiling, also known as cross building, can be used to build Python @@ -1456,7 +1467,7 @@ Cross compiling example:: --with-build-python=../x86_64/python -Python Build System +Python build system =================== Main files of the build system @@ -1480,107 +1491,88 @@ Main build steps Main Makefile targets --------------------- -make -^^^^ - -For the most part, when rebuilding after editing some code or -refreshing your checkout from upstream, all you need to do is execute -``make``, which (per Make's semantics) builds the default target, the -first one defined in the Makefile. By tradition (including in the -CPython project) this is usually the ``all`` target. The -``configure`` script expands an ``autoconf`` variable, -``@DEF_MAKE_ALL_RULE@`` to describe precisely which targets ``make -all`` will build. The three choices are: - -* ``profile-opt`` (configured with ``--enable-optimizations``) -* ``build_wasm`` (chosen if the host platform matches ``wasm32-wasi*`` or - ``wasm32-emscripten``) -* ``build_all`` (configured without explicitly using either of the others) - -Depending on the most recent source file changes, Make will rebuild -any targets (object files and executables) deemed out-of-date, -including running ``configure`` again if necessary. Source/target -dependencies are many and maintained manually however, so Make -sometimes doesn't have all the information necessary to correctly -detect all targets which need to be rebuilt. Depending on which -targets aren't rebuilt, you might experience a number of problems. If -you have build or test problems which you can't otherwise explain, -``make clean && make`` should work around most dependency problems, at -the expense of longer build times. - - -make platform -^^^^^^^^^^^^^ +.. make-target:: make -Build the ``python`` program, but don't build the standard library -extension modules. This generates a file named ``platform`` which -contains a single line describing the details of the build platform, -e.g., ``macosx-14.3-arm64-3.12`` or ``linux-x86_64-3.13``. + For the most part, when rebuilding after editing some code or + refreshing your checkout from upstream, all you need to do is execute + ``make``, which (per Make's semantics) builds the default target, the + first one defined in the Makefile. By tradition (including in the + CPython project) this is usually the ``all`` target. The + ``configure`` script expands an ``autoconf`` variable, + ``@DEF_MAKE_ALL_RULE@`` to describe precisely which targets ``make + all`` will build. The choices are: + * ``profile-opt`` (configured with ``--enable-optimizations``) + * ``bolt-opt`` (configured with ``--enable-bolt``) + * ``build_emscripten`` (chosen if the host platform matches + ``wasm32-emscripten``) + * ``build_wasm`` (chosen if the host platform matches ``wasm32-wasi*``) + * ``build_all`` (configured without explicitly using any of the others) -make profile-opt -^^^^^^^^^^^^^^^^ + Depending on the most recent source file changes, Make will rebuild + any targets (object files and executables) deemed out-of-date, + including running ``configure`` again if necessary. Source/target + dependencies are many and maintained manually however, so Make + sometimes doesn't have all the information necessary to correctly + detect all targets which need to be rebuilt. Depending on which + targets aren't rebuilt, you might experience a number of problems. If + you have build or test problems which you can't otherwise explain, + ``make clean && make`` should work around most dependency problems, at + the expense of longer build times. -Build Python using profile-guided optimization (PGO). You can use the -configure :option:`--enable-optimizations` option to make this the -default target of the ``make`` command (``make all`` or just -``make``). +.. make-target:: make platform + Build the ``python`` program, but don't build the standard library + extension modules. This generates a file named ``platform`` which + contains a single line describing the details of the build platform, + for example, ``macosx-14.3-arm64-3.12`` or ``linux-x86_64-3.13``. +.. make-target:: make profile-opt -make clean -^^^^^^^^^^ + Build Python using profile-guided optimization (PGO). You can use the + configure :option:`--enable-optimizations` option to make this the + default target of the ``make`` command (``make all`` or just + ``make``). -Remove built files. +.. make-target:: make clean + Remove built files. -make distclean -^^^^^^^^^^^^^^ +.. make-target:: make distclean -In addition to the work done by ``make clean``, remove files -created by the configure script. ``configure`` will have to be run -before building again. [#]_ + In addition to the work done by ``make clean``, remove files + created by the configure script. ``configure`` will have to be run + before building again. [#]_ +.. make-target:: make install -make install -^^^^^^^^^^^^ + Build the ``all`` target and install Python. -Build the ``all`` target and install Python. +.. make-target:: make test + Build the ``all`` target and run the Python test suite with the + ``--fast-ci`` option without GUI tests. Variables: -make test -^^^^^^^^^ + * ``TESTOPTS``: additional regrtest command-line options. + * ``TESTPYTHONOPTS``: additional Python command-line options. + * ``TESTTIMEOUT``: timeout in seconds (default: 10 minutes). -Build the ``all`` target and run the Python test suite with the -``--fast-ci`` option without GUI tests. Variables: +.. make-target:: make ci -* ``TESTOPTS``: additional regrtest command-line options. -* ``TESTPYTHONOPTS``: additional Python command-line options. -* ``TESTTIMEOUT``: timeout in seconds (default: 10 minutes). + This is similar to ``make test``, but uses the ``-ugui`` to also run GUI tests. + .. versionadded:: 3.14 -make ci -^^^^^^^ - -This is similar to ``make test``, but uses the ``-ugui`` to also run GUI tests. - -.. versionadded:: 3.14 - - -make buildbottest -^^^^^^^^^^^^^^^^^ - -This is similar to ``make test``, but uses the ``--slow-ci`` -option and default timeout of 20 minutes, instead of ``--fast-ci`` option. +.. make-target:: make buildbottest + This is similar to ``make test``, but uses the ``--slow-ci`` + option and default timeout of 20 minutes, instead of ``--fast-ci`` option. -make regen-all -^^^^^^^^^^^^^^ +.. make-target:: make regen-all -Regenerate (almost) all generated files. These include (but are not -limited to) bytecode cases, and parser generator file. -``make regen-stdlib-module-names`` and ``autoconf`` must be run -separately for the remaining `generated files <#generated-files>`_. + Regenerate (almost) all generated files. These include (but are not + limited to) bytecode cases, and parser generator files. See + :ref:`generated-files` for the targets that must be run separately. C extensions @@ -1600,23 +1592,23 @@ Built-in modules have no ``__file__`` attribute: File "", line 1, in AttributeError: module 'sys' has no attribute '__file__' -Other C extensions are built as dynamic libraries, like the ``_asyncio`` module. +Other C extensions are built as dynamic libraries, like the ``_zoneinfo`` module. They are built with the ``Py_BUILD_CORE_MODULE`` macro defined. -Example on Linux x86-64: +For example on Linux x86-64: .. code-block:: pycon - >>> import _asyncio - >>> _asyncio - - >>> _asyncio.__file__ - '/usr/lib64/python3.9/lib-dynload/_asyncio.cpython-39-x86_64-linux-gnu.so' + >>> import _zoneinfo + >>> _zoneinfo + + >>> _zoneinfo.__file__ + '/.../lib.linux-x86_64-3.16/_zoneinfo.cpython-316d-x86_64-linux-gnu.so' :file:`Modules/Setup` is used to generate Makefile targets to build C extensions. At the beginning of the files, C extensions are built as built-in modules. Extensions defined after the ``*shared*`` marker are built as dynamic libraries. -The :c:macro:`!PyAPI_FUNC()`, :c:macro:`!PyAPI_DATA()` and +The :c:macro:`PyAPI_FUNC()`, :c:macro:`PyAPI_DATA()` and :c:macro:`PyMODINIT_FUNC` macros of :file:`Include/exports.h` are defined differently depending if the ``Py_BUILD_CORE_MODULE`` macro is defined: diff --git a/Doc/using/ios.rst b/Doc/using/ios.rst index d7cf699d5bc3bf..4dbb6e77459c48 100644 --- a/Doc/using/ios.rst +++ b/Doc/using/ios.rst @@ -226,7 +226,7 @@ To add Python to an iOS Xcode project: .. code-block:: bash set -e - source $PROJECT_DIR/Python.xcframework/build/build_utils.sh + source "$PROJECT_DIR/Python.xcframework/build/utils.sh" install_python Python.xcframework app If you have placed your XCframework somewhere other than the root of your diff --git a/Doc/using/windows.rst b/Doc/using/windows.rst index 2b824fd70e7d34..a53e3040ab3bdc 100644 --- a/Doc/using/windows.rst +++ b/Doc/using/windows.rst @@ -169,7 +169,8 @@ It should be separated from the tag by a slash (either ``/`` or ``\``), and may be shortened to any prefix of its full value. Specifying the company is optional when it is ``PythonCore``, and specifying the tag is optional (but not the slash) when you want the latest release from a -specific company. +specific company. For more details about how tags are matched, see +:ref:`pymanager-tag-matching`. .. code:: @@ -998,6 +999,145 @@ Credentials may be embedded in either setting, however, when ``HTTPS_PROXY`` has credentials embedded they will be used in preference to other credentials. Only one set of credentials will be used for both types of proxy. +.. _pymanager-tag-matching: + +Tags and tag matching +--------------------- + +.. versionchanged:: 26.5 + Previously the behavior was very similar to what is described here, but had + many more inconsistencies. We have streamlined it and ensured the behavior is + well-defined, which may result in some small differences from previous + versions. + +Tags identify Python runtimes that may be installed, modified, or launched by +the Python install manager. They are based on :pep:`514`, with matching and +ordering rules specific to the install manager. These rules do not change +PEP 514's specification. + +A tag has three components: Company, Tag, and Platform. Each part is +case-insensitive, but the exact comparison semantics vary. + +.. code-block:: text + + [/][-] + +The Company is optional and defaults to ``PythonCore``. It may contain slashes, +while the rest may not, and so the last forward slash or backslash separates it +from the Tag. When omitting Company, also omit any slash. In general, Company is +matched as a prefix. + +The optional Platform suffix is one of the known platform suffixes, currently +``-32``, ``-64``, or ``-arm64``. Broadly speaking, the Platform suffix behaves +as part of the Tag, but it may be implied from the current configuration or +system in a way that is different from the rest of the tag. It also does not +parse or compare as a number. + +Runtimes are defined as having a primary tag, and offer several more tags for +installation (``install-for``) and launching (``run-for``). Whether the install +tags or the launch tags are considered depends on context, but both sets are +used to filter to matching runtimes, from which the best option is selected +using more complex logic. If a runtime doesn't have specific tags, its primary +tag may be used for installation and uninstallation (but not launch - it will be +unlaunchable without any ``run-for`` tags). + +You can observe this behaviour using ``py list`` with and without the ``--one`` +option (to show the "best" or all candidates) and with and without the +``--online``/``--source`` options (to show install matches or launch matches). + +Tags are not version numbers, though numeric sequences are treated as version +numbers. Hyphens divide the Tag into segments, where each segment is either text +or a version number with a text suffix. The version number part includes those +ending in ``a``, ``b``, or ``rc`` followed by a serial number, but any other +text is considered a separate suffix. Without a serial number, an ``a``, ``b`` +or ``rc`` suffix is also treated as text. + +Broadly, numbers are compared as numerical versions, so ``3.10`` is greater than +``3.1``, ``3.14`` is greater than ``3.14a1``, and ``3`` is equal to both ``3.0`` +and ``3.0.0``. Text is compared case-insensitively, and must match, so ``2024b`` +is not equivalent to ``2024.0`` because ``'b' != ''``. + +.. note:: + + Do not interpret the depth of detail of the following few paragraphs about + constraints as representing their importance. Constraints are a necessary + evil, and we would prefer nobody rely on them instead of specific tags, which + are much simpler to reason about for publishers and end users. Unfortunately, + we are obliged to explain how our implementation handles constraints, and + this is the place for that explanation. + +Constraints can select multiple tags. A matching tag will start with the same +segments as the constraining tag, where text segments are equal and numeric +segments are above, below, or equal, according to the constraint. The first +non-equal segment determines the result: non-equal text results in failure, +while a non-equal version either succeeds or fails based on the constraint. +Remaining segments in the matching tag are ignored. + +When applying a constraint check to a version segment that was shorter than the +constraint (e.g. ``3`` has one field and so is shorter than ``==3.14``), it is +interpreted as representing any possible version. This will result in many +failed constraint checks that will usually be overridden by a more specific one. +For example ``3`` could represent ``3.99`` and so it is not equal to ``3.14``, +but if the install is offering tags ``['3', '3.14']`` then the later tag will +result in the constraint matching. + +Here are some example cases showing the different applications of constraints: + +* ``>=3.13`` matches ``3.13.4`` and ``3.14`` +* ``>3.13`` matches ``3.14``, but not ``3.13.4`` +* ``>=3.13t`` matches ``3.14t``, while ``>=3.13`` does not +* ``>=3.13`` matches ``3.14-abc`` but not ``3.14abc`` +* ``>3.13-abc-123`` matches ``3.14`` (number is higher) and ``3.13-abc-124`` + (number ``124`` is higher) but not ``3.13-def-123`` (text mismatch), + ``3.14def`` (text ``def`` is not in the constraint), + ``3.13-abc-123`` (number is not higher) or + ``3.13-abc`` (not enough segments) +* ``>=3.0`` matches ``3``, but ``>3.0`` does not +* ``<4`` matches ``3``, but ``<3.12`` and ``<=3.12`` do not +* ``==3.0`` does not match ``3``, while ``==3.10`` still matches ``3.10.1`` +* ``!=3.13`` rejects ``3`` (because it could be ``3.13``), but ``!=3.13t`` + accepts ``3`` (different suffix) + +The difference between hyphen-separated segments and adjacent text and numbers +is subtle, but should not be relevant to most users. Publishers will choose and +advertise the tags for their packages, and so should take care to choose between +adjacent text or separated text. For example, the core python\.org releases use +``3.14t`` for free-threaded builds, which should not be confused with original +builds and so ``3.14t`` should not match ``3.14``. However, ``3.14`` should be +allowed to match with ``3.14-64`` (recalling that known platform tags are +treated at text), and so the hyphenated convention is chosen instead. End users +get intuitive behavior because the publisher considered the impact of their +choices. + +Note that constraints are not complementary, that is, the ``>=`` constraint does +not necessarily include every tag rejected by the ``<`` constraint. +For example, ``3.14t`` satisfies neither ``>=3.14`` nor ``<3.14``. + +Publishers should provide tags for each way they want users to select a +runtime, commonly from most specific to least specific. For example, a +free-threaded runtime might offer ``3.14.4t``, ``3.14t``, and ``3t``. Include +a generic tag such as ``3.14`` only if that runtime should be eligible for +generic requests. Exact matches of user requested tags are preferred over +partial matches, so include the tags you expect your users to use. + +The primary tag should be orderable against other runtimes from the same +publisher, but should not change with in-place updates. Also be aware that +operations reading an online feed assume that the feed is already ordered, and +will select the first runtime that matches requested tags. + +After filtering, runtimes are ordered by their primary tag and then the best is +selected. "Best" is very context sensitive, and may be influenced by user +configuration (such as the default tag), active virtual environments, prerelease +marks on a runtime's sortable version, and whether a windowless interpreter is +desired (such as launching through ``pyw.exe`` instead of ``py.exe``). + +Most notably, a request containing the text field ``dev``, such as ``3-dev``, +causes prereleases to be considered on equal footing with stable releases. +Typically stable releases are preferred unless filtering excludes all viable +options. However, this behavior only takes place after filtering, and so a +runtime that does not offer a tag containing ``dev`` will not be selected as a +candidate. + .. _pymanager-troubleshoot: Troubleshooting diff --git a/Doc/whatsnew/2.3.rst b/Doc/whatsnew/2.3.rst index f2d37244071751..984f9d363eb718 100644 --- a/Doc/whatsnew/2.3.rst +++ b/Doc/whatsnew/2.3.rst @@ -1357,7 +1357,7 @@ complete list of changes, or look through the CVS logs for all the details. other than ``e`` and ``10``. (Contributed by Raymond Hettinger.) * Several new POSIX functions (:func:`!getpgid`, :func:`!killpg`, :func:`!lchown`, - :func:`!loadavg`, :func:`!major`, :func:`!makedev`, :func:`!minor`, and + :func:`!getloadavg`, :func:`!major`, :func:`!makedev`, :func:`!minor`, and :func:`!mknod`) were added to the :mod:`posix` module that underlies the :mod:`os` module. (Contributed by Gustavo Niemeyer, Geert Jansen, and Denis S. Otkidach.) @@ -1658,7 +1658,7 @@ complete list of changes, or look through the CVS logs for all the details. has been added. The "idna" encoding can be used to convert between a Unicode domain name and the ASCII-compatible encoding (ACE) of that name. :: - >{}>{}> u"www.Alliancefrançaise.nu".encode("idna") + >>> u"www.Alliancefrançaise.nu".encode("idna") 'www.xn--alliancefranaise-npb.nu' The :mod:`socket` module has also been extended to transparently convert diff --git a/Doc/whatsnew/2.4.rst b/Doc/whatsnew/2.4.rst index ae3e9ed4646eed..a3c75ec7f8e39a 100644 --- a/Doc/whatsnew/2.4.rst +++ b/Doc/whatsnew/2.4.rst @@ -1072,7 +1072,7 @@ complete list of changes, or look through the CVS logs for all the details. 3.0 version of the package uses a new incremental parser for MIME messages, available in the :mod:`!email.FeedParser` module. The new parser doesn't require reading the entire message into memory, and doesn't raise exceptions if a - message is malformed; instead it records any problems in the :attr:`!defect` + message is malformed; instead it records any problems in the :attr:`!defects` attribute of the message. (Developed by Anthony Baxter, Barry Warsaw, Thomas Wouters, and others.) @@ -1177,7 +1177,7 @@ complete list of changes, or look through the CVS logs for all the details. import logging logging.basicConfig(filename='/var/log/application.log', level=0, # Log all messages - format='%(levelname):%(process):%(thread):%(message)') + format='%(levelname)s:%(process)d:%(thread)d:%(message)s') Other additions to the :mod:`logging` package include a ``log(level, msg)`` convenience method, as well as a :class:`~logging.handlers.TimedRotatingFileHandler` class that diff --git a/Doc/whatsnew/2.5.rst b/Doc/whatsnew/2.5.rst index 33d8b741ba1766..1e0c2bec9fb98e 100644 --- a/Doc/whatsnew/2.5.rst +++ b/Doc/whatsnew/2.5.rst @@ -1738,7 +1738,7 @@ constructor that will create a :c:expr:`PyObject *` pointer. A simple usage:: d = {} ctypes.pythonapi.PyObject_SetItem(ctypes.py_object(d), ctypes.py_object("abc"), ctypes.py_object(1)) - # d is now {'abc', 1}. + # d is now {'abc': 1}. Don't forget to use :func:`~ctypes.py_object`; if it's omitted you end up with a segmentation fault. @@ -2182,7 +2182,7 @@ Changes to Python's build process and to the C API include: specified *type* and uses a fast-calling qualifier. :c:macro:`Py_LOCAL_INLINE` does the same thing and also requests the function be inlined. If macro :c:macro:`!PY_LOCAL_AGGRESSIVE` is defined before - :file:`python.h` is included, a set of more aggressive optimizations are enabled + :file:`Python.h` is included, a set of more aggressive optimizations are enabled for the module; you should benchmark the results to find out if these optimizations actually make the code faster. (Contributed by Fredrik Lundh at the NeedForSpeed sprint.) diff --git a/Doc/whatsnew/2.7.rst b/Doc/whatsnew/2.7.rst index de90c37a38d38f..bc766e37e380e8 100644 --- a/Doc/whatsnew/2.7.rst +++ b/Doc/whatsnew/2.7.rst @@ -2298,7 +2298,7 @@ and getting the pointer requires providing the name: void *vtable; - if (!PyCapsule_IsValid(capsule, "mymodule.CAPI") { + if (!PyCapsule_IsValid(capsule, "mymodule.CAPI")) { PyErr_SetString(PyExc_ValueError, "argument type invalid"); return NULL; } diff --git a/Doc/whatsnew/3.1.rst b/Doc/whatsnew/3.1.rst index 9dc63c7976c512..6c170abd402b87 100644 --- a/Doc/whatsnew/3.1.rst +++ b/Doc/whatsnew/3.1.rst @@ -310,7 +310,7 @@ New, Improved, and Deprecated Modules CSV header, SQL field list, or user input:: >>> query = input() - SELECT region, dept, count(*) FROM main GROUPBY region, dept + SELECT region, dept, count(*) FROM main GROUP BY region, dept >>> cursor.execute(query) >>> query_fields = [desc[0] for desc in cursor.description] diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 9f30141c10aa4c..605de1570b2b96 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -1912,8 +1912,8 @@ C APIs pending removal are * :meth:`!importlib.machinery.PathFinder.find_module` * :meth:`!importlib.machinery.WindowsRegistryFinder.find_module` * :func:`!importlib.util.module_for_loader` -* :func:`!importlib.util.set_loader_wrapper` -* :func:`!importlib.util.set_package_wrapper` +* :func:`!importlib.util.set_loader` +* :func:`!importlib.util.set_package` * :class:`!pkgutil.ImpImporter` * :class:`!pkgutil.ImpLoader` * :meth:`!pathlib.Path.link_to` diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index d822d83164db6f..ddaf07384a6b8c 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -133,7 +133,7 @@ Security improvements: C API improvements: -* :ref:`PEP 697 `, unstable C API tier +* :ref:`PEP 689 `, unstable C API tier * :ref:`PEP 683 `, immortal objects CPython implementation improvements: @@ -1035,13 +1035,13 @@ unittest Add a ``--durations`` command line option, showing the N slowest test cases:: - python3 -m unittest --durations=3 lib.tests.test_threading + python3 -m unittest --durations=3 test.test_threading ..... Slowest test durations ---------------------------------------------------------------------- - 1.210s test_timeout (Lib.test.test_threading.BarrierTests) - 1.003s test_default_timeout (Lib.test.test_threading.BarrierTests) - 0.518s test_timeout (Lib.test.test_threading.EventTests) + 1.210s test_timeout (test.test_threading.BarrierTests) + 1.003s test_default_timeout (test.test_threading.BarrierTests) + 0.518s test_timeout (test.test_threading.EventTests) (0.000 durations hidden. Use -v to show these durations.) ---------------------------------------------------------------------- @@ -1862,8 +1862,9 @@ New Features ------------ .. _whatsnew312-pep697: +.. _whatsnew312-pep689: -* :pep:`697`: Introduce the :ref:`Unstable C API tier `, +* :pep:`689`: Introduce the :ref:`Unstable C API tier `, intended for low-level tools like debuggers and JIT compilers. This API may change in each minor release of CPython without deprecation warnings. diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 87d14720fb78d6..b07c94287bdee6 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -1555,7 +1555,7 @@ ssl key agreement, extending the previous :meth:`ssl.SSLContext.set_ecdh_curve` method. This new API provides the ability to list multiple groups and - supports fixed-field and post-quantum groups in addition to ECDH + supports finite-field and post-quantum groups in addition to ECDH curves. This method can also be used to control what key shares are sent in the TLS handshake. * :meth:`ssl.SSLSocket.group` returns the group selected for doing key @@ -2526,6 +2526,17 @@ New features (Contributed by Victor Stinner in :gh:`141510`.) +* Add the :pep:`757` export/import functions to the limited C API: + + * :c:func:`PyLong_GetNativeLayout` + * :c:func:`PyLong_Export` + * :c:func:`PyLong_FreeExport` + * :c:func:`PyLongWriter_Create` + * :c:func:`PyLongWriter_Finish` + * :c:func:`PyLongWriter_Discard` + + (Contributed by Sergey B Kirpichev in :gh:`143869`.) + * Add :c:func:`PyObject_CallFinalizerFromDealloc` function to the limited C API. (Contributed by Victor Stinner in :gh:`146063`.) diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index 4af5d3c0bd072e..101be240d23760 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -62,11 +62,48 @@ Summary -- Release highlights .. This section singles out the most important changes in Python 3.16. Brevity is key. +* :ref:`Set operations and Unicode properties in regular expressions + `. +* :ref:`Encrypted Client Hello support in ssl `. +* :ref:`Faster interpreter startup `. +* :ref:`Faster UUID generation `, with + UUIDv4 and UUIDv7 about 15 and 30 times faster in microbenchmarks. +* :ref:`Build support for tvOS, watchOS, and visionOS + `. +* :ref:`Improved error messages `. New features ============ +.. _whatsnew316-improved-error-messages: + +Improved error messages +----------------------- + +* :exc:`SyntaxError` messages now suggest Python alternatives to some + keywords from other programming languages. For example, ``switch x:`` + suggests ``match``, ``delete x`` suggests ``del``, and ``function f():`` + suggests ``def``. + (Contributed by Zang Langyan in :gh:`151128`.) + +* Improve :exc:`SyntaxError` message for ``&&`` and ``||`` operators, + suggesting ``and``/``&`` and ``or``/``|`` respectively. + (Contributed by Aniket, Sergey B Kirpichev, and Nikita Sobolev in + :gh:`146495`.) + +* Suggest the closest valid feature name in the :exc:`SyntaxError` message + when an unknown name is imported from :mod:`__future__`. + (Contributed by Nikita Sobolev and Pablo Galindo Salgado in :gh:`145868`.) + +* :exc:`TypeError` messages for some instance methods called with too many + positional arguments now suggest checking whether the method definition is + missing the ``self`` parameter. (Contributed by Suren Nihalani in + :gh:`152315`.) + +* Add context notes to :exc:`TypeError` raised while hashing a + :class:`tuple`, identifying the index of the item whose hash failed. + (Contributed by Ivan Zorin in :gh:`161433`.) Other language changes @@ -95,11 +132,6 @@ Other language changes they reference) alive indefinitely. (Contributed by Łukasz Langa in :gh:`102960`.) -* :exc:`TypeError` messages for some instance methods called with too many - positional arguments now suggest checking whether the method definition is - missing the ``self`` parameter. (Contributed by Suren Nihalani in - :gh:`152315`.) - * :func:`anext` and :func:`aiter` are now implemented in Python instead of C. The awaitable returned by ``anext(it, default)`` and the ``__anext__()`` of the iterator returned by ``aiter(callable, ...)`` are @@ -204,6 +236,19 @@ concurrent.interpreters :meth:`~concurrent.interpreters.Interpreter.close`. (Contributed by Peter Bierma in :gh:`158370`.) + +copy +---- + +* :func:`copy.replace` now supports :class:`argparse.Namespace`, + :class:`optparse.Values`, :class:`decimal.Context`, + :class:`tarfile.TarInfo`, and CSV dialects (instances of + :class:`csv.Dialect` subclasses and objects returned by + :func:`csv.get_dialect`). + (Contributed by Serhiy Storchaka and Stan Ulbrych in :gh:`155043`, + :gh:`155044`, :gh:`155041`, :gh:`155040`, and :gh:`155033`.) + + csv --- @@ -463,6 +508,34 @@ gzip (Contributed by Marin Misur in :gh:`91372`.) +http +---- + +* Add the ``QUERY`` HTTP method (:rfc:`10008`) to :class:`~http.HTTPMethod`. + (Contributed by Michiel W. Beijen in :gh:`153309`.) + + +http.client +----------- + +* :class:`~http.client.HTTPConnection` now honors an ``Expect: 100-Continue`` + request header: the request body is sent only after the server responds + with ``100 Continue``, or once the new *continue_timeout* of + :meth:`~http.client.HTTPConnection.request` and + :meth:`~http.client.HTTPConnection.putrequest` expires. If the server + responds with a final status instead, the body is not sent. + :meth:`~http.client.HTTPConnection.getresponse` gains a *support_continue* + parameter to return ``100 Continue`` responses to the caller. + (Contributed by Ashwin Menon, Tim Bartlett and Senthil Kumaran in + :gh:`42550`.) + +* Add the :exc:`~http.client.ProxyTunnelError` exception, a subclass of + :exc:`OSError` raised when a proxy rejects the CONNECT request sent for + :meth:`~http.client.HTTPConnection.set_tunnel`. It exposes the status + code, reason and headers of the proxy's response. + (Contributed by Senthil Kumaran in :gh:`51540`.) + + imaplib ------- @@ -528,6 +601,10 @@ ipaddress and :meth:`IPv6Network.next_network() ` methods to find the next nearest network with a specific prefix size. (Contributed by Faisal Mahmood in :gh:`87027`.) +* Add :meth:`IPv4Network.prev_network() ` + and :meth:`IPv6Network.prev_network() ` + methods to find the previous nearest network with a specific prefix size. + (Contributed by Faisal Mahmood in :gh:`87027`.) logging @@ -642,6 +719,8 @@ quopri re -- +.. _whatsnew316-re-set-operations-and-unicode-properties: + * :mod:`re` now supports set operations and nested sets in character classes, as described in `Unicode Technical Standard #18 `__: set difference (``[A--B]``), @@ -715,6 +794,8 @@ sqlite3 ssl --- +.. _whatsnew316-ssl-ech: + * Added support for :ref:`Encrypted Client Hello ` (ECH) with OpenSSL 4.0 or later. Clients can supply an ECHConfigList discovered through DNS, for example using a third-party library such as @@ -812,6 +893,15 @@ tkinter native print dialog. It requires Tk 8.7/9.0 or newer. (Contributed by Serhiy Storchaka in :gh:`153256`.) +* Added :mod:`tkinter` support for the Tk 9.1 accessibility API: the + :attr:`~tkinter.Misc.accessible` property of widgets returns an + :class:`~tkinter.Accessible` object whose attributes are exposed to + assistive technologies such as screen readers and whose methods register + custom widgets with the platform accessibility API, and the + :meth:`~tkinter.Misc.tk_check_screenreader` method reports whether a + screen reader is running. + (Contributed by Serhiy Storchaka in :gh:`145578`.) + * Added new window-management methods :meth:`~tkinter.Misc.winfo_isdark` (dark mode detection), :meth:`~tkinter.Wm.wm_iconbadge` (application icon badge) and :meth:`~tkinter.Wm.wm_stackorder` (toplevel stacking order). @@ -891,6 +981,14 @@ tkinter with no unit suffix to :class:`int` or :class:`float`. (Contributed by Serhiy Storchaka in :gh:`153513`.) +* Added symbolic constants for Tk option values which were missing in + :mod:`!tkinter.constants`: values for the *labelanchor*, *compound*, + *bordermode*, *direction*, *stretch*, *tabstyle*, *insertunfocussed*, + *mode*, *selecttype*, *show*, *state*, *validate* and *type* options, + the names of the themed widget states, and ``PIXELS`` for scrolling + the :class:`~tkinter.Text` widget. + (Contributed by Serhiy Storchaka in :gh:`159301`.) + turtle ------ @@ -907,6 +1005,18 @@ unicodedata * The Unicode database has been updated to Unicode 18.0.0. +urllib.request +-------------- + +* :class:`~urllib.request.ProxyBasicAuthHandler` and + :class:`~urllib.request.ProxyDigestAuthHandler` now authenticate with a + proxy that answers the CONNECT request for an ``https`` URL with + ``407 Proxy Authentication Required``. When no credentials are available, + :exc:`~urllib.error.HTTPError` with code 407 is raised instead of + :exc:`~urllib.error.URLError`. + (Contributed by Senthil Kumaran in :gh:`51540`.) + + uuid ---- @@ -1015,6 +1125,35 @@ zlib Optimizations ============= +.. _whatsnew316-interpreter-startup: + +* Interpreter startup is faster, and several common operations have been + optimized. Improvements include less work during garbage collection and + faster construction of :class:`list`, :class:`dict`, :class:`bytes`, and + :class:`bytearray` objects. + Startup performance is tracked on the `Python Speed Center + `__. + (Contributed by Pablo Galindo Salgado, Cody Maloney, and Pieter Eendebak in + :gh:`159137`, :gh:`160858`, :gh:`160913`, and :gh:`87613`.) + +* Speed up iteration over asynchronous generators: values yielded to + ``async for`` no longer create and discard a :exc:`StopIteration` exception + at every nesting level. The ``async_generators`` pyperformance benchmark is + about 30% faster. + (Contributed by Shamil Abdulaev in :gh:`158304`.) + + +ast +--- + +* AST nodes now store attributes inline with shared keys instead of + allocating a separate dictionary for each node. Benchmarks of this + change measured about 16% faster :func:`ast.parse` calls and 52% less + memory used by the resulting ASTs. + (Contributed by Lysandros Nikolaou and Pablo Galindo Salgado + in :gh:`153568`.) + + datetime and time ----------------- @@ -1258,6 +1397,16 @@ New deprecations Use :pep:`695` syntax instead. (Contributed by Nikita Sobolev in :gh:`150190`.) +* :mod:`urllib.parse`: + + * Accepting objects with false values (like ``0`` and ``[]``) except empty + strings, byte-like objects and ``None`` is now deprecated in the URL + parsing functions, and is scheduled to raise :exc:`TypeError` in Python + 3.18. This extends the same deprecation made in Python 3.14 for + :func:`~urllib.parse.parse_qsl`, :func:`~urllib.parse.parse_qs`, and + :func:`~urllib.parse.urlencode`. + (Contributed by Jacob Walls in :gh:`63293`.) + .. Add deprecations above alphabetically, not here at the end. .. include:: ../deprecations/pending-removal-in-3.17.rst @@ -1287,6 +1436,28 @@ that may require changes to your code. :exc:`TypeError`. (Contributed by Serhiy Storchaka in :gh:`152587`.) +* :func:`urllib.parse.urlsplit` and :func:`urllib.parse.urlparse` now raise + :exc:`ValueError` when the URL is parsed if the port is not a decimal + number or is outside the range 0--65535. Previously, the error was only + raised when the :attr:`!port` attribute was read. This also affects + :func:`~urllib.parse.urljoin`, and :func:`~urllib.parse.urldefrag` for + URLs with a fragment. Code which never read :attr:`!port` may now fail + on URLs such as: + + * SCP-style addresses given a scheme, such as + ``git+ssh://git@github.com:org/repo.git``; + * multi-host URLs with ports, such as + ``mongodb://h1:27017,h2:27017/db`` or + ``postgresql://h1:5432,h2:5433/db``; + * URL templates with a placeholder port, such as + ``http://localhost:{port}/``. + + These are not valid URLs under :rfc:`3986`. To process them, split off + the netloc yourself before calling :func:`~urllib.parse.urlsplit`, for + example by replacing it with a placeholder host, or fill in templates + before parsing them. + (Contributed by Miguel Brito and Senthil Kumaran in :gh:`88037`.) + * :class:`uuid.UUID` now consistently raises :exc:`TypeError` when the *bytes* or *bytes_le* argument is not a :class:`bytes` object. Previously, some invalid inputs raised :exc:`AssertionError` or @@ -1380,6 +1551,31 @@ Build changes (Contributed by Stan Ulbrych, Florian Mayer and AnnaAr321 in :gh:`156049`.) +* iOS builds can now be configured as free-threaded or debug builds. + + (Contributed by Russell Keith-Magee in :gh:`159385`.) + +* :mod:`tkinter` now requires Tcl/Tk built with thread support. + This only affects Tcl/Tk 8, since Tcl/Tk 9 is always built with + thread support. + + (Contributed by Michiel de Hoon in :gh:`140494`.) + + +.. _whatsnew316-apple-platforms: + +Build support for tvOS, watchOS, and visionOS +--------------------------------------------- + +Added build support for Apple's tvOS, watchOS, and visionOS. +These platforms are not currently included in the supported +platform tiers defined by :pep:`11`. +See the :source:`tvOS `, +:source:`watchOS `, and +:source:`visionOS ` build instructions. + +(Contributed by Russell Keith-Magee in :gh:`131999`.) + C API changes ============= diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst index 273cbe7ff925a0..32d1bbe6af7f83 100644 --- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -2281,7 +2281,7 @@ Changes in the Python API * :meth:`!importlib.util.module_for_loader` now sets ``__loader__`` and ``__package__`` unconditionally to properly support reloading. If this is not desired then you will need to set these attributes manually. You can use - :func:`!importlib.util.module_to_load` for module management. + :func:`!importlib.util._module_to_load` for module management. * Import now resets relevant attributes (e.g. ``__name__``, ``__loader__``, ``__package__``, ``__file__``, ``__cached__``) unconditionally when reloading. @@ -2298,7 +2298,7 @@ Changes in the Python API * Frozen modules no longer define a ``__file__`` attribute. It's semantically incorrect for frozen modules to set the attribute as they are not loaded from any explicit location. If you must know that a module comes from frozen code - then you can see if the module's ``__spec__.location`` is set to ``'frozen'``, + then you can see if the module's ``__spec__.origin`` is set to ``'frozen'``, check if the loader is a subclass of :class:`importlib.machinery.FrozenImporter`, or if Python 2 compatibility is necessary you can use :func:`!imp.is_frozen`. diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst index b0382dd8215bc0..d2daf052e43f82 100644 --- a/Doc/whatsnew/3.6.rst +++ b/Doc/whatsnew/3.6.rst @@ -2111,11 +2111,11 @@ API and Feature Removals name for a given path. (Contributed by Yury Selivanov in :issue:`13248`.) -* ``traceback.Ignore`` class and ``traceback.usage``, ``traceback.modname``, - ``traceback.fullmodname``, ``traceback.find_lines_from_code``, - ``traceback.find_lines``, ``traceback.find_strings``, - ``traceback.find_executable_lines`` methods were removed from the - :mod:`traceback` module. They were undocumented methods deprecated since +* ``trace.Ignore`` class and ``trace.usage``, ``trace.modname``, + ``trace.fullmodname``, ``trace.find_lines_from_code``, + ``trace.find_lines``, ``trace.find_strings``, + ``trace.find_executable_linenos`` methods were removed from the + :mod:`trace` module. They were undocumented methods deprecated since Python 3.2 and equivalent functionality is available from private methods. * The ``tk_menuBar()`` and ``tk_bindForTraversal()`` dummy methods in diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 6900c97e9546c9..671b83f87d3430 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -248,7 +248,7 @@ locales are available on the running system, it has the downside of having no effect on extension modules (such as GNU ``readline``), child processes running non-Python applications, and child processes running older versions of Python. To reduce the risk of corrupting text data when communicating with such -components, Python 3.7 also implements :ref:`whatsnew37-pep540`). +components, Python 3.7 also implements :ref:`whatsnew37-pep538`. The UTF-8 mode is enabled by default when the locale is ``C`` or ``POSIX``, and the :pep:`538` locale coercion feature fails to change it to a UTF-8 based @@ -1085,7 +1085,7 @@ itertools :func:`itertools.islice` now accepts :meth:`integer-like objects ` as start, stop, -and slice arguments. +and step arguments. (Contributed by Will Roberts in :issue:`30537`.) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 5f3fad1a1a51b5..37dfde66687387 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -2082,7 +2082,7 @@ Changes in the C API return NULL; #if PY_VERSION_HEX < 0x03080000 // Workaround for Python issue 35810; no longer necessary in Python 3.8 - PY_INCREF(type) + Py_INCREF(type); #endif return foo; } diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index 49a52b7504bc95..e2731b6de15a3e 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -1031,8 +1031,8 @@ Removed :exc:`DeprecationWarning` since Python 3.8. (Contributed by Inada Naoki in :issue:`39377`) -* ``with (await asyncio.lock):`` and ``with (yield from asyncio.lock):`` statements are - not longer supported, use ``async with lock`` instead. The same is correct for +* ``with (await lock):`` and ``with (yield from lock):`` statements are + no longer supported, use ``async with lock`` instead. The same is correct for ``asyncio.Condition`` and ``asyncio.Semaphore``. (Contributed by Andrew Svetlov in :issue:`34793`.) diff --git a/Include/Python.h b/Include/Python.h index 337119c15fe8b6..3d723a83616d50 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -35,12 +35,12 @@ // // The and headers are not included by limited C API // version 3.13 and newer. -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # include // errno # include // FILE* # include // getenv() #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13) # include // tolower() # ifndef MS_WINDOWS # include // close() diff --git a/Include/abstract.h b/Include/abstract.h index 80f3298701d249..34ab9eca44739e 100644 --- a/Include/abstract.h +++ b/Include/abstract.h @@ -141,7 +141,7 @@ extern "C" { This is the equivalent of the Python statement: del o.attr_name. Implemented as a macro in the limited C API 3.12 and older. */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030d0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13) # define PyObject_DelAttrString(O, A) PyObject_SetAttrString((O), (A), NULL) #endif @@ -155,7 +155,7 @@ extern "C" { statement: del o.attr_name. Implemented as a macro in the limited C API 3.12 and older. */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030d0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13) # define PyObject_DelAttr(O, A) PyObject_SetAttr((O), (A), NULL) #endif @@ -282,7 +282,7 @@ PyAPI_FUNC(Py_ssize_t) PyVectorcall_NARGS(size_t nargsf); "tuple" and keyword arguments "dict". "dict" may also be NULL */ PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 12) #define PY_VECTORCALL_ARGUMENTS_OFFSET \ (_Py_STATIC_CAST(size_t, 1) << (8 * sizeof(size_t) - 1)) @@ -407,7 +407,7 @@ PyAPI_FUNC(int) PyIter_Check(PyObject *); This function always succeeds. */ PyAPI_FUNC(int) PyAIter_Check(PyObject *); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030e0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 14) /* Return 1 and set 'item' to the next item of 'iter' on success. * Return 0 and set 'item' to NULL when there are no remaining values. * Return -1, set 'item' to NULL and set an exception on error. @@ -426,7 +426,7 @@ PyAPI_FUNC(int) PyIter_NextItem(PyObject *iter, PyObject **item); Prefer PyIter_NextItem() instead. */ PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) /* Takes generator, coroutine or iterator object and sends the value into it. Returns: @@ -885,7 +885,7 @@ PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o, If an error other than KeyError is raised, return -1 and set *result to NULL. */ -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(int) PyMapping_GetOptionalItem(PyObject *, PyObject *, PyObject **); PyAPI_FUNC(int) PyMapping_GetOptionalItemString(PyObject *, const char *, PyObject **); #endif diff --git a/Include/audit.h b/Include/audit.h index 9be54ad4411096..e9f8cc371cdad8 100644 --- a/Include/audit.h +++ b/Include/audit.h @@ -5,7 +5,7 @@ extern "C" { #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(int) PySys_Audit( const char *event, const char *argFormat, diff --git a/Include/boolobject.h b/Include/boolobject.h index b56e2baecaa36c..aa412780cfdfc2 100644 --- a/Include/boolobject.h +++ b/Include/boolobject.h @@ -18,7 +18,7 @@ PyAPI_DATA(PyLongObject) _Py_FalseStruct; PyAPI_DATA(PyLongObject) _Py_TrueStruct; /* Use these macros */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) # define Py_False Py_GetConstantBorrowed(Py_CONSTANT_FALSE) # define Py_True Py_GetConstantBorrowed(Py_CONSTANT_TRUE) #else @@ -37,7 +37,7 @@ PyAPI_FUNC(int) Py_IsFalse(PyObject *x); /* Macros for returning Py_True or Py_False, respectively. * Only treat Py_True and Py_False as immortal in the limited C API 3.12 * and newer. */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 12) # define Py_RETURN_TRUE return Py_NewRef(Py_True) # define Py_RETURN_FALSE return Py_NewRef(Py_False) #else diff --git a/Include/cpython/initconfig.h b/Include/cpython/initconfig.h index 846e05fb26a4d8..e1bdc6174d9de0 100644 --- a/Include/cpython/initconfig.h +++ b/Include/cpython/initconfig.h @@ -15,7 +15,12 @@ typedef struct { } _type; const char *func; const char *err_msg; +#if defined(__cplusplus) && defined(__clang__) && __clang_major__ < 22 + // gh-160436: clang < 22 warns in the implicit copy/move members + int exitcode; +#else Py_DEPRECATED(3.16) int exitcode; +#endif } PyStatus; PyAPI_FUNC(PyStatus) PyStatus_Ok(void); diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h index e0c2ce286f13ce..49f73ad73eae01 100644 --- a/Include/cpython/objimpl.h +++ b/Include/cpython/objimpl.h @@ -15,15 +15,40 @@ static inline size_t _PyObject_SIZE(PyTypeObject *type) { Note that there's no memory wastage in doing this, as malloc has to return (at worst) pointer-aligned memory anyway. + + Return SIZE_MAX on integer overflow (if the result would be greater than + PY_SSIZE_T_MAX). */ #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0 # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2" #endif -static inline size_t _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems) { - size_t size = _Py_STATIC_CAST(size_t, type->tp_basicsize); - size += _Py_STATIC_CAST(size_t, nitems) * _Py_STATIC_CAST(size_t, type->tp_itemsize); - return _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P); +static inline size_t +_PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems_signed) +{ + size_t basicsize; + size_t size; + + assert(type->tp_itemsize >= 0); + basicsize = _Py_STATIC_CAST(size_t, type->tp_basicsize); + + if (type->tp_itemsize != 0) { + size_t nitems = _Py_STATIC_CAST(size_t, nitems_signed); + size_t itemsize = _Py_STATIC_CAST(size_t, type->tp_itemsize); + if (nitems > ((size_t)PY_SSIZE_T_MAX - basicsize) / itemsize) { + return SIZE_MAX; + } + size = basicsize + nitems * itemsize; + } + else { + size = basicsize; + } + + size = _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P); + if (size > (size_t)PY_SSIZE_T_MAX) { + return SIZE_MAX; + } + return size; } diff --git a/Include/cpython/pyatomic.h b/Include/cpython/pyatomic.h index 71a76bdc82ce47..3115b9e0824372 100644 --- a/Include/cpython/pyatomic.h +++ b/Include/cpython/pyatomic.h @@ -4,12 +4,14 @@ // Operations are sequentially consistent unless they have a suffix indicating // otherwise. If in doubt, prefer the sequentially consistent operations. // -// The "_relaxed" suffix for load and store operations indicates the "relaxed" -// memory order. They don't provide synchronization, but (roughly speaking) -// guarantee somewhat sane behavior for races instead of undefined behavior. -// In practice, they correspond to "normal" hardware load and store +// The "_relaxed" suffix indicates the "relaxed" memory order. Relaxed +// operations don't provide synchronization, but (roughly speaking) guarantee +// somewhat sane behavior for races instead of undefined behavior. In practice, +// relaxed loads and stores correspond to "normal" hardware load and store // instructions, so they are almost as inexpensive as plain loads and stores -// in C. +// in C. Relaxed read-modify-write operations, such as +// _Py_atomic_add_*_relaxed, are still atomic, but do not order surrounding +// memory accesses. // // Note that atomic read-modify-write operations like _Py_atomic_add_* return // the previous value of the atomic variable, not the new value. @@ -49,12 +51,27 @@ // expected = obj // return False // +// def _Py_atomic_compare_exchange_relaxed(obj, expected, desired): +// # relaxed consistency +// if obj == expected: +// obj = desired +// return True +// else: +// expected = obj +// return False +// // def _Py_atomic_add(obj, value): // # sequential consistency // old_obj = obj // obj += value // return old_obj // +// def _Py_atomic_add_relaxed(obj, value): +// # relaxed consistency +// old_obj = obj +// obj += value +// return old_obj +// // def _Py_atomic_and(obj, value): // # sequential consistency // old_obj = obj @@ -130,6 +147,50 @@ static inline Py_ssize_t _Py_atomic_add_ssize(Py_ssize_t *obj, Py_ssize_t value); +// --- _Py_atomic_add_relaxed ------------------------------------------------ +// Atomically adds `value` to `obj` and returns the previous value +// (relaxed consistency, i.e., no ordering) + +static inline int +_Py_atomic_add_int_relaxed(int *obj, int value); + +static inline int8_t +_Py_atomic_add_int8_relaxed(int8_t *obj, int8_t value); + +static inline int16_t +_Py_atomic_add_int16_relaxed(int16_t *obj, int16_t value); + +static inline int32_t +_Py_atomic_add_int32_relaxed(int32_t *obj, int32_t value); + +static inline int64_t +_Py_atomic_add_int64_relaxed(int64_t *obj, int64_t value); + +static inline intptr_t +_Py_atomic_add_intptr_relaxed(intptr_t *obj, intptr_t value); + +static inline unsigned int +_Py_atomic_add_uint_relaxed(unsigned int *obj, unsigned int value); + +static inline uint8_t +_Py_atomic_add_uint8_relaxed(uint8_t *obj, uint8_t value); + +static inline uint16_t +_Py_atomic_add_uint16_relaxed(uint16_t *obj, uint16_t value); + +static inline uint32_t +_Py_atomic_add_uint32_relaxed(uint32_t *obj, uint32_t value); + +static inline uint64_t +_Py_atomic_add_uint64_relaxed(uint64_t *obj, uint64_t value); + +static inline uintptr_t +_Py_atomic_add_uintptr_relaxed(uintptr_t *obj, uintptr_t value); + +static inline Py_ssize_t +_Py_atomic_add_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t value); + + // --- _Py_atomic_compare_exchange ------------------------------------------- // Performs an atomic compare-and-exchange. // @@ -185,6 +246,64 @@ static inline int _Py_atomic_compare_exchange_ptr(void *obj, void *expected, void *value); +// --- _Py_atomic_compare_exchange_relaxed ----------------------------------- +// Performs an atomic compare-and-exchange (relaxed consistency, i.e., no +// ordering on either success or failure). +// +// - If `*obj` and `*expected` are equal, store `desired` into `*obj` +// and return 1 (success). +// - Otherwise, store the `*obj` current value into `*expected` +// and return 0 (failure). +// +// These correspond to the C11 atomic_compare_exchange_strong_explicit() +// function with memory_order_relaxed for both the success and failure +// memory orders. + +static inline int +_Py_atomic_compare_exchange_int_relaxed(int *obj, int *expected, int desired); + +static inline int +_Py_atomic_compare_exchange_int8_relaxed(int8_t *obj, int8_t *expected, int8_t desired); + +static inline int +_Py_atomic_compare_exchange_int16_relaxed(int16_t *obj, int16_t *expected, int16_t desired); + +static inline int +_Py_atomic_compare_exchange_int32_relaxed(int32_t *obj, int32_t *expected, int32_t desired); + +static inline int +_Py_atomic_compare_exchange_int64_relaxed(int64_t *obj, int64_t *expected, int64_t desired); + +static inline int +_Py_atomic_compare_exchange_intptr_relaxed(intptr_t *obj, intptr_t *expected, intptr_t desired); + +static inline int +_Py_atomic_compare_exchange_uint_relaxed(unsigned int *obj, unsigned int *expected, unsigned int desired); + +static inline int +_Py_atomic_compare_exchange_uint8_relaxed(uint8_t *obj, uint8_t *expected, uint8_t desired); + +static inline int +_Py_atomic_compare_exchange_uint16_relaxed(uint16_t *obj, uint16_t *expected, uint16_t desired); + +static inline int +_Py_atomic_compare_exchange_uint32_relaxed(uint32_t *obj, uint32_t *expected, uint32_t desired); + +static inline int +_Py_atomic_compare_exchange_uint64_relaxed(uint64_t *obj, uint64_t *expected, uint64_t desired); + +static inline int +_Py_atomic_compare_exchange_uintptr_relaxed(uintptr_t *obj, uintptr_t *expected, uintptr_t desired); + +static inline int +_Py_atomic_compare_exchange_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t *expected, Py_ssize_t desired); + +// NOTE: `obj` and `expected` are logically `void**` types, but we use `void*` +// so that we can pass types like `PyObject**` without a cast. +static inline int +_Py_atomic_compare_exchange_ptr_relaxed(void *obj, void *expected, void *value); + + // --- _Py_atomic_exchange --------------------------------------------------- // Atomically replaces `*obj` with `value` and returns the previous value of `*obj`. @@ -585,7 +704,7 @@ static inline void _Py_atomic_fence_release(void); # define Py_ATOMIC_GCC_H # include "pyatomic_gcc.h" # undef Py_ATOMIC_GCC_H -#elif _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_ATOMICS__) +#elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_ATOMICS__) # define Py_ATOMIC_STD_H # include "pyatomic_std.h" # undef Py_ATOMIC_STD_H diff --git a/Include/cpython/pyatomic_gcc.h b/Include/cpython/pyatomic_gcc.h index c2441a1eb91392..b0544c64490abd 100644 --- a/Include/cpython/pyatomic_gcc.h +++ b/Include/cpython/pyatomic_gcc.h @@ -63,6 +63,61 @@ _Py_atomic_add_ssize(Py_ssize_t *obj, Py_ssize_t value) { return __atomic_fetch_add(obj, value, __ATOMIC_SEQ_CST); } +// --- _Py_atomic_add_relaxed ------------------------------------------------ + +static inline int +_Py_atomic_add_int_relaxed(int *obj, int value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline int8_t +_Py_atomic_add_int8_relaxed(int8_t *obj, int8_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline int16_t +_Py_atomic_add_int16_relaxed(int16_t *obj, int16_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline int32_t +_Py_atomic_add_int32_relaxed(int32_t *obj, int32_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline int64_t +_Py_atomic_add_int64_relaxed(int64_t *obj, int64_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline intptr_t +_Py_atomic_add_intptr_relaxed(intptr_t *obj, intptr_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline unsigned int +_Py_atomic_add_uint_relaxed(unsigned int *obj, unsigned int value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline uint8_t +_Py_atomic_add_uint8_relaxed(uint8_t *obj, uint8_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline uint16_t +_Py_atomic_add_uint16_relaxed(uint16_t *obj, uint16_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline uint32_t +_Py_atomic_add_uint32_relaxed(uint32_t *obj, uint32_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline uint64_t +_Py_atomic_add_uint64_relaxed(uint64_t *obj, uint64_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline uintptr_t +_Py_atomic_add_uintptr_relaxed(uintptr_t *obj, uintptr_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + +static inline Py_ssize_t +_Py_atomic_add_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t value) +{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); } + + // --- _Py_atomic_compare_exchange ------------------------------------------- static inline int @@ -136,6 +191,79 @@ _Py_atomic_compare_exchange_ptr(void *obj, void *expected, void *desired) __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); } +// --- _Py_atomic_compare_exchange_relaxed ----------------------------------- + +static inline int +_Py_atomic_compare_exchange_int_relaxed(int *obj, int *expected, int desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_int8_relaxed(int8_t *obj, int8_t *expected, int8_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_int16_relaxed(int16_t *obj, int16_t *expected, int16_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_int32_relaxed(int32_t *obj, int32_t *expected, int32_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_int64_relaxed(int64_t *obj, int64_t *expected, int64_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_intptr_relaxed(intptr_t *obj, intptr_t *expected, intptr_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_uint_relaxed(unsigned int *obj, unsigned int *expected, unsigned int desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_uint8_relaxed(uint8_t *obj, uint8_t *expected, uint8_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_uint16_relaxed(uint16_t *obj, uint16_t *expected, uint16_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_uint32_relaxed(uint32_t *obj, uint32_t *expected, uint32_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_uint64_relaxed(uint64_t *obj, uint64_t *expected, uint64_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_uintptr_relaxed(uintptr_t *obj, uintptr_t *expected, uintptr_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t *expected, Py_ssize_t desired) +{ return __atomic_compare_exchange_n(obj, expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +static inline int +_Py_atomic_compare_exchange_ptr_relaxed(void *obj, void *expected, void *desired) +{ return __atomic_compare_exchange_n((void **)obj, (void **)expected, desired, 0, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + + // --- _Py_atomic_exchange --------------------------------------------------- static inline int diff --git a/Include/cpython/pyatomic_msc.h b/Include/cpython/pyatomic_msc.h index ef5b6d2d71c5c0..c00c541dc62fdc 100644 --- a/Include/cpython/pyatomic_msc.h +++ b/Include/cpython/pyatomic_msc.h @@ -125,6 +125,132 @@ _Py_atomic_add_ssize(Py_ssize_t *obj, Py_ssize_t value) } +// --- _Py_atomic_add_relaxed ------------------------------------------------ + +// The "_nf" (no fence) intrinsic variants provide relaxed memory order on +// ARM64. On x86 and x86-64 they do not exist; the plain interlocked +// intrinsics are used instead, which have stronger (sequentially consistent) +// ordering. That is a conforming implementation of relaxed memory order; +// x86 simply has no cheaper atomic read-modify-write. + +static inline int8_t +_Py_atomic_add_int8_relaxed(int8_t *obj, int8_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(char); +#if defined(_M_ARM64) + return (int8_t)_InterlockedExchangeAdd8_nf((volatile char *)obj, (char)value); +#else + return (int8_t)_InterlockedExchangeAdd8((volatile char *)obj, (char)value); +#endif +} + +static inline int16_t +_Py_atomic_add_int16_relaxed(int16_t *obj, int16_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(short); +#if defined(_M_ARM64) + return (int16_t)_InterlockedExchangeAdd16_nf((volatile short *)obj, (short)value); +#else + return (int16_t)_InterlockedExchangeAdd16((volatile short *)obj, (short)value); +#endif +} + +static inline int32_t +_Py_atomic_add_int32_relaxed(int32_t *obj, int32_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(long); +#if defined(_M_ARM64) + return (int32_t)_InterlockedExchangeAdd_nf((volatile long *)obj, (long)value); +#else + return (int32_t)_InterlockedExchangeAdd((volatile long *)obj, (long)value); +#endif +} + +static inline int64_t +_Py_atomic_add_int64_relaxed(int64_t *obj, int64_t value) +{ +#if defined(_M_ARM64) + _Py_atomic_ASSERT_ARG_TYPE(__int64); + return (int64_t)_InterlockedExchangeAdd64_nf((volatile __int64 *)obj, (__int64)value); +#elif defined(_M_X64) + _Py_atomic_ASSERT_ARG_TYPE(__int64); + return (int64_t)_InterlockedExchangeAdd64((volatile __int64 *)obj, (__int64)value); +#else + int64_t old_value = _Py_atomic_load_int64_relaxed(obj); + for (;;) { + int64_t new_value = old_value + value; + if (_Py_atomic_compare_exchange_int64(obj, &old_value, new_value)) { + return old_value; + } + } +#endif +} + +static inline uint8_t +_Py_atomic_add_uint8_relaxed(uint8_t *obj, uint8_t value) +{ + return (uint8_t)_Py_atomic_add_int8_relaxed((int8_t *)obj, (int8_t)value); +} + +static inline uint16_t +_Py_atomic_add_uint16_relaxed(uint16_t *obj, uint16_t value) +{ + return (uint16_t)_Py_atomic_add_int16_relaxed((int16_t *)obj, (int16_t)value); +} + +static inline uint32_t +_Py_atomic_add_uint32_relaxed(uint32_t *obj, uint32_t value) +{ + return (uint32_t)_Py_atomic_add_int32_relaxed((int32_t *)obj, (int32_t)value); +} + +static inline int +_Py_atomic_add_int_relaxed(int *obj, int value) +{ + _Py_atomic_ASSERT_ARG_TYPE(int32_t); + return (int)_Py_atomic_add_int32_relaxed((int32_t *)obj, (int32_t)value); +} + +static inline unsigned int +_Py_atomic_add_uint_relaxed(unsigned int *obj, unsigned int value) +{ + _Py_atomic_ASSERT_ARG_TYPE(int32_t); + return (unsigned int)_Py_atomic_add_int32_relaxed((int32_t *)obj, (int32_t)value); +} + +static inline uint64_t +_Py_atomic_add_uint64_relaxed(uint64_t *obj, uint64_t value) +{ + return (uint64_t)_Py_atomic_add_int64_relaxed((int64_t *)obj, (int64_t)value); +} + +static inline intptr_t +_Py_atomic_add_intptr_relaxed(intptr_t *obj, intptr_t value) +{ +#if SIZEOF_VOID_P == 8 + _Py_atomic_ASSERT_ARG_TYPE(int64_t); + return (intptr_t)_Py_atomic_add_int64_relaxed((int64_t *)obj, (int64_t)value); +#else + _Py_atomic_ASSERT_ARG_TYPE(int32_t); + return (intptr_t)_Py_atomic_add_int32_relaxed((int32_t *)obj, (int32_t)value); +#endif +} + +static inline uintptr_t +_Py_atomic_add_uintptr_relaxed(uintptr_t *obj, uintptr_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(intptr_t); + return (uintptr_t)_Py_atomic_add_intptr_relaxed((intptr_t *)obj, (intptr_t)value); +} + +static inline Py_ssize_t +_Py_atomic_add_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(intptr_t); + return (Py_ssize_t)_Py_atomic_add_intptr_relaxed((intptr_t *)obj, (intptr_t)value); +} + + // --- _Py_atomic_compare_exchange ------------------------------------------- static inline int @@ -280,6 +406,200 @@ _Py_atomic_compare_exchange_ssize(Py_ssize_t *obj, Py_ssize_t *expected, Py_ssiz } +// --- _Py_atomic_compare_exchange_relaxed ----------------------------------- + +// As with _Py_atomic_add_*_relaxed, the "_nf" (no fence) intrinsic variants +// provide relaxed memory order on ARM64, and the plain interlocked intrinsics +// stand in for them on x86 and x86-64, where they do not exist. + +static inline int +_Py_atomic_compare_exchange_int8_relaxed(int8_t *obj, int8_t *expected, int8_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(char); +#if defined(_M_ARM64) + int8_t initial = (int8_t)_InterlockedCompareExchange8_nf( + (volatile char *)obj, + (char)value, + (char)*expected); +#else + int8_t initial = (int8_t)_InterlockedCompareExchange8( + (volatile char *)obj, + (char)value, + (char)*expected); +#endif + if (initial == *expected) { + return 1; + } + *expected = initial; + return 0; +} + +static inline int +_Py_atomic_compare_exchange_int16_relaxed(int16_t *obj, int16_t *expected, int16_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(short); +#if defined(_M_ARM64) + int16_t initial = (int16_t)_InterlockedCompareExchange16_nf( + (volatile short *)obj, + (short)value, + (short)*expected); +#else + int16_t initial = (int16_t)_InterlockedCompareExchange16( + (volatile short *)obj, + (short)value, + (short)*expected); +#endif + if (initial == *expected) { + return 1; + } + *expected = initial; + return 0; +} + +static inline int +_Py_atomic_compare_exchange_int32_relaxed(int32_t *obj, int32_t *expected, int32_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(long); +#if defined(_M_ARM64) + int32_t initial = (int32_t)_InterlockedCompareExchange_nf( + (volatile long *)obj, + (long)value, + (long)*expected); +#else + int32_t initial = (int32_t)_InterlockedCompareExchange( + (volatile long *)obj, + (long)value, + (long)*expected); +#endif + if (initial == *expected) { + return 1; + } + *expected = initial; + return 0; +} + +static inline int +_Py_atomic_compare_exchange_int64_relaxed(int64_t *obj, int64_t *expected, int64_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(__int64); +#if defined(_M_ARM64) + int64_t initial = (int64_t)_InterlockedCompareExchange64_nf( + (volatile __int64 *)obj, + (__int64)value, + (__int64)*expected); +#else + int64_t initial = (int64_t)_InterlockedCompareExchange64( + (volatile __int64 *)obj, + (__int64)value, + (__int64)*expected); +#endif + if (initial == *expected) { + return 1; + } + *expected = initial; + return 0; +} + +static inline int +_Py_atomic_compare_exchange_ptr_relaxed(void *obj, void *expected, void *value) +{ +#if defined(_M_ARM64) + void *initial = _InterlockedCompareExchangePointer_nf( + (void**)obj, + value, + *(void**)expected); +#else + void *initial = _InterlockedCompareExchangePointer( + (void**)obj, + value, + *(void**)expected); +#endif + if (initial == *(void**)expected) { + return 1; + } + *(void**)expected = initial; + return 0; +} + + +static inline int +_Py_atomic_compare_exchange_uint8_relaxed(uint8_t *obj, uint8_t *expected, uint8_t value) +{ + return _Py_atomic_compare_exchange_int8_relaxed((int8_t *)obj, + (int8_t *)expected, + (int8_t)value); +} + +static inline int +_Py_atomic_compare_exchange_uint16_relaxed(uint16_t *obj, uint16_t *expected, uint16_t value) +{ + return _Py_atomic_compare_exchange_int16_relaxed((int16_t *)obj, + (int16_t *)expected, + (int16_t)value); +} + +static inline int +_Py_atomic_compare_exchange_uint32_relaxed(uint32_t *obj, uint32_t *expected, uint32_t value) +{ + return _Py_atomic_compare_exchange_int32_relaxed((int32_t *)obj, + (int32_t *)expected, + (int32_t)value); +} + +static inline int +_Py_atomic_compare_exchange_int_relaxed(int *obj, int *expected, int value) +{ + _Py_atomic_ASSERT_ARG_TYPE(int32_t); + return _Py_atomic_compare_exchange_int32_relaxed((int32_t *)obj, + (int32_t *)expected, + (int32_t)value); +} + +static inline int +_Py_atomic_compare_exchange_uint_relaxed(unsigned int *obj, unsigned int *expected, unsigned int value) +{ + _Py_atomic_ASSERT_ARG_TYPE(int32_t); + return _Py_atomic_compare_exchange_int32_relaxed((int32_t *)obj, + (int32_t *)expected, + (int32_t)value); +} + +static inline int +_Py_atomic_compare_exchange_uint64_relaxed(uint64_t *obj, uint64_t *expected, uint64_t value) +{ + return _Py_atomic_compare_exchange_int64_relaxed((int64_t *)obj, + (int64_t *)expected, + (int64_t)value); +} + +static inline int +_Py_atomic_compare_exchange_intptr_relaxed(intptr_t *obj, intptr_t *expected, intptr_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(void*); + return _Py_atomic_compare_exchange_ptr_relaxed((void**)obj, + (void**)expected, + (void*)value); +} + +static inline int +_Py_atomic_compare_exchange_uintptr_relaxed(uintptr_t *obj, uintptr_t *expected, uintptr_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(void*); + return _Py_atomic_compare_exchange_ptr_relaxed((void**)obj, + (void**)expected, + (void*)value); +} + +static inline int +_Py_atomic_compare_exchange_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t *expected, Py_ssize_t value) +{ + _Py_atomic_ASSERT_ARG_TYPE(void*); + return _Py_atomic_compare_exchange_ptr_relaxed((void**)obj, + (void**)expected, + (void*)value); +} + + // --- _Py_atomic_exchange --------------------------------------------------- static inline int8_t diff --git a/Include/cpython/pyatomic_std.h b/Include/cpython/pyatomic_std.h index b8540ff3baf20a..92a8cba1302c18 100644 --- a/Include/cpython/pyatomic_std.h +++ b/Include/cpython/pyatomic_std.h @@ -112,6 +112,113 @@ _Py_atomic_add_ssize(Py_ssize_t *obj, Py_ssize_t value) } +// --- _Py_atomic_add_relaxed ------------------------------------------------ + +static inline int +_Py_atomic_add_int_relaxed(int *obj, int value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(int)*)obj, value, + memory_order_relaxed); +} + +static inline int8_t +_Py_atomic_add_int8_relaxed(int8_t *obj, int8_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(int8_t)*)obj, value, + memory_order_relaxed); +} + +static inline int16_t +_Py_atomic_add_int16_relaxed(int16_t *obj, int16_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(int16_t)*)obj, value, + memory_order_relaxed); +} + +static inline int32_t +_Py_atomic_add_int32_relaxed(int32_t *obj, int32_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(int32_t)*)obj, value, + memory_order_relaxed); +} + +static inline int64_t +_Py_atomic_add_int64_relaxed(int64_t *obj, int64_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(int64_t)*)obj, value, + memory_order_relaxed); +} + +static inline intptr_t +_Py_atomic_add_intptr_relaxed(intptr_t *obj, intptr_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(intptr_t)*)obj, value, + memory_order_relaxed); +} + +static inline unsigned int +_Py_atomic_add_uint_relaxed(unsigned int *obj, unsigned int value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(unsigned int)*)obj, value, + memory_order_relaxed); +} + +static inline uint8_t +_Py_atomic_add_uint8_relaxed(uint8_t *obj, uint8_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(uint8_t)*)obj, value, + memory_order_relaxed); +} + +static inline uint16_t +_Py_atomic_add_uint16_relaxed(uint16_t *obj, uint16_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(uint16_t)*)obj, value, + memory_order_relaxed); +} + +static inline uint32_t +_Py_atomic_add_uint32_relaxed(uint32_t *obj, uint32_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(uint32_t)*)obj, value, + memory_order_relaxed); +} + +static inline uint64_t +_Py_atomic_add_uint64_relaxed(uint64_t *obj, uint64_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(uint64_t)*)obj, value, + memory_order_relaxed); +} + +static inline uintptr_t +_Py_atomic_add_uintptr_relaxed(uintptr_t *obj, uintptr_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(uintptr_t)*)obj, value, + memory_order_relaxed); +} + +static inline Py_ssize_t +_Py_atomic_add_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t value) +{ + _Py_USING_STD; + return atomic_fetch_add_explicit((_Atomic(Py_ssize_t)*)obj, value, + memory_order_relaxed); +} + + // --- _Py_atomic_compare_exchange ------------------------------------------- static inline int @@ -227,6 +334,149 @@ _Py_atomic_compare_exchange_ptr(void *obj, void *expected, void *desired) } +// --- _Py_atomic_compare_exchange_relaxed ----------------------------------- + +static inline int +_Py_atomic_compare_exchange_int_relaxed(int *obj, int *expected, int desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(int)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_int8_relaxed(int8_t *obj, int8_t *expected, int8_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(int8_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_int16_relaxed(int16_t *obj, int16_t *expected, int16_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(int16_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_int32_relaxed(int32_t *obj, int32_t *expected, int32_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(int32_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_int64_relaxed(int64_t *obj, int64_t *expected, int64_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(int64_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_intptr_relaxed(intptr_t *obj, intptr_t *expected, intptr_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(intptr_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_uint_relaxed(unsigned int *obj, unsigned int *expected, unsigned int desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(unsigned int)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_uint8_relaxed(uint8_t *obj, uint8_t *expected, uint8_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(uint8_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_uint16_relaxed(uint16_t *obj, uint16_t *expected, uint16_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(uint16_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_uint32_relaxed(uint32_t *obj, uint32_t *expected, uint32_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(uint32_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_uint64_relaxed(uint64_t *obj, uint64_t *expected, uint64_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(uint64_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_uintptr_relaxed(uintptr_t *obj, uintptr_t *expected, uintptr_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(uintptr_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t *expected, Py_ssize_t desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(Py_ssize_t)*)obj, + expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + +static inline int +_Py_atomic_compare_exchange_ptr_relaxed(void *obj, void *expected, void *desired) +{ + _Py_USING_STD; + return atomic_compare_exchange_strong_explicit((_Atomic(void *)*)obj, + (void **)expected, desired, + memory_order_relaxed, + memory_order_relaxed); +} + + // --- _Py_atomic_exchange --------------------------------------------------- static inline int diff --git a/Include/dictobject.h b/Include/dictobject.h index 0384e3131dcdb5..f6f0303237495c 100644 --- a/Include/dictobject.h +++ b/Include/dictobject.h @@ -58,7 +58,7 @@ PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key); PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item); PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) // Return the object from dictionary *op* which has a key *key*. // - If the key is present, set *result to a new strong reference to the value // and return 1. @@ -68,7 +68,7 @@ PyAPI_FUNC(int) PyDict_GetItemRef(PyObject *mp, PyObject *key, PyObject **result PyAPI_FUNC(int) PyDict_GetItemStringRef(PyObject *mp, const char *key, PyObject **result); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030F0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15) // Inserts `key` with a value `default_value`, if `key` is not already present // in the dictionary. If `result` is not NULL, then the value associated // with `key` is returned in `*result` (either the existing value, or the now @@ -80,7 +80,7 @@ PyAPI_FUNC(int) PyDict_GetItemStringRef(PyObject *mp, const char *key, PyObject PyAPI_FUNC(int) PyDict_SetDefaultRef(PyObject *mp, PyObject *key, PyObject *default_value, PyObject **result); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) PyAPI_FUNC(PyObject *) PyObject_GenericGetDict(PyObject *, void *); #endif diff --git a/Include/import.h b/Include/import.h index c062e46bff46bf..95ab0228420a32 100644 --- a/Include/import.h +++ b/Include/import.h @@ -43,7 +43,7 @@ PyAPI_FUNC(PyObject *) PyImport_AddModuleObject( PyAPI_FUNC(PyObject *) PyImport_AddModule( const char *name /* UTF-8 encoded string */ ); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(PyObject *) PyImport_AddModuleRef( const char *name /* UTF-8 encoded string */ ); diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index a0fea389b6502c..c404fc6e67d193 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -350,9 +350,10 @@ _PyEval_SpecialMethodCanSuggest(PyObject *self, int oparg); #define _PY_EVAL_PLEASE_STOP_BIT (1U << 5) #define _PY_EVAL_EXPLICIT_MERGE_BIT (1U << 6) #define _PY_EVAL_JIT_INVALIDATE_COLD_BIT (1U << 7) +#define _PY_EVAL_PROCESS_DELAYED_BIT (1U << 8) /* Reserve a few bits for future use */ -#define _PY_EVAL_EVENTS_BITS 8 +#define _PY_EVAL_EVENTS_BITS 9 #define _PY_EVAL_EVENTS_MASK ((1 << _PY_EVAL_EVENTS_BITS)-1) static inline void diff --git a/Include/internal/pycore_freelist_state.h b/Include/internal/pycore_freelist_state.h index 46e2a82ea03456..266703a63b61f1 100644 --- a/Include/internal/pycore_freelist_state.h +++ b/Include/internal/pycore_freelist_state.h @@ -31,6 +31,7 @@ extern "C" { # define Py_pycfunctionobject_MAXFREELIST 16 # define Py_pycmethodobject_MAXFREELIST 16 # define Py_pymethodobjects_MAXFREELIST 20 +# define Py_tracebacks_MAXFREELIST 8 // A generic freelist of either PyObjects or other data structures. struct _Py_freelist { @@ -66,6 +67,7 @@ struct _Py_freelists { struct _Py_freelist pycfunctionobject; struct _Py_freelist pycmethodobject; struct _Py_freelist pymethodobjects; + struct _Py_freelist tracebacks; }; #ifdef __cplusplus diff --git a/Include/internal/pycore_global_objects_fini.h b/Include/internal/pycore_global_objects_fini.h index ee3a593db37b69..41bc2c5d8fb140 100644 --- a/Include/internal/pycore_global_objects_fini.h +++ b/Include/internal/pycore_global_objects_fini.h @@ -42,7 +42,7 @@ _PyStaticObject_CheckLongSingleton(PyObject *obj, long value, int is_bool) lv_tag |= IMMORTALITY_BIT_MASK; } _PyObject_ASSERT(obj, long_value->lv_tag == lv_tag); - _PyObject_ASSERT(obj, long_value->ob_digit[0] == Py_ABS(value)); + _PyObject_ASSERT(obj, long_value->ob_digit[0] == (digit)Py_ABS(value)); } diff --git a/Include/internal/pycore_global_objects_fini_generated.h b/Include/internal/pycore_global_objects_fini_generated.h index 483a1dc58791cd..fed4cef9353f10 100644 --- a/Include/internal/pycore_global_objects_fini_generated.h +++ b/Include/internal/pycore_global_objects_fini_generated.h @@ -229,6 +229,7 @@ _PyStaticObjects_CheckAll(PyInterpreterState *interp) { _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(__str__), "__str__", 7); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(__sub__), "__sub__", 7); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(__subclasscheck__), "__subclasscheck__", 17); + _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(__subclasses__), "__subclasses__", 14); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(__subclasshook__), "__subclasshook__", 16); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(__truediv__), "__truediv__", 11); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(__trunc__), "__trunc__", 9); diff --git a/Include/internal/pycore_global_strings.h b/Include/internal/pycore_global_strings.h index 949e19196dc527..70870105113051 100644 --- a/Include/internal/pycore_global_strings.h +++ b/Include/internal/pycore_global_strings.h @@ -241,6 +241,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(__str__) STRUCT_FOR_ID(__sub__) STRUCT_FOR_ID(__subclasscheck__) + STRUCT_FOR_ID(__subclasses__) STRUCT_FOR_ID(__subclasshook__) STRUCT_FOR_ID(__truediv__) STRUCT_FOR_ID(__trunc__) diff --git a/Include/internal/pycore_instruction_sequence.h b/Include/internal/pycore_instruction_sequence.h index 6257b771fd9539..1643776c47ee69 100644 --- a/Include/internal/pycore_instruction_sequence.h +++ b/Include/internal/pycore_instruction_sequence.h @@ -40,8 +40,8 @@ typedef struct instruction_sequence { /* Map of a label id to instruction offset (index into s_instrs). * If s_labelmap is NULL, then each label id is the offset itself. */ - int *s_labelmap; int s_labelmap_size; + int *s_labelmap; /* PyList of instruction sequences of nested functions */ PyObject *s_nested; diff --git a/Include/internal/pycore_interp_structs.h b/Include/internal/pycore_interp_structs.h index a9ad61f458944a..c9002081306eb8 100644 --- a/Include/internal/pycore_interp_structs.h +++ b/Include/internal/pycore_interp_structs.h @@ -654,7 +654,7 @@ struct _warnings_runtime_state { // Whether the thread calling fork() owns the warnings lock. bool lock_held_at_fork; #endif - long filters_version; + Py_ssize_t filters_version; PyObject *context; }; diff --git a/Include/internal/pycore_magic_number.h b/Include/internal/pycore_magic_number.h index 15d3ad6f3a336b..041256df56b493 100644 --- a/Include/internal/pycore_magic_number.h +++ b/Include/internal/pycore_magic_number.h @@ -304,6 +304,8 @@ Known values: Python 3.16a1 3704 (Replace DELETE_ATTR with PUSH_NULL; STORE_ATTR) Python 3.16a1 3705 (Add INTRINSIC_ADD_CONDITIONAL_ANNOTATION) Python 3.16a1 3706 (Add INTRINSIC_MATCH_CLASS_ISINSTANCE) + Python 3.16a1 3707 (GET_AWAITABLE also pushes NULL and None for SEND) + Python 3.16a1 3708 (GET_ANEXT also pushes NULL and None for SEND) Python 3.17 will start with 3750 @@ -313,7 +315,7 @@ Known values: */ -#define PYC_MAGIC_NUMBER 3706 +#define PYC_MAGIC_NUMBER 3708 /* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes (little-endian) and then appending b'\r\n'. */ #define PYC_MAGIC_NUMBER_TOKEN \ diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index f6a71fe0417601..30333e14655a60 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -27,9 +27,6 @@ extern "C" { // `ob_ref_shared` are used for flags. #define _Py_REF_DEFERRED (PY_SSIZE_T_MAX / 8) -/* For backwards compatibility -- Do not use this */ -#define _Py_IsImmortalLoose(op) _Py_IsImmortal - /* Check if an object is consistent. For example, ensure that the reference counter is greater than or equal to 1, and ensure that ob_type is not NULL. @@ -712,6 +709,8 @@ _PyObject_ResurrectEnd(PyObject *op) if (_Py_IsOwnedByCurrentThread(op) && local == 1 && shared == 0) { // Fast-path: object has a single refcount and is owned by this thread _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0); + _Py_atomic_store_uintptr_relaxed(&op->ob_tid, _Py_UNOWNED_TID); + _Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, _Py_REF_MERGED); # ifdef Py_TRACE_REFS _Py_ForgetReference(op); # endif diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h index 04b826ed09144e..f1e6f1beae644a 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -714,9 +714,9 @@ int _PyOpcode_num_pushed(int opcode, int oparg) { case GET_AITER: return 1; case GET_ANEXT: - return 2; + return 4; case GET_AWAITABLE: - return 1; + return 3; case GET_ITER: return 2; case GET_ITER_SELF: diff --git a/Include/internal/pycore_pyatomic_ft_wrappers.h b/Include/internal/pycore_pyatomic_ft_wrappers.h index 31455f6b815312..de3a78ec02ec02 100644 --- a/Include/internal/pycore_pyatomic_ft_wrappers.h +++ b/Include/internal/pycore_pyatomic_ft_wrappers.h @@ -139,6 +139,8 @@ extern "C" { _Py_atomic_load_ullong_relaxed(&value) #define FT_ATOMIC_ADD_SSIZE(value, new_value) \ (void)_Py_atomic_add_ssize(&value, new_value) +#define FT_ATOMIC_ADD_SSIZE_RELAXED(value, new_value) \ + (void)_Py_atomic_add_ssize_relaxed(&value, new_value) #define FT_MUTEX_LOCK(lock) PyMutex_Lock(lock) #define FT_MUTEX_LOCK_FLAGS(lock, flags) PyMutex_LockFlags(lock, flags) #define FT_MUTEX_UNLOCK(lock) PyMutex_Unlock(lock) @@ -204,6 +206,7 @@ extern "C" { #define FT_ATOMIC_LOAD_ULLONG_RELAXED(value) value #define FT_ATOMIC_STORE_ULLONG_RELAXED(value, new_value) value = new_value #define FT_ATOMIC_ADD_SSIZE(value, new_value) (void)(value += new_value) +#define FT_ATOMIC_ADD_SSIZE_RELAXED(value, new_value) (void)(value += new_value) #define FT_MUTEX_LOCK(lock) do {} while (0) #define FT_MUTEX_LOCK_FLAGS(lock, flags) do {} while (0) #define FT_MUTEX_UNLOCK(lock) do {} while (0) diff --git a/Include/internal/pycore_qsbr.h b/Include/internal/pycore_qsbr.h index 91d3415614b154..98c23384221d9c 100644 --- a/Include/internal/pycore_qsbr.h +++ b/Include/internal/pycore_qsbr.h @@ -61,9 +61,6 @@ struct _qsbr_thread_state { // and in a different thread. size_t deferred_page_memory; - // True if the deferred memory frees should be processed. - bool should_process; - // Is this thread state allocated? bool allocated; struct _qsbr_thread_state *freelist_next; @@ -142,14 +139,6 @@ _Py_qsbr_advance(struct _qsbr_shared *shared); extern uint64_t _Py_qsbr_shared_next(struct _qsbr_shared *shared); -// Return true if deferred memory frees held by QSBR should be processed to -// determine if they can be safely freed. -static inline bool -_Py_qsbr_should_process(struct _qsbr_thread_state *qsbr) -{ - return qsbr->should_process; -} - // Have the read sequences advanced to the given goal? If this returns true, // it safe to reclaim any memory tagged with the goal (or earlier goal). extern bool diff --git a/Include/internal/pycore_runtime_init_generated.h b/Include/internal/pycore_runtime_init_generated.h index 4ee954942521ae..313fbea999d070 100644 --- a/Include/internal/pycore_runtime_init_generated.h +++ b/Include/internal/pycore_runtime_init_generated.h @@ -1516,6 +1516,7 @@ extern "C" { INIT_ID(__str__), \ INIT_ID(__sub__), \ INIT_ID(__subclasscheck__), \ + INIT_ID(__subclasses__), \ INIT_ID(__subclasshook__), \ INIT_ID(__truediv__), \ INIT_ID(__trunc__), \ diff --git a/Include/internal/pycore_unicodeobject_generated.h b/Include/internal/pycore_unicodeobject_generated.h index 3aad3345abc4d0..b34648433b89a5 100644 --- a/Include/internal/pycore_unicodeobject_generated.h +++ b/Include/internal/pycore_unicodeobject_generated.h @@ -193,6 +193,7 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) { offsetof(struct _Py_global_strings, identifiers._py___str__._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py___sub__._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py___subclasscheck__._ascii.ob_base), + offsetof(struct _Py_global_strings, identifiers._py___subclasses__._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py___subclasshook__._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py___truediv__._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py___trunc__._ascii.ob_base), diff --git a/Include/internal/pycore_uop_ids.h b/Include/internal/pycore_uop_ids.h index ba6e0252024a6d..31a8e60259f4ba 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -689,8 +689,8 @@ enum { _FOR_ITER_VIRTUAL_r23, _FOR_ITER_VIRTUAL_TIER_TWO_r23, _GET_AITER_r11, - _GET_ANEXT_r12, - _GET_AWAITABLE_r11, + _GET_ANEXT_r13, + _GET_AWAITABLE_r13, _GET_ITER_r12, _GET_ITER_TRAD_r12, _GET_LEN_r12, diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index 0cd82c53aa932a..a264ad3f7848b7 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -1630,7 +1630,7 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = { .best = { 1, 1, 1, 1 }, .entries = { { -1, -1, -1 }, - { 2, 1, _GET_ANEXT_r12 }, + { 3, 1, _GET_ANEXT_r13 }, { -1, -1, -1 }, { -1, -1, -1 }, }, @@ -1639,7 +1639,7 @@ const _PyUopCachingInfo _PyUop_Caching[MAX_UOP_ID+1] = { .best = { 1, 1, 1, 1 }, .entries = { { -1, -1, -1 }, - { 1, 1, _GET_AWAITABLE_r11 }, + { 3, 1, _GET_AWAITABLE_r13 }, { -1, -1, -1 }, { -1, -1, -1 }, }, @@ -4283,8 +4283,8 @@ const uint16_t _PyUop_Uncached[MAX_UOP_REGS_ID+1] = { [_MAKE_HEAP_SAFE_r33] = _MAKE_HEAP_SAFE, [_RETURN_VALUE_r11] = _RETURN_VALUE, [_GET_AITER_r11] = _GET_AITER, - [_GET_ANEXT_r12] = _GET_ANEXT, - [_GET_AWAITABLE_r11] = _GET_AWAITABLE, + [_GET_ANEXT_r13] = _GET_ANEXT, + [_GET_AWAITABLE_r13] = _GET_AWAITABLE, [_SEND_GEN_FRAME_r33] = _SEND_GEN_FRAME, [_GUARD_TOS_IS_NONE_r01] = _GUARD_TOS_IS_NONE, [_GUARD_TOS_IS_NONE_r11] = _GUARD_TOS_IS_NONE, @@ -5236,9 +5236,9 @@ const char *const _PyOpcode_uop_name[MAX_UOP_REGS_ID+1] = { [_GET_AITER] = "_GET_AITER", [_GET_AITER_r11] = "_GET_AITER_r11", [_GET_ANEXT] = "_GET_ANEXT", - [_GET_ANEXT_r12] = "_GET_ANEXT_r12", + [_GET_ANEXT_r13] = "_GET_ANEXT_r13", [_GET_AWAITABLE] = "_GET_AWAITABLE", - [_GET_AWAITABLE_r11] = "_GET_AWAITABLE_r11", + [_GET_AWAITABLE_r13] = "_GET_AWAITABLE_r13", [_GET_ITER] = "_GET_ITER", [_GET_ITER_r12] = "_GET_ITER_r12", [_GET_ITER_TRAD] = "_GET_ITER_TRAD", diff --git a/Include/listobject.h b/Include/listobject.h index e1e059b0ba7466..48720a8a7a3453 100644 --- a/Include/listobject.h +++ b/Include/listobject.h @@ -29,7 +29,7 @@ PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size); PyAPI_FUNC(Py_ssize_t) PyList_Size(PyObject *); PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *, Py_ssize_t); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(PyObject *) PyList_GetItemRef(PyObject *, Py_ssize_t); #endif PyAPI_FUNC(int) PyList_SetItem(PyObject *, Py_ssize_t, PyObject *); diff --git a/Include/longobject.h b/Include/longobject.h index 38673bc18785fa..124675a6b7768a 100644 --- a/Include/longobject.h +++ b/Include/longobject.h @@ -26,11 +26,11 @@ PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *); PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *); PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(int) PyLong_AsInt(PyObject *); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030e0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 14) PyAPI_FUNC(PyObject*) PyLong_FromInt32(int32_t value); PyAPI_FUNC(PyObject*) PyLong_FromUInt32(uint32_t value); PyAPI_FUNC(PyObject*) PyLong_FromInt64(int64_t value); @@ -104,7 +104,7 @@ PyAPI_FUNC(PyObject *) PyLong_GetInfo(void); #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT #define _Py_PARSE_PID "i" #define PyLong_FromPid PyLong_FromLong -# if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +# if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) # define PyLong_AsPid PyLong_AsInt # elif SIZEOF_INT == SIZEOF_LONG # define PyLong_AsPid PyLong_AsLong diff --git a/Include/memoryobject.h b/Include/memoryobject.h index 2c9146aa2b5b06..18064dfae44972 100644 --- a/Include/memoryobject.h +++ b/Include/memoryobject.h @@ -15,7 +15,7 @@ PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base); PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 11) PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(const Py_buffer *info); #endif PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base, diff --git a/Include/methodobject.h b/Include/methodobject.h index e6ec6421d1e59d..8d10715118987a 100644 --- a/Include/methodobject.h +++ b/Include/methodobject.h @@ -111,7 +111,7 @@ PyAPI_FUNC(PyObject *) PyCMethod_New(PyMethodDef *, PyObject *, #define METH_COEXIST 0x0040 -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030a0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) # define METH_FASTCALL 0x0080 #endif diff --git a/Include/modsupport.h b/Include/modsupport.h index 504a19d341a78a..7cd227fd262f72 100644 --- a/Include/modsupport.h +++ b/Include/modsupport.h @@ -19,14 +19,14 @@ PyAPI_FUNC(int) PyArg_UnpackTuple(PyObject *, const char *, Py_ssize_t, Py_ssize PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...); PyAPI_FUNC(PyObject *) Py_VaBuildValue(const char *, va_list); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030a0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) // Add an attribute with name 'name' and value 'obj' to the module 'mod. // On success, return 0. // On error, raise an exception and return -1. PyAPI_FUNC(int) PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value); #endif /* Py_LIMITED_API */ -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) // Similar to PyModule_AddObjectRef() but steal a reference to 'value'. PyAPI_FUNC(int) PyModule_Add(PyObject *mod, const char *name, PyObject *value); #endif /* Py_LIMITED_API */ diff --git a/Include/moduleobject.h b/Include/moduleobject.h index 3e12839ec50981..926432da2c371e 100644 --- a/Include/moduleobject.h +++ b/Include/moduleobject.h @@ -76,14 +76,14 @@ struct PyModuleDef_Slot { #endif /* New in 3.5 */ /* for Py_mod_multiple_interpreters: */ -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030c0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 12) # define Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED ((void *)0) # define Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED ((void *)1) # define Py_MOD_PER_INTERPRETER_GIL_SUPPORTED ((void *)2) #endif /* for Py_mod_gil: */ -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) # define Py_MOD_GIL_USED ((void *)0) # define Py_MOD_GIL_NOT_USED ((void *)1) #endif diff --git a/Include/object.h b/Include/object.h index 549b91ebecf4f2..58b310fc018fa3 100644 --- a/Include/object.h +++ b/Include/object.h @@ -317,7 +317,7 @@ typedef int (*initproc)(PyObject *, PyObject *, PyObject *); typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *); typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030c0000 // 3.12 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 12) // 3.12 typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames); #endif @@ -347,20 +347,20 @@ PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObje PyAPI_FUNC(PyObject *) PyType_GetModule(PyTypeObject *); PyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 11) PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *); PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(PyObject *) PyType_GetFullyQualifiedName(PyTypeObject *type); PyAPI_FUNC(PyObject *) PyType_GetModuleName(PyTypeObject *type); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 12) PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*); PyAPI_FUNC(void *) PyObject_GetTypeData(PyObject *obj, PyTypeObject *cls); PyAPI_FUNC(Py_ssize_t) PyType_GetTypeDataSize(PyTypeObject *cls); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030E0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 14) PyAPI_FUNC(int) PyType_GetBaseByToken(PyTypeObject *, void *, PyTypeObject **); #define Py_TP_USE_SPEC NULL #endif @@ -374,7 +374,7 @@ PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); static inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) { return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type); } -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), (type)) #endif @@ -403,14 +403,14 @@ PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *); PyAPI_FUNC(int) PyObject_DelAttrString(PyObject *v, const char *name); PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *); PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(int) PyObject_GetOptionalAttr(PyObject *, PyObject *, PyObject **); PyAPI_FUNC(int) PyObject_GetOptionalAttrString(PyObject *, const char *, PyObject **); #endif PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); PyAPI_FUNC(int) PyObject_DelAttr(PyObject *v, PyObject *name); PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(int) PyObject_HasAttrWithError(PyObject *, PyObject *); PyAPI_FUNC(int) PyObject_HasAttrStringWithError(PyObject *, const char *); #endif @@ -506,7 +506,7 @@ given type object has a specified feature. #define Py_TPFLAGS_BASETYPE (1UL << 10) /* Set if the type implements the vectorcall protocol (PEP 590) */ -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 12) #define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11) #ifndef Py_LIMITED_API // Backwards compatibility alias for API that was provisional in Python 3.8 @@ -598,7 +598,7 @@ given type object has a specified feature. #define Py_CONSTANT_EMPTY_BYTES 8 #define Py_CONSTANT_EMPTY_TUPLE 9 -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(PyObject*) Py_GetConstant(unsigned int constant_id); PyAPI_FUNC(PyObject*) Py_GetConstantBorrowed(unsigned int constant_id); #endif @@ -610,7 +610,7 @@ where NULL (nil) is not suitable (since NULL often means 'error'). */ PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) # define Py_None Py_GetConstantBorrowed(Py_CONSTANT_NONE) #else # define Py_None (&_Py_NoneStruct) @@ -622,7 +622,7 @@ PyAPI_FUNC(int) Py_IsNone(PyObject *x); /* Macro for returning Py_None from a function. * Only treat Py_None as immortal in the limited C API 3.12 and newer. */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 12) # define Py_RETURN_NONE return Py_NewRef(Py_None) #else # define Py_RETURN_NONE return Py_None @@ -634,7 +634,7 @@ not implemented for a given type combination. */ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) # define Py_NotImplemented Py_GetConstantBorrowed(Py_CONSTANT_NOT_IMPLEMENTED) #else # define Py_NotImplemented (&_Py_NotImplementedStruct) @@ -642,7 +642,7 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ /* Macro for returning Py_NotImplemented from a function. Only treat * Py_NotImplemented as immortal in the limited C API 3.12 and newer. */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 12) # define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented) #else # define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented @@ -656,7 +656,7 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ #define Py_GT 4 #define Py_GE 5 -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) /* Result of calling PyIter_Send */ typedef enum { PYGEN_RETURN = 0, @@ -762,7 +762,7 @@ PyType_HasFeature(PyTypeObject *type, unsigned long feature) static inline int PyType_Check(PyObject *op) { return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS); } -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define PyType_Check(op) PyType_Check(_PyObject_CAST(op)) #endif @@ -772,15 +772,15 @@ static inline int PyType_Check(PyObject *op) { static inline int PyType_CheckExact(PyObject *op) { return Py_IS_TYPE(op, &PyType_Type); } -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op)) #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(PyObject *) PyType_GetModuleByDef(PyTypeObject *, PyModuleDef *); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030e0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 14) PyAPI_FUNC(int) PyType_Freeze(PyTypeObject *type); #endif diff --git a/Include/objimpl.h b/Include/objimpl.h index 56472a72e42d34..54d5ba500ddb49 100644 --- a/Include/objimpl.h +++ b/Include/objimpl.h @@ -160,8 +160,6 @@ PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t); #define PyObject_GC_Resize(type, op, n) \ ( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) ) - - PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *); PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t); diff --git a/Include/patchlevel.h b/Include/patchlevel.h index c6c7dde1797e94..96787626d91fe4 100644 --- a/Include/patchlevel.h +++ b/Include/patchlevel.h @@ -24,10 +24,10 @@ #define PY_MINOR_VERSION 16 #define PY_MICRO_VERSION 0 #define PY_RELEASE_LEVEL PY_RELEASE_LEVEL_ALPHA -#define PY_RELEASE_SERIAL 1 +#define PY_RELEASE_SERIAL 2 /* Version as a string */ -#define PY_VERSION "3.16.0a1+dev" +#define PY_VERSION "3.16.0a2+dev" /*--end constants--*/ @@ -37,6 +37,7 @@ (((Z) & 0xff) << 8) | \ (((LEVEL) & 0xf) << 4) | \ (((SERIAL) & 0xf) << 0)) +#define _Py_PACK_VERSION(X, Y) _Py_PACK_FULL_VERSION(X, Y, 0, 0, 0) /* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2. Use this for numeric comparisons, e.g. #if PY_VERSION_HEX >= ... */ diff --git a/Include/pyabi.h b/Include/pyabi.h index 21a6ab0c1ee6ea..cb8fb9f1e49aec 100644 --- a/Include/pyabi.h +++ b/Include/pyabi.h @@ -39,7 +39,7 @@ # undef Py_LIMITED_API # define Py_LIMITED_API 0x03020000 #endif -#if defined(Py_TARGET_ABI3T) && Py_TARGET_ABI3T+0 < 0x030f0000 +#if defined(Py_TARGET_ABI3T) && Py_TARGET_ABI3T+0 < _Py_PACK_VERSION(3, 15) # error "Py_TARGET_ABI3T must be 0x030f0000 (3.15) or above" #endif diff --git a/Include/pybuffer.h b/Include/pybuffer.h index 6371b9b483777b..7624e967302c13 100644 --- a/Include/pybuffer.h +++ b/Include/pybuffer.h @@ -6,7 +6,7 @@ extern "C" { #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 11) /* === New Buffer API ============================================ * Limited API and stable ABI since Python 3.11 diff --git a/Include/pyerrors.h b/Include/pyerrors.h index cfabbc5fe8d5da..0f93d2791fb575 100644 --- a/Include/pyerrors.h +++ b/Include/pyerrors.h @@ -18,7 +18,7 @@ PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **); PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *); PyAPI_FUNC(PyObject *) PyErr_GetRaisedException(void); PyAPI_FUNC(void) PyErr_SetRaisedException(PyObject *); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 11) PyAPI_FUNC(PyObject*) PyErr_GetHandledException(void); PyAPI_FUNC(void) PyErr_SetHandledException(PyObject *); #endif @@ -239,7 +239,7 @@ PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *); /* In signalmodule.c */ PyAPI_FUNC(int) PyErr_CheckSignals(void); PyAPI_FUNC(void) PyErr_SetInterrupt(void); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) PyAPI_FUNC(int) PyErr_SetInterruptEx(int signum); #endif diff --git a/Include/pylifecycle.h b/Include/pylifecycle.h index f11ac4d2a9f10e..c1501c6cb28e23 100644 --- a/Include/pylifecycle.h +++ b/Include/pylifecycle.h @@ -49,11 +49,11 @@ typedef void (*PyOS_sighandler_t)(int); PyAPI_FUNC(PyOS_sighandler_t) PyOS_getsig(int); PyAPI_FUNC(PyOS_sighandler_t) PyOS_setsig(int, PyOS_sighandler_t); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 11) PyAPI_DATA(const unsigned long) Py_Version; #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(int) Py_IsFinalizing(void); #endif diff --git a/Include/pymacro.h b/Include/pymacro.h index f08ed1a1ecf0f9..2444a73c7f12e0 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -1,13 +1,6 @@ #ifndef Py_PYMACRO_H #define Py_PYMACRO_H -// _Py_STDC_VERSION: the version of the C standard. -// gh-160208: The C++ standard allows __STDC_VERSION__ to be defined to an -// arbitrary value, which means we can't use __STDC_VERSION__ on C++. -#if !defined(__cplusplus) && defined(__STDC_VERSION__) -# define _Py_STDC_VERSION __STDC_VERSION__ -#endif - // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are // defined, disables C11 support and does not define // the static_assert() macro. @@ -318,7 +311,6 @@ #define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0) // Version helpers. These are primarily macros, but have exported equivalents. -#define _Py_PACK_VERSION(X, Y) _Py_PACK_FULL_VERSION(X, Y, 0, 0, 0) #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 14) PyAPI_FUNC(uint32_t) Py_PACK_FULL_VERSION(int x, int y, int z, int level, int serial); PyAPI_FUNC(uint32_t) Py_PACK_VERSION(int x, int y); diff --git a/Include/pymem.h b/Include/pymem.h index a80da99e1dd7fc..6162148a996d7d 100644 --- a/Include/pymem.h +++ b/Include/pymem.h @@ -87,7 +87,7 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr); #define PyMem_DEL(p) PyMem_Free((p)) -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) // Memory allocator which doesn't require the GIL to be held. // Usually, it's just a thin wrapper to functions of the standard C library: // malloc(), calloc(), realloc() and free(). The difference is that diff --git a/Include/pyport.h b/Include/pyport.h index c6cc47f7a14103..869829cbb50dd4 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -8,6 +8,13 @@ # error "Python's source code assumes C's unsigned char is an 8-bit type" #endif +// _Py_STDC_VERSION: the version of the C standard. +// gh-160208: The C++ standard allows __STDC_VERSION__ to be defined to an +// arbitrary value, which means we can't use __STDC_VERSION__ on C++. +#if defined(__STDC_VERSION__) && !defined(__cplusplus) +# define _Py_STDC_VERSION __STDC_VERSION__ +#endif + // Preprocessor check for a builtin preprocessor function. Always return 0 // if __has_builtin() macro is not defined. @@ -190,7 +197,7 @@ typedef Py_ssize_t Py_ssize_clean_t; #endif // Soft deprecated since Python 3.14, use memcpy() instead. -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define Py_MEMCPY memcpy #endif @@ -306,16 +313,15 @@ extern "C" { * Usage: * int _Py_HOT_FUNCTION x(void) { return 3; } * - * Issue #28618: This attribute must not be abused, otherwise it can have a + * Issue gh-72804: This attribute must not be abused, otherwise it can have a * negative effect on performance. Only the functions were Python spend most of * its time must use it. Use a profiler when running performance benchmark * suite to find these functions. */ -#if defined(__GNUC__) \ - && ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)) -#define _Py_HOT_FUNCTION __attribute__((hot)) +#if _Py__has_attribute(hot) +# define _Py_HOT_FUNCTION __attribute__((hot)) #else -#define _Py_HOT_FUNCTION +# define _Py_HOT_FUNCTION #endif // Ask the compiler to always inline a static inline function. The compiler can @@ -491,7 +497,7 @@ extern "C" { #ifdef WITH_THREAD # ifdef thread_local # define _Py_thread_local thread_local -# elif _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__) +# elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__) # define _Py_thread_local _Thread_local # elif defined(_MSC_VER) /* AKA NT_THREADS */ # define _Py_thread_local __declspec(thread) diff --git a/Include/pythonrun.h b/Include/pythonrun.h index 92b50aa807bf70..af706c91387aea 100644 --- a/Include/pythonrun.h +++ b/Include/pythonrun.h @@ -13,7 +13,7 @@ PyAPI_FUNC(void) PyErr_Print(void); PyAPI_FUNC(void) PyErr_PrintEx(int); PyAPI_FUNC(void) PyErr_Display(PyObject *, PyObject *, PyObject *); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 12) PyAPI_FUNC(void) PyErr_DisplayException(PyObject *); #endif diff --git a/Include/refcount.h b/Include/refcount.h index 9b681f6a254a42..4804caec2bc845 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -96,7 +96,7 @@ check by comparing the reference count field to the minimum immortality refcount // Py_REFCNT() implementation for the stable ABI PyAPI_FUNC(Py_ssize_t) Py_REFCNT(PyObject *ob); -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030e0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 14) // Stable ABI implements Py_REFCNT() as a function call // on limited C API version 3.14 and newer, and on abi3t. #elif defined(_Py_OPAQUE_PYOBJECT) @@ -115,7 +115,7 @@ PyAPI_FUNC(Py_ssize_t) Py_REFCNT(PyObject *ob); Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT); #endif } - #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 + #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define Py_REFCNT(ob) _Py_REFCNT(_PyObject_CAST(ob)) #else # define Py_REFCNT(ob) _Py_REFCNT(ob) @@ -153,7 +153,7 @@ PyAPI_FUNC(void) _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt); static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { assert(refcnt >= 0); -#if (defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030d0000) \ +#if (defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13)) \ || defined(_Py_OPAQUE_PYOBJECT) // Stable ABI implements Py_SET_REFCNT() as a function call // on limited C API version 3.13 and newer, and abi3t. @@ -197,7 +197,7 @@ static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { #endif // Py_GIL_DISABLED #endif // Py_LIMITED_API } -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt)) #endif @@ -254,13 +254,12 @@ PyAPI_FUNC(void) _Py_DecRef(PyObject *); static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) { -#if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \ - || defined(_Py_OPAQUE_PYOBJECT) - // Stable ABI implements Py_INCREF() as a function call on limited C API - // version 3.12 and newer, abi3t, and on Python built in debug mode. - // _Py_IncRef() was added to Python 3.10.0a7, use Py_IncRef() on older versions. - // Py_IncRef() accepts NULL whereas _Py_IncRef() doesn't. -# if Py_LIMITED_API+0 >= 0x030a00A7 +#if defined(Py_LIMITED_API) || defined(_Py_OPAQUE_PYOBJECT) + // gh-158401: The stable ABI now implements Py_INCREF() as a function + // call on all limited C API versions. _Py_IncRef() was added to Python + // 3.10.0a7, use Py_IncRef() on older versions. Py_IncRef() accepts NULL, + // whereas _Py_IncRef() doesn't. +# if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) _Py_IncRef(op); # else Py_IncRef(op); @@ -306,7 +305,7 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) #endif #endif } -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op)) #endif @@ -324,14 +323,13 @@ PyAPI_FUNC(void) _Py_MergeZeroLocalRefcount(PyObject *); #endif // Py_GIL_DISABLED #endif // Py_LIMITED_API -#if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \ - || defined(_Py_OPAQUE_PYOBJECT) -// Stable ABI implements Py_DECREF() as a function call on limited C API -// version 3.12 and newer, abi3t, and on Python built in debug mode. -// _Py_DecRef() was added to Python 3.10.0a7, use Py_DecRef() on older versions. -// Py_DecRef() accepts NULL whereas _Py_DecRef() doesn't. +#if defined(Py_LIMITED_API) || defined(_Py_OPAQUE_PYOBJECT) +// gh-158401: The stable ABI now implements Py_DECREF() as a function call +// on all limited C API versions. _Py_DecRef() was added to Python 3.10.0a7, +// use Py_DecRef() on older versions. Py_DecRef() accepts NULL, whereas +// _Py_DecRef() doesn't. static inline void Py_DECREF(PyObject *op) { -# if Py_LIMITED_API+0 >= 0x030a00A7 +# if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) _Py_DecRef(op); # else Py_DecRef(op); @@ -607,7 +605,7 @@ static inline void Py_XINCREF(PyObject *op) Py_INCREF(op); } } -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define Py_XINCREF(op) Py_XINCREF(_PyObject_CAST(op)) #endif @@ -617,7 +615,7 @@ static inline void Py_XDECREF(PyObject *op) Py_DECREF(op); } } -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define Py_XDECREF(op) Py_XDECREF(_PyObject_CAST(op)) #endif @@ -643,7 +641,7 @@ static inline PyObject* _Py_XNewRef(PyObject *obj) // Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI. // Names overridden with macros by static inline functions for best // performances. -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) # define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj)) # define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj)) #else diff --git a/Include/sliceobject.h b/Include/sliceobject.h index 9d6a16da95fe2f..0c1141ac2b7c92 100644 --- a/Include/sliceobject.h +++ b/Include/sliceobject.h @@ -8,7 +8,7 @@ extern "C" { PyAPI_DATA(PyObject) _Py_EllipsisObject; /* Don't use this directly */ -#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000 +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) # define Py_Ellipsis Py_GetConstantBorrowed(Py_CONSTANT_ELLIPSIS) #else # define Py_Ellipsis (&_Py_EllipsisObject) diff --git a/Include/sysmodule.h b/Include/sysmodule.h index fc6da6f4c797b3..85521a6546dde9 100644 --- a/Include/sysmodule.h +++ b/Include/sysmodule.h @@ -4,7 +4,7 @@ extern "C" { #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030f0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15) PyAPI_FUNC(PyObject *) PySys_GetAttr(PyObject *); PyAPI_FUNC(PyObject *) PySys_GetAttrString(const char *); PyAPI_FUNC(int) PySys_GetOptionalAttr(PyObject *, PyObject **); diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h index fbced4896bd014..301b841944c744 100644 --- a/Include/unicodeobject.h +++ b/Include/unicodeobject.h @@ -397,7 +397,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String( when the unicodeobject is deallocated. */ -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize( PyObject *unicode, Py_ssize_t *size); @@ -900,7 +900,7 @@ PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString( const char *right /* ASCII-encoded string */ ); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) /* Compare a Unicode object with UTF-8 encoded C string. Return 1 if they are equal, or 0 otherwise. This function does not raise exceptions. */ @@ -909,7 +909,7 @@ PyAPI_FUNC(int) PyUnicode_EqualToUTF8(PyObject *, const char *); PyAPI_FUNC(int) PyUnicode_EqualToUTF8AndSize(PyObject *, const char *, Py_ssize_t); #endif -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030e0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 14) PyAPI_FUNC(int) PyUnicode_Equal(PyObject *str1, PyObject *str2); #endif diff --git a/Include/weakrefobject.h b/Include/weakrefobject.h index 17fac62961c0fb..421995143482ee 100644 --- a/Include/weakrefobject.h +++ b/Include/weakrefobject.h @@ -28,7 +28,7 @@ PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob, PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob, PyObject *callback); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000 +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13) PyAPI_FUNC(int) PyWeakref_GetRef(PyObject *ref, PyObject **pobj); #endif diff --git a/InternalDocs/README.md b/InternalDocs/README.md index 5de575b0f44952..4258088a73f81c 100644 --- a/InternalDocs/README.md +++ b/InternalDocs/README.md @@ -65,3 +65,5 @@ Modules --- - [asyncio](asyncio.md) + +- [fractions](fractions.md) diff --git a/InternalDocs/fractions.md b/InternalDocs/fractions.md new file mode 100644 index 00000000000000..a01950f5778648 --- /dev/null +++ b/InternalDocs/fractions.md @@ -0,0 +1,365 @@ +fractions +========= + +This document describes the working and implementation details of the +[`fractions`](https://docs.python.org/3/library/fractions.html) module, +providing some proofs for algorithms used. + + +# Proof of correctness of the `limit_denominator` algorithm + +## The problem being solved + +The `limit_denominator` algorithm takes a fraction $m/n$ and a positive integer +$\ell$, and aims to return a "best" approximation to $m/n$ from amongst all +fractions $r/s$ with denominator bounded by $\ell$. To make "best" precise, +let's establish some conventions and terminology: + +- All fractions are assumed to be in lowest terms, with positive denominator. +- A **candidate approximation** to $m / n$ with denominator bounded by $\ell$ + (or just **candidate** for short) is a fraction $r/s$ with $s \le \ell$. +- Given two candidates $r/s$ and $t/u$, say that $r/s$ is **better** than $t/u$ + if _either_ + - $r/s$ is closer to $m/n$ than $t/u$ is, _or_ + - $r/s$ and $t/u$ are equidistant from $m/n$, and $s \le u$. +- A candidate $r/s$ is a **best approximation** if it's better than all (other) + candidates. + +The goal of this exposition is to convince the reader that Python's +implementation of `limit_denominator` always returns a best approximation for +the given inputs. + +Some notes: + +- There's a tension with the English language: our "better" relation is + reflexive (a candidate $r/s$ is better than itself), while the word "better" + suggests an irreflexive relation. But other more accurate wordings ("no worse + than", "better or equally good", "at least as good") are more cumbersome, so + we'll live with the tension. +- We'll show below that outside one corner case, there's a _unique_ best + approximation for a given set of arguments. That corner case is the case + where $m/n$ is exactly halfway between two consecutive integers and $\ell = 1$; + we'll call this the ambiguous case, and we'll return to it below. +- For an example of the tie-break condition for "better", consider the case + where $m/n = 99/140 = 0.707142...$ and $\ell = 10$. Then $7/10 = 0.7$ and + $5/7 = 0.714285...$ are candidate approximations, they're equidistant from the + target fraction $99/140$, and they're each closer than any other candidate - + any fraction that's closer must be strictly between $7/10$ and $5/7$, and all + fractions in the open interval $(7/10, 5/7)$ have denominator larger than $10$. + Since $5/7$ has smaller denominator than $7/10$, our definition says that $5/7$ + is better than $7/10$ (but not vice versa). So $5/7$ is the unique best + approximation. + +## Simplifying the code + +Here's the standard library implementation of `Fraction.limit_denominator`, shorn of +comments and docstring details, but otherwise unaltered. + +```python +class Fraction(numbers.Rational): + + # other methods elided ... + + def limit_denominator(self, max_denominator=1000000): + """Closest Fraction to self with denominator at most max_denominator.""" + if max_denominator < 1: + raise ValueError("max_denominator should be at least 1") + if self._denominator <= max_denominator: + return Fraction(self) + + p0, q0, p1, q1 = 0, 1, 1, 0 + n, d = self._numerator, self._denominator + while True: + a = n//d + q2 = q0+a*q1 + if q2 > max_denominator: + break + p0, q0, p1, q1 = p1, q1, p0+a*p1, q2 + n, d = d, n-a*d + k = (max_denominator-q0)//q1 + + if 2*d*(q0+k*q1) <= self._denominator: + return Fraction._from_coprime_ints(p1, q1) + else: + return Fraction._from_coprime_ints(p0+k*p1, q0+k*q1) +``` + +Instead of proving correctness of the above code directly, we'll prove correctness +of an equivalent simplified version. Here's that version: + +```python +def limit_denominator(m: int, n: int, l: int) -> tuple[int, int]: + """ + Given a fraction m/n and a positive integer l, return integers r and s such + that r/s is the closest fraction to m/n with denominator bounded by l. + + m/n need not be in lowest terms, but n must be positive. + + On return, 0 < s <= l and gcd(r, s) = 1. + """ + if l < 1: + raise ValueError("max_denominator should be at least 1") + + a, b, p, q, r, s, v = n, m % n, 1, 0, m // n, 1, 1 + while 0 < b and q + a // b * s <= l: + a, b, p, q, r, s, v = b, a % b, r, s, p + a // b * r, q + a // b * s, -v + k = (l - q) // s + t, u = p + k * r, q + k * s + return (r, s) if 2 * b * u <= n else (t, u) +``` + +The differences with the standard library version are mostly cosmetic: there +are some de-optimizations (like computing `a//b` several times); we deal purely +with integers to avoid the `Fraction` unpacking and repacking; some variables +are renamed, and we've removed the fast path where a `Fraction` is returned +unaltered if its denominator is _already_ at most `l`. The most significant +difference is that we've unrolled the first loop-and-a-half of the `while` +loop, with the side-effect of shifting the `break` condition to the top of the +loop, where it becomes the loop guard. There's also an extra variable `v`, +which alternates between values `1` and `-1`. It's not needed for computing the +final result, but we'll be making use of it in the statements and proofs below. +Finally, the standard library code has no positivity check in the `break` +condition to match the `0 < b` check in the loop guard - that check proves to +be unnecessary if the fast path is present. + +## Proof overview + +The main loop of `limit_denominator` is a variant of the extended Euclidean +algorithm applied to $m$ and $n$, with an extra termination condition designed +to make sure that denominators don't go out of bounds - the moment a full +Euclidean step would give us a denominator larger than $\ell$, we halt the +loop. After the loop we take a partial Euclidean step, advancing exactly as far +as possible without violating the denominator bound. + +The main loop, together with that partial Euclidean step, produces candidate +approximations $r/s$ and $t/u$ which bracket the target fraction $m/n$, and +whose denominators are no greater than the limit $\ell$. With just two +additional pieces of information, namely that $ts - ru = ±1$ and that +$\ell < s + u$, we can deduce that _all_ fractions strictly between $r/s$ and +$t/u$ have denominator greater than $\ell$, hence that either $r/s$ or $t/u$ +is a best approximation. The final line of the code then determines which of +these two fractions is better and returns it. + +In more detail, we'll show in the sections below that after exiting the `while` +loop and defining $t$ and $u$, the following five properties hold: + +- $(ts - ru)v = 1$ +- $0 < s \le \ell$ +- $0 < u \le \ell$ +- $\ell < s + u$ +- $r/s \le m/n < t/u$ if $v=1$, and $t/u < m/n \le r/s$ if $v = -1$ + +Note that the first three properties make $r/s$ and $t/u$ well-defined as +fractions, and moreover both are candidates: reduced, with a positive +denominator bounded by $\ell$. We now show that the five properties imply that +any fraction $y/z$ strictly between $r/s$ and $t/u$ has $z > \ell$. We have: + +$$z = (ts - ru)vz = (tz - yu)vs + (ys - rz)vu.$$ + +Now $v$ is either $1$ or $-1$. If $v = 1$ then $r/s < t/u$, so $r/s < y/z < t/u$. +So $ys - rz$ and $tz - yu$ are positive, hence $(ys - rz)v$ and $(tz - yu)v$ +are positive integers. Conversely if $v = -1$ then $t/u < r/s$, so +$t/u < y/z < r/s$ and $ys - rz$ and $tz - yu$ are negative. So again +$(ys - rz)v$ and $(tz - yu)v$ are positive integers. So in either case, +$(tz - yu)v \ge 1$ and $(ys - rz)v \ge 1$, hence +$$z = [(tz-yu)v]s + [(ys - rz)v]u \ge s + u > \ell.$$ + +At this point $r/s$ and $t/u$ are candidates, there are no candidates strictly +between $r/s$ and $t/u$, while $m/n$ _does_ lie between $r/s$ and $t/u$ (and +may be equal to $r/s$, but not to $t/u$). It follows that $r/s$ and $t/u$ are +the only possible best approximations to $m/n$, and that either $r/s$ or $t/u$ +(or possibly both) is a best approximation. The code need only determine which +of $r/s$ and $t/u$ is better than the other. The details are given in the +"Choosing the best approximation" section below, while the proofs of the five +properties that we used above are in the "Details" sections below. + +## Details: loop invariants + +Both the initial state and the state after every iteration of the `while` loop +satisfy the following six invariants: + +- $(ps - rq)v = 1$ +- $(pn - mq)v = a$ +- $(ms - rn)v = b$ +- $0 \le b < a$ +- $0 \le q$ +- $0 < s \le \ell$ + +These are all easily proved by induction: they hold for the initial state by +inspection, and it's easy to see that the way that the variables are updated +within the `while` loop keeps all of these properties invariant. Note that at +the end of an iteration of the `while` loop, the condition $s \le \ell$ is +exactly the condition that the loop was entered (that +$q + \lfloor a / b \rfloor s \le \ell$). + +That $v$ is either $1$ or $-1$ follows from the first invariant. + +## Details: loop termination + +For a complete proof of correctness we need to establish that our `while` loop +does not run indefinitely - it must terminate after a finite number of +iterations. But this follows immediately from the observation that the value of +$a$ remains positive and that it strictly decreases at every iteration: at each +iteration the new value of $a$ is the old value of $b$, which from our loop +invariant $b < a$ is smaller than the old value of $a$. + +## Details: after the loop + +Here we prove that the five properties mentioned earlier hold. + +One of our two possible return values, $r/s$, comes directly from the loop. We +construct the second candidate $t/u$ after the loop: $t = p + kr$ and +$u = q + ks$, with $k = \lfloor (\ell - q) / s \rfloor$. Think of $t/u$ as +the result of taking a _partial_ Euclidean step, as a replacement for the +full Euclidean step which would have taken the denominator out of bounds. + +That $0 < s \le \ell$ follows directly from the loop invariants. From the +definition of $k$ we have $ks \le \ell - q < s + ks$, hence +$ks + q \le \ell < s + ks + q$, that is +$$u \le \ell < s + u.$$ +That $u$ is positive follows from $s \le \ell < s + u$. So $t/u$ is also a +valid candidate approximation. + +From construction of $t$ and $u$ and the relation $(ps - rq)v = 1$ we get an +analogous relation for $r/s$ and $t/u$: +$$(ts - ru)v = 1.$$ + +We also define an integer $c = a - kb$, the result of taking the partial +Euclidean step that gives $t$ and $u$. It's immediate from the definitions +and the earlier loop invariants that: +$$(tn - mu)v = c$$ +and +$$bt + cr = m$$ +and finally, +$$bu + cs = n.$$ + +We next prove that $b \le c$. The intuition here is that a full Euclidean step +would have given us the nonnegative remainder $a \bmod b$; since we took only a +partial step instead, our remainder $c = a - kb$ is the full remainder +$a \bmod b$ plus at least one copy of $b$. Concretely, there are two cases. +If $0 < b$ then from the failed loop guard $\ell < q + \lfloor a /b \rfloor s$. +But $q + ks = u \le \ell$, so $k < \lfloor a/b \rfloor$, from which +$c = a - kb \ge a - b\lfloor a/b \rfloor + b \ge b$. Otherwise $b = 0$ +and $c = a - kb = a$, and the result is immediate from the positivity of $a$. + +Finally, note that $0 < c$. If $0 < b$ this follows from $b \le c$. If $b = 0$ +it follows from the positivity of $a$ together with $c = a - kb = a$. + +We've now established four of the five properties that were used in the proof +overview above. The only property that we're missing is that $m/n$ lies between +$r/s$ and $t/u$ (and can be equal to $r/s$, but not to $t/u$). But this follows +from the facts that $0 \le b = (ms - rn)v$ and that $0 < c = (tn - mu)v$: +if $v = 1$ then these give $r/s \le m/n < t/u$, while if $v = -1$ then +$t/u < m/n \le r/s$. + +## Choosing the best approximation + +By the time we get to the final line of `limit_denominator`, we know that +either $r/s$ or $t/u$ is our best approximation; we need to choose the one +that's closer to $m/n$. So we want to compare $|m/n - r/s|$ with $|t/u - m/n|$. +Scaling up by $nsu$, we need to compare $|(ms - rn)u|$ with $|(tn - mu)s|$. +Since $v$ is either plus or minus one, we can insert a factor of $v$ without +affecting the absolute value, so we're now comparing $|(ms-rn)vu|$ with +$|(tn - mu)vs|$. But from above, $(ms-rn)v = b$ is nonnegative, as is +$(tn - mu)v = c$. So the comparison we want to make is +$$bu \le cs.$$ +Adding $bu$ to both sides and using the fact that $bu + cs = n$, this is +equivalent to the check +$$2bu \le n,$$ +which is the check that we use in the code. + +In the case that $r/s$ and $t/u$ are equidistant from $m/n$, we have $bu = cs$ +(or equivalently $2bu = n$), and $r/s$ is returned. Since $b \le c$ and $c$ is +positive, we must have $s = (b/c) u \le u$, so in this case $r/s$ is better +than $t/u$ by the tie-break condition, so $r/s$ is a best approximation. + +## Uniqueness, and the ambiguous case + +From earlier, any best approximation must be either $r/s$ or $t/u$, and at +least one of these _is_ a best approximation. Outside one corner case _exactly_ +one of these is a best approximation, so there's a unique best approximation to +$m/n$ with denominator bounded by $\ell$, and `limit_denominator` returns that +best approximation. In this section we explore the corner case where _both_ +$r/s$ and $t/u$ are best approximations, and we describe what +`limit_denominator` returns in that situation. + +So suppose that both $r/s$ and $t/u$ are best approximations. Then each is +better than the other, so from the definition of _better_ (including the +tie-break condition) they must be equidistant from $m/n$, and we must also have +$s \le u$ and $u \le s$, hence $s = u$. From $(ts - ru)v = 1$ and $s = u$ we +have $s = u = 1$, so $t = r \pm 1$ and the candidates $r/s$ and $t/u$ are +consecutive integers (in some order). Since $m/n$ is equidistant from both, +it's a half-integer - a value of the form $w + 1/2$ for some integer $w$. +Moreover, since $\ell < s + u$ and $\ell$ is positive, we must have $\ell = 1$. + +Conversely, if $m / n$ is a half-integer and $\ell = 1$ then our candidates +(denominator bounded by $1$) are simply integers, so it's clear that there are +exactly _two_ best approximations to $m / n$, namely the floor and ceiling of +$m / n$. + +We call the case where $m / n$ is a half-integer and $\ell = 1$ the +**ambiguous** case. The above argument shows that uniqueness fails precisely in +the ambiguous case: if $\ell > 1$ or $m / n$ is _not_ a half-integer, then +there's a _unique_ best approximation to $m / n$ with denominator bounded by +$\ell$. + +We now prove that in the ambiguous case, the `limit_denominator` algorithm +returns the floor of $m / n$ rather than the ceiling. So suppose again that +both $r/s$ and $t/u$ are best approximations. They are then equidistant from +$m/n$, so as shown in the previous section $2bu = n$ and `limit_denominator` +returns $r/s$. We need to show that $r/s$ is the lower of the two candidates; +that is, that $r/s < t/u$, or equivalently that $v = 1$. + +We first claim that $q = 0$. From the loop invariants it follows that +$$as + bq = (pn - mq)vs + (ms - rn)vq = (ps - rq)vn = n$$ +so we have +$$as + bq = 2bu.$$ +Subtracting $bs$ from both sides and rearranging gives +$$(a - b)s = b(2u - q - s).$$ +But $b < a$ and $0 < s$ from the loop invariants, so the left-hand side is +positive. Since $b$ is nonnegative, $2u - q - s$ must also be positive. As +before, we also know that in this ambiguous case $s = u = 1$. So +$2u - q - s = 1 - q$ and $1 - q$ is positive, so since $q$ is nonnegative, +$q = 0$ is the only possibility. Looking back at the loop, $q$ is nonzero after +every iteration of the loop body: the only time it's zero is when the loop hasn't +run. And in that initial loop state we do indeed have $v = 1$, which is what we +needed. + +## Optimizations: removing the $0 < b$ check and avoiding unnecessary `gcd`s + +The standard library version of the code has a fast path: if the denominator of +the target fraction is _already_ bounded by $\ell$, the code simply returns (a +copy of) the target fraction immediately - a moment's thought should convince +the reader that $m/n$ is the unique best approximation in this situation. + +With the fast path in place, we can simplify the loop guard. Suppose that when +the loop guard is evaluated, the $0 < b$ part of the condition is false. We +claim that in that case, $n$ is at most $\ell$. For if $0 < b$ is false, then +$b = 0$ (we know that $b$ is always nonnegative), the loop exits immediately, +we construct $t/u$ as before, and the post-loop equations $bt + cr = m$ and +$bu + cs = n$ give $cr = m$ and $cs = n$. So $c$ is a common factor of $m$ +and $n$. But since $m$ and $n$ are relatively prime and $c$ is positive, +$c = 1$, $m = r$ and $n = s$. But $s \le \ell$, so $n \le \ell$. + +So if we've _already_ excluded the case that $n \le \ell$ via the fast path, +then the $0 < b$ condition at the top of the loop is always true, and we can +drop it. The standard library version of the code does exactly that. + +It's worth noting that the simplified listing _does_ require the extra $0 < b$ +check, for two reasons. First, the simplified listing does not require that its +inputs $m$ and $n$ be relatively prime (though it does require that $n$ is +positive). Second, it lacks the fast path. Removing the $0 < b$ check would +require both adding a fast path _and_ requiring that $m$ and $n$ be relatively +prime. + +One other optimization in the standard library version: since we already know +that $r/s$ and $t/u$ are in lowest terms, we have no need to construct the +returned `Fraction` through the default `Fraction` constructor, which would +require a potentially expensive `gcd` computation. We can use the +`Fraction._from_coprime_ints` constructor instead. + +## Formal proof + +In addition to the above proof, a formal, computer-verified proof +of correctness (using Lean) of the algorithm can be found here: +https://github.com/mdickinson/snippets/tree/1f47f8cc33185c646b679647e88cb24f29fd6a4e/proofs/limit_denominator diff --git a/InternalDocs/interpreter.md b/InternalDocs/interpreter.md index fa89996aa65429..987ac7bbed181b 100644 --- a/InternalDocs/interpreter.md +++ b/InternalDocs/interpreter.md @@ -525,8 +525,9 @@ There are three different types of interpreters to choose from based on compiler [preserve_none](https://clang.llvm.org/docs/AttributeReference.html#preserve-none) calling convention between the small C functions that implement individual Python opcodes. - Not all compilers support these and if they do not all targets might be supported (for example, - MSVC currently only supports x64 and only in optimized builds). + Support depends on the compiler and target architecture. With MSVC, the + tail-calling interpreter is available only in optimized builds. The minimum + MSVC version is 14.50 for x64 and 14.52 for ARM64. In addition, compilers must do [escape analysis](https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-musttail) of the lifetimes of automatic variables, function parameters, and temporaries to ensure proper tail-calls. They diff --git a/InternalDocs/profiling_binary_format.md b/InternalDocs/profiling_binary_format.md index a3d86b1a4ab569..f38c8dd3306448 100644 --- a/InternalDocs/profiling_binary_format.md +++ b/InternalDocs/profiling_binary_format.md @@ -41,7 +41,7 @@ The file consists of five required sections and one optional extension: | Header | 64 bytes (fixed) +------------------+ Offset 64 | | -| Sample Data | Variable size (optionally compressed) +| Sample Data | Blocks of columns (optionally compressed) | | +------------------+ string_table_offset | String Table | Variable size @@ -60,8 +60,8 @@ so these tables must be built incrementally and written at the end. The header comes first so readers can quickly validate the file and locate the metadata tables. The sample data follows immediately, allowing the writer -to stream samples directly to disk (or through a compression stream) without -buffering the entire dataset in memory. +to write each block of samples as soon as it is full without buffering the +entire dataset in memory. The string and frame tables are placed after sample data because they grow as new unique entries are discovered during profiling. By deferring their @@ -119,87 +119,142 @@ network streams. ## Sample Data -Sample data begins at offset 64 and extends to `string_table_offset`. Samples -use delta compression to minimize redundancy when consecutive samples from the -same thread have identical or similar call stacks. +Sample data begins at offset 64 and extends to `string_table_offset`. Each +block has a header followed by its columns. The columns together take at most +1 MiB on disk and at most 1 MiB when decoded. Within a block, samples use +delta compression to minimize redundancy when consecutive samples from the +same thread have identical or similar call stacks. Once the reader loads the +string and frame tables, it can decode each block on its own. + +### Block Format + +A block stores values of the same kind together, in six columns: + +``` ++------------------------+ +| Block header | 21 to 88 bytes ++------------------------+ +| STREAM_TABLE | (thread_id, interpreter_id) pairs ++------------------------+ +| SAMPLE_STREAM_IDS | One stream index per sample ++------------------------+ +| TIMESTAMP_RESIDUALS | One timestamp residual per sample ++------------------------+ +| STATUS_BITMAPS | One bitmap per varying status flag ++------------------------+ +| STACK_INSTRUCTIONS | Stack encodings ++------------------------+ +| FRAME_IDS | Frame table indices ++------------------------+ +``` + +**Block header:** +``` ++---------------------+-----------+----------------------------------------+ +| magic | 4 bytes | "TBLK" | +| sample_count | varint | Number of samples in this block | +| base_timestamp | varint | Timestamp of the first sample | +| frame_id_width | 1 byte | Bytes per frame index (1 to 4) | +| common_status_flags | 1 byte | Status bits set in every sample | +| status_bitmap_flags | 1 byte | Status bits stored as bitmaps | +| columns | varies | One column descriptor per column | ++---------------------+-----------+----------------------------------------+ +``` + +**Column descriptor:** +``` ++---------------------+-----------+----------------------------------------+ +| decoded_size | varint | Size of the decoded column | +| encoding | 1 byte | 0=raw, 1=zstd, 2=constant | +| stored_size | varint | Compressed size (zstd only) | ++---------------------+-----------+----------------------------------------+ +``` + +A raw column is stored as is, and a constant column is a single byte repeated +`decoded_size` times, so neither needs a stored size. The number of streams +and frame indices in a block follows from the decoded sizes of +`STREAM_TABLE` and `FRAME_IDS`. + +A block holds at most 4,096 streams. There is no separate limit on the number +of samples: the stream index and timestamp columns need at least one byte per +sample, so the 1 MiB limit already bounds it. ### Stack Encoding Types -Each sample record begins with thread identification, then an encoding byte: +Each thread's stack is reconstructed from the previous stack of the same +thread in the block. The first sample of a thread in a block starts from an +empty stack. `STACK_INSTRUCTIONS` holds one of these encodings for every +sample not covered by a `REPEAT`: | Code | Name | Description | |------|------|-------------| | 0x00 | REPEAT | RLE: identical stack repeated N times | -| 0x01 | FULL | Complete stack (first sample or no match) | -| 0x02 | SUFFIX | Shares N frames from bottom of previous stack | -| 0x03 | POP_PUSH | Remove M frames from top, add N new frames | +| 0x01 | REPLACE_SUFFIX | Keeps N outer frames of the previous stack, adds M new frames | +| 0x02 | REUSE | Complete stack stored earlier in the block | + +Stacks hold at most 256 frames. The writer keeps the innermost 256 frames of +deeper stacks. -### Record Formats +### Stack Encoding Formats + +All fields are varints. Stacks are written outermost frame first. **REPEAT (0x00) - Run-Length Encoded Identical Stacks:** ``` +-----------------+-----------+----------------------------------------+ -| thread_id | 8 bytes | Thread identifier (uint64, fixed) | -| interpreter_id | 4 bytes | Interpreter ID (uint32, fixed) | -| encoding | 1 byte | 0x00 (REPEAT) | +| encoding | varint | 0x00 (REPEAT) | | count | varint | Number of samples in this RLE group | -| samples | varies | Interleaved: [delta: varint, status: 1]| -| | | repeated count times | +-----------------+-----------+----------------------------------------+ ``` -The stack is inherited from this thread's previous sample. Each sample in the -group gets its own timestamp delta and status byte, stored as interleaved pairs -(delta1, status1, delta2, status2, ...) rather than separate arrays. +The stack is inherited from this thread's previous sample, for this sample +and the thread's next `count - 1` samples. Samples from other threads can +appear in between: the reader keeps a remaining count for each thread and +reads no encoding for that thread until the count reaches zero. Each sample +in the group keeps its own timestamp and status in the other columns. -**FULL (0x01) - Complete Stack:** +**REPLACE_SUFFIX (0x01) - Shared Outer Frames:** ``` +-----------------+-----------+----------------------------------------+ -| thread_id | 8 bytes | Thread identifier (uint64, fixed) | -| interpreter_id | 4 bytes | Interpreter ID (uint32, fixed) | -| encoding | 1 byte | 0x01 (FULL) | -| timestamp_delta | varint | Microseconds since thread's last sample| -| status | 1 byte | Thread state flags | -| stack_depth | varint | Number of frames in call stack | -| frame_indices | varint[] | Array of frame table indices | +| encoding | varint | 0x01 (REPLACE_SUFFIX) | +| keep_count | varint | Outer frames kept from previous stack | +| new_count | varint | Inner frames read from FRAME_IDS | +-----------------+-----------+----------------------------------------+ ``` -Used for the first sample from a thread, or when delta encoding would not -provide savings. +Used when a call or return changed the inner part of the stack: the outer +frames are kept, and `new_count` new frames are read from `FRAME_IDS`. A +complete stack, such as the first sample of a thread, uses `keep_count` 0. -**SUFFIX (0x02) - Shared Suffix Match:** +**REUSE (0x02) - Repeated Complete Stack:** ``` +-----------------+-----------+----------------------------------------+ -| thread_id | 8 bytes | Thread identifier (uint64, fixed) | -| interpreter_id | 4 bytes | Interpreter ID (uint32, fixed) | -| encoding | 1 byte | 0x02 (SUFFIX) | -| timestamp_delta | varint | Microseconds since thread's last sample| -| status | 1 byte | Thread state flags | -| shared_count | varint | Frames shared from bottom of prev stack| -| new_count | varint | New frames at top of stack | -| new_frames | varint[] | Array of new_count frame indices | +| encoding | varint | 0x02 (REUSE) | +| offset | varint | Index of the first frame in FRAME_IDS | +| count | varint | Number of frames in the stack | +-----------------+-----------+----------------------------------------+ ``` -Used when a function call added frames to the top of the stack. The shared -frames from the previous stack are kept, and new frames are prepended. +Used when a complete stack was already written earlier in the same block, +for example by another thread running the same code. The frames are copied +from `FRAME_IDS` instead of being stored again. -**POP_PUSH (0x03) - Pop and Push:** -``` -+-----------------+-----------+----------------------------------------+ -| thread_id | 8 bytes | Thread identifier (uint64, fixed) | -| interpreter_id | 4 bytes | Interpreter ID (uint32, fixed) | -| encoding | 1 byte | 0x03 (POP_PUSH) | -| timestamp_delta | varint | Microseconds since thread's last sample| -| status | 1 byte | Thread state flags | -| pop_count | varint | Frames to remove from top of prev stack| -| push_count | varint | New frames to add at top | -| new_frames | varint[] | Array of push_count frame indices | -+-----------------+-----------+----------------------------------------+ -``` -Used when the code path changed: some frames were popped (function returns) -and new frames were pushed (different function calls). +For example, two threads produce four samples: + +| Sample | Thread | Stack | Encoding | +|--------|--------|----------------|----------------------| +| 0 | A | `[4, 21, 37]` | `REPLACE_SUFFIX 0 3` | +| 1 | B | `[4, 21, 37]` | `REUSE 0 3` | +| 2 | A | `[4, 21, 52]` | `REPLACE_SUFFIX 2 1` | +| 3 | A | `[4, 21, 52]` | `REPEAT 1` | + +`FRAME_IDS` holds only `[4, 21, 37, 52]`. ### Thread and Interpreter Identification +Each `(thread_id, interpreter_id)` pair used in a block is stored once in +`STREAM_TABLE`, as an 8-byte thread ID followed by a 4-byte interpreter ID. +`SAMPLE_STREAM_IDS` then holds one varint per sample: the index of its pair +in the table. Samples are kept in arrival order, so samples from different +threads are interleaved. + Thread IDs are 64-bit values that can be large (memory addresses on some platforms) and vary unpredictably. Using a fixed 8-byte encoding avoids the overhead of varint encoding for large values and simplifies parsing @@ -226,23 +281,30 @@ Multiple flags can be set simultaneously (e.g., a thread can hold the GIL while also running on CPU). Analysis tools use these to filter samples or visualize thread states over time. +Most flags rarely change within a block, so the status bytes are stored as +bit planes. Bits set in every sample are recorded once in +`common_status_flags`. Each bit in `status_bitmap_flags` has a bitmap of +`ceil(sample_count / 8)` bytes in `STATUS_BITMAPS`, in increasing bit order, +with sample `i` in bit `i % 8` of byte `i / 8`. Bits that never occur take no +space. The reader rebuilds each status byte from `common_status_flags` and +the sample's bit in each bitmap. + ### Timestamp Delta Encoding Timestamps use delta encoding rather than absolute values. Absolute timestamps in microseconds require 8 bytes each, but consecutive samples from the same thread are typically separated by the sampling interval -(e.g., 1000 microseconds), so the delta between them is small and fits -in 1-2 varint bytes. The writer tracks the previous timestamp for each -thread separately. The first sample from a thread encodes its delta from -the profiling start time; subsequent samples encode the delta from that -thread's previous sample. This per-thread tracking is necessary because -samples are interleaved across threads in arrival order, not grouped by -thread. - -For REPEAT (RLE) records, timestamp deltas and status bytes are stored as -interleaved pairs (delta, status, delta, status, ...) - one pair per -repeated sample - allowing efficient batching while preserving the exact -timing and state of each sample. +(e.g., 1000 microseconds), so each timestamp is expected to be the thread's +previous timestamp plus the interval, and only the difference from that +expected timestamp is stored. The writer tracks the previous timestamp for +each thread separately. The expected timestamp for a thread's first sample +in a block is the block's `base_timestamp`. This per-thread tracking is +necessary because samples are interleaved across threads in arrival order, +not grouped by thread. + +Each residual is a signed 64-bit zigzag varint. A sample taken exactly on +schedule has a residual of 0 and takes one byte, while timestamps are still +preserved exactly. ### Frame Indexing @@ -251,15 +313,25 @@ rather than inline data. This provides massive space savings because call stacks are highly repetitive: the same function appears in many samples (hot functions), call stacks often share common prefixes (main -> app -> handler -> ...), and recursive functions create repeated frame sequences. -A frame index is typically 1-2 varint bytes. Inline frame data would be -20-200+ bytes (two strings plus a line number). For a profile with 100,000 -samples averaging 30 frames each, this reduces frame data from potentially +A frame index is typically 1-2 bytes. Inline frame data would be 20-200+ +bytes (two strings plus a line number). For a profile with 100,000 samples +averaging 30 frames each, this reduces frame data from potentially gigabytes to tens of megabytes. -Frame indices are written innermost-first (the currently executing frame -has index 0 in the array). This ordering works well with delta compression: -function calls typically add frames at the top (index 0), while shared -frames remain at the bottom. +`FRAME_IDS` stores the indices read by `REPLACE_SUFFIX`, using the smallest +byte width that fits the largest index in the block. The bytes are split into +planes, low bytes first, so similar bytes sit together for compression: + +``` +indices: 0x1234 0x5678 0x9abc +low-byte plane: 0x34 0x78 0xbc +high-byte plane: 0x12 0x56 0x9a +``` + +Frame indices are written outermost-first (the outermost frame has index 0 +in the array). This ordering works well with delta compression: function +calls typically change frames at the end of the array, while shared frames +remain at the start. ## String Table @@ -453,29 +525,34 @@ bytes. Zigzag encoding interleaves positive and negative values: ``` This ensures small-magnitude values (whether positive or negative) encode -in few bytes. +in few bytes. Frame table deltas use 32-bit zigzag values and timestamp +residuals use 64-bit zigzag values. ## Compression -When compression is enabled, the sample data region contains a zstd stream. +The header's compression type controls whether zstd may be used. Each column +of each block is encoded separately, using whichever representation is +smallest: + +- **constant**, when every byte of the column is the same, such as the stream + IDs of a single-threaded block or residuals of on-time samples; +- **zstd**, when compression is enabled and the compressed column plus its + stored-size varint is smaller than the raw column; +- **raw** otherwise. + The string table, frame table, and footer remain uncompressed so readers can access metadata without decompressing the entire file. A tool that only needs to report "this file contains 50,000 samples of 3 threads" can read the header -and footer without touching the compressed sample data. This also simplifies -the format: the header's offset fields point directly to the tables rather -than to positions within a decompressed stream. +and footer without touching the compressed sample data. Zstd provides an excellent balance of compression ratio and speed. Profiling data compresses very well (often 5-10x) due to repetitive patterns: the same -small set of frame indices appears repeatedly, and delta-encoded timestamps -cluster around the sampling interval. Zstd's streaming API allows compression -without buffering the entire dataset. The writer feeds sample data through -the compressor incrementally, flushing compressed chunks to disk as they -become available. +small set of frame indices appears repeatedly, and timestamp residuals +cluster around zero. -Level 5 compression is used as a default. Lower levels (1-3) are faster but -compress less; higher levels (6+) compress more but slow down writing. Level -5 provides good compression with minimal impact on profiling overhead. +Level 3 compression is used as a default. It gives good compression without +slowing down each block flush. Compressing columns separately keeps blocks +independent, at the cost of restarting compression history at every block. ## Reading and Writing @@ -486,9 +563,9 @@ compress less; higher levels (6+) compress more but slow down writing. Level 3. For each sample: - Intern any new strings, assigning sequential indices - Intern any new frames, assigning sequential indices - - Encode the sample record and write to the buffer - - Flush the buffer through compression (if enabled) when full -4. Flush remaining buffered data and finalize compression + - Append the sample to the current block's columns + - Flush the block when it reaches its size or stream limit +4. Flush the last block 5. Write the string table (length-prefixed strings in index order) 6. Write the frame table (varint-encoded entries in index order) 7. Write measured profile statistics, when available @@ -497,7 +574,13 @@ compress less; higher levels (6+) compress more but slow down writing. Level The writer maintains two dictionaries: one mapping strings to indices, one mapping (filename_idx, funcname_idx, lineno) tuples to frame indices. These -enable O(1) lookup during interning. +enable O(1) lookup during interning. To find `REUSE` candidates, it also keeps +a small per-block cache of complete stacks, confirmed by a full comparison. + +When a block is flushed, the status and frame ID columns are transposed into +bit and byte planes, the `REPEAT` counts are inserted into the instruction +column, and each column is encoded as described in +[Compression](#compression). ### Reading @@ -510,13 +593,32 @@ enable O(1) lookup during interning. 6. Parse the string table, populating the array 7. Allocate the frame array 8. Parse the frame table, populating the array -9. If compressed, decompress the sample data region -10. Iterate through samples, resolving indices to strings/frames - (byte-swapping thread_id and interpreter_id if needed) +9. For each block, decode its columns into a reusable buffer and replay its + samples in order, resolving indices to strings/frames (byte-swapping the + stream table if needed) The reader builds lookup arrays rather than dictionaries since it only needs index-to-value mapping, not value-to-index. +Consecutive samples of one stream with the same stack and status are passed +to the collector in a single call, with up to 8,192 timestamps. + +The block header and columns are validated before any sample of the block is +replayed. Stack instructions are checked as they are replayed, so a block +with a bad instruction may already have passed earlier samples to the +collector before the error is raised. The reader rejects a block when: + +- a size, encoding, or stream or frame reference is out of range, or a + column has trailing data; +- the stream table lists a thread twice or has a stream with no samples; +- a stream's first instruction in the block is `REPEAT`, or a `REPEAT` + count is zero or runs past the block; +- a stack would be deeper than 256 frames, or `REUSE` refers to frame IDs + that have not been read yet; +- not every frame ID is used; +- status bitmap padding bits are not zero; +- it uses zstd in an uncompressed file. + ## Platform Considerations ### Byte Ordering and Cross-Platform Portability @@ -540,7 +642,7 @@ zeros instead of the actual value. The reader tracks whether byte-swapping is needed via a `needs_swap` flag set during header parsing. All fixed-width fields in the header, footer, and -sample data are conditionally byte-swapped using Python's internal byte-swap +stream tables are conditionally byte-swapped using Python's internal byte-swap functions (`_Py_bswap32`, `_Py_bswap64` from `pycore_bitutils.h`). Variable-length integers (varints) are byte-order independent since they @@ -557,21 +659,28 @@ patterns benefit from kernel read-ahead. The implementation uses `madvise()` to hint the access pattern to the kernel: `MADV_SEQUENTIAL` indicates the file will be read linearly, enabling -aggressive read-ahead. `MADV_WILLNEED` requests pre-faulting of pages. -On Linux, `MAP_POPULATE` pre-faults all pages at mmap time rather than on -first access, moving page fault overhead from the parsing loop to the -initial mapping for more predictable performance. For large files (over -32 MB), `MADV_HUGEPAGE` requests transparent huge pages (2 MB instead of -4 KB) to reduce TLB pressure when accessing large amounts of data. - -On Windows, the implementation falls back to standard file I/O with full -file buffering. Profiling data files are typically small enough (tens to -hundreds of megabytes) that this is acceptable. - -The writer uses a 512 KB buffer to batch small writes. Each sample record -is typically tens of bytes; writing these individually would incur excessive -syscall overhead. The buffer accumulates data until full, then flushes in -one write() call (or feeds through the compression stream). +aggressive read-ahead. For large files (over 32 MB), `MADV_HUGEPAGE` requests +transparent huge pages (2 MB instead of 4 KB) to reduce TLB pressure when +accessing large amounts of data. Pages are not loaded up front, only when +replay reaches them. + +On Linux, the reader also gives back memory it no longer needs. Once the +string and frame tables have been parsed, and after each sample block has +been replayed, it uses `MADV_DONTNEED` to tell the kernel those pages can be +dropped. The mapping is read-only, so if a dropped page is needed again, the +kernel reads it back from the file. This keeps the memory used for sample +data small, however large the file is. + +On Windows, the reader uses standard file I/O instead. When a file is opened, +it only reads the header and the part after the sample data, which holds the +string and frame tables, the statistics and the footer. During replay, +samples are read into a reusable buffer. The reader requests the smaller of +2 MiB and the sample section size. A complete block, including its header, +fits in this buffer. When the buffer runs low, the bytes not yet +replayed are moved to the start and the rest is filled from the file. + +The writer buffers samples in the current block and writes each completed +block with a single call, so individual samples never cause a write. ## Future Considerations diff --git a/InternalDocs/qsbr.md b/InternalDocs/qsbr.md index d511396fdab645..58532a6045d3fe 100644 --- a/InternalDocs/qsbr.md +++ b/InternalDocs/qsbr.md @@ -116,8 +116,9 @@ its last advance: is larger than `QSBR_FREE_MEM_LIMIT` (1 MiB), when the accumulated deferred memory exceeds that limit, or when the count exceeds `QSBR_DEFERRED_LIMIT` (127, sized so a chunk of work items is processed before it overflows). - Crossing any of these thresholds also sets a per-thread `should_process` - flag, signalling that the deferred-free list should be drained. + Crossing any of these thresholds also sets the thread's + `_PY_EVAL_PROCESS_DELAYED_BIT` eval breaker bit, signalling that the + deferred-free list should be drained. * For mimalloc pages held by QSBR, the thread tracks `deferred_page_memory` and advances `wr_seq` when either the individual page or the accumulated @@ -125,11 +126,12 @@ its last advance: promptly here matters because a held page cannot be reused for a different size class or by a different thread. -Processing of the deferred-free list normally happens from the eval breaker -(rather than from inside `_PyMem_FreeDelayed`), which gives the global -`rd_seq` a better chance to have advanced far enough that items can actually -be freed. `_PyMem_ProcessDelayed` is still called from the free path as a -safety valve when a work-item chunk fills up. +Processing of the deferred-free list happens from the eval breaker rather +than from inside `_PyMem_FreeDelayed`. This gives the global `rd_seq` a better +chance to have advanced far enough that items can actually be freed. It is +also required for correctness: items queued by `_PyObject_XDecRefDelayed` may +run arbitrary code when they are released, and the callers of +`_PyMem_FreeDelayed` may be in the middle of mutating a dict, list or type. This optimization improves runtime speed but may increase peak memory usage by slightly delaying when memory can be reclaimed; the size-based thresholds diff --git a/Lib/_ast_unparse.py b/Lib/_ast_unparse.py index 58bc06397f5134..bf6a647d2f0d7f 100644 --- a/Lib/_ast_unparse.py +++ b/Lib/_ast_unparse.py @@ -638,13 +638,15 @@ def _write_ftstring_inner(self, node, is_format_spec=False): for value in node.values: self._write_ftstring_inner(value, is_format_spec=is_format_spec) elif isinstance(node, Constant) and isinstance(node.value, str): - value = node.value.replace("{", "{{").replace("}", "}}") - + value = node.value if is_format_spec: value = value.replace("\\", "\\\\") + value = value.replace("{", "\\x7b").replace("}", "\\x7d") value = value.replace("'", "\\'") value = value.replace('"', '\\"') value = value.replace("\n", "\\n") + else: + value = value.replace("{", "{{").replace("}", "}}") self.write(value) elif isinstance(node, FormattedValue): self.visit_FormattedValue(node) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 5bbb6d433ee2ad..b974887fbe0970 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -90,67 +90,32 @@ def _ymd2ord(year, month, day): # pasting together 25 4-year cycles. assert _DI100Y == 25 * _DI4Y - 1 -def _ord2ymd(n): +def _ord2ymd(ordinal): "ordinal -> (year, month, day), considering 01-Jan-0001 as day 1." - # n is a 1-based index, starting at 1-Jan-1. The pattern of leap years - # repeats exactly every 400 years. The basic strategy is to find the - # closest 400-year boundary at or before n, then work with the offset - # from that boundary to n. Life is much clearer if we subtract 1 from - # n first -- then the values of n at 400-year boundaries are exactly - # those divisible by _DI400Y: + # Neri and Schneider's algorithm, https://doi.org/10.1002/spe.3172. + # ord_to_ymd() in Modules/_datetimemodule.c does the same steps. # - # D M Y n n-1 - # -- --- ---- ---------- ---------------- - # 31 Dec -400 -_DI400Y -_DI400Y -1 - # 1 Jan -399 -_DI400Y +1 -_DI400Y 400-year boundary - # ... - # 30 Dec 000 -1 -2 - # 31 Dec 000 0 -1 - # 1 Jan 001 1 0 400-year boundary - # 2 Jan 001 2 1 - # 3 Jan 001 3 2 - # ... - # 31 Dec 400 _DI400Y _DI400Y -1 - # 1 Jan 401 _DI400Y +1 _DI400Y 400-year boundary - n -= 1 - n400, n = divmod(n, _DI400Y) - year = n400 * 400 + 1 # ..., -399, 1, 401, ... - - # Now n is the (non-negative) offset, in days, from January 1 of year, to - # the desired date. Now compute how many 100-year cycles precede n. - # Note that it's possible for n100 to equal 4! In that case 4 full - # 100-year cycles precede the desired day, which implies the desired - # day is December 31 at the end of a 400-year cycle. - n100, n = divmod(n, _DI100Y) - - # Now compute how many 4-year cycles precede it. - n4, n = divmod(n, _DI4Y) - - # And now how many single years. Again n1 can be 4, and again meaning - # that the desired day is December 31 at the end of the 4-year cycle. - n1, n = divmod(n, 365) - - year += n100 * 100 + n4 * 4 + n1 - if n1 == 4 or n100 == 4: - assert n == 0 - return year-1, 12, 31 - - # Now the year is correct, and n is the offset from January 1. We find - # the month via an estimate that's either exact or one too large. - leapyear = n1 == 3 and (n4 != 24 or n100 == 3) - assert leapyear == _is_leap(year) - month = (n + 50) >> 5 - preceding = _DAYS_BEFORE_MONTH[month] + (month > 2 and leapyear) - if preceding > n: # estimate is too large - month -= 1 - preceding -= _DAYS_IN_MONTH[month] + (month == 2 and leapyear) - n -= preceding - assert 0 <= n < _days_in_month(year, month) - - # Now the year and month are correct, and n is the offset from the - # start of that month: we're done! - return year, month, n+1 + # Count days from 0000-03-01, so that the leap day is the last day of + # its year. 0001-01-01 is day 306. + days = ordinal + 305 + + # Centuries average _DI400Y/4 days: three of 36524, then one of 36525. + century, rest = divmod(4 * days + 3, _DI400Y) + day_of_century = rest // 4 + + # Likewise, years average _DI4Y/4 days: three of 365, then one of 366. + year_of_century, rest = divmod(4 * day_of_century + 3, _DI4Y) + day_of_year = rest // 4 + + # From March, month lengths repeat 31, 30, 31, 30, 31 (153 days). + # 461 = 3*153 + 2 numbers the months from 3 (March) to 14 (February). + month, rest = divmod(5 * day_of_year + 461, 153) + day = rest // 5 + 1 + year = 100 * century + year_of_century + if month > 12: + return year + 1, month - 12, day + return year, month, day # Month and day names. For localized versions, see the calendar module. _MONTHNAMES = [None, "Jan", "Feb", "Mar", "Apr", "May", "Jun", @@ -396,6 +361,10 @@ def _parse_isoformat_date(dtstr): pos += has_sep dayno = _read_isoformat_component(dtstr[pos:pos + 1], 1) + pos += 1 + + if pos != len(dtstr): + raise ValueError("Invalid isoformat string") return list(_isoweek_to_gregorian(year, weekno, dayno)) else: @@ -406,6 +375,10 @@ def _parse_isoformat_date(dtstr): pos += has_sep day = _read_isoformat_component(dtstr[pos:pos + 2], 2) + pos += 2 + + if pos != len(dtstr): + raise ValueError("Invalid isoformat string") return [year, month, day] @@ -1931,7 +1904,7 @@ def fromisoformat(cls, date_string): raise ValueError( f'Invalid isoformat string: {date_string!r}') from None - if tstr: + if separator_location < len(date_string): try: (time_components, became_next_day, diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 370b518f1e5e5a..9d1cf209622320 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -733,6 +733,8 @@ def _readinto(self, b, read1): data = self.read1(len(b)) else: data = self.read(len(b)) + if data is None: + return None n = len(data) b[:n] = data @@ -1208,7 +1210,8 @@ def _readinto(self, buf, read1): while written < len(buf): # First try to read from internal buffer - avail = min(len(self._read_buf) - self._read_pos, len(buf)) + avail = min(len(self._read_buf) - self._read_pos, + len(buf) - written) if avail: buf[written:written+avail] = \ self._read_buf[self._read_pos:self._read_pos+avail] @@ -1222,14 +1225,22 @@ def _readinto(self, buf, read1): if len(buf) - written > self.buffer_size: n = self.raw.readinto(buf[written:]) if not n: + if n is None and not written: + return None # would block break # eof written += n # Otherwise refill internal buffer - unless we're # in read1 mode and already got some data elif not (read1 and written): - if not self._peek_unlocked(1): + # The internal buffer is empty here. + chunk = self.raw.read(self.buffer_size) + if not chunk: + if chunk is None and not written: + return None # would block break # eof + self._read_buf = chunk + self._read_pos = 0 # In readinto1 mode, return as soon as we have some data if read1 and written: @@ -1384,8 +1395,12 @@ def __init__(self, reader, writer, buffer_size=DEFAULT_BUFFER_SIZE): if not writer.writable(): raise OSError('"writer" argument must be writable.') - self.reader = BufferedReader(reader, buffer_size) - self.writer = BufferedWriter(writer, buffer_size) + new_reader = BufferedReader(reader, buffer_size) + new_writer = BufferedWriter(writer, buffer_size) + # Release a previous writer before a previous reader so it flushes + # before the reader closes a shared raw stream. + self.writer = new_writer + self.reader = new_reader def read(self, size=-1): if size is None: @@ -1404,6 +1419,15 @@ def peek(self, size=0): def read1(self, size=-1): return self.reader.read1(size) + def readline(self, size=-1): + r"""Read and return a line from the stream. + + If size is specified, at most size bytes will be read. + + The line terminator is always b'\n'. + """ + return self.reader.readline(size) + def readinto1(self, b): return self.reader.readinto1(b) @@ -2076,10 +2100,14 @@ class TextIOWrapper(TextIOBase): r"""Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be - decoded or encoded with. It defaults to locale.getencoding(). + decoded or encoded with. It defaults to UTF-8; if UTF-8 Mode is + disabled, the encoding returned by locale.getencoding() is used + instead. See the codecs module for the list of supported + encodings. - errors determines the strictness of encoding and decoding (see the - codecs.register) and defaults to "strict". + errors determines the strictness of encoding and decoding (see + help(codecs.Codec) or the documentation for codecs.register) and + defaults to "strict". newline can be None, '', '\n', '\r', or '\r\n'. It controls the handling of line endings. If it is None, universal newlines is @@ -2458,9 +2486,12 @@ def tell(self): # be O(1) in most situations (common decoders, sensible input). # Actually, it will be exactly 1 for fixed-size codecs (all # 8-bit codecs, also UTF-16 and UTF-32). - skip_bytes = int(self._b2cratio * chars_to_skip) + skip_estimate = self._b2cratio * chars_to_skip + if 0 <= skip_estimate <= len(next_input): + skip_bytes = int(skip_estimate) + else: + skip_bytes = 0 skip_back = 1 - assert skip_bytes <= len(next_input) while skip_bytes > 0: decoder.setstate((b'', dec_flags)) # Decode up to temptative start point diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 4232a868a34e9a..fbc0f5926ea16b 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -377,7 +377,7 @@ def input(self, prompt: object = "") -> str: prompt_str = str(prompt) reader.ps1 = prompt_str sys.audit("builtins.input", prompt_str) - with reader.suspend_history_writes(): + with reader.suspend_history_writes(), reader.suspend_colorization(): result = reader.readline(startup_hook=self.startup_hook) sys.audit("builtins.input/result", result) return result diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py index 3e15a340135669..d556e6dd7c85f6 100644 --- a/Lib/_pyrepl/unix_console.py +++ b/Lib/_pyrepl/unix_console.py @@ -835,7 +835,7 @@ def __plan_changed_line( planned_cells = diff.new_cells changed_cell = visible_new_cells[0] return LineUpdate( - kind="insert_char", + kind=("replace_char" if start_x == oldline.width else "insert_char"), y=y, start_cell=start_cell, start_x=start_x, diff --git a/Lib/_pyrepl/utils.py b/Lib/_pyrepl/utils.py index 180e111873dbb3..bde82253490407 100644 --- a/Lib/_pyrepl/utils.py +++ b/Lib/_pyrepl/utils.py @@ -280,14 +280,14 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool: trace("is_soft_keyword_used{t}", t=tokens) match tokens: case ( - None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"), + None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"), TI(string="match"), TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START) | TI(T.OP, string="(" | "*" | "-" | "+" | "[" | "{" | "~" | "...") ): return True case ( - None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"), + None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"), TI(string="match"), TI(T.NAME, string=s) ): @@ -295,14 +295,14 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool: return s in keyword_first_sets_match return True case ( - None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"), + None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"), TI(string="case"), TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START) | TI(T.OP, string="(" | "*" | "-" | "+" | "[" | "{") ): return True case ( - None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"), + None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"), TI(string="case"), TI(T.NAME, string=s) ): @@ -312,13 +312,13 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool: case (TI(string="case"), TI(string="_"), TI(string=":")): return True case ( - None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"), + None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"), TI(string="type"), TI(T.NAME, string=s) ): return not keyword.iskeyword(s) case ( - None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":" | ";"), + None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":" | ";"), TI(string="lazy"), TI(string="import") | TI(string="from") ): diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py index 51079e27a77a55..466a3e62d8cb9b 100644 --- a/Lib/_pyrepl/windows_console.py +++ b/Lib/_pyrepl/windows_console.py @@ -165,19 +165,16 @@ def __init__( trace('console supports virtual terminal') # Save original console modes so we can recover on cleanup. - original_input_mode = DWORD() - if not GetConsoleMode(InHandle, original_input_mode): - raise WinError(get_last_error()) - trace(f'saved original input mode 0x{original_input_mode.value:x}') - self.__original_input_mode = original_input_mode.value + assert _winapi is not None # to make mypy happy + self.__original_input_mode = _winapi.GetConsoleMode(InHandle) + trace(f'saved original input mode 0x{self.__original_input_mode:x}') - if not SetConsoleMode( + _winapi.SetConsoleMode( OutHandle, ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING, - ): - raise WinError(get_last_error()) + ) self.width = 80 self.height = 25 @@ -474,8 +471,8 @@ def prepare(self) -> None: self.sync_rendered_screen(RenderedScreen.empty(), self.posxy) if self.__vt_support: - if not SetConsoleMode(InHandle, self.__original_input_mode | ENABLE_VIRTUAL_TERMINAL_INPUT): - raise WinError(get_last_error()) + assert _winapi is not None # to make mypy happy + _winapi.SetConsoleMode(InHandle, self.__original_input_mode | ENABLE_VIRTUAL_TERMINAL_INPUT) self._enable_bracketed_paste() def restore(self) -> None: @@ -484,8 +481,8 @@ def restore(self) -> None: self.__write(ANSIColors.RESET) # Recover to original mode before running REPL self._disable_bracketed_paste() - if not SetConsoleMode(InHandle, self.__original_input_mode): - raise WinError(get_last_error()) + assert _winapi is not None # to make mypy happy + _winapi.SetConsoleMode(InHandle, self.__original_input_mode) def _move_relative(self, x: int, y: int) -> None: """Moves relative to the current posxy""" @@ -661,8 +658,8 @@ def flushoutput(self) -> None: def forgetinput(self) -> None: """Forget all pending, but not yet processed input.""" - if not FlushConsoleInputBuffer(InHandle): - raise WinError(get_last_error()) + assert _winapi is not None # to make mypy happy + _winapi.FlushConsoleInputBuffer(InHandle) def getpending(self) -> Event: """Return the characters that have been typed but not yet @@ -815,23 +812,10 @@ class INPUT_RECORD(Structure): ] ScrollConsoleScreenBuffer.restype = BOOL - GetConsoleMode = _KERNEL32.GetConsoleMode - GetConsoleMode.argtypes = [HANDLE, POINTER(DWORD)] - GetConsoleMode.restype = BOOL - - SetConsoleMode = _KERNEL32.SetConsoleMode - SetConsoleMode.argtypes = [HANDLE, DWORD] - SetConsoleMode.restype = BOOL - ReadConsoleInput = _KERNEL32.ReadConsoleInputW ReadConsoleInput.argtypes = [HANDLE, POINTER(INPUT_RECORD), DWORD, POINTER(DWORD)] ReadConsoleInput.restype = BOOL - - FlushConsoleInputBuffer = _KERNEL32.FlushConsoleInputBuffer - FlushConsoleInputBuffer.argtypes = [HANDLE] - FlushConsoleInputBuffer.restype = BOOL - OutHandle = _winapi.GetStdHandle(_winapi.STD_OUTPUT_HANDLE) InHandle = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE) else: @@ -841,9 +825,6 @@ def _win_only(*args, **kwargs): GetConsoleScreenBufferInfo = _win_only ScrollConsoleScreenBuffer = _win_only - GetConsoleMode = _win_only - SetConsoleMode = _win_only ReadConsoleInput = _win_only - FlushConsoleInputBuffer = _win_only OutHandle = 0 InHandle = 0 diff --git a/Lib/abc.py b/Lib/abc.py index 08f708d1b89208..e782fe7d315abf 100644 --- a/Lib/abc.py +++ b/Lib/abc.py @@ -105,7 +105,7 @@ def __init__( try: from _abc import (get_cache_token, _abc_init, _abc_register, _abc_instancecheck, _abc_subclasscheck, _get_dump, - _reset_registry, _reset_caches) + _reset_registry, _reset_caches, _set_subclasscheck) except ImportError: from _py_abc import ABCMeta, get_cache_token ABCMeta.__module__ = 'abc' @@ -163,6 +163,10 @@ def _abc_caches_clear(cls): """Clear the caches (for debugging or testing).""" _reset_caches(cls) + # Classes using the default __subclasscheck__ are checked + # directly by the C implementation. + _set_subclasscheck(ABCMeta.__subclasscheck__) + def update_abstractmethods(cls): """Recalculate the set of abstract methods of an abstract class. diff --git a/Lib/asyncio/subprocess.py b/Lib/asyncio/subprocess.py index 26d7b755105c9d..f83d00af5b5d45 100644 --- a/Lib/asyncio/subprocess.py +++ b/Lib/asyncio/subprocess.py @@ -77,13 +77,14 @@ def pipe_connection_lost(self, fd, exc): if pipe is not None: pipe.close() self.connection_lost(exc) - if exc is None: - self._stdin_closed.set_result(None) - else: - self._stdin_closed.set_exception(exc) - # Since calling `wait_closed()` is not mandatory, - # we shouldn't log the traceback if this is not awaited. - self._stdin_closed._log_traceback = False + if not self._stdin_closed.done(): + if exc is None: + self._stdin_closed.set_result(None) + else: + self._stdin_closed.set_exception(exc) + # Since calling `wait_closed()` is not mandatory, + # we shouldn't log the traceback if this is not awaited. + self._stdin_closed._log_traceback = False return if fd == 1: reader = self.stdout diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 6c8d7cbbda6440..cc242cbf1238d5 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -257,9 +257,12 @@ def __eager_start(self): prev_task = _py_swap_current_task(self._loop, self) try: _py_register_eager_task(self) + # gh-157299: the eager step must record the awaited_by edge + futures.future_add_to_awaited_by(self, prev_task) try: self._context.run(self.__step_run_and_handle_result, None) finally: + futures.future_discard_from_awaited_by(self, prev_task) _py_unregister_eager_task(self) finally: try: @@ -838,17 +841,12 @@ def gather(*coros_or_futures, return_exceptions=False): return outer loop = events._get_running_loop() - if loop is not None: - cur_task = current_task(loop) - else: - cur_task = None - def _done_callback(fut, cur_task=cur_task): + def _done_callback(fut): nonlocal nfinished nfinished += 1 - if cur_task is not None: - futures.future_discard_from_awaited_by(fut, cur_task) + futures.future_discard_from_awaited_by(fut, outer) if outer is None or outer.done(): if not fut.cancelled(): @@ -863,13 +861,13 @@ def _done_callback(fut, cur_task=cur_task): # instead of returning it. exc = fut._make_cancelled_error() # gh-157213: children outliving gather() must lose the edge - _discard_awaited_by(children, cur_task) + _discard_awaited_by(children, outer) outer.set_exception(exc) return else: exc = fut.exception() if exc is not None: - _discard_awaited_by(children, cur_task) + _discard_awaited_by(children, outer) outer.set_exception(exc) return @@ -926,8 +924,6 @@ def _done_callback(fut, cur_task=cur_task): if fut.done(): done_futs.append(fut) else: - if cur_task is not None: - futures.future_add_to_awaited_by(fut, cur_task) fut.add_done_callback(_done_callback) else: @@ -937,6 +933,10 @@ def _done_callback(fut, cur_task=cur_task): children.append(fut) outer = _GatheringFuture(children, loop=loop) + # Add the awaited-by edges only after all arguments are validated + for fut in arg_to_fut.values(): + if not fut.done(): + futures.future_add_to_awaited_by(fut, outer) # Run done callbacks after GatheringFuture created so any post-processing # can be performed at this point # optimization: in the special case that *all* futures finished eagerly, @@ -987,15 +987,11 @@ def shield(arg): loop = futures._get_loop(inner) outer = loop.create_future() - if loop is not None and (cur_task := current_task(loop)) is not None: - futures.future_add_to_awaited_by(inner, cur_task) - else: - cur_task = None - - def _clear_awaited_by_callback(inner): - futures.future_discard_from_awaited_by(inner, cur_task) + futures.future_add_to_awaited_by(inner, outer) def _inner_done_callback(inner): + futures.future_discard_from_awaited_by(inner, outer) + if outer.cancelled(): return @@ -1011,13 +1007,7 @@ def _inner_done_callback(inner): def _outer_done_callback(outer): if not inner.done(): inner.remove_done_callback(_inner_done_callback) - if cur_task is not None: - inner.remove_done_callback(_clear_awaited_by_callback) - futures.future_discard_from_awaited_by(inner, cur_task) - - if cur_task is not None: - inner.add_done_callback(_clear_awaited_by_callback) - + futures.future_discard_from_awaited_by(inner, outer) inner.add_done_callback(_inner_done_callback) outer.add_done_callback(_outer_done_callback) diff --git a/Lib/asyncio/tools.py b/Lib/asyncio/tools.py index 6bc53f4d7d3e5f..1ee4a471ba5bb0 100644 --- a/Lib/asyncio/tools.py +++ b/Lib/asyncio/tools.py @@ -157,12 +157,13 @@ def get_all_awaited_by(pid): return unwinder.get_all_awaited_by() -def build_async_tree(result, task_emoji="(T)", cor_emoji=""): +def build_async_tree(result, task_emoji="(T)", cor_emoji="", fut_emoji="(F)"): """ Build a list of strings for pretty-print an async call tree. The call tree is produced by `get_all_async_stacks()`, prefixing tasks - with `task_emoji` and coroutine frames with `cor_emoji`. + with `task_emoji`, futures with `fut_emoji`, and coroutine frames with + `cor_emoji`. """ id2name, awaits, task_stacks = _index(result) g = _task_graph(awaits) @@ -170,9 +171,17 @@ def build_async_tree(result, task_emoji="(T)", cor_emoji=""): if cycles: raise CycleFoundException(cycles, id2name) labels, children = _build_tree(id2name, awaits, task_stacks) + future_ids = { + node.task_id + for thread in result + for node in thread.awaited_by + if node.is_future + } def pretty(node): flag = task_emoji if node[0] == NodeType.TASK else cor_emoji + if node[0] == NodeType.TASK and node[1] in future_ids: + flag = fut_emoji return f"{flag} {labels[node]}" def render(root): diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py index 10e1c936688d8f..41558523a08eb8 100644 --- a/Lib/fnmatch.py +++ b/Lib/fnmatch.py @@ -11,9 +11,7 @@ """ import functools -import itertools import os -import posixpath import re __all__ = ["filter", "filterfalse", "fnmatch", "fnmatchcase", "translate"] @@ -52,34 +50,22 @@ def _compile_pattern(pat): def filter(names, pat): """Construct a list from those elements of the iterable NAMES that match PAT.""" - result = [] pat = os.path.normcase(pat) match = _compile_pattern(pat) - if os.path is posixpath: - # normcase on posix is NOP. Optimize it away from the loop. - for name in names: - if match(name): - result.append(name) - else: - for name in names: - if match(os.path.normcase(name)): - result.append(name) - return result + return [ + name for name in names + if match(os.path.normcase(name)) + ] def filterfalse(names, pat): """Construct a list from those elements of the iterable NAMES that do not match PAT.""" pat = os.path.normcase(pat) match = _compile_pattern(pat) - if os.path is posixpath: - # normcase on posix is NOP. Optimize it away from the loop. - return list(itertools.filterfalse(match, names)) - - result = [] - for name in names: - if match(os.path.normcase(name)) is None: - result.append(name) - return result + return [ + name for name in names + if not match(os.path.normcase(name)) + ] def fnmatchcase(name, pat): diff --git a/Lib/fractions.py b/Lib/fractions.py index 84e844cdd69768..e7c5105a90aeb1 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -402,7 +402,8 @@ def limit_denominator(self, max_denominator=1000000): # proved that a rational number is a best upper or lower # approximation to x if, and only if, it is a convergent or # semiconvergent of the (unique shortest) continued fraction - # associated to x. + # associated to x. See e.g. Continued Fractions by A. Ya. + # Khinchin (1964), Theorem 15. # # To find a best rational approximation with denominator <= M, # we find the best upper and lower approximations with diff --git a/Lib/getopt.py b/Lib/getopt.py index 25f3e2439b3e35..272a4bf7d0c282 100644 --- a/Lib/getopt.py +++ b/Lib/getopt.py @@ -47,11 +47,10 @@ def __str__(self): error = GetoptError # backward compatibility def getopt(args, shortopts, longopts = []): - """getopt(args, options[, long_options]) -> opts, args + """Parse command line options and parameter list. - Parses command line options and parameter list. args is the - argument list to be parsed, without the leading reference to the - running program. Typically, this means "sys.argv[1:]". shortopts + args is the argument list to be parsed, without the leading reference + to the running program. Typically, this means "sys.argv[1:]". shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon and options that accept an optional argument followed by @@ -72,7 +71,6 @@ def getopt(args, shortopts, longopts = []): string if the option has no argument. The options occur in the list in the same order in which they were found, thus allowing multiple occurrences. Long and short options may be mixed. - """ opts = [] @@ -92,7 +90,7 @@ def getopt(args, shortopts, longopts = []): return opts, args def gnu_getopt(args, shortopts, longopts = []): - """getopt(args, options[, long_options]) -> opts, args + """Parse command line options using GNU style scanning. This function works like getopt(), except that GNU style scanning mode is used by default. This means that option and non-option @@ -103,7 +101,6 @@ def gnu_getopt(args, shortopts, longopts = []): If the first character of the option string is '+', or if the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a non-option argument is encountered. - """ opts = [] diff --git a/Lib/gzip.py b/Lib/gzip.py index 3ed2055d801a3c..48c4ae4920f6d3 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -746,6 +746,11 @@ def opener(path, flags): def main(): from argparse import ArgumentParser + import runpy + + # Prevent shadowing a stdlib module from the extracted file + runpy._remove_sys_path0() + parser = ArgumentParser(description= "A simple command line interface for the `gzip` module: act like `gzip`, " "but do not delete the input file.", diff --git a/Lib/http/__init__.py b/Lib/http/__init__.py index 691b4a9a367bd0..219b3d1355b729 100644 --- a/Lib/http/__init__.py +++ b/Lib/http/__init__.py @@ -192,6 +192,7 @@ class HTTPMethod: * RFC 9110: HTTP Semantics, obsoletes 7231, which obsoleted 2616 * RFC 5789: PATCH Method for HTTP + * RFC 10008: The HTTP QUERY Method """ def __new__(cls, value, description): obj = str.__new__(cls, value) @@ -210,4 +211,5 @@ def __repr__(self): PATCH = 'PATCH', 'Apply partial modifications to a target.' POST = 'POST', 'Perform target-specific processing with the request payload.' PUT = 'PUT', 'Replace the target with the request payload.' + QUERY = 'QUERY', 'Request that the target process the request payload in a safe and idempotent manner.' TRACE = 'TRACE', 'Perform a message loop-back test along the path to the target.' diff --git a/Lib/http/client.py b/Lib/http/client.py index fe67d0f7963f78..c05d7d93302843 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -75,11 +75,17 @@ import http import io import re +import select import socket import sys import collections.abc from urllib.parse import urlsplit +try: + import ssl +except ImportError: + ssl = None + # HTTPMessage, parse_headers(), and the HTTP status code constants are # intentionally omitted for simplicity __all__ = ["HTTPResponse", "HTTPConnection", @@ -87,8 +93,8 @@ "UnknownTransferEncoding", "UnimplementedFileMode", "IncompleteRead", "InvalidURL", "ImproperConnectionState", "CannotSendRequest", "CannotSendHeader", "ResponseNotReady", - "BadStatusLine", "LineTooLong", "RemoteDisconnected", "error", - "responses"] + "BadStatusLine", "LineTooLong", "RemoteDisconnected", + "ProxyTunnelError", "error", "responses"] HTTP_PORT = 80 HTTPS_PORT = 443 @@ -119,6 +125,10 @@ # timeout window. _MAXINTERIMRESPONSES = 100 +# default number of seconds to wait for a "100 Continue" response before +# sending the body of a request with an "Expect: 100-Continue" header. +_DEFAULT_CONTINUE_TIMEOUT = 2.5 + # Data larger than this will be read in chunks, to prevent extreme # overallocation. _MIN_READ_BUF_SIZE = 1 << 20 @@ -173,7 +183,7 @@ # We always set the Content-Length header for these methods because some # servers will otherwise respond with a 411 -_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'} +_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT', 'QUERY'} def _encode(data, name='data'): @@ -341,7 +351,7 @@ def _read_status(self): raise BadStatusLine(line) return version, status, reason - def begin(self, *, _max_headers=None): + def begin(self, *, support_continue=False, _max_headers=None): if self.headers is not None: # we've already started reading the response return @@ -354,8 +364,13 @@ def begin(self, *, _max_headers=None): # read until we get a non-100 response for _ in range(_MAXINTERIMRESPONSES): version, status, reason = self._read_status() - if status != CONTINUE: + + if support_continue or status != CONTINUE: + # This is the response to return to the caller: either a + # final response (status != CONTINUE), or a 100 Continue + # the caller asked to see. break + # skip the header from the 100 response skipped_headers = _read_headers(self.fp, _max_headers) if self.debuglevel > 0: @@ -929,7 +944,9 @@ def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, self.sock = None self._buffer = [] self.__response = None + self.__pending_response = None self.__state = _CS_IDLE + self._continue_timeout = _DEFAULT_CONTINUE_TIMEOUT self._method = None self._tunnel_host = None self._tunnel_port = None @@ -941,9 +958,10 @@ def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, self._validate_host(self.host) - # This is stored as an instance variable to allow unit - # tests to replace it with a suitable mockup + # These are stored as instance variables to allow unit + # tests to replace them with a suitable mockup self._create_connection = socket.create_connection + self._select = select.select def set_tunnel(self, host, port=None, headers=None): """Set up host and port for HTTP CONNECT tunnelling. @@ -1010,11 +1028,15 @@ def _wrap_ipv6(self, ip): return ip def _tunnel(self): - if _contains_disallowed_url_pchar_re.search(self._tunnel_host): + # Check the host as it is sent: the IDNA codec normalizes the name + # first (NFKC), which maps some non-ASCII characters to ASCII ones, + # e.g. U+00A0 (NO-BREAK SPACE) to a space. + tunnel_host = self._tunnel_host.encode("idna") + if _contains_disallowed_url_pchar_re.search(tunnel_host.decode("ascii")): raise ValueError('Tunnel host can\'t contain control characters %r' % (self._tunnel_host,)) connect = b"CONNECT %s:%d %s\r\n" % ( - self._wrap_ipv6(self._tunnel_host.encode("idna")), + self._wrap_ipv6(tunnel_host), self._tunnel_port, self._http_vsn_str.encode("ascii")) headers = [connect] @@ -1045,7 +1067,8 @@ def _tunnel(self): if code != http.HTTPStatus.OK: self.close() - raise OSError(f"Tunnel connection failed: {code} {message.strip()}") + raise ProxyTunnelError(code, message.strip(), + _parse_header_lines(self._raw_proxy_headers)) finally: response.close() @@ -1088,6 +1111,10 @@ def close(self): self.sock = None sock.close() # close it manually... there may be other refs finally: + pending_response = self.__pending_response + if pending_response: + self.__pending_response = None + pending_response.close() response = self.__response if response: self.__response = None @@ -1148,7 +1175,9 @@ def _read_readable(self, readable): datablock = datablock.encode("iso-8859-1") yield datablock - def _send_output(self, message_body=None, encode_chunked=False): + def _send_output(self, message_body=None, encode_chunked=False, + expect_continue=False, + continue_timeout=_DEFAULT_CONTINUE_TIMEOUT): """Send the currently buffered request and clear the buffer. Appends an extra \\r\\n to the buffer. @@ -1160,6 +1189,34 @@ def _send_output(self, message_body=None, encode_chunked=False): self.send(msg) if message_body is not None: + if expect_continue and not self.__response: + # For a TLS socket, check for already decrypted data in the + # internal buffer of the SSLSocket. + if (ssl is not None and isinstance(self.sock, ssl.SSLSocket) + and self.sock.pending() > 0): + read_ready = True + else: + read_ready, _, _ = self._select([self.sock], [], [], + continue_timeout) + if read_ready: + if self.debuglevel > 0: + response = self.response_class(self.sock, + self.debuglevel, + method=self._method) + else: + response = self.response_class(self.sock, + method=self._method) + if self.max_response_headers is None: + response.begin(support_continue=True) + else: + response.begin( + support_continue=True, + _max_headers=self.max_response_headers) + if response.code != CONTINUE: + # Break without sending the body + self.__pending_response = response + return + response.close() # create a consistent interface to message_body if hasattr(message_body, 'read'): @@ -1203,7 +1260,8 @@ def _send_output(self, message_body=None, encode_chunked=False): self.send(b'0\r\n\r\n') def putrequest(self, method, url, skip_host=False, - skip_accept_encoding=False): + skip_accept_encoding=False, + continue_timeout=_DEFAULT_CONTINUE_TIMEOUT): """Send a request to the server. 'method' specifies an HTTP request method, e.g. 'GET'. @@ -1212,12 +1270,13 @@ def putrequest(self, method, url, skip_host=False, 'skip_accept_encoding' if True does not add automatically an 'Accept-Encoding:' header """ + if continue_timeout <= 0.0: + raise ValueError("continue_timeout must be greater than 0") # if a prior response has been completed, then forget about it. if self.__response and self.__response.isclosed(): self.__response = None - # in certain cases, we cannot issue another request on this connection. # this occurs when: # 1) we are in the process of sending a request. (_CS_REQ_STARTED) @@ -1238,6 +1297,7 @@ def putrequest(self, method, url, skip_host=False, # if self.__state == _CS_IDLE: self.__state = _CS_REQ_STARTED + self._continue_timeout = continue_timeout else: raise CannotSendRequest(self.__state) @@ -1397,14 +1457,31 @@ def endheaders(self, message_body=None, *, encode_chunked=False): self.__state = _CS_REQ_SENT else: raise CannotSendHeader() - self._send_output(message_body, encode_chunked=encode_chunked) + + expect_continue = False + if message_body is not None and not self.__response: + prefix = b'expect:' + for line in self._buffer: + if line.lower().startswith(prefix): + value = line[len(prefix):].strip().lower() + expect_continue = value == b'100-continue' + break + + self._send_output(message_body, encode_chunked=encode_chunked, + expect_continue=expect_continue, + continue_timeout=self._continue_timeout) def request(self, method, url, body=None, headers={}, *, - encode_chunked=False): + encode_chunked=False, + continue_timeout=_DEFAULT_CONTINUE_TIMEOUT): """Send a complete request to the server.""" - self._send_request(method, url, body, headers, encode_chunked) + if continue_timeout <= 0.0: + raise ValueError("continue_timeout must be greater than 0") + self._send_request(method, url, body, headers, encode_chunked, + continue_timeout) - def _send_request(self, method, url, body, headers, encode_chunked): + def _send_request(self, method, url, body, headers, encode_chunked, + continue_timeout): # Honor explicitly requested Host: and Accept-Encoding: headers. header_names = frozenset(k.lower() for k in headers) skips = {} @@ -1413,7 +1490,8 @@ def _send_request(self, method, url, body, headers, encode_chunked): if 'accept-encoding' in header_names: skips['skip_accept_encoding'] = 1 - self.putrequest(method, url, **skips) + self.putrequest(method, url, **skips, + continue_timeout=continue_timeout) # chunked encoding will happen if HTTP/1.1 is used and either # the caller passes encode_chunked=True or the following @@ -1450,11 +1528,13 @@ def _send_request(self, method, url, body, headers, encode_chunked): body = _encode(body, 'body') self.endheaders(body, encode_chunked=encode_chunked) - def getresponse(self): + def getresponse(self, support_continue=False): """Get the response from the server. If the HTTPConnection is in the correct state, returns an - instance of HTTPResponse. + instance of HTTPResponse or of whatever object is returned by the + response_class variable. The connection will wait for a response other + than code 100 ('Continue') unless support_continue is set to True. If a request has not been sent or if a previous response has not be handled, ResponseNotReady is raised. If the HTTP @@ -1485,7 +1565,10 @@ def getresponse(self): if self.__state != _CS_REQ_SENT or self.__response: raise ResponseNotReady(self.__state) - if self.debuglevel > 0: + if self.__pending_response: + response = self.__pending_response + self.__pending_response = None + elif self.debuglevel > 0: response = self.response_class(self.sock, self.debuglevel, method=self._method) else: @@ -1494,32 +1577,32 @@ def getresponse(self): try: try: if self.max_response_headers is None: - response.begin() + response.begin(support_continue=support_continue) else: - response.begin(_max_headers=self.max_response_headers) + response.begin(support_continue=support_continue, + _max_headers=self.max_response_headers) except ConnectionError: self.close() raise assert response.will_close != _UNKNOWN - self.__state = _CS_IDLE + if response.code != 100: + # Code 100 is effectively 'not a response' for this purpose + self.__state = _CS_IDLE - if response.will_close: - # this effectively passes the connection to the response - self.close() - else: - # remember this, so we can tell when it is complete - self.__response = response + if response.will_close: + # this effectively passes the connection to the response + self.close() + else: + # remember this, so we can tell when it is complete + self.__response = response return response except: response.close() raise -try: - import ssl -except ImportError: - pass -else: +if ssl is not None: + class HTTPSConnection(HTTPConnection): "This class allows communication via SSL." @@ -1617,5 +1700,15 @@ def __init__(self, *pos, **kw): BadStatusLine.__init__(self, "") ConnectionResetError.__init__(self, *pos, **kw) +class ProxyTunnelError(OSError): + def __init__(self, code, reason, headers): + OSError.__init__(self, f"Tunnel connection failed: {code} {reason}") + self.code = code + self.reason = reason + self.headers = headers + + def __reduce__(self): + return self.__class__, (self.code, self.reason, self.headers) + # for backwards compatibility error = HTTPException diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 21dd6a9a6c2f52..d87e9a90ecfcfe 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -1568,28 +1568,33 @@ def _cookie_from_cookie_tuple(self, tup, request): if expires is Absent: expires = None discard = True - elif expires <= self._now: - # Expiry date in past is request to delete cookie. This can't be - # in DefaultCookiePolicy, because can't delete cookies there. - try: - self.clear(domain, path, name) - except KeyError: - pass - _debug("Expiring cookie, domain='%s', path='%s', name='%s'", - domain, path, name) + + cookie = Cookie(version, + name, value, + port, port_specified, + domain, domain_specified, domain_initial_dot, + path, path_specified, + secure, + expires, + discard, + comment, + comment_url, + rest) + + if expires is not None and expires <= self._now: + # RFC 6265 section 5.3 ignores the cookie entirely if its Domain + # attribute does not match the request host (step 6) before an + # existing cookie is removed (steps 11-12) + if self._policy.set_ok(cookie, request): + try: + self.clear(domain, path, name) + except KeyError: + pass + _debug("Expiring cookie, domain='%s', path='%s', name='%s'", + domain, path, name) return None - return Cookie(version, - name, value, - port, port_specified, - domain, domain_specified, domain_initial_dot, - path, path_specified, - secure, - expires, - discard, - comment, - comment_url, - rest) + return cookie def _cookies_from_attrs_set(self, attrs_set, request): cookie_tuples = self._normalized_cookie_tuples(attrs_set) diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 9a6f01dfb5e69a..57ee1bd12f6ff2 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -300,20 +300,31 @@ def value(self): def coded_value(self): return self._coded_value + def _check_attribute(self, key, val): + """Normalize and validate an attribute name and value + + Raise `CookieError` if the key is not a recognized attribute name or + value contains illegal characters. Otherwise return the canonicalized + key. + """ + key = key.lower() + val_str = str(val) + if key not in self._reserved: + raise CookieError(f"Invalid attribute {key!r}") + if _has_control_character(key, val_str): + raise CookieError(f"Control characters are not allowed in cookies {key!r} {val!r}") + # Comment is quoted like a cookie value so semicolons can be escaped. + # No other attribute expects `;` in the value, so refuse it. + if key != 'comment' and ';' in val_str: + raise CookieError(f"Reserved character ';' is not allowed in cookie attribute value {key!r} {val!r}") + return key + def __setitem__(self, K, V): - K = K.lower() - if not K in self._reserved: - raise CookieError("Invalid attribute %r" % (K,)) - if _has_control_character(K, V): - raise CookieError(f"Control characters are not allowed in cookies {K!r} {V!r}") + K = self._check_attribute(K, V) dict.__setitem__(self, K, V) def setdefault(self, key, val=None): - key = key.lower() - if key not in self._reserved: - raise CookieError("Invalid attribute %r" % (key,)) - if _has_control_character(key, val): - raise CookieError("Control characters are not allowed in cookies %r %r" % (key, val,)) + key = self._check_attribute(key, val) return dict.setdefault(self, key, val) def __eq__(self, morsel): @@ -335,12 +346,7 @@ def copy(self): def update(self, values): data = {} for key, val in dict(values).items(): - key = key.lower() - if key not in self._reserved: - raise CookieError("Invalid attribute %r" % (key,)) - if _has_control_character(key, val): - raise CookieError("Control characters are not allowed in " - f"cookies {key!r} {val!r}") + key = self._check_attribute(key, val) data[key] = val dict.update(self, data) diff --git a/Lib/idlelib/idle_test/test_editmenu.py b/Lib/idlelib/idle_test/test_editmenu.py index 0d1eb9054918da..e197b1ef8faedf 100644 --- a/Lib/idlelib/idle_test/test_editmenu.py +++ b/Lib/idlelib/idle_test/test_editmenu.py @@ -23,6 +23,10 @@ def setUpClass(cls): cls.entry = tk.Entry(root) cls.tentry = ttk.Entry(root) cls.spin = tk.Spinbox(root) + # Generated events are not delivered to widgets whose window + # does not exist yet. + for widget in cls.text, cls.entry, cls.tentry, cls.spin: + widget.winfo_id() root.clipboard_clear() root.clipboard_append('two') diff --git a/Lib/idlelib/idle_test/test_redirector.py b/Lib/idlelib/idle_test/test_redirector.py index bd486d7da66010..58e7775071c19c 100644 --- a/Lib/idlelib/idle_test/test_redirector.py +++ b/Lib/idlelib/idle_test/test_redirector.py @@ -64,8 +64,10 @@ def tearDown(self): self.redir.close() def test_repr(self): # partly for 100% coverage - self.assertIn('Redirector', repr(self.redir)) - self.assertIn('Original', repr(self.orig_insert)) + self.assertEqual(repr(self.redir), + f"WidgetRedirector(Text<{self.text._w}>)") + self.assertEqual(repr(self.orig_insert), + f"OriginalCommand({self.redir!r}, 'insert')") def test_register(self): self.assertEqual(self.text.get('1.0', 'end'), '\n') diff --git a/Lib/idlelib/redirector.py b/Lib/idlelib/redirector.py index 8e2ba68d3815bf..821a45ee7982a4 100644 --- a/Lib/idlelib/redirector.py +++ b/Lib/idlelib/redirector.py @@ -48,7 +48,7 @@ def __init__(self, widget): def __repr__(self): w = self.widget - return f"{self.__class__.__name__,}({w.__class__.__name__}<{w._w}>)" + return f"{self.__class__.__name__}({w.__class__.__name__}<{w._w}>)" def close(self): "Unregister operations and revert redirection created by .__init__." @@ -143,7 +143,7 @@ def __init__(self, redir, operation): self.orig_and_operation = (redir.orig, operation) def __repr__(self): - return f"{self.__class__.__name__,}({self.redir!r}, {self.operation!r})" + return f"{self.__class__.__name__}({self.redir!r}, {self.operation!r})" def __call__(self, *args): return self.tk_call(self.orig_and_operation + args) diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 977d6bdb1c3d86..d9ec48cbc21959 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -1176,6 +1176,12 @@ def starttls(self, ssl_context=None): ssl_context = ssl._create_stdlib_context() typ, dat = self._simple_command(name) if typ == 'OK': + if any(self._readbuf): + raise self.abort('unexpected data after STARTTLS response') + # Discard received state before TLS was established + # since data could have been injected by a MITM. + self._readbuf = [] + self.untagged_responses = {} self._close_imaplib_file() self.sock = ssl_context.wrap_socket(self.sock, server_hostname=self.host) diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 2c8d482e4f6436..53b85f0dad8fcb 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1461,6 +1461,13 @@ def _find_children(self): while True: try: entry = next(scan_iterator) + except StopIteration: + break + except OSError: + # A failing scan cannot make progress; end the listing + # like _fill_cache() treats an unreadable directory. + break + try: if entry.name == _PYCACHE: continue # packages @@ -1474,9 +1481,7 @@ def _find_children(self): if entry.name.endswith(suffix) } except OSError: - pass # ignore exceptions from next(scan_iterator) and os.DirEntry - except StopIteration: - break + pass # skip entries whose os.DirEntry methods fail def discover(self, parent=None): if parent and parent.submodule_search_locations is None: diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index b11999a0f44bcc..3421c9f40ce843 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1119,6 +1119,46 @@ def is_loopback(self): return (self.network_address.is_loopback and self.broadcast_address.is_loopback) + def _network_by_offset(self, prefix, offset): + """Get the network offset from this network. + + Args: + prefix: Target prefix length. + offset: Number of networks to move (+1 or -1). + + Returns: + An IPv(4|6) Network object. + + """ + if prefix < 1 or prefix > self.max_prefixlen: + raise ValueError( + f"prefix must be between 1 and {self.max_prefixlen}" + ) + new_netmask, _ = self._make_netmask(prefix) + error = f"out of address space, cannot make another /{prefix} network" + + if prefix > self.prefixlen: + if offset < 0: + new_ip = self.network_address._ip - 1 + else: + new_ip = self.broadcast_address._ip + 1 + if new_ip < 0 or new_ip > self._ALL_ONES: + raise ValueError(error) + + new_ip &= new_netmask._ip + else: + bit_shift = self.max_prefixlen - prefix + new_ip = ( + ((new_netmask._ip & self.network_address._ip) >> bit_shift) + + offset + ) << bit_shift + if not 0 <= new_ip <= self._ALL_ONES: + raise ValueError(error) + + return self.__class__( + f"{self._string_from_ip_int(new_ip)}/{prefix}" + ) + def next_network(self, next_prefix=None): """Get the next closest network with a specific prefix. @@ -1136,31 +1176,25 @@ def next_network(self, next_prefix=None): """ if next_prefix is None: next_prefix = self.prefixlen - new_netmask = self.netmask - else: - if next_prefix < 1 or next_prefix > self.max_prefixlen: - raise ValueError( - f"next prefix must be between 1 and {self.max_prefixlen}" - ) - new_netmask, _ = self._make_netmask(next_prefix) - bit_shift = ( - self.max_prefixlen - next_prefix - if next_prefix <= self.prefixlen - else self.max_prefixlen - self.prefixlen - ) + return self._network_by_offset(next_prefix, 1) - next_ip = ( - ((new_netmask._ip & self.network_address._ip) >> bit_shift) + 1 - ) << bit_shift - if next_ip > self._ALL_ONES: - raise ValueError( - f"out of address space, cannot make another /{next_prefix} " - "network" - ) + def prev_network(self, prev_prefix=None): + """Get the previous closest network with a specific prefix. + + Args: + prev_prefix: The desired previous prefix length, if not specified the + same self.prefixlen will be used + + Returns: + An IPv(4|6) Network object of the previous closest network. + + """ + if prev_prefix is None: + prev_prefix = self.prefixlen - return self.__class__((next_ip, next_prefix)) + return self._network_by_offset(prev_prefix, -1) class _BaseConstants: diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py index ee046431845811..31f410632d4f87 100644 --- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -112,7 +112,7 @@ def py_scanstring(s, end, strict=True, char = _b[esc] except KeyError: msg = "Invalid \\escape: {0!r}".format(esc) - raise JSONDecodeError(msg, s, end) + raise JSONDecodeError(msg, s, end - 1) end += 1 else: uni = _decode_uXXXX(s, end) diff --git a/Lib/multiprocessing/shared_memory.py b/Lib/multiprocessing/shared_memory.py index b5396451d0e64a..de74a0cbbba853 100644 --- a/Lib/multiprocessing/shared_memory.py +++ b/Lib/multiprocessing/shared_memory.py @@ -186,7 +186,7 @@ def __init__(self, name=None, create=False, size=0, *, track=True): def __del__(self): try: - self.close() + self._closefd() except OSError: pass @@ -222,6 +222,11 @@ def size(self): "Size in bytes." return self._size + def _closefd(self): + if _USE_POSIX and self._fd >= 0: + os.close(self._fd) + self._fd = -1 + def close(self): """Closes access to the shared memory from this instance but does not destroy the shared memory block.""" @@ -231,9 +236,7 @@ def close(self): if self._mmap is not None: self._mmap.close() self._mmap = None - if _USE_POSIX and self._fd >= 0: - os.close(self._fd) - self._fd = -1 + self._closefd() def unlink(self): """Requests that the underlying shared memory block be destroyed. diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 0aa6cb7c52f2c6..a515dda420f705 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -516,6 +516,28 @@ def normpath(path): from nt import _getfullpathname except ImportError: # not running on Windows - mock up something sensible + def _strip_dots_and_spaces(path): + # Imitate the trailing dot and space stripping performed by the + # Windows path normalization. This is a best effort, not a + # specification. + if isinstance(path, bytes): + sep = b'\\' + dot = b'.' + dots_and_spaces = b'. ' + else: + sep = '\\' + dot = '.' + dots_and_spaces = '. ' + drive, root, tail = splitroot(path) + if not tail: + return path + comps = tail.split(sep) + for i, comp in enumerate(comps[:-1]): + if len(comp) > 1 and comp.endswith(dot) and not comp.endswith(dot*2): + comps[i] = comp[:-1] + comps[-1] = comps[-1].rstrip(dots_and_spaces) + return drive + root + sep.join(comps) + def abspath(path): """Return the absolute version of a path.""" path = os.fspath(path) @@ -525,7 +547,7 @@ def abspath(path): else: cwd = os.getcwd() path = join(cwd, path) - return normpath(path) + return _strip_dots_and_spaces(normpath(path)) else: # use native Windows method on Windows def abspath(path): diff --git a/Lib/pdb.py b/Lib/pdb.py index 114634a4469c13..13f629ae818059 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -628,11 +628,20 @@ async def set_trace_async(self, frame=None, *, commands=None): self.start_trace() + def _install_sigint_handler(self): + if self.nosigint: + return + try: + Pdb._previous_sigint_handler = \ + signal.signal(signal.SIGINT, self.sigint_handler) + except ValueError: + # signal only works in the main thread. + pass + def sigint_handler(self, signum, frame): if self.allow_kbdint: raise KeyboardInterrupt self.message("\nProgram interrupted. (Use 'cont' to resume).") - self.set_step() self.set_trace(frame) def reset(self): @@ -2045,6 +2054,7 @@ def do_until(self, arg): else: lineno = None self.set_until(self.curframe, lineno) + self._install_sigint_handler() return 1 do_unt = do_until @@ -2072,6 +2082,7 @@ def do_next(self, arg): self._print_invalid_arg(arg) return self.set_next(self.curframe) + self._install_sigint_handler() return 1 do_n = do_next @@ -2110,6 +2121,7 @@ def do_return(self, arg): self._print_invalid_arg(arg) return self.set_return(self.curframe) + self._install_sigint_handler() return 1 do_r = do_return @@ -2121,16 +2133,7 @@ def do_continue(self, arg): if arg: self._print_invalid_arg(arg) return - if not self.nosigint: - try: - Pdb._previous_sigint_handler = \ - signal.signal(signal.SIGINT, self.sigint_handler) - except ValueError: - # ValueError happens when do_continue() is invoked from - # a non-main thread in which case we just continue without - # SIGINT set. Would printing a message here (once) make - # sense? - pass + self._install_sigint_handler() self.set_continue() return 1 do_c = do_cont = do_continue diff --git a/Lib/profiling/sampling/__main__.py b/Lib/profiling/sampling/__main__.py index a45b645eae05fc..c0e022ca4d0fa7 100644 --- a/Lib/profiling/sampling/__main__.py +++ b/Lib/profiling/sampling/__main__.py @@ -58,13 +58,13 @@ def handle_permission_error(): print(WINDOWS_PERMISSION_ERROR, file=sys.stderr) else: print(GENERIC_PERMISSION_ERROR, file=sys.stderr) - sys.exit(1) if __name__ == '__main__': try: main() except PermissionError: handle_permission_error() + sys.exit(1) except SamplingUnknownProcessError as err: print(f"Tachyon cannot find the process: {err}", file=sys.stderr) sys.exit(1) diff --git a/Lib/profiling/sampling/_child_monitor.py b/Lib/profiling/sampling/_child_monitor.py index ec56f75719f9d1..7c73ec26d88a33 100644 --- a/Lib/profiling/sampling/_child_monitor.py +++ b/Lib/profiling/sampling/_child_monitor.py @@ -237,7 +237,6 @@ def _spawn_profiler_for_child(self, child_pid): cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, ) with self._lock: if self._stop_event.is_set(): diff --git a/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js b/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js index d66da0402e52ca..58e33703771fee 100644 --- a/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js +++ b/Lib/profiling/sampling/_flamegraph_assets/flamegraph.js @@ -335,7 +335,7 @@ function createPythonTooltip(data) { const childCount = d.children ? d.children.length : 0; const source = d.data.source; - const funcname = resolveString(d.data.funcname) || resolveString(d.data.name); + const funcname = escapeHtml(resolveString(d.data.funcname) || resolveString(d.data.name) || ""); const filename = resolveString(d.data.filename) || ""; const moduleName = resolveString(d.data.module) || ""; const displayName = escapeHtml(useModuleNames ? (moduleName || filename) : filename); diff --git a/Lib/profiling/sampling/binary_collector.py b/Lib/profiling/sampling/binary_collector.py index 3d6d988077cfa2..21f58daf76f3dd 100644 --- a/Lib/profiling/sampling/binary_collector.py +++ b/Lib/profiling/sampling/binary_collector.py @@ -146,7 +146,7 @@ def get_stats(self): """Get encoding statistics. Returns: - Dict with encoding statistics including repeat/full/suffix/pop-push + Dict with encoding statistics including repeat/full/suffix/reuse record counts, frames written/saved, and compression ratio. """ return self._writer.get_stats() diff --git a/Lib/profiling/sampling/cli.py b/Lib/profiling/sampling/cli.py index c5fdcd7331096b..bf5ebe1f022b91 100644 --- a/Lib/profiling/sampling/cli.py +++ b/Lib/profiling/sampling/cli.py @@ -213,6 +213,15 @@ def _parse_mode(mode_string): return mode_map[mode_string] +def _skip_idle_for_mode(mode): + """Return whether collectors should discard idle threads for a mode.""" + return mode not in ( + PROFILING_MODE_WALL, + PROFILING_MODE_ALL, + PROFILING_MODE_EXCEPTION, + ) + + def _check_process_died(process): """Check if process died and raise an error with stderr if available.""" if process.poll() is None: @@ -1186,9 +1195,7 @@ def _handle_attach(args): ) # Determine skip_idle based on mode - skip_idle = ( - mode != PROFILING_MODE_WALL if mode != PROFILING_MODE_ALL else False - ) + skip_idle = _skip_idle_for_mode(mode) output_file = None if args.format == "binary": @@ -1294,9 +1301,7 @@ def _handle_run(args): ) # Determine skip_idle based on mode - skip_idle = ( - mode != PROFILING_MODE_WALL if mode != PROFILING_MODE_ALL else False - ) + skip_idle = _skip_idle_for_mode(mode) output_file = None if args.format == "binary": @@ -1344,7 +1349,7 @@ def _handle_live_attach(args, pid): mode = _parse_mode(args.mode) # Determine skip_idle based on mode - skip_idle = mode != PROFILING_MODE_WALL + skip_idle = _skip_idle_for_mode(mode) # Create live collector with default settings collector = LiveStatsCollector( @@ -1391,7 +1396,7 @@ def _handle_live_run(args): mode = _parse_mode(args.mode) # Determine skip_idle based on mode - skip_idle = mode != PROFILING_MODE_WALL + skip_idle = _skip_idle_for_mode(mode) # Create live collector with default settings collector = LiveStatsCollector( diff --git a/Lib/profiling/sampling/collector.py b/Lib/profiling/sampling/collector.py index 1dc3656e0ebe97..3e2baa0f1a6f91 100644 --- a/Lib/profiling/sampling/collector.py +++ b/Lib/profiling/sampling/collector.py @@ -20,16 +20,13 @@ def normalize_location(location): """Normalize location to a 4-tuple format. Args: - location: tuple (lineno, end_lineno, col_offset, end_col_offset), - an integer line number, or None + location: tuple (lineno, end_lineno, col_offset, end_col_offset) or None Returns: tuple: (lineno, end_lineno, col_offset, end_col_offset) """ if location is None: return DEFAULT_LOCATION - if isinstance(location, int): - return (location, location, -1, -1) return location @@ -37,16 +34,13 @@ def extract_lineno(location): """Extract lineno from location. Args: - location: tuple (lineno, end_lineno, col_offset, end_col_offset), - an integer line number, or None + location: tuple (lineno, end_lineno, col_offset, end_col_offset) or None Returns: int: The line number (0 for synthetic frames) """ if location is None: return 0 - if isinstance(location, int): - return location return location[0] def _is_internal_frame(frame): diff --git a/Lib/profiling/sampling/gecko_collector.py b/Lib/profiling/sampling/gecko_collector.py index 2de8cce387e7f2..ba17e6e4faee98 100644 --- a/Lib/profiling/sampling/gecko_collector.py +++ b/Lib/profiling/sampling/gecko_collector.py @@ -245,10 +245,9 @@ def collect(self, stack_frames, timestamps_us=None): first_time = times[0] - # Update interval calculation - if self.sample_count > 0 and self.last_sample_time > 0: - self.interval = (times[-1] - self.last_sample_time) / self.sample_count - self.last_sample_time = times[-1] + # Binary replay can deliver batches from different threads out of + # order, so keep the latest time seen rather than the last one. + self.last_sample_time = max(self.last_sample_time, times[-1]) # Process async tasks if stack_frames and hasattr(stack_frames[0], "awaited_by"): @@ -368,11 +367,10 @@ def _record_stack_sample(self, thread_data, frames, tid, times, first_time): if self.opcodes_enabled and frames: leaf_frame = frames[0] filename, location, funcname, opcode = leaf_frame - if isinstance(location, tuple): - lineno, _, col_offset, _ = location + if location is None: + lineno, col_offset = None, -1 else: - lineno = location - col_offset = -1 + lineno, _, col_offset, _ = location current_state = (opcode, lineno, col_offset, funcname, filename) @@ -541,15 +539,11 @@ def _process_stack(self, thread_data, frames): for frame_tuple in reversed(frames): # frame_tuple is (filename, location, funcname, opcode) - # location is (lineno, end_lineno, col_offset, end_col_offset) or just lineno filename, location, funcname, opcode = frame_tuple - if isinstance(location, tuple): - lineno, end_lineno, col_offset, end_col_offset = location + if location is None: + lineno, col_offset = None, -1 else: - # Legacy format: location is just lineno - lineno = location - col_offset = -1 - end_col_offset = -1 + lineno, _, col_offset, _ = location # Get or create function func_idx = self._get_or_create_func( @@ -617,7 +611,7 @@ def _get_or_create_func(self, thread_data, filename, funcname, lineno): func_resources.append(resource_idx) if is_python: - filename_idx = self._intern_string(os.path.basename(filename)) + filename_idx = self._intern_string(filename) func_filenames.append(filename_idx) func_line_numbers.append(lineno) else: @@ -732,9 +726,6 @@ def _finalize_markers(self): def export(self, filename): """Export the profile to a Gecko JSON file.""" - if self.sample_count > 0 and self.last_sample_time > 0: - self.interval = self.last_sample_time / self.sample_count - # Spinner for progress indication spinner = itertools.cycle(['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']) stop_spinner = threading.Event() @@ -864,6 +855,8 @@ def _profile_tail(self): def _prepare_for_serialization(self): if self.exported: raise RuntimeError("GeckoCollector has already been exported") + if self.sample_count > 0 and self.last_sample_time > 0: + self.interval = self.last_sample_time / self.sample_count self._finalize_markers() for thread_data in self.threads.values(): thread_data["_spill"].prepare_read() diff --git a/Lib/profiling/sampling/live_collector/collector.py b/Lib/profiling/sampling/live_collector/collector.py index 553c856c6f8c56..a69351e4c795c4 100644 --- a/Lib/profiling/sampling/live_collector/collector.py +++ b/Lib/profiling/sampling/live_collector/collector.py @@ -313,7 +313,6 @@ def process_frames(self, frames, thread_id=None): self._seen_locations.clear() # Process each frame in the stack to track cumulative calls - # frame.location is (lineno, end_lineno, col_offset, end_col_offset), int, or None for frame in frames: lineno = extract_lineno(frame.location) location = (frame.filename, lineno, frame.funcname) diff --git a/Lib/profiling/sampling/module_utils.py b/Lib/profiling/sampling/module_utils.py index dfde2b28ab29a4..f6a859110da49b 100644 --- a/Lib/profiling/sampling/module_utils.py +++ b/Lib/profiling/sampling/module_utils.py @@ -65,14 +65,17 @@ def extract_module_name(filename, path_info): except (ValueError, OSError): return (str(filename), 'other') - # Check if it's in stdlib - if path_info['stdlib'] and file_path.is_relative_to(path_info['stdlib']): - return (_path_to_module(file_path.relative_to(path_info['stdlib'])), 'stdlib') - - # Check site-packages - for site_pkg in path_info['site_packages']: - if file_path.is_relative_to(site_pkg): - return (_path_to_module(file_path.relative_to(site_pkg)), 'site-packages') + # Check stdlib and site-packages, preferring the most specific directory + # because site-packages is usually located inside the stdlib directory + bases = [(path_info['stdlib'], 'stdlib')] + bases.extend((site_pkg, 'site-packages') + for site_pkg in path_info['site_packages']) + matches = [(base, module_type) for base, module_type in bases + if base and file_path.is_relative_to(base)] + if matches: + # max() returns the first of equal candidates, so stdlib wins ties + base, module_type = max(matches, key=lambda m: len(m[0].parts)) + return (_path_to_module(file_path.relative_to(base)), module_type) # Check other sys.path entries (project files) if not str(file_path).startswith(('<', '[')): # Skip special files diff --git a/Lib/profiling/sampling/pstats_collector.py b/Lib/profiling/sampling/pstats_collector.py index 0f31d93582b83e..66144f9fdbf192 100644 --- a/Lib/profiling/sampling/pstats_collector.py +++ b/Lib/profiling/sampling/pstats_collector.py @@ -30,7 +30,6 @@ def _process_frames(self, frames, weight=1): self._seen_locations.clear() # Process each frame in the stack to track cumulative calls - # frame.location is int, tuple (lineno, end_lineno, col_offset, end_col_offset), or None for frame in frames: lineno = extract_lineno(frame.location) location = (frame.filename, lineno, frame.funcname) diff --git a/Lib/profiling/sampling/sample.py b/Lib/profiling/sampling/sample.py index 1dff6529ade040..e50af2528e93bf 100644 --- a/Lib/profiling/sampling/sample.py +++ b/Lib/profiling/sampling/sample.py @@ -411,22 +411,22 @@ def _print_binary_stats(self, collector): repeat_samples = stats.get('repeat_samples', 0) full_records = stats.get('full_records', 0) suffix_records = stats.get('suffix_records', 0) - pop_push_records = stats.get('pop_push_records', 0) + reuse_records = stats.get('reuse_records', 0) total_records = stats.get('total_records', 0) if total_records > 0: repeat_pct = repeat_records / total_records * 100 full_pct = full_records / total_records * 100 suffix_pct = suffix_records / total_records * 100 - pop_push_pct = pop_push_records / total_records * 100 + reuse_pct = reuse_records / total_records * 100 else: - repeat_pct = full_pct = suffix_pct = pop_push_pct = 0 + repeat_pct = full_pct = suffix_pct = reuse_pct = 0 print(f" Records: {total_records:,}") print(f" RLE repeat: {repeat_records:,} ({ANSIColors.GREEN}{repeat_pct:.1f}%{ANSIColors.RESET}) [{repeat_samples:,} samples]") print(f" Full stack: {full_records:,} ({full_pct:.1f}%)") print(f" Suffix match: {suffix_records:,} ({suffix_pct:.1f}%)") - print(f" Pop-push: {pop_push_records:,} ({pop_push_pct:.1f}%)") + print(f" Stack reuse: {reuse_records:,} ({reuse_pct:.1f}%)") frames_written = stats.get('total_frames_written', 0) frames_saved = stats.get('frames_saved', 0) diff --git a/Lib/profiling/sampling/stack_collector.py b/Lib/profiling/sampling/stack_collector.py index 60061b4a2ecf11..b3fc4460792488 100644 --- a/Lib/profiling/sampling/stack_collector.py +++ b/Lib/profiling/sampling/stack_collector.py @@ -504,6 +504,11 @@ def _create_flamegraph_html(self, data): # The C encoder can exhaust the C stack independently of the # Python recursion limit. iterencode() uses the Python encoder. data_json = "".join(json.JSONEncoder().iterencode(data)) + data_json = ( + data_json.replace("<", "\\u003c") + .replace(">", "\\u003e") + .replace("&", "\\u0026") + ) template_dir = importlib.resources.files(__package__) vendor_dir = template_dir / "_vendor" diff --git a/Lib/pydoc.py b/Lib/pydoc.py index a586d35866b038..e1efe8fc0a1b63 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -2028,12 +2028,12 @@ class Helper: topics = { 'TYPES': ('types', 'STRINGS UNICODE NUMBERS SEQUENCES MAPPINGS ' - 'FUNCTIONS CLASSES MODULES FILES inspect'), + 'FUNCTIONS CLASSES MODULES inspect'), 'STRINGS': ('strings', 'str UNICODE SEQUENCES STRINGMETHODS ' 'FORMATTING TYPES'), 'STRINGMETHODS': ('string-methods', 'STRINGS FORMATTING'), 'FORMATTING': ('formatstrings', 'OPERATORS'), - 'UNICODE': ('strings', 'encodings unicode SEQUENCES STRINGMETHODS ' + 'UNICODE': ('strings', 'encodings SEQUENCES STRINGMETHODS ' 'FORMATTING TYPES'), 'NUMBERS': ('numbers', 'INTEGER FLOAT COMPLEX TYPES'), 'INTEGER': ('integers', 'int range'), diff --git a/Lib/pydoc_data/module_docs.py b/Lib/pydoc_data/module_docs.py index f5ead162492924..5a9a92410d30c2 100644 --- a/Lib/pydoc_data/module_docs.py +++ b/Lib/pydoc_data/module_docs.py @@ -1,4 +1,4 @@ -# Autogenerated by Sphinx on Tue Oct 13 08:05:49 2026 +# Autogenerated by Sphinx on Tue Nov 10 08:57:12 2026 # as part of the release process. module_docs = { @@ -48,6 +48,7 @@ 'crypt': 'crypt#module-crypt', 'csv': 'csv#module-csv', 'ctypes': 'ctypes#module-ctypes', + 'ctypes.wintypes': 'ctypes#module-ctypes.wintypes', 'curses': 'curses#module-curses', 'curses.ascii': 'curses.ascii#module-curses.ascii', 'curses.panel': 'curses.panel#module-curses.panel', diff --git a/Lib/pydoc_data/topics.py b/Lib/pydoc_data/topics.py index 7278c2dba6d219..7119825ac14e5b 100644 --- a/Lib/pydoc_data/topics.py +++ b/Lib/pydoc_data/topics.py @@ -1,4 +1,4 @@ -# Autogenerated by Sphinx on Wed Oct 14 15:38:08 2026 +# Autogenerated by Sphinx on Tue Nov 10 08:57:12 2026 # as part of the release process. topics = { @@ -2404,7 +2404,7 @@ class namespace that define a "__set_name__()" method; The last two expressions apply to totally ordered collections (e.g. to sequences, but not to sets or mappings). See also the - "@~functools.total_ordering" decorator. + "@total_ordering" decorator. * The "hash()" result should be consistent with equality. Objects that are equal should either have the same hash value, or be marked as @@ -4112,9 +4112,9 @@ def add(self, arg: T) -> None: def overly_generic[ SimpleTypeVar, - TypeVarWithDefault = int, TypeVarWithBound: int, TypeVarWithConstraints: (str, bytes), + TypeVarWithDefault = int, *SimpleTypeVarTuple = (int, float), **SimpleParamSpec = (str, bytearray), ]( @@ -4212,7 +4212,7 @@ class Bag(typing.Generic[T]): outside that scope. This is illustrated by this example: @decorator - class Bag(Base[T], arg=T): ... + class Bag[T](Base[T], arg=T): ... This is equivalent to: @@ -4959,8 +4959,8 @@ def double(x): awaitable pdb.set_trace_async(*, header=None, commands=None) - async version of "set_trace()". This function should be used inside - an async function with "await". + Asynchronous version of "set_trace()". This function should be used + inside a *coroutine function* with "await". For instance: async def f(): await pdb.set_trace_async() @@ -5019,10 +5019,11 @@ class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=Fa originate in a module that matches one of these patterns. [1] By default, Pdb sets a handler for the SIGINT signal (which is sent - when the user presses "Ctrl"-"C" on the console) when you give a - "continue" command. This allows you to break into the debugger - again by pressing "Ctrl"-"C". If you want Pdb not to touch the - SIGINT handler, set *nosigint* to true. + when the user presses "Ctrl"-"C" on the console) when you resume + execution with "continue", "next", "until", or "return". This + allows you to break into the debugger again by pressing "Ctrl"-"C". + If you want Pdb not to touch the SIGINT handler, set *nosigint* to + true. The *readrc* argument defaults to true and controls whether Pdb will load .pdbrc files from the filesystem. @@ -5065,6 +5066,9 @@ class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=Fa "pdb.set_trace()" will always stop the program at calling frame, ignoring the *skip* pattern (if any). + Changed in version 3.16: SIGINT handling also applies to "next", + "until", and "return". + run(statement, globals=None, locals=None) runeval(expression, globals=None, locals=None) runcall(function, *args, **kwds) @@ -5188,7 +5192,8 @@ class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=Fa With a *function* argument, set a break at the first executable statement within that function. *function* can be any expression - that evaluates to a function in the current namespace. + that evaluates to a function in the current namespace, or in the + form "package.module.function" if "package.module" is importable. If a second argument is present, it is an expression which must evaluate to true before the breakpoint is honored. @@ -5536,7 +5541,7 @@ class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=Fa List or jump between chained exceptions. - When using "pdb.pm()" or "Pdb.post_mortem(...)" with a chained + When using "pdb.pm()" or "pdb.post_mortem(...)" with a chained exception instead of a traceback, it allows the user to move between the chained exceptions using "exceptions" command to list exceptions, and "exceptions " to switch to that exception. @@ -11742,7 +11747,14 @@ class is used in a class pattern with positional arguments, each str.capitalize() Return a copy of the string with its first character capitalized - and the rest lowercased. + and the rest lowercased. For example: + + >>> 'PYTHON IS AMAZING'.capitalize() + 'Python is amazing' + >>> 'Njemačka starts With a non-ASCII digraph'.capitalize() + 'Njemačka starts with a non-ascii digraph' + + See also "title()". Changed in version 3.8: The first character is now put into titlecase rather than uppercase. This means that characters like @@ -11903,18 +11915,18 @@ class is used in a class pattern with positional arguments, each Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields - delimited by braces "{}". Each replacement field contains either - the numeric index of a positional argument, or the name of a + delimited by braces "{}". Each replacement field refers to a + positional argument (numbered explicitly or automatically) or a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. For example: - >>> "The sum of 1 + 2 is {0}".format(1+2) - 'The sum of 1 + 2 is 3' - >>> "The sum of {a} + {b} is {answer}".format(answer=1+2, a=1, b=2) + >>> "The sum of {} + {} is {}".format(1, 2, 1+2) 'The sum of 1 + 2 is 3' >>> "{1} expects the {0} Inquisition!".format("Spanish", "Nobody") 'Nobody expects the Spanish Inquisition!' + >>> "The sum of {a} + {b} is {answer}".format(answer=1+2, a=1, b=2) + 'The sum of 1 + 2 is 3' See Format string syntax for a description of the various formatting options that can be specified in format strings. @@ -12644,7 +12656,7 @@ class is used in a class pattern with positional arguments, each >>> titlecase("they're bill's friends.") "They're Bill's Friends." - See also "istitle()". + See also "istitle()" and "capitalize()". str.translate(table, /) @@ -12673,7 +12685,7 @@ class is used in a class pattern with positional arguments, each Return a copy of the string with all the cased characters [4] converted to uppercase. Note that "s.upper().isupper()" might be - "False" if "s" contains uncased characters or if the Unicode + "False" if "s" contains no cased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase). @@ -14221,7 +14233,8 @@ def foo(): * "__name__" is the function’s name. See "function.__name__". -* "__self__" is set to "None" (but see the next item). +* "__self__" is the module object the function was defined in or + "None" if unavailable. * "__module__" is the name of the module the function was defined in or "None" if unavailable. See "function.__module__". @@ -15124,7 +15137,7 @@ class dict(iterable, /, **kwargs) "{'jack': 4098, 'sjoerd': 4127}" or "{4098: 'jack', 4127: 'sjoerd'}" - * Use a dict comprehension: "{}", "{x: x ** 2 for x in range(10)}" + * Use a dict comprehension: "{x: x ** 2 for x in range(10)}" * Use the type constructor: "dict()", "dict([('foo', 100), ('bar', 200)])", "dict(foo=100, bar=200)" @@ -15340,9 +15353,9 @@ class dict(iterable, /, **kwargs) Return a new view of the dictionary’s values. See the documentation of view objects. - An equality comparison between one "dict.values()" view and - another will always return "False". This also applies when - comparing "dict.values()" to itself: + An equality comparison between two distinct "dict.values()" + views will always return "False", even for views of the same + dictionary: >>> d = {'a': 1} >>> d.values() == d.values() @@ -15381,8 +15394,8 @@ class dict(iterable, /, **kwargs) See also: - "frozendict" and "types.MappingProxyType" can be used to create a - read-only view of a "dict". + "types.MappingProxyType" can be used to create a read-only view + of a "dict". "frozendict" is an immutable version of "dict". See also: @@ -15540,8 +15553,7 @@ class frozendict(iterable, /, **kwargs) Similar to "dict.fromkeys()", but call again the type constructor with an initialized "frozendict" if the type is a - "frozendict" subclass or if the constructor returned a - "frozendict". + "frozendict" subclass. Added in version 3.15. ''', diff --git a/Lib/random.py b/Lib/random.py index 7db761034509d3..a6a5784e8b51a4 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -1041,18 +1041,24 @@ def _parse_args(arg_list: list[str] | None): integer: same as `--integer` float: same as `--float`""") args = parser.parse_args(arg_list) - return args, parser.format_help() + return args, parser + + +def _randint_cli(parser, n: int) -> int: + if n < 1: + parser.error(f"integer must be 1 or greater, got {n}") + return randint(1, n) def main(arg_list: list[str] | None = None) -> int | str: - args, help_text = _parse_args(arg_list) + args, parser = _parse_args(arg_list) # Explicit arguments if args.choice: return choice(args.choice) if args.integer is not None: - return randint(1, args.integer) + return _randint_cli(parser, args.integer) if args.float is not None: return uniform(0, args.float) @@ -1067,20 +1073,22 @@ def main(arg_list: list[str] | None = None) -> int | str: try: # Is it an integer? val = int(val) - return randint(1, val) except ValueError: try: # Is it a float? val = float(val) - return uniform(0, val) except ValueError: # Split in case of space-separated string: "a b c" return choice(val.split()) + else: + return uniform(0, val) + else: + return _randint_cli(parser, val) if len(args.input) >= 2: return choice(args.input) - return help_text + return parser.format_help() if __name__ == '__main__': diff --git a/Lib/re/__init__.py b/Lib/re/__init__.py index e6d29fd7a403b6..8ab77efc8ce48c 100644 --- a/Lib/re/__init__.py +++ b/Lib/re/__init__.py @@ -391,6 +391,13 @@ def _pickle(p): # experimental stuff (see python-dev discussions for details) class Scanner: + """Experimental regex-based tokenizer, not part of the documented API. + + New code is better off starting from the tokenizer example in the + documentation: + https://docs.python.org/3/library/re.html#writing-a-tokenizer + """ + def __init__(self, lexicon, flags=0): from ._constants import BRANCH, SUBPATTERN if isinstance(flags, RegexFlag): diff --git a/Lib/runpy.py b/Lib/runpy.py index a535b4f651a5ae..332e620b002543 100644 --- a/Lib/runpy.py +++ b/Lib/runpy.py @@ -234,22 +234,24 @@ def run_module(mod_name, init_globals=None, def _get_main_module_details(error=ImportError): # Helper that gives a nicer error message when attempting to # execute a zipfile or directory by invoking __main__.py - # Also moves the standard __main__ out of the way so that the - # preexisting __loader__ entry doesn't cause issues + # The module is searched only in sys.path[0] -- the executed directory + # or zipfile -- not in the whole sys.path. main_name = "__main__" kwargs = {"name": main_name} if issubclass(error, ImportError) else {} - saved_main = sys.modules[main_name] - del sys.modules[main_name] + path_name = sys.path[0] + spec = importlib.machinery.PathFinder.find_spec(main_name, [path_name]) + if spec is None or spec.loader is None: + raise error("can't find %r module in %r" % (main_name, path_name), + **kwargs) + if spec.submodule_search_locations is not None: + raise error("Cannot use package as __main__ module", **kwargs) try: - return _get_module_details(main_name) + code = spec.loader.get_code(main_name) except ImportError as exc: - if main_name in str(exc): - raise error("can't find %r module in %r" % - (main_name, sys.path[0]), - **kwargs) from exc - raise - finally: - sys.modules[main_name] = saved_main + raise error(format(exc), **kwargs) from exc + if code is None: + raise error("No code object available for %s" % main_name, **kwargs) + return main_name, spec, code def _get_code_from_file(fname, module): @@ -296,12 +298,6 @@ def run_path(path_name, init_globals=None, run_name=None): # the start of sys.path sys.path.insert(0, path_name) try: - # Here's where things are a little different from the run_module - # case. There, we only had to replace the module in sys while the - # code was running and doing so was somewhat optional. Here, we - # have no choice and we have to remove it even while we read the - # code. If we don't do this, a __loader__ attribute in the - # existing __main__ module may prevent location of the new module. mod_name, mod_spec, code = _get_main_module_details() with _TempModule(run_name) as temp_module, \ _ModifiedArgv0(path_name): @@ -315,6 +311,26 @@ def run_path(path_name, init_globals=None, run_name=None): pass +def _remove_sys_path0(): + """ + Remove sys._path0 from sys.path. Do nothing if sys._path0 is + sys._stdlib_dir or if it has already been removed. Similar to using -P + (safe path) command line option. + """ + path0 = sys._path0 + if path0 is None: + return + + if path0 == sys._stdlib_dir: + return + + # Remove all path0 copies, not only the first one. + # We are intentionally not trying to normalize & match alternate + # representations of path0, this is only intended to remove copies + # of the exact value the runtime itself may have added. + sys.path[:] = [path for path in sys.path if path != path0] + + if __name__ == "__main__": # Run the module specified as the next command line argument if len(sys.argv) < 2: diff --git a/Lib/shutil.py b/Lib/shutil.py index c24b786fb63924..f6bc8e16619e76 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -1608,6 +1608,7 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None): path. """ + cmd = os.fspath(cmd) use_bytes = isinstance(cmd, bytes) # If we're given a path with a directory part, look it up directly rather diff --git a/Lib/ssl.py b/Lib/ssl.py index df2eece13f2b13..49eec9cde0b631 100644 --- a/Lib/ssl.py +++ b/Lib/ssl.py @@ -1638,7 +1638,8 @@ def get_server_certificate(addr, ssl_version=PROTOCOL_TLS_CLIENT, ca_certs=None, timeout=_GLOBAL_DEFAULT_TIMEOUT): """Retrieve the certificate from the server at the specified address, and return it as a PEM-encoded string. - If 'ca_certs' is specified, validate the server cert against it. + If 'ca_certs' is specified, validate the server cert against it and + check that it matches the hostname. If 'ssl_version' is specified, use it in the connection attempt. If 'timeout' is specified, use it in the connection attempt. """ @@ -1650,6 +1651,7 @@ def get_server_certificate(addr, ssl_version=PROTOCOL_TLS_CLIENT, cert_reqs = CERT_NONE context = _create_stdlib_context(ssl_version, cert_reqs=cert_reqs, + check_hostname=ca_certs is not None, cafile=ca_certs) with create_connection(addr, timeout=timeout) as sock: with context.wrap_socket(sock, server_hostname=host) as sslsock: diff --git a/Lib/statistics.py b/Lib/statistics.py index eee3e6eb94e990..aaf3799716d8fb 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1776,9 +1776,11 @@ def _sqrtprod(x: float, y: float) -> float: return _sqrtprod(scale * x, scale * y) / scale return h - if not h: + subnormal_cutoff = 2.0 ** -485 # sqrt(sys.float_info.min / sys.float_info.epsilon) + if h < subnormal_cutoff: + # x * y is subnormal or underflowed, or the correction term + # below would be. Rescale non-zero inputs. if x and y: - # Non-zero inputs underflowed, so scale up, and recompute. # Scale: 1 / sqrt(sys.float_info.min * sys.float_info.epsilon) scale = 2.0 ** 537 return _sqrtprod(scale * x, scale * y) / scale diff --git a/Lib/subprocess.py b/Lib/subprocess.py index e2dac4938d57d2..3e9f58fb1f4f76 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -237,11 +237,17 @@ def __repr__(self): __del__ = Close else: - # When select or poll has indicated that the file is writable, - # we can write up to _PIPE_BUF bytes without risk of blocking. - # POSIX defines PIPE_BUF as >= 512. + # Write at most _PIPE_BUF bytes per writable event. POSIX defines + # PIPE_BUF as >= 512. Some platforms report a full pipe as writable, + # so communicate() keeps stdin non-blocking while it writes. _PIPE_BUF = getattr(select, 'PIPE_BUF', 512) + # A full pipe that is reported as writable gives no event to wait for, + # so communicate() retries the write after a delay that doubles from + # _STDIN_BACKOFF_MIN to _STDIN_BACKOFF_MAX seconds. + _STDIN_BACKOFF_MIN = 0.001 + _STDIN_BACKOFF_MAX = 0.05 + # poll/select have the advantage of not requiring any extra file # descriptor, contrarily to epoll/kqueue (also, they require a single # syscall). @@ -282,23 +288,46 @@ def _communicate_io_posix(selector, stdin, input_view, input_offset, - Appends to output_buffers lists in place """ stdin_fd = stdin.fileno() if stdin else None - - while selector.get_map(): + # Seconds to keep stdin out of the selector after a write that + # would have blocked; 0 when stdin is registered or finished. + stdin_backoff = 0 + # The previous such delay, which the next one doubles. A + # successful write resets it. + stdin_paused_for = _STDIN_BACKOFF_MIN / 2 + + while selector.get_map() or stdin_backoff: remaining = _deadline_remaining(endtime) if remaining is not None and remaining <= 0: return (input_offset, False) # Timed out - ready = selector.select(remaining) + if stdin_backoff and (remaining is None + or stdin_backoff < remaining): + ready = selector.select(stdin_backoff) + else: + ready = selector.select(remaining) # Check timeout after select (may have woken spuriously) if endtime is not None and _time() > endtime: return (input_offset, False) # Timed out + if stdin_backoff: + # The wait is over or the child made progress on another + # pipe. Either way, ask about stdin again. + selector.register(stdin, selectors.EVENT_WRITE) + stdin_paused_for, stdin_backoff = stdin_backoff, 0 + for key, events in ready: if key.fd == stdin_fd: chunk = input_view[input_offset:input_offset + _PIPE_BUF] try: input_offset += os.write(key.fd, chunk) + except BlockingIOError: + # The pipe was reported writable but is full, and + # will be reported writable again right away. Stop + # asking for a while instead of spinning. + selector.unregister(key.fd) + stdin_backoff = min(stdin_paused_for * 2, + _STDIN_BACKOFF_MAX) except BrokenPipeError: selector.unregister(key.fd) try: @@ -306,6 +335,7 @@ def _communicate_io_posix(selector, stdin, input_view, input_offset, except BrokenPipeError: pass else: + stdin_paused_for = _STDIN_BACKOFF_MIN / 2 if input_offset >= len(input_view): selector.unregister(key.fd) try: @@ -2430,17 +2460,28 @@ def _communicate(self, input, endtime, orig_timeout): stdin_to_write = (self.stdin if self.stdin and self._input and not self.stdin.closed else None) + if stdin_to_write is not None: + # poll() and select() can report a pipe as writable + # when it has no free space (gh-162016). A blocking + # write() would then hang past the timeout. + os.set_blocking(stdin_to_write.fileno(), False) + try: + new_offset, completed = _communicate_io_posix( + selector, + stdin_to_write, + input_view, + input_offset, + self._fileobj2output, + endtime, + close_on_eof=True) + finally: + # Code that uses self.stdin after communicate() returns + # or times out expects writes to it to block. + if stdin_to_write is not None and not stdin_to_write.closed: + os.set_blocking(stdin_to_write.fileno(), True) # Persist the returned offset on self so a subsequent # communicate() after a TimeoutExpired resumes mid-input # rather than re-sending bytes the child already consumed. - new_offset, completed = _communicate_io_posix( - selector, - stdin_to_write, - input_view, - input_offset, - self._fileobj2output, - endtime, - close_on_eof=True) if self._input: self._input_offset = new_offset diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 227a28766b634d..ca6aea8396aaa1 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -594,6 +594,8 @@ def _read(self, size): else: # Other decompressors have needs_input. # decompress() can buffer data internally. + if self.cmp.eof: + break if self.cmp.needs_input: cbuf = self.fileobj.read(self.bufsize) if not cbuf: @@ -2552,7 +2554,7 @@ def extractall(self, path=".", members=None, *, numeric_owner=False, continue dirpath = os.path.join(path, tarinfo.name) try: - lstat = os.lstat(dirpath) + lstat = os.lstat(dirpath.replace(r'/', os.sep)) except FileNotFoundError: self._log_no_directory_fixup(tarinfo, 'missing') continue @@ -3172,6 +3174,10 @@ def is_tarfile(name): def main(): import argparse + import runpy + + # Prevent shadowing stdlib modules from extracted files + runpy._remove_sys_path0() description = 'A simple command-line interface for tarfile module.' parser = argparse.ArgumentParser(description=description) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 82c1ee353117e9..53fea2873d2bae 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -987,10 +987,13 @@ def __init__(self, suffix=None, prefix=None, dir=None, @classmethod def _rmtree(cls, name, ignore_errors=False, repeated=False, dir_fd=None, fullname=None): - if fullname is None: - fullname = name + + # work with str paths throughout + name = _os.fsdecode(name) + fullname = name if fullname is None else _os.fsdecode(fullname) def onexc(func, path, exc, direntry=None, dir_fd=None): + path = _os.fsdecode(path) # On DragonFly BSD, UF_NOUNLINK removal fails with EISDIR, not EPERM. if isinstance(exc, (PermissionError, IsADirectoryError)): if repeated and path == name: diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 44e66707607ab5..540189147cb4d7 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -4997,6 +4997,42 @@ def test_shared_memory_pickle_unpickle_dead_object(self): with self.assertRaises(FileNotFoundError): pickle.loads(pickled_sms) + def test_shared_memory_del_with_live_buffer(self): + # gh-155003: Garbage collecting a SharedMemory must not release a + # buffer that is still in use, and must still close the file + # descriptor. + cases = { + 'buf': lambda sms: sms.buf, + 'cast': lambda sms: sms.buf.cast('i'), + } + if c_int is not None: + # Holds a buffer on the memoryview object itself (as a NumPy + # array would), so releasing the memoryview fails, not just + # closing the mmap. + cases['ctypes'] = lambda sms: (c_int * 4).from_buffer(sms.buf) + for case, make_view in cases.items(): + with self.subTest(case=case): + sms = shared_memory.SharedMemory(create=True, size=16) + name = sms.name + fd = sms._fd + sms.buf[0] = 42 + kept = make_view(sms) + with support.catch_unraisable_exception() as cm: + del sms + support.gc_collect() + self.assertIsNone(cm.unraisable) + with memoryview(kept) as mv, mv.cast('B') as view: + self.assertEqual(view[0], 42) + view[1] = 43 + if shared_memory._USE_POSIX: + with self.assertRaises(OSError): + os.fstat(fd) + other = shared_memory.SharedMemory(name) + self.assertEqual(bytes(other.buf[:2]), b'\x2a\x2b') + other.unlink() + other.close() + del kept + @warnings_helper.ignore_fork_in_thread_deprecation_warnings() def test_shared_memory_across_processes(self): # bpo-40135: don't define shared memory block's name in case of diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 92607e381db20c..323bf1921778a8 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -697,6 +697,29 @@ def test_total_seconds(self): td = timedelta(microseconds=ms) self.assertEqual(td.total_seconds(), td / timedelta(seconds=1)) + # Converting to double before division would lose the +1. + us = 2**53 + 1 + td = timedelta(microseconds=us) + self.assertEqual(td.total_seconds(), us / 1_000_000) + + def test_to_microseconds_int64_boundary(self): + us_per_day = 86_400 * 1_000_000 + max_seconds = (2**63 - 1) // 1_000_000 - 1 + first_fast = -max_seconds * 1_000_000 + last_fast = max_seconds * 1_000_000 + 999_999 + + for value in ( + timedelta.min.days * us_per_day, + -2**63 - 1, -2**63, + first_fast - 1, first_fast, + last_fast, last_fast + 1, + 2**63 - 1, 2**63, + (timedelta.max.days + 1) * us_per_day - 1, + ): + with self.subTest(value=value): + td = timedelta(microseconds=value) + self.assertEqual(td // timedelta.resolution, value) + def test_carries(self): t1 = timedelta(days=100, weeks=-7, @@ -1434,6 +1457,23 @@ def test_ordinal_conversions(self): self.assertEqual(d, self.theclass.fromordinal(n)) n += 1 + # Check the first and last days of every month in years at the + # start and the end of the 4-, 100- and 400-year cycles. + for year in (1, 3, 4, 5, 99, 100, 101, 399, 400, 401, 1899, 1900, + 1901, 1999, 2001, 2100, 2399, 2400, 9996, 9999): + isleap = year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) + n = self.theclass(year, 1, 1).toordinal() + for month, maxday in zip(range(1, 13), dim): + if month == 2 and isleap: + maxday += 1 + for day, ordinal in (1, n), (maxday, n + maxday - 1): + with self.subTest(year=year, month=month, day=day): + d = self.theclass(year, month, day) + self.assertEqual(d.toordinal(), ordinal) + self.assertEqual(d, self.theclass.fromordinal(ordinal)) + n += maxday + self.assertEqual(n, self.theclass(year, 12, 31).toordinal() + 1) + def test_extreme_ordinals(self): a = self.theclass.min a = self.theclass(a.year, a.month, a.day) # get rid of time parts @@ -1455,6 +1495,13 @@ def test_extreme_ordinals(self): self.assertRaises(ValueError, lambda: a.fromordinal(aord + 1)) + # Past the maximum, the error still reports the year of the ordinal. + for n, year in ((aord + 1, 10000), (aord + 146097 + 1, 10400), + (2**31 - 1, 5879611)): + with self.subTest(n=n): + with self.assertRaisesRegex(ValueError, rf"\b{year}\b"): + self.theclass.fromordinal(n) + b = a - timedelta(days=1) self.assertEqual(b.toordinal(), aord - 1) self.assertEqual(b, self.theclass.fromordinal(aord - 1)) @@ -1590,6 +1637,15 @@ def test_insane_fromtimestamp(self): self.assertRaises(OverflowError, self.theclass.fromtimestamp, insane) + def test_fromtimestamp_large_year(self): + timestamp = 67_767_976_249_084_800 # Year 2147483648. + try: + year = _time.localtime(timestamp).tm_year + except (OverflowError, OSError): + self.skipTest('timestamp out of range on this platform') + with self.assertRaisesRegex(ValueError, rf'\b{year}\b'): + self.theclass.fromtimestamp(timestamp) + def test_fromtimestamp_with_none_arg(self): # See gh-120268 for more details with self.assertRaises(TypeError): @@ -2243,6 +2299,10 @@ def test_fromisoformat_fails(self): '2020-W 5', # space in the week number '2020061', # 7 chars: day slice reads a 1-character tail '2020-W2', # 1-digit week number + # gh-158500: trailing characters must not be ignored + '2020010112', # YYYYMMDD followed by two characters + '2020W011xx', # YYYYWwwD followed by two characters + '20200101\u00e9', # 9 characters, but 10 bytes in UTF-8 '٢025-03-09', # Unicode characters '2009\ud80002\ud80028', # Separators are surrogate codepoints ] @@ -3984,6 +4044,11 @@ def test_fromisoformat_fails_datetime(self): '2009-04-19T12:30:45.-05:00', # Empty fraction before offset '2009-04-19T12:30:45.Z', # Empty fraction before Z '2009-04-19T12:30:45,+05:00', # Empty fraction (comma) before offset + # gh-158500: a separator must be followed by a time + '2009-04-19T', # Separator without a time + '20090419T', # Separator without a time + '2009-W16-7T', # Separator without a time + '2009W167T', # Separator without a time ] for bad_str in bad_strs: diff --git a/Lib/test/lazy_imports_all_exclude.txt b/Lib/test/lazy_imports_all_exclude.txt index 45796d1db9f0a5..f85ea5e4f301c0 100644 --- a/Lib/test/lazy_imports_all_exclude.txt +++ b/Lib/test/lazy_imports_all_exclude.txt @@ -19,21 +19,14 @@ test.test_inspect.test_inspect test___all__ test__interpreters -test_builtin -test_clinic test_crossinterp -test_datetime -test_generated_cases -test_heapq test_import test_importlib -test_json test_pkg test_profile test_profiling test_pyrepl test_subprocess test_symtable -test_tools test_type_annotations test_unittest diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py index 720b073e460127..918cda62dae2f5 100644 --- a/Lib/test/libregrtest/cmdline.py +++ b/Lib/test/libregrtest/cmdline.py @@ -180,6 +180,7 @@ def __init__(self, **kwargs) -> None: self.header = False self.failfast = False self.match_tests: TestFilter = [] + self.match_labels: TestFilter = [] self.pgo = False self.pgo_extended = False self.tsan = False @@ -300,6 +301,14 @@ def _create_parser(): group.add_argument('-i', '--ignore', metavar='PAT', dest='match_tests', action=FilterAction, const=False, help='ignore test cases and methods with glob pattern `PAT`') + group.add_argument('--label', metavar='NAME[=VALUE]', + dest='match_labels', action=FilterAction, const=True, + help='match test cases and methods with label `NAME` ' + '(optionally only if its value is `VALUE`)') + group.add_argument('--no-label', metavar='NAME[=VALUE]', + dest='match_labels', action=FilterAction, const=False, + help='ignore test cases and methods with label `NAME` ' + '(optionally only if its value is `VALUE`)') group.add_argument('--matchfile', metavar='FILENAME', dest='match_tests', action=FromFileFilterAction, const=True, diff --git a/Lib/test/libregrtest/filter.py b/Lib/test/libregrtest/filter.py index 41372e427ffd03..a190a97fd9e3ae 100644 --- a/Lib/test/libregrtest/filter.py +++ b/Lib/test/libregrtest/filter.py @@ -1,21 +1,61 @@ import itertools import operator import re +import sys # By default, don't filter tests _test_matchers = () _test_patterns = () +_match_labels = () + +# Sentinel returned by _get_label() when the test has no such label. +_no_label = object() def match_test(test): # Function used by support.run_unittest() and regrtest --list-cases + return match_test_id(test) and match_test_label(test) + +def match_test_id(test): result = False for matcher, result in reversed(_test_matchers): if matcher(test.id()): return result return not result +def match_test_label(test): + result = False + for name, value, result in reversed(_match_labels): + actual = _get_label(test, name) + if actual is _no_label: + continue + # value is None for a plain "--label name" (match any value). + if value is None or value == str(actual): + return result + return not result + +def _get_label(test, label): + attrname = f'_label_{label}' + value = getattr(test, attrname, _no_label) + if value is not _no_label: + return value + testMethod = getattr(test, test._testMethodName) + while testMethod is not None: + value = getattr(testMethod, attrname, _no_label) + if value is not _no_label: + return value + testMethod = getattr(testMethod, '__wrapped__', None) + try: + module = sys.modules[test.__class__.__module__] + except KeyError: + pass + else: + value = getattr(module, attrname, _no_label) + if value is not _no_label: + return value + return _no_label + def _is_full_match_test(pattern): # If a pattern contains at least one dot, it's considered @@ -32,8 +72,8 @@ def get_match_tests(): return _test_patterns -def set_match_tests(patterns): - global _test_matchers, _test_patterns +def set_match_tests(patterns=None, match_labels=None): + global _test_matchers, _test_patterns, _match_labels if not patterns: _test_matchers = () @@ -48,6 +88,17 @@ def set_match_tests(patterns): ] _test_patterns = patterns + if not match_labels: + _match_labels = () + else: + # "name" matches a label with any value, "name=value" matches only + # the specified value. + _match_labels = tuple( + (name, value if sep else None, result) + for label, result in match_labels + for name, sep, value in [label.partition('=')] + ) + def _compile_match_function(patterns): patterns = list(patterns) diff --git a/Lib/test/libregrtest/findtests.py b/Lib/test/libregrtest/findtests.py index 88a105488d95d9..142a4d068bc47e 100644 --- a/Lib/test/libregrtest/findtests.py +++ b/Lib/test/libregrtest/findtests.py @@ -92,9 +92,11 @@ def _list_cases(suite: unittest.TestSuite) -> None: def list_cases(tests: TestTuple, *, match_tests: TestFilter | None = None, + match_labels: TestFilter | None = None, test_dir: StrPath | None = None) -> None: support.verbose = False cases_by_module, skipped = collect_cases(tests, match_tests=match_tests, + match_labels=match_labels, test_dir=test_dir) for cases in cases_by_module.values(): for case_id in cases: @@ -112,12 +114,13 @@ class _ModuleLoadFailed(Exception): def collect_cases(tests: TestTuple, *, match_tests: TestFilter | None = None, + match_labels: TestFilter | None = None, test_dir: StrPath | None = None ) -> tuple[dict[TestName, list[str]], list[TestName]]: # Install the filter unconditionally: passing None clears any # previously installed global filter, so collection is not # affected by unrelated state in this process. - set_match_tests(match_tests) + set_match_tests(match_tests, match_labels) result: dict[TestName, list[str]] = {} skipped: list[TestName] = [] for test_name in tests: diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index 032cb94223a7e6..5505ceea465aba 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -88,6 +88,7 @@ def __init__(self, ns: Namespace, _add_python_opts: bool = False): # Select tests self.match_tests: TestFilter = ns.match_tests + self.match_labels: TestFilter = ns.match_labels self.exclude: bool = ns.exclude self.fromfile: StrPath | None = ns.fromfile self.starting_test: TestName | None = ns.start @@ -210,9 +211,11 @@ def find_tests(self, tests: TestList | None = None) -> tuple[TestTuple, TestList if self.tsan: setup_tsan_tests(self.cmdline_args) + self.want_header = True if self.tsan_parallel: setup_tsan_parallel_tests(self.cmdline_args) + self.want_header = True alltests = findtests(testdir=self.test_dir, exclude=exclude_tests) @@ -513,6 +516,7 @@ def create_run_tests(self, tests: TestTuple) -> RunTests: fail_fast=self.fail_fast, fail_env_changed=self.fail_env_changed, match_tests=self.match_tests, + match_labels=self.match_labels, match_tests_dict=None, rerun=False, forever=self.forever, @@ -818,6 +822,7 @@ def main(self, tests: TestList | None = None) -> NoReturn: elif self.want_list_cases: list_cases(selected, match_tests=self.match_tests, + match_labels=self.match_labels, test_dir=self.test_dir) else: exitcode = self.run_tests(selected, tests) diff --git a/Lib/test/libregrtest/result.py b/Lib/test/libregrtest/result.py index f8aa256be1ecfe..ba4ba4d13ea1d7 100644 --- a/Lib/test/libregrtest/result.py +++ b/Lib/test/libregrtest/result.py @@ -226,6 +226,7 @@ def get_rerun_match_tests(self) -> FilterTuple | None: def write_json_into(self, file) -> None: json.dump(self, file, cls=_EncodeTestResult) + file.flush() @staticmethod def from_json(worker_json: StrJSON) -> 'TestResult': diff --git a/Lib/test/libregrtest/results.py b/Lib/test/libregrtest/results.py index 3475f645729182..c57b82e05a5b21 100644 --- a/Lib/test/libregrtest/results.py +++ b/Lib/test/libregrtest/results.py @@ -5,9 +5,10 @@ from .runtests import RunTests from .result import State, TestResult, TestStats, Location +from .skipped import expected_to_be_skipped from .utils import ( StrPath, TestName, TestTuple, TestList, FilterDict, - printlist, count, format_duration) + printlist, count, format_duration, cleanup_test_name) # Python uses exit code 1 when an exception is not caught @@ -97,7 +98,7 @@ def get_exitcode(self, fail_env_changed: bool, fail_rerun: bool) -> int: return exitcode def accumulate_result(self, result: TestResult, runtests: RunTests) -> None: - test_name = result.test_name + test_name = cleanup_test_name(result.test_name) rerun = runtests.rerun fail_env_changed = runtests.fail_env_changed @@ -167,6 +168,7 @@ def prepare_rerun(self, *, clear: bool = True) -> tuple[TestTuple, FilterDict]: self.bad.clear() self.env_changed.clear() self.rerun_results.clear() + self.worker_bug = False return (tuple(tests), match_tests_dict) @@ -204,7 +206,19 @@ def write_junit(self, filename: StrPath) -> None: for s in ET.tostringlist(root): f.write(s) + def split_expected_skip(self, tests): + skipped = [] + expected = [] + for name in tests: + if expected_to_be_skipped(name): + expected.append(name) + else: + skipped.append(name) + return (skipped, expected) + def display_result(self, tests: TestTuple, quiet: bool, print_slowest: bool) -> None: + tests = tuple(cleanup_test_name(name) for name in tests) + ansi = get_colors() green = ansi.GREEN red = ansi.BOLD_RED @@ -221,13 +235,20 @@ def display_result(self, tests: TestTuple, quiet: bool, print_slowest: bool) -> all_tests = [] omitted = set(tests) - self.get_executed() + # Filter tests expected to be skipped + skipped, expected_skip = self.split_expected_skip(self.skipped) + if expected_skip: + print() + count_text = count(len(expected_skip), "test") + print(f"{count_text} expected to be skipped (not listed)") + # less important all_tests.append( (sorted(omitted), "test", f"{yellow}{{}} omitted:{reset}") ) if not quiet: all_tests.append( - (self.skipped, "test", f"{yellow}{{}} skipped:{reset}") + (skipped, "test", f"{yellow}{{}} skipped:{reset}") ) all_tests.append( ( diff --git a/Lib/test/libregrtest/run_workers.py b/Lib/test/libregrtest/run_workers.py index ad02d8dba7c23e..7118a67a2336c6 100644 --- a/Lib/test/libregrtest/run_workers.py +++ b/Lib/test/libregrtest/run_workers.py @@ -153,6 +153,9 @@ def _kill(self) -> None: # On OpenBSD getsid() is only allowed for a process in the # same session, so the failure means that it is not. use_killpg = True + except ProcessLookupError: + # The process completed in the meanwhile + return if use_killpg: what = f"{self} process group" diff --git a/Lib/test/libregrtest/runtests.py b/Lib/test/libregrtest/runtests.py index 525f9573ad992d..bb2e651e867111 100644 --- a/Lib/test/libregrtest/runtests.py +++ b/Lib/test/libregrtest/runtests.py @@ -81,6 +81,7 @@ class RunTests: fail_fast: bool fail_env_changed: bool match_tests: TestFilter + match_labels: TestFilter match_tests_dict: FilterDict | None rerun: bool forever: bool diff --git a/Lib/test/libregrtest/setup.py b/Lib/test/libregrtest/setup.py index 75a596c1cccc52..e1a9b231f8c15b 100644 --- a/Lib/test/libregrtest/setup.py +++ b/Lib/test/libregrtest/setup.py @@ -112,7 +112,7 @@ def setup_tests(runtests: RunTests) -> None: support.PGO = runtests.pgo support.PGO_EXTENDED = runtests.pgo_extended - set_match_tests(runtests.match_tests) + set_match_tests(runtests.match_tests, runtests.match_labels) reporter = get_reporter() reporter.run_started(runtests) diff --git a/Lib/test/libregrtest/skipped.py b/Lib/test/libregrtest/skipped.py new file mode 100644 index 00000000000000..45ecbb94d66f8d --- /dev/null +++ b/Lib/test/libregrtest/skipped.py @@ -0,0 +1,333 @@ +""" +Ruleset to check if a test is expected to be skipped. +Tests are mostly filtered using sys.platform, except a few exceptions. +""" + +import os +import sys +import sysconfig +from test import support +from .utils import cleanup_test_name + + +# On a WASI CI, the main regrtest process is run on the build Python and test +# worker processes are run on the host Python. Detect WASI using +# _PYTHON_HOST_PLATFORM environment variable. Below, IS_WASI should be checked +# explicitly since test.support constants (such as support.has_socket_support) +# are set by the build Python which is different. +HOST_PLATFORM = os.environ.get('_PYTHON_HOST_PLATFORM', '') +IS_WASI = support.is_wasi or HOST_PLATFORM.startswith('wasi') + + +NEED_MULTIPROCESSING = { + 'test_multiprocessing_fork', + 'test_multiprocessing_forkserver', + 'test_multiprocessing_main_handling', + 'test_multiprocessing_spawn', +} +SELECT_TESTS = { + 'test_devpoll', + 'test_epoll', + 'test_kqueue', + 'test_poll', +} +NEED_TESTMULTIPHASE = { + # Lib/test/test_importlib/util.py requires _testmultiphase + 'test_compileall', + 'test_import', + 'test_pkgutil', + 'test_pyclbr', + 'test_runpy', +} +NEED_WORKING_THREADING = { + 'test_concurrent_futures', + 'test_queue', + 'test_thread', + 'test_threadedtempfile', + 'test_threading', + 'test_threading_local', + 'test_webbrowser', +} +NEED_TKINTER = { + 'test_idle', + 'test_tcl', + 'test_tkinter', + 'test_ttk', + 'test_ttk_textonly', + 'test_turtle', +} + + +# List of tests expected to be skipped on sys.platform +EXPECTED_SKIP = set() +if support.Py_GIL_DISABLED: + # Need GIL build + EXPECTED_SKIP.add('test_interpreters') +else: + # Need Free Threading build + EXPECTED_SKIP |= { + 'test_free_threading', + 'test_io.test_free_threading', + } + +if not support.has_socket_support or IS_WASI: + # Need socket support + EXPECTED_SKIP |= { + 'test_asyncgen', + 'test_asyncio', + 'test_docxmlrpc', + 'test_ftplib', + 'test_httplib', + 'test_httpservers', + 'test_imaplib', + 'test_mailbox', + 'test_poplib', + 'test_select', + 'test_selectors', + 'test_smtplib', + 'test_socket', + 'test_urllib', + 'test_urllib2', + 'test_urllib2_localnet', + 'test_urllib_response', + 'test_wsgiref', + 'test_xml_dom_xmlbuilder', + 'test_xmlrpc', + } + EXPECTED_SKIP |= SELECT_TESTS +if not support.has_subprocess_support or IS_WASI: + # Need subprocess support + EXPECTED_SKIP |= { + 'test_argparse', + 'test_asyncio.test_subprocess', + 'test_c_stack_unwind', + 'test_cext', + 'test_clinic', + 'test_cmd_line', + 'test_embed', + 'test_faulthandler', + 'test_gdb', + 'test_generated_cases', + 'test_getopt', + 'test_io.test_file_eintr', + 'test_optparse', + 'test_perf_profiler', + 'test_regrtest', + 'test_repl', + 'test_samply_profiler', + 'test_subprocess', + 'test_tools', + } +if not support.has_fork_support or IS_WASI: + # Need fork + EXPECTED_SKIP |= { + 'test_fork1', + 'test_multiprocessing_forkserver', + 'test_wait3', + 'test_wait4', + } +if not(sys.platform in ("linux", "darwin") or support.MS_WINDOWS): + # Need Py_SUPPORTS_REMOTE_DEBUG macro + EXPECTED_SKIP.add('test_remote_pdb') +if not sysconfig.is_python_build(): + # Skip tests on installed Python. + # Tests only relevant for from-source Python builds + EXPECTED_SKIP |= { + # Parser/asdl.py is not installed + 'test_asdl_parser', + # Tools/clinic/ is not installed + 'test_clinic', + # python-gdb.py is not installed + 'test_gdb', + # Tools/cases_generator is not installed + 'test_generated_cases', + } +if not sysconfig.get_config_var('WITH_DTRACE'): + # Need ./configure --with-dtrace + EXPECTED_SKIP.add('test_dtrace') + + +if support.MS_WINDOWS: + # Tests specific to non-Windows platforms + EXPECTED_SKIP |= { + 'test_asyncio.test_unix_events', # UNIX only + 'test_dbm_gnu', # Need dbm.gnu module + 'test_dbm_ndbm', # Need dbm.ndbm module + 'test_fcntl', # Need fcntl module + 'test_free_threading.test_dbm_gnu', # Need dbm.gnu module + 'test_free_threading.test_grp', # Need grp module + 'test_free_threading.test_pwd', # Need pwd module + 'test_free_threading.test_readline', # Need readline module + 'test_free_threading.test_resource', # Need resource module + 'test_free_threading.test_syslog', # Need syslog module + 'test_gdb', # test_gdb doesn't work on Windows + 'test_grp', # Need grp module + 'test_ioctl', # Need fcntl module + 'test_openpty', # Need os.openpty() + 'test_pty', # Need termios module + 'test_pwd', # Need pwd module + 'test_readline', # Need readline module + 'test_resource', # Need resource module + 'test_syslog', # Need syslog module + 'test_termios', # Need termios module + 'test_threadsignals', # Can't test signal on Windows + 'test_tty', # Need termios and tty modules + 'test_wait3', # Need os.wait3() + 'test_wait4', # Need os.wait4() + } +else: + EXPECTED_SKIP |= { + # Tests specific to Windows + 'test_asyncio.test_windows_events', + 'test_asyncio.test_windows_utils', + 'test_msvcrt', + 'test_os.test_windows', + 'test_io.test_winconsoleio', + 'test_startfile', # Need os.startfile() + 'test_winapi', + 'test_winreg', + 'test_winsound', + 'test_wmi', + } +if sys.platform in ("win32", 'darwin'): + EXPECTED_SKIP.add('test_multiprocessing_fork') +if sys.platform in ("win32", "cygwin"): + EXPECTED_SKIP.add('test_multiprocessing_forkserver') + +if not support.is_android: + # Test specific to Android + EXPECTED_SKIP.add('test_android') + + +if not support.is_apple: + # Test specific to Apple + EXPECTED_SKIP.add('test_apple') + + +if sys.platform == "darwin": + # On macOS, running gdb fails badly: + # https://github.com/python/cpython/pull/161520 + EXPECTED_SKIP.add('test_gdb') + + +if support.is_apple_mobile: + # configure disables multiple extension modules on Apple mobile + EXPECTED_SKIP |= { + 'test_curses', # need _curses + 'test_dbm_gnu', # need _gdbm + 'test_grp', # need grp + 'test_pwd', # need pwd + 'test_readline', # need readline + 'test_syslog', # need syslog + } + # configure disables _multiprocessing on Apple mobile + EXPECTED_SKIP |= NEED_MULTIPROCESSING + # configure disables _tkinter on Apple mobile + EXPECTED_SKIP |= NEED_TKINTER + + +if support.is_emscripten: + # configure disables multiple extension modules on Emscripten + EXPECTED_SKIP |= { + 'test__interpchannels', # need _interpchannels + 'test__interpreters', # need _interpreters + 'test_crossinterp', # need _interpreters + 'test_curses', # need _curses + 'test_dbm_gnu', # need _gdbm + 'test_dbm_ndbm', # need _dbm + 'test_grp', # need grp + 'test_pwd', # need pwd + 'test_readline', # need readline + 'test_resource', # need resource + 'test_syslog', # need syslog + } + # configure disables _multiprocessing on Emscripten + EXPECTED_SKIP |= NEED_MULTIPROCESSING + + # Missing os.openpty() function on Emscripten + EXPECTED_SKIP.add('test_openpty') + + EXPECTED_SKIP |= { + # Incompatible with Emscripten's mmap emulation + 'test_mmap', + # Cannot create socketpair on Emscripten/WASI + 'test_selectors', + } + + +if support.is_android or support.is_apple_mobile or support.is_wasm32 or IS_WASI: + EXPECTED_SKIP |= { + # pty is not available on these platforms + 'test_pty', + # venv is not available on these platforms + 'test_venv', + } + + +if IS_WASI: + # configure disables multiple extension modules on WASI + EXPECTED_SKIP |= { + 'test__interpchannels', # need _interpchannels + 'test__interpreters', # need _interpreters + 'test_crossinterp', # need _interpreters + 'test_curses', # need _curses + 'test_dbm_gnu', # need _gdbm + 'test_dbm_ndbm', # need _dbm + 'test_external_inspection', # need _remote_debugging + 'test_fcntl', # need fcntl + 'test_gc_stats', # need _remote_debugging + 'test_grp', # need grp + 'test_ioctl', # need fcntl + 'test_mmap', # need mmap + 'test_pwd', # need pwd + 'test_pyrepl', # need termios + 'test_remote_pdb', # need _remote_debugging + 'test_resource', # need resource + 'test_syslog', # need syslog + 'test_termios', # need termios + 'test_tty', # need termios + 'test_xxlimited', # need xxlimited + } + # configure disables _multiprocessing and _testmultiphase + # extension modules on WASI + EXPECTED_SKIP |= NEED_MULTIPROCESSING + EXPECTED_SKIP |= NEED_TESTMULTIPHASE + + # Missing os.openpty() function on WASI + EXPECTED_SKIP.add('test_openpty') + + +def expected_to_be_skipped(test_name): + # Test EXPECTED_SKIP + test_name = cleanup_test_name(test_name) + if test_name in EXPECTED_SKIP: + return True + package = test_name.split('.')[0] + if package in EXPECTED_SKIP: + return True + + # Custom ruleset + if (test_name in NEED_WORKING_THREADING + or package in NEED_WORKING_THREADING): + # Detect whether Python can start new threads + # - wasm32-wasi does not have threading. + # - wasm32-emscripten can be compiled with or without pthread + # support (-s USE_PTHREADS / __EMSCRIPTEN_PTHREADS__). + if IS_WASI: + return True + from test.support import threading_helper + return not threading_helper.can_start_thread + + if test_name in SELECT_TESTS: + # For example, test_epoll needs select.epoll() + import select + name = test_name.removeprefix('test_') + return not hasattr(select, name) + + if test_name in ('test_perf_profiler', 'test_perfmaps', 'test_samply_profiler'): + # Need perf trampoline profiling: configure defines the + # PY_HAVE_PERF_TRAMPOLINE macro on Linux (x86-64 and AArch64) + # and on macOS. + return not support.supports_perf_trampoline_profiling() + + return False diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index b34f106a83ca85..d4921134851ecd 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -67,6 +67,10 @@ FilterDict = dict[TestName, FilterTuple] +def cleanup_test_name(name: TestName) -> TestName: + return name.removeprefix('test.') + + def format_duration(seconds: float) -> str: ms = math.ceil(seconds * 1e3) seconds, ms = divmod(ms, 1000) @@ -325,6 +329,20 @@ def noop_search_function(name): codecs.register(noop_search_function) codecs.unregister(noop_search_function) + try: + _asyncio = sys.modules['_asyncio'] + except KeyError: + pass + else: + _asyncio._clear_caches() + + try: + asyncio_coroutines = sys.modules['asyncio.coroutines'] + except KeyError: + pass + else: + asyncio_coroutines._iscoroutine_typecache.clear() + def get_build_info(): # Get most important configure and build options as a list of strings. @@ -410,6 +428,9 @@ def get_build_info(): # --with-thread-sanitizer if support.check_sanitizer(thread=True): sanitizers.append("TSAN") + # --with-thread-sanitizer + if support.check_sanitizer(function=True): + sanitizers.append("fsanitize=function") if sanitizers: build.append('+'.join(sanitizers)) @@ -658,8 +679,32 @@ def display_header(use_resources: dict[str, str | None], if process_cpu_count and process_cpu_count != cpu_count: cpu_count = f"{process_cpu_count} (process) / {cpu_count} (system)" print("== CPU count:", cpu_count) - print("== encodings: locale=%s FS=%s" - % (locale.getencoding(), sys.getfilesystemencoding())) + + encodings = [] + if _winapi is not None: + # Log ANSI and OEM code pages + ansi = f'cp{_winapi.GetACP()}' + oem = f'cp{_winapi.GetOEMCP()}' + if ansi == oem: + encodings.append(f'ansi=oem={ansi}') + else: + encodings.append(f'ansi={ansi}') + encodings.append(f'oem={oem}') + encodings.append(f'FS={sys.getfilesystemencoding()}') + else: + locale_encoding = locale.getencoding() # nl_langinfo(CODESET) + fs_encoding = sys.getfilesystemencoding() # normalized name + if locale_encoding.lower() == fs_encoding.lower(): + encodings.append(f'locale=FS={fs_encoding}') + else: + encodings.append(f'locale={locale_encoding}') + encodings.append(f'FS={fs_encoding}') + if not sys.flags.utf8_mode: + encodings.append('utf8=0') + if (not sys.flags.ignore_environment + and os.environ.get('PYTHONCOERCECLOCALE') == '0'): + encodings.append('coerce=0') + print(f"== encodings: {' '.join(encodings)}") if use_resources: text = format_resources(use_resources) @@ -697,6 +742,7 @@ def display_header(use_resources: dict[str, str | None], msan = support.check_sanitizer(memory=True) ubsan = support.check_sanitizer(ub=True) tsan = support.check_sanitizer(thread=True) + function_san = support.check_sanitizer(function=True) sanitizers = [] if asan: sanitizers.append("address") @@ -706,12 +752,14 @@ def display_header(use_resources: dict[str, str | None], sanitizers.append("undefined behavior") if tsan: sanitizers.append("thread") + if function_san: + sanitizers.append("function") if sanitizers: print(f"== sanitizers: {', '.join(sanitizers)}") for sanitizer, env_var in ( (asan, "ASAN_OPTIONS"), (msan, "MSAN_OPTIONS"), - (ubsan, "UBSAN_OPTIONS"), + (ubsan or function_san, "UBSAN_OPTIONS"), (tsan, "TSAN_OPTIONS"), ): options= os.environ.get(env_var) diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py index 47025c041daf1e..043bcf8f1c1aa3 100644 --- a/Lib/test/lock_tests.py +++ b/Lib/test/lock_tests.py @@ -566,6 +566,8 @@ def test_nan_timeout(self): evt = self.eventtype() self.assertRaises(ValueError, evt.wait, float('nan')) + # Use private attributes (event._cond._waiters) + @support.cpython_only def test_set_and_clear(self): # gh-57711: check that wait() returns true even when the event is # cleared before the waiting thread is woken up. @@ -576,8 +578,11 @@ def f(): N = 5 with Bunch(f, N): - # Threads blocked on event.wait() - wait_threads_blocked(N) + # Wait until all threads are registered as waiters in + # event.wait(). + for _ in support.sleeping_retry(support.SHORT_TIMEOUT): + if len(event._cond._waiters) >= N: + break # Threads unblocked event.set() diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index c14c50dae5bedc..a3e4c394186a49 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -340,6 +340,9 @@ def get_resource_value(resource): def requires(resource, msg=None): """Raise ResourceDenied if the specified resource is not available.""" + f = sys._getframe(1) + if f.f_globals is f.f_locals: + mark_module(f'requires_{resource}', globals=f.f_globals) if not is_resource_enabled(resource): if msg is None: msg = "Use of the %r resource not enabled" % resource @@ -462,11 +465,11 @@ def skip_if_buildbot(reason=None): return unittest.skipIf(isbuildbot, reason) def check_sanitizer(*, address=False, hwaddress=False, memory=False, ub=False, - thread=False, function=True): + thread=False, function=False): """Returns True if Python is compiled with sanitizer support""" - if not (address or hwaddress or memory or ub or thread): - raise ValueError('At least one of address, hwaddress, memory, ub or ' - 'thread must be True') + if not (address or hwaddress or memory or ub or thread or function): + raise ValueError('At least one of address, hwaddress, memory, ub, ' + 'thread or function must be True') cflags = sysconfig.get_config_var('CFLAGS') or '' @@ -517,11 +520,13 @@ def configured_with_sanitizer(name): ) -def skip_if_sanitizer(reason=None, *, address=False, memory=False, ub=False, thread=False): +def skip_if_sanitizer(reason=None, *, address=False, memory=False, ub=False, + thread=False, function=False): """Decorator raising SkipTest if running with a sanitizer active.""" if not reason: reason = 'not working with sanitizers active' - skip = check_sanitizer(address=address, memory=memory, ub=ub, thread=thread) + skip = check_sanitizer(address=address, memory=memory, ub=ub, + thread=thread, function=function) return unittest.skipIf(skip, reason) # gh-89363: True if fork() can hang if Python is built with Address Sanitizer @@ -583,35 +588,35 @@ def requires_zlib(reason='requires zlib'): import zlib except ImportError: zlib = None - return unittest.skipUnless(zlib, reason) + return skipUnless(zlib, reason, label='requires_zlib') def requires_gzip(reason='requires gzip'): try: import gzip except ImportError: gzip = None - return unittest.skipUnless(gzip, reason) + return skipUnless(gzip, reason, label='requires_gzip') def requires_bz2(reason='requires bz2'): try: import bz2 except ImportError: bz2 = None - return unittest.skipUnless(bz2, reason) + return skipUnless(bz2, reason, label='requires_bz2') def requires_lzma(reason='requires lzma'): try: import lzma except ImportError: lzma = None - return unittest.skipUnless(lzma, reason) + return skipUnless(lzma, reason, label='requires_lzma') def requires_zstd(reason='requires zstd'): try: from compression import zstd except ImportError: zstd = None - return unittest.skipUnless(zstd, reason) + return skipUnless(zstd, reason, label='requires_zstd') def has_no_debug_ranges(): try: @@ -626,7 +631,7 @@ def requires_debug_ranges(reason='requires co_positions / debug_ranges'): except unittest.SkipTest as e: skip = True reason = e.args[0] if e.args else reason - return unittest.skipIf(skip, reason) + return skipIf(skip, reason, label='requires_debug_ranges') MS_WINDOWS = (sys.platform == 'win32') @@ -675,7 +680,8 @@ def skip_wasi_stack_overflow(): ) def requires_fork(): - return unittest.skipUnless(has_fork_support, "requires working os.fork()") + return skipUnless(has_fork_support, "requires working os.fork()", + label='requires_fork') has_subprocess_support = not ( # WASM and Apple mobile platforms do not support subprocesses. @@ -691,7 +697,8 @@ def requires_fork(): def requires_subprocess(): """Used for subprocess, os.spawn calls, fd inheritance""" - return unittest.skipUnless(has_subprocess_support, "requires subprocess support") + return skipUnless(has_subprocess_support, "requires subprocess support", + label='requires_subprocess') # Emscripten's socket emulation and WASI sockets have limitations. has_socket_support = not ( @@ -699,17 +706,21 @@ def requires_subprocess(): or is_wasi ) -def requires_working_socket(*, module=False): +def requires_working_socket(*, module=False, globals=None): """Skip tests or modules that require working sockets Can be used as a function/class decorator or to skip an entire module. """ + label = 'requires_socket' msg = "requires socket support" - if module: + if module or globals is not None: + if globals is None: + globals = sys._getframe(1).f_globals + mark_module(label, globals=globals) if not has_socket_support: raise unittest.SkipTest(msg) else: - return unittest.skipUnless(has_socket_support, msg) + return skipUnless(has_socket_support, msg, label=label) @functools.cache @@ -1340,6 +1351,7 @@ def bigmemtest(size, memuse, dry_run=True): def decorator(f): from test.support import isolation + @mark('bigmemtest') @functools.wraps(f) def wrapper(self): size = wrapper.size @@ -1405,10 +1417,17 @@ def internal(*args, **kwargs): import_module('_testcapi') return test(*args, **kwargs) - return cpython_only(internal) + # gh-162144: set_nomemory() and call_with_nomemory() of _testcapi are not + # thread safe and emit warnings when using a thread sanitizer (TSan). + use_tsan = check_sanitizer(thread=True) + reason ='not working with thread sanitizer (gh-157415)' + skip_if_tsan = unittest.skipIf(use_tsan, reason) + + return cpython_only(skip_if_tsan(internal)) def bigaddrspacetest(f): """Decorator for tests that fill the address space.""" + @mark('bigaddrspacetest') @functools.wraps(f) def wrapper(self): if max_memuse < MAX_Py_ssize_t: @@ -1429,13 +1448,47 @@ def wrapper(self): def _id(obj): return obj +def mark(label, value=True, /): + """Add a label to a test method or class. Use it as a decorator. + + The optional value (``True`` by default) can be matched on the command + line with ``--label name=value``. + """ + def decorator(test): + setattr(test, f'_label_{label}', value) + return test + return decorator + +def mark_module(label, value=True, /, *, globals=None): + """Add a label to every test of a module. + + The module is the caller, unless its globals() dict is passed as the + globals argument. + """ + if globals is None: + globals = sys._getframe(1).f_globals + globals[f'_label_{label}'] = value + +def combine(*decorators): + def decorator(test): + for deco in reversed(decorators): + test = deco(test) + return test + return decorator + +def skipUnless(condition, reason, *, label): + return combine(unittest.skipUnless(condition, reason), mark(label)) + +def skipIf(condition, reason, *, label): + return combine(unittest.skipIf(condition, reason), mark(label)) + def requires_resource(resource): + label = 'requires_' + resource if resource == 'gui' and not _is_gui_available(): - return unittest.skip(_is_gui_available.reason) - if is_resource_enabled(resource): - return _id - else: - return unittest.skip("resource {0!r} is not enabled".format(resource)) + return skipUnless(False, _is_gui_available.reason, label=label) + return skipUnless(is_resource_enabled(resource), + f"resource {resource!r} is not enabled", + label=label) def cpython_only(test): """ @@ -1444,8 +1497,16 @@ def cpython_only(test): return impl_detail(cpython=True)(test) def impl_detail(msg=None, **guards): + guards, _ = _parse_guards(guards) + decorators = [] + for name in reversed(guards): + if guards[name]: + label = f'impl_detail_{name}' + else: + label = f'impl_detail_no_{name}' + decorators.append(mark(label)) if check_impl_detail(**guards): - return _id + return combine(*decorators) if msg is None: guardnames, default = _parse_guards(guards) if default: @@ -1454,7 +1515,7 @@ def impl_detail(msg=None, **guards): msg = "implementation detail specific to {0}" guardnames = sorted(guardnames.keys()) msg = msg.format(' or '.join(guardnames)) - return unittest.skip(msg) + return combine(unittest.skip(msg), *decorators) def _parse_guards(guards): # Returns a tuple ({platform_name: run_me}, default_value) @@ -3614,3 +3675,10 @@ def inject_memory_error_cm(start=0, stop=0): yield finally: _testcapi.remove_mem_hooks() + + +def supports_perf_trampoline_profiling(): + perf_trampoline = sysconfig.get_config_var("PY_HAVE_PERF_TRAMPOLINE") + if not perf_trampoline: + return False + return int(perf_trampoline) == 1 diff --git a/Lib/test/support/hypothesis_helper.py b/Lib/test/support/hypothesis_helper.py index 6249ba43f06716..769d031cfcce15 100644 --- a/Lib/test/support/hypothesis_helper.py +++ b/Lib/test/support/hypothesis_helper.py @@ -1,5 +1,7 @@ import os +from test.support import mark + try: import hypothesis except ImportError: @@ -55,3 +57,5 @@ ), ) hypothesis.settings.load_profile("cpython-local-dev") + +mark_hypothesis = mark("hypothesis") diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index e8a3d176ad6943..abe3c7c2841046 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -88,6 +88,10 @@ def import_module(name, deprecated=False, *, required_on=()): compared against sys.platform. """ with _ignore_deprecated_imports(deprecated): + f = sys._getframe(1) + if f.f_globals is f.f_locals: + from test.support import mark_module + mark_module(f'requires_{name}', globals=f.f_globals) try: return importlib.import_module(name) except ImportError as msg: @@ -119,6 +123,20 @@ def frozen_modules(enabled=True): _imp._override_frozen_modules_for_tests(0) +@contextlib.contextmanager +def force_eager_imports(): + """Force eager imports inside the context manager.""" + original_lazy_imports_filter = sys.get_lazy_imports_filter() + sys.set_lazy_imports_filter( + # Tools must be imported eagerly under this context manager: + lambda importing, imported, fromlist: False + ) + try: + yield + finally: + sys.set_lazy_imports_filter(original_lazy_imports_filter) + + @contextlib.contextmanager def multi_interp_extensions_check(enabled=True): """Force legacy modules to be allowed in subinterpreters (or not). @@ -142,6 +160,7 @@ def multi_interp_extensions_check(enabled=True): def import_fresh_module(name, fresh=(), blocked=(), *, deprecated=False, usefrozen=False, + eager_imports=True, ): """Import and return a module, deliberately bypassing sys.modules. @@ -173,7 +192,12 @@ def import_fresh_module(name, fresh=(), blocked=(), *, """ # NOTE: test_heapq, test_json and test_warnings include extra sanity checks # to make sure that this utility function is working as expected - with _ignore_deprecated_imports(deprecated): + eager_imports_context = ( + force_eager_imports + if eager_imports + else contextlib.nullcontext + ) + with eager_imports_context(), _ignore_deprecated_imports(deprecated): # Keep track of modules saved for later restoration as well # as those which just need a blocking entry removed fresh = list(fresh) diff --git a/Lib/test/support/socket_helper.py b/Lib/test/support/socket_helper.py index 66e5379d2c6e25..34451896024cce 100644 --- a/Lib/test/support/socket_helper.py +++ b/Lib/test/support/socket_helper.py @@ -147,6 +147,7 @@ def _is_ipv6_enabled(): _bind_nix_socket_error = None def skip_unless_bind_unix_socket(test): """Decorator for tests requiring a functional bind() for unix sockets.""" + test = support.mark('requires_unix_sockets')(test) if not hasattr(socket, 'AF_UNIX'): return unittest.skip('No UNIX Sockets')(test) if sys.platform == 'cygwin': diff --git a/Lib/test/support/threading_helper.py b/Lib/test/support/threading_helper.py index 31be31ac9a9dad..d391c7fcf42cf9 100644 --- a/Lib/test/support/threading_helper.py +++ b/Lib/test/support/threading_helper.py @@ -243,17 +243,21 @@ def _can_start_thread() -> bool: can_start_thread = _can_start_thread() -def requires_working_threading(*, module=False): +def requires_working_threading(*, module=False, globals=None): """Skip tests or modules that require working threading. Can be used as a function/class decorator or to skip an entire module. """ + label = 'requires_threading' msg = "requires threading support" - if module: + if module or globals is not None: + if globals is None: + globals = sys._getframe(1).f_globals + support.mark_module(label, globals=globals) if not can_start_thread: raise unittest.SkipTest(msg) else: - return unittest.skipUnless(can_start_thread, msg) + return support.skipUnless(can_start_thread, msg, label=label) def run_concurrently(worker_func, nthreads=None, args=(), kwargs={}): diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py index a47af15e6a28fb..1ff93169d05a02 100644 --- a/Lib/test/test_abc.py +++ b/Lib/test/test_abc.py @@ -556,6 +556,144 @@ class C: self.assertNotIsSubclass(C, A) self.assertNotIsSubclass(C, (A,)) + def test_subclasshook_of_subclass(self): + # The hook of a subclass of an ABC applies to the ABC too. + class A(metaclass=abc_ABCMeta): + pass + class B(A): + @classmethod + def __subclasshook__(cls, C): + if cls is B: + return 'foo' in C.__dict__ + return NotImplemented + class C: + foo = 42 + class D: + spam = 42 + self.assertIsSubclass(C, A) + self.assertIsSubclass(C, B) + self.assertNotIsSubclass(D, A) + self.assertNotIsSubclass(D, B) + self.assertIsInstance(C(), A) + self.assertNotIsInstance(D(), A) + + def test_subclasscheck_of_subclass_metaclass(self): + # A __subclasscheck__ override on the metaclass of a subclass + # is honoured when walking the subclass tree of an ABC. + class Meta(abc_ABCMeta): + def __subclasscheck__(cls, subclass): + if subclass is int: + return True + return super().__subclasscheck__(subclass) + class A(metaclass=abc_ABCMeta): + pass + class B(A, metaclass=Meta): + pass + self.assertIsSubclass(int, B) + self.assertIsSubclass(int, A) + self.assertNotIsSubclass(str, A) + self.assertIsInstance(1, A) + self.assertNotIsInstance('', A) + + def test_subclasses_override_of_subclass(self): + # A __subclasses__ override in a subclass is honoured. + class Other: + pass + class A(metaclass=abc_ABCMeta): + pass + class B(A): + @classmethod + def __subclasses__(cls): + return [Other] + self.assertIsSubclass(Other, B) + self.assertIsSubclass(Other, A) + self.assertNotIsSubclass(int, A) + + def test_registration_in_subclass_tree(self): + class A(metaclass=abc_ABCMeta): + pass + class B(A): + pass + class C(B): + pass + class D: + pass + class E(D): + pass + self.assertNotIsSubclass(E, A) + C.register(D) + self.assertIsSubclass(D, C) + self.assertIsSubclass(D, B) + self.assertIsSubclass(D, A) + self.assertIsSubclass(E, A) + self.assertIsInstance(E(), A) + self.assertNotIsSubclass(int, A) + + def test_instancecheck_class_attribute(self): + class A(metaclass=abc_ABCMeta): + pass + class B(A): + pass + class ClassProperty: + @property + def __class__(self): + return B + class GetAttribute: + def __getattribute__(self, name): + if name == '__class__': + return B + return object.__getattribute__(self, name) + class ClassAttributeNotAType: + __class__ = 42 + self.assertIsInstance(ClassProperty(), A) + self.assertIsInstance(GetAttribute(), A) + self.assertNotIsInstance(ClassProperty(), int) + with self.assertRaises(TypeError): + isinstance(ClassAttributeNotAType(), A) + + def test_cache_with_transient_classes(self): + # Classes come and go, and their addresses get reused; the + # caches must never confuse a dead class with a new one. + import gc + class A(metaclass=abc_ABCMeta): + pass + for i in range(50): + class Sub(A): + pass + class NotSub: + pass + self.assertIsSubclass(Sub, A) + self.assertNotIsSubclass(NotSub, A) + self.assertIsInstance(Sub(), A) + self.assertNotIsInstance(NotSub(), A) + A.register(NotSub) + self.assertIsSubclass(NotSub, A) + del Sub, NotSub + gc.collect() + + def test_negative_cache_not_populated_in_subclass_tree(self): + # gh-92810: checking a class against an ABC must not add it to + # the negative cache of every subclass of that ABC. + if abc_ABCMeta is not abc.ABCMeta: + self.skipTest('C implementation only') + try: + from _abc import _get_dump + except ImportError: + self.skipTest('C implementation only') + class A(metaclass=abc_ABCMeta): + pass + subclasses = [type(f'B{i}', (A,), {}) for i in range(10)] + class C: + pass + self.assertNotIsSubclass(C, A) + self.assertEqual(len(_get_dump(A)[2]), 1) + for B in subclasses: + self.assertEqual(_get_dump(B)[2], set()) + # Results are cached at the top level only. + self.assertNotIsSubclass(C, A) + self.assertNotIsSubclass(C, subclasses[0]) + self.assertEqual(len(_get_dump(subclasses[0])[2]), 1) + def test_all_new_methods_are_called(self): class A(metaclass=abc_ABCMeta): pass diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 7aba0da52462ae..bb6e5117a369c6 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -1839,6 +1839,75 @@ class TestNargsRemainder(ParserTestCase): ] +class TestNargsRemainderBoundaries(TestCase): + + def test_first_positional(self): + parser = argparse.ArgumentParser(exit_on_error=False) + parser.add_argument('--foo') + parser.add_argument('args', nargs=argparse.REMAINDER) + + for args in (['--unknown'], ['--foo', 'B', '--unknown']): + with self.subTest(args=args): + with self.assertRaisesRegex( + argparse.ArgumentError, + 'unrecognized arguments: --unknown'): + parser.parse_args(args) + self.assertEqual(parser.parse_args(['--foo', 'B']), + NS(foo='B', args=[])) + self.assertEqual(parser.parse_args(['cmd', '--foo', 'C']), + NS(foo=None, args=['cmd', '--foo', 'C'])) + self.assertEqual(parser.parse_args(['--', '--foo', 'C']), + NS(foo=None, args=['--', '--foo', 'C'])) + + def test_first_positional_in_subparser(self): + parser = argparse.ArgumentParser(exit_on_error=False) + subparser = parser.add_subparsers().add_parser('run') + subparser.add_argument('--foo', action='store_true') + subparser.add_argument('args', nargs=argparse.REMAINDER) + + with self.assertRaisesRegex(argparse.ArgumentError, + 'unrecognized arguments: --unknown'): + parser.parse_args(['run', '--unknown']) + self.assertEqual(parser.parse_args(['run', '--foo']), + NS(foo=True, args=[])) + self.assertEqual(parser.parse_args(['run', 'cmd', '--foo']), + NS(foo=False, args=['cmd', '--foo'])) + self.assertEqual(parser.parse_args(['run', '--', '--foo']), + NS(foo=False, args=['--', '--foo'])) + + def test_optional_captures_options(self): + parser = argparse.ArgumentParser() + parser.add_argument('--foo', action='store_true') + parser.add_argument('--forward', nargs=argparse.REMAINDER, + default=['fallback']) + + self.assertEqual(parser.parse_args(['--forward']), + NS(foo=False, forward=[])) + self.assertEqual(parser.parse_args(['--forward', '--foo']), + NS(foo=False, forward=['--foo'])) + + def test_optional_attached_value(self): + parser = argparse.ArgumentParser() + parser.add_argument('-f', '--forward', nargs=argparse.REMAINDER) + + for option in ('--forward=a', '-fa'): + with self.subTest(option=option): + self.assertEqual(parser.parse_known_args([option, 'b']), + (NS(forward=['a']), ['b'])) + + def test_empty_positional_ignores_default(self): + parser = argparse.ArgumentParser() + parser.add_argument('args', nargs=argparse.REMAINDER, + default=['fallback']) + self.assertEqual(parser.parse_args([]), NS(args=[])) + + def test_type_conversion_without_choices_validation(self): + parser = argparse.ArgumentParser() + parser.add_argument('args', nargs=argparse.REMAINDER, + type=int, choices=[1]) + self.assertEqual(parser.parse_args(['2']), NS(args=[2])) + + class TestOptionLike(ParserTestCase): """Tests options that may or may not be arguments""" @@ -6857,7 +6926,7 @@ def test_basic(self): self.assertEqual(bar.required, True) def test_remainder(self): - # Intermixed and remainder are incompatible + # Intermixed and positional remainder are incompatible parser = ErrorRaisingArgumentParser(prog='PROG') parser.add_argument('-z') parser.add_argument('x') @@ -6869,6 +6938,14 @@ def test_remainder(self): parser.parse_intermixed_args(argv) self.assertRegex(str(cm.exception), r'\.\.\.') + def test_optional_remainder(self): + parser = argparse.ArgumentParser() + parser.add_argument('command') + parser.add_argument('--forward', nargs=argparse.REMAINDER) + self.assertEqual( + parser.parse_intermixed_args(['cmd', '--forward', '--foo']), + NS(command='cmd', forward=['--foo'])) + def test_required_exclusive(self): # required mutually exclusive group; intermixed works fine parser = argparse.ArgumentParser(prog='PROG', exit_on_error=False) diff --git a/Lib/test/test_asyncio/test_eager_task_factory.py b/Lib/test/test_asyncio/test_eager_task_factory.py index 73496de81a58ef..05646ba373abd8 100644 --- a/Lib/test/test_asyncio/test_eager_task_factory.py +++ b/Lib/test/test_asyncio/test_eager_task_factory.py @@ -69,6 +69,20 @@ async def run(): self.assertEqual(self.run_coro(run()), 'my message') + def test_awaited_by_during_eager_step(self): + async def run(): + parent = asyncio.current_task() + + async def coro(): + self.assertEqual(asyncio.current_task()._asyncio_awaited_by, + frozenset({parent})) + + t = self.loop.create_task(coro()) + self.assertFalse(t._asyncio_awaited_by) + await t + + self.run_coro(run()) + def test_eager_completion(self): async def coro(): @@ -292,11 +306,29 @@ def setUp(self): self._current_task = asyncio.current_task asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._py_all_tasks + + futures = asyncio.futures + + self._future_add_to_awaited_by = asyncio.future_add_to_awaited_by + futures.future_add_to_awaited_by = futures._py_future_add_to_awaited_by + asyncio.future_add_to_awaited_by = futures.future_add_to_awaited_by + + self._future_discard_from_awaited_by = asyncio.future_discard_from_awaited_by + futures.future_discard_from_awaited_by = futures._py_future_discard_from_awaited_by + asyncio.future_discard_from_awaited_by = futures.future_discard_from_awaited_by return super().setUp() def tearDown(self): asyncio.current_task = asyncio.tasks.current_task = self._current_task asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks + + futures = asyncio.futures + + futures.future_discard_from_awaited_by = self._future_discard_from_awaited_by + asyncio.future_discard_from_awaited_by = self._future_discard_from_awaited_by + + futures.future_add_to_awaited_by = self._future_add_to_awaited_by + asyncio.future_add_to_awaited_by = self._future_add_to_awaited_by return super().tearDown() @@ -318,6 +350,49 @@ def tearDown(self): asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks return super().tearDown() + def test_eager_step_with_non_native_parent(self): + async def child(): + self.assertFalse(asyncio.current_task()._asyncio_awaited_by) + + async def run(): + parent = tasks._c_swap_current_task(self.loop, object()) + try: + task = self.loop.create_task(child()) + finally: + tasks._c_swap_current_task(self.loop, parent) + await task + + self.run_coro(run()) + + def test_eager_step_preserves_exception(self): + # gh-157299: check that the exception is saved and cleared before + # discarding the awaited-by edge, and restored afterwards. + # A default task can still pass with the wrong order since its hash + # is in C. Overriding __hash__ makes cleanup call back into Python. + # If the exception is still pending, this raises SystemError. + class ParentTask(self.Task): + def __hash__(self): + return super().__hash__() + + child_tasks = [] + exc = SystemExit("eager step") + + async def child(): + task = asyncio.current_task() + child_tasks.append(task) + asyncio.future_add_to_awaited_by(task, self.loop.create_future()) + raise exc + + async def parent(): + with self.assertRaises(SystemExit) as cm: + self.loop.create_task(child()) + task, = child_tasks + self.assertIs(cm.exception, exc) + self.assertIs(task.exception(), exc) + self.assertNotIn(asyncio.current_task(), task._asyncio_awaited_by) + + self.loop.run_until_complete(ParentTask(parent(), loop=self.loop)) + def test_issue105987(self): code = """if 1: from _asyncio import _swap_current_task, _set_running_loop diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 33444c76290b6b..3c4a20dddbc9c9 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -795,11 +795,19 @@ def test_create_ssl_unix_connection(self): def test_create_connection_local_addr(self): with test_utils.run_test_server() as httpd: - port = socket_helper.find_unused_port() - f = self.loop.create_connection( - lambda: MyProto(loop=self.loop), - *httpd.address, local_addr=(httpd.address[0], port)) - tr, pr = self.loop.run_until_complete(f) + # The port can be taken by another process before we bind it. + for try_count in range(5): + port = socket_helper.find_unused_port() + f = self.loop.create_connection( + lambda: MyProto(loop=self.loop), + *httpd.address, local_addr=(httpd.address[0], port)) + try: + tr, pr = self.loop.run_until_complete(f) + except OSError as ex: + if ex.errno != errno.EADDRINUSE or try_count == 4: + raise + else: + break expected = pr.transport.get_extra_info('sockname')[1] self.assertEqual(port, expected) tr.close() @@ -807,24 +815,28 @@ def test_create_connection_local_addr(self): @socket_helper.skip_if_tcp_blackhole def test_create_connection_local_addr_skip_different_family(self): # See https://github.com/python/cpython/issues/86508 - port1 = socket_helper.find_unused_port() - port2 = socket_helper.find_unused_port() - getaddrinfo_orig = self.loop.getaddrinfo + # A bound but non-listening socket reserves a port that refuses + # connections (macOS times them out immediately instead); + # find_unused_port() could be grabbed by another test. + with socket.socket() as sock: + sock.bind(('127.0.0.1', 0)) + port = sock.getsockname()[1] + getaddrinfo_orig = self.loop.getaddrinfo - async def getaddrinfo(host, port, *args, **kwargs): - if port == port2: - return [(socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 0, 0, 0)), - (socket.AF_INET, socket.SOCK_STREAM, 0, '', ('127.0.0.1', 0))] - return await getaddrinfo_orig(host, port, *args, **kwargs) + async def getaddrinfo(host, port, *args, **kwargs): + if host == 'localhost': + return [(socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 0, 0, 0)), + (socket.AF_INET, socket.SOCK_STREAM, 0, '', ('127.0.0.1', 0))] + return await getaddrinfo_orig(host, port, *args, **kwargs) - self.loop.getaddrinfo = getaddrinfo + self.loop.getaddrinfo = getaddrinfo - f = self.loop.create_connection( - lambda: MyProto(loop=self.loop), - 'localhost', port1, local_addr=('localhost', port2)) + f = self.loop.create_connection( + lambda: MyProto(loop=self.loop), + '127.0.0.1', port, local_addr=('localhost', 0)) - with self.assertRaises(OSError): - self.loop.run_until_complete(f) + with self.assertRaises((ConnectionRefusedError, TimeoutError)): + self.loop.run_until_complete(f) @socket_helper.skip_if_tcp_blackhole def test_create_connection_local_addr_nomatch_family(self): diff --git a/Lib/test/test_asyncio/test_graph.py b/Lib/test/test_asyncio/test_graph.py index 8af42a851707a5..e9e8ef2cd0471a 100644 --- a/Lib/test/test_asyncio/test_graph.py +++ b/Lib/test/test_asyncio/test_graph.py @@ -526,10 +526,32 @@ async def main(): 'T', ['s capture_test_stack', 'a deep', 'a c1'], [ - ['T', ['a main', 'a test_stack_gather'], []] + ['F', [], [ + ['T', ['a main', 'a test_stack_gather'], []], + ]], ] ]) + async def test_gather_awaited_by_different_task(self): + # gh-162061 + async def consume(fut): + await fut + + release = asyncio.Event() + child = asyncio.create_task(release.wait()) + gathered = asyncio.gather(child) + consumer = asyncio.create_task(consume(gathered), eager_start=True) + graph = asyncio.capture_call_graph(child, limit=0) + release.set() + await consumer + self.assertFalse(asyncio.capture_call_graph(child, limit=0).awaited_by) + + waiter, = graph.awaited_by + self.assertIs(waiter.future, gathered) + parent, = waiter.awaited_by + self.assertIs(parent.future, consumer) + self.assertFalse(parent.awaited_by) + async def test_stack_gather_survivor(self): # gh-157213: a child that outlives gather() must not be shown as awaited @@ -575,10 +597,79 @@ async def main(): 'T', ['s capture_test_stack', 'a deep', 'a c1'], [ - ['T', ['a main', 'a test_stack_shield'], []] + ['F', [], [ + ['T', ['a main', 'a test_stack_shield'], []], + ]], ] ]) + async def test_shield_awaited_by_different_task(self): + async def consume(fut): + await fut + + release = asyncio.Event() + child = asyncio.create_task(release.wait()) + shielded = asyncio.shield(child) + consumer = asyncio.create_task(consume(shielded), eager_start=True) + graph = asyncio.capture_call_graph(child, limit=0) + release.set() + await consumer + self.assertFalse(asyncio.capture_call_graph(child, limit=0).awaited_by) + + waiter, = graph.awaited_by + self.assertIs(waiter.future, shielded) + parent, = waiter.awaited_by + self.assertIs(parent.future, consumer) + self.assertFalse(parent.awaited_by) + + async def test_shield_indirect_consumers(self): + async def wait(fut): + await asyncio.wait([fut]) + + async def gather(fut): + await asyncio.gather(fut) + + async def add_done_callback(fut): + done = asyncio.get_running_loop().create_future() + fut.add_done_callback(done.set_result) + await done + + for consume in (wait, gather, add_done_callback): + with self.subTest(method=consume.__name__): + release = asyncio.Event() + child = asyncio.create_task(release.wait()) + shielded = asyncio.shield(child) + consumer = asyncio.create_task( + consume(shielded), eager_start=True) + graph = asyncio.capture_call_graph(child, limit=0) + release.set() + await consumer + + waiter, = graph.awaited_by + self.assertIs(waiter.future, shielded) + while waiter.awaited_by: + waiter, = waiter.awaited_by + expected = shielded if consume is add_done_callback else consumer + self.assertIs(waiter.future, expected) + + async def test_shield_discards_awaited_by_on_cancel(self): + async def consume(fut): + await fut + + release = asyncio.Event() + child = asyncio.create_task(release.wait()) + shielded = asyncio.shield(child) + consumer = asyncio.create_task(consume(shielded), eager_start=True) + consumer.cancel() + with self.assertRaises(asyncio.CancelledError): + await consumer + + self.assertFalse(child.done()) + graph = asyncio.capture_call_graph(child, limit=0) + release.set() + await child + self.assertFalse(graph.awaited_by) + async def test_stack_timeout(self): stack_for_inner = None @@ -602,7 +693,9 @@ async def main(): 'T', ['s capture_test_stack', 'a inner', 'a c1'], [ - ['T', ['a main', 'a test_stack_timeout'], []] + ['F', [], [ + ['T', ['a main', 'a test_stack_timeout'], []], + ]], ] ]) @@ -969,6 +1062,23 @@ async def main(): await main() self.assertEqual(stack[:3], ['gen', 'middle', 'main']) + def set_eager_task_factory(self): + loop = asyncio.get_running_loop() + loop.set_task_factory(asyncio.create_eager_task_factory(asyncio.Task)) + self.addCleanup(loop.set_task_factory, None) + + async def test_stack_eager_task(self): + self.set_eager_task_factory() + + async def child(): + nonlocal stack + stack = capture_test_stack() + + stack = None + await asyncio.gather(child()) + + self.assertEqual(stack[0][2], [['T', ['a test_stack_eager_task'], []]]) + @unittest.skipIf( not hasattr(asyncio.futures, "_c_future_add_to_awaited_by"), @@ -1031,6 +1141,17 @@ def setUp(self): self._Future = asyncio.Future asyncio.Future = futures.Future = futures._PyFuture + # The real _GatheringFuture subclasses the C Future, which the + # pure-Python awaited-by helpers ignore. + class _PyGatheringFuture(futures._PyFuture): + def __init__(self, children, *, loop): + super().__init__(loop=loop) + self._children = children + self._cancel_requested = False + cancel = tasks._GatheringFuture.cancel + self.enterContext(mock.patch.object(tasks, '_GatheringFuture', + _PyGatheringFuture)) + self._Task = asyncio.Task asyncio.Task = tasks.Task = tasks._PyTask diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py index 9e3c29c2987269..f59ccb2e781536 100644 --- a/Lib/test/test_asyncio/test_proactor_events.py +++ b/Lib/test/test_asyncio/test_proactor_events.py @@ -13,7 +13,6 @@ from asyncio.proactor_events import _ProactorDuplexPipeTransport from asyncio.proactor_events import _ProactorDatagramTransport from test.support import os_helper -from test.support import socket_helper from test.test_asyncio import utils as test_utils @@ -1071,9 +1070,8 @@ def run_loop(self, coro): def prepare(self): sock = self.make_socket() proto = self.MyProto(self.loop) - port = socket_helper.find_unused_port() srv_sock = self.make_socket(cleanup=False) - srv_sock.bind(('127.0.0.1', port)) + srv_sock.bind(('127.0.0.1', 0)) server = self.run_loop(self.loop.create_server( lambda: proto, sock=srv_sock)) self.run_loop(self.loop.sock_connect(sock, srv_sock.getsockname())) diff --git a/Lib/test/test_asyncio/test_sendfile.py b/Lib/test/test_asyncio/test_sendfile.py index f2c9913a131251..beeb5526296f11 100644 --- a/Lib/test/test_asyncio/test_sendfile.py +++ b/Lib/test/test_asyncio/test_sendfile.py @@ -176,9 +176,9 @@ def reduce_send_buffer_size(self, sock, transport=None): def prepare_socksendfile(self): proto = MyProto(self.loop) - port = socket_helper.find_unused_port() srv_sock = self.make_socket(cleanup=False) - srv_sock.bind((socket_helper.HOST, port)) + srv_sock.bind((socket_helper.HOST, 0)) + port = srv_sock.getsockname()[1] server = self.run_loop(self.loop.create_server( lambda: proto, sock=srv_sock)) self.reduce_receive_buffer_size(srv_sock) @@ -308,7 +308,6 @@ class SendfileMixin(SendfileBase): # Note: sendfile via SSL transport is equal to sendfile fallback def prepare_sendfile(self, *, is_ssl=False, close_after=0): - port = socket_helper.find_unused_port() srv_proto = MySendfileProto(loop=self.loop, close_after=close_after) if is_ssl: @@ -320,7 +319,8 @@ def prepare_sendfile(self, *, is_ssl=False, close_after=0): srv_ctx = None cli_ctx = None srv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - srv_sock.bind((socket_helper.HOST, port)) + srv_sock.bind((socket_helper.HOST, 0)) + port = srv_sock.getsockname()[1] server = self.run_loop(self.loop.create_server( lambda: srv_proto, sock=srv_sock, ssl=srv_ctx)) self.reduce_receive_buffer_size(srv_sock) diff --git a/Lib/test/test_asyncio/test_sock_lowlevel.py b/Lib/test/test_asyncio/test_sock_lowlevel.py index 723da648870987..23ae9fb5dba4c0 100644 --- a/Lib/test/test_asyncio/test_sock_lowlevel.py +++ b/Lib/test/test_asyncio/test_sock_lowlevel.py @@ -530,21 +530,19 @@ def test_unix_sock_client_ops(self): self._basetest_sock_recv_into(httpd, sock) def test_sock_client_fail(self): - # Make sure that we will get an unused port - address = None - try: - s = socket.socket() + # Keep the port bound but not listening so that no other process + # can take it and connections to it are refused (macOS times them + # out immediately instead). + with socket.socket() as s: s.bind(('127.0.0.1', 0)) address = s.getsockname() - finally: - s.close() - sock = socket.socket() - sock.setblocking(False) - with self.assertRaises(ConnectionRefusedError): - self.loop.run_until_complete( - self.loop.sock_connect(sock, address)) - sock.close() + sock = socket.socket() + sock.setblocking(False) + with self.assertRaises((ConnectionRefusedError, TimeoutError)): + self.loop.run_until_complete( + self.loop.sock_connect(sock, address)) + sock.close() def test_sock_accept(self): listener = socket.socket() diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 459e27250d494d..0b5de55fbca80b 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -1289,13 +1289,11 @@ async def outer(): self.assertEqual(messages, []) def _basetest_unhandled_exceptions(self, handle_echo): - port = socket_helper.find_unused_port() - messages = [] self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) - async def client(): - rd, wr = await asyncio.open_connection('localhost', port) + async def client(addr): + rd, wr = await asyncio.open_connection(*addr) wr.write(b'test msg') await wr.drain() wr.close() @@ -1303,9 +1301,9 @@ async def client(): async def main(): server = await asyncio.start_server( - handle_echo, 'localhost', port) + handle_echo, socket_helper.HOSTv4, 0) await server.start_serving() - await client() + await client(server.sockets[0].getsockname()) server.close() await server.wait_closed() @@ -1329,13 +1327,11 @@ async def handle_echo(reader, writer): def test_unhandled_exception_sync_callback(self): # An exception raised by a plain-function client_connected_cb is # reported like the coroutine case and the transport is closed. - port = socket_helper.find_unused_port() - messages = [] self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) - async def client(): - rd, wr = await asyncio.open_connection('localhost', port) + async def client(addr): + rd, wr = await asyncio.open_connection(*addr) async with asyncio.timeout(60): data = await rd.read() self.assertEqual(data, b'') # the server closed the connection @@ -1347,9 +1343,9 @@ def handle_echo(reader, writer): raise Exception('test') server = await asyncio.start_server( - handle_echo, 'localhost', port) + handle_echo, socket_helper.HOSTv4, 0) await server.start_serving() - await client() + await client(server.sockets[0].getsockname()) server.close() await server.wait_closed() @@ -1359,7 +1355,22 @@ def handle_echo(reader, writer): 'Unhandled exception in client_connected_cb') def test_open_connection_happy_eyeball_refcycles(self): - port = socket_helper.find_unused_port() + # Keep the port bound but not listening on both loopback addresses + # that "localhost" may resolve to, so connections fail. + reserved = [] + self.addCleanup(lambda: [s.close() for s in reserved]) + sock4 = socket.socket() + reserved.append(sock4) + sock4.bind((socket_helper.HOSTv4, 0)) + port = sock4.getsockname()[1] + if socket_helper.IPV6_ENABLED: + sock6 = socket.socket(socket.AF_INET6) + reserved.append(sock6) + try: + sock6.bind((socket_helper.HOSTv6, port)) + except OSError: + self.skipTest(f'cannot bind {socket_helper.HOSTv6} port {port}') + async def main(): exc = None try: diff --git a/Lib/test/test_asyncio/test_subprocess.py b/Lib/test/test_asyncio/test_subprocess.py index 6e85bdd57c9085..38c236c9441ee0 100644 --- a/Lib/test/test_asyncio/test_subprocess.py +++ b/Lib/test/test_asyncio/test_subprocess.py @@ -435,6 +435,31 @@ async def write_stdin(proc, data): self.loop.run_until_complete, coro) self.loop.run_until_complete(proc.wait()) + def test_stdin_wait_closed_cancelled(self): + # gh-162217: cancelling a task waiting in stdin.wait_closed() must + # not make pipe_connection_lost() fail with InvalidStateError. + async def main(): + proc = await asyncio.create_subprocess_exec( + *PROGRAM_CAT, + stdin=subprocess.PIPE, + stdout=subprocess.DEVNULL, + ) + try: + task = asyncio.create_task(proc.stdin.wait_closed()) + await asyncio.sleep(0) + task.cancel() + with self.assertRaises(asyncio.CancelledError): + await task + finally: + proc.stdin.close() + await proc.wait() + + messages = [] + self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) + + self.loop.run_until_complete(main()) + self.assertEqual(messages, []) + def test_communicate_ignore_broken_pipe(self): # buffer large enough to feed the whole pipe buffer large_data = b'x' * support.PIPE_MAX_SIZE diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index f16815474e2d19..ac4856bd7a64c8 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1328,6 +1328,18 @@ async def coro(): self.loop.run_until_complete(self.new_task(self.loop, coro())) + def test_gather_invalid_arg_does_not_add_awaited_by(self): + async def coro(): + task = self.new_task(self.loop, asyncio.sleep(0)) + try: + with self.assertRaises(TypeError): + asyncio.gather(task, None) + self.assertFalse(task._asyncio_awaited_by) + finally: + await task + + self.loop.run_until_complete(self.new_task(self.loop, coro())) + def test_gather_discards_awaited_by_for_pending(self): # gh-157213: a child outliving gather() must lose the awaited-by edge async def fail(): diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index c383a3bff962d7..f54e310a8e924b 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -563,9 +563,9 @@ def run_loop(self, coro): def prepare(self): sock = self.make_socket() proto = self.MyProto(self.loop) - port = socket_helper.find_unused_port() srv_sock = self.make_socket(cleanup=False) - srv_sock.bind((socket_helper.HOST, port)) + srv_sock.bind((socket_helper.HOST, 0)) + port = srv_sock.getsockname()[1] server = self.run_loop(self.loop.create_server( lambda: proto, sock=srv_sock)) self.run_loop(self.loop.sock_connect(sock, (socket_helper.HOST, port))) diff --git a/Lib/test/test_augassign.py b/Lib/test/test_augassign.py index 5930d9e7a29fa2..8cef5a970a5213 100644 --- a/Lib/test/test_augassign.py +++ b/Lib/test/test_augassign.py @@ -64,6 +64,25 @@ def testSequences(self): self.assertEqual(x, [1, 2, 1, 2, 3]) self.assertTrue(x is y) + def testSequenceRepeatRightOperand(self): + # gh-161968 + class MyInt(int): + pass + class Index: + def __index__(self): + return 2 + + seq = [1] + x = MyInt(2) + x *= seq + self.assertEqual(x, [1, 1]) + # The right operand is not modified in place. + self.assertEqual(seq, [1]) + + x = Index() + x *= 'a' + self.assertEqual(x, 'aa') + def testCustomMethods1(self): class aug_test: diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py index 868abcfee24e10..e7da3f76ea9254 100644 --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -16,7 +16,7 @@ class LazyImportTest(unittest.TestCase): def test_lazy_import(self): ensure_lazy_imports("base64", {"re", "getopt"}) -from test.support.hypothesis_helper import hypothesis +from test.support.hypothesis_helper import hypothesis, mark_hypothesis class LegacyBase64TestCase(unittest.TestCase): @@ -74,6 +74,7 @@ def test_decodebytes(self): @hypothesis.given(payload=hypothesis.strategies.binary()) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz') + @mark_hypothesis def test_bytes_encode_decode_round_trip(self, payload): encoded = base64.encodebytes(payload) decoded = base64.decodebytes(encoded) @@ -109,6 +110,7 @@ def test_decode(self): @hypothesis.given(payload=hypothesis.strategies.binary()) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz') + @mark_hypothesis def test_legacy_encode_decode_round_trip(self, payload): from io import BytesIO payload_file_r = BytesIO(payload) @@ -503,6 +505,7 @@ def _altchars_strategy(): validate=hypothesis.strategies.booleans()) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', b"_-", True) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', b"_-", False) + @mark_hypothesis def test_b64_encode_decode_round_trip(self, payload, altchars, validate): encoded = base64.b64encode(payload, altchars=altchars) decoded = base64.b64decode(encoded, altchars=altchars, @@ -511,6 +514,7 @@ def test_b64_encode_decode_round_trip(self, payload, altchars, validate): @hypothesis.given(payload=hypothesis.strategies.binary()) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz') + @mark_hypothesis def test_standard_b64_encode_decode_round_trip(self, payload): encoded = base64.standard_b64encode(payload) decoded = base64.standard_b64decode(encoded) @@ -518,6 +522,7 @@ def test_standard_b64_encode_decode_round_trip(self, payload): @hypothesis.given(payload=hypothesis.strategies.binary()) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz') + @mark_hypothesis def test_urlsafe_b64_encode_decode_round_trip(self, payload): encoded = base64.urlsafe_b64encode(payload) decoded = base64.urlsafe_b64decode(encoded) @@ -701,6 +706,7 @@ def test_b32decode_error(self): | hypothesis.strategies.binary(min_size=1, max_size=1))) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', True, None) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', False, None) + @mark_hypothesis def test_b32_encode_decode_round_trip(self, payload, casefold, map01): encoded = base64.b32encode(payload) decoded = base64.b32decode(encoded, casefold=casefold, map01=map01) @@ -837,6 +843,7 @@ def test_b32hexdecode_error(self): casefold=hypothesis.strategies.booleans()) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', True) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', False) + @mark_hypothesis def test_b32_hexencode_decode_round_trip(self, payload, casefold): encoded = base64.b32hexencode(payload) decoded = base64.b32hexdecode(encoded, casefold=casefold) @@ -899,6 +906,7 @@ def test_b16decode_ignorechars(self): casefold=hypothesis.strategies.booleans()) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', True) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', False) + @mark_hypothesis def test_b16_encode_decode_round_trip(self, payload, casefold): endoded = base64.b16encode(payload) decoded = base64.b16decode(endoded, casefold=casefold) @@ -1321,6 +1329,7 @@ def add_padding(self, payload): @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', False, 20, True, True) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', True, 0, False, True) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', True, 20, True, False) + @mark_hypothesis def test_a85_encode_decode_round_trip( self, payload, foldspaces, wrapcol, pad, adobe ): @@ -1349,6 +1358,7 @@ def test_a85_encode_decode_round_trip( pad=hypothesis.strategies.booleans()) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', True) @hypothesis.example(b'abcdefghijklmnopqrstuvwxyz', False) + @mark_hypothesis def test_b85_encode_decode_round_trip(self, payload, pad): encoded = base64.b85encode(payload, pad=pad) if pad: diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py index 77ea77ed37bdc5..94b1abd4079efd 100644 --- a/Lib/test/test_binascii.py +++ b/Lib/test/test_binascii.py @@ -6,7 +6,7 @@ import re import sys from test.support import bigmemtest, _1G, _4G, check_impl_detail -from test.support.hypothesis_helper import hypothesis +from test.support.hypothesis_helper import hypothesis, mark_hypothesis # Note: "*_hex" functions are aliases for "(un)hexlify" @@ -420,6 +420,7 @@ def test_base64_canonical(self): @hypothesis.example(b'\x00') @hypothesis.example(b'\xff\xff') @hypothesis.example(b'abc') + @mark_hypothesis def test_base64_canonical_roundtrip(self, payload): # The encoder must always produce canonical output. encoded = binascii.b2a_base64(payload, newline=False) @@ -866,6 +867,7 @@ def test_base85_canonical(self): @hypothesis.example(b'\x00') @hypothesis.example(b'\xff\xff') @hypothesis.example(b'abc') + @mark_hypothesis def test_base85_canonical_roundtrip(self, payload): encoded = binascii.b2a_base85(payload) decoded = binascii.a2b_base85(encoded, canonical=True) @@ -875,6 +877,7 @@ def test_base85_canonical_roundtrip(self, payload): @hypothesis.example(b'\x00') @hypothesis.example(b'\xff') @hypothesis.example(b'ab\x00') + @mark_hypothesis def test_base85_canonical_unique(self, payload): # For a partial group, sweeping all 85 last-digit values should # yield exactly one encoding that both decodes to the original @@ -953,6 +956,7 @@ def test_ascii85_canonical(self): @hypothesis.example(b'\x00\x00\x00\x00') # triggers z abbreviation @hypothesis.example(b'\xff\xff') @hypothesis.example(b'abc') + @mark_hypothesis def test_ascii85_canonical_roundtrip(self, payload): encoded = binascii.b2a_ascii85(payload) decoded = binascii.a2b_ascii85(encoded, canonical=True) @@ -962,6 +966,7 @@ def test_ascii85_canonical_roundtrip(self, payload): @hypothesis.example(b'\x00') @hypothesis.example(b'\xff') @hypothesis.example(b'ab\x00') + @mark_hypothesis def test_ascii85_canonical_unique(self, payload): hypothesis.assume(len(payload) % 4 != 0) canonical_enc = binascii.b2a_ascii85(payload) @@ -1189,6 +1194,7 @@ def test_base32_canonical(self): @hypothesis.example(b'\x00') @hypothesis.example(b'\xff\xff') @hypothesis.example(b'abc') + @mark_hypothesis def test_base32_canonical_roundtrip(self, payload): encoded = binascii.b2a_base32(payload) decoded = binascii.a2b_base32(encoded, canonical=True) @@ -1331,6 +1337,7 @@ def test_uu(self): binary=hypothesis.strategies.binary(max_size=45), backtick=hypothesis.strategies.booleans(), ) + @mark_hypothesis def test_b2a_roundtrip(self, binary, backtick): converted = binascii.b2a_uu(self.type2test(binary), backtick=backtick) restored = binascii.a2b_uu(self.type2test(converted)) @@ -1374,6 +1381,7 @@ def test_hex(self): self.assertEqual(binascii.unhexlify(self.type2test(t)), u) @hypothesis.given(binary=hypothesis.strategies.binary()) + @mark_hypothesis def test_hex_roundtrip(self, binary): converted = binascii.hexlify(self.type2test(binary)) restored = binascii.unhexlify(self.type2test(converted)) @@ -1527,6 +1535,7 @@ def test_qp(self): istext=hypothesis.strategies.booleans(), header=hypothesis.strategies.booleans(), ) + @mark_hypothesis def test_b2a_qp_a2b_qp_round_trip(self, binary, quotetabs, istext, header): converted = binascii.b2a_qp( self.type2test(binary), @@ -1624,6 +1633,7 @@ def test_b2a_base64_wrapcol(self): binary=hypothesis.strategies.binary(), newline=hypothesis.strategies.booleans(), ) + @mark_hypothesis def test_base64_roundtrip(self, binary, newline): converted = binascii.b2a_base64(self.type2test(binary), newline=newline) restored = binascii.a2b_base64(self.type2test(converted)) diff --git a/Lib/test/test_build_details.py b/Lib/test/test_build_details.py index 12b7664d83aae1..40bf1bef11a888 100644 --- a/Lib/test/test_build_details.py +++ b/Lib/test/test_build_details.py @@ -5,11 +5,12 @@ import sys import sysconfig import string +import textwrap import unittest from pathlib import Path from unittest import mock -from test.support import is_android, is_apple_mobile, is_wasm32 +from test.support import is_android, is_apple_mobile, is_wasm32, os_helper BASE_PATH = Path( __file__, # Lib/test/test_build_details.py @@ -142,6 +143,17 @@ def contents(self): def test_location(self): self.assertTrue(os.path.isfile(self.location)) + def test_abi(self): + self.assertEqual(self.key('abi.flags'), list(sys.abiflags)) + self.assertEqual(self.key('abi.extension_suffix'), + sysconfig.get_config_var('EXT_SUFFIX')) + self.assertIn(self.key('abi.stable_abi_suffix'), + importlib.machinery.EXTENSION_SUFFIXES) + + def test_extension_suffixes(self): + self.assertEqual(self.key('suffixes.extensions'), + importlib.machinery.EXTENSION_SUFFIXES) + # Override generic format tests with tests for our specific implemenation. @needs_installed_python @@ -289,6 +301,100 @@ def check_relative_paths(self, data_abs_path, data_rel_path): self.assertEqual(data_abs_path[part][key], abs_rel_path) +def generate_data(**config_vars): + """Generate build-details data with overridden config vars.""" + get_config_var = sysconfig.get_config_var + def patched_get_config_var(name): + if name in config_vars: + return config_vars[name] + return get_config_var(name) + with mock.patch('sysconfig.get_config_var', patched_get_config_var): + data = generate_build_details.generate_data(schema_version='1.0') + return json.loads(json.dumps(data)) + + +@unittest.skipIf( + generate_build_details is None, + "Failed to import generate-build-details" +) +@unittest.skipIf(os.name != 'posix', 'Feature only implemented on POSIX') +class BuildDetailsTargetTests(unittest.TestCase): + """Values that must come from the target configuration (gh-136267).""" + + def test_native_extension_suffixes(self): + self.assertEqual(generate_build_details.get_extension_suffixes(), + importlib.machinery.EXTENSION_SUFFIXES) + + def test_free_threaded_debug(self): + # A GIL-enabled release build Python generating for this target + # used to report its own flags and suffixes. + data = generate_data( + ABIFLAGS='td', MACHDEP='linux', DYNLOADFILE='dynload_shlib.o', + SOABI_PLATFORM='aarch64-linux-gnu', + SOABI='cpython-3XXtd-aarch64-linux-gnu', + ALT_SOABI='cpython-3XXt-aarch64-linux-gnu', + EXT_SUFFIX='.cpython-3XXtd-aarch64-linux-gnu.so', + Py_GIL_DISABLED=1) + self.assertEqual(data['abi']['flags'], ['t', 'd']) + self.assertEqual(data['abi']['extension_suffix'], + '.cpython-3XXtd-aarch64-linux-gnu.so') + self.assertEqual(data['abi']['stable_abi_suffix'], + '.abi3t-aarch64-linux-gnu.so') + self.assertEqual(data['suffixes']['extensions'], [ + '.cpython-3XXtd-aarch64-linux-gnu.so', + '.cpython-3XXt-aarch64-linux-gnu.so', + '.abi3t-aarch64-linux-gnu.so', + '.abi3t.so', + '.so', + ]) + + def test_no_dynamic_loading(self): + data = generate_data(MACHDEP='wasi', DYNLOADFILE='dynload_stub.o') + self.assertEqual(data['suffixes']['extensions'], []) + self.assertNotIn('extension_suffix', data['abi']) + self.assertNotIn('stable_abi_suffix', data['abi']) + self.assertFalse( + data['implementation']['supports_isolated_interpreters']) + + +@unittest.skipIf( + generate_build_details is None, + "Failed to import generate-build-details" +) +class BuildDetailsVersionTests(unittest.TestCase): + def test_native(self): + version_info = generate_build_details.get_version_info() + self.assertEqual(version_info, + generate_build_details.version_info_to_dict( + sys.version_info)) + self.assertEqual(generate_build_details.hexversion(version_info), + sys.hexversion) + + def test_patchlevel(self): + patchlevel_h = os.path.join(os_helper.TESTFN, 'patchlevel.h') + self.addCleanup(os_helper.rmtree, os_helper.TESTFN) + os.mkdir(os_helper.TESTFN) + with open(patchlevel_h, 'w', encoding='utf-8') as f: + f.write(textwrap.dedent('''\ + #define PY_RELEASE_LEVEL_GAMMA 0xC /* For release candidates */ + #define PY_MAJOR_VERSION 3 + #define PY_MINOR_VERSION 99 + #define PY_MICRO_VERSION 7 + #define PY_RELEASE_LEVEL PY_RELEASE_LEVEL_GAMMA + #define PY_RELEASE_SERIAL 2 + #define PY_VERSION "3.99.7rc2" + ''')) + with mock.patch.object(generate_build_details, 'PATCHLEVEL_H', + patchlevel_h): + version_info = generate_build_details.get_version_info() + self.assertEqual(version_info, { + 'major': 3, 'minor': 99, 'micro': 7, + 'releaselevel': 'candidate', 'serial': 2, + }) + self.assertEqual(generate_build_details.hexversion(version_info), + 0x036307c2) + + @unittest.skipIf( generate_build_details is None, "Failed to import generate-build-details" @@ -299,26 +405,25 @@ class BuildDetailsLibpythonTests(unittest.TestCase): LIBDIR = '/prefix/lib' LIBPL = '/prefix/lib/python3.X/config-3.X' + def generate(self, **config_vars): + return generate_data(LIBDIR=self.LIBDIR, LIBPL=self.LIBPL, + LIBRARY='libpython3.X.a', **config_vars) + def generate_libpython(self, **config_vars): """Generate the libpython section with overridden config vars.""" - config_vars = dict(LIBDIR=self.LIBDIR, LIBPL=self.LIBPL, - LIBRARY='libpython3.X.a', **config_vars) - get_config_var = sysconfig.get_config_var - def patched_get_config_var(name): - if name in config_vars: - return config_vars[name] - return get_config_var(name) - with mock.patch('sysconfig.get_config_var', patched_get_config_var): - data = generate_build_details.generate_data(schema_version='1.0') - return json.loads(json.dumps(data)).get('libpython', {}) + return self.generate(**config_vars).get('libpython', {}) def test_static_only(self): - libpython = self.generate_libpython( + data = self.generate( LDLIBRARY='libpython3.X.a', STATIC_LIBPYTHON=1, PYTHONFRAMEWORK='') + libpython = data['libpython'] self.assertNotIn('dynamic', libpython) self.assertEqual(libpython['static'], os.path.join(self.LIBDIR, 'libpython3.X.a')) + # Extension modules are supported without a shared libpython. + self.assertIn('extension_suffix', data['abi']) + self.assertIn('stable_abi_suffix', data['abi']) def test_shared_and_static(self): # The static library is only installed into LIBPL, not LIBDIR. diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 501f482570ac32..d21de4adc2a98d 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1052,10 +1052,16 @@ def test_eval_builtins_mapping(self): eval, code, ns) def test_exec_builtins_mapping_import(self): + # Globally enabled lazy imports do use different import hooks: + if sys.get_lazy_imports() == "all": + hook = "__lazy_import__" + else: + hook = "__import__" + code = compile("import foo.bar", "test", "exec") ns = {'__builtins__': types.MappingProxyType({})} - self.assertRaisesRegex(ImportError, "__import__ not found", exec, code, ns) - ns = {'__builtins__': types.MappingProxyType({'__import__': lambda *args: args})} + self.assertRaisesRegex(ImportError, f"{hook} not found", exec, code, ns) + ns = {'__builtins__': types.MappingProxyType({hook: lambda *args: args})} exec(code, ns) self.assertEqual(ns['foo'], ('foo.bar', ns, ns, None, 0)) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 67960f5709b08e..8a3b60dca42d6c 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -1661,6 +1661,10 @@ def test_resize(self): def test_resize_error(self): # gh-157242: If bytearray.resize() fails (MemoryError), # the bytearray must be left unchanged. + # + # gh-160303: If a slice is deleted or replaced by shorter data and + # shrinking the memory block fails (MemoryError), the bytearray + # must be modified anyway. offset = 3 for logical_offset in (False, True): @@ -1687,6 +1691,33 @@ def test_resize_error(self): ba.resize(1) self.assertEqual(ba, expected) + # shrink bytearray with a slice at the front, in the middle + # and at the end + data = b'0123456789' + if logical_offset: + data = data[offset:] + for start, stop in ((0, -2), (1, -1), (2, len(data))): + for replacement in (b'', b'.'): + with self.subTest(start=start, stop=stop, + replacement=replacement): + ba = bytearray(b'0123456789') + if logical_offset: + del ba[:offset] + index = slice(start, stop) + value = bytearray(replacement) + expected = data[:start] + replacement + data[stop:] + with inject_memory_error(self): + if replacement: + ba[index] = value + else: + del ba[index] + self.assertEqual(ba, expected) + # float() relies on the null byte after the data. + self.assertEqual(float(ba), float(expected)) + ba.append(ord('5')) + del ba[:1] + self.assertEqual(ba, expected[1:] + b'5') + def test_take_bytes(self): ba = bytearray(b'ab') self.assertEqual(ba.take_bytes(), b'ab') diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py index 7115359156eb69..8d8b7f9bbc38fb 100644 --- a/Lib/test/test_call.py +++ b/Lib/test/test_call.py @@ -208,7 +208,7 @@ def test_varargs14_kw(self): itertools.product, 0, repeat=1, foo=2) def test_varargs15_kw(self): - msg = r"^ImportError\(\) takes at most 3 keyword arguments \(4 given\)$" + msg = r"^ImportError\(\) got an unexpected keyword argument 'foo'$" self.assertRaisesRegex(TypeError, msg, ImportError, 0, name=1, path=2, name_from=3, foo=3) diff --git a/Lib/test/test_capi/test_dict.py b/Lib/test/test_capi/test_dict.py index 9a2e79d4da7e71..4028b56a9cc22c 100644 --- a/Lib/test/test_capi/test_dict.py +++ b/Lib/test/test_capi/test_dict.py @@ -635,11 +635,47 @@ class DictSubtype(dict): ... self.assertIs(type(d), DictSubtype) self.assertEqual(d, DictSubtype({})) + # non str keys: + d = {1: 'a', 2: 'b', 3: 'c', None: 'f'} + f = as_frozendict(d) + self.assertIs(type(f), frozendict) + self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'})) + self.assertIs(type(d), dict) + self.assertEqual(d, {}) + + d = DictSubtype({1: 'a', 2: 'b', 3: 'c', None: 'f'}) + f = as_frozendict(d) + self.assertIs(type(f), frozendict) + self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'})) + self.assertIs(type(d), DictSubtype) + self.assertEqual(d, DictSubtype({})) + + # wrong inputs: for wrong_input in (frozendict(), [], None): with self.subTest(wrong_input=wrong_input): with self.assertRaises(SystemError): as_frozendict(wrong_input) + # gh-161767: check type with inline values and dict with embedded values + class Regular: ... + + for klass in (Regular, _testcapi.ManagedDictType): + with self.subTest(klass=klass): + instance = klass() # empty `.__dict__` + with self.assertRaisesRegex(ValueError, 'dict with embedded values'): + as_frozendict(instance.__dict__) + self.assertEqual(instance.__dict__, {}) + + instance = klass() # non-empty `.__dict__` + instance.key = 'value' + with self.assertRaisesRegex(ValueError, 'dict with embedded values'): + as_frozendict(instance.__dict__) + + self.assertEqual(instance.__dict__, {'key': 'value'}) + # Both of these actions were needed to cause a crash: + instance.other = 'new' + del instance # must not crash + def test_frozendict_check(self): # Test PyFrozenDict_Check() check = _testcapi.frozendict_check diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 91da2cf5ee9f61..bf7ae26648c49a 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -103,12 +103,17 @@ class BadFloat3: def __float__(self): raise RuntimeError + class IntSubclass(int): + def __float__(self): + return 4.25 + self.assertEqual(asdouble(4.25), 4.25) self.assertEqual(asdouble(-1.0), -1.0) self.assertEqual(asdouble(42), 42.0) self.assertEqual(asdouble(-1), -1.0) self.assertEqual(asdouble(2**1000), float(2**1000)) + self.assertEqual(asdouble(IntSubclass(42)), 4.25) self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25) self.assertEqual(asdouble(FloatSubclass2(4.25)), 4.25) self.assertEqual(asdouble(Index()), 99.) diff --git a/Lib/test/test_capi/test_function.py b/Lib/test/test_capi/test_function.py index 9a07e3106f3a22..5024c58679bb2b 100644 --- a/Lib/test/test_capi/test_function.py +++ b/Lib/test/test_capi/test_function.py @@ -235,7 +235,7 @@ def inner(arg5, arg6): self.assertEqual(len(closure), len(func.__code__.co_freevars)) for cell in closure: self.assertIsInstance(cell, CellType) - self.assertTrue(closure[0].cell_contents, 5) + self.assertEqual(closure[0].cell_contents, 5) func = with_two_levels(1, 2)(3, 4) closure = _testcapi.function_get_closure(func) diff --git a/Lib/test/test_capi/test_object.py b/Lib/test/test_capi/test_object.py index 338fc61ea1eb5f..ece673dbbec089 100644 --- a/Lib/test/test_capi/test_object.py +++ b/Lib/test/test_capi/test_object.py @@ -354,6 +354,83 @@ def test_pyobject_dump(self): output = self.pyobject_dump(NULL) self.assertRegex(output, r'') + def test_object_clear_managed_dict(self): + # PyObject_ClearManagedDict + clear_managed_dict = _testcapi.pyobject_clear_managed_dict + ManagedDictType = _testcapi.ManagedDictType + + obj = ManagedDictType() + obj.value = 'first' + # asserting `__dict__` is a must to materialize the value before clean: + self.assertEqual(obj.__dict__, {'value': 'first'}) + self.assertIs(type(obj.__dict__), dict) + clear_managed_dict(obj) + self.assertEqual(obj.__dict__, {}) + self.assertIs(type(obj.__dict__), dict) + + # big object: + obj = ManagedDictType() + existing_dict = {f'attr_{x}': x for x in range(100)} + obj.__dict__ = existing_dict + self.assertIs(obj.__dict__, existing_dict) + clear_managed_dict(obj) + self.assertEqual(obj.__dict__, {}) + self.assertIs(type(obj.__dict__), dict) + + # empty dict: + obj = ManagedDictType() + self.assertEqual(obj.__dict__, {}) + self.assertIs(type(obj.__dict__), dict) + clear_managed_dict(obj) + self.assertEqual(obj.__dict__, {}) + self.assertIs(type(obj.__dict__), dict) + + # double clear: + obj = ManagedDictType() + obj.value = 'first' + self.assertEqual(obj.__dict__, {'value': 'first'}) + clear_managed_dict(obj) + clear_managed_dict(obj) + self.assertEqual(obj.__dict__, {}) + self.assertIs(type(obj.__dict__), dict) + + # unmaterialized: + obj = ManagedDictType() + obj.value = 'first' + clear_managed_dict(obj) + self.assertEqual(obj.__dict__, {}) + self.assertIs(type(obj.__dict__), dict) + + # CRASHES: clear_managed_dict(NULL) + + @threading_helper.requires_working_threading() + @support.nomemtest + def test_object_clear_managed_dict_ft_memory_error(self): + # gh-152254: Test PyObject_ClearManagedDict() error. + # The setup must contain: object owned by another thread + # and the attempt to clear the managed dict with no memory left. + clear_managed_dict = _testcapi.pyobject_clear_managed_dict + ManagedDictType = _testcapi.ManagedDictType + + obj = ManagedDictType() + obj.value = 'first' + self.assertEqual(len(obj.__dict__), 1) + + def worker(): + # This needs to be in a separate thread for the crash to happen: + with support.catch_unraisable_exception() as ex: + # `PyObject_ClearManagedDict()` used to crash: + _testcapi.call_with_nomemory(0, 1, clear_managed_dict, obj) + self.assertIsNotNone(ex.unraisable) + self.assertIs(ex.unraisable.exc_type, MemoryError) + self.assertIn('managed dict', ex.unraisable.err_msg) + self.assertEqual(obj.__dict__, {}) + self.assertIs(type(obj.__dict__), dict) + + threading_helper.run_concurrently([worker]) + self.assertEqual(obj.__dict__, {}) + self.assertIs(type(obj.__dict__), dict) + class RefTracerTest(unittest.TestCase): @support.requires_resource('cpu') diff --git a/Lib/test/test_capi/test_tuple.py b/Lib/test/test_capi/test_tuple.py index 3f2318f303d57a..1f8f5da0e51e90 100644 --- a/Lib/test/test_capi/test_tuple.py +++ b/Lib/test/test_capi/test_tuple.py @@ -1,6 +1,7 @@ import gc import sys import unittest +from test import support from test.support import import_helper _testcapi = import_helper.import_module('_testcapi') @@ -364,6 +365,21 @@ def resize(tup, new_size, new=True, compute_hash=False): with self.assertRaises(SystemError): resize(a, 3, True, compute_hash=True) + # Test _PyTuple_Resize() failure: inject MemoryError on + # PyObject_GC_Resize(). Check that the test doesn't leak references + # if the test is run with -R 3:3. + a = tuple(object() for _ in range(10)) + got_memory_error = False + # Test different start values since _testcapi _tuple_resize() also + # allocates memory (to copy the tuple). + for start in range(5): + try: + with support.inject_memory_error_cm(start=start): + resize(a, 30) + except MemoryError: + got_memory_error = True + self.assertTrue(got_memory_error) + a = () self.assertRaises(MemoryError, resize, a, PY_SSIZE_T_MAX) self.assertRaises(SystemError, resize, a, -1) diff --git a/Lib/test/test_capi/test_unicode.py b/Lib/test/test_capi/test_unicode.py index 84303bc379cf13..fe4e8e3b5046e6 100644 --- a/Lib/test/test_capi/test_unicode.py +++ b/Lib/test/test_capi/test_unicode.py @@ -1883,7 +1883,9 @@ def test_check_interned(self): def fresh_string(text): # Create a new string object by decoding a bytes string. - return text.encode().decode() + # Add a suffix to make sure that it's not interned, even if + # the string is interned in the PYC file (Free Threading). + return (text + ' suffix').encode().decode() s = fresh_string('hello') self.assertEqual(check_interned(s), SSTATE_NOT_INTERNED) diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c index 65dd25d71f494b..b462a949bd9899 100644 --- a/Lib/test/test_cext/extension.c +++ b/Lib/test/test_cext/extension.c @@ -483,6 +483,18 @@ module_exec(PyObject *module) return -1; } #endif + +#ifdef Py_LIMITED_API + if (PyModule_AddIntMacro(module, Py_LIMITED_API) < 0) { + return -1; + } +#endif +#ifdef Py_TARGET_ABI3T + if (PyModule_AddIntMacro(module, Py_TARGET_ABI3T) < 0) { + return -1; + } +#endif + // Ignore "unused argument" warning when none of these macros is defined (void)module; return 0; diff --git a/Lib/test/test_cext/runtests.py b/Lib/test/test_cext/runtests.py index 6700b410b7d7a7..cb2725209de7dc 100644 --- a/Lib/test/test_cext/runtests.py +++ b/Lib/test/test_cext/runtests.py @@ -63,14 +63,19 @@ def main(): '__STRICT_ANSI__', '__cplusplus', '_MSVC_LANG', + 'Py_LIMITED_API', + 'Py_TARGET_ABI3T', ): try: value = getattr(testmod, name) except AttributeError: - pass - else: - print(f'{name}: {value}') - newline = True + continue + + if name in ('Py_LIMITED_API', 'Py_TARGET_ABI3T'): + value = f'0x{value:08x}' + + print(f'{name}: {value}') + newline = True if newline: print() diff --git a/Lib/test/test_cext/setup.py b/Lib/test/test_cext/setup.py index 10a3887931932a..3b613ee729f52c 100644 --- a/Lib/test/test_cext/setup.py +++ b/Lib/test/test_cext/setup.py @@ -42,6 +42,9 @@ # Ask for strict(er) compliance with the standard '-pedantic-errors', + + # gh-154643: Headers should not use undefined macros in #if + '-Werror=undef', ] if not support.Py_GIL_DISABLED: PUBLIC_CFLAGS.append( @@ -94,6 +97,9 @@ # We also use `long long`, a C++11 feature we can enable individually. '-Wno-long-long', + + # gh-154643: Headers should not use undefined macros in #if + '-Werror=undef', ] INTERNAL_CXXFLAGS = [ # Do not complain about unused parameteres in the internal C API diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index e57f4c4a9d556a..cb4ed55fabbbc1 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -2,7 +2,7 @@ # Copyright 2012-2013 by Larry Hastings. # Licensed to the PSF under a contributor agreement. -from functools import partial +from functools import partial, wraps from test import support, test_tools from test.support import force_not_colorized_test_class from test.support import os_helper @@ -1847,6 +1847,15 @@ def test_c_name(self): """) self.assertEqual("os_stat_fn", function.c_basename) + def test_var_keyword_c_name(self): + function = self.parse_function(""" + module foo + foo.bar + **kwargs as kwds: dict + """) + p = function.parameters['kwargs_as_kwds'] + self.assertEqual("kwds", p.converter.name) + def test_base_invalid_syntax(self): block = """ module os @@ -3570,6 +3579,18 @@ class Foo "FooObject *" "Foo_Type" """ self.expect_failure(block, err, lineno=7) + def test_vectorcall_with_deprecated_parameters(self): + err = "@vectorcall does not support deprecated parameters" + block = """ + module m + class Foo "FooObject *" "Foo_Type" + @vectorcall + Foo.__init__ + a: object = None + / [from 3.14] + """ + self.expect_failure(block, err, lineno=5) + def test_unused_param(self): block = self.parse(""" module foo @@ -4621,6 +4642,16 @@ def test_file_dest(self): except ImportError: ac_tester = None +def static_and_heap(test): + # Run test with both a static type and a heap type. + @wraps(test) + def wrapper(self): + for cls in ac_tester.TestClass, ac_tester.HeapTestClass: + with self.subTest(cls=cls): + test(self, cls) + return wrapper + + @unittest.skipIf(ac_tester is None, "_testclinic is missing") class ClinicFunctionalTest(unittest.TestCase): locals().update((name, getattr(ac_tester, name)) @@ -5295,43 +5326,46 @@ def test_cloned_func_with_converter_exception_message(self): func = getattr(ac_tester, name) self.assertEqual(func(), name) - def test_get_defining_class(self): - obj = ac_tester.TestClass() + @static_and_heap + def test_get_defining_class(self, cls): + obj = cls() meth = obj.get_defining_class - self.assertIs(obj.get_defining_class(), ac_tester.TestClass) + self.assertIs(obj.get_defining_class(), cls) # 'defining_class' argument is a positional only argument with self.assertRaises(TypeError): - obj.get_defining_class_arg(cls=ac_tester.TestClass) + obj.get_defining_class_arg(cls=cls) check = partial(self.assertRaisesRegex, TypeError, "no arguments") check(meth, 1) check(meth, a=1) - def test_get_defining_class_capi(self): + @static_and_heap + def test_get_defining_class_capi(self, cls): from _testcapi import pyobject_vectorcall - obj = ac_tester.TestClass() + obj = cls() meth = obj.get_defining_class pyobject_vectorcall(meth, None, None) pyobject_vectorcall(meth, (), None) pyobject_vectorcall(meth, (), ()) pyobject_vectorcall(meth, None, ()) - self.assertIs(pyobject_vectorcall(meth, (), ()), ac_tester.TestClass) + self.assertIs(pyobject_vectorcall(meth, (), ()), cls) check = partial(self.assertRaisesRegex, TypeError, "no arguments") check(pyobject_vectorcall, meth, (1,), None) check(pyobject_vectorcall, meth, (1,), ("a",)) - def test_get_defining_class_arg(self): - obj = ac_tester.TestClass() + @static_and_heap + def test_get_defining_class_arg(self, cls): + obj = cls() self.assertEqual(obj.get_defining_class_arg("arg"), - (ac_tester.TestClass, "arg")) + (cls, "arg")) self.assertEqual(obj.get_defining_class_arg(arg=123), - (ac_tester.TestClass, 123)) + (cls, 123)) # 'defining_class' argument is a positional only argument with self.assertRaises(TypeError): - obj.get_defining_class_arg(cls=ac_tester.TestClass, arg="arg") + obj.get_defining_class_arg(cls=cls, arg="arg") # wrong number of arguments with self.assertRaises(TypeError): @@ -5339,9 +5373,9 @@ def test_get_defining_class_arg(self): with self.assertRaises(TypeError): obj.get_defining_class_arg("arg1", "arg2") - def test_defclass_varpos(self): + @static_and_heap + def test_defclass_varpos(self, cls): # fn(*args) - cls = ac_tester.TestClass obj = cls() fn = obj.defclass_varpos self.assertEqual(fn(), (cls, ())) @@ -5351,9 +5385,9 @@ def test_defclass_varpos(self): self.assertEqual(fn(obj), (cls, ())) self.assertEqual(fn(obj, 1, 2), (cls, (1, 2))) - def test_defclass_posonly_varpos(self): + @static_and_heap + def test_defclass_posonly_varpos(self, cls): # fn(a, b, /, *args) - cls = ac_tester.TestClass obj = cls() fn = obj.defclass_posonly_varpos errmsg = 'expected at least 2 arguments' @@ -5708,7 +5742,7 @@ def errmsg(name): def test_lone_kwds(self): with self.assertRaises(TypeError): ac_tester.lone_kwds(1, 2) - self.assertEqual(ac_tester.lone_kwds(), ({},)) + self.assertEqual(ac_tester.lone_kwds(), (None,)) self.assertEqual(ac_tester.lone_kwds(y='y'), ({'y': 'y'},)) kwds = {'y': 'y', 'z': 'z'} self.assertEqual(ac_tester.lone_kwds(y='y', z='z'), (kwds,)) @@ -5721,7 +5755,7 @@ def test_kwds_with_pos_only(self): ac_tester.kwds_with_pos_only(y='y') with self.assertRaises(TypeError): ac_tester.kwds_with_pos_only(1, y='y') - self.assertEqual(ac_tester.kwds_with_pos_only(1, 2), (1, 2, {})) + self.assertEqual(ac_tester.kwds_with_pos_only(1, 2), (1, 2, None)) self.assertEqual(ac_tester.kwds_with_pos_only(1, 2, y='y'), (1, 2, {'y': 'y'})) kwds = {'y': 'y', 'z': 'z'} self.assertEqual(ac_tester.kwds_with_pos_only(1, 2, y='y', z='z'), (1, 2, kwds)) @@ -5732,16 +5766,16 @@ def test_kwds_with_optional_pos_only(self): ac_tester.kwds_with_optional_pos_only() with self.assertRaises(TypeError): ac_tester.kwds_with_optional_pos_only(y='y') - self.assertEqual(ac_tester.kwds_with_optional_pos_only(1), (1, None, {})) - self.assertEqual(ac_tester.kwds_with_optional_pos_only(1, 2), (1, 2, {})) + self.assertEqual(ac_tester.kwds_with_optional_pos_only(1), (1, None, None)) + self.assertEqual(ac_tester.kwds_with_optional_pos_only(1, 2), (1, 2, None)) self.assertEqual(ac_tester.kwds_with_optional_pos_only(1, y='y'), (1, None, {'y': 'y'})) self.assertEqual(ac_tester.kwds_with_optional_pos_only(1, 2, y='y'), (1, 2, {'y': 'y'})) def test_kwds_with_stararg(self): - self.assertEqual(ac_tester.kwds_with_stararg(), ((), {})) - self.assertEqual(ac_tester.kwds_with_stararg(1, 2), ((1, 2), {})) + self.assertEqual(ac_tester.kwds_with_stararg(), ((), None)) + self.assertEqual(ac_tester.kwds_with_stararg(1, 2), ((1, 2), None)) self.assertEqual(ac_tester.kwds_with_stararg(y='y'), ((), {'y': 'y'})) args = (1, 2) kwds = {'y': 'y', 'z': 'z'} @@ -5753,7 +5787,7 @@ def test_kwds_with_pos_only_and_stararg(self): ac_tester.kwds_with_pos_only_and_stararg() with self.assertRaises(TypeError): ac_tester.kwds_with_pos_only_and_stararg(y='y') - self.assertEqual(ac_tester.kwds_with_pos_only_and_stararg(1, 2), (1, 2, (), {})) + self.assertEqual(ac_tester.kwds_with_pos_only_and_stararg(1, 2), (1, 2, (), None)) self.assertEqual(ac_tester.kwds_with_pos_only_and_stararg(1, 2, y='y'), (1, 2, (), {'y': 'y'})) args = ('lobster', 'thermidor') kwds = {'y': 'y', 'z': 'z'} @@ -5821,6 +5855,20 @@ def test_vc_kwonly_missing_required(self): with self.assertRaises(TypeError): ac_tester.VcKwOnly() + def test_vc_kwds(self): + with self.assertRaises(TypeError): + ac_tester.VcKwds() + with self.assertRaises(TypeError): + ac_tester.VcKwds(y='y') + with self.assertRaises(TypeError): + ac_tester.VcKwds(1, 2, 3) + self.assertEqual(ac_tester.VcKwds(1), (1, None, None)) + self.assertEqual(ac_tester.VcKwds(1, 2), (1, 2, None)) + self.assertEqual(ac_tester.VcKwds(1, a='a', b='b'), + (1, None, {'a': 'a', 'b': 'b'})) + kwds = {'y': 'y', 'z': 'z'} + self.assertEqual(ac_tester.VcKwds(1, 2, **kwds), (1, 2, kwds)) + def test_parse_errors_match_slot(self): # tp_vectorcall and tp_new/tp_init slot should match in argument parsing # error messages. Explicit calls to __new__ and __init__, as well as @@ -5846,6 +5894,7 @@ def through_init(cls): through_new(ac_tester.VcNew), through_new(ac_tester.VcNewBase), through_new(ac_tester.VcKwOnly), + through_new(ac_tester.VcKwds), through_init(ac_tester.VcInit), ] invalid_calls = [ diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py index a986fd6b892bd2..c13e3aff7b4e9a 100644 --- a/Lib/test/test_cmath.py +++ b/Lib/test/test_cmath.py @@ -406,8 +406,6 @@ def polar_with_errno_set(z): _testcapi.set_errno(0) self.check_polar(polar_with_errno_set) - @unittest.skipIf(sys.platform.startswith("sunos"), - "skipping, see gh-138573") def test_phase(self): self.assertAlmostEqual(phase(0), 0.) self.assertAlmostEqual(phase(1.), 0.) @@ -423,6 +421,16 @@ def test_phase(self): self.assertEqual(phase(complex(-0.0, 0.0)), pi) self.assertEqual(phase(complex(-0.0, -0.0)), -pi) + # overflow and underflow of imag/real + self.assertEqual(phase(complex(1E300, 1E-320)), 0.0) + self.assertEqual(phase(complex(1E300, -1E-320)), -0.0) + self.assertAlmostEqual(phase(complex(1E-320, 1E300)), pi/2) + self.assertAlmostEqual(phase(complex(-1E-320, 1E300)), pi/2) + self.assertAlmostEqual(phase(complex(-1E300, 1E-320)), pi) + self.assertAlmostEqual(phase(complex(-1E300, -1E-320)), -pi) + self.assertAlmostEqual(phase(complex(1E-320, -1E300)), -pi/2) + self.assertAlmostEqual(phase(complex(-1E-320, -1E300)), -pi/2) + # infinities self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi) self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index df766387dd4c72..b17626252fb537 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -64,6 +64,27 @@ def test_assemble_init_allocation_failure(self): """) script_helper.assert_python_ok('-c', code) + @support.requires_subprocess() + @support.nomemtest + def test_push_cold_blocks_to_end_allocation_failure(self): + code = textwrap.dedent(""" + from test import support + + source = "async def f():\\n await x\\n" + for n in range(1, 1000): + with support.inject_memory_error_cm(n, n + 1): + try: + compile(source, "", "exec") + except MemoryError: + continue + break + else: + raise AssertionError("compile() never succeeded") + if n == 1: + raise AssertionError("no MemoryError raised") + """) + script_helper.assert_python_ok('-c', code) + def test_other_newlines(self): compile("\r\n", "", "exec") compile("\r", "", "exec") @@ -2342,6 +2363,29 @@ def g(self, __x): return [__x] """, strings=['_ID_A__x']), + # A constant of an earlier method is equal to the co_names of a + # later one. The mangled name is not an ASCII identifier, so the + # compiler does not intern the equal string constants. + 'mangled_names_non_ascii': Case( + source=""" + class ID_\xe9: + def g(self): + return ("_ID_\xe9__x",), "_ID_\xe9__x" + def f(self): + return self.__x + """, + strings=['_ID_\xe9__x']), + # The same with the names of local variables. + 'mangled_varnames_non_ascii': Case( + source=""" + class ID_\xe9: + def g(self): + return ("_ID_\xe9__x",), "_ID_\xe9__x" + @staticmethod + def f(__x): + return __x + """, + strings=['_ID_\xe9__x']), # The names of the keyword arguments are a constant equal to # the co_names of the function. 'kwnames': Case( @@ -2548,6 +2592,29 @@ def code_constants(code): self.assertEqual(len(names), 1) self.assertTrue(any(const is names for const in consts)) + def test_ast_names_not_interned(self): + # The identifiers of an AST which was not created by the parser are + # not interned yet. A constant of an earlier function is equal to + # the co_names of a later one. + source = textwrap.dedent(""" + def g(self): + return ("gh129724_ast_\xe9",), "gh129724_ast_\xe9" + def f(self): + return self.placeholder + """) + keep = [] + dumps = [] + for attempt in range(2): + tree = ast.parse(source) + attribute, = [node for node in ast.walk(tree) + if isinstance(node, ast.Attribute)] + # Not a literal: a new string object, which is not interned. + attribute.attr = ''.join(['gh129724_ast_', '\xe9']) + code = compile(tree, '', 'exec') + keep.append(code) + dumps.append(marshal.dumps(code)) + self.assertEqual(dumps[0], dumps[1]) + @support.cpython_only def test_slice_strings(self): # The strings of a slice constant are interned like the other diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py index 7f1f6cf58402c9..5b7708afdaa124 100644 --- a/Lib/test/test_ctypes/test_arrays.py +++ b/Lib/test/test_ctypes/test_arrays.py @@ -1,11 +1,14 @@ import ctypes import sys +import threading import unittest +from concurrent.futures import ThreadPoolExecutor from ctypes import (Structure, Array, ARRAY, sizeof, addressof, create_string_buffer, create_unicode_buffer, c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, c_long, c_ulonglong, c_float, c_double, c_longdouble) -from test.support import bigmemtest, _2G, threading_helper, Py_GIL_DISABLED +from test.support import (bigmemtest, _2G, threading_helper, + Py_GIL_DISABLED, SHORT_TIMEOUT) from ._support import (_CData, PyCArrayType, Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE) @@ -288,5 +291,49 @@ def run(): raise cm.exc_value +class ArrayTypeCacheTest(unittest.TestCase): + # gh-160978: the array type cache could hand out distinct type objects for + # the same (item type, length) pair when the array type was created from + # more than one thread at a time. + + @threading_helper.requires_working_threading() + def test_concurrent_creation_with_set_name(self): + # Creating the array type runs Python code -- ``__set_name__`` here -- + # which lets a second thread reach the cache before the first one has + # stored its type. Without the fix this fails on the default build as + # well, so it guards the fix even where threads cannot interleave + # freely. + barrier = threading.Barrier(2) + + class Meta(type(c_int)): + def __set_name__(cls, owner, name): + barrier.wait(timeout=SHORT_TIMEOUT) + + class Item(c_int, metaclass=Meta): + pass + + with ThreadPoolExecutor(2) as pool: + first, second = pool.map(lambda _: Item * 100, range(2)) + + self.assertIs(first, second) + # Raises TypeError if the two types are distinct. + first.from_param(second()) + + @threading_helper.requires_working_threading() + def test_concurrent_creation_returns_identical_types(self): + nthreads = 8 + barrier = threading.Barrier(nthreads) + + def make_type(length): + barrier.wait(timeout=SHORT_TIMEOUT) + return c_int * length + + with ThreadPoolExecutor(nthreads) as pool: + for length in range(1, 51): + types = list(pool.map(make_type, [length] * nthreads)) + barrier.reset() + self.assertEqual(len(set(types)), 1) + + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_ctypes/test_callbacks.py b/Lib/test/test_ctypes/test_callbacks.py index c7f287d9fb6293..7d746749130db2 100644 --- a/Lib/test/test_ctypes/test_callbacks.py +++ b/Lib/test/test_ctypes/test_callbacks.py @@ -15,6 +15,7 @@ from ctypes.util import find_library, struct, wrap_dll_function from test import support from test.support import import_helper +from test.support import script_helper _ctypes_test = import_helper.import_module("_ctypes_test") @@ -559,6 +560,19 @@ def cb(): self.assertEqual(_testfunc_callback_int_to_longlong(cb), -1) + def test_import_does_not_open_fds(self): + # gh-135893: Importing ctypes must not allocate a libffi closure, + # which can leave a file descriptor open for the rest of the + # process' lifetime. + code = """if 1: + from test.support import os_helper + before = os_helper.fd_count() + import ctypes + after = os_helper.fd_count() + assert before == after + """ + script_helper.assert_python_ok("-c", code) + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_ctypes/test_memfunctions.py b/Lib/test/test_ctypes/test_memfunctions.py index e3cb5db775ef62..4edf819ca98eba 100644 --- a/Lib/test/test_ctypes/test_memfunctions.py +++ b/Lib/test/test_ctypes/test_memfunctions.py @@ -60,7 +60,7 @@ def test_cast(self): @support.refcount_test def test_string_at(self): s = string_at(b"foo bar") - self.assertTrue(s, "foo bar") + self.assertEqual(s, b"foo bar") self.assertEqual(string_at(b"foo bar", 7), b"foo bar") self.assertEqual(string_at(b"foo bar", 3), b"foo") diff --git a/Lib/test/test_ctypes/test_python_api.py b/Lib/test/test_ctypes/test_python_api.py index 1072a109833261..8102280e44510b 100644 --- a/Lib/test/test_ctypes/test_python_api.py +++ b/Lib/test/test_ctypes/test_python_api.py @@ -68,6 +68,12 @@ def test_PyOS_snprintf(self): PyOS_snprintf(buf, sizeof(buf), b"Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3) self.assertEqual(buf.value, b"Hello from ctypes (1, 2, 3)") + # Test truncated output + small = create_string_buffer(14) + rv = PyOS_snprintf(small, sizeof(small), b"Hello from %s", b"ctypes") + self.assertEqual(rv, len(b"Hello from ctypes")) + self.assertEqual(small.raw, b"Hello from ct\0") + # not enough arguments self.assertRaises(TypeError, PyOS_snprintf, buf) diff --git a/Lib/test/test_ctypes/test_returnfuncptrs.py b/Lib/test/test_ctypes/test_returnfuncptrs.py index 337801b226ab06..8f0a61be4c5a80 100644 --- a/Lib/test/test_ctypes/test_returnfuncptrs.py +++ b/Lib/test/test_ctypes/test_returnfuncptrs.py @@ -28,7 +28,7 @@ def test_without_prototype(self): # _CFuncPtr instances are now callable with an integer argument # which denotes a function address: strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(addr) - self.assertTrue(strchr(b"abcdef", b"b"), "bcdef") + self.assertEqual(strchr(b"abcdef", b"b"), b"bcdef") self.assertEqual(strchr(b"abcdef", b"x"), None) self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0) self.assertRaises(TypeError, strchr, b"abcdef") @@ -38,7 +38,7 @@ def test_from_dll(self): # _CFuncPtr instances are now callable with a tuple argument # which denotes a function name and a dll: strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(("my_strchr", dll)) - self.assertTrue(strchr(b"abcdef", b"b"), "bcdef") + self.assertEqual(strchr(b"abcdef", b"b"), b"bcdef") self.assertEqual(strchr(b"abcdef", b"x"), None) self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0) self.assertRaises(TypeError, strchr, b"abcdef") @@ -57,7 +57,7 @@ def __getitem__(self, key): # which denotes a function name and a dll: strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)( BadSequence(("my_strchr", CDLL(_ctypes_test.__file__)))) - self.assertTrue(strchr(b"abcdef", b"b"), "bcdef") + self.assertEqual(strchr(b"abcdef", b"b"), b"bcdef") self.assertEqual(strchr(b"abcdef", b"x"), None) self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0) self.assertRaises(TypeError, strchr, b"abcdef") diff --git a/Lib/test/test_ctypes/test_slicing.py b/Lib/test/test_ctypes/test_slicing.py index 66f9e530104cac..b8c981ca2b17fa 100644 --- a/Lib/test/test_ctypes/test_slicing.py +++ b/Lib/test/test_ctypes/test_slicing.py @@ -63,6 +63,35 @@ def test_setslice_cint(self): with self.assertRaises(ValueError): a[:5] = range(32) + def test_setslice_len_error(self): + # gh-161840: exceptions from PySequence_Length() must propagate + # instead of being replaced with ValueError. + class CustomError(Exception): + pass + + class BadLen: + def __len__(self): + raise CustomError("boom") + + class BadValueErrorLen: + def __len__(self): + raise ValueError("custom value error") + + a = (c_int * 3)(1, 2, 3) + with self.assertRaisesRegex(CustomError, "boom"): + a[0:1] = BadLen() + with self.assertRaisesRegex(CustomError, "boom"): + a[0:0] = BadLen() + with self.assertRaisesRegex(CustomError, "boom"): + a[::2] = BadLen() + with self.assertRaisesRegex(ValueError, "custom value error"): + a[0:1] = BadValueErrorLen() + with self.assertRaisesRegex(TypeError, "object of type 'int' has no len"): + a[0:1] = 42 + with self.assertRaisesRegex(TypeError, "dict is not a sequence"): + a[0:1] = {"x": 1} + self.assertEqual(a[:], [1, 2, 3]) + def test_char_ptr(self): s = b"abcdefghijklmnopqrstuvwxyz" diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index 4d39e5014e654b..8579b3eed73a97 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -1873,6 +1873,17 @@ def test_terminfo(self): self.assertIsInstance(cud_2, bytes) curses.putp(cud_2) + # The few capabilities that take strings take them as bytes. + ms = curses.tigetstr('Ms') + if ms is not None and b'%p2%s' in ms: + self.assertIn(b'aGk=', curses.tparm(ms, b'c', b'aGk=')) + self.assertRaises(TypeError, curses.tparm, ms, 1, 2) + self.assertRaises(ValueError, curses.tparm, ms, b'c', b'a\0') + # An omitted string is empty. + self.assertNotIn(b'%', curses.tparm(ms, b'c')) + self.assertRaises(TypeError, curses.tparm, b'%p1%d', b'x') + self.assertRaises(TypeError, curses.tparm, b'%p1%d', *range(10)) + curses.putp(b'abc\n') def test_misc_module_funcs(self): diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index c4b24617af7c43..ef7aa5358b5cc6 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -3780,6 +3780,27 @@ def test_crashing_signal_dict_flags(self): @requires_cdecimal class CContextAPItests(ContextAPItests, unittest.TestCase): decimal = C + + def test_current_context_var_wrong_type(self): + # gh-161387 + if not self.decimal.HAVE_CONTEXTVAR: + self.skipTest("_decimal built without context variables") + value = object() + context_vars = [var for var in contextvars.copy_context() + if var.name == "decimal_context"] + self.assertTrue(context_vars) + tokens = [var.set(value) for var in context_vars] + + def restore(): + for var, token in zip(context_vars, tokens): + var.reset(token) + + self.addCleanup(restore) + with self.assertRaisesRegex(TypeError, "got object"): + self.decimal.Decimal(1) + self.decimal.Decimal(2) + for var in context_vars: + self.assertIs(var.get(), value) + class PyContextAPItests(ContextAPItests, unittest.TestCase): decimal = P diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index c4c4d0858ec00b..2f5953ba096d43 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4181,6 +4181,28 @@ def test_pow_wrapper_error_messages(self): 'expected 1 or 2 arguments, got 3', int().__rpow__, 1, 2, 3) + def test_setattr_slot_update_error(self): + # An error raised while updating the slots of subclasses must be + # propagated by the attribute assignment. + class K: + def __hash__(self): + return hash('__repr__') + def __eq__(self, other): + raise ZeroDivisionError + + class A: + pass + + with self.assertWarns(RuntimeWarning): + B = type('B', (A,), {K(): 1}) + if support.Py_GIL_DISABLED: + # The slot update skips non-str keys without comparing them. + A.__repr__ = lambda self: 'A' + else: + with self.assertRaises(ZeroDivisionError): + A.__repr__ = lambda self: 'A' + self.assertEqual(repr(A()), 'A') + def test_mutable_bases(self): # Testing mutable bases... diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index ea697a5a920f77..6179ad053e7a2a 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -1020,6 +1020,26 @@ def test_splittable_setdefault(self): self.assertEqual(list(a), ['x', 'y', 'z', 'a', 'b']) self.assertEqual(list(b), ['x', 'y', 'z', 'b', 'a']) + @support.cpython_only + def test_splittable_setdefault_str_subclass(self): + class C: + pass + + class S(str): + pass + + a = C() + a.x = 1 + b = C() + b.y = 2 + d = b.__dict__ + + self.assertEqual(d.setdefault(S('x'), 5), 5) + self.assertEqual(d, {'y': 2, 'x': 5}) + self.assertEqual(d.setdefault(S('x'), 6), 5) + self.assertEqual(list(d), ['y', 'x']) + self.assertEqual(a.__dict__, {'x': 1}) + @support.cpython_only def test_splittable_del(self): """split table must be combined when del d[k]""" @@ -1958,6 +1978,20 @@ def test_copy(self): self.assertEqual(d2, frozendict(x=1, y=2)) self.assertEqual(type(d2), frozendict) + def test_copy_iter_override(self): + class CustomReversedDict(frozendict): + def keys(self): + return reversed(list(frozendict.keys(self))) + + __iter__ = keys + + pairs = [('a', 1), ('b', 2), ('c', 3)] + d = CustomReversedDict(pairs) + d2 = d.copy() + self.assertIsNot(d2, d) + self.assertEqual(type(d2), frozendict) + self.assertEqual(list(d2.items()), pairs[::-1]) + def test_merge(self): # test "a | b" operator self.assertEqual(frozendict(x=1) | frozendict(y=2), diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 709fa374704f1f..2c3972c7394d01 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -615,8 +615,6 @@ async def _asyncwith(c): LOAD_SPECIAL 2 (__aenter__) CALL 0 GET_AWAITABLE 1 - PUSH_NULL - LOAD_COMMON_CONSTANT 7 (None) L2: SEND 4 (to L5) L3: YIELD_VALUE 1 L4: RESUME 3 @@ -632,8 +630,6 @@ async def _asyncwith(c): LOAD_COMMON_CONSTANT 7 (None) CALL 3 GET_AWAITABLE 2 - PUSH_NULL - LOAD_COMMON_CONSTANT 7 (None) L8: SEND 4 (to L11) L9: YIELD_VALUE 1 L10: RESUME 3 @@ -647,14 +643,12 @@ async def _asyncwith(c): RETURN_VALUE %4d L12: CLEANUP_THROW - L13: JUMP_BACKWARD_NO_INTERRUPT 28 (to L5) + L13: JUMP_BACKWARD_NO_INTERRUPT 26 (to L5) L14: CLEANUP_THROW L15: JUMP_BACKWARD_NO_INTERRUPT 10 (to L11) L16: PUSH_EXC_INFO WITH_EXCEPT_START GET_AWAITABLE 2 - PUSH_NULL - LOAD_COMMON_CONSTANT 7 (None) L17: SEND 5 (to L21) L18: YIELD_VALUE 1 L19: RESUME 3 diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 447f847f33da93..4484749eac449e 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -1032,7 +1032,7 @@ class OpenXYZ(self.enum_type): self.assertIs(~(Y|Z), OpenXYZ(28)) self.assertIs(~(X|Y|Z), OpenXYZ(24)) self.assertIs(~XYZ_MASK, OpenXYZ(0)) - self.assertTrue(~OpenXYZ(0), XYZ_MASK) + self.assertIs(~OpenXYZ(0), XYZ_MASK) else: self.assertIs(~X, Y|Z) self.assertIs(~Y, X|Z) @@ -1045,7 +1045,7 @@ class OpenXYZ(self.enum_type): self.assertIs(~(Y|Z), X) self.assertIs(~(X|Y|Z), OpenXYZ(0)) self.assertIs(~XYZ_MASK, OpenXYZ(0)) - self.assertTrue(~OpenXYZ(0), (X|Y|Z)) + self.assertIs(~OpenXYZ(0), (X|Y|Z)) def test_assigned_negative_value(self): class X(self.enum_type): @@ -1895,7 +1895,7 @@ class Season(Enum): @reraise_if_not_enum(Name) def test_subclassing(self): self.assertEqual(Name.BDFL, 'Guido van Rossum') - self.assertTrue(Name.BDFL, Name('Guido van Rossum')) + self.assertIs(Name.BDFL, Name('Guido van Rossum')) self.assertIs(Name.BDFL, getattr(Name, 'BDFL')) test_pickle_dump_load(self.assertIs, Name.BDFL) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index a3f2eabacf6e88..78c66c0e0fc1de 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -1,4 +1,5 @@ import asyncio +import collections.abc import unittest import os import textwrap @@ -14,6 +15,7 @@ from unittest.mock import ANY from test.support import ( os_helper, + threading_helper, SHORT_TIMEOUT, busy_retry, requires_gil_enabled, @@ -55,7 +57,7 @@ try: from _remote_debugging import PROCESS_VM_READV_SUPPORTED from _remote_debugging import RemoteUnwinder - from _remote_debugging import FrameInfo, CoroInfo, TaskInfo + from _remote_debugging import FrameInfo, CoroInfo, TaskInfo, AwaitedInfo except ImportError: raise unittest.SkipTest( "Test only runs when _remote_debugging is available" @@ -487,6 +489,23 @@ def _extract_coroutine_funcnames(self, trace): for task in info.awaited_by } + @skip_if_not_supported + def test_async_awaited_info(self): + async def main(): + task = asyncio.current_task() + unwinder = RemoteUnwinder(os.getpid()) + for method in (unwinder.get_async_stack_trace, + unwinder.get_all_awaited_by): + with self.subTest(method=method.__name__): + info, = (info for info in method() + if info.thread_id == threading.get_native_id()) + self.assertIsInstance(info, AwaitedInfo) + task_info, = info.awaited_by + self.assertEqual(task_info.task_id, id(task)) + self.assertEqual(task_info.task_name, task.get_name()) + + asyncio.run(main()) + @skip_if_not_supported def test_long_task_name_is_truncated(self): # gh-157788 @@ -520,6 +539,22 @@ async def rec(n): self.assertEqual(asyncio.run(rec(3)), ["rec"] * 4) + @skip_if_not_supported + def test_future_waiter_has_empty_stack(self): + async def main(): + task = asyncio.current_task() + future = asyncio.get_running_loop().create_future() + asyncio.future_add_to_awaited_by(task, future) + try: + trace = RemoteUnwinder(os.getpid()).get_all_awaited_by() + task_info = self._get_task_id_map(trace)[id(task)] + self.assertEqual(task_info.awaited_by, [([], id(future))]) + self.assertTrue(task_info.coroutine_stack[0].call_stack) + finally: + asyncio.future_discard_from_awaited_by(task, future) + + asyncio.run(main()) + @skip_if_not_supported def test_async_generator_stack_is_not_truncated(self): # gh-159620 @@ -575,6 +610,73 @@ async def main(): stacks = self._extract_coroutine_funcnames(asyncio.run(main())) self.assertEqual(stacks["anext"], ["agen"]) + @skip_if_not_supported + def test_future_between_tasks(self): + # gh-161502 + from asyncio.tools import build_async_tree + + async def worker(method): + return getattr(RemoteUnwinder(os.getpid()), method)() + + async def main(method): + parent = asyncio.current_task() + parent.set_name("main") + future = asyncio.Future() + task = asyncio.create_task(worker(method), name="worker") + asyncio.future_add_to_awaited_by(task, future) + task.add_done_callback(future.set_result) + try: + await future + return task.result(), { + id(parent): [], + id(future): [id(parent)], + id(task): [id(future)], + } + finally: + asyncio.future_discard_from_awaited_by(task, future) + + for method in ("get_all_awaited_by", "get_async_stack_trace"): + with self.subTest(method=method): + snapshot, expected = asyncio.run(main(method)) + tree, = build_async_tree(snapshot) + rendered = "\n".join(tree) + self.assertIn("(T) main", rendered) + self.assertIn("(F) Future-", rendered) + self.assertIn("(T) worker", rendered) + relationships = { + node.task_id: [edge.task_name for edge in node.awaited_by] + for thread in snapshot for node in thread.awaited_by + } + self.assertEqual(relationships, expected) + + @skip_if_not_supported + def test_shared_future_waiter(self): + async def main(): + layers = [[asyncio.current_task()]] + for _ in range(14): + waiters = [asyncio.Future(), asyncio.Future()] + for child in layers[-1]: + for waiter in waiters: + asyncio.future_add_to_awaited_by(child, waiter) + self.addCleanup(asyncio.future_discard_from_awaited_by, + child, waiter) + layers.append(waiters) + + leaf, root = layers[-1][0], layers[0][0] + asyncio.future_add_to_awaited_by(leaf, root) + self.addCleanup(asyncio.future_discard_from_awaited_by, leaf, root) + + expected_ids = [id(node) for layer in layers for node in layer] + unwinder = RemoteUnwinder(os.getpid()) + for method in (unwinder.get_all_awaited_by, + unwinder.get_async_stack_trace): + with self.subTest(method=method.__name__): + ids = [node.task_id for thread in method() + for node in thread.awaited_by] + self.assertCountEqual(ids, expected_ids) + + asyncio.run(main()) + @skip_if_not_supported def test_async_generator_running_task_stack(self): # gh-159620 @@ -663,6 +765,95 @@ async def main(): stacks = self._extract_coroutine_funcnames(asyncio.run(main())) self.assertEqual(stacks["consumer"], ["consumer"]) + @skip_if_not_supported + def test_running_task_stack_between_steps(self): + # gh-159645 + traces = [] + + class Loop(asyncio.SelectorEventLoop): + def call_soon(self, *args, **kwargs): + if asyncio.current_task(self) is not None: + unwinder = RemoteUnwinder(os.getpid()) + traces.append(unwinder.get_async_stack_trace()) + return super().call_soon(*args, **kwargs) + + async def child(): + await asyncio.sleep(0) + + async def main(): + await asyncio.create_task(child(), name="child") + + asyncio.run(main(), loop_factory=Loop) + stacks = [list(self._extract_coroutine_funcnames(trace).values()) + for trace in traces] + self.assertIn([["__sleep0", "sleep", "child"], ["main"]], stacks) + self.assertIn([[], ["main"]], stacks) + + @skip_if_not_supported + def test_running_task_frame_not_in_thread_stack(self): + # gh-159645 + ready = threading.Event() + release = threading.Event() + + async def child(): + ready.set() + release.wait() + await asyncio.sleep(0) + + async def main(): + loop = asyncio.get_running_loop() + coro = child() + task = asyncio.create_task(coro, name="child") + thread = threading.Thread(target=coro.send, args=(None,)) + with threading_helper.start_threads([thread], unlock=release.set): + self.assertTrue(ready.wait(SHORT_TIMEOUT)) + self.assertTrue(coro.cr_running) + previous = tasks._swap_current_task(loop, task) + try: + trace = RemoteUnwinder(os.getpid()).get_async_stack_trace() + finally: + tasks._swap_current_task(loop, previous) + await task + return trace + + stacks = self._extract_coroutine_funcnames(asyncio.run(main())) + self.assertEqual(stacks["child"], ["child"]) + + @skip_if_not_supported + def test_running_task_with_non_native_coroutine(self): + # gh-159645 + class Wrapper(collections.abc.Coroutine): + def __init__(self, coro): + self._coro = coro + def send(self, value): + return self._coro.send(value) + def throw(self, *args): + return self._coro.throw(*args) + def close(self): + return self._coro.close() + def __await__(self): + return self._coro.__await__() + + async def inner(): + return RemoteUnwinder(os.getpid()).get_async_stack_trace() + + async def main(): + return await asyncio.create_task(Wrapper(inner()), name="wrapper") + + stacks = self._extract_coroutine_funcnames(asyncio.run(main())) + self.assertEqual(stacks["wrapper"], []) + + @skip_if_not_supported + def test_self_trace_with_empty_linetable(self): + def sample(): + return RemoteUnwinder(os.getpid()).get_stack_trace() + + sample.__code__ = sample.__code__.replace(co_linetable=b"") + frame = self._find_frame_in_trace( + sample(), lambda f: f.funcname == sample.__qualname__ + ) + self.assertEqual(frame.location, (-1, -1, -1, -1)) + @skip_if_not_supported @unittest.skipIf( sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED, @@ -1280,11 +1471,14 @@ async def main(): relationships = self._get_awaited_by_relationships( stack_trace ) + future_name, = (node.task_name for node in stack_trace[0].awaited_by + if node.is_future) self.assertEqual( relationships, { "Task-1": set(), - "Task-2": {"Task-1"}, + "Task-2": {future_name}, + future_name: {"Task-1"}, }, ) @@ -1298,6 +1492,7 @@ async def main(): coroutine_stacks, { "Task-1": [((script_name, 21, "main", None),)], + future_name: [()], "Task-2": [ ( (script_name, 11, "deep", None), @@ -1325,7 +1520,8 @@ async def main(): awaited_by_coroutine_stacks, { "Task-1": [], - "Task-2": [ + "Task-2": [(future_name, ())], + future_name: [ ("Task-1", ((script_name, 21, "main", None),)) ], }, @@ -2024,7 +2220,7 @@ def test_empty_native_thread_stack(self): "Test only runs on Linux with process_vm_readv support", ) def test_popping_python_frame_is_not_native(self): - script = """\ + script_body = """\ def leaf(depth): if depth: leaf(depth - 1) @@ -2032,15 +2228,11 @@ def leaf(depth): while True: leaf(300) """ - with _managed_subprocess([sys.executable, "-c", script]) as process: - for _ in busy_retry(SHORT_TIMEOUT): - try: - unwinder = RemoteUnwinder( - process.pid, native=True, gc=False, cache_frames=False, - ) - except RuntimeError: - continue - break + + with self._target_process(script_body) as (process, _, _): + unwinder = RemoteUnwinder( + process.pid, native=True, gc=False, cache_frames=False, + ) # The unwinder can attach before the main loop starts. _get_stack_trace_with_retry( unwinder, @@ -3736,6 +3928,30 @@ def gen(): skip_non_matching_threads=True, ) + @requires_gil_enabled("Free threaded builds don't have an 'active thread'") + def test_filtered_active_thread_is_not_replaced(self): + samples = [] + + def sample(): + for kwargs in ({"all_threads": True}, {"only_active_thread": True}): + unwinder = RemoteUnwinder( + os.getpid(), mode=PROFILING_MODE_EXCEPTION, **kwargs + ) + samples.append( + self._get_thread_statuses(unwinder.get_stack_trace()) + ) + + thread = threading.Thread(target=sample) + try: + raise ValueError + except ValueError: + thread.start() + thread.join() + + all_threads, active_thread = samples + self.assertEqual(list(all_threads), [threading.get_native_id()]) + self.assertEqual(active_thread, {}) + @requires_remote_subprocess_debugging() class TestFrameCaching(RemoteInspectionTestBase): @@ -4687,6 +4903,28 @@ def test_dead_process_raises_not_degrades(self): else: self.fail("ProcessLookupError never raised for dead process") + def test_dead_process_before_first_async_read(self): + script_body = """\ + import asyncio + sock.sendall(b"ready") + sock.recv(16) + """ + with self._target_process(script_body) as (p, client_socket, _): + _wait_for_signal(client_socket, b"ready") + unwinders = [ + RemoteUnwinder(p.pid, debug=debug) + for debug in (False, True) + ] + + p.kill() + p.wait() + + for debug, unwinder in zip((False, True), unwinders): + for method in ("get_async_stack_trace", "get_all_awaited_by"): + with self.subTest(debug=debug, method=method): + with self.assertRaises(ProcessLookupError): + getattr(unwinder, method)() + @requires_remote_subprocess_debugging() class TestFrameChainLimits(RemoteInspectionTestBase): @@ -4721,6 +4959,37 @@ def _assert_unwinder_limit_error(self, unwind, expected_substring): f"{last_error!r}" ) + @skip_if_not_supported + def test_get_all_awaited_by_at_waiter_limit(self): + # gh-161502 + async def main(): + task = asyncio.current_task() + limit = self.TASK_WAITER_WALK_TASKS - 1 + for _ in range(limit): + future = asyncio.Future() + asyncio.future_add_to_awaited_by(task, future) + self.addCleanup(asyncio.future_discard_from_awaited_by, task, future) + + release = asyncio.Event() + child = asyncio.create_task(release.wait()) + waiter = asyncio.create_task(asyncio.wait([child]), eager_start=True) + unwinder = RemoteUnwinder(os.getpid()) + snapshot = unwinder.get_all_awaited_by() + count = sum(node.is_future for thread in snapshot + for node in thread.awaited_by) + self.assertEqual(count, limit) + + extra = asyncio.Future() + asyncio.future_add_to_awaited_by(task, extra) + self.addCleanup(asyncio.future_discard_from_awaited_by, task, extra) + with self.assertRaisesRegex(RuntimeError, "Too many task waiters"): + unwinder.get_all_awaited_by() + + release.set() + await waiter + + asyncio.run(main()) + @skip_if_not_supported @unittest.skipIf( sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED, diff --git a/Lib/test/test_fnmatch.py b/Lib/test/test_fnmatch.py index 5daaf3b3fddb9e..ab3561f84d568d 100644 --- a/Lib/test/test_fnmatch.py +++ b/Lib/test/test_fnmatch.py @@ -1,6 +1,7 @@ """Test cases for the fnmatch module.""" import os +import pathlib import string import unittest import warnings @@ -324,10 +325,23 @@ def test_filter(self): self.assertEqual(filter([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*'), [b'Python', b'Perl']) + def test_filter_pathlike(self): + names = [pathlib.Path("Python"), pathlib.Path("Ruby")] + self.assertEqual(filter(names, "P*"), [names[0]]) + def test_mix_bytes_str(self): self.assertRaises(TypeError, filter, ['test'], b'*') self.assertRaises(TypeError, filter, [b'test'], '*') + def test_mix_bytes_str_pathlike(self): + path = pathlib.Path("test") + self.assertRaises(TypeError, filter, [path], b'*') + self.assertRaises(TypeError, filter, [path, b'test'], '*') + self.assertRaises(TypeError, filter, [b'test', path], '*') + self.assertRaises(TypeError, filter, [path, 'test'], b'*') + self.assertRaises(TypeError, filter, ['test', path], b'*') + self.assertEqual(filter([path, 'other'], 'te*'), [path]) + def test_case(self): self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'), ['Test.py', 'Test.PL'] if IGNORECASE else ['Test.py']) @@ -349,10 +363,23 @@ def test_filterfalse(self): actual = filterfalse([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*') self.assertListEqual(actual, [b'Ruby', b'Tcl']) + def test_filterfalse_pathlike(self): + names = [pathlib.Path("Python"), pathlib.Path("Ruby")] + self.assertEqual(filterfalse(names, "P*"), [names[1]]) + def test_mix_bytes_str(self): self.assertRaises(TypeError, filterfalse, ['test'], b'*') self.assertRaises(TypeError, filterfalse, [b'test'], '*') + def test_mix_bytes_str_pathlike(self): + path = pathlib.Path("test") + self.assertRaises(TypeError, filterfalse, [path], b'*') + self.assertRaises(TypeError, filterfalse, [path, b'test'], '*') + self.assertRaises(TypeError, filterfalse, [b'test', path], '*') + self.assertRaises(TypeError, filterfalse, [path, 'test'], b'*') + self.assertRaises(TypeError, filterfalse, ['test', path], b'*') + self.assertEqual(filterfalse([path, 'other'], 'te*'), ['other']) + def test_case(self): self.assertEqual(filterfalse(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'), ['Test.rb'] if IGNORECASE else ['Test.rb', 'Test.PL']) diff --git a/Lib/test/test_frame.py b/Lib/test/test_frame.py index a0a11966ebf3c1..271694297966c8 100644 --- a/Lib/test/test_frame.py +++ b/Lib/test/test_frame.py @@ -780,6 +780,50 @@ def g(xs): self.assertEqual(f(), 2) + def test_setitem_key_closes_generator(self): + # gh-162593: the key lookup can run code which closes the generator + # and moves its frame into the frame object. + class X: + pass + + def gen(): + a = X() + yield + + g = gen() + next(g) + proxy = g.gi_frame.f_locals + old = proxy['a'] + ref = weakref.ref(old) + + class Key(str): + def __hash__(self): + g.close() + return super().__hash__() + + proxy[Key('a')] = 'new' + self.assertEqual(proxy['a'], 'new') + del proxy + support.gc_collect() + self.assertIs(ref(), old) + + def test_setitem_free_var_cleared_frame(self): + def outer(): + c = 1 + def gen(): + c + yield sys._getframe() + return gen() + + g = outer() + frame = next(g) + proxy = frame.f_locals + g.close() + frame.clear() + self.assertNotIn('c', proxy) + proxy['c'] = 2 + self.assertEqual(proxy['c'], 2) + class FrameLocalsProxyMappingTests(mapping_tests.TestHashMappingProtocol): """Test that FrameLocalsProxy behaves like a Mapping (with exceptions)""" @@ -867,13 +911,13 @@ def PyFrame_GetLocals(frame: ctypes.py_object) -> ctypes.py_object: pass frame_locals = PyEval_GetFrameLocals() - self.assertTrue(type(frame_locals), dict) + self.assertIs(type(frame_locals), dict) self.assertEqual(frame_locals['x'], 1) frame_locals['x'] = 2 self.assertEqual(x, 1) frame_globals = PyEval_GetFrameGlobals() - self.assertTrue(type(frame_globals), dict) + self.assertIs(type(frame_globals), dict) self.assertIs(frame_globals, globals()) frame_builtins = PyEval_GetFrameBuiltins() @@ -881,7 +925,7 @@ def PyFrame_GetLocals(frame: ctypes.py_object) -> ctypes.py_object: frame = sys._getframe() f_locals = PyFrame_GetLocals(frame) - self.assertTrue(f_locals['x'], 1) + self.assertEqual(f_locals['x'], 1) f_locals['x'] = 2 self.assertEqual(x, 2) diff --git a/Lib/test/test_free_threading/test_abc.py b/Lib/test/test_free_threading/test_abc.py new file mode 100644 index 00000000000000..75b181b8dd175d --- /dev/null +++ b/Lib/test/test_free_threading/test_abc.py @@ -0,0 +1,103 @@ +import gc +import os +import threading +import unittest +from abc import ABC + +from test.support import threading_helper + + +class TestABC(unittest.TestCase): + + @threading_helper.reap_threads + @threading_helper.requires_working_threading() + def test_concurrent_checks(self): + # isinstance()/issubclass() from many threads while other threads + # register classes, clear the caches and create and destroy + # subclasses. + class A(ABC): + pass + class B(A): + pass + class C(B): + pass + class Registered: + pass + class Other: + pass + A.register(Registered) + instances = [B(), C(), Registered(), Other()] + num_threads = 8 + iterations = 2000 + barrier = threading.Barrier(num_threads + 2) + stop = threading.Event() + errors = [] + + def checker(): + barrier.wait() + try: + for _ in range(iterations): + for cls in (A, B, C): + self.assertTrue(issubclass(C, cls)) + self.assertFalse(issubclass(Other, cls)) + self.assertFalse(isinstance(instances[3], cls)) + self.assertTrue(isinstance(instances[0], A)) + self.assertTrue(isinstance(instances[1], A)) + self.assertTrue(isinstance(instances[2], A)) + self.assertTrue(issubclass(Registered, A)) + self.assertFalse(isinstance(instances[0], C)) + except Exception as exc: + errors.append(exc) + finally: + stop.set() + + def mutator(): + barrier.wait() + i = 0 + while not stop.is_set(): + # Transient classes get cached, registered and destroyed. + Sub = type(f'Sub{i}', (B,), {}) + Plain = type(f'Plain{i}', (), {}) + self.assertTrue(issubclass(Sub, A)) + self.assertFalse(issubclass(Plain, A)) + A.register(Plain) + self.assertTrue(issubclass(Plain, A)) + if i % 10 == 0: + A._abc_caches_clear() + B._abc_caches_clear() + if i % 50 == 0: + gc.collect() + i += 1 + + threads = [threading.Thread(target=checker) for _ in range(num_threads)] + threads += [threading.Thread(target=mutator) for _ in range(2)] + with threading_helper.start_threads(threads): + pass + self.assertEqual(errors, []) + + @threading_helper.reap_threads + @threading_helper.requires_working_threading() + def test_concurrent_registry_clear(self): + class A(ABC): + pass + classes = [type(f'R{i}', (), {}) for i in range(32)] + barrier = threading.Barrier(4) + + def worker(): + barrier.wait() + for _ in range(500): + for cls in classes: + A.register(cls) + issubclass(cls, A) + A._abc_registry_clear() + A._abc_caches_clear() + with open(os.devnull, 'w') as f: + A._dump_registry(f) + + threads = [threading.Thread(target=worker) for _ in range(4)] + with threading_helper.start_threads(threads): + pass + + +if __name__ == "__main__": + unittest.main() diff --git a/Lib/test/test_free_threading/test_collections.py b/Lib/test/test_free_threading/test_collections.py index a0a73c965d072a..d6efde55e698d0 100644 --- a/Lib/test/test_free_threading/test_collections.py +++ b/Lib/test/test_free_threading/test_collections.py @@ -1,5 +1,5 @@ import unittest -from collections import OrderedDict, deque +from collections import Counter, OrderedDict, deque from copy import copy from test.support import threading_helper @@ -49,6 +49,19 @@ def mutate(): ) +class TestCounter(unittest.TestCase): + def test_update_concurrent(self): + # gh-151633: concurrent Counter.update calls must not cause use-after-free + NTHREADS = 4 + PER_THREAD = 5000 + c = Counter() + data = ('x',) * PER_THREAD + threading_helper.run_concurrently( + c.update, nthreads=NTHREADS, args=(data,) + ) + self.assertEqual(c['x'], NTHREADS * PER_THREAD) + + class TestOrderedDict(unittest.TestCase): def test_iterator_update_clear_race(self): # gh-151627: OrderedDict iterator construction must not race with diff --git a/Lib/test/test_free_threading/test_dict.py b/Lib/test/test_free_threading/test_dict.py index faf0fa96f07a15..47fe8b2b0bc634 100644 --- a/Lib/test/test_free_threading/test_dict.py +++ b/Lib/test/test_free_threading/test_dict.py @@ -565,6 +565,51 @@ def race(iter_times: int, CHURN_SIZE = 512 race(ITER_TIMES, CHURN_SIZE, APPENDER_THREADS, REPLACER_THREADS) + def test_racing_shared_iterator_exhaustion(self): + # gh-154130: exhausting a dict iterator shared between threads must + # not release the iterator's reference to the dict more than once. + makers = [ + lambda d: iter(d), + lambda d: iter(d.values()), + lambda d: iter(d.items()), + lambda d: reversed(d), + lambda d: reversed(d.items()), + ] + for make in makers: + cell = [make({i: i for i in range(8)})] + + def worker(): + for _ in range(5000): + it = cell[0] + try: + next(it) + except StopIteration: + cell[0] = make({i: i for i in range(8)}) + except RuntimeError: + # Concurrent next() calls may observe each other's + # updates as a modification; only crashes are bugs. + pass + + with self.subTest(make=make): + threading_helper.run_concurrently(worker, nthreads=4) + + def test_exhausted_iterator_stays_exhausted(self): + d = {1: 1} + for it in (iter(d), iter(d.items()), reversed(d)): + self.assertEqual(len(list(it)), 1) + d[2] = 2 + self.assertRaises(StopIteration, next, it) + self.assertEqual(it.__length_hint__(), 0) + del d[2] + + def test_reversed_empty_dict_iterator_not_exhausted(self): + # A reverse iterator over an empty dict starts at position -1 but is + # not exhausted, so a later size change must still be detected. + d = {} + it = reversed(d) + d[1] = 1 + self.assertRaises(RuntimeError, next, it) + def test_racing_update_from_mutating_pair(self): # gh-159244: dict() must not read the items of a key/value pair # that another thread empties concurrently. diff --git a/Lib/test/test_free_threading/test_frame.py b/Lib/test/test_free_threading/test_frame.py index bea49df557aa2c..82d1bf97d985fa 100644 --- a/Lib/test/test_free_threading/test_frame.py +++ b/Lib/test/test_free_threading/test_frame.py @@ -1,9 +1,10 @@ import functools +import gc import sys import threading import unittest -from test.support import threading_helper +from test.support import import_helper, threading_helper threading_helper.requires_working_threading(module=True) @@ -122,6 +123,267 @@ def writer(frame): run_with_frame([reader, writer, reader, writer]) + def test_concurrent_f_locals_read_values(self): + def runner(): + a = 1 + b = "hello" + c = [1, 2, 3] + for i in range(100): + a += i + + def reader(frame): + locals_dict = frame.f_locals + list(locals_dict.keys()) + list(locals_dict.values()) + + run_with_frame(reader, runner=runner) + + def test_concurrent_f_locals_write(self): + def runner(): + x = 0 + for i in range(100): + x += i + + def writer(frame): + frame.f_locals["new_var"] = 42 + + run_with_frame(writer, runner=runner) + + def test_concurrent_f_locals_read_write(self): + def runner(): + a = 1 + b = 2 + for i in range(100): + a += i + + def reader(frame): + _ = frame.f_locals.get("a") + _ = frame.f_locals.get("b") + + def writer(frame): + frame.f_locals["a"] = 42 + + run_with_frame([reader, writer, reader, writer], runner=runner) + + def test_concurrent_f_locals_iteration(self): + def runner(): + a = 1 + b = "hello" + c = [1, 2, 3] + for i in range(100): + a += i + + def iterator(frame): + for key, value in frame.f_locals.items(): + pass + + run_with_frame(iterator, runner=runner) + + def test_gen_f_locals_read_while_running(self): + # gh-144446: reading f_locals of a generator frame while the + # generator is executing on another thread. + for _ in range(5): + def gen_fn(): + x = 0 + obj = None + s = None + yield + for i in range(2000): + obj = [i] * 4 + s = str(i) * 8 + x += i + yield x + + g = gen_fn() + next(g) + frame = g.gi_frame + barrier = threading.Barrier(3) + + def runner(): + barrier.wait() + next(g) + + def reader(): + barrier.wait() + for _ in range(100): + fl = frame.f_locals + list(fl.values()) + fl.get("obj") + fl.get("s") + len(fl) + + threading_helper.run_concurrently([runner, reader, reader]) + g.close() + + def test_gen_f_locals_write_suspended(self): + # Writes through f_locals must be synchronized with resuming. + for _ in range(5): + def gen_fn(): + x = 0 + extra = None + while True: + x += 1 + yield x + + g = gen_fn() + next(g) + frame = g.gi_frame + barrier = threading.Barrier(3) + + def runner(): + barrier.wait() + for _ in range(500): + next(g) + + def writer(): + barrier.wait() + for i in range(200): + frame.f_locals["extra"] = [i] + frame.f_locals["new_var"] = i + + threading_helper.run_concurrently([runner, writer, writer]) + g.close() + + def test_gen_f_locals_dying_generator(self): + # Access f_locals while the last reference to the generator is + # dropped and the frame ownership moves to the frame object. + for _ in range(20): + def gen_fn(): + x = 42 + yield x + + g = gen_fn() + next(g) + frame = g.gi_frame + barrier = threading.Barrier(3) + ref = [g] + del g + + def dropper(): + barrier.wait() + ref.clear() + + def reader(): + barrier.wait() + for _ in range(100): + frame.f_locals.get("x") + list(frame.f_locals.values()) + + threading_helper.run_concurrently([dropper, reader, reader]) + + def test_gen_setitem_cell_old_value_destructor_stw(self): + # the old cell value must be released after the world restarts + deleted = [] + + def make_gen(): + x = None + def gen_fn(): + nonlocal x + yield x + return gen_fn() + + g = make_gen() + next(g) + frame = g.gi_frame + + class Old: + def __del__(self): + deleted.append(frame.f_locals.get("x")) + + frame.f_locals["x"] = Old() + frame.f_locals["x"] = "new" # replace cell value: destructor runs + self.assertEqual(deleted, ["new"]) + g.close() + + def test_gen_f_locals_key_runs_python_code(self): + # key's __hash__ and __eq__ must not run with the world stopped + class Key: + def __init__(self, value): + self.value = value + + def __hash__(self): + gc.collect() + return hash(self.value) + + def __eq__(self, other): + gc.collect() + if isinstance(other, Key): + other = other.value + return self.value == other + + def gen_fn(): + x = 1 + yield + + g = gen_fn() + next(g) + f_locals = g.gi_frame.f_locals + self.assertEqual(f_locals[Key("x")], 1) + self.assertIn(Key("x"), f_locals) + f_locals[Key("x")] = 2 + self.assertEqual(f_locals["x"], 2) + with self.assertRaises(ValueError): + f_locals.pop(Key("x")) + + self.assertNotIn(Key("extra"), f_locals) + f_locals[Key("extra")] = 3 + self.assertEqual(f_locals[Key("extra")], 3) + self.assertIn(Key("extra"), f_locals) + self.assertEqual(f_locals.pop(Key("extra")), 3) + with self.assertRaises(KeyError): + f_locals[Key("extra")] + g.close() + + def test_concurrent_f_locals_extra_iteration(self): + # iterate the extra locals while they are modified + frame = (lambda: sys._getframe())() + + def writer(): + for i in range(5000): + frame.f_locals[f"k{i % 32}"] = [i] + frame.f_locals.pop(f"k{i * 7 % 32}", None) + + def reader(): + for _ in range(2000): + f_locals = frame.f_locals + list(f_locals.keys()) + list(f_locals.values()) + list(f_locals.items()) + + threading_helper.run_concurrently([writer, writer, reader, reader]) + + def test_gen_getvar_while_running(self): + # PyFrame_GetVar() reads fast locals and must synchronize with + # the frame's owner as well. + _testcapi = import_helper.import_module("_testcapi") + for _ in range(5): + def gen_fn(): + obj = None + yield + for i in range(2000): + obj = [i] * 4 + yield obj + + g = gen_fn() + next(g) + frame = g.gi_frame + barrier = threading.Barrier(3) + + def runner(): + barrier.wait() + next(g) + + def reader(): + barrier.wait() + for _ in range(100): + try: + _testcapi.frame_getvar(frame, "obj") + except NameError: + pass + + threading_helper.run_concurrently([runner, reader, reader]) + g.close() + def test_concurrent_frame_clear(self): # Test race between frame.clear() and attribute reads. def create_frame(): diff --git a/Lib/test/test_free_threading/test_gc.py b/Lib/test/test_free_threading/test_gc.py index b287b51826999c..8c97b04ed4f71b 100644 --- a/Lib/test/test_free_threading/test_gc.py +++ b/Lib/test/test_free_threading/test_gc.py @@ -9,6 +9,7 @@ from test import support from test.support import threading_helper +from test.support.script_helper import assert_python_ok class MyObj: @@ -110,6 +111,35 @@ def run(self): self.assertTrue(C().run()) + def test_debug_collectable_python_stderr(self): + # The DEBUG_COLLECTABLE output is written with sys.stderr.write(), + # which may run Python code or wait for another thread (e.g. for the + # lock of a buffered stream), so it must not be written while the + # world is stopped. Debug builds assert that the world is not stopped + # when Python code runs, so use a sys.stderr written in Python. + code = """if 1: + import gc + import io + import sys + + class Stderr(io.StringIO): + def write(self, s): + return super().write(s) + + sys.stderr = Stderr() + gc.set_debug(gc.DEBUG_COLLECTABLE) + a = [] + a.append(a) + del a + gc.collect() + gc.set_debug(0) + output = sys.stderr.getvalue() + sys.stderr = sys.__stderr__ + print(output) + """ + _, out, _ = assert_python_ok("-c", code) + self.assertRegex(out, b"gc: collectable ") + def test_merge_brc_queue_of_detached_thread(self): # GH-157838: objects queued for merging by a thread that is detached # (blocked in a lock acquire, sleep, etc.) are merged and freed on its diff --git a/Lib/test/test_free_threading/test_iteration.py b/Lib/test/test_free_threading/test_iteration.py index 540e8edcc67628..bf42304b41e8b9 100644 --- a/Lib/test/test_free_threading/test_iteration.py +++ b/Lib/test/test_free_threading/test_iteration.py @@ -1,3 +1,4 @@ +import itertools import sys import threading import unittest @@ -170,6 +171,31 @@ def drain(it, barrier, results): self.assertEqual(sys.getrefcount(seq), refcount_before) +class ContendedCallIterExhaustionTest(unittest.TestCase): + """Test exhausting a shared iter(callable, sentinel) iterator. + + The values seen by each thread are unspecified, but exhausting the + iterator from several threads at once must not crash. + """ + + def test_shared_iterator_exhaustion(self): + niters = 2000 if support.check_sanitizer(thread=True) else 20000 + + def make_iter(): + counter = itertools.count().__next__ + return iter(lambda: counter(), 3) + + shared = [make_iter()] + + def worker(): + for _ in range(niters): + it = shared[0] + if next(it, None) is None: + shared[0] = make_iter() + + threading_helper.run_concurrently(worker, nthreads=NUMTHREADS) + + class ContendedRangeIterationTest(ContendedTupleIterationTest): def make_testdata(self, n): return range(n) diff --git a/Lib/test/test_free_threading/test_qsbr.py b/Lib/test/test_free_threading/test_qsbr.py new file mode 100644 index 00000000000000..ec2a7454ca2907 --- /dev/null +++ b/Lib/test/test_free_threading/test_qsbr.py @@ -0,0 +1,72 @@ +import threading +import unittest + +from test.support import threading_helper + + +class Holder: + pass + + +def make_shared(func, *args): + t = threading.Thread(target=func, args=args) + t.start() + t.join() + + +@threading_helper.requires_working_threading() +class TestDelayedFreeReentrancy(unittest.TestCase): + ROUNDS = 1000 + + def queue_finalizers(self, callback, count=ROUNDS): + class Finalizer: + def __del__(self): + callback() + + holders = [Holder() for _ in range(count)] + for h in holders: + h.__dict__ = {"f": Finalizer()} + return holders + + def test_dict_resize(self): + d = {} + make_shared(d.get, 0) + holders = self.queue_finalizers(lambda: d.__setitem__(object(), 0), + count=100) + for h in holders: + h.__dict__ = {} + for _ in range(self.ROUNDS): + for k in list(d): + del d[k] + for i in range(6): + d[i] = i + self.assertEqual(len(d), sum(1 for _ in d)) + + def test_list_resize(self): + L = [] + make_shared(L.append, 0) + holders = self.queue_finalizers(L.clear) + for n, h in enumerate(holders): + h.__dict__ = {} + if n % 2: + h.__dict__ = {} + L.clear() + for i in range(9): + L.insert(len(L), i) + self.assertEqual(len(L), len(list(L))) + + def test_type_cache_invalidation(self): + class C: + x = None + + holders = self.queue_finalizers(lambda: C.x) + for h in holders: + h.__dict__ = {} + C.x + new = object() + C.x = new + self.assertIs(C.x, new) + + +if __name__ == "__main__": + unittest.main() diff --git a/Lib/test/test_free_threading/test_races.py b/Lib/test/test_free_threading/test_races.py index f08de7496eb526..dc1617512f8795 100644 --- a/Lib/test/test_free_threading/test_races.py +++ b/Lib/test/test_free_threading/test_races.py @@ -288,6 +288,22 @@ def set_recursion_limit(): do_race(something_recursive, set_recursion_limit) + def test_racing_intern_queued_for_merge(self): + # gh-152276: an object may become immortal while it is queued for + # its owning thread to merge its reference count. + # The same strings are used on every run, so that reruns do not + # create new immortal strings. + for i in range(10): + strings = [f"test_racing_intern_queued_for_merge_{i}_{j}" + for j in range(1000)] + shared = list(strings) + # Dropping the references from another thread queues the strings + # to be merged by this thread, which is busy interning them. + thread = threading.Thread(target=shared.clear) + thread.start() + list(map(sys.intern, strings)) + thread.join() + @threading_helper.requires_working_threading() class TestWarningsRaces(TestBase): diff --git a/Lib/test/test_free_threading/test_set.py b/Lib/test/test_free_threading/test_set.py index 9dd3d68d5dad13..639bc729fa31ac 100644 --- a/Lib/test/test_free_threading/test_set.py +++ b/Lib/test/test_free_threading/test_set.py @@ -159,5 +159,72 @@ class LargeSetTest(RaceTestBase, unittest.TestCase): SET_SIZE = 20 # larger than PySet_MINSIZE +class TestSetIteratorExhaustion(unittest.TestCase): + def test_racing_shared_iterator_exhaustion(self): + # gh-160573: exhausting a set iterator shared between threads must + # not release the iterator's reference to the set more than once. + for tp in (set, frozenset): + cell = [iter(tp(range(8)))] + + def worker(): + for _ in range(5000): + it = cell[0] + try: + next(it) + except StopIteration: + cell[0] = iter(tp(range(8))) + + with self.subTest(tp=tp): + threading_helper.run_concurrently(worker, nthreads=4) + + def test_racing_shared_iterator_exhaustion_contended(self): + # Same as above, but the set's lock is contended: other threads + # keep mutating and iterating the set while its iterator is shared. + s = set(range(8)) + cell = [iter(s)] + done = Event() + + def exhaust(): + for _ in range(5000): + it = cell[0] + try: + next(it) + except StopIteration: + cell[0] = iter(s) + except RuntimeError: + # The set changed size during iteration. + cell[0] = iter(s) + + def mutate(): + while not done.is_set(): + s.add(100) + s.discard(100) + + def iterate(): + while not done.is_set(): + try: + for _ in s: + pass + except RuntimeError: + pass + + def run_exhaust(): + try: + threading_helper.run_concurrently(exhaust, nthreads=4) + finally: + done.set() + + threading_helper.run_concurrently([run_exhaust, mutate, iterate]) + + def test_exhausted_iterator_stays_exhausted(self): + s = {1} + it = iter(s) + self.assertEqual(list(it), [1]) + s.add(2) + self.assertRaises(StopIteration, next, it) + self.assertEqual(it.__length_hint__(), 0) + self.assertEqual(list(it.__reduce__()[1][0]), []) + + if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_free_threading/test_type.py b/Lib/test/test_free_threading/test_type.py index 6e750e0bc6084e..f7e4adf2e02ce8 100644 --- a/Lib/test/test_free_threading/test_type.py +++ b/Lib/test/test_free_threading/test_type.py @@ -375,6 +375,7 @@ def test_concurrent_setattr_deadlock(self): # dict mutex and waited for the type lock. # This is fairly difficult to trigger the race but this N seems to do # it at least sometimes. + type_freeze = import_helper.import_module("_testlimitedcapi").type_freeze N = 200 done = False @@ -401,6 +402,15 @@ def lister(): while not done: Base.__subclasses__() + def freezer(): + # gh-158194: Exercise type freezing concurrently with slot + # updates and competing stop-the-world requests. + barrier.wait() + for _ in range(10): + class Frozen: + pass + type_freeze(Frozen) + def basesetter(): nonlocal done barrier.wait() @@ -418,7 +428,7 @@ class B(A): # to keep the type lock and the stop-the-world mutex contended, which # is what gets the setters into the window where it happens. targets = (setter, setter, subclasser, subclasser, - lister, lister, basesetter) + lister, lister, basesetter, freezer) barrier = threading.Barrier(len(targets)) threads = [Thread(target=target) for target in targets] with threading_helper.start_threads(threads): diff --git a/Lib/test/test_gdb/util.py b/Lib/test/test_gdb/util.py index 29c7e9ee8f0549..ffe92137d844b7 100644 --- a/Lib/test/test_gdb/util.py +++ b/Lib/test/test_gdb/util.py @@ -11,9 +11,16 @@ GDB_PROGRAM = shutil.which('gdb') or 'gdb' +# First, try to use the build directory to support out-of-tree build +for key in ('abs_builddir', 'abs_srcdir', 'srcdir'): + SRC_DIR = sysconfig.get_config_var(key) + if SRC_DIR: + break +else: + raise Exception("unable to get the Python source directory") + # Location of custom hooks file in a repository checkout. -CHECKOUT_HOOK_PATH = os.path.join(os.path.dirname(sys.executable), - 'python-gdb.py') +CHECKOUT_HOOK_PATH = os.path.join(SRC_DIR, 'python-gdb.py') SAMPLE_SCRIPT = os.path.join(os.path.dirname(__file__), 'gdb_sample.py') BREAKPOINT_FN = 'builtin_id' diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index a718be415b9905..eff95fd8aa84f6 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -1236,6 +1236,11 @@ class KDFTests(unittest.TestCase): (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt', 4096, -1), (b'pass\0word', b'sa\0lt', 4096, 16), + # more vectors that don't trip on self.fips_incompatible(...) + (b'password', b'salt'*4, 1024, None), + (b'password', b'salt'*4, 2048, None), + (b'password', b'salt'*4, 4096, None), + (b'pass\0word', b'sa\0lt'*4, 4096, 16), ] pbkdf2_results = { @@ -1247,7 +1252,14 @@ class KDFTests(unittest.TestCase): #(bytes.fromhex('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None), (bytes.fromhex('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c' 'f2f07038'), 25), - (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None),], + (bytes.fromhex('56fa6aa75548099dcc37d7f03425e0c3'), None), + # more test vectors (not from RFC 6070) + (bytes.fromhex('1a39e29b04aa0488865b1e80fb125a8063e30b92'), None), + (bytes.fromhex('f19c46e6f912bc53a7b315ad4d3b717abe708c07'), None), + (bytes.fromhex('b7845a91aea7946bc875fcd9d19fd515381ddf94'), None), + (bytes.fromhex('bcc0d390b6671f99995e4c64223fa7c1fe12122460' + '94abe408'), 25), + ], "sha256": [ (bytes.fromhex('120fb6cffcf8b32c43e7225256c4f837' 'a86548c92ccc35480805987cb70be17b'), None), @@ -1259,7 +1271,16 @@ class KDFTests(unittest.TestCase): # 'f7f179e89b3b0bcb17ad10e3ac6eba46'), None), (bytes.fromhex('348c89dbcbd32b2f32d814b8116e84cf2b17' '347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9'), 40), - (bytes.fromhex('89b69d0516f829893c696226650a8687'), None),], + (bytes.fromhex('89b69d0516f829893c696226650a8687'), None), + (bytes.fromhex('72b62d16d04f2d4ce237e499167fa395' + '07191d6f4579bac8952aa7c74d24ff49'), None), + (bytes.fromhex('bd9d964fc9a92bb860829d795063ea08' + 'a5deccc6f4bdf295c86f8ad834ce5479'), None), + (bytes.fromhex('16e35b12b41f465ca64d069578d75005' + '9775ccad5df3ad53d6b204acb122d1ee'), None), + (bytes.fromhex('b6d737069a6b4aeed21574c73e38b342fa0f' + '4449112470e1e80f075611d8d0ef6a82669d662e15e2'), 40), + ], "sha512": [ (bytes.fromhex('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5' 'd513554e1c8cf252c02d470a285a0501bad999bfe943c08f' @@ -1273,7 +1294,20 @@ class KDFTests(unittest.TestCase): (bytes.fromhex('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b8' '68c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30' '225c583a186cd82bd4daea9724a3d3b8'), 64), - (bytes.fromhex('9d9e9c4cd21fe4be24d5b8244c759665'), None),], + (bytes.fromhex('9d9e9c4cd21fe4be24d5b8244c759665'), None), + (bytes.fromhex('38284e5354120cfe567a1f6d2fbed512a613b3db36ba695e' + 'cf876a9d178ef55462986f2df5eb71f1b90ae9fbc4211b29' + '6405f882b910062d854d28953f90bde6'), None), + (bytes.fromhex('e41733921704084e8d31f823846c6d98ad3597a5035ce141' + 'f51963c08630a33cc2a1e263ed7a33b1e63e83adaf3ad912' + '6c0f5b5496cfb2d29e7cd35ef9aa39c5'), None), + (bytes.fromhex('7dc69993c22ab444a13574b9d71b2cafc54e68914d9ddfd0' + '0ed5c23545b854d5e0e68b7f5614eef5df9087573bab8360' + 'b20267791ed7a77669005fd94b5731e2'), None), + (bytes.fromhex('a7555be88503f21c2314259add27d302db6bb03b6ea52b36' + '777f1af2e91f802c72b2b02b76745d7e0c3239e84cc8451c' + '5188d526a56c740f5f0f7c85c44717e7'), 64), + ], } def _test_pbkdf2_hmac(self, pbkdf2, supported): @@ -1282,21 +1316,23 @@ def _test_pbkdf2_hmac(self, pbkdf2, supported): continue for i, vector in enumerate(self.pbkdf2_test_vectors): password, salt, rounds, dklen = vector + if self.fips_incompatible(password, salt, rounds): + continue expected, overwrite_dklen = results[i] if overwrite_dklen: dklen = overwrite_dklen out = pbkdf2(digest_name, password, salt, rounds, dklen) - self.assertEqual(out, expected, + self.assertEqual(out.hex(), expected.hex(), (digest_name, password, salt, rounds, dklen)) out = pbkdf2(digest_name, memoryview(password), memoryview(salt), rounds, dklen) - self.assertEqual(out, expected) + self.assertEqual(out.hex(), expected.hex()) out = pbkdf2(digest_name, bytearray(password), bytearray(salt), rounds, dklen) - self.assertEqual(out, expected) + self.assertEqual(out.hex(), expected.hex()) if dklen is None: out = pbkdf2(digest_name, password, salt, rounds) - self.assertEqual(out, expected, + self.assertEqual(out.hex(), expected.hex(), (digest_name, password, salt, rounds)) with self.assertRaisesRegex(ValueError, '.*unsupported.*'): @@ -1321,9 +1357,22 @@ def _test_pbkdf2_hmac(self, pbkdf2, supported): self.assertRaises( ValueError, pbkdf2, 'sha1', b'pass', b'salt', 1, -1 ) - out = pbkdf2(hash_name='sha1', password=b'password', salt=b'salt', - iterations=1, dklen=None) - self.assertEqual(out, self.pbkdf2_results['sha1'][0][0]) + # Pick a single FIPS compatible vector + index = 5 + password, salt, rounds, dklen = self.pbkdf2_test_vectors[index] + self.assertFalse(self.fips_incompatible(password, salt, rounds)) + out = pbkdf2(hash_name='sha1', password=password, salt=salt, + iterations=rounds, dklen=dklen) + expected = self.pbkdf2_results['sha1'][index][0] + self.assertEqual(out.hex(), expected.hex()) + + # FIPS (NIST SP 800-132) needs: + # - password of at least 64 bits (8 bytes) + # - salt of at least 128 bits (16 bytes) + # - rounds of least 1000 + def fips_incompatible(self, password, salt, rounds): + return (get_fips_mode() + and (len(password) < 8 or len(salt) < 16 or rounds < 1000)) @unittest.skipIf(openssl_hashlib is None, "requires OpenSSL bindings") def test_pbkdf2_hmac_c(self): diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py index d6623fee9bb2b4..0d288caeabc8ed 100644 --- a/Lib/test/test_heapq.py +++ b/Lib/test/test_heapq.py @@ -39,7 +39,7 @@ def load_tests(loader, tests, ignore): class HeapqMergeDocTestFinder: def find(self, *args, **kwargs): dtf = doctest.DocTestFinder() - return dtf.find(py_heapq.merge) + return dtf.find(py_heapq.merge, globs=py_heapq.__dict__) tests.addTests(doctest.DocTestSuite(py_heapq, test_finder=HeapqMergeDocTestFinder())) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index da6f468b2f3718..d53e8c6b614d34 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -743,6 +743,55 @@ def test_expires(self): # XXX RFC 2965 expiry rules (some apply to V0 too) + def test_expired_cookie_from_other_domain_is_ignored(self): + c = CookieJar() + interact_netscape(c, "http://www.victim.test/", + 'session=secret; Domain=.victim.test; Path=/') + + for domain in ('.victim.test', 'victim.test'): + for expires in ( + 'Max-Age=0', + 'Expires=Thu, 01 Jan 1970 00:00:00 GMT', + ): + hdr = f'session=x; Domain={domain}; Path=/; {expires}' + with self.subTest(hdr=hdr): + interact_netscape(c, "http://www.attacker.test/", hdr) + self.assertEqual(len(c), 1) + self.assertIn( + 'session=secret', + interact_netscape(c, "http://www.victim.test/")) + + interact_netscape(c, "http://www.victim.test/", + 'session=x; Domain=.victim.test; Path=/; Max-Age=0') + self.assertEqual(len(c), 0) + + def test_expired_cookie_from_other_path_is_ignored(self): + pol = DefaultCookiePolicy(strict_ns_set_path=True) + c = CookieJar(pol) + interact_netscape(c, "http://www.acme.com/a/", + 'token=secret; Path=/a') + self.assertEqual(len(c), 1) + interact_netscape(c, "http://www.acme.com/b/", + 'token=x; Path=/a; Max-Age=0') + self.assertEqual(len(c), 1) + interact_netscape(c, "http://www.acme.com/a/", + 'token=x; Path=/a; Max-Age=0') + self.assertEqual(len(c), 0) + + def test_expired_cookie_for_blocked_domain_is_ignored(self): + c = CookieJar() + interact_netscape(c, "http://www.victim.test/", + 'session=secret; Domain=.victim.test; Path=/') + self.assertEqual(len(c), 1) + c.set_policy(DefaultCookiePolicy(blocked_domains=[".victim.test"])) + interact_netscape(c, "http://www.victim.test/", + 'session=x; Domain=.victim.test; Path=/; Max-Age=0') + self.assertEqual(len(c), 1) + c.set_policy(DefaultCookiePolicy()) + interact_netscape(c, "http://www.victim.test/", + 'session=x; Domain=.victim.test; Path=/; Max-Age=0') + self.assertEqual(len(c), 0) + def test_default_path(self): # RFC 2965 pol = DefaultCookiePolicy(rfc2965=True) diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index d1df2ec42f0d14..6632c39159865c 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -628,6 +628,30 @@ def test_control_characters(self): with self.assertRaises(cookies.CookieError): morsel |= {c0: "val"} + def test_no_semicolons_in_attribute_values(self): + for attrkey, attrname in cookies.Morsel._reserved.items(): + if attrname == "Comment": + continue + with self.subTest(attrkey=attrkey, attrname=attrname): + morsel = cookies.Morsel() + with self.assertRaises(cookies.CookieError): + morsel[attrkey] = "foo; SameSite=none" + with self.assertRaises(cookies.CookieError): + morsel.setdefault(attrkey, "foo; SameSite=none") + with self.assertRaises(cookies.CookieError): + morsel.update({attrkey: "foo; SameSite=none"}) + with self.assertRaises(cookies.CookieError): + morsel |= {attrkey: "foo; SameSite=none"} + + def test_semicolon_escaped_in_comment(self): + morsel = cookies.Morsel() + morsel.set("key", "value", "coded") + morsel["comment"] = "foo;SameSite=none;Domain=example.com" + self.assertEqual( + morsel.OutputString(), + r'key=coded; Comment="foo\073SameSite=none\073Domain=example.com"', + ) + def test_control_characters_output(self): # Tests that even if the internals of Morsel are modified # that a call to .output() has control character safeguards. diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 1e5ec0d4e43c27..eb604bd2c4421b 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -4,8 +4,10 @@ import io import itertools import os +import pickle import array import re +import select import socket import threading @@ -16,6 +18,7 @@ from test import support from test.support import os_helper from test.support import socket_helper +from test.support import threading_helper support.requires_working_socket(module=True) @@ -125,6 +128,7 @@ def __init__(self, *args): super().__init__('example.com') self.fake_socket_args = args self._create_connection = self.create_connection + self._select = self.fake_select def connect(self): """Count the number of times connect() is invoked""" @@ -134,6 +138,23 @@ def connect(self): def create_connection(self, *pos, **kw): return FakeSocket(*self.fake_socket_args) + @staticmethod + def fake_select(rlist, wlist, xlist, timeout=None): + """Real select.select() won't work on our fake socket""" + # Only needs to support read select for current tests + ret = [] + for r in rlist: + assert isinstance(r, FakeSocket) + if r.data and not hasattr(r, 'file'): + # Has data to read, hasn't called makefile() yet + ret.append(r) + elif getattr(r, 'file', None) and not r.file_closed: + if r.file.read(1): + r.file.seek(r.file.tell() - 1) + ret.append(r) + return ret, [], [] + + class HeaderTests(TestCase): def test_auto_headers(self): # Some headers are added automatically, but should not be added by @@ -179,7 +200,7 @@ def append(self, item): # Here, we're testing that methods expecting a body get a # content-length set to zero if the body is empty (either None or '') bodies = (None, '') - methods_with_body = ('PUT', 'POST', 'PATCH') + methods_with_body = ('PUT', 'POST', 'PATCH', 'QUERY') for method, body in itertools.product(methods_with_body, bodies): conn = client.HTTPConnection('example.com') conn.sock = FakeSocket(None) @@ -405,6 +426,11 @@ def test_invalid_tunnel_host(self): 'invalid.host\r\n', 'invalid.host\x00', 'invalid host', + # The IDNA codec normalizes these to an ASCII space + 'invalid\xa0host', + 'invalid\u3000host', + # ... and this one to a space and a combining character + 'invalid\xa8host', ) for tunnel_host in cases: with self.subTest(tunnel_host): @@ -528,6 +554,34 @@ def test_invalid_method_names(self): conn.sock = FakeSocket(None) conn.request(method=method, url="/") + def test_query_request(self): + # QUERY (RFC 10008) is sent with a body, like PUT/POST/PATCH. + conn = client.HTTPConnection('example.com') + conn.sock = FakeSocket(None) + conn.request('QUERY', '/contacts', body='name=python', + headers={'Content-Type': 'application/x-www-form-urlencoded'}) + self.assertEqual(conn.sock.data, + b'QUERY /contacts HTTP/1.1\r\n' + b'Host: example.com\r\n' + b'Accept-Encoding: identity\r\n' + b'Content-Length: 11\r\n' + b'Content-Type: application/x-www-form-urlencoded\r\n' + b'\r\n' + b'name=python') + + def test_query_request_no_body(self): + # A QUERY without a body still gets Content-Length: 0, since some + # servers respond with 411 otherwise. + conn = client.HTTPConnection('example.com') + conn.sock = FakeSocket(None) + conn.request('QUERY', '/contacts') + self.assertEqual(conn.sock.data, + b'QUERY /contacts HTTP/1.1\r\n' + b'Host: example.com\r\n' + b'Accept-Encoding: identity\r\n' + b'Content-Length: 0\r\n' + b'\r\n') + class TransferEncodingTest(TestCase): expected_body = b"It's just a flesh wound" @@ -2503,6 +2557,274 @@ def test_binary_file_body(self): self.assertEqual(b'5\r\nbody\xc1\r\n0\r\n\r\n', f.read()) +class ExpectContinueTests(TestCase): + + def test_data_sent_on_continue(self): + # Prior versions would pass this too, as they send body immediately + # and ignore the Continue, but make sure it still behaves as expected. + conn = FakeSocketHTTPConnection( + b'HTTP/1.1 100 Continue\r\n' + b'\r\n' + b'HTTP/1.1 200 OK\r\n' + b'Content-Length: 0\r\n' + b'\r\n' + ) + conn.request('PUT', '/', headers={'Expect': '100-continue'}, + body=b'Body content') + resp = conn.getresponse() + self.assertEqual(resp.code, 200) + self.assertIn(b'Expect: 100-continue', conn.sock.data) + self.assertIn(b'Body content', conn.sock.data) + + def test_data_not_sent_on_error(self): + conn = FakeSocketHTTPConnection( + b'HTTP/1.1 429 Too Many Requests\r\n' + b'Content-Length: 0\r\n' + b'\r\n' + ) + conn.request('PUT', '/', headers={'Expect': '100-continue'}, + body=b'Body content') + resp = conn.getresponse() + self.assertEqual(resp.code, 429) + self.assertIn(b'Expect: 100-continue', conn.sock.data) + self.assertNotIn(b'Body content', conn.sock.data) + + def test_request_continue_timeout(self): + conn = FakeSocketHTTPConnection( + b'HTTP/1.1 100 Continue\r\n' + b'\r\n' + b'HTTP/1.1 200 OK\r\n' + b'Content-Length: 0\r\n' + b'\r\n' + ) + conn._select = mock.Mock(wraps=conn.fake_select) + conn.request('PUT', '/', headers={'Expect': '100-continue'}, + body=b'Body content', continue_timeout=0.25) + conn._select.assert_called_once_with([conn.sock], [], [], 0.25) + self.assertEqual(conn.getresponse().code, 200) + + def test_putrequest_continue_timeout(self): + conn = FakeSocketHTTPConnection( + b'HTTP/1.1 100 Continue\r\n' + b'\r\n' + b'HTTP/1.1 200 OK\r\n' + b'Content-Length: 0\r\n' + b'\r\n' + ) + conn._select = mock.Mock(wraps=conn.fake_select) + conn.putrequest('PUT', '/', continue_timeout=0.25) + conn.putheader('Expect', '100-continue') + conn.putheader('Content-Length', '12') + conn.endheaders(b'Body content') + conn._select.assert_called_once_with([conn.sock], [], [], 0.25) + self.assertIn(b'Body content', conn.sock.data) + self.assertEqual(conn.getresponse().code, 200) + + def test_failed_putrequest_preserves_continue_timeout(self): + conn = FakeSocketHTTPConnection( + b'HTTP/1.1 100 Continue\r\n' + b'\r\n' + ) + conn._select = mock.Mock(wraps=conn.fake_select) + conn.putrequest('PUT', '/', continue_timeout=0.25) + with self.assertRaises(client.CannotSendRequest): + conn.putrequest('GET', '/', continue_timeout=10) + conn.putheader('Expect', '100-continue') + conn.putheader('Content-Length', '12') + conn.endheaders(b'Body content') + conn._select.assert_called_once_with([conn.sock], [], [], 0.25) + + def test_invalid_continue_timeout(self): + conn = FakeSocketHTTPConnection(b'') + for timeout in (0, -1): + with self.subTest(timeout=timeout): + with self.assertRaises(ValueError): + conn.request('PUT', '/', body=b'Body content', + continue_timeout=timeout) + with self.assertRaises(ValueError): + conn.putrequest('PUT', '/', continue_timeout=timeout) + + @unittest.skipUnless(hasattr(client, 'HTTPSConnection'), + 'ssl support required') + def test_https_pending_data_skips_select(self): + # Data already decrypted and buffered in the SSLSocket is not + # visible to select(), so it must be checked with pending(). + class FakeSSLSocket(FakeSocket): + def pending(self): + return len(self.text) + + conn = client.HTTPSConnection('example.com') + conn.sock = FakeSSLSocket( + b'HTTP/1.1 417 Expectation Failed\r\n' + b'Content-Length: 0\r\n' + b'\r\n' + ) + conn._select = mock.Mock(side_effect=AssertionError('select called')) + with mock.patch.object(client, 'ssl', + mock.Mock(SSLSocket=FakeSSLSocket)): + conn.request('PUT', '/', headers={'Expect': '100-continue'}, + body=b'Body content') + conn._select.assert_not_called() + self.assertEqual(conn.getresponse().code, 417) + self.assertNotIn(b'Body content', conn.sock.data) + + @unittest.skipUnless(hasattr(client, 'HTTPSConnection'), + 'ssl support required') + def test_https_no_pending_data_uses_select(self): + class FakeSSLSocket(FakeSocket): + def pending(self): + return 0 + + conn = client.HTTPSConnection('example.com') + conn.sock = FakeSSLSocket( + b'HTTP/1.1 100 Continue\r\n' + b'\r\n' + b'HTTP/1.1 200 OK\r\n' + b'Content-Length: 0\r\n' + b'\r\n' + ) + conn._select = mock.Mock(wraps=FakeSocketHTTPConnection.fake_select) + with mock.patch.object(client, 'ssl', + mock.Mock(SSLSocket=FakeSSLSocket)): + conn.request('PUT', '/', headers={'Expect': '100-continue'}, + body=b'Body content', continue_timeout=0.25) + conn._select.assert_called_once_with([conn.sock], [], [], 0.25) + self.assertEqual(conn.getresponse().code, 200) + self.assertIn(b'Body content', conn.sock.data) + + def test_pending_response_kept_on_invalid_putrequest(self): + # A real socket is needed: FakeSocket.makefile() returns the whole + # response again, which would hide a lost early response. + def server_thread(sock): + conn, _ = sock.accept() + with conn: + conn.recv(4096) + conn.sendall( + b'HTTP/1.1 417 Expectation Failed\r\n' + b'Content-Length: 4\r\n' + b'\r\n' + b'nope' + ) + # Keep the connection open until the client is done. + conn.recv(4096) + + with socket.socket() as sock: + sock.bind(('localhost', 0)) + sock.listen(1) + t = threading.Thread(target=server_thread, args=(sock,)) + t.start() + conn = client.HTTPConnection('localhost', sock.getsockname()[1], + timeout=support.SHORT_TIMEOUT) + try: + conn.request('PUT', '/', + headers={'Expect': '100-continue'}, + body=b'Body content') + with self.assertRaises(client.CannotSendRequest): + conn.putrequest('GET', '/') + with conn.getresponse() as resp: + self.assertEqual(resp.status, 417) + self.assertEqual(resp.read(), b'nope') + finally: + conn.close() + t.join() + + def test_body_sent_on_timeout(self): + def client_thread(port): + conn = client.HTTPConnection('localhost', port) + conn.request('PUT', '/', + headers={'Expect': '100-continue'}, + body=b'Body content', continue_timeout=0.75) + resp = conn.getresponse() + self.assertEqual(resp.code, 200) + resp.close() + conn.close() + + with threading_helper.catch_threading_exception() as cm: + with socket.socket() as sock: + sock.bind(('localhost', 0)) + sock.listen(1) + t = threading.Thread(target=client_thread, + args=(sock.getsockname()[1],)) + t.start() + conn, _ = sock.accept() + req_data = conn.recv(4096) + self.assertTrue(req_data.startswith(b'PUT / HTTP/1.1\r\n')) + self.assertIn(b'Expect: 100-continue\r\n', req_data) + self.assertIn(b'Content-Length: 12\r\n', req_data) + # Client should not send body data yet + self.assertNotIn(b'Body content', req_data) + + # Server does not respond to the continue request with 100 Continue + # so the client will time out and send the body data + # Wait for 0.5 seconds to see if the client sends the body data + rr, _, _ = select.select([conn], [], [], 0.5) + self.assertEqual(rr, []) + # Client should time out and send body data in another ~0.25s + rr, _, _ = select.select([conn], [], [], 0.5) + self.assertEqual(rr, [conn]) + body = conn.recv(4096) + self.assertEqual(body, b'Body content') + conn.sendall( + b'HTTP/1.1 200 OK\r\n' + b'Content-Length: 0\r\n' + b'\r\n' + ) + conn.close() + sock.close() + t.join() + if cm.exc_value is not None: + raise cm.exc_value + + def test_manual_getresponse(self): + def client_thread(port): + conn = client.HTTPConnection('localhost', port) + conn.putrequest("PUT", "/file", continue_timeout=0.1) + conn.putheader('Expect', '100-Continue') + conn.putheader('Content-Length', '42') + conn.endheaders() + with conn.getresponse(support_continue=True) as resp: + self.assertEqual(resp.status, 100) + conn.send(b'Go away or I shall taunt you a second time') + with conn.getresponse() as resp: + self.assertEqual(resp.status, 200) + conn.close() + + with threading_helper.catch_threading_exception() as cm: + with socket.socket() as sock: + sock.bind(('localhost', 0)) + sock.listen(1) + t = threading.Thread(target=client_thread, + args=(sock.getsockname()[1],)) + t.start() + conn, _ = sock.accept() + req_data = conn.recv(4096) + self.assertTrue(req_data.startswith(b'PUT /file HTTP/1.1\r\n')) + self.assertIn(b'Expect: 100-Continue\r\n', req_data) + self.assertIn(b'Content-Length: 42\r\n', req_data) + self.assertNotIn(b'I shall taunt you', req_data) + # Client is not expected to send body data until we respond, + # regardless of continue_timeout + rr, _, _ = select.select([conn], [], [], 1.0) + self.assertEqual(rr, []) + conn.sendall(b'HTTP/1.1 100 Continue\r\n\r\n') + rr, _, _ = select.select([conn], [], [], 0.5) + self.assertEqual(rr, [conn]) + b = conn.recv(42) + self.assertEqual( + b, b'Go away or I shall taunt you a second time') + conn.sendall( + b'HTTP/1.1 200 OK\r\n' + b'Content-Length: 0\r\n' + b'\r\n' + ) + conn.close() + sock.close() + t.join() + if cm.exc_value is not None: + raise cm.exc_value + + + class HTTPResponseTest(TestCase): def setUp(self): @@ -2786,6 +3108,36 @@ def test_no_proxy_response_headers(self): headers = self.conn.get_proxy_response_headers() self.assertIsNone(headers) + def test_tunnel_error(self): + response_text = ( + 'HTTP/1.1 407 Proxy Authentication Required\r\n' + 'Proxy-Authenticate: Basic realm="proxy"\r\n\r\n' + ) + self.conn._create_connection = self._create_connection(response_text) + self.conn.set_tunnel('destination.com') + + with self.assertRaises(client.ProxyTunnelError) as cm: + self.conn.request('HEAD', '/', '') + exc = cm.exception + self.assertIsInstance(exc, OSError) + self.assertEqual(exc.code, 407) + self.assertEqual(exc.reason, 'Proxy Authentication Required') + self.assertEqual(exc.headers['Proxy-Authenticate'], + 'Basic realm="proxy"') + self.assertEqual(str(exc), 'Tunnel connection failed: ' + '407 Proxy Authentication Required') + self.assertIsNone(self.conn.sock) + + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.subTest(proto=proto): + copy = pickle.loads(pickle.dumps(exc, proto)) + self.assertIs(type(copy), client.ProxyTunnelError) + self.assertEqual(copy.code, 407) + self.assertEqual(copy.reason, exc.reason) + self.assertEqual(copy.headers['Proxy-Authenticate'], + 'Basic realm="proxy"') + self.assertEqual(str(copy), str(exc)) + def test_tunnel_leak(self): sock = None diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index 51e3412324b72d..906c78f07d9c33 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -353,6 +353,33 @@ def test_substitute_errors(self): self.assertRaises(ValueError, sub, '?s', [[(1,)]]) # not a range pair self.assertRaises(ValueError, sub, '?s', [[(1, 2, 3)]]) # not a range pair + @unittest.skipUnless(ssl, "SSL not available") + def test_starttls_rejects_buffered_plaintext(self): + sock = mock.Mock(spec=socket.socket) + sock.recv.return_value = ( + b'* OK [CAPABILITY IMAP4rev1 STARTTLS] ready\r\n') + with mock.patch.object(imaplib.IMAP4, '_create_socket', + return_value=sock): + client = imaplib.IMAP4('localhost') + self.addCleanup(client.shutdown) + + tag = client.tagpre + str(client.tagnum).encode('ascii') + next_tag = client.tagpre + str(client.tagnum + 1).encode('ascii') + # Return the STARTTLS response and injected plaintext in one recv(), + # so the test does not depend on how TCP packets are split. + sock.recv.side_effect = [ + tag + b' OK Begin TLS\r\n' + b'* CAPABILITY IMAP4rev1 INJECTED\r\n' + + next_tag + b' OK CAPABILITY completed\r\n' + ] + context = mock.Mock() + context.wrap_socket.return_value = sock + with self.assertRaisesRegex(imaplib.IMAP4.abort, + 'unexpected data after STARTTLS response'): + client.starttls(context) + context.wrap_socket.assert_not_called() + self.assertFalse(client._tls_established) + if ssl: class SecureTCPServer(socketserver.TCPServer): @@ -2344,10 +2371,67 @@ def test_file_property_setter_should_not_close_previous_file(self): f.close.assert_not_called() +class StartTLSHandler(SimpleIMAPHandler): + capabilities = 'STARTTLS' + + def cmd_STARTTLS(self, tag, args): + context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + context.load_cert_chain(CERTFILE) + self._send_tagged(tag, 'OK', 'Begin TLS') + self.connection = context.wrap_socket(self.connection, + server_side=True) + self.rfile.close() + self.wfile.close() + self.rfile = self.connection.makefile('rb') + self.wfile = self.connection.makefile('wb', buffering=0) + self.capabilities = 'AUTH=PLAIN' + + def finish(self): + try: + super().finish() + finally: + self.connection.close() + + class NewIMAPTests(NewIMAPTestsMixin, unittest.TestCase): imap_class = imaplib.IMAP4 server_class = socketserver.TCPServer + @unittest.skipUnless(ssl, "SSL not available") + def test_starttls(self): + _, server = self._setup(StartTLSHandler, connect=False) + self.client = client = self.imap_class( + 'localhost', server.server_address[1], + timeout=support.LOOPBACK_TIMEOUT) + context = ssl.create_default_context(cafile=CAFILE) + self.assertEqual(client.capabilities, ('IMAP4REV1', 'STARTTLS')) + self.assertEqual(client.starttls(context), ('OK', [None])) + self.assertIsInstance(client.sock, ssl.SSLSocket) + self.assertEqual(client.capabilities, ('IMAP4REV1', 'AUTH=PLAIN')) + client.logout() + + @unittest.skipUnless(ssl, "SSL not available") + def test_starttls_discards_untagged_responses(self): + # Untagged responses received before STARTTLS could have been + # injected by a MITM, so they must not survive the switch to TLS. + class InjectingStartTLSHandler(StartTLSHandler): + def cmd_STARTTLS(self, tag, args): + self._send_textline('* 3 EXISTS') + self._send_textline(r'* FLAGS (\Injected)') + super().cmd_STARTTLS(tag, args) + + _, server = self._setup(InjectingStartTLSHandler, connect=False) + self.client = client = self.imap_class( + 'localhost', server.server_address[1], + timeout=support.LOOPBACK_TIMEOUT) + context = ssl.create_default_context(cafile=CAFILE) + self.assertEqual(client.starttls(context), ('OK', [None])) + self.assertIsInstance(client.sock, ssl.SSLSocket) + self.assertEqual(client.untagged_responses, {}) + self.assertEqual(client.response('EXISTS'), ('EXISTS', [None])) + self.assertEqual(client.response('FLAGS'), ('FLAGS', [None])) + client.logout() + @unittest.skipUnless(ssl, "SSL not available") class NewIMAPSSLTests(NewIMAPTestsMixin, unittest.TestCase): @@ -2381,6 +2465,7 @@ def test_ssl_verified(self): ssl_context=ssl_context) client.shutdown() + class ThreadedNetworkedTests(unittest.TestCase): server_class = socketserver.TCPServer imap_class = imaplib.IMAP4 diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 2ab61827e69623..0d857ac46f2fad 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -1293,6 +1293,37 @@ class Spec: name = "abc" _imp.create_dynamic(Spec()) + @unittest.skipUnless(_testsinglephase is not None, + 'requires _testsinglephase') + @unittest.skipUnless(os_helper.TESTFN_UNDECODABLE, + 'requires undecodable file names') + def test_import_from_undecodable_path(self): + # gh-155247: the path of the extension module is not encodable + # in UTF-8. + origin = _testsinglephase.__file__ + with os_helper.temp_dir() as tempdir: + subdir = os.path.join(os.fsencode(tempdir), + os_helper.TESTFN_UNDECODABLE) + try: + os.mkdir(subdir) + except OSError: + self.skipTest('undecodable paths are not supported') + path = os.path.join(subdir, os.fsencode(os.path.basename(origin))) + shutil.copyfile(origin, path) + # Import it in a subprocess: the extension module stays loaded, + # and on Windows its file cannot be removed. + script = textwrap.dedent(f""" + import importlib.util + path = {os.fsdecode(path)!a} + spec = importlib.util.spec_from_file_location( + '_testsinglephase', path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + assert module.__name__ == '_testsinglephase', module.__name__ + assert module.__file__ == path, module.__file__ + """) + script_helper.assert_python_ok('-c', script) + def test_create_builtin(self): class Spec: pass diff --git a/Lib/test/test_importlib/metadata/fixtures.py b/Lib/test/test_importlib/metadata/fixtures.py index 79b2e1e33ad7ff..f3e2003fb910b6 100644 --- a/Lib/test/test_importlib/metadata/fixtures.py +++ b/Lib/test/test_importlib/metadata/fixtures.py @@ -6,7 +6,9 @@ import shutil import sys import textwrap +import zipfile from importlib import resources +from importlib.metadata import FastPath from test.support import import_helper from test.support import os_helper @@ -365,11 +367,21 @@ def _fixture_on_path(self, filename): assert file.name.startswith('example'), file.name sys.path.insert(0, str(file)) self.resources.callback(sys.path.pop, 0) + self.resources.callback(self._close_cached_zip, str(file)) + + @staticmethod + def _close_cached_zip(root): + # TODO gh-162342: Remove once FastPath no longer leaks the zip file. + zip_path = getattr(FastPath(root).joinpath, '__self__', None) + if isinstance(zip_path, zipfile.Path): + zip_path.root.close() def setUp(self): # Add self.zip_name to the front of sys.path. self.resources = contextlib.ExitStack() self.addCleanup(self.resources.close) + # Registered first so it runs last, after every zip has been closed. + self.resources.callback(FastPath.__new__.cache_clear) def parameterize(*args_set): diff --git a/Lib/test/test_importlib/test_discover.py b/Lib/test/test_importlib/test_discover.py index c4ab7b6982e3ba..490b54569e09c4 100644 --- a/Lib/test/test_importlib/test_discover.py +++ b/Lib/test/test_importlib/test_discover.py @@ -1,4 +1,4 @@ -from unittest.mock import Mock +from unittest.mock import Mock, patch from test.test_importlib import util @@ -114,6 +114,68 @@ def test_invalid_parent(self): with self.assertRaises(ValueError): list(finder.discover(example)) + def _patch_scandir(self, scandir): + module_os = self.machinery.FileFinder._fill_cache.__globals__['_os'] + return patch.object(module_os, 'scandir', scandir) + + def test_discover_persistently_failing_scan(self): + # gh-155935: an iterator that raises OSError on every next() call + # must end the listing instead of looping forever. + class FailingScandirIterator: + calls = 0 + + def __enter__(self): + return self + + def __exit__(self, *args): + return False + + def __next__(self): + self.calls += 1 + if self.calls > 100: + # Safety net so regressed code fails fast on the call + # count below instead of hanging the test forever. + raise StopIteration + raise OSError('persistently failing directory scan') + + scan_iterator = FailingScandirIterator() + with self._patch_scandir(lambda path: scan_iterator): + finder = self.get_finder('dummy') + discovered = list(finder.discover()) + self.assertEqual(discovered, []) + # A failed scan must not be retried. + self.assertEqual(scan_iterator.calls, 1) + + def test_find_children_failing_direntry(self): + # An entry whose DirEntry methods raise OSError is skipped; the + # remaining entries are still listed. + failing = Mock() + failing.name = 'failing' + failing.is_dir.side_effect = OSError('stat failed') + good = Mock() + good.name = 'example.py' + good.is_dir.return_value = False + good.is_file.return_value = True + + class FakeScandirIterator: + def __init__(self, entries): + self._iterator = iter(entries) + + def __enter__(self): + return self + + def __exit__(self, *args): + return False + + def __next__(self): + return next(self._iterator) + + with self._patch_scandir( + lambda path: FakeScandirIterator([failing, good])): + finder = self.get_finder('dummy') + children = list(finder._find_children()) + self.assertEqual(children, ['example']) + ( Frozen_TestFileFinder, diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index fa675f626cbe10..40c636d586c550 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -778,6 +778,14 @@ def test_pylong_int_divmod(self): a, b = divmod(n*3 + 1, n) assert a == 3 and b == 1 + def test_pylong_int_mod(self): + n = (1 << 100_000) + m = (1 << 50_000) + self.assertEqual((n * 3 + 1) % m, 1) + self.assertEqual((-n * 3 - 1) % m, m - 1) + self.assertEqual((n * 3 + 1) % -m, 1 - m) + self.assertEqual((-n * 3 - 1) % -m, -1) + @support.cpython_only # tests implementation details of CPython. @unittest.skipUnless(_pylong, "_pylong module required") def test_pylong_int_divmod_crash(self): @@ -789,6 +797,7 @@ def test_pylong_int_divmod_crash(self): with mock.patch.object(_pylong, "int_divmod", wraps=bad_int_divmod): msg = r"tuple of length 2 is required from int_divmod\(\)" self.assertRaisesRegex(ValueError, msg, divmod, a, b) + self.assertRaisesRegex(ValueError, msg, lambda: a % b) def test_pylong_str_to_int(self): v1 = 1 << 100_000 diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index a9e296891bb870..e9a64915a1b4a8 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -947,6 +947,21 @@ def test_failure(self): with self.assertRaises(ExecutionFailed): interp.exec('raise Exception') + def test_failure_with_broken_exception_str(self): + interp = interpreters.create() + for raised, expected in [ + ('ValueError', interpreters.InterpreterError), + ('MemoryError', MemoryError), + ]: + with self.subTest(raised=raised): + with self.assertRaises(expected): + interp.exec(dedent(f""" + class BrokenError(Exception): + def __str__(self): + raise {raised} + raise BrokenError + """)) + @force_not_colorized def test_display_preserved_exception(self): tempdir = self.temp_dir() diff --git a/Lib/test/test_io/__init__.py b/Lib/test/test_io/__init__.py index c94fad3e779381..ab735e1538185f 100644 --- a/Lib/test/test_io/__init__.py +++ b/Lib/test/test_io/__init__.py @@ -3,16 +3,14 @@ Tests are split across multiple files to increase parallelism and focus on specific implementation pieces. -* test_io - * test_bufferedio - tests file buffering - * test_memoryio - tests BytesIO and StringIO - * test_fileio - tests FileIO - * test_file - tests the file interface - * test_general - tests everything else in the io module - * test_univnewlines - tests universal newline support - * test_largefile - tests operations on a file greater than 2**32 bytes - (only enabled with -ulargefile) -* test_free_threading/test_io - tests thread safety of io objects +Tests for code that uses io live with that code, for example: + +* test_capi.test_file - the PyFile_* C API +* test_builtin, test_print - open(), input() and print() +* test_cmd_line, test_subprocess - stdio buffering and newlines at startup + and through pipes +* test_utf8_mode - UTF-8 mode encodings, including with _pyio +* test_os, test_eintr - fd-level I/O and EINTR retries .. attention:: When writing tests for io, it's important to test both the C and Python diff --git a/Lib/test/test_io/test_bufferedio.py b/Lib/test/test_io/test_bufferedio.py index e83dd0d4e28d00..38316894fb90ce 100644 --- a/Lib/test/test_io/test_bufferedio.py +++ b/Lib/test/test_io/test_bufferedio.py @@ -306,12 +306,26 @@ def test_readinto(self): self.assertEqual(b, b"gf") self.assertEqual(bufio.readinto(b), 0) self.assertEqual(b, b"gf") - rawio = self.MockRawIO((b"abc", None)) - bufio = self.tp(rawio) + rawio = self.MockRawIO((b"abc", None, None, None, None, None)) + bufio = self.tp(rawio, buffer_size=4) self.assertEqual(bufio.readinto(b), 2) self.assertEqual(b, b"ab") self.assertEqual(bufio.readinto(b), 1) self.assertEqual(b, b"cb") + self.assertIsNone(bufio.readinto(b)) + self.assertIsNone(bufio.readinto1(b)) + # Larger than buffer_size: read directly into b + b = bytearray(5) + self.assertIsNone(bufio.readinto(b)) + self.assertIsNone(bufio.readinto1(b)) + + def test_readinto_with_remaining_buffered_data(self): + bufio = self.tp(self.BytesIO(b"abcd"), buffer_size=2) + self.assertEqual(bufio.read(1), b"a") + b = bytearray(2) + self.assertEqual(bufio.readinto(b), 2) + self.assertEqual(b, b"bc") + self.assertEqual(bufio.read(), b"d") def test_readinto1(self): buffer_size = 10 @@ -1026,6 +1040,21 @@ def test_uninitialized(self): self.assertEqual(pair.read(0), b'') self.assertEqual(pair.write(b''), 0) + def test_reinit(self): + # gh-162246: Calling __init__() again must release the previous + # reader and writer, the writer first so it flushes before the + # reader closes a shared raw stream. + raw = self.MockRawIO() + pair = self.tp(raw, raw) + pair.write(b"abc") + ref = weakref.ref(raw) + pair.__init__(self.MockRawIO(), self.MockRawIO()) + support.gc_collect() + self.assertEqual(raw._write_stack, [b"abc"]) + del raw + support.gc_collect() + self.assertIsNone(ref()) + def test_detach(self): pair = self.tp(self.MockRawIO(), self.MockRawIO()) self.assertRaises(self.UnsupportedOperation, pair.detach) @@ -1057,6 +1086,20 @@ def test_read(self): pair = self.tp(self.BytesIO(b"abc"), self.MockRawIO()) self.assertEqual(pair.read(None), b"abc") + def test_readline(self): + pair = self.tp(self.BytesIO(b"ab\ncd"), self.MockRawIO()) + + self.assertEqual(pair.readline(1), b"a") + self.assertEqual(pair.readline(5), b"b\n") + self.assertEqual(pair.readline(0), b"") + self.assertEqual(pair.readline(None), b"cd") + + def test_readline_nonblocking(self): + raw = self.MockRawIO([b"abc", None, None]) + pair = self.tp(raw, self.MockRawIO()) + + self.assertEqual(pair.readline(), b"abc") + def test_readlines(self): pair = lambda: self.tp(self.BytesIO(b"abc\ndef\nh"), self.MockRawIO()) self.assertEqual(pair().readlines(), [b"abc\n", b"def\n", b"h"]) @@ -1096,6 +1139,15 @@ def test_write(self): pair.flush() self.assertEqual(w._write_stack, [b"abc", b"def"]) + def test_destructor_flushes_shared_stream(self): + raw = self.MockRawIO() + pair = self.tp(raw, raw) + pair.write(b"abc") + self.assertEqual(raw._write_stack, []) + del pair + support.gc_collect() + self.assertEqual(raw._write_stack, [b"abc"]) + def test_peek(self): pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO()) diff --git a/Lib/test/test_file_eintr.py b/Lib/test/test_io/test_file_eintr.py similarity index 100% rename from Lib/test/test_file_eintr.py rename to Lib/test/test_io/test_file_eintr.py diff --git a/Lib/test/test_free_threading/test_io.py b/Lib/test/test_io/test_free_threading.py similarity index 98% rename from Lib/test/test_free_threading/test_io.py rename to Lib/test/test_io/test_free_threading.py index a2b1ec8eb72bb1..d4cbfe446d9228 100644 --- a/Lib/test/test_free_threading/test_io.py +++ b/Lib/test/test_io/test_free_threading.py @@ -2,12 +2,15 @@ import io import _pyio as pyio import threading -from unittest import TestCase -from test.support import threading_helper +from unittest import SkipTest, TestCase +from test.support import Py_GIL_DISABLED, threading_helper from test.support.threading_helper import run_concurrently from random import randint from sys import getsizeof +if not Py_GIL_DISABLED: + raise SkipTest("GIL enabled") + threading_helper.requires_working_threading(module=True) diff --git a/Lib/test/test_io/test_general.py b/Lib/test/test_io/test_general.py index c4d5a80086718b..883144817dddc3 100644 --- a/Lib/test/test_io/test_general.py +++ b/Lib/test/test_io/test_general.py @@ -713,15 +713,18 @@ def test_invalid_newline(self): def test_buffered_readinto_mixin(self): # Test the implementation provided by BufferedIOBase class Stream(self.BufferedIOBase): + def __init__(self, data): + self.data = data def read(self, size): - return b"12345" + return self.data read1 = read - stream = Stream() for method in ("readinto", "readinto1"): with self.subTest(method): buffer = byteslike(5) - self.assertEqual(getattr(stream, method)(buffer), 5) + self.assertEqual(getattr(Stream(b"12345"), method)(buffer), 5) self.assertEqual(bytes(buffer), b"12345") + # Non-blocking stream with no data + self.assertIsNone(getattr(Stream(None), method)(buffer)) def test_fspath_support(self): def check_path_succeeds(path): @@ -895,6 +898,41 @@ def test_immutable_types(self): with self.assertRaisesRegex(TypeError, "immutable"): tp.foo = "bar" + def test_managed_dict(self): + # The _io types use Py_TPFLAGS_MANAGED_DICT: there is no explicit + # dict slot and instance attributes are stored as inline values. + _testinternalcapi = import_helper.import_module("_testinternalcapi") + _io = self._io + no_dict = {_io.IncrementalNewlineDecoder, _io._BytesIOBuffer} + for tp in self.types: + with self.subTest(tp=tp): + if tp in no_dict: + self.assertEqual(tp.__dictoffset__, 0) + else: + self.assertEqual(tp.__dictoffset__, -1) + + # Use fresh subclasses: each class has its own shared keys, which + # may already be exhausted on the base types by earlier tests. + class BytesIO(_io.BytesIO): pass + class StringIO(_io.StringIO): pass + class BufferedReader(_io.BufferedReader): pass + class TextIOWrapper(_io.TextIOWrapper): pass + class RawIO(_io._RawIOBase): + def __init__(self): + self.attr = 1 + instances = [ + BytesIO(b"data"), + StringIO("data"), + BufferedReader(_io.BytesIO(b"data")), + TextIOWrapper(_io.BytesIO()), + RawIO(), + ] + for obj in instances: + with self.subTest(obj=type(obj).__name__): + obj.attr = 1 + self.assertTrue(_testinternalcapi.has_inline_values(obj)) + self.assertEqual(obj.__dict__["attr"], 1) + def test_class_hierarchy(self): def check_subs(types, base): for tp in types: diff --git a/Lib/test/test_io/test_memoryio.py b/Lib/test/test_io/test_memoryio.py index 89b0e4c9463ac0..20c2777b6c3ce0 100644 --- a/Lib/test/test_io/test_memoryio.py +++ b/Lib/test/test_io/test_memoryio.py @@ -10,6 +10,7 @@ import io import _pyio as pyio import pickle +import struct import sys import weakref @@ -1037,11 +1038,13 @@ def test_setstate(self): @support.cpython_only def test_sizeof(self): if support.Py_GIL_DISABLED: - basesize = support.calcobjsize('P2n2Pni') + basesize = support.calcobjsize('P2nni') else: - basesize = support.calcobjsize('P2n2Pn') + basesize = support.calcobjsize('P2nn') check = self.check_sizeof self.assertEqual(object.__sizeof__(io.BytesIO()), basesize) + # sys.getsizeof() includes the managed dict and weakref pre-header + basesize += 2 * struct.calcsize('P') check(io.BytesIO(), basesize ) n = 1000 # use a variable to prevent constant folding check(io.BytesIO(b'a' * n), basesize + sys.getsizeof(b'a' * n)) diff --git a/Lib/test/test_io/test_textio.py b/Lib/test/test_io/test_textio.py index 6e649240f08d5b..246b2dc6087bd3 100644 --- a/Lib/test/test_io/test_textio.py +++ b/Lib/test/test_io/test_textio.py @@ -707,6 +707,22 @@ def test_tell_after_readline_with_cr(self): remaining = f.read() self.assertEqual(remaining, "") + def test_tell_after_seek_past_cr(self): + # tell() must tolerate a stale bytes-per-char ratio after seek(). + for data in (b'one\rtwo\rthree\r', + ('ab\r' + '\u20ac' * 15).encode()): + expected = data.decode().replace('\r', '\n') + for i in range(len(expected)): + with self.subTest(data=data, i=i): + f = self.TextIOWrapper(self.BytesIO(data), + encoding='utf-8') + f.read(i) + pos = f.tell() + f.read(1) + f.seek(pos) + self.assertEqual(f.read(1), expected[i]) + f.seek(f.tell()) + self.assertEqual(f.read(), expected[i + 1:]) def test_seek_with_encoder_state(self): f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004") @@ -1772,6 +1788,17 @@ def search(name): class PyTextIOWrapperTest(TextIOWrapperTest, PyTestCase): shutdown_error = "LookupError: unknown encoding: ascii" + def test_tell_with_invalid_b2cratio(self): + for ratio in (float('nan'), float('inf'), -float('inf'), 1e100): + with self.subTest(ratio=ratio): + with self.TextIOWrapper(self.BytesIO(b'abc'), + encoding='utf-8') as f: + self.assertEqual(f.read(1), 'a') + f._b2cratio = ratio + position = f.tell() + f.seek(position) + self.assertEqual(f.read(), 'bc') + class IncrementalNewlineDecoderTest: diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_io/test_winconsoleio.py similarity index 99% rename from Lib/test/test_winconsoleio.py rename to Lib/test/test_io/test_winconsoleio.py index 1bae884ed9ae3e..b2474007da9893 100644 --- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_io/test_winconsoleio.py @@ -160,7 +160,7 @@ def test_write(self): testcases.append(data + b'\xff') # incomplete multibyte sequence with self.subTest(data=data): - self.assertEqual(f.write(data), len(a)) + self.assertEqual(f.write(data), len(a or data)) for data in testcases: with self.subTest(data=data): self.assertEqual(f.write(data), len(data)) diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 9110124d9423cf..4b398c1f41ef60 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -1596,16 +1596,69 @@ def testNextNetworkWithBadPrefix(self): self.assertRaises(ValueError, self.ipv6_network.next_network, 150) def testNextNetworkOutOfAddressSpace(self): + error = r"out of address space, cannot make another /{} network" ipv4 = ipaddress.IPv4Network('255.255.255.0/24') - self.assertRaisesRegex( - ValueError, - 'out of address space, cannot make another /24 network', - ipv4.next_network) + with self.assertRaisesRegex(ValueError, error.format(24)): + ipv4.next_network() + with self.assertRaisesRegex(ValueError, error.format(25)): + ipv4.next_network(25) ipv6 = ipaddress.IPv6Network('ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112') - self.assertRaisesRegex( - ValueError, - 'out of address space, cannot make another /112 network', - ipv6.next_network) + with self.assertRaisesRegex(ValueError, error.format(112)): + ipv6.next_network() + with self.assertRaisesRegex(ValueError, error.format(120)): + ipv6.next_network(120) + + def testPrevNetwork(self): + ipv4 = ipaddress.IPv4Network('1.2.3.0/24') + self.assertEqual( + ipv4.prev_network(), + ipaddress.IPv4Network('1.2.2.0/24'), + ) + self.assertEqual( + ipv4.prev_network(prev_prefix=16), + ipaddress.IPv4Network('1.1.0.0/16'), + ) + self.assertEqual( + ipv4.prev_network(prev_prefix=25), + ipaddress.IPv4Network('1.2.2.128/25'), + ) + self.assertEqual( + ipv4.prev_network(prev_prefix=23), + ipaddress.IPv4Network('1.2.0.0/23'), + ) + + ipv6 = ipaddress.IPv6Network('2001:dbb8:aaaa:aaaa::/64') + self.assertEqual( + ipv6.prev_network(), + ipaddress.IPv6Network('2001:dbb8:aaaa:aaa9::/64'), + ) + self.assertEqual( + ipv6.prev_network(prev_prefix=48), + ipaddress.IPv6Network('2001:dbb8:aaa9::/48'), + ) + self.assertEqual( + ipv6.prev_network(prev_prefix=88), + ipaddress.IPv6Network('2001:dbb8:aaaa:aaa9:ffff:ff00::/88'), + ) + + def testPrevNetworkWithBadPrefix(self): + self.assertRaises(ValueError, self.ipv4_network.prev_network, 0) + self.assertRaises(ValueError, self.ipv4_network.prev_network, 35) + self.assertRaises(ValueError, self.ipv6_network.prev_network, 0) + self.assertRaises(ValueError, self.ipv6_network.prev_network, 150) + + def testPrevNetworkOutOfAddressSpace(self): + error = r"out of address space, cannot make another /{} network" + ipv4 = ipaddress.IPv4Network('0.0.0.0/24') + with self.assertRaisesRegex(ValueError, error.format(24)): + ipv4.prev_network() + with self.assertRaisesRegex(ValueError, error.format(25)): + ipv4.prev_network(25) + ipv6 = ipaddress.IPv6Network('::/112') + with self.assertRaisesRegex(ValueError, error.format(112)): + ipv6.prev_network() + with self.assertRaisesRegex(ValueError, error.format(120)): + ipv6.prev_network(120) def testFancySubnetting(self): self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)), diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index fe8617309da29d..6ebde4848a5a49 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -532,6 +532,23 @@ def spam(): with self.assertRaises(StopIteration): next(spam.iterator) + def test_iter_function_sentinel_replaced_during_compare(self): + # gh-158031: the sentinel's __eq__ replaces the sentinel, which must + # stay alive until the comparison is finished. + class Sentinel: + def __eq__(self, other): + it.__setstate__(((), StopIteration)) + return NotImplemented + + class Result: + def __eq__(self, other): + self.other_type = type(other) + return NotImplemented + + it = iter(Result, Sentinel()) + result = next(it) + self.assertIs(result.other_type, Sentinel) + # Test exception propagation through function iterator def test_exception_function(self): def spam(state=[0]): diff --git a/Lib/test/test_json/test_default.py b/Lib/test/test_json/test_default.py index 811880a15c8020..7f3ef8b8cb9f96 100644 --- a/Lib/test/test_json/test_default.py +++ b/Lib/test/test_json/test_default.py @@ -1,7 +1,29 @@ import collections +import collections.abc from test.test_json import PyTest, CTest +class CustomIndexDict(collections.abc.Mapping, dict): + # Using `Mapping` here to make this a complete dict subclass, but note the bug in #110941 + # is not specific to `Mapping` subclasses and the outcome would have been the same with a + # fully manually implemented dict subclass. + + def __init__(self, keys: tuple = ()): + self._keys = keys + + def __getitem__(self, k): + try: + return self._keys.index(k) + except ValueError: + raise KeyError(k) + + def __iter__(self): + return iter(self._keys) + + def __len__(self): + return len(self._keys) + + class TestDefault: def test_default(self): self.assertEqual( @@ -36,6 +58,23 @@ def test_ordereddict(self): self.dumps(od, sort_keys=True), '{"a": 1, "b": 2, "c": 3, "d": 4}') + # This should behave identically for PyTest and CTest: see #110941. + def test_custom_dict(self): + cd = CustomIndexDict(("b", "a")) + self.assertEqual( + self.dumps(cd), + '{"b": 0, "a": 1}') + self.assertEqual( + self.dumps(cd, sort_keys=True), + '{"a": 1, "b": 0}') + + def test_empty_custom_dict(self): + # Exercise the fast path when a dict subclass is empty. + cd = CustomIndexDict() + self.assertEqual( + self.dumps(cd), + '{}') + class TestPyDefault(TestDefault, PyTest): pass class TestCDefault(TestDefault, CTest): pass diff --git a/Lib/test/test_json/test_fail.py b/Lib/test/test_json/test_fail.py index 7f99a6331d3625..76ab6f9ced8901 100644 --- a/Lib/test/test_json/test_fail.py +++ b/Lib/test/test_json/test_fail.py @@ -201,6 +201,24 @@ def test_unexpected_data(self): '%s: line 1 column %d (char %d)' % (msg, idx + 1, idx)) + def test_invalid_escape_position(self): + # The position points to the backslash of the invalid escape. + test_cases = [ + ('"\\a"', 1), + ('"ab\\x"', 3), + ('["\\\'"]', 2), + ('{"k": "v\\ "}', 8), + ] + for data, idx in test_cases: + with self.subTest(data=data): + with self.assertRaises(self.JSONDecodeError) as cm: + self.loads(data) + err = cm.exception + self.assertStartsWith(err.msg, 'Invalid \\escape') + self.assertEqual(err.pos, idx) + self.assertEqual(err.lineno, 1) + self.assertEqual(err.colno, idx + 1) + def test_extra_data(self): test_cases = [ ('[]]', 'Extra data', 2), diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index c16bf1b1fe020e..c33a9a5e6f2609 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1734,6 +1734,32 @@ class MyInt(int): # GH-117195 -- This shouldn't crash object.__sizeof__(1) + def test_long_add_overallocate(self): + # see gh-100687 + x = (MASK//2) * (MASK+1) + x2 = (MASK//2 + 1) * (MASK+1) + self.assertEqual(x + x2, MASK * (MASK + 1)) + + def test_karatsuba_single_digit_parts(self): + # gh-100687: k_mul() adds the halves of its operands with x_add(), + # and those halves can be single digits. + a = 1 + (1 << (SHIFT * 70)) + b = 1 << (SHIFT * 139) + self.assertEqual(a * b, (1 << (SHIFT * 139)) + (1 << (SHIFT * 209))) + self.assertEqual(b * a, a * b) + + def test_karatsuba_zero_parts(self): + # gh-100687: k_lopsided_mul() passes slices of the larger operand + # to k_mul(), and such a slice can consist of zero digits only. + n = KARATSUBA_CUTOFF + 30 + a = (1 << (SHIFT * n)) - 1 + b = 1 << (SHIFT * n * 3) + self.assertEqual(a * b, a << (SHIFT * n * 3)) + self.assertEqual(b * a, a * b) + # Only the high half of a slice is zero. + b += 1 << (SHIFT * (n + 1)) + self.assertEqual(a * b, (a << (SHIFT * n * 3)) + (a << (SHIFT * (n + 1)))) + def test_hash(self): # gh-136599 self.assertEqual(hash(-1), -2) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index a1250c668223e7..93be513bf26a91 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -287,8 +287,6 @@ def testAtanh(self): self.assertRaises(ValueError, math.atanh, NINF) self.assertTrue(math.isnan(math.atanh(NAN))) - @unittest.skipIf(sys.platform.startswith("sunos"), - "skipping, see gh-138573") def testAtan2(self): self.assertRaises(TypeError, math.atan2) self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) @@ -350,6 +348,15 @@ def testAtan2(self): self.assertTrue(math.isnan(math.atan2(NAN, 2.3))) self.assertTrue(math.isnan(math.atan2(NAN, INF))) self.assertTrue(math.isnan(math.atan2(NAN, NAN))) + # overflow and underflow of y/x + self.assertEqual(math.atan2(1E-320, 1E300), 0.0) + self.assertEqual(math.atan2(-1E-320, 1E300), -0.0) + self.assertAlmostEqual(math.atan2(1E300, 1E-320), math.pi/2) + self.assertAlmostEqual(math.atan2(1E300, -1E-320), math.pi/2) + self.assertAlmostEqual(math.atan2(1E-320, -1E300), math.pi) + self.assertAlmostEqual(math.atan2(-1E-320, -1E300), -math.pi) + self.assertAlmostEqual(math.atan2(-1E300, 1E-320), -math.pi/2) + self.assertAlmostEqual(math.atan2(-1E300, -1E-320), -math.pi/2) def testCbrt(self): self.assertRaises(TypeError, math.cbrt) diff --git a/Lib/test/test_math_property.py b/Lib/test/test_math_property.py index 7d51aa17b4cc27..a4b901fb4d4f35 100644 --- a/Lib/test/test_math_property.py +++ b/Lib/test/test_math_property.py @@ -1,9 +1,11 @@ import functools import unittest from math import isnan, nextafter -from test.support import requires_IEEE_754 +from test.support import requires_IEEE_754, mark_module from test.support.hypothesis_helper import hypothesis +mark_module('hypothesis') + floats = hypothesis.strategies.floats integers = hypothesis.strategies.integers diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 274c6acab4a651..290f22340f546f 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -6,6 +6,7 @@ from test import support import unittest +import xml.dom.expatbuilder import xml.dom.minidom from xml.dom.minidom import parse, Attr, Node, Document, Element, parseString @@ -1274,9 +1275,55 @@ def testCloneElementDeep(self): self.assertEqual(len(clone.childNodes), 1) self.assertEqual(clone.childNodes.length, 1) self.assertIsNone(clone.parentNode) - self.assertTrue(clone.toxml(), '') + self.assertEqual(clone.toxml(), '') dom.unlink() + def create_dom1_element(self): + # DOM Level 1 nodes have no namespace, but their names can contain + # a colon. + doc = Document() + root = doc.appendChild(doc.createElement("root")) + root.setAttribute("xmlns", "http://xml.python.org/default") + root.setAttribute("xmlns:xsi", "http://xml.python.org/xsi") + root.setAttribute("xsi:type", "t") + root.setAttributeNS("http://xml.python.org/ns", "p:a", "v") + root.appendChild(doc.createElement("svg:rect")) + root.appendChild( + doc.createElementNS("http://xml.python.org/ns", "p:b")) + return doc, root + + def check_clone_names(self, clone, node): + for name in ("nodeName", "prefix", "localName", "namespaceURI"): + self.assertEqual(getattr(clone, name), getattr(node, name), name) + + def check_clone_element(self, clone, elem, doc): + self.assertIs(clone.ownerDocument, doc) + self.assertIsNone(clone.parentNode) + self.check_clone_names(clone, elem) + self.assertEqual(len(clone.attributes), len(elem.attributes)) + for attr in elem.attributes.values(): + with self.subTest(attr=attr.name): + a = clone.getAttributeNode(attr.name) + self.check_clone_names(a, attr) + self.assertEqual(a.value, attr.value) + self.assertIs(a.ownerElement, clone) + self.assertEqual(len(clone.childNodes), len(elem.childNodes)) + for c, child in zip(clone.childNodes, elem.childNodes): + self.check_clone_names(c, child) + + def testCloneElementDOM1Names(self): + doc, root = self.create_dom1_element() + self.check_clone_element(root.cloneNode(True), root, doc) + doc.unlink() + + def testCloneElementNoNamespaces(self): + doc = xml.dom.expatbuilder.parseString( + "", + namespaces=False) + root = doc.documentElement + self.check_clone_element(root.cloneNode(True), root, doc) + doc.unlink() + def testCloneDocumentShallow(self): doc = parseString("\n" "" @@ -1395,6 +1442,13 @@ def testImportDocumentTypeDeep(self): self.assertRaises(xml.dom.NotSupportedErr, target.importNode, src.doctype, 1) + def testImportElementDOM1Names(self): + doc1, root = self.create_dom1_element() + doc2 = Document() + self.check_clone_element(doc2.importNode(root, True), root, doc2) + doc1.unlink() + doc2.unlink() + # Testing attribute clones uses a helper, and should always be deep, # even if the argument to cloneNode is false. def check_clone_attribute(self, deep, testName): @@ -1417,6 +1471,24 @@ def testCloneAttributeShallow(self): def testCloneAttributeDeep(self): self.check_clone_attribute(1, "testCloneAttributeDeep") + def testCloneAttributeDOM1Names(self): + doc1 = Document() + attr = doc1.createAttribute("a:b") + attr.value = "v" + doc2 = Document() + for method, clone, owner in [ + ("cloneNode", attr.cloneNode(True), doc1), + ("importNode", doc2.importNode(attr, True), doc2), + ]: + with self.subTest(method=method): + self.check_clone_names(clone, attr) + self.assertEqual(clone.value, "v") + self.assertTrue(clone.specified) + self.assertIsNone(clone.ownerElement) + self.assertIs(clone.ownerDocument, owner) + doc1.unlink() + doc2.unlink() + def check_clone_pi(self, deep, testName): doc = parseString("") pi = doc.firstChild @@ -1880,6 +1952,28 @@ def testRenameAttribute(self): self.checkRenameNodeSharedConstraints(doc, attr) doc.unlink() + def testRenameAttributeToPrefixedName(self): + doc = parseString("") + elem = doc.documentElement + attr = elem.getAttributeNode("a") + attr = doc.renameNode(attr, "http://xml.python.org/ns", "p:c") + self.assertEqual(attr.name, "p:c") + self.assertEqual(attr.prefix, "p") + self.assertEqual(attr.localName, "c") + self.assertEqual(attr.namespaceURI, "http://xml.python.org/ns") + self.assertIsNone(elem.getAttributeNode("a")) + self.assertIs(elem.getAttributeNodeNS("http://xml.python.org/ns", "c"), + attr) + + # A rejected rename leaves the attribute on its element. + attr = elem.getAttributeNode("b") + self.assertRaises(xml.dom.NamespaceErr, doc.renameNode, attr, + "http://xml.python.org/ns", "xml:lang") + self.assertEqual(attr.name, "b") + self.assertIsNone(attr.namespaceURI) + self.assertIs(elem.getAttributeNode("b"), attr) + doc.unlink() + def testRenameElement(self): doc = parseString("") elem = doc.documentElement @@ -1923,6 +2017,18 @@ def testRenameElement(self): self.checkRenameNodeSharedConstraints(doc, elem) doc.unlink() + def testRenameElementToPrefixedName(self): + doc = parseString("") + elem = doc.documentElement + elem = doc.renameNode(elem, "http://xml.python.org/ns", "p:c") + self.assertEqual(elem.tagName, "p:c") + self.assertEqual(elem.nodeName, "p:c") + self.assertEqual(elem.prefix, "p") + self.assertEqual(elem.localName, "c") + self.assertEqual(elem.namespaceURI, "http://xml.python.org/ns") + self.assertIs(doc.documentElement, elem) + doc.unlink() + def testRenameOther(self): # We have to create a comment node explicitly since not all DOM # builders used with minidom add comments to the DOM. @@ -2228,10 +2334,71 @@ def testDeepcopiedDocument(self): doc2 = copy.deepcopy(doc) self.assert_recursive_equal(doc, doc2) - def testSerializeCommentNodeWithDoubleHyphen(self): - doc = create_doc_without_doctype() - doc.appendChild(doc.createComment("foo--bar")) - self.assertRaises(ValueError, doc.toxml) + def testWriteXMLDoctypeIdentifiers(self): + impl = getDOMImplementation() + def serialize(publicId, systemId): + doctype = impl.createDocumentType("r", publicId, systemId) + return impl.createDocument(None, "r", doctype).toxml()[22:] + self.assertEqual(serialize(None, None), '') + self.assertEqual(serialize(None, "s"), "") + self.assertEqual(serialize("p", "s"), + "") + # the quote which is not in the literal is used + self.assertEqual(serialize("it's", "sys'tem"), + '') + # a system literal cannot contain both quotes + with self.assertRaisesRegex(ValueError, "both quotes"): + serialize(None, 'a"b\'c') + # a public identifier can only contain PubidChars + for publicId in ['pub"lic', 'p>', 'p<', 'p&', 'p[', '\xe9']: + with self.subTest(publicId=publicId): + with self.assertRaisesRegex(ValueError, + "invalid public identifier"): + serialize(publicId, "s") + # a public identifier is meaningless without a system identifier + with self.assertRaisesRegex(ValueError, "requires a system"): + serialize("p", None) + + def testSerializeComment(self): + impl = getDOMImplementation() + def serialize(data): + doc = impl.createDocument(None, "r", None) + doc.documentElement.appendChild(doc.createComment(data)) + return doc.toxml()[22:] + self.assertEqual(serialize("foo"), "") + self.assertEqual(serialize(""), "") + self.assertEqual(serialize("-foo"), "") + with self.assertRaisesRegex(ValueError, "'--' is not allowed"): + serialize("foo--bar") + # a trailing hyphen would be fused with the closing "-->" + with self.assertRaisesRegex(ValueError, "cannot end with"): + serialize("foo-") + + def testSerializeProcessingInstruction(self): + impl = getDOMImplementation() + def serialize(target, data): + doc = impl.createDocument(None, "r", None) + pi = doc.createProcessingInstruction(target, data) + doc.documentElement.appendChild(pi) + return doc.toxml()[22:] + self.assertEqual(serialize("p", "a"), "") + self.assertEqual(serialize("p", ""), "") + self.assertEqual(serialize("xml-stylesheet", 'href="a"'), + '') + # "?>" would close the processing instruction + with self.assertRaisesRegex(ValueError, "'\\?>' is not allowed"): + serialize("p", "a?>b") + # the target "xml" is reserved + for target in ["xml", "XML", "XmL"]: + with self.subTest(target=target): + with self.assertRaisesRegex(ValueError, "reserved"): + serialize(target, "a") + # the target is a name + doc = impl.createDocument(None, "r", None) + pi = doc.createProcessingInstruction("p", "a") + doc.documentElement.appendChild(pi) + pi.target = "a b" + self.assertRaises(xml.dom.InvalidCharacterErr, doc.toxml) def testEmptyXMLNSValue(self): diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 96fce534ba75d3..e24d3918cfa978 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -1235,6 +1235,36 @@ def test_expanduser(self): + def test_abspath_dots_and_spaces(self): + # gh-85681: trailing dots and spaces are stripped from the last + # component, and a single trailing dot from other components. + # This is the observed Windows behavior, not a specification. + tester('ntpath.abspath("C:/spam. . .")', "C:\\spam") + tester('ntpath.abspath("C:/spam.")', "C:\\spam") + tester('ntpath.abspath("C:/spam ")', "C:\\spam") + tester('ntpath.abspath("C:/spam..")', "C:\\spam") + tester('ntpath.abspath("C:/.spam")', "C:\\.spam") + tester('ntpath.abspath("C:/spam./eggs")', "C:\\spam\\eggs") + tester('ntpath.abspath("C:/spam.b./eggs")', "C:\\spam.b\\eggs") + tester('ntpath.abspath("C:/spam ./eggs")', "C:\\spam \\eggs") + # A trailing dot preceded by a dot is not stripped. + tester('ntpath.abspath("C:/spam../eggs")', "C:\\spam..\\eggs") + tester('ntpath.abspath("C:/spam.../eggs")', "C:\\spam...\\eggs") + # Trailing dots and spaces are stripped even in extended paths. + tester('ntpath.abspath("\\\\?\\C:/spam. . .")', "\\\\?\\C:\\spam") + tester('ntpath.abspath("\\\\.\\C:/spam. . .")', "\\\\.\\C:\\spam") + tester('ntpath.abspath("//server/share/spam. ")', + "\\\\server\\share\\spam") + + def test_relpath_dots_and_spaces(self): + # gh-85681: relpath() is based on abspath(). + tester('ntpath.relpath("foo ", "foo")', ".") + tester('ntpath.relpath("foo.", "foo")', ".") + tester('ntpath.relpath("foo/bar ", "foo")', "bar") + tester('ntpath.relpath("foo/bar.", "foo")', "bar") + # "foo.." denotes "foo" as the last component, but not as other. + tester('ntpath.relpath("foo../bar", "foo..")', "..\\foo..\\bar") + @unittest.skipUnless(nt, "abspath requires 'nt' module") def test_abspath(self): tester('ntpath.abspath("C:\\")', "C:\\") diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py index 10ecdd3bccc23f..f393e67ecbff59 100644 --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -1018,7 +1018,7 @@ def test_group_manipulate(self): description="Some more options") group.set_title("Bacon") group.add_option("--bacon", type="int") - self.assertTrue(self.parser.get_option_group("--bacon"), group) + self.assertIs(self.parser.get_option_group("--bacon"), group) # -- Test extending and parser.parse_args() ---------------------------- diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 662b57702fe26c..8b6b7deb65b88b 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -84,6 +84,19 @@ HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0 +def _get_file_attribute_mask(): + mask = 0 + for name in dir(stat): + if name.startswith("FILE_ATTRIBUTE_"): + mask |= getattr(stat, name) + return mask + + +# gh-126253: Ignore unknown attributes, such as 0x1_000_0000 +# (Directory flag of DOS file attribute in NTFS) +FILE_ATTRIBUTE_MASK = _get_file_attribute_mask() + + def requires_os_func(name): return unittest.skipUnless(hasattr(os, name), 'requires os.%s' % name) @@ -178,15 +191,28 @@ def test_type_fqdn(self): def fqdn(obj): return (obj.__module__, obj.__qualname__) - native = os.name self.assertEqual(fqdn(os.stat_result), ("os", "stat_result")) - self.assertEqual(fqdn(os.times_result), (native, "times_result")) + self.assertEqual(fqdn(os.times_result), ("os", "times_result")) + self.assertEqual(fqdn(os.DirEntry), ("os", "DirEntry")) if hasattr(os, "statvfs_result"): self.assertEqual(fqdn(os.statvfs_result), ("os", "statvfs_result")) + self.assertEqual(fqdn(os.uname_result), ("os", "uname_result")) if hasattr(os, "sched_param"): - self.assertEqual(fqdn(os.sched_param), (native, "sched_param")) + self.assertEqual(fqdn(os.sched_param), ("os", "sched_param")) if hasattr(os, "waitid_result"): - self.assertEqual(fqdn(os.waitid_result), (native, "waitid_result")) + self.assertEqual(fqdn(os.waitid_result), ("os", "waitid_result")) + + def test_pickle_global_names(self): + # Existing pickles name these via os or the platform module (posix + # or nt); both must keep loading, as must new pickles. + for name in ('stat', 'stat_result', 'statvfs_result', 'times_result', + 'uname_result'): + with self.subTest(name): + obj = getattr(os, name) + for module in ('os', os.name): + p = f'c{module}\n{name}\n.'.encode() + self.assertIs(pickle.loads(p), obj) + self.assertIs(pickle.loads(pickle.dumps(obj)), obj) # Tests creating TESTFN @@ -5032,8 +5058,15 @@ def assert_stat_equal(self, stat1, stat2, skip_fields): if attr in ("st_dev", "st_ino", "st_nlink", "st_ctime", "st_ctime_ns"): continue - self.assertEqual(getattr(stat1, attr), - getattr(stat2, attr), + value1 = getattr(stat1, attr) + value2 = getattr(stat2, attr) + if attr == "st_file_attributes": + # Filesystems may report attribute bits that aren't + # exposed by the stat module. + value1 &= FILE_ATTRIBUTE_MASK + value2 &= FILE_ATTRIBUTE_MASK + self.assertEqual(value1, + value2, (stat1, stat2, attr)) else: self.assertEqual(stat1, stat2) diff --git a/Lib/test/test_os/test_windows.py b/Lib/test/test_os/test_windows.py index e79d2c2c94d25b..b9659267af54c4 100644 --- a/Lib/test/test_os/test_windows.py +++ b/Lib/test/test_os/test_windows.py @@ -454,6 +454,67 @@ def test_unlink_removes_junction(self): self.assertFalse(os.path.exists(self.junction)) +class Win32ChmodTests(unittest.TestCase): + # gh-140774: os.chmod(follow_symlinks=True) and os.fchmod() go through + # SetFileInformationByHandle(), which interprets FileAttributes == 0 as + # "leave the attributes unchanged". Clearing the read-only flag of a + # file with no other attribute set must substitute FILE_ATTRIBUTE_NORMAL + # for 0 to take effect. + + def setUp(self): + self.fname = os.path.abspath(os_helper.TESTFN) + self.addCleanup(os_helper.unlink, self.fname) + self.addCleanup(self._make_writable) + create_file(self.fname) + + def _make_writable(self): + try: + os.chmod(self.fname, stat.S_IWRITE | stat.S_IREAD, + follow_symlinks=False) + except OSError: + pass + + def _set_readonly_only_attribute(self): + # Make FILE_ATTRIBUTE_READONLY the only attribute of the file + # (in particular, clear FILE_ATTRIBUTE_ARCHIVE). + ctypes = import_helper.import_module('ctypes') + wintypes = import_helper.import_module('ctypes.wintypes') + kernel32 = ctypes.WinDLL('Kernel32.dll', use_last_error=True) + SetFileAttributesW = kernel32.SetFileAttributesW + SetFileAttributesW.argtypes = (wintypes.LPCWSTR, wintypes.DWORD) + SetFileAttributesW.restype = wintypes.BOOL + + if not SetFileAttributesW(self.fname, stat.FILE_ATTRIBUTE_READONLY): + raise ctypes.WinError(ctypes.get_last_error()) + attributes = os.stat(self.fname).st_file_attributes + if attributes != stat.FILE_ATTRIBUTE_READONLY: + self.skipTest('file system reports extra file attributes ' + f'{attributes:#x}') + + def _assert_writable(self): + self.assertEqual(os.stat(self.fname).st_file_attributes + & stat.FILE_ATTRIBUTE_READONLY, 0) + + def test_chmod_clears_readonly_without_other_attributes(self): + self._set_readonly_only_attribute() + os.chmod(self.fname, stat.S_IWRITE | stat.S_IREAD, + follow_symlinks=True) + self._assert_writable() + + def test_fchmod_clears_readonly_without_other_attributes(self): + fd = os.open(self.fname, os.O_RDWR) + try: + self._set_readonly_only_attribute() + os.fchmod(fd, stat.S_IWRITE | stat.S_IREAD) + self._assert_writable() + + self._set_readonly_only_attribute() + os.chmod(fd, stat.S_IWRITE | stat.S_IREAD) + self._assert_writable() + finally: + os.close(fd) + + class Win32NtTests(unittest.TestCase): def test_getfinalpathname_handles(self): nt = import_helper.import_module('nt') diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index aff66c8efedbbc..8013b0df26ec81 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -1697,8 +1697,8 @@ def test_move_dir(self): self.assertEqual(target.joinpath('dirD', 'fileD').read_text(), "this is file D\n") self.assertTrue(target.joinpath('fileC').is_file()) - self.assertTrue(target.joinpath('fileC').read_text(), - "this is file C\n") + self.assertEqual(target.joinpath('fileC').read_text(), + "this is file C\n") @patch_replace def test_move_dir_other_fs(self): @@ -2332,6 +2332,9 @@ def test_touch_common(self): self.assertFalse(p.exists()) p.touch() self.assertTrue(p.exists()) + # Take the baseline from utime(None) like touch() below; creation can + # use a different clock (Emscripten NODEFS). + os.utime(p, None) st = p.stat() old_mtime = st.st_mtime old_mtime_ns = st.st_mtime_ns @@ -3062,12 +3065,7 @@ def test_glob_dotdot(self): else: # ".." segments are normalized first on Windows, so this path is stat()able. self.assertEqual(set(p.glob("xyzzy/..")), { P(self.base, "xyzzy", "..") }) - if sys.platform == "emscripten": - # Emscripten will return ELOOP if there are 49 or more ..'s. - # Can remove when https://github.com/emscripten-core/emscripten/pull/24591 is merged. - NDOTDOTS = 48 - else: - NDOTDOTS = 50 + NDOTDOTS = 50 self.assertEqual(set(p.glob("/".join([".."] * NDOTDOTS))), { P(self.base, *[".."] * NDOTDOTS)}) def test_glob_inaccessible(self): diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index cfcc8c6df57b94..bc9591d05de6b3 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -7,6 +7,7 @@ import os import pdb import re +import signal import sys import types import codecs @@ -2973,6 +2974,68 @@ def test_pdb_with_inline_breakpoint(): (Pdb) c """ +def test_pdb_sigint(): + """SIGINT should pause continue, next, until and return + + >>> def interrupt(): + ... signal.raise_signal(signal.SIGINT) + + >>> def test_function(): + ... import pdb; pdb.Pdb(readrc=False).set_trace() + ... interrupt() + ... interrupt() + ... interrupt() + ... interrupt() + + >>> with PdbTestInput([ + ... 'n', + ... 'n', + ... 'up', + ... 'until 5', + ... 'up', + ... 'r', + ... 'up', + ... 'c', + ... 'c', + ... ]): + ... test_function() + > (2)test_function() + -> import pdb; pdb.Pdb(readrc=False).set_trace() + (Pdb) n + > (3)test_function() + -> interrupt() + (Pdb) n + + Program interrupted. (Use 'cont' to resume). + > (2)interrupt() + -> signal.raise_signal(signal.SIGINT) + (Pdb) up + > (3)test_function() + -> interrupt() + (Pdb) until 5 + + Program interrupted. (Use 'cont' to resume). + > (2)interrupt() + -> signal.raise_signal(signal.SIGINT) + (Pdb) up + > (4)test_function() + -> interrupt() + (Pdb) r + + Program interrupted. (Use 'cont' to resume). + > (2)interrupt() + -> signal.raise_signal(signal.SIGINT) + (Pdb) up + > (5)test_function() + -> interrupt() + (Pdb) c + + Program interrupted. (Use 'cont' to resume). + > (2)interrupt() + -> signal.raise_signal(signal.SIGINT) + (Pdb) c + """ + def test_pdb_issue_20766(): """Test for reference leaks when the SIGINT handler is set. @@ -5432,9 +5495,15 @@ def setUpPdbBackend(backend): def setUp(test): import pdb pdb.set_default_backend(backend) + test.original_sigint_handler = signal.getsignal(signal.SIGINT) + test.previous_sigint_handler = pdb.Pdb._previous_sigint_handler return setUp def tearDown(test): + # Restore signal state even if a SIGINT doctest fails. + signal.signal(signal.SIGINT, test.original_sigint_handler) + pdb.Pdb._previous_sigint_handler = test.previous_sigint_handler + # Ensure that asyncio state has been cleared at the end of the test. # This prevents a "test altered the execution environment" warning if # asyncio features are used. diff --git a/Lib/test/test_perf_profiler.py b/Lib/test/test_perf_profiler.py index 0ebc0792cfc822..8a2c4889d9471c 100644 --- a/Lib/test/test_perf_profiler.py +++ b/Lib/test/test_perf_profiler.py @@ -23,14 +23,7 @@ raise unittest.SkipTest("test crash randomly on ASAN/MSAN build") -def supports_trampoline_profiling(): - perf_trampoline = sysconfig.get_config_var("PY_HAVE_PERF_TRAMPOLINE") - if not perf_trampoline: - return False - return int(perf_trampoline) == 1 - - -if not supports_trampoline_profiling(): +if not support.supports_perf_trampoline_profiling(): raise unittest.SkipTest("perf trampoline profiling not supported") diff --git a/Lib/test/test_perfmaps.py b/Lib/test/test_perfmaps.py index ee4eb50033c470..e6e5985233d1a8 100644 --- a/Lib/test/test_perfmaps.py +++ b/Lib/test/test_perfmaps.py @@ -1,20 +1,14 @@ import os import sys -import sysconfig import unittest +from test import support try: from _testinternalcapi import perf_map_state_teardown, write_perf_map_entry except ImportError: raise unittest.SkipTest("requires _testinternalcapi") -def supports_trampoline_profiling(): - perf_trampoline = sysconfig.get_config_var("PY_HAVE_PERF_TRAMPOLINE") - if not perf_trampoline: - return False - return int(perf_trampoline) == 1 - -if not supports_trampoline_profiling(): +if not support.supports_perf_trampoline_profiling(): raise unittest.SkipTest("perf trampoline profiling not supported") class TestPerfMapWriting(unittest.TestCase): diff --git a/Lib/test/test_profiling/test_heatmap.py b/Lib/test/test_profiling/test_heatmap.py index 1b4de0caa3d00f..f0aa4255b592cf 100644 --- a/Lib/test/test_profiling/test_heatmap.py +++ b/Lib/test/test_profiling/test_heatmap.py @@ -87,6 +87,25 @@ def test_extract_module_name_with_stdlib_file(self): self.assertEqual(module_type, 'stdlib') self.assertIn('os', module_name) + def test_extract_module_name_with_site_packages_inside_stdlib(self): + """Test extract_module_name when site-packages is inside stdlib.""" + stdlib = Path('/usr/lib/python3.15') + site_packages = stdlib / 'site-packages' + # On Windows, site.getsitepackages() also includes sys.prefix + path_info = { + 'stdlib': stdlib, + 'site_packages': [Path('/usr'), site_packages], + 'sys_path': [], + } + + site_packages_file = str(site_packages / 'requests' / 'api.py') + self.assertEqual(extract_module_name(site_packages_file, path_info), + ('requests.api', 'site-packages')) + + stdlib_file = str(stdlib / 'json' / 'decoder.py') + self.assertEqual(extract_module_name(stdlib_file, path_info), + ('json.decoder', 'stdlib')) + def test_extract_module_name_with_project_file(self): """Test extract_module_name with a project file.""" path_info = get_python_path_info() diff --git a/Lib/test/test_profiling/test_sampling_profiler/test_binary_format.py b/Lib/test/test_profiling/test_sampling_profiler/test_binary_format.py index 66a9c68a61c47d..94f7df6f8080d6 100644 --- a/Lib/test/test_profiling/test_sampling_profiler/test_binary_format.py +++ b/Lib/test/test_profiling/test_sampling_profiler/test_binary_format.py @@ -10,7 +10,7 @@ from unittest import mock from collections import defaultdict -from test.support import captured_stderr +from test.support import captured_stderr, import_helper try: import _remote_debugging @@ -63,29 +63,16 @@ def make_interpreter(interp_id, threads): return InterpreterInfo((interp_id, threads)) -def extract_lineno(location): - """Extract line number from location (tuple or int or None).""" - if location is None: - return 0 # Treat None as 0 - if isinstance(location, tuple): - return location[0] if location[0] is not None else 0 - return location - - def extract_location(location): """Extract full location info as dict from location tuple or None.""" if location is None: return {"lineno": 0, "end_lineno": 0, "column": 0, "end_column": 0} - if isinstance(location, tuple) and len(location) >= 4: - return { - "lineno": location[0] if location[0] is not None else 0, - "end_lineno": location[1] if location[1] is not None else 0, - "column": location[2] if location[2] is not None else 0, - "end_column": location[3] if location[3] is not None else 0, - } - # Fallback for old-style location - lineno = location[0] if isinstance(location, tuple) else location - return {"lineno": lineno or 0, "end_lineno": lineno or 0, "column": 0, "end_column": 0} + return { + "lineno": location[0], + "end_lineno": location[1], + "column": location[2], + "end_column": location[3], + } def frame_to_dict(frame): @@ -127,6 +114,28 @@ def export(self, filename): pass +def encode_varint(value): + """Encode an unsigned varint.""" + out = bytearray() + while value >= 0x80: + out.append((value & 0x7F) | 0x80) + value >>= 7 + out.append(value) + return bytes(out) + + +def decode_varint(data, offset): + """Decode an unsigned varint, returning (value, next offset).""" + value = shift = 0 + while True: + byte = data[offset] + offset += 1 + value |= (byte & 0x7F) << shift + shift += 7 + if byte < 0x80: + return value, offset + + def samples_to_by_thread(samples): """Convert input samples to by-thread format for comparison.""" by_thread = defaultdict(list) @@ -147,6 +156,20 @@ def samples_to_by_thread(samples): class BinaryFormatTestBase(unittest.TestCase): """Base class with common setup/teardown for binary format tests.""" + HDR_OFF_STR_TABLE = 40 + FILE_HEADER_PLACEHOLDER_SIZE = 64 + + # Sample block layout (see binary_io.h). + BLOCK_MAGIC = b"TBLK" + COLUMN_COUNT = 6 + COLUMN_STREAM_IDS = 1 + COLUMN_TIMESTAMP_RESIDUALS = 2 + COLUMN_STATUS_BITMAPS = 3 + COLUMN_STACK_INSTRUCTIONS = 4 + COLUMN_FRAME_IDS = 5 + COLUMN_ZSTD = 1 + COLUMN_CONSTANT = 2 + def setUp(self): self.temp_files = [] @@ -155,6 +178,13 @@ def tearDown(self): if os.path.exists(f): os.unlink(f) + def temp_filename(self): + """Return a new temporary file name that is removed on cleanup.""" + with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as f: + filename = f.name + self.temp_files.append(filename) + return filename + def create_binary_file(self, samples, interval=1000, compression="none", mode=None, capture_config=None): """Create a test binary file and track it for cleanup.""" @@ -166,9 +196,7 @@ def create_binary_file(self, samples, interval=1000, compression="none", def write_binary_file(self, samples, interval=1000, compression="none", mode=None, capture_config=None): """Like create_binary_file but also returns the writer collector.""" - with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as f: - filename = f.name - self.temp_files.append(filename) + filename = self.temp_filename() collector = BinaryCollector( filename, interval, compression=compression, mode=mode, @@ -179,6 +207,50 @@ def write_binary_file(self, samples, interval=1000, compression="none", collector.export(None) return filename, collector + def _read_offset(self, filename, hdr_off): + with open(filename, "rb") as raw: + raw.seek(hdr_off) + return struct.unpack("=Q", raw.read(8))[0] + + def read_block_columns(self, filename): + """Return the header flags and (decoded size, encoding) per column. + + Each block is a dict with frame_width, common_status_flags, + status_bitmap_flags and columns. + """ + with open(filename, "rb") as raw: + data = raw.read() + sample_end = struct.unpack_from("=Q", data, self.HDR_OFF_STR_TABLE)[0] + offset = self.FILE_HEADER_PLACEHOLDER_SIZE + blocks = [] + while offset < sample_end: + magic_end = offset + len(self.BLOCK_MAGIC) + self.assertEqual(data[offset:magic_end], self.BLOCK_MAGIC) + _, offset = decode_varint(data, magic_end) # sample count + _, offset = decode_varint(data, offset) # base timestamp + block = {"frame_width": data[offset], + "common_status_flags": data[offset + 1], + "status_bitmap_flags": data[offset + 2]} + offset += 3 + columns = [] + stored_total = 0 + for _ in range(self.COLUMN_COUNT): + decoded_size, offset = decode_varint(data, offset) + encoding = data[offset] + offset += 1 + if encoding == self.COLUMN_ZSTD: + stored_size, offset = decode_varint(data, offset) + elif encoding == self.COLUMN_CONSTANT: + stored_size = 1 + else: + stored_size = decoded_size + columns.append((decoded_size, encoding)) + stored_total += stored_size + offset += stored_total + block["columns"] = columns + blocks.append(block) + return blocks + def roundtrip(self, samples, interval=1000, compression="none"): """Write samples to binary and read back.""" filename = self.create_binary_file(samples, interval, compression) @@ -393,6 +465,29 @@ def test_binary_replay_preserves_main_thread_for_gecko(self): self.assertTrue(threads[1]["isMainThread"]) self.assertFalse(threads[2]["isMainThread"]) + def test_binary_replay_starts_gecko_at_earliest_sample(self): + """Binary replay uses the earliest sample as Gecko's time origin.""" + filename = self.temp_filename() + collector = BinaryCollector(filename, 1000, compression="none") + for thread_id, name, timestamp in [(1, "first", 1000), + (2, "second", 2000), + (2, "changed", 3000)]: + frame = make_frame(f"{name}.py", 10, name) + collector.collect( + [make_interpreter(0, [make_thread(thread_id, [frame])])], + timestamp_us=timestamp, + ) + collector.export(None) + + gecko = GeckoCollector(1000) + with BinaryReader(filename) as reader: + self.assertEqual(reader.replay_samples(gecko), 3) + + profile = gecko._build_profile() + threads = {thread["tid"]: thread for thread in profile["threads"]} + self.assertEqual(threads[1]["samples"]["time"], [0.0]) + self.assertEqual(threads[2]["samples"]["time"], [1.0, 2.0]) + def test_multiple_threads_per_sample(self): """Multiple threads in one sample roundtrip exactly.""" threads = [ @@ -739,6 +834,34 @@ def test_unicode_funcnames(self): self.assertEqual(count, 1) self.assert_samples_equal(samples, collector) + def test_surrogates_in_frame_names(self): + cases = [ + ("\udce9", r"\udce9"), # A surrogate-escaped filename byte. + ("\uddd0", r"\uddd0"), # The surrogate from gh-160601. + ("\ud800", r"\ud800"), + ("\udfff", r"\udfff"), + ("\ud800\udfff", r"\ud800\udfff"), + ("caf\xe9\udce9\U0001f40d", "caf\xe9\\udce9\U0001f40d"), + ] + for name, escaped_name in cases: + with self.subTest(name=name): + frames = [ + make_frame(f"/path/{name}.py", 42, f"func_{name}"), + make_frame("test.py", 1, "main"), + ] + samples = [[make_interpreter(0, [make_thread(1, frames)])]] * 3 + collector, count = self.roundtrip(samples) + self.assertEqual(count, 3) + + expected_frames = [ + make_frame(f"/path/{escaped_name}.py", 42, + f"func_{escaped_name}"), + frames[1], + ] + expected = [[make_interpreter( + 0, [make_thread(1, expected_frames)])]] * 3 + self.assert_samples_equal(expected, collector) + def test_special_char_filenames(self): """Filenames with special characters roundtrip exactly.""" filenames = [ @@ -897,6 +1020,307 @@ def test_rejects_non_pathlike(self): with self.assertRaises(TypeError): _remote_debugging.BinaryReader(123) + def test_writer_rejects_invalid_compression(self): + """BinaryWriter rejects an unknown compression type.""" + with self.assertRaisesRegex(ValueError, "compression"): + _remote_debugging.BinaryWriter( + self.temp_filename(), 1000, 0, compression=2 + ) + + def test_writer_rejects_status_outside_byte_range(self): + """A status above 255 raises OverflowError and drops that sample.""" + filename = self.temp_filename() + writer = _remote_debugging.BinaryWriter(filename, 1000, 0) + valid = [make_interpreter(0, [make_thread(1, [], status=1)])] + invalid = [make_interpreter(0, [make_thread(1, [], status=256)])] + writer.write_sample(valid, 1000) + with self.assertRaisesRegex(OverflowError, "thread status"): + writer.write_sample(invalid, 2000) + self.assertTrue(writer.limit_reached) + writer.write_sample(valid, 3000) + writer.finalize() + self.assertEqual(writer.total_samples, 2) + + with BinaryReader(filename) as reader: + self.assertEqual(reader.replay_samples(RawCollector()), 2) + + def test_writer_reports_stack_reuse(self): + """A stack seen earlier in the block is stored as STACK_REUSE.""" + filename = self.temp_filename() + frames = [make_frame("shared.py", line, "shared") for line in range(3)] + sample = [make_interpreter(0, [ + make_thread(1, frames), + make_thread(2, frames), + ])] + writer = _remote_debugging.BinaryWriter(filename, 1000, 0) + writer.write_sample(sample, 1000) + stats = writer.get_stats() + self.assertEqual(stats["reuse_records"], 1) + self.assertEqual(stats["total_records"], 2) + self.assertEqual(stats["total_samples"], 2) + writer.close() + + def test_writer_buffered_bytes_match_flushed_bytes(self): + """bytes_written before finalize() matches the flushed size.""" + filename = self.temp_filename() + frames = [make_frame("a.py", line, "f") for line in range(300)] + writer = _remote_debugging.BinaryWriter(filename, 1000, 0) + for i in range(100): + sample = [make_interpreter(0, [ + make_thread(1, frames[i:i + 5], status=i % 3), + make_thread(2, frames[:3]), + ])] + writer.write_sample(sample, 1000 * (i + 1)) + buffered = writer.get_stats()["bytes_written"] + writer.finalize() + + [block] = self.read_block_columns(filename) + self.assertEqual(buffered, + sum(size for size, _ in block["columns"])) + + def test_samples_span_multiple_blocks(self): + """Replay preserves samples and progress across block boundaries.""" + # Stream 4097 starts the second block. Thread 1 then returns. + frame = make_frame("many_threads.py", 1, "work") + samples = [ + [make_interpreter(0, [make_thread(i + 1, [frame])])] + for i in range(4097) + ] + samples.append(samples[0]) + filename = self.create_binary_file(samples) + self.assertEqual(len(self.read_block_columns(filename)), 2) + + collector = RawCollector() + progress = mock.Mock() + with BinaryReader(filename) as reader: + self.assertEqual(reader.replay_samples(collector, progress), + len(samples)) + self.assert_samples_equal(samples, collector) + total = len(samples) + self.assertEqual(progress.call_args_list, + [mock.call(current, total) + for current in (0, 1000, 2000, 3000, 4000, total)]) + + def test_block_size_limit_starts_new_block(self): + """A thread whose samples exceed 1 MiB is split across blocks.""" + # Each sample writes a new 256-frame stack, about 1 KiB while + # buffered, so 1,100 samples need more than one block. + frames = [make_frame("deep.py", line, "f") for line in range(1356)] + samples = [ + [make_interpreter(0, [make_thread(1, frames[i:i + 256])])] + for i in range(1100) + ] + filename = self.create_binary_file(samples) + blocks = self.read_block_columns(filename) + self.assertGreaterEqual(len(blocks), 2) + for block in blocks: + self.assertLessEqual(sum(size for size, _ in block["columns"]), + 1024 * 1024) + with BinaryReader(filename) as reader: + self.assertEqual(reader.replay_samples(RawCollector()), + len(samples)) + + def test_writer_uses_constant_columns_and_narrow_frame_ids(self): + """Repeated bytes are stored once and frame IDs use the least width.""" + frame = make_frame("steady.py", 1, "steady") + sample = [make_interpreter(0, [make_thread(1, [frame])])] + filename = self.temp_filename() + writer = _remote_debugging.BinaryWriter(filename, 1000, 0) + for i in range(100): + writer.write_sample(sample, 1000 * (i + 1)) + writer.finalize() + # One stream and on-time samples: every stream ID and residual is 0 + [block] = self.read_block_columns(filename) + self.assertEqual(block["frame_width"], 1) + self.assertEqual(block["columns"][self.COLUMN_STREAM_IDS], + (100, self.COLUMN_CONSTANT)) + self.assertEqual(block["columns"][self.COLUMN_TIMESTAMP_RESIDUALS], + (100, self.COLUMN_CONSTANT)) + + # 300 distinct frames need two bytes per frame ID + frames = [make_frame("wide.py", line, "f") for line in range(300)] + samples = [ + [make_interpreter(0, [make_thread(1, frames[:150])])], + [make_interpreter(0, [make_thread(1, frames[150:])])], + ] + filename = self.create_binary_file(samples) + [block] = self.read_block_columns(filename) + self.assertEqual(block["frame_width"], 2) + self.assertEqual(block["columns"][self.COLUMN_FRAME_IDS][0], 300 * 2) + + def test_writer_stores_common_status_bits_once(self): + """Status bits set in every sample go in the header, not a bitmap.""" + frame = make_frame("status.py", 1, "f") + filename = self.temp_filename() + writer = _remote_debugging.BinaryWriter(filename, 1000, 0) + for i in range(20): + # HAS_GIL in every sample; ON_CPU in every other one + status = THREAD_STATUS_HAS_GIL | (THREAD_STATUS_ON_CPU * (i % 2)) + sample = [make_interpreter(0, [make_thread(1, [frame], status)])] + writer.write_sample(sample, 1000 * (i + 1)) + writer.finalize() + [block] = self.read_block_columns(filename) + self.assertEqual(block["common_status_flags"], THREAD_STATUS_HAS_GIL) + self.assertEqual(block["status_bitmap_flags"], THREAD_STATUS_ON_CPU) + # One bitmap of ceil(20 / 8) bytes, for ON_CPU only + self.assertEqual(block["columns"][self.COLUMN_STATUS_BITMAPS][0], 3) + + @unittest.skipUnless(ZSTD_AVAILABLE, "zstd compression not available") + def test_writer_compresses_repetitive_columns(self): + """Compressed files store repetitive columns with zstd.""" + root = [make_frame("root.py", line, "root") for line in range(20)] + leaves = [make_frame("leaf.py", 1, "even"), + make_frame("leaf.py", 2, "odd")] + samples = [ + [make_interpreter(0, [make_thread(1, [leaves[i % 2]] + root)])] + for i in range(2000) + ] + filename = self.create_binary_file(samples, compression="zstd") + [block] = self.read_block_columns(filename) + columns = block["columns"] + self.assertEqual(columns[self.COLUMN_STACK_INSTRUCTIONS][1], + self.COLUMN_ZSTD) + self.assertEqual(columns[self.COLUMN_FRAME_IDS][1], self.COLUMN_ZSTD) + + def test_replay_byte_swapped_file(self): + """A file written with the other byte order replays the same.""" + samples = [[make_interpreter(0, [ + make_thread(1, [make_frame("a.py", 1, "f")]), + make_thread(2, [make_frame("b.py", 2, "g")]), + ])] for _ in range(3)] + filename = self.temp_filename() + writer = _remote_debugging.BinaryWriter(filename, 1000, 0) + for i, sample in enumerate(samples): + writer.write_sample(sample, 1000 * (i + 1)) + writer.finalize() + + data = bytearray(pathlib.Path(filename).read_bytes()) + # Header fields: magic, version, start time, interval, sample + # count, thread count, table offsets, compression and config + fields = [(0, 4), (4, 4), (12, 8), (20, 8), (28, 8), (36, 4), + (40, 8), (48, 8), (56, 4), (60, 4)] + # Footer string and frame counts + fields += [(len(data) - 32, 4), (len(data) - 28, 4)] + # The block's stream table entries follow its header + offset = self.FILE_HEADER_PLACEHOLDER_SIZE + len(self.BLOCK_MAGIC) + _, offset = decode_varint(data, offset) # sample count + _, offset = decode_varint(data, offset) # base timestamp + offset += 3 + for _ in range(self.COLUMN_COUNT): + _, offset = decode_varint(data, offset) + offset += 1 # raw encoding, no stored size + for entry in range(2): + fields += [(offset + entry * 12, 8), (offset + entry * 12 + 8, 4)] + for start, size in fields: + data[start:start + size] = data[start:start + size][::-1] + pathlib.Path(filename).write_bytes(data) + + collector = RawCollector() + with BinaryReader(filename) as reader: + self.assertEqual(reader.replay_samples(collector), 6) + self.assert_samples_equal(samples, collector) + + def test_reader_replays_twice(self): + """Replaying twice gives the same samples and statistics.""" + class RetainingCollector(RawCollector): + def __init__(self): + super().__init__() + self.frame_lists = [] + + def collect(self, stack_frames, timestamps_us): + self.frame_lists.extend( + thread.frame_info + for interp in stack_frames + for thread in interp.threads + ) + super().collect(stack_frames, timestamps_us) + + samples = [ + [make_interpreter(0, [ + make_thread(1, [make_frame("a.py", i % 3, "f")]), + make_thread(2, [make_frame("b.py", 1, "g")]), + ])] + for i in range(10) + ] + filename = self.create_binary_file(samples) + with BinaryReader(filename) as reader: + first = RetainingCollector() + second = RetainingCollector() + self.assertEqual(reader.replay_samples(first), 20) + self.assertEqual(reader.replay_samples(second), 20) + self.assertEqual(reader.get_stats()["total_samples"], 20) + self.assertEqual(first.by_thread, second.by_thread) + for first_frames, second_frames in zip( + first.frame_lists, second.frame_lists, strict=True + ): + self.assertIsNot(first_frames, second_frames) + self.assertEqual(len(first_frames), len(second_frames)) + for first_frame, second_frame in zip( + first_frames, second_frames, strict=True + ): + self.assertIs(first_frame, second_frame) + + def test_reader_replays_large_file_twice(self): + """Replaying more sample data than the reader buffers is exact.""" + class StackCollector: + def __init__(self): + self.stacks = [] + + def collect(self, stack_frames, timestamps_us): + for interp in stack_frames: + for thread in interp.threads: + stack = list(thread.frame_info) + self.stacks.extend([stack] * len(timestamps_us)) + + # Deep, shifting stacks give about 2.5 MB of sample data, more than + # the reader reads at once on platforms without mmap. + frames = [make_frame("deep.py", line, "f") for line in range(1356)] + stacks = [frames[i % 1100:i % 1100 + 256] for i in range(5000)] + samples = [[make_interpreter(0, [make_thread(1, stack)])] + for stack in stacks] + filename = self.create_binary_file(samples) + sample_size = (self._read_offset(filename, self.HDR_OFF_STR_TABLE) + - self.FILE_HEADER_PLACEHOLDER_SIZE) + self.assertGreater(sample_size, 2 * 1024 * 1024) + with BinaryReader(filename) as reader: + first = StackCollector() + second = StackCollector() + self.assertEqual(reader.replay_samples(first), len(samples)) + self.assertEqual(reader.replay_samples(second), len(samples)) + self.assertEqual(first.stacks, stacks) + self.assertEqual(second.stacks, stacks) + + def test_writer_repeat_run_spans_interleaved_streams(self): + """A thread's repeat run continues across other threads' samples.""" + filename = self.temp_filename() + idle = [make_frame("idle.py", 1, "wait")] + busy = [[make_frame("busy.py", 1, "even")], + [make_frame("busy.py", 2, "odd")]] + writer = _remote_debugging.BinaryWriter(filename, 1000, 0) + for i in range(10): + sample = [make_interpreter(0, [ + make_thread(1, idle), + make_thread(2, busy[i % 2]), + ])] + writer.write_sample(sample, 1000 * (i + 1)) + stats = writer.get_stats() + self.assertEqual(stats["repeat_records"], 1) + self.assertEqual(stats["repeat_samples"], 9) + writer.finalize() + + collector = RawCollector() + with BinaryReader(filename) as reader: + self.assertEqual(reader.replay_samples(collector), 20) + self.assertEqual(reader.get_stats()["repeat_samples"], 9) + idle_frames = [frame_to_dict(frame) for frame in idle] + self.assertEqual( + [sample["frames"] for sample in collector.by_thread[(0, 1)]], + [idle_frames] * 10) + self.assertEqual( + [sample["frames"][0]["funcname"] + for sample in collector.by_thread[(0, 2)]], + ["even", "odd"] * 5) + def test_invalid_path_error_preserves_pathlib(self): """Missing path: OSError carries the original path object, not a string.""" missing = pathlib.Path("/i/do/not/exist") @@ -907,13 +1331,9 @@ def test_invalid_path_error_preserves_pathlib(self): def test_writer_handles_empty_stack_first_sample(self): """BinaryWriter.write_sample tolerates an empty stack on a fresh thread. - Regression test for the C-level RLE bug in process_thread_sample: a - freshly-created ThreadEntry has prev_stack_depth == 0, so an empty - curr_stack compares as STACK_REPEAT against the zero-initialized - previous stack. Before the fix, this fell through the - `&& !is_new_thread` guard into write_sample_with_encoding, which had - no handler for STACK_REPEAT and raised - RuntimeError("Invalid stack encoding type"). Goes through + Regression test for gh-149342: an empty first stack equals the empty + stack a new thread starts from, but it must still be written as a + stack (STACK_REPLACE_SUFFIX 0 0) rather than a repeat. Goes through BinaryWriter.write_sample directly so the test cannot be masked by any Python-level filtering. """ @@ -931,8 +1351,7 @@ def test_writer_handles_empty_stack_first_sample(self): # scenario that exposes the bug. writer.write_sample(empty_sample, 1000) writer.write_sample(empty_sample, 2000) - # Mix in a real sample to exercise the transition out of the - # empty-stack RLE buffer. + # Mix in a real sample after the repeated empty stack. real_sample = [ make_interpreter(0, [make_thread(1, [make_frame("a.py", 1, "f")])]) ] @@ -942,8 +1361,8 @@ def test_writer_handles_empty_stack_first_sample(self): reader_collector = RawCollector() with BinaryReader(filename) as reader: count = reader.replay_samples(reader_collector) - # Empty-stack samples are recorded as STACK_REPEAT records with - # depth-0 stacks; the file must replay all three samples. + # The empty stack is written once and then repeated; the file must + # replay all three samples. self.assertEqual(count, 3) def test_writer_handles_mixed_empty_and_real_first_sample(self): @@ -977,11 +1396,9 @@ def test_writer_handles_mixed_empty_and_real_first_sample(self): def test_writer_total_samples_after_finalize_matches_reader(self): """BinaryWriter.total_samples after finalize() matches the reader's count.""" - # Five IDENTICAL samples force every sample beyond the first into the - # per-thread RLE buffer. Regression for the cached_total_samples - # ordering bug: capturing the cache BEFORE binary_writer_finalize() - # missed the buffered samples that flush_pending_rle() counts. Keep - # the samples identical to preserve coverage. See gh-149342. + # Five IDENTICAL samples stay in the open block, as one repeat, until + # finalize() flushes it; total_samples must still count all of them. + # Keep the samples identical to preserve coverage. See gh-149342. samples = [ [make_interpreter(0, [make_thread(1, [make_frame("a.py", 1, "f")])])] ] * 5 @@ -992,8 +1409,8 @@ def test_writer_total_samples_after_finalize_matches_reader(self): self.assertEqual(writer_collector.total_samples, len(samples)) self.assertEqual(writer_collector.total_samples, replayed) - def test_rle_buffer_flushes_before_finalize(self): - """RLE buffer flushes before the writer is destroyed during finalize().""" + def test_open_block_flushes_on_finalize(self): + """finalize() writes the open block before the writer is destroyed.""" with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as f: filename = f.name self.temp_files.append(filename) @@ -1194,11 +1611,10 @@ def test_finalize_failure_breaks_writer(self): class TestBinaryFormatValidation(BinaryFormatTestBase): """Tests for malformed binary files.""" + HDR_OFF_VERSION = 4 HDR_OFF_SAMPLES = 28 HDR_OFF_THREADS = 36 - HDR_OFF_STR_TABLE = 40 HDR_OFF_FRAME_TABLE = 48 - FILE_HEADER_PLACEHOLDER_SIZE = 64 FILE_FOOTER_SIZE = 32 FTR_OFF_STRINGS = 0 FTR_OFF_FRAMES = 4 @@ -1261,17 +1677,13 @@ def test_replay_rejects_trailing_partial_sample_header(self): with BinaryReader(filename) as reader: with self.assertRaises(ValueError) as cm: reader.replay_samples(RawCollector()) - self.assertEqual(str(cm.exception), "Truncated sample data: 1 trailing bytes") + self.assertEqual(str(cm.exception), + "Invalid sample block: truncated header") # Minimum on-disk size of one table entry (see binary_io.h). MIN_STRING_ENTRY_SIZE = 1 MIN_FRAME_ENTRY_SIZE = 7 - def _read_offset(self, filename, hdr_off): - with open(filename, "rb") as raw: - raw.seek(hdr_off) - return struct.unpack("=Q", raw.read(8))[0] - def _patch_footer_count(self, filename, ftr_off, value): size = os.path.getsize(filename) with open(filename, "r+b") as raw: @@ -1345,6 +1757,21 @@ def test_open_accepts_frame_count_at_capacity_boundary(self): f"possible {max_frames}", ) + def test_open_rejects_older_format_version(self): + """Open rejects version 1 files and reports version 2.""" + filename = self.create_binary_file([], compression="none") + with BinaryReader(filename) as reader: + self.assertEqual(reader.get_info()["version"], 2) + with open(filename, "r+b") as raw: + raw.seek(self.HDR_OFF_VERSION) + raw.write(struct.pack("=I", 1)) + with self.assertRaisesRegex( + ValueError, + r"Unsupported format version 1 \(this reader supports " + r"version 2\)"): + with BinaryReader(filename): + pass + def test_sample_count_reads_full_64_bits(self): """sample_count values requiring the upper 32 bits decode correctly.""" filename = self.create_binary_file([], compression="none") @@ -1370,12 +1797,333 @@ def test_sample_count_boundary_values(self): with BinaryReader(filename) as reader: self.assertEqual(reader.get_info()["sample_count"], value) + # Block column indices and encodings (see binary_io.h). + MAX_BLOCK_SIZE = 1024 * 1024 + + def _write_single_block(self, num_samples=1, instructions=b"\x01\x00\x00", + frame_ids=b"", *, header_samples=None, + file_samples=None, stream_count=1, + stream_table=None, stream_ids=None, + residuals=None, statuses=b"", status_flags=(0, 0), + frame_width=1, descriptors=None, magic=b"TBLK", + truncate=None, frame_count=1): + """Write a file whose only block is built from the given parts. + + By default every sample belongs to stream 0, has status 0 and is + exactly one sample interval after the previous one, and the only + stack is empty (STACK_REPLACE_SUFFIX 0 0). The frame table holds + *frame_count* frames: frame 0 is in "a.py" and frame N in "N.py". + *descriptors* maps a column index to (encoding, decoded size) to + replace its raw descriptor. + """ + frames = [make_frame("a.py", 1, "f")] + frames += [make_frame(f"{index}.py", 1, "f") + for index in range(1, frame_count)] + samples = [[make_interpreter(0, [make_thread(1, frames[i:i + 256])])] + for i in range(0, frame_count, 256)] + filename = self.create_binary_file(samples, compression="none") + data = bytearray(pathlib.Path(filename).read_bytes()) + sample_end = struct.unpack_from("=Q", data, self.HDR_OFF_STR_TABLE)[0] + if stream_table is None: + stream_table = b"".join(struct.pack("=QI", thread, 0) + for thread in range(1, stream_count + 1)) + columns = ( + stream_table, + b"\x00" * num_samples if stream_ids is None else stream_ids, + b"\x00" * num_samples if residuals is None else residuals, + statuses, + instructions, + frame_ids, + ) + if header_samples is None: + header_samples = num_samples + block = (magic + encode_varint(header_samples) + + b"\x01" # base timestamp + + bytes([frame_width, *status_flags])) + payload = b"" + for index, column in enumerate(columns): + encoding, decoded_size = (descriptors or {}).get( + index, (0, len(column))) + block += encode_varint(decoded_size) + bytes([encoding]) + if encoding == self.COLUMN_ZSTD: + block += encode_varint(len(column)) + if encoding == self.COLUMN_CONSTANT: + column = column[:1] + payload += column + block = (block + payload)[:truncate] + data[self.FILE_HEADER_PLACEHOLDER_SIZE:sample_end] = block + shift = len(block) - (sample_end - self.FILE_HEADER_PLACEHOLDER_SIZE) + if file_samples is None: + file_samples = num_samples + struct.pack_into("=Q", data, self.HDR_OFF_SAMPLES, file_samples) + struct.pack_into("=I", data, self.HDR_OFF_THREADS, + max(len(stream_table) // 12, 1)) + for offset in (self.HDR_OFF_STR_TABLE, self.HDR_OFF_FRAME_TABLE): + old_offset = struct.unpack_from("=Q", data, offset)[0] + struct.pack_into("=Q", data, offset, old_offset + shift) + struct.pack_into("=Q", data, len(data) - 24, len(data)) + pathlib.Path(filename).write_bytes(data) + return filename + + def assert_block_rejected(self, filename, message): + with BinaryReader(filename) as reader: + with self.assertRaises(ValueError) as cm: + reader.replay_samples(RawCollector()) + self.assertEqual(str(cm.exception), f"Invalid sample block: {message}") + + def test_replay_accepts_hand_built_block(self): + """Replay accepts a block built by hand from its columns.""" + # STACK_REPLACE_SUFFIX 0 0, then STACK_REPEAT 2. + filename = self._write_single_block(3, b"\x01\x00\x00\x00\x02") + collector = RawCollector() + with BinaryReader(filename) as reader: + self.assertEqual(reader.replay_samples(collector), 3) + self.assertEqual(len(collector.by_thread[(0, 1)]), 3) + + def test_replay_decodes_every_frame_id_width(self): + """Frame IDs decode correctly at every byte width.""" + # STACK_REPLACE_SUFFIX 0 1 reads one frame ID, stored in one byte + # plane per byte of the width, low byte first. + for width, frame_id in ((1, 43), (2, 299), (3, 299), (4, 299)): + with self.subTest(width=width): + filename = self._write_single_block( + 1, b"\x01\x00\x01", frame_id.to_bytes(width, "little"), + frame_width=width, frame_count=300) + collector = RawCollector() + with BinaryReader(filename) as reader: + self.assertEqual(reader.replay_samples(collector), 1) + frames = collector.by_thread[(0, 1)][0]["frames"] + self.assertEqual([frame["filename"] for frame in frames], + [f"{frame_id}.py"]) + + def test_replay_reads_constant_stream_and_residual_columns(self): + """Constant stream index and residual columns decode every sample.""" + # Every sample is in stream 0 and one microsecond late (zigzag 2). + filename = self._write_single_block( + 3, b"\x01\x00\x00\x00\x02", residuals=b"\x02" * 3, + descriptors={self.COLUMN_STREAM_IDS: (self.COLUMN_CONSTANT, 3), + self.COLUMN_TIMESTAMP_RESIDUALS: + (self.COLUMN_CONSTANT, 3)}) + collector = mock.Mock() + with BinaryReader(filename) as reader: + interval = reader.get_info()["sample_interval_us"] + self.assertEqual(reader.replay_samples(collector), 3) + timestamps = [ts for call in collector.collect.call_args_list + for ts in call.args[1]] + self.assertEqual(timestamps, + [2 + i * (interval + 1) for i in range(3)]) + + def test_replay_rejects_unterminated_constant_varint(self): + """A constant byte that never ends a varint is rejected.""" + for column, part in ((self.COLUMN_STREAM_IDS, "stream_ids"), + (self.COLUMN_TIMESTAMP_RESIDUALS, "residuals")): + with self.subTest(part): + filename = self._write_single_block( + **{part: b"\x80"}, + descriptors={column: (self.COLUMN_CONSTANT, 1)}) + with BinaryReader(filename) as reader: + with self.assertRaisesRegex(ValueError, + "incomplete varint"): + reader.replay_samples(RawCollector()) + + def test_replay_rejects_reentrant_replay(self): + """Replaying from inside the same reader's collector raises.""" + filename = self._write_single_block( + 2, b"\x01\x00\x00\x00\x01") # REPEAT 1 + + with BinaryReader(filename) as reader: + class ReentrantCollector: + def collect(self, stack_frames, timestamps_us): + reader.replay_samples(RawCollector()) + + with self.assertRaisesRegex(RuntimeError, "already replaying"): + reader.replay_samples(ReentrantCollector()) + self.assertEqual(reader.replay_samples(RawCollector()), 2) + + def test_replay_rejects_close_during_replay(self): + """Closing the reader from inside its collector raises.""" + filename = self._write_single_block( + 2, b"\x01\x00\x00\x00\x01") # REPEAT 1 + closers = { + "close": lambda reader: reader._reader.close(), + "__exit__": lambda reader: reader._reader.__exit__( + None, None, None), + "__init__": lambda reader: reader._reader.__init__(filename), + } + for name, close in closers.items(): + with self.subTest(name): + with BinaryReader(filename) as reader: + class ClosingCollector: + def collect(self, stack_frames, timestamps_us): + close(reader) + + with self.assertRaisesRegex(RuntimeError, "replaying"): + reader.replay_samples(ClosingCollector()) + self.assertEqual(reader.replay_samples(RawCollector()), 2) + + def test_replay_rejects_close_from_initial_progress_callback(self): + """Closing the reader from its initial progress callback raises.""" + filename = self._write_single_block( + 2, b"\x01\x00\x00\x00\x01") # REPEAT 1 + + with BinaryReader(filename) as reader: + def close_reader(current, total): + reader._reader.close() + + with self.assertRaisesRegex(RuntimeError, "replaying"): + reader.replay_samples(RawCollector(), close_reader) + self.assertEqual(reader.replay_samples(RawCollector()), 2) + + def test_replay_rejects_invalid_block(self): + """Replay rejects every kind of malformed block.""" + frames = self.COLUMN_FRAME_IDS + cases = [ + ("wrong magic", dict(magic=b"XBLK")), + # Magic, sample count and base timestamp, then 1 of 3 flag bytes + ("truncated header", dict(truncate=7)), + ("no samples", dict(header_samples=0)), + ("frame ID width out of range", dict(frame_width=0)), + ("frame ID width out of range", dict(frame_width=5)), + ("common and varying status flags overlap", + dict(status_flags=(1, 1))), + # Magic, sample count, base timestamp, 3 flag bytes and the + # first decoded size + ("truncated column descriptor", dict(truncate=10)), + ("unknown column encoding", dict(descriptors={frames: (3, 0)})), + ("zstd column in an uncompressed file", + dict(descriptors={self.COLUMN_STACK_INSTRUCTIONS: + (self.COLUMN_ZSTD, 3)})), + ("encoded column is empty", + dict(descriptors={frames: (self.COLUMN_CONSTANT, 0)})), + ("decoded columns exceed the block limit", + dict(frame_ids=b"\x00", + descriptors={frames: (self.COLUMN_CONSTANT, + self.MAX_BLOCK_SIZE)})), + # Rejected before anything is allocated for the samples + ("more samples than stored stream indices", + dict(header_samples=0xFFFFFFFF)), + ("column extends past the sample section", dict(truncate=-1)), + ("stream table has a partial entry", dict(stream_table=b"0" * 11)), + ("stream count out of range", dict(stream_table=b"")), + ("frame ID column has a partial value", + dict(frame_width=2, frame_ids=b"\x00")), + ("status bitmaps have the wrong size", dict(status_flags=(0, 1))), + ("status bitmap padding is not zero", + dict(status_flags=(0, 1), statuses=b"\x02")), + ("stream table repeats a thread", + dict(stream_table=struct.pack("=QI", 1, 0) * 2)), + ("sample refers to an unknown stream", dict(stream_ids=b"\x01")), + ("sample refers to an unknown stream", + dict(num_samples=2, stream_ids=b"\x01", + descriptors={self.COLUMN_STREAM_IDS: + (self.COLUMN_CONSTANT, 2)})), + ("stream index column has trailing data", + dict(num_samples=2, header_samples=1)), + # A constant column longer than the sample count is expanded + # and then rejected like any other trailing data + ("stream index column has trailing data", + dict(descriptors={self.COLUMN_STREAM_IDS: + (self.COLUMN_CONSTANT, 2)})), + ("timestamp column has trailing data", + dict(residuals=b"\x00\x00")), + ("timestamp column has trailing data", + dict(descriptors={self.COLUMN_TIMESTAMP_RESIDUALS: + (self.COLUMN_CONSTANT, 2)})), + ("frame index out of range", + dict(instructions=b"\x01\x00\x01", frame_ids=b"\x05")), + # Frame IDs 65536 and 2**24 only have a byte in the third or + # fourth plane + ("frame index out of range", + dict(instructions=b"\x01\x00\x01", frame_width=3, + frame_ids=(1 << 16).to_bytes(3, "little"), + frame_count=300)), + ("frame index out of range", + dict(instructions=b"\x01\x00\x01", frame_width=4, + frame_ids=(1 << 24).to_bytes(4, "little"), + frame_count=300)), + ("unknown stack instruction", dict(instructions=b"\x03")), + ("first stack of a stream cannot repeat", + dict(instructions=b"\x00\x01")), + ("repeat count out of range", + dict(num_samples=2, instructions=b"\x01\x00\x00\x00\x00")), + ("stack prefix out of range", dict(instructions=b"\x01\x01\x00")), + ("stack uses more frame IDs than stored", + dict(instructions=b"\x01\x00\x02", frame_ids=b"\x00")), + # STACK_REUSE 0 257 + ("stack is too deep", dict(instructions=b"\x02\x00\x81\x02")), + ("reused stack is not stored earlier", + dict(instructions=b"\x02\x00\x01", frame_ids=b"\x00")), + ("stack instruction column has trailing data", + dict(instructions=b"\x01\x00\x00\x01")), + ("frame ID column has unused values", dict(frame_ids=b"\x00")), + ("stream table has an unused entry", dict(stream_count=2)), + ("repeat count extends past the block", + dict(num_samples=2, instructions=b"\x01\x00\x00\x00\x02")), + ("more samples than the file header declares", + dict(file_samples=0)), + ] + for message, kwargs in cases: + with self.subTest(message, **kwargs): + filename = self._write_single_block(**kwargs) + self.assert_block_rejected(filename, message) + + @unittest.skipUnless(ZSTD_AVAILABLE, "zstd compression not available") + def test_replay_rejects_invalid_zstd_column(self): + """Replay rejects corrupt and mismatched zstd columns.""" + zstd = import_helper.import_module("compression.zstd") + instructions = b"\x01\x00\x00" + frame = zstd.compress( + instructions, + options={zstd.CompressionParameter.checksum_flag: 1}) + bad_checksum = frame[:-1] + bytes([frame[-1] ^ 1]) + cases = [ + ("Corrupted zstd frame header", instructions, len(instructions)), + ("zstd frame size does not match the decoded size", + frame, len(instructions) + 1), + ("zstd decompression error: ", bad_checksum, len(instructions)), + ("Invalid sample block: stored columns exceed the block limit", + b"\x00" * (self.MAX_BLOCK_SIZE + 1), len(instructions)), + ] + for message, stored, decoded_size in cases: + with self.subTest(message): + filename = self._write_single_block( + instructions=stored, + descriptors={self.COLUMN_STACK_INSTRUCTIONS: + (self.COLUMN_ZSTD, decoded_size)}) + with open(filename, "r+b") as raw: + raw.seek(56) # compression type + raw.write(struct.pack("=I", 1)) + with BinaryReader(filename) as reader: + with self.assertRaises(ValueError) as cm: + reader.replay_samples(RawCollector()) + self.assertStartsWith(str(cm.exception), message) + + def test_replay_propagates_callback_errors(self): + """Errors raised by the collector or progress callback propagate.""" + filename = self._write_single_block( + 2000, b"\x01\x00\x00\x00\xcf\x0f") # REPEAT 1999 + + class FailingCollector: + def collect(self, stack_frames, timestamps_us): + raise ZeroDivisionError + + def failing_progress(current, total): + if current: + raise ZeroDivisionError + + with BinaryReader(filename) as reader: + with self.assertRaises(ZeroDivisionError): + reader.replay_samples(FailingCollector()) + with self.assertRaises(ZeroDivisionError): + reader.replay_samples(RawCollector(), failing_progress) + self.assertEqual(reader.replay_samples(RawCollector()), 2000) + class TestBinaryEncodings(BinaryFormatTestBase): """Tests specifically targeting different stack encodings.""" - def test_stack_full_encoding(self): - """First sample uses STACK_FULL encoding and roundtrips.""" + def test_stack_first_sample_pattern(self): + """First sample stores its complete stack and roundtrips.""" frames = [make_frame(f"f{i}.py", i + 1, f"func{i}") for i in range(5)] samples = [[make_interpreter(0, [make_thread(1, frames)])]] collector, count = self.roundtrip(samples) @@ -1393,8 +2141,8 @@ def test_stack_repeat_encoding(self): self.assertEqual(count, 1000) self.assert_samples_equal(samples, collector) - def test_stack_suffix_encoding(self): - """Samples sharing suffix use STACK_SUFFIX and roundtrip.""" + def test_stack_shared_outer_frames_pattern(self): + """Samples keeping outer frames roundtrip (STACK_REPLACE_SUFFIX).""" samples = [] for i in range(10): frames = [make_frame(f"new{i}.py", i + 1, f"new{i}")] @@ -1409,8 +2157,8 @@ def test_stack_suffix_encoding(self): self.assertEqual(count, 10) self.assert_samples_equal(samples, collector) - def test_stack_pop_push_encoding(self): - """Samples with pop+push pattern roundtrip.""" + def test_stack_pop_push_pattern(self): + """Samples that pop frames and push new ones roundtrip.""" samples = [] base_frames = [make_frame("base.py", 10, "base")] @@ -1549,15 +2297,29 @@ def export(self, filename): def test_rle_long_run_splits_batches(self): - # Construct a single repeat record larger than the writer's buffer. - num_samples = 8193 + # Construct one block whose repeat run is longer than a replay batch. + num_samples = 8194 filename = self.create_binary_file([], compression="none") data = bytearray(pathlib.Path(filename).read_bytes()) - record = (struct.pack("=QIB", 1, 0, 0) # STACK_REPEAT - + b"\x81\x40" # 8193 as a varint - + b"\x01\x00" * num_samples) # delta=1, status=0 + instructions = (b"\x01\x00\x00" # STACK_REPLACE_SUFFIX 0 0 + + b"\x00\x81\x40") # STACK_REPEAT 8193 + record = (b"TBLK" + + b"\x82\x40" # 8194 samples as a varint + + b"\x01" # base timestamp + + b"\x01\x00\x00" # frame width, status flags + + b"\x0c\x00" # stream table: 12 raw bytes + + b"\x82\x40\x02" # stream indices: constant + + b"\x82\x40\x02" # timestamp residuals: constant + + b"\x00\x00" # status bitmaps: empty + + bytes([len(instructions), 0]) # raw instructions + + b"\x00\x00" # frame IDs: empty + + struct.pack("=QI", 1, 0) + + b"\x00" # every sample is in stream 0 + + b"\x00" # every residual is zero + + instructions) data[64:64] = record struct.pack_into("=Q", data, 12, 0) # start timestamp + struct.pack_into("=Q", data, 20, 1) # sample interval struct.pack_into("=Q", data, 28, num_samples) struct.pack_into("=I", data, 36, 1) # thread count for offset in (40, 48): # string and frame table offsets @@ -1571,7 +2333,7 @@ def test_rle_long_run_splits_batches(self): count = reader.replay_samples(collector) batches = [call.args[1] for call in collector.collect.call_args_list] self.assertEqual(count, num_samples) - self.assertEqual([len(batch) for batch in batches], [8192, 1]) + self.assertEqual([len(batch) for batch in batches], [1, 8192, 1]) self.assertEqual([ts for batch in batches for ts in batch], list(range(1, num_samples + 1))) @@ -1701,7 +2463,7 @@ def test_encoding_transitions_stress(self): # Repeat previous (will use RLE if identical) frames = base_frames[: random.randint(1, 5)] elif choice == 2: - # Add frames on top (suffix encoding) + # Add frames on top (keeps the outer frames) extra = random.randint(1, 3) frames = [ make_frame(f"top{i}_{j}.py", j, f"top{j}") @@ -1709,7 +2471,7 @@ def test_encoding_transitions_stress(self): ] frames.extend(base_frames[: random.randint(2, 4)]) else: - # Pop and push (pop-push encoding) + # Pop some frames and push new ones keep = random.randint(1, 3) push = random.randint(0, 2) frames = [ @@ -1893,6 +2655,25 @@ def test_replay_includes_persisted_stats(self): self.assertIn('"error_rate": 2.5', content) self.assertIn('"missed_samples": 1.5', content) + @unittest.skipUnless(ZSTD_AVAILABLE, "zstd compression not available") + def test_measured_stats_with_zstd(self): + """Measured statistics are read back when zstd is enabled.""" + frames = [make_frame(f"pkg/module_{i:03}.py", i + 1, f"func_{i:03}") + for i in range(200)] + sample = [make_interpreter(0, [make_thread(1, frames)])] + filename = self.temp_filename() + collector = BinaryCollector(filename, 2000, compression="zstd") + collector.collect(sample, timestamp_us=1000) + collector.set_stats(2000, 1.25, 4.0, error_rate=2.5, + missed_samples=1.5) + collector.export(None) + + with BinaryReader(filename) as reader: + info = reader.get_info() + self.assertEqual(info["duration_sec"], 1.25) + self.assertEqual(info["missed_samples"], 1.5) + self.assertEqual(reader.replay_samples(RawCollector()), 1) + def test_legacy_binary_has_no_measured_stats(self): frame = make_frame("hot.py", 99, "hot_func") bin_path = self.create_binary_file([ diff --git a/Lib/test/test_profiling/test_sampling_profiler/test_children.py b/Lib/test/test_profiling/test_sampling_profiler/test_children.py index 4b27c6dbb1896c..5908f71ea417d4 100644 --- a/Lib/test/test_profiling/test_sampling_profiler/test_children.py +++ b/Lib/test/test_profiling/test_sampling_profiler/test_children.py @@ -3,6 +3,7 @@ import argparse import io import os +import shutil import signal import subprocess import sys @@ -85,12 +86,11 @@ def reader(): return result[0] -def _wait_for_process_exec(proc, name, timeout): +def _wait_for_process_exec(proc, executable, timeout): # Wait until a child process calls exec() to replace the forked process # executable. Raise AssertionError on timeout. for _ in sleeping_retry(timeout, max_delay=0.1): - exe = os.readlink(f"/proc/{proc.pid}/exe") - if os.path.basename(exe) == name: + if os.path.samefile(f"/proc/{proc.pid}/exe", executable): break @@ -617,15 +617,18 @@ def test_is_python_process_python_subprocess(self): @unittest.skipUnless(sys.platform == "linux", "Linux only test") def test_is_python_process_non_python_subprocess(self): """Test that a non-Python subprocess is not detected as Python.""" + executable = shutil.which("sleep") + if executable is None: + self.skipTest("sleep command not found") # Start a non-Python subprocess (sleep command) proc = subprocess.Popen( - ["sleep", "60"], + [executable, "60"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) try: - _wait_for_process_exec(proc, "sleep", SHORT_TIMEOUT) + _wait_for_process_exec(proc, executable, SHORT_TIMEOUT) self.assertFalse( is_python_process(proc.pid), diff --git a/Lib/test/test_profiling/test_sampling_profiler/test_cli.py b/Lib/test/test_profiling/test_sampling_profiler/test_cli.py index e5dcd72c27ef5c..d548e4f06c8507 100644 --- a/Lib/test/test_profiling/test_sampling_profiler/test_cli.py +++ b/Lib/test/test_profiling/test_sampling_profiler/test_cli.py @@ -30,17 +30,72 @@ _generate_output_filename, _handle_output, _replay_with_reader, + _skip_idle_for_mode, main, ) from profiling.sampling.constants import ( PROFILING_MODE_ALL, PROFILING_MODE_CPU, + PROFILING_MODE_EXCEPTION, + PROFILING_MODE_GIL, PROFILING_MODE_WALL, ) from profiling.sampling.errors import SamplingScriptNotFoundError, SamplingModuleNotFoundError, SamplingUnknownProcessError from profiling.sampling.jsonl_collector import JsonlCollector class TestSampleProfilerCLI(unittest.TestCase): + def test_skip_idle_mode_mapping(self): + self.assertFalse(_skip_idle_for_mode(PROFILING_MODE_WALL)) + self.assertTrue(_skip_idle_for_mode(PROFILING_MODE_CPU)) + self.assertTrue(_skip_idle_for_mode(PROFILING_MODE_GIL)) + self.assertFalse(_skip_idle_for_mode(PROFILING_MODE_ALL)) + self.assertFalse(_skip_idle_for_mode(PROFILING_MODE_EXCEPTION)) + # Preserve the previous default for modes unknown to this CLI. + self.assertTrue(_skip_idle_for_mode(999)) + + @requires_remote_subprocess_debugging() + def test_exception_mode_captures_blocked_handler(self): + script = """ +import time + +try: + raise ValueError("boom") +except ValueError: + time.sleep(30) +""" + with tempfile.TemporaryDirectory() as temp_dir: + script_path = os.path.join(temp_dir, "blocked_handler.py") + output_path = os.path.join(temp_dir, "output.txt") + with open(script_path, "w", encoding="utf-8") as file: + file.write(script) + + result = subprocess.run( + [ + sys.executable, + "-m", + "profiling.sampling", + "run", + "--mode", + "exception", + "--collapsed", + "-r", + "1000", + "-d", + "1", + "-o", + output_path, + script_path, + ], + capture_output=True, + text=True, + timeout=30, + ) + + self.assertEqual(result.returncode, 0, result.stderr) + with open(output_path, encoding="utf-8") as file: + output = file.read() + self.assertIn("blocked_handler.py", output) + def _setup_sync_mocks(self, mock_socket, mock_popen): """Helper to set up socket and process mocks for coordinator tests.""" # Mock the sync socket with context manager support @@ -954,7 +1009,7 @@ def test_cli_replay_reader_errors_exit_cleanly(self): mock.patch("sys.argv", ["profiling.sampling.cli", "replay", profile]), mock.patch( "profiling.sampling.cli.BinaryReader", - side_effect=ValueError("Unsupported format version 2"), + side_effect=ValueError("Unsupported format version 3"), ), ): with self.assertRaises(SystemExit) as cm: @@ -962,7 +1017,7 @@ def test_cli_replay_reader_errors_exit_cleanly(self): self.assertEqual( str(cm.exception), - "Error: Unsupported format version 2", + "Error: Unsupported format version 3", ) def test_cli_replay_propagates_recorded_mode(self): diff --git a/Lib/test/test_profiling/test_sampling_profiler/test_collectors.py b/Lib/test/test_profiling/test_sampling_profiler/test_collectors.py index d4ba2df5044559..36e6b036b11940 100644 --- a/Lib/test/test_profiling/test_sampling_profiler/test_collectors.py +++ b/Lib/test/test_profiling/test_sampling_profiler/test_collectors.py @@ -467,6 +467,24 @@ def test_collapsed_stack_collector_export(self): self.assertIn(stack1_expected, lines) self.assertIn(stack2_expected, lines) + def test_collapsed_stack_collector_export_synthetic_frames(self): + collapsed_out = tempfile.NamedTemporaryFile(delete=False) + self.addCleanup(close_and_unlink, collapsed_out) + + for name in ("", "", ""): + with self.subTest(name=name): + collector = CollapsedStackCollector(1000) + collector.collect([ + MockInterpreterInfo(0, [MockThreadInfo(1, [ + _remote_debugging.FrameInfo(("~", None, name, None)), + MockFrameInfo("app.py", 10, "main"), + ])]) + ]) + with captured_stdout(): + collector.export(collapsed_out.name) + with open(collapsed_out.name, encoding="utf-8") as f: + self.assertEqual(f.read(), f"tid:1;app.py:main:10;{name} 1\n") + def test_collapsed_stack_collector_export_non_ascii_names(self): # gh-156810: frame names are written verbatim, so the output must be # opened with an encoding that can represent non-ASCII and @@ -596,6 +614,47 @@ def test_flamegraph_collector_export(self): self.assertIn('"sample_interval_usec": 10000', content) self.assertIn("samples * data.stats.sample_interval_usec / 1000", content) + def test_flamegraph_escapes_special_chars_in_funcname(self): + # Function names with <, >, & must be escaped in the HTML. + flamegraph_out = tempfile.NamedTemporaryFile( + suffix=".html", delete=False + ) + self.addCleanup(close_and_unlink, flamegraph_out) + + collector = FlamegraphCollector(1000) + frames = [ + MockInterpreterInfo( + 0, + [MockThreadInfo( + 1, + [MockFrameInfo("test.py", 1, "root"), + MockFrameInfo("test.py", 2, '&')] + )], + ) + ] + collector.collect(frames) + + with captured_stdout(), captured_stderr(): + export_ok = collector.export(flamegraph_out.name) + self.assertTrue(export_ok) + + with open(flamegraph_out.name, "r", encoding="utf-8") as f: + content = f.read() + + # The funcname's must be escaped in the embedded data so it + # cannot close the inline ", content) + + # The tooltip inserts the function name as HTML after resolving it. + self.assertIn( + "const funcname = escapeHtml(" + 'resolveString(d.data.funcname) || resolveString(d.data.name) || "");', + content, + ) + def test_flamegraph_collector_empty_export_fails(self): """Test empty flamegraph export reports no output.""" flamegraph_out = tempfile.NamedTemporaryFile( @@ -741,6 +800,51 @@ def test_gecko_collector_basic(self): self.assertGreater(stack_table["length"], 0) self.assertGreater(len(stack_table["frame"]), 0) + def test_gecko_collector_preserves_source_file_paths(self): + """Test that GeckoCollector preserves source file paths and distinguishes + files with the same basename in different directories.""" + collector = GeckoCollector(1000) + file_a = os.path.join("pkg_a", "__init__.py") + file_b = os.path.join("pkg_b", "__init__.py") + + test_frames = [ + MockInterpreterInfo( + 0, + [ + MockThreadInfo( + 1, + [ + MockFrameInfo(file_a, 10, "func_a"), + MockFrameInfo(file_b, 20, "func_b"), + ], + status=THREAD_STATUS_MAIN_THREAD, + ) + ], + ) + ] + collector.collect(test_frames) + profile_data = collector._build_profile() + + string_array = profile_data["shared"]["stringArray"] + func_table = profile_data["threads"][0]["funcTable"] + + self.assertEqual(func_table["length"], 2) + + funcs_by_name = { + string_array[func_table["name"][i]]: ( + func_table["fileName"][i], + string_array[func_table["fileName"][i]], + ) + for i in range(func_table["length"]) + } + + file_idx_a, resolved_file_a = funcs_by_name["func_a"] + file_idx_b, resolved_file_b = funcs_by_name["func_b"] + + self.assertEqual(resolved_file_a, file_a) + self.assertEqual(resolved_file_b, file_b) + self.assertNotEqual(file_idx_a, file_idx_b) + def test_gecko_collector_async_aware(self): collector = GeckoCollector(1000) @@ -1072,6 +1176,36 @@ def test_gecko_collector_markers(self): f"Marker {i} should have valid category", ) + def test_gecko_collector_replay_out_of_order_batches(self): + """Open markers are closed at the latest replayed sample.""" + collector = GeckoCollector(1000) + # Binary replay can send thread 2's last batch after thread 1's later + # samples + batches = [ + (1, [1000], THREAD_STATUS_ON_CPU), + (2, [1500], 0), + (1, [2000], THREAD_STATUS_ON_CPU), + (1, [3000, 4000], 0), + (2, [2500], 0), + ] + for tid, timestamps, status in batches: + frames = [MockFrameInfo(f"t{tid}.py", 10, f"func{tid}")] + collector.collect( + [MockInterpreterInfo(0, [MockThreadInfo(tid, frames, status=status)])], + timestamps_us=timestamps, + ) + + profile_data = collector._build_profile() + + end_times = [] + for thread_data in profile_data["threads"]: + markers = thread_data["markers"] + for start_time, end_time in zip(markers["startTime"], + markers["endTime"]): + self.assertLessEqual(start_time, end_time) + end_times.append(end_time) + self.assertEqual(max(end_times), 3.0) + def test_pstats_collector_export(self): collector = PstatsCollector( sample_interval_usec=1000000 @@ -2957,20 +3091,6 @@ def test_extract_lineno_from_none(self): """Test extracting lineno from None (synthetic frames).""" self.assertEqual(extract_lineno(None), 0) - def test_extract_lineno_from_int(self): - """Test extracting lineno from a bare integer line number. - - Mirrors normalize_location's int contract so callers like the - collapsed/flamegraph collectors do not crash on a bare-int location. - """ - self.assertEqual(extract_lineno(42), 42) - self.assertEqual(extract_lineno(0), 0) - - def test_normalize_location_with_int(self): - """Test normalize_location expands a legacy integer line number.""" - result = normalize_location(42) - self.assertEqual(result, (42, 42, -1, -1)) - def test_normalize_location_with_location_info(self): """Test normalize_location passes through LocationInfo.""" loc = LocationInfo(10, 15, 0, 5) diff --git a/Lib/test/test_profiling/test_sampling_profiler/test_integration.py b/Lib/test/test_profiling/test_sampling_profiler/test_integration.py index 3487647b76683e..0b9d55bacc6659 100644 --- a/Lib/test/test_profiling/test_sampling_profiler/test_integration.py +++ b/Lib/test/test_profiling/test_sampling_profiler/test_integration.py @@ -508,7 +508,10 @@ def test_sampling_with_collapsed_export(self): if ";" in stack_trace: stack_parts = stack_trace.split(";") for part in stack_parts: - # Each part should be file:function:line + # Special frames, such as and , + # do not have file and line information. + if part.startswith("<") and part.endswith(">"): + continue self.assertIn(":", part) def test_sampling_all_threads(self): diff --git a/Lib/test/test_pydoc/test_pydoc.py b/Lib/test/test_pydoc/test_pydoc.py index a63530c2905827..552b0c7d075949 100644 --- a/Lib/test/test_pydoc/test_pydoc.py +++ b/Lib/test/test_pydoc/test_pydoc.py @@ -2294,6 +2294,19 @@ def test_keywords(self): self.assertEqual(sorted(pydoc.Helper.keywords), sorted(keyword.kwlist + ['case', 'match', 'lazy'])) + def test_related_topics(self): + # Every related help topic and alias must be something help() can + # document. + for name, target in (*pydoc.Helper.topics.items(), + *pydoc.Helper.keywords.items()): + if isinstance(target, str): + target = ('', target) + for xref in target[1].split(): + with self.subTest(topic=name, xref=xref): + buf = StringIO() + pydoc.Helper(output=buf).help(xref) + self.assertNotIn('No help entry found', buf.getvalue()) + def test_symbols(self): # gh-161414 for symbol in pydoc.Helper.symbols: diff --git a/Lib/test/test_pyexpat.py b/Lib/test/test_pyexpat.py index c52c640bd4b26f..b962f47bc844c4 100644 --- a/Lib/test/test_pyexpat.py +++ b/Lib/test/test_pyexpat.py @@ -834,6 +834,103 @@ def handler(text): self.assertRaises(SpecificException, parser.Parse, xml.encode('iso8859')) + +class DefaultHandlerExceptionTest(unittest.TestCase): + # In a document that is not UTF-8, Expat passes a long piece of text such + # as this comment to the default handler in chunks of at most 1024 bytes, + # re-reading the handler between chunks. Clearing the handler from within + # the callback, by raising or by setting it to None, must not crash. + xml = ("" + % ('a' * 2000)).encode('iso8859') + + class SpecificException(Exception): + pass + + def check(self, name): + def handler(text): + if text.startswith('" + % ('a' * 2000)).encode('iso8859') + parser.Parse(xml, True) + + def test_CharacterDataHandler_with_buffer_text(self): + # With buffer_text, buffered character data is passed to the + # CharacterDataHandler just before the default handler is called; + # an exception there clears the handlers while Expat is still + # passing the comment to the default handler. + def handler(text): + raise self.SpecificException + + parser = expat.ParserCreate() + parser.buffer_text = True + parser.CharacterDataHandler = handler + parser.DefaultHandler = lambda text: None + xml = ("a" + % ('a' * 2000)).encode('iso8859') + self.assertRaises(self.SpecificException, parser.Parse, xml, True) + + class ChardataBufferTest(unittest.TestCase): """ test setting of chardata buffer size diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 1828fd735669d5..c11c759f559aff 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -732,6 +732,16 @@ def test_input_does_not_modify_history(self): self.assertEqual(wrapper.input(), "new input") self.assertEqual(reader.history, ["previous input"]) + def test_input_is_not_syntax_highlighted(self): + reader = self.prepare_reader(code_to_events("def f(): pass\n")) + reader.can_colorize = True + reader.gen_colors = Mock(return_value=()) + wrapper = _ReadlineWrapper(f_in=None, f_out=None, reader=reader) + + self.assertEqual(wrapper.input(), "def f(): pass") + reader.gen_colors.assert_not_called() + self.assertTrue(reader.can_colorize) + def test_input_error_does_not_modify_history(self): for text, exception in ( ("partial input\x03", KeyboardInterrupt), diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py index dbf7f91696b559..05c716f4a75a9c 100644 --- a/Lib/test/test_pyrepl/test_unix_console.py +++ b/Lib/test/test_pyrepl/test_unix_console.py @@ -259,6 +259,27 @@ def test_cursor_back_write(self, _os_write): _os_write.assert_any_call(ANY, b"2") con.restore() + def test_insert_char_only_for_midline_insert(self, _os_write): + insert_char = b"" + events = itertools.chain( + code_to_events("1"), + [Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))], + code_to_events("2"), + ) + _, con = handle_all_events( + events, + prepare_console=partial(unix_console, ich1=insert_char), + ) + relevant_writes = [] + for write in _os_write.call_args_list: + output = write.args[1] + if insert_char in output: + relevant_writes.append(insert_char) + elif output in {b"1", b"2"}: + relevant_writes.append(output) + self.assertEqual(relevant_writes, [b"1", insert_char, b"2"]) + con.restore() + def test_multiline_function_move_up_short_terminal(self, _os_write): # fmt: off code = ( diff --git a/Lib/test/test_pyrepl/test_utils.py b/Lib/test/test_pyrepl/test_utils.py index 5c60770c511c07..9ff178c74a091c 100644 --- a/Lib/test/test_pyrepl/test_utils.py +++ b/Lib/test/test_pyrepl/test_utils.py @@ -168,6 +168,67 @@ def test_gen_colors_keyword_highlighting(self): actual_highlights.append((span_text, color.tag)) self.assertEqual(actual_highlights, expected_highlights) + def test_gen_colors_soft_keywords_after_blank_lines(self): + cases = [ + ( + "def f(): pass\n", + "match -1:\n case _: pass", + ["match", "case", "_"], + ), + ( + "def f(): pass\n", + "match value:\n case _: pass", + ["match", "case", "_"], + ), + ( + "match value:\n case 0: pass\n", + " case -1: pass", + ["match", "case", "case"], + ), + ( + "match value:\n case 0: pass\n", + " case other: pass", + ["match", "case", "case"], + ), + ("def f(): pass\n", "type Alias = int", ["type"]), + ("def f(): pass\n", "lazy import mod", ["lazy"]), + ("def f(): pass\n", "lazy from mod import name", ["lazy"]), + ] + for prefix, statement, expected in cases: + for blank_line in ("\n", " \n", "# comment\n"): + code = prefix + blank_line + statement + with self.subTest(code=code): + actual = [ + code[color.span.start:color.span.end + 1] + for color in gen_colors(code) + if color.tag == "soft_keyword" + ] + self.assertEqual(actual, expected) + + def test_gen_colors_match_after_dedent(self): + for subject in ("-1", "value"): + for separator in ("", "\n", "# comment\n"): + code = ( + "if x:\n pass\n" + f"{separator}match {subject}:\n case _: pass" + ) + with self.subTest(code=code): + actual = [ + code[color.span.start:color.span.end + 1] + for color in gen_colors(code) + if color.tag == "soft_keyword" + ] + self.assertEqual(actual, ["match", "case", "_"]) + + def test_gen_colors_soft_keyword_names_in_brackets(self): + for opening, closing in ("()", "[]", "{}"): + for name in ("match", "case"): + code = f"{opening}\n{name} - 1\n{closing}" + with self.subTest(code=code): + self.assertNotIn( + "soft_keyword", [color.tag for color in gen_colors(code)] + ) + def test_gen_colors_command_highlighting(self): cases = [ # highlights bare command names (after stripping whitespaces) diff --git a/Lib/test/test_pyrepl/test_windows_console.py b/Lib/test/test_pyrepl/test_windows_console.py index 32c4255aa6b190..784b33d9247c11 100644 --- a/Lib/test/test_pyrepl/test_windows_console.py +++ b/Lib/test/test_pyrepl/test_windows_console.py @@ -399,7 +399,7 @@ def test_reset_on_restore(self): _, con = self.handle_events(events) con._WindowsConsole__vt_support = True con._WindowsConsole__original_input_mode = 0 - with patch.object(wc, "SetConsoleMode", return_value=1): + with patch.object(wc._winapi, "SetConsoleMode"): con.restore() con.out.write.assert_any_call(ANSIColors.RESET.encode(con.encoding)) diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 8d093ab1b7014a..80af93d6e45ba2 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -1443,29 +1443,23 @@ def test_after_fork(self): class CommandLineTest(unittest.TestCase): @support.force_not_colorized def test_parse_args(self): - args, help_text = random._parse_args(shlex.split("--choice a b c")) + args, parser = random._parse_args(shlex.split("--choice a b c")) self.assertEqual(args.choice, ["a", "b", "c"]) - self.assertStartsWith(help_text, "usage: ") - args, help_text = random._parse_args(shlex.split("--integer 5")) + args, parser = random._parse_args(shlex.split("--integer 5")) self.assertEqual(args.integer, 5) - self.assertStartsWith(help_text, "usage: ") - args, help_text = random._parse_args(shlex.split("--float 2.5")) + args, parser = random._parse_args(shlex.split("--float 2.5")) self.assertEqual(args.float, 2.5) - self.assertStartsWith(help_text, "usage: ") - args, help_text = random._parse_args(shlex.split("a b c")) + args, parser = random._parse_args(shlex.split("a b c")) self.assertEqual(args.input, ["a", "b", "c"]) - self.assertStartsWith(help_text, "usage: ") - args, help_text = random._parse_args(shlex.split("5")) + args, parser = random._parse_args(shlex.split("5")) self.assertEqual(args.input, ["5"]) - self.assertStartsWith(help_text, "usage: ") - args, help_text = random._parse_args(shlex.split("2.5")) + args, parser = random._parse_args(shlex.split("2.5")) self.assertEqual(args.input, ["2.5"]) - self.assertStartsWith(help_text, "usage: ") def test_main(self): for command, expected in [ @@ -1482,6 +1476,20 @@ def test_main(self): random.seed(0) self.assertEqual(random.main(shlex.split(command)), expected) + @support.force_not_colorized + def test_empty_integer_range_error(self): + # An integer input is the same as --integer, even when it is + # not positive. + for value in ["0", "-5"]: + err_msg = f"error: integer must be 1 or greater, got {value}\n" + for args in [[value], ["--integer", value]]: + with self.subTest(args=args): + with (support.captured_stderr() as stderr, + self.assertRaises(SystemExit) as cm): + random.main(args) + self.assertEqual(cm.exception.code, 2) + self.assertEndsWith(stderr.getvalue(), err_msg) + if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py index f999ea6cc1affd..074faf94d2d792 100644 --- a/Lib/test/test_readline.py +++ b/Lib/test/test_readline.py @@ -471,7 +471,7 @@ def test_write_read_limited_history(self): # So, we've only tested that the read did not fail. # See TestHistoryManipulation for the full test. - @unittest.skipUnless(sysconfig.get_config_var("HAVE_RL_CHANGE_ENVIRONMENT"), + @unittest.skipUnless(sysconfig.get_config_var("_Py_HAVE_RL_CHANGE_ENVIRONMENT"), "readline can modify the environment") def test_environment_is_not_modified(self): # os.environ contains environment at the time "os" module was loaded, so diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index e8211526ae847c..cd2cd8a21e8ea7 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -2981,6 +2981,21 @@ def test_sanitize_xml(self): self.assertEqual(sanitize_xml('valid t\xe9xt \u20ac'), 'valid t\xe9xt \u20ac') + def test_cleanup_test_name(self): + cleanup_test_name = utils.cleanup_test_name + + # Strip "test." prefix + self.assertEqual(cleanup_test_name('test.test_asyncio.test_graph'), + 'test_asyncio.test_graph') + self.assertEqual(cleanup_test_name('test.test_multiprocessing_spawn.test_manager'), + 'test_multiprocessing_spawn.test_manager') + + # No change + self.assertEqual(cleanup_test_name('test_cmath'), + 'test_cmath') + self.assertEqual(cleanup_test_name('test_asyncio.test_graph'), + 'test_asyncio.test_graph') + class GitHubActionsReporterTestCase(unittest.TestCase): def setUp(self): diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py index 9693cfc259286d..504180a282da37 100644 --- a/Lib/test/test_robotparser.py +++ b/Lib/test/test_robotparser.py @@ -875,5 +875,80 @@ def test_read_404(self): self.assertIsNone(parser.crawl_delay('*')) self.assertIsNone(parser.request_rate('*')) + +class RepeatedUserAgentTest(unittest.TestCase): + + def test_many_duplicate_user_agents(self): + # gh-162282: a User-agent repeated within one group must not be + # merged into that group once per repetition (quadratic time). + body = "User-agent: evil\n" * 1000 + "Disallow: /\n" + parser = urllib.robotparser.RobotFileParser() + parser.parse(body.splitlines()) + entry = parser.groups["evil"] + self.assertIs(entry, parser.entries[0]) + self.assertEqual(len(entry.rulelines), 1) + self.assertFalse(parser.can_fetch("evil", "/")) + + def test_many_groups_for_same_user_agent(self): + body = "User-agent: evil\nDisallow: /x\n\n" * 999 + parser = urllib.robotparser.RobotFileParser() + parser.parse(body.splitlines()) + entry = parser.groups["evil"] + self.assertEqual(len(entry.rulelines), 999) + # The merged entry is extended in place rather than copied. + parser.parse("User-agent: evil\nDisallow: /x\nCrawl-delay: 5".splitlines()) + self.assertIs(parser.groups["evil"], entry) + self.assertEqual(len(entry.rulelines), 1000) + self.assertEqual(entry.delay, 5) + # The original entries are left intact. + self.assertEqual([len(e.rulelines) for e in parser.entries], [1] * 1000) + self.assertIsNone(parser.entries[-2].delay) + + def test_merged_group_shared_by_user_agents(self): + parser = urllib.robotparser.RobotFileParser() + parser.parse("""\ +User-agent: a +User-agent: b +User-agent: c +Disallow: /x + +User-agent: a +User-agent: b +Disallow: /y + +User-agent: a +Disallow: /z +""".splitlines()) + groups = parser.groups + self.assertIs(groups["c"], parser.entries[0]) + self.assertEqual(list(map(str, groups["a"].rulelines)), + ["Disallow: /x", "Disallow: /y", "Disallow: /z"]) + self.assertEqual(list(map(str, groups["b"].rulelines)), + ["Disallow: /x", "Disallow: /y"]) + self.assertEqual(list(map(str, groups["c"].rulelines)), + ["Disallow: /x"]) + self.assertFalse(parser.can_fetch("a", "/z")) + self.assertTrue(parser.can_fetch("b", "/z")) + self.assertTrue(parser.can_fetch("c", "/y")) + + def test_many_user_agents_in_one_group(self): + body = "".join(f"User-agent: bot{i}\n" for i in range(1000)) + body += "Disallow: /\n\nUser-agent: other\nDisallow: /x\n" + parser = urllib.robotparser.RobotFileParser() + parser.parse(body.splitlines()) + self.assertIs(parser.groups["bot0"], parser.groups["bot999"]) + calls = [] + orig = urllib.robotparser.Entry.applies_to + def applies_to(entry, useragent): + calls.append(entry) + return orig(entry, useragent) + with support.swap_attr(urllib.robotparser.Entry, "applies_to", + applies_to): + self.assertTrue(parser.can_fetch("unknown", "/")) + self.assertFalse(parser.can_fetch("xbot5x", "/")) + # Each distinct entry is checked at most once per lookup. + self.assertEqual(len(calls), 2 + 1) + + if __name__=='__main__': unittest.main() diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 55b9673ef6c91c..fbe83131ff99cd 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -14,13 +14,11 @@ import warnings from test.support import ( force_not_colorized_test_class, - infinite_recursion, - no_tracing, - requires_resource, requires_subprocess, verbose, ) from test import support +from test.support import os_helper from test.support.import_helper import forget, make_legacy_pyc, unload from test.support.os_helper import create_empty_file, temp_dir, FakePath from test.support.script_helper import make_script, make_zip_script @@ -771,17 +769,17 @@ def test_zipfile_error(self): msg = "can't find '__main__' module in %r" % zip_name self._check_import_error(zip_name, msg) - @no_tracing - @requires_resource('cpu') - def test_main_recursion_error(self): + def test_main_no_recursion(self): + # gh-75221: __main__ is searched only in the executed directory or + # zipfile, so running a directory without __main__ from a zipfile's + # __main__ fails instead of recursing. with temp_dir() as script_dir, temp_dir() as dummy_dir: mod_name = '__main__' source = ("import runpy\n" "runpy.run_path(%r)\n") % dummy_dir script_name = self._make_test_script(script_dir, mod_name, source) zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) - with infinite_recursion(25): - self.assertRaises(RecursionError, run_path, zip_name) + self.assertRaises(ImportError, run_path, zip_name) def test_encoding(self): with temp_dir() as script_dir: @@ -917,5 +915,95 @@ def test_pymain_run_module(self): self.assertSigInt(["-m", ham.stem], cwd=ham.parent) +class MiscTests(unittest.TestCase): + def test_remove_sys_path0(self): + # Check that all path0 copies are removed + PATH0 = '/path/zero' + STDLIB_PATH = '/to/stdlib' + OTHER_PATH = '/other/path' + sys_path = [PATH0, STDLIB_PATH, PATH0, '', OTHER_PATH, PATH0] + expected = [STDLIB_PATH, '', OTHER_PATH] + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), + support.swap_attr(sys, '_path0', PATH0), + support.swap_attr(sys, 'path', sys_path)): + runpy._remove_sys_path0() + self.assertEqual(sys.path, expected) + + # Calling it twice does nothing + runpy._remove_sys_path0() + self.assertEqual(sys.path, expected) + + # Do nothing if sys._path0 is sys._stdlib_dir + sys_path = ['', STDLIB_PATH, OTHER_PATH] + expected = sys_path.copy() + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), + support.swap_attr(sys, '_path0', STDLIB_PATH), + support.swap_attr(sys, 'path', sys_path)): + runpy._remove_sys_path0() + self.assertEqual(sys.path, expected) + + # Do nothing if sys._path0 is None + sys_path = ['', STDLIB_PATH, OTHER_PATH] + expected = sys_path.copy() + with (support.swap_attr(sys, '_stdlib_dir', STDLIB_PATH), + support.swap_attr(sys, '_path0', None), + support.swap_attr(sys, 'path', sys_path)): + runpy._remove_sys_path0() + self.assertEqual(sys.path, expected) + + def run_python(self, *args, PYTHONPATH=None): + cmd = [sys.executable, *args] + env = support.make_clean_env() + if PYTHONPATH: + env['PYTHONPATH'] = PYTHONPATH + proc = subprocess.run(cmd, text=True, stderr=subprocess.PIPE, env=env) + self.assertEqual(proc.returncode, 0, proc.stderr) + + @support.requires_subprocess() + def test_remove_sys_path0_run_python(self): + CODE = textwrap.dedent(""" + import runpy, sys + + # Remove sys.path[0] + orig_path = sys.path.copy() + path0 = sys._path0 + runpy._remove_sys_path0() + + if path0 is not None and path0 in sys.path: + raise Exception(f"path0 {path0!r} found in sys.path {sys.path!r}") + if not sys.flags.safe_path: + if sys.path == orig_path: + raise Exception("sys.path was not changed") + else: + if sys.path != orig_path: + raise Exception("sys.path was changed") + + # Calling it twice must do nothing + clean_path = sys.path.copy() + runpy._remove_sys_path0() + if sys.path != clean_path: + raise Exception("sys.path was changed") + """) + + # Test "python -c code" + self.run_python('-c', CODE) + self.run_python('-P', '-c', CODE) + + with tempfile.TemporaryDirectory() as tmpdir: + name = 'test_remove_sys_path0' + filename = os.path.join(tmpdir, f'{name}.py') + with open(filename, 'w') as fp: + fp.write(CODE) + + # Test "python script.py" + self.run_python(filename) + self.run_python('-P', filename) + + with os_helper.change_cwd(tmpdir): + # Test "python -m module" + self.run_python('-m', name) + self.run_python('-P', '-m', name, PYTHONPATH=tmpdir) + + if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_samply_profiler.py b/Lib/test/test_samply_profiler.py index b9ac6586f4f7f5..ae3f047b15194a 100644 --- a/Lib/test/test_samply_profiler.py +++ b/Lib/test/test_samply_profiler.py @@ -1,7 +1,6 @@ import unittest import subprocess import sys -import sysconfig import os import pathlib from test import support @@ -20,14 +19,7 @@ raise unittest.SkipTest("test crash randomly on ASAN/MSAN build") -def supports_trampoline_profiling(): - perf_trampoline = sysconfig.get_config_var("PY_HAVE_PERF_TRAMPOLINE") - if not perf_trampoline: - return False - return int(perf_trampoline) == 1 - - -if not supports_trampoline_profiling(): +if not support.supports_perf_trampoline_profiling(): raise unittest.SkipTest("perf trampoline profiling not supported") diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 9bfd4bc7d63669..54d229b71def9a 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -762,6 +762,17 @@ def test_constructor_identity(self): t = self.thetype(s) self.assertEqual(id(s), id(t)) + def test_gc_tracking(self): + # gh-140232: frozensets whose elements can never be tracked by the + # GC are not tracked, including the compiled frozenset({...}) form + s = {1, 2} + self.assertFalse(gc.is_tracked(frozenset(s))) + self.assertFalse(gc.is_tracked(frozenset({1, 2}))) + self.assertFalse(gc.is_tracked(frozenset({x for x in range(2)}))) + class C: + pass + self.assertTrue(gc.is_tracked(frozenset({C()}))) + def test_hash(self): self.assertEqual(hash(self.thetype('abcdeb')), hash(self.thetype('ebecda'))) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index c0b48ea306b71e..47221d1bf46fa1 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -566,6 +566,63 @@ def onexc(*args): self.assertTrue(onexc_called) self.assertFalse(onerror_called) + @unittest.skipUnless(shutil.rmtree.avoids_symlink_attacks, + 'requires the fd based implementation of rmtree()') + def test_onexc_kwargs_dir_fd(self): + os.mkdir(TESTFN) + self.addCleanup(os_helper.rmtree, TESTFN) + top_name = os.path.join(TESTFN, 'top') + child_name = os.path.join(TESTFN, 'top', 'child') + os.mkdir(top_name) + os.mkdir(child_name) + parent_fd = os.open(TESTFN, os.O_RDONLY) + self.addCleanup(os.close, parent_fd) + + errors = [] + def onexc(func, path, err, direntry=None, dir_fd=None): + errors.append((func, path.replace(os.path.sep, '/'))) + name = path if direntry is None else direntry.name + self.assertEqual(name, os.path.basename(path)) + self.assertTrue(os.path.samestat( + os.stat(os.path.join(TESTFN, path)), + os.stat(name, dir_fd=dir_fd))) + + def failing_rmdir(path, *, dir_fd=None): + raise PermissionError(errno.EACCES, 'simulated') + with unittest.mock.patch('os.rmdir', failing_rmdir): + shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc, + _onexc_kwargs=True) + self.assertEqual( + errors, + [(failing_rmdir, 'top/child'), (failing_rmdir, 'top')]) + + child_stat = os.stat(child_name) + scandir = os.scandir + + # Fail for the top-level directory + errors.clear() + def failing_scandir(fd): + raise PermissionError(errno.EACCES, 'simulated') + with unittest.mock.patch('os.scandir', failing_scandir): + shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc, + _onexc_kwargs=True) + self.assertEqual( + errors, + [(failing_scandir, 'top'), (os.rmdir, 'top')]) + self.assertTrue(os.path.exists(top_name)) + + # Fail for the child (which is empty, so the rmdir succeeds) + errors.clear() + def failing_scandir_2(fd): + if not os.path.samestat(os.fstat(fd), child_stat): + return scandir(fd) + raise PermissionError(errno.EACCES, 'simulated') + with unittest.mock.patch('os.scandir', failing_scandir_2): + shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc, + _onexc_kwargs=True) + self.assertEqual(errors, [(failing_scandir_2, 'top/child')]) + self.assertFalse(os.path.exists(top_name)) + def test_rmtree_does_not_choke_on_failing_lstat(self): try: orig_lstat = os.lstat @@ -2614,6 +2671,10 @@ def test_basic(self): rv = shutil.which(self.file, path=self.dir) self.assertEqual(rv, self.filepath) + def test_pathlike_cmd(self): + rv = shutil.which(FakePath(self.file), path=self.dir) + self.assertEqual(rv, self.filepath) + def test_absolute_cmd(self): # When given the fully qualified path to an executable that exists, # it should be returned. diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 2cad18a69ff7c0..a8f82faee77553 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -1231,6 +1231,17 @@ def setsig(self, signum, handler): old_handler = signal.signal(signum, handler) self.addCleanup(signal.signal, signum, old_handler) + def wait_for_signals(self, sigs, expected_sigs, deadline): + # Wait for the handlers to run, so that pending signals are not + # coalesced. + # + # Signals normally arrive within microseconds, so poll with a short + # sleep rather than sleeping_retry() whose 10 ms minimum delay + # dominates the runtime (and is stretched by timer coalescing on + # macOS). + while len(sigs) < expected_sigs and time.monotonic() < deadline: + time.sleep(1e-5) + def measure_itimer_resolution(self): N = 20 times = [] @@ -1302,14 +1313,11 @@ def second_handler(signum=None, frame=None): while expected_sigs < N: os.kill(os.getpid(), signal.SIGPROF) expected_sigs += 1 - # Wait for handlers to run to avoid signal coalescing - while len(sigs) < expected_sigs and time.monotonic() < deadline: - time.sleep(1e-5) + self.wait_for_signals(sigs, expected_sigs, deadline) os.kill(os.getpid(), signal.SIGUSR1) expected_sigs += 1 - while len(sigs) < expected_sigs and time.monotonic() < deadline: - time.sleep(1e-5) + self.wait_for_signals(sigs, expected_sigs, deadline) # All ITIMER_REAL signals should have been delivered to the # Python handler @@ -1334,6 +1342,8 @@ def handler(signum, frame): self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL expected_sigs = 0 + deadline = time.monotonic() + support.SHORT_TIMEOUT + while expected_sigs < N: # Hopefully the SIGALRM will be received somewhere during # initial processing of SIGUSR2. @@ -1341,10 +1351,7 @@ def handler(signum, frame): os.kill(os.getpid(), signal.SIGUSR2) expected_sigs += 2 - # Wait for handlers to run to avoid signal coalescing - for _ in support.sleeping_retry(support.SHORT_TIMEOUT): - if len(sigs) >= expected_sigs: - break + self.wait_for_signals(sigs, expected_sigs, deadline) # All ITIMER_REAL signals should have been delivered to the # Python handler diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 5b4e4f08060772..837445dc06d67b 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -40,6 +40,10 @@ import ctypes except ImportError: ctypes = None +try: + from _hashlib import get_fips_mode +except ImportError: + def get_fips_mode(): return False ssl = import_helper.import_module("ssl") @@ -2478,6 +2482,9 @@ def test_outer_alpn_does_not_replace_inner(self): self.assertEqual(server.selected_alpn_protocol(), 'h2') @unittest.skipUnless(ssl.HAS_ECH, 'requires ECH') + # At 2026-11-13 (OpenSSL 4.0.3 on Fedora 45), the test is incompatible + # with FIPS mode: https://github.com/openssl/openssl/issues/33724 + @unittest.skipIf(get_fips_mode(), 'gh-161932: test incompatible with FIPS') def test_outer_alpn_without_ech_config(self): for options in (0, ssl.OP_ECH_GREASE): client_ctx, server_ctx, hostname = testing_context() @@ -2491,6 +2498,9 @@ def test_outer_alpn_without_ech_config(self): self.assertEqual(server.selected_alpn_protocol(), 'h2') @unittest.skipUnless(ssl.HAS_ECH, 'requires ECH') + # At 2026-11-13 (OpenSSL 4.0.3 on Fedora 45), the test is incompatible + # with FIPS mode: https://github.com/openssl/openssl/issues/33724 + @unittest.skipIf(get_fips_mode(), 'gh-161932: test incompatible with FIPS') def test_grease(self): client_ctx, server_ctx, hostname = testing_context() client_ctx.options |= ssl.OP_ECH_GREASE @@ -3112,6 +3122,10 @@ def test_get_server_certificate_fail(self): # independent test method _test_get_server_certificate_fail(self, *self.server_addr) + def test_get_server_certificate_fail_by_hostname(self): + _, port = self.server_addr + _test_get_server_certificate_fail(self, socket_helper.HOSTv4, port, SIGNING_CA) + def test_get_server_certificate_timeout(self): def servername_cb(ssl_sock, server_name, initial_context): time.sleep(0.2) @@ -3364,10 +3378,10 @@ def _test_get_server_certificate(test, host, port, cert=None): if support.verbose: sys.stdout.write("\nVerified certificate for %s:%s is\n%s\n" % (host, port ,pem)) -def _test_get_server_certificate_fail(test, host, port): +def _test_get_server_certificate_fail(test, host, port, ca_certs=CERTFILE): with warnings_helper.check_no_resource_warning(test): try: - pem = ssl.get_server_certificate((host, port), ca_certs=CERTFILE) + pem = ssl.get_server_certificate((host, port), ca_certs=ca_certs) except ssl.SSLError as x: #should fail if support.verbose: @@ -4213,8 +4227,6 @@ def test_ecc_cert(self): s.connect((HOST, server.port)) cert = s.getpeercert() self.assertTrue(cert, "Can't get peer certificate.") - cipher = s.cipher()[0].split('-') - self.assertTrue(cipher[:2], ('ECDHE', 'ECDSA')) @unittest.skipUnless(IS_OPENSSL_3_0_0, "test requires RFC 5280 check added in OpenSSL 3.0+") @@ -4272,7 +4284,7 @@ def test_dual_rsa_ecc(self): cert = s.getpeercert() self.assertTrue(cert, "Can't get peer certificate.") cipher = s.cipher()[0].split('-') - self.assertTrue(cipher[:2], ('ECDHE', 'ECDSA')) + self.assertEqual(cipher[:2], ['ECDHE', 'ECDSA']) def test_check_hostname_idn(self, warnings_filters=True): if support.verbose: diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 700c5ac304f717..266d92828fe154 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2790,6 +2790,66 @@ def test_sqrtprod_helper_function_fundamentals(self): self.assertEqual(actual, expected) self.assertEqual(sign(actual), sign(expected)) + @requires_IEEE_754 + def test_sqrtprod_helper_function_subnormal_products(self): + # gh-161259: accuracy was lost when the product or the correction + # term was subnormal. The reference scales the inputs by an exact + # power of two so that the product is a normal float. + scale = 2.0 ** 600 + for x, y in [ + (2e-200, 2e-124), + (1e-160, 3e-150), + (3e-170, 7e-140), + (1e-160, 1e-135), + ]: + expected = math.sqrt((scale * x) * (scale * y)) / scale + actual = statistics._sqrtprod(x, y) + with self.subTest(x=x, y=y, expected=expected, actual=actual): + self.assertAlmostEqual(actual / expected, 1.0, delta=2**-50) + + @requires_IEEE_754 + def test_sqrtprod_helper_function_subnormal_boundary(self): + # sqrt(3 * 7) * 2**e straddles the 2**-485 cutoff where the + # correction term can become subnormal. + scale = 2.0 ** 600 + for e in (-488, -487): + x = 3.0 * 2.0 ** e + y = 7.0 * 2.0 ** e + expected = math.sqrt((scale * x) * (scale * y)) / scale + actual = statistics._sqrtprod(x, y) + with self.subTest(e=e, expected=expected, actual=actual): + self.assertAlmostEqual(actual / expected, 1.0, delta=2**-50) + + cutoff = 2.0 ** -485 + self.assertEqual(statistics._sqrtprod(cutoff, cutoff), cutoff) + + @requires_IEEE_754 + def test_correlation_tiny_inputs(self): + # gh-161259: correlation() is scale-invariant, even when the + # intermediate sums of squares are tiny. + x = [1e-100, 2e-100, 3e-100] + y = [1e-62, 2e-62, 3e-62] + self.assertAlmostEqual(statistics.correlation(x, y), 1.0) + + @requires_IEEE_754 + def test_correlation_asymmetric_scales(self): + for a, b in [ + (2.0 ** 50, 2.0 ** -537), + (1.3 * 2.0 ** -260, 2.7 * 2.0 ** -270), + ]: + x = [-a, 0.0, a] + y = [0.0, -b, b] + with self.subTest(a=a, b=b): + self.assertAlmostEqual(statistics.correlation(x, y), 0.5, + delta=2**-50) + + @requires_IEEE_754 + def test_correlation_tiny_products_max_recursion(self): + # Make both sums of squares round to the smallest subnormal. + a = 2.0 ** -537 + x = [0.0, 0.0, 0.0, a] + self.assertEqual(statistics.correlation(x, x), 1.0) + @requires_IEEE_754 @skip_if_double_rounding @support.cpython_only # Allow for a weaker sumprod() implementation diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index bef0e62fb42bcc..5e553094cac107 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -267,6 +267,30 @@ class MyStr(str): pass self.checkequal(3, MyStr('aaa'), 'count', 'a') + def test_find_adaptive_widths(self): + # The adaptive path in Objects/stringlib/fastsearch.h needs a haystack + # of at least 2500 characters and a needle of at least a third of it, + # and it is compiled once per character width. Exercise all four. + cases = [ + ('ascii', 'a', 'b', 'c'), + ('latin1', '\xe9', '\xea', '\xeb'), + ('ucs2', '\u0100', '\u0101', '\u0102'), + ('ucs4', '\U0001d11e', '\U0001d11f', '\U0001d120'), + ] + for kind, c, d, x in cases: + # A needle of 2000 characters, none of which is x. + needle = c * 1000 + d * 1000 + with self.subTest(kind=kind): + # 4000 characters, mostly x: the skip branches jump by m. + haystack = c * 1000 + x * 3000 + self.assertEqual(haystack.find(needle), -1) + self.assertEqual(haystack.count(needle), 0) + # A match at 3000 in a haystack of 5000, so count() reaches + # the match branch inside adaptive_find rather than two-way. + haystack = c * 1000 + x * 2000 + needle + self.assertEqual(haystack.find(needle), 3000) + self.assertEqual(haystack.count(needle), 1) + def test_find(self): string_tests.StringLikeTest.test_find(self) # test implementation details of the memchr fast path @@ -566,6 +590,18 @@ def __str__(self): return self.sval self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd')) self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz')) + # mixed kinds of the separator and the items + self.checkequalnofix('a\xe9b', '\xe9', 'join', ['a', 'b']) + self.checkequalnofix('a\u20acb', '\u20ac', 'join', ['a', 'b']) + self.checkequalnofix('a\xe9', '', 'join', ['a', '\xe9']) + self.checkequalnofix('\u20ac\xe9', '', 'join', ['\u20ac', '\xe9']) + self.checkequalnofix('\xe9-\u20ac-\U0001f600', '-', 'join', + ['\xe9', '\u20ac', '\U0001f600']) + self.checkequalnofix('\U0001f600-a', '-', 'join', ['\U0001f600', 'a']) + # a single item which is a str subclass is copied + class S(str): pass + for s in 'a', '\xe9', '\u20ac', '\U0001f600': + self.checkequalnofix(s, '-', 'join', [S(s)]) self.checkraises(TypeError, ' ', 'join', ['1', '2', MyWrapper('foo')]) self.checkraises(TypeError, ' ', 'join', ['1', '2', '3', bytes()]) self.checkraises(TypeError, ' ', 'join', [1, 2, 3]) @@ -2605,7 +2641,7 @@ def test_compare(self): self.assertFalse(copy1 != copy2) self.assertTrue(copy1 <= copy2) - self.assertTrue(copy2 >= copy2) + self.assertTrue(copy1 >= copy2) self.assertTrue(ascii < ascii2) self.assertTrue(ascii < latin) diff --git a/Lib/test/test_stringprep.py b/Lib/test/test_stringprep.py index d4b4a13d0de264..a4ddc8b33e2570 100644 --- a/Lib/test/test_stringprep.py +++ b/Lib/test/test_stringprep.py @@ -13,11 +13,11 @@ def test(self): self.assertTrue(in_table_b1("\u00ad")) self.assertFalse(in_table_b1("\u00ae")) - self.assertTrue(map_table_b2("\u0041"), "\u0061") - self.assertTrue(map_table_b2("\u0061"), "\u0061") + self.assertEqual(map_table_b2("\u0041"), "\u0061") + self.assertEqual(map_table_b2("\u0061"), "\u0061") - self.assertTrue(map_table_b3("\u0041"), "\u0061") - self.assertTrue(map_table_b3("\u0061"), "\u0061") + self.assertEqual(map_table_b3("\u0041"), "\u0061") + self.assertEqual(map_table_b3("\u0061"), "\u0061") self.assertTrue(in_table_c11("\u0020")) self.assertFalse(in_table_c11("\u0021")) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index dedd3cc0e9e867..23359d3147ade3 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -3590,6 +3590,74 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self): proc.communicate(timeout=999) mock_proc_stdin.close.assert_called_once_with() + def _spuriously_writable_selector(self): + # A selector that reports stdin as writable whenever it is + # registered. poll() on macOS and FreeBSD can do this for a full + # pipe when many pipes are in use system-wide (gh-162016). + class AlwaysWritableSelector(subprocess._PopenSelector): + spurious_events = 0 + + def select(self, timeout=None): + ready = super().select(0) + writable = {key.fd for key, events in ready + if events & selectors.EVENT_WRITE} + for key in self.get_map().values(): + if (key.events & selectors.EVENT_WRITE + and key.fd not in writable): + ready.append((key, selectors.EVENT_WRITE)) + type(self).spurious_events += 1 + return ready or super().select(timeout) + + return AlwaysWritableSelector + + def test_communicate_timeout_stdin_spuriously_writable(self): + # The child never reads stdin, so the pipe fills up. communicate() + # must neither block in write() past its timeout nor spin on a pipe + # that keeps being reported as writable. + selector = self._spuriously_writable_selector() + sleep = support.SHORT_TIMEOUT + with mock.patch.object(subprocess, '_PopenSelector', selector), \ + subprocess.Popen([sys.executable, '-c', + f'import time; time.sleep({sleep})'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE) as proc: + try: + start = time.monotonic() + with self.assertRaises(subprocess.TimeoutExpired): + proc.communicate(b'x' * support.PIPE_MAX_SIZE, + timeout=0.5) + self.assertLess(time.monotonic() - start, sleep) + # communicate() must wait between attempts to write to a + # full pipe. Retrying in a busy loop would produce many + # thousands of these events within the timeout. + self.assertLess(selector.spurious_events, 100) + self.assertTrue(os.get_blocking(proc.stdin.fileno())) + finally: + proc.kill() + + def test_communicate_stdin_spuriously_writable_last_pipe(self): + # All input must still arrive when stdin is full, is reported as + # writable, and is the only pipe communicate() has left to service. + selector = self._spuriously_writable_selector() + data = bytes(range(256)) * (support.PIPE_MAX_SIZE // 256) + code = textwrap.dedent(f""" + import os, sys, time + os.close(1) + time.sleep(0.1) # Let the parent fill the stdin pipe. + data = bytes(range(256)) * {len(data) // 256} + sys.exit(sys.stdin.buffer.read() != data) + """) + with mock.patch.object(subprocess, '_PopenSelector', selector), \ + subprocess.Popen([sys.executable, '-c', code], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE) as proc: + try: + proc.communicate(data, timeout=support.SHORT_TIMEOUT) + self.assertEqual(proc.returncode, 0) + self.assertGreater(selector.spurious_events, 0) + finally: + proc.kill() + @unittest.skipUnless(_testcapi is not None and hasattr(_testcapi, 'W_STOPCODE'), 'need _testcapi.W_STOPCODE') diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 201c209d821415..3b65bf5128cdbf 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1493,6 +1493,15 @@ class PythonType: self.assertFalse(is_immortal(b'abc'.decode())) self.assertFalse(is_immortal('abc'.encode())) + def test_path(self): + self.assertIsInstance(sys.path, list) + for path in sys.path: + self.assertIsInstance(path, str) + + def test_path0(self): + self.assertTrue(sys._path0 is None or isinstance(sys._path0, str), + sys._path0) + @test.support.cpython_only @test.support.force_not_colorized_test_class diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 2ad6d0452ce29a..1927ff227d5356 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1061,6 +1061,44 @@ def test_compare_members(self): finally: tar1.close() + def test_ignore_zeros_concatenated(self): + # gh-159643: reading past the end of the compressed data + # must not raise EOFError. + with self.open(tmpname, "wb") as fobj: + for name in ("a", "b"): + with tarfile.open(fileobj=fobj, mode="w") as tar: + tar.addfile(tarfile.TarInfo(name)) + with tarfile.open(tmpname, self.mode, ignore_zeros=True) as tar: + self.assertEqual(tar.getnames(), ["a", "b"]) + + def test_no_end_of_archive_blocks(self): + # gh-159643 + tarinfo = tarfile.TarInfo("a") + tarinfo.size = 5 + with self.open(tmpname, "wb") as fobj: + fobj.write(tarinfo.tobuf()) + fobj.write(b"hello".ljust(tarfile.BLOCKSIZE, b"\0")) + with tarfile.open(tmpname, self.mode) as tar: + self.assertEqual(tar.getnames(), ["a"]) + + def test_data_after_end_of_stream(self): + # gh-159643: with a small bufsize, the compressed data spans + # several reads of the file and the padding that follows it + # is not read together with the end of the compressed data. + bufsize = tarfile.BLOCKSIZE + data = Random(0).randbytes(8 * bufsize) + tarinfo = tarfile.TarInfo("a") + tarinfo.size = len(data) + with self.open(tmpname, "wb") as fobj: + with tarfile.open(fileobj=fobj, mode="w") as tar: + tar.addfile(tarinfo, io.BytesIO(data)) + with open(tmpname, "ab") as fobj: + fobj.write(bytes(4 * bufsize)) + with tarfile.open(tmpname, self.mode, bufsize=bufsize, + ignore_zeros=True) as tar: + members = [(m.name, tar.extractfile(m).read()) for m in tar] + self.assertEqual(members, [("a", data)]) + class GzipStreamReadTest(GzipTest, GzipReadTestBase, StreamReadTest): pass diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py index 1d90ac10ad77ad..46a18f727e40d2 100644 --- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tcl.py @@ -2,9 +2,12 @@ import subprocess import sys import os +import threading from test import support from test.support import import_helper from test.support import os_helper +from test.support import threading_helper +from test.support.isolation import runInSubprocess # Skip this test if the _tkinter module wasn't built. _tkinter = import_helper.import_module('_tkinter') @@ -33,6 +36,29 @@ def testFlattenLen(self): self.assertRaises(TypeError, _tkinter._flatten, 'string') self.assertRaises(TypeError, _tkinter._flatten, {'set'}) + @unittest.skipUnless(support.Py_GIL_DISABLED, + 'requires the free-threaded build') + @threading_helper.requires_working_threading() + @runInSubprocess() + def test_create_concurrent(self): + # gh-154923: Create Tcl interpreters in different threads at once. + # Run in a fresh process, where no interpreter was created yet. + nthreads = 16 + start = threading.Barrier(nthreads) + errors = [] + def worker(): + try: + start.wait() + for _ in range(8): + _tkinter.create(None, '', 'Tk', False, 1, False, False, + None) + except Exception as e: + errors.append(e) + threads = [threading.Thread(target=worker) for _ in range(nthreads)] + with threading_helper.start_threads(threads): + pass + self.assertEqual(errors, []) + class TclTest(unittest.TestCase): @@ -595,6 +621,34 @@ def test_returning_deeply_nested(self): interp.eval('set x a; for {set i 0} {$i < %d} {incr i} {set x [list $x]}' % support.exceeds_recursion_limit()) self.assertRaises(RecursionError, interp.getvar, 'x') + interp = None + + # gh-161353: Clean up explicitly per-thread state to avoid leaking an + # event handle on Windows when test_tcl is run with -R 3:3 option. + _tkinter._finalize_thread() + + def test_setvar_evil(self): + # gh-161388 + class StrRaises: + def __str__(self): + raise RuntimeError("setvar evil") + + value = StrRaises() + tk = self.interp.tk + + with self.assertRaisesRegex(RuntimeError, "setvar evil"): + tk.setvar("local_name1", value) + self.assertRaises(TclError, tk.getvar, "local_name1") + with self.assertRaisesRegex(RuntimeError, "setvar evil"): + tk.setvar("local_name2", "local_name3", value) + self.assertRaises(TclError, tk.getvar, "local_name2", "local_name3") + + with self.assertRaisesRegex(RuntimeError, "setvar evil"): + tk.globalsetvar("global_name1", value) + self.assertRaises(TclError, tk.globalgetvar, "global_name1") + with self.assertRaisesRegex(RuntimeError, "setvar evil"): + tk.globalsetvar("global_name2", "global_name3", value) + self.assertRaises(TclError, tk.globalgetvar, "global_name2", "global_name3") def test_user_command(self): result = None diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 2f7b8798daa8c9..3e87ccfc436d12 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1653,10 +1653,14 @@ def __exit__(self, *exc_info): class TestTemporaryDirectory(BaseTestCase): """Test TemporaryDirectory().""" - def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1, + def do_create(self, dir=None, pre=None, suf=None, recurse=1, dirs=1, files=1, ignore_cleanup_errors=False): if dir is None: dir = tempfile.gettempdir() + if pre is None: + pre = type(dir)() + if suf is None: + suf = type(dir)() tmp = tempfile.TemporaryDirectory( dir=dir, prefix=pre, suffix=suf, ignore_cleanup_errors=ignore_cleanup_errors) @@ -1666,13 +1670,17 @@ def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1, def do_create2(self, path, recurse=1, dirs=1, files=1): # Create subdirectories and some files + def join(name): + if isinstance(path, bytes): + name = os.fsencode(name) + return os.path.join(path, name) if recurse: for i in range(dirs): - name = os.path.join(path, "dir%d" % i) + name = join("dir%d" % i) os.mkdir(name) self.do_create2(name, recurse-1, dirs, files) for i in range(files): - with open(os.path.join(path, "test%d.txt" % i), "wb") as f: + with open(join("test%d.txt" % i), "wb") as f: f.write(b"Hello world!") def test_mkdtemp_failure(self): @@ -1698,6 +1706,15 @@ def test_explicit_cleanup(self): finally: os.rmdir(dir) + def test_explicit_cleanup_bytes(self): + # A TemporaryDirectory with a bytes name is deleted when cleaned up + with tempfile.TemporaryDirectory() as working_dir: + d = self.do_create(dir=os.fsencode(working_dir), recurse=2) + self.assertIsInstance(d.name, bytes) + self.assertTrue(os.path.exists(d.name)) + d.cleanup() + self.assertFalse(os.path.exists(d.name)) + def test_explicit_cleanup_ignore_errors(self): """Test that cleanup doesn't return an error when ignoring them.""" with tempfile.TemporaryDirectory() as working_dir: @@ -1910,6 +1927,87 @@ def hook(path, *, dir_fd=None): self.assertTrue(os.path.exists(target_file)) self.assertEqual(os.stat(target_file).st_mode, target_mode) + def _make_unopenable_subdir(self): + # Create the following structure: + # work/ - top-level + # / - tempdir to clean up (random name) + # sub/ - an unopenable dir + # subsub/ + # file + # Return: + # - `work` name (str) + # - `d` (TemporaryDirectory) + # - `subsub` stat result + work = self.enterContext(os_helper.temp_dir()) + d = tempfile.TemporaryDirectory(dir=work) + sub = os.path.join(d.name, 'sub') + subsub = os.path.join(sub, 'subsub') + os.mkdir(sub) + os.mkdir(subsub) + subsub_stat = os.lstat(subsub) + with open(os.path.join(subsub, 'file'), 'w') as f: + f.write("rm me pls") + os.chmod(sub, 0) + # If the test fails, make sure everything can still be removed. + self.addCleanup(d.cleanup) + @self.addCleanup + def _clean(): + if os.path.isdir(sub): + os.chmod(sub, 0o700) + return work, d, subsub_stat + + @os_helper.skip_unless_working_chmod + @support.requires_non_root_user + @unittest.skipUnless(tempfile._rmtree_use_dir_fd + and shutil.rmtree.avoids_symlink_attacks, + 'requires the fd based cleanup') + def test_cleanup_nested_toplevel_error(self): + # rmtree() reports errors relative to the correct parent directory + work, d, subsub_stat = self._make_unopenable_subdir() + # An unrelated directory with the same name; should not be touched + unrelated = os.path.join(work, 'sub') + os.mkdir(unrelated) + + rmdir = os.rmdir + failed_path = None + def failing_rmdir(path, *, dir_fd=None): + nonlocal failed_path + if path == 'sub' and dir_fd is not None and failed_path is None: + failed_path = path + raise PermissionError(errno.EPERM, 'simulated', path) + return rmdir(path, dir_fd=dir_fd) + with os_helper.change_cwd(work), mock.patch('os.rmdir', failing_rmdir): + d.cleanup() + self.assertEqual(failed_path, 'sub') + self.assertFalse(os.path.exists(d.name)) + self.assertTrue(os.path.exists(unrelated)) + + @os_helper.skip_unless_working_chmod + @support.requires_non_root_user + @unittest.skipUnless(tempfile._rmtree_use_dir_fd + and shutil.rmtree.avoids_symlink_attacks, + 'requires the fd based cleanup') + def test_cleanup_nested_scandir_error(self): + # A scandir() error is reported relative to the parent directory. + work, d, subsub_stat = self._make_unopenable_subdir() + + scandir = os.scandir + failed = False + def failing_scandir(path='.'): + nonlocal failed + if ( + not failed + and isinstance(path, int) + and os.path.samestat(os.fstat(path), subsub_stat) + ): + failed = True + raise PermissionError(errno.EACCES, 'simulated') + return scandir(path) + with os_helper.change_cwd(work), mock.patch('os.scandir', failing_scandir): + d.cleanup() + self.assertTrue(failed) + self.assertFalse(os.path.exists(d.name)) + @support.cpython_only def test_del_on_collection(self): # A TemporaryDirectory is deleted when garbage collected @@ -2068,11 +2166,17 @@ def test_context_manager(self): self.assertEqual(name, d.name) self.assertFalse(os.path.exists(name)) - def test_modes(self): + @support.subTests('bytes_name', (False, True)) + def test_modes(self, bytes_name): for mode in range(8): mode <<= 6 with self.subTest(mode=format(mode, '03o')): - d = self.do_create(recurse=3, dirs=2, files=2) + if bytes_name: + d = self.do_create(recurse=3, dirs=2, files=2, + dir=tempfile.gettempdirb()) + else: + d = self.do_create(recurse=3, dirs=2, files=2) + self.assertIsInstance(d.name, bytes if bytes_name else str) with d: # Change files and directories mode recursively. for root, dirs, files in os.walk(d.name, topdown=False): @@ -2083,13 +2187,17 @@ def test_modes(self): self.assertFalse(os.path.exists(d.name)) @support.subTests('ignore_errors', (True, False)) - def test_parent_mode_preserved(self, ignore_errors): + @support.subTests('bytes_name', (False, True)) + def test_parent_mode_preserved(self, ignore_errors, bytes_name): # Test that cleanup does not touch the parent directory, # even if that prevents removal. for mode in range(8): mode <<= 6 with self.subTest(mode=format(mode, '03o')): - outer = self.do_create() + if bytes_name: + outer = self.do_create(dir=tempfile.gettempdirb()) + else: + outer = self.do_create() with outer: d = self.do_create(dir=outer.name, dirs=2, files=2, ignore_cleanup_errors=ignore_errors) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index dac4dd9060ccdd..861076357e0019 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -21,10 +21,11 @@ from test.support import skip_if_buggy_ucrt_strfptime, SuppressCrashReport -# Max year is only limited by the size of C int. +# Years are limited by the range of struct tm's int tm_year field. SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4 -TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1 -TIME_MINYEAR = -TIME_MAXYEAR - 1 + 1900 +C_INT_MAX = (1 << 8 * SIZEOF_INT - 1) - 1 +TIME_MAXYEAR = C_INT_MAX + 1900 +TIME_MINYEAR = -C_INT_MAX - 1 + 1900 SEC_TO_US = 10 ** 6 US_TO_NS = 10 ** 3 @@ -153,6 +154,23 @@ def test_clock_settime(self): self.assertRaises(OSError, time.clock_settime, time.CLOCK_MONOTONIC, 0) + def test_large_timestamp_year(self): + # 2147483648-06-29 00:00:00 UTC, away from local year boundaries. + timestamp = 67_767_976_249_084_800 + for convert in (time.gmtime, time.localtime, time.ctime): + with self.subTest(convert=convert): + try: + result = convert(timestamp) + except (OverflowError, OSError): + self.skipTest('timestamp out of range on this platform') + if isinstance(result, str): + year = int(result.rsplit(' ', 1)[-1]) + else: + year = result.tm_year + self.assertEqual(year, 2**31) + if convert is time.localtime: + self.assertEqual(time.asctime(result), time.ctime(timestamp)) + def test_conversions(self): self.assertEqual(time.ctime(self.t), time.asctime(time.localtime(self.t))) @@ -456,7 +474,7 @@ def test_strptime_leap_year(self): def test_asctime(self): time.asctime(time.gmtime(self.t)) - # Max year is only limited by the size of C int. + # The year offset must fit into a C int. for bigyear in TIME_MAXYEAR, TIME_MINYEAR: asc = time.asctime((bigyear, 6, 1) + (0,) * 6) self.assertEqual(asc[-len(str(bigyear)):], str(bigyear)) @@ -544,8 +562,8 @@ def test_tzset(self): # on some operating systems (e.g. FreeBSD), which is wrong. See for # example this bug: # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810 - self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0]) - self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1])) + self.assertIn(time.tzname[0], ('AEST', 'EST')) + self.assertIn(time.tzname[1], ('AEDT', 'EDT')) self.assertEqual(len(time.tzname), 2) self.assertEqual(time.daylight, 1) self.assertEqual(time.timezone, -36000) @@ -827,7 +845,7 @@ def test_year(self, fmt=None, func=None): def test_large_year(self): self.assertEqual(self.yearstr(12345).lstrip('+'), '12345') self.assertEqual(self.yearstr(123456789).lstrip('+'), '123456789') - self.assertEqual(self.yearstr(TIME_MAXYEAR).lstrip('+'), str(TIME_MAXYEAR)) + self.assertEqual(self.yearstr(C_INT_MAX).lstrip('+'), str(C_INT_MAX)) self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1) def test_negative(self): diff --git a/Lib/test/test_tkinter/test_font.py b/Lib/test/test_tkinter/test_font.py index c80098271039d1..4217bed966131b 100644 --- a/Lib/test/test_tkinter/test_font.py +++ b/Lib/test/test_tkinter/test_font.py @@ -250,11 +250,6 @@ def test_equality(self): self.assertEqual(w1, w2) w3 = font.Font(root=self.root, font=('Times', 12), exists=True) self.assertNotEqual(w1, w3) - # A wrapped description never equals a named font, even one whose name - # is the string form of the description. - named = font.Font(root=self.root, name=str(w1), family='Courier') - self.assertNotEqual(w1, named) - self.assertNotEqual(named, w1) self.assertNotEqual(font1, 0) self.assertEqual(font1, ALWAYS_EQ) diff --git a/Lib/test/test_tkinter/test_geometry_managers.py b/Lib/test/test_tkinter/test_geometry_managers.py index e98a6af060e80b..1823d7a58ad5ba 100644 --- a/Lib/test/test_tkinter/test_geometry_managers.py +++ b/Lib/test/test_tkinter/test_geometry_managers.py @@ -5,7 +5,7 @@ from test.support import requires from test.test_tkinter.support import setUpModule # noqa: F401 -from test.test_tkinter.support import pixels_conv +from test.test_tkinter.support import pixels_conv, get_tk_patchlevel from test.test_tkinter.widget_tests import AbstractWidgetTest requires('gui') @@ -233,8 +233,8 @@ def test_pack_forget(self): def test_pack_info(self): pack, a, b, c, d = self.create2() - with self.assertRaisesRegex(TclError, 'window "%s" isn\'t packed' % a): - a.pack_info() + if get_tk_patchlevel(self.root) >= (9, 1, 1): + self.assertEqual(a.pack_info(), {}) a.pack_configure() b.pack_configure(side='right', in_=a, anchor='s', expand=True, fill='x', ipadx=5, padx=10, ipady=2, pady=(5, 15)) diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py index 59769b7250d76a..7978a8d5e3eeff 100644 --- a/Lib/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_misc.py @@ -6,6 +6,7 @@ import signal import sys import textwrap +import threading import time import unittest import weakref @@ -14,7 +15,9 @@ from tkinter import TclError, ttk import enum from test import support +from test.support import import_helper from test.support import os_helper +from test.support import threading_helper from test.support.isolation import runInSubprocess from test.support.script_helper import assert_python_ok from test.test_tkinter.support import setUpModule # noqa: F401 @@ -712,6 +715,109 @@ def test_tk_print(self): self.assertNotHasAttr(tkinter.Frame, 'tk_print') self.assertNotHasAttr(tkinter.Misc, 'tk_print') + @requires_tk(9, 1) + def test_accessible(self): + # Without an active screen reader every "tk accessible" call is + # a no-op returning 0, so only test that the calls do not raise + # and check the values when a screen reader is active. + try: + sr = self.root.tk_check_screenreader() + except tkinter.TclError: + # Tk 9.1a0 or Tk compiled without accessibility support + # (e.g. without ATK on X11). + self.skipTest('the "tk accessible" command is not supported') + self.assertIsInstance(sr, bool) + f = tkinter.Frame(self.root) + acc = f.accessible + self.assertIsInstance(acc, tkinter.Accessible) + self.assertIs(f.accessible, acc) + # The role has to be set first, it creates the table in which + # other attributes are stored. + acc.role = 'Label' + result = acc.role + if sr: + self.assertEqual(result, 'Label') + # Reading an attribute that has not been set raises TclError. + with self.assertRaises(tkinter.TclError): + acc.help + for attr in ('name', 'description', 'value', 'state', + 'action', 'help'): + with self.subTest(attr=attr): + setattr(acc, attr, 'spam') + result = getattr(acc, attr) + if sr: + self.assertEqual(result, 'spam') + acc.add_object() + acc.emit_selection_change() + if self.root._windowingsystem != 'aqua': + acc.emit_focus_change() + + @unittest.skipUnless(sys.platform == 'win32', 'requires Windows') + def test_callback_from_foreign_event_loop(self): + # gh-84256: Tk can call an event handler when Tcl was not entered + # by tkinter, for example when a window message is dispatched + # from a foreign event loop while the GIL is released. + ctypes = import_helper.import_module('ctypes') + from ctypes import wintypes + user32 = ctypes.WinDLL('user32') + user32.SendMessageW.argtypes = [wintypes.HWND, wintypes.UINT, + wintypes.WPARAM, wintypes.LPARAM] + user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND, + ctypes.c_int, ctypes.c_int, + ctypes.c_int, ctypes.c_int, + wintypes.UINT] + WM_ENTERSIZEMOVE = 0x0231 + WM_EXITSIZEMOVE = 0x0232 + SWP_NOMOVE = 0x0002 + SWP_NOZORDER = 0x0004 + SWP_NOACTIVATE = 0x0010 + + top = tkinter.Toplevel(self.root) + top.geometry('200x150') + if not wait_until_mapped(top): + self.skipTest('the window is not mapped') + sizes = [] + def callback(event): + # Call Tcl from the handler. + sizes.append((event.width, event.height, top.winfo_width())) + top.bind('', callback) + hwnd = int(top.wm_frame(), 16) + # Emulate resizing the window with the mouse: Tk handles the + # resulting window messages in its window procedure, outside + # of the Tcl event loop run by tkinter. + user32.SendMessageW(hwnd, WM_ENTERSIZEMOVE, 0, 0) + try: + user32.SetWindowPos(hwnd, None, 0, 0, 250, 180, + SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE) + finally: + user32.SendMessageW(hwnd, WM_EXITSIZEMOVE, 0, 0) + self.assertTrue(sizes) + + def test_nested_mainloop(self): + # gh-162650: Calls from other thread are still dispatched after + # a nested mainloop() exits. + root = self.root + results = [] + def call(): + try: + results.append(root.tk.call('expr', '6*7')) + except RuntimeError as e: + results.append(e) + def start(): + root.after(0, root.quit) + root.mainloop() # nested + thread = threading.Thread(target=call) + thread.start() + def wait(): + if thread.is_alive(): + root.after(10, wait) + else: + root.quit() + wait() + root.after(0, start) + root.mainloop() + self.assertEqual(results, [42]) + def test_wait_variable(self): var = tkinter.StringVar(self.root) self.assertEqual(self.root.waitvar, self.root.wait_variable) @@ -900,6 +1006,91 @@ def test_iterable_protocol(self): widget in widget +@unittest.skipIf(sys.platform == 'darwin', + 'Tk only works in the main thread on macOS') +@threading_helper.requires_working_threading() +class MainloopThreadsTest(unittest.TestCase): + # gh-162647: The mainloop() of Tcl interpreters in different threads + # should not affect each other. + + def setUp(self): + # Tk objects created in other threads must not become the default + # root. + self.addCleanup(setattr, tkinter, '_support_default_root', + tkinter._support_default_root) + tkinter._support_default_root = False + self.roots = {} + self.results = [] + + def mainloop_thread(self, name, ticking=False): + # Return a thread which runs mainloop() of a new Tk. + def run(): + root = tkinter.Tk() + root.withdraw() + if ticking: + def tick(): + root.after(20, tick) + tick() + self.roots[name] = root + try: + root.mainloop() + self.results.append((name, None)) + except BaseException as e: + self.results.append((name, e)) + finally: + # The interpreter should be deleted in its thread. + del self.roots[name] + root.destroy() + del root + _tkinter._finalize_thread() + return threading.Thread(target=run) + + def wait_mainloop(self, name): + for _ in support.sleeping_retry(support.SHORT_TIMEOUT): + if name in self.roots: + break + # A call from other thread waits until mainloop() runs. + self.roots[name].tk.call('info', 'patchlevel') + + def quit_all(self): + for root in list(self.roots.values()): + root.quit() + + def test_quit(self): + threads = [self.mainloop_thread('a'), + self.mainloop_thread('b', ticking=True)] + with threading_helper.start_threads(threads, unlock=self.quit_all): + self.wait_mainloop('a') + self.wait_mainloop('b') + # quit() stops the mainloop() of its interpreter even if it + # waits for events, and does not stop other mainloop(). + self.roots['a'].quit() + threads[0].join(support.SHORT_TIMEOUT) + self.assertEqual(self.results, [('a', None)]) + self.assertEqual(self.results, [('a', None), ('b', None)]) + + def test_callback_error(self): + threads = [self.mainloop_thread('b', ticking=True)] + with threading_helper.start_threads(threads, unlock=self.quit_all): + self.wait_mainloop('b') + root = tkinter.Tk() + self.addCleanup(root.destroy) + root.withdraw() + def callback(): + raise ZeroDivisionError + root.tk.createcommand('callback', callback) + root.tk.call('after', 0, 'callback') + root.update() + # Let the mainloop() in other thread run for a while. + time.sleep(0.2) + # The error was not raised in other thread... + self.assertEqual(self.results, [('b', None)]) + # ...but by the next mainloop() in this thread. + root.after(100, root.quit) + with self.assertRaises(ZeroDivisionError): + root.mainloop() + + class TkTest(AbstractTkTest, unittest.TestCase): def test_readprofile(self): diff --git a/Lib/test/test_tools/__init__.py b/Lib/test/test_tools/__init__.py index c4395c7c0ad0c9..310bf48c2ef03a 100644 --- a/Lib/test/test_tools/__init__.py +++ b/Lib/test/test_tools/__init__.py @@ -32,7 +32,10 @@ def skip_if_missing(tool=None): @contextlib.contextmanager def imports_under_tool(name, *subdirs): tooldir = os.path.join(toolsdir, name, *subdirs) - with import_helper.DirsOnSysPath(tooldir) as cm: + with ( + import_helper.force_eager_imports(), + import_helper.DirsOnSysPath(tooldir) as cm, + ): yield cm def import_tool(toolname): diff --git a/Lib/test/test_tools/msgfmt_data/fuzzy.mo b/Lib/test/test_tools/msgfmt_data/fuzzy.mo index 4b144831cf5f75..273edbbbd7cbd7 100644 Binary files a/Lib/test/test_tools/msgfmt_data/fuzzy.mo and b/Lib/test/test_tools/msgfmt_data/fuzzy.mo differ diff --git a/Lib/test/test_tools/msgfmt_data/general.mo b/Lib/test/test_tools/msgfmt_data/general.mo index ee905cbb3ec58d..3107f6711bdd10 100644 Binary files a/Lib/test/test_tools/msgfmt_data/general.mo and b/Lib/test/test_tools/msgfmt_data/general.mo differ diff --git a/Lib/test/test_tools/test_makeunicodedata.py b/Lib/test/test_tools/test_makeunicodedata.py index f31375117e2e92..3f34584239d70c 100644 --- a/Lib/test/test_tools/test_makeunicodedata.py +++ b/Lib/test/test_tools/test_makeunicodedata.py @@ -1,7 +1,7 @@ import unittest from test.test_tools import skip_if_missing, imports_under_tool from test import support -from test.support.hypothesis_helper import hypothesis +from test.support.hypothesis_helper import hypothesis, mark_hypothesis st = hypothesis.strategies given = hypothesis.given @@ -115,6 +115,7 @@ def test_forbid_empty_dawg(self): ("ab", "1"), ] ) + @mark_hypothesis def test_dawg(self, data): # suppress debug prints with support.captured_stdout() as output: diff --git a/Lib/test/test_tools/test_msgfmt.py b/Lib/test/test_tools/test_msgfmt.py index 3019e52d22ec82..0b39f509e0a415 100644 --- a/Lib/test/test_tools/test_msgfmt.py +++ b/Lib/test/test_tools/test_msgfmt.py @@ -47,6 +47,29 @@ def test_compilation(self): self.assertDictEqual(actual._catalog, expected._catalog) + def test_hash_table(self): + # Check _hashpjw generates correct hash values + self.assertEqual(msgfmt._hashpjw(b"stan"), 502398) + self.assertEqual(msgfmt._hashpjw(b"foo"), 27999) + # Carries into bit 32 are discarded, as GNU gettext does + self.assertEqual(msgfmt._hashpjw(b"kL{M-#[J)pxyz"), 32778) + + # Check hash table is generated correctly for general.po + with temp_cwd(): + tmp_mo_file = "messages.mo" + compile_messages(data_dir / "general.po", tmp_mo_file) + with open(tmp_mo_file, "rb") as f: + mo_data = f.read() + + header = struct.unpack("=7I", mo_data[:28]) + hash_table_size, hash_table_offset = header[5:7] + + hash_tab_end = hash_table_offset + hash_table_size * 4 + hash_tab = struct.unpack(f"={hash_table_size}I", + mo_data[hash_table_offset:hash_tab_end]) + + self.assertEqual(hash_tab, (1, 3, 0, 8, 9, 7, 2, 0, 4, 5, 0, 6, 0)) + def test_binary_header(self): with temp_cwd(): tmp_mo_file = 'messages.mo' @@ -69,8 +92,8 @@ def test_binary_header(self): self.assertEqual(num_strings, 9) self.assertEqual(orig_table_offset, 28) self.assertEqual(trans_table_offset, 100) - self.assertEqual(hash_table_size, 0) - self.assertEqual(hash_table_offset, 0) + self.assertEqual(hash_table_size, 13) + self.assertEqual(hash_table_offset, 172) def test_translations(self): with open(data_dir / 'general.mo', 'rb') as f: diff --git a/Lib/test/test_ttk_textonly.py b/Lib/test/test_ttk_textonly.py index e6525c4d6c6982..7fecbb6305442b 100644 --- a/Lib/test/test_ttk_textonly.py +++ b/Lib/test/test_ttk_textonly.py @@ -169,7 +169,7 @@ def test_format_mapdict(self): def test_format_elemcreate(self): - self.assertTrue(ttk._format_elemcreate(None), (None, ())) + self.assertEqual(ttk._format_elemcreate(None), ((),)) ## Testing type = image # image type expects at least an image name, so this should raise diff --git a/Lib/test/test_tuple.py b/Lib/test/test_tuple.py index 5650dec09adef4..a3bd60eca4f6b1 100644 --- a/Lib/test/test_tuple.py +++ b/Lib/test/test_tuple.py @@ -450,6 +450,45 @@ def test_lexicographic_ordering(self): self.assertLess(a, b) self.assertLess(b, c) + def test_hash_unhashable_note(self): + with self.assertRaisesRegex(TypeError, "unhashable type: 'list'") as cm: + hash(("alice", [])) + self.assertEqual(cm.exception.__notes__, + ["when hashing tuple item 1"]) + + with self.assertRaisesRegex(TypeError, "unhashable type: 'list'") as cm: + hash((("alice", []), 42)) + self.assertEqual(cm.exception.__notes__, + ["when hashing tuple item 1", + "when hashing tuple item 0"]) + + class BrokenHash: + def __hash__(self): + raise TypeError("custom failure") + + with self.assertRaisesRegex(TypeError, "custom failure") as cm: + hash((BrokenHash(),)) + self.assertEqual(cm.exception.__notes__, ["when hashing tuple item 0"]) + + class BrokenHashNote: + def __hash__(self): + exc = TypeError("custom failure") + exc.add_note("from custom hash") + raise exc + + with self.assertRaisesRegex(TypeError, "custom failure") as cm: + hash((BrokenHashNote(),)) + self.assertEqual(cm.exception.__notes__, + ["from custom hash", "when hashing tuple item 0"]) + + class BrokenHashValueError: + def __hash__(self): + raise ValueError("custom failure") + + with self.assertRaisesRegex(ValueError, "custom failure") as cm: + hash((BrokenHashValueError(),)) + self.assertFalse(hasattr(cm.exception, "__notes__")) + # Notes on testing hash codes. The primary thing is that Python doesn't # care about "random" hash codes. To the contrary, we like them to be # very regular when possible, so that the low-order bits are as evenly diff --git a/Lib/test/test_type_annotations.py b/Lib/test/test_type_annotations.py index cb164ccd147127..02f3e8b9caddb4 100644 --- a/Lib/test/test_type_annotations.py +++ b/Lib/test/test_type_annotations.py @@ -153,7 +153,11 @@ class D(metaclass=C): self.assertEqual(D.__annotations__, {}) def test_partially_executed_module(self): - partialexe = import_helper.import_fresh_module("test.typinganndata.partialexecution") + # Results are different with and without lazy imports, we need both: + partialexe = import_helper.import_fresh_module( + "test.typinganndata.partialexecution", + eager_imports=False, + ) self.assertEqual( partialexe.a.__annotations__, {"v1": int, "v2": int}, @@ -505,6 +509,32 @@ class C: """) self.assertEqual(ns["C"].__annotations__, {"__classdict__": int}) + def test_class_binds_dunder_classdict(self): + # The class body binding __classdict__ must not hide the cell that + # annotation and type parameter scopes use to see the class namespace. + for binding in ( + "__classdict__ = 1", + "__classdict__: str = 1", + "for __classdict__ in (1,): pass", + "global __classdict__", + ): + with self.subTest(binding=binding): + ns = run_code(f""" + class C: + {binding} + y = int + def f(x: y): pass + class D[T: y]: pass + class E[T]: pass + type A[T: y] = y + """) + C = ns["C"] + self.assertEqual(C.f.__annotations__, {"x": int}) + self.assertIs(C.D.__type_params__[0].__bound__, int) + self.assertEqual(C.E.__type_params__[0].__name__, "T") + self.assertIs(C.A.__type_params__[0].__bound__, int) + self.assertIs(C.A.__value__, int) + def test_future_annotations(self): code = """ from __future__ import annotations diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index b1775ba7d81e52..3879c49c714dd9 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -11300,6 +11300,7 @@ def test_all_exported_names(self): def test_any_str_deprecated(self): # gh-105578 fresh = import_helper.import_fresh_module('typing') + self.addCleanup(atexit.unregister, fresh._clear_caches) with self.assertWarnsRegex(DeprecationWarning, r'typing\.AnyStr'): fresh.AnyStr diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py index b1f359764fa4fe..3025a86b3b7328 100644 --- a/Lib/test/test_unparse.py +++ b/Lib/test/test_unparse.py @@ -921,6 +921,12 @@ def test_backslash_in_format_spec(self): self.check_ast_roundtrip("""f"{x:\\\\\\\\ }" """) + def test_braces_in_format_spec(self): + for prefix in "ft": + self.check_ast_roundtrip(prefix + r'"{value:\x7b>3}"') + self.check_ast_roundtrip(prefix + r'"{value:\x7d>3}"') + self.check_ast_roundtrip(r't"{value:\x7bwidth\x7d}"') + def test_quote_in_format_spec(self): self.check_ast_roundtrip("""f"{x:'}" """) self.check_ast_roundtrip("""f"{x:\\'}" """) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index ab59727a8fd182..3858b03546061e 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -7,6 +7,7 @@ import email.message import io import unittest +import unittest.mock from test import support from test.support import os_helper from test.support import socket_helper @@ -400,11 +401,17 @@ def test_url_host_with_newline_header_injection_rejected(self): host = "localhost\r\nX-injected: header\r\n" schemeless_url = "//" + host + ":8080/test/?test=a" try: - InvalidURL = http.client.InvalidURL + # Once \r\n are stripped from the URL, the ':' in + # "X-injected:" is mistaken for the port separator, and + # urlsplit()/urlparse() now reject the resulting + # non-numeric "port" at parse time, before a Request is + # even built -- rejecting the injection earlier than + # http.client's own InvalidURL check ever gets a chance to. with self.assertRaisesRegex( - InvalidURL, r"contain control.*\\r"): + ValueError, "Port could not be cast to integer value"): urllib.request.urlopen(f"http:{schemeless_url}") - with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"): + with self.assertRaisesRegex( + ValueError, "Port could not be cast to integer value"): urllib.request.urlopen(f"https:{schemeless_url}") finally: self.unfakehttp() @@ -1672,6 +1679,17 @@ def test_url2pathname_resolve_host(self): self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar'), f'{sep}foo{sep}bar') self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar', resolve_host=True), f'{sep}foo{sep}bar') + def test_url2pathname_win_netloc_not_validated_as_port(self): + # The Windows branch of url2pathname() stuffs things like drive + # letters into the netloc (e.g. '//C:\\' -> authority 'C:\\'), + # which is not a real host[:port] pair. url2pathname() must not + # reject it as an invalid port; run the Windows branch directly + # so this is exercised on every platform, not just win32. + with unittest.mock.patch('os.name', 'nt'): + fn = urllib.request.url2pathname + self.assertEqual(fn('//C:\\'), 'C:\\') + self.assertEqual(fn('//C:80/'), 'C:80\\') + @unittest.skipUnless(sys.platform == 'win32', 'test specific to Windows pathnames.') def test_url2pathname_win(self): diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index a5b5a3d706dd40..be1da0735b98fd 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -8,6 +8,7 @@ import os import io +import hashlib import ftplib import socket import array @@ -617,6 +618,42 @@ def https_open(self, req): return self.do_open(self.httpconn, req) +class MockHTTPTunnelClass(MockHTTPClass): + # Stands in for a proxy that requires authentication: CONNECT fails + # with tunnel_code until the credentials arrive in the tunnel headers + # (tunnel_code=None: no authentication). The origin then answers + # through the tunnel with origin_code. + def __init__(self, tunnel_code=407, challenge='Basic realm="proxy"', + origin_code=200): + super().__init__() + self.tunnel_code = tunnel_code + self.challenge = challenge + self.origin_code = origin_code + self.tunnel_requests = [] + + def challenge_headers(self): + return http.client.parse_headers(io.BytesIO( + b'Proxy-Authenticate: %s\r\n\r\n' % self.challenge.encode())) + + def request(self, method, url, body=None, headers=None, *, + encode_chunked=False): + self.tunnel_requests.append(dict(self._tunnel_headers)) + if (self.tunnel_code is not None and + "Proxy-Authorization" not in self._tunnel_headers): + raise http.client.ProxyTunnelError( + self.tunnel_code, http.client.responses[self.tunnel_code], + self.challenge_headers()) + super().request(method, url, body, headers, + encode_chunked=encode_chunked) + + def getresponse(self): + response = super().getresponse() + response.code = self.origin_code + if self.origin_code != 200: + response.info = self.challenge_headers + return response + + class MockHTTPHandlerCheckAuth(urllib.request.BaseHandler): # useful for testing auth # sends supplied code response @@ -951,9 +988,9 @@ def test_file(self): parsed._replace(netloc='localhost:80').geturl(), "file:///file_does_not_exist.txt", "file://not-a-local-host.com//dir/file.txt", - "file://%s:80%s/%s" % (socket.gethostbyname('localhost'), + "file://%s:80/%s/%s" % (socket.gethostbyname('localhost'), os.getcwd(), TESTFN), - "file://somerandomhost.ontheinternet.com%s/%s" % + "file://somerandomhost.ontheinternet.com/%s/%s" % (os.getcwd(), TESTFN), ]: try: @@ -1645,6 +1682,142 @@ def test_proxy_https_proxy_authorization(self): self.assertEqual(req.host, "proxy.example.com:3128") self.assertEqual(req.get_header("Proxy-authorization"), "FooBar") + def _open_through_tunnel(self, httpconn, password_mgr, + auth_handler=urllib.request.ProxyBasicAuthHandler, + url="https://www.example.com/path"): + https_handler = MockHTTPSHandler() + https_handler.httpconn = httpconn + opener = urllib.request.build_opener( + urllib.request.ProxyHandler(dict(https="proxy.example.com:3128")), + auth_handler(password_mgr), + https_handler) + return opener.open(url) + + @unittest.skipUnless(hasattr(http.client, 'HTTPSConnection'), 'HTTPSConnection required for HTTPS tests.') + def test_proxy_https_basic_auth_handler(self): + # gh-51540: a 407 from the proxy to CONNECT is passed to + # ProxyBasicAuthHandler, which retries with credentials + password_mgr = urllib.request.HTTPPasswordMgr() + password_mgr.add_password("proxy", "proxy.example.com:3128", + "user", "pass") + httpconn = MockHTTPTunnelClass() + response = self._open_through_tunnel(httpconn, password_mgr) + self.assertEqual(response.code, 200) + + self.assertEqual(len(httpconn.tunnel_requests), 2) + self.assertNotIn("Proxy-Authorization", httpconn.tunnel_requests[0]) + self.assertEqual(httpconn.tunnel_requests[1]["Proxy-Authorization"], + "Basic dXNlcjpwYXNz") + # The credentials go to the proxy only, not to the origin. + self.assertEqual(httpconn._tunnel_host, "www.example.com") + self.assertEqual(httpconn.selector, "/path") + self.assertNotIn("Proxy-Authorization", + dict(httpconn.req_headers)) + + @unittest.skipUnless(hasattr(http.client, 'HTTPSConnection'), 'HTTPSConnection required for HTTPS tests.') + @skip_libssl_fips_mode # Uses MD5 algorithm in challenge + def test_proxy_https_digest_auth_handler(self): + # gh-51540: a 407 from the proxy to CONNECT is passed to + # ProxyDigestAuthHandler, which retries with credentials + password_mgr = urllib.request.HTTPPasswordMgr() + password_mgr.add_password("proxy", "proxy.example.com:3128", + "user", "pass") + httpconn = MockHTTPTunnelClass( + challenge='Digest realm="proxy", nonce="abcdef0123456789", ' + 'qop="auth"') + response = self._open_through_tunnel( + httpconn, password_mgr, urllib.request.ProxyDigestAuthHandler) + self.assertEqual(response.code, 200) + + self.assertEqual(len(httpconn.tunnel_requests), 2) + self.assertNotIn("Proxy-Authorization", httpconn.tunnel_requests[0]) + self.assertStartsWith( + httpconn.tunnel_requests[1]["Proxy-Authorization"], + 'Digest username="user", realm="proxy", ') + self.assertEqual(httpconn.selector, "/path") + self.assertNotIn("Proxy-Authorization", + dict(httpconn.req_headers)) + + @unittest.skipUnless(hasattr(http.client, 'HTTPSConnection'), 'HTTPSConnection required for HTTPS tests.') + @skip_libssl_fips_mode # Uses MD5 algorithm in challenge + def test_proxy_https_digest_auth_connect(self): + # Through an https tunnel, the digest covers the CONNECT request + # (method CONNECT, request-target host:port), not the request + # later sent to the origin + def md5(text): + return hashlib.md5(text.encode("ascii")).hexdigest() + + nonce = "abcdef0123456789" + for url, target in [ + ("https://www.example.com/path", "www.example.com:443"), + ("https://www.example.com:8443/path", "www.example.com:8443"), + ]: + with self.subTest(url=url): + password_mgr = urllib.request.HTTPPasswordMgr() + password_mgr.add_password("proxy", "proxy.example.com:3128", + "user", "pass") + httpconn = MockHTTPTunnelClass( + challenge='Digest realm="proxy", nonce="%s"' % nonce) + self._open_through_tunnel( + httpconn, password_mgr, + urllib.request.ProxyDigestAuthHandler, url) + + expected = md5("%s:%s:%s" % (md5("user:proxy:pass"), nonce, + md5("CONNECT:" + target))) + auth = httpconn.tunnel_requests[1]["Proxy-Authorization"] + self.assertIn('uri="%s"' % target, auth) + self.assertIn('response="%s"' % expected, auth) + self.assertEqual(httpconn.selector, "/path") + + @unittest.skipUnless(hasattr(http.client, 'HTTPSConnection'), 'HTTPSConnection required for HTTPS tests.') + def test_proxy_https_origin_407(self): + # A 407 sent by the origin through the tunnel is retried like one + # from the proxy, but the credentials only go on the CONNECT + password_mgr = urllib.request.HTTPPasswordMgr() + password_mgr.add_password("proxy", "proxy.example.com:3128", + "user", "pass") + httpconn = MockHTTPTunnelClass(tunnel_code=None, origin_code=407) + with self.assertRaises(urllib.error.HTTPError) as cm: + self._open_through_tunnel(httpconn, password_mgr) + with cm.exception as err: + self.assertEqual(err.code, 407) + + self.assertEqual(len(httpconn.tunnel_requests), 2) + self.assertNotIn("Proxy-Authorization", httpconn.tunnel_requests[0]) + self.assertEqual(httpconn.tunnel_requests[1]["Proxy-Authorization"], + "Basic dXNlcjpwYXNz") + # Neither request to the origin carried the credentials. + self.assertNotIn("Proxy-Authorization", + dict(httpconn.req_headers)) + + @unittest.skipUnless(hasattr(http.client, 'HTTPSConnection'), 'HTTPSConnection required for HTTPS tests.') + def test_proxy_https_basic_auth_handler_no_password(self): + password_mgr = urllib.request.HTTPPasswordMgr() + httpconn = MockHTTPTunnelClass() + with self.assertRaises(urllib.error.HTTPError) as cm: + self._open_through_tunnel(httpconn, password_mgr) + with cm.exception as err: + self.assertEqual(err.code, 407) + self.assertEqual(err.reason, "Proxy Authentication Required") + self.assertEqual(err.headers["Proxy-Authenticate"], + 'Basic realm="proxy"') + self.assertEqual(err.read(), b"") + self.assertEqual(len(httpconn.tunnel_requests), 1) + + @unittest.skipUnless(hasattr(http.client, 'HTTPSConnection'), 'HTTPSConnection required for HTTPS tests.') + def test_proxy_https_tunnel_error(self): + # Other failures to CONNECT are still raised as URLError + password_mgr = urllib.request.HTTPPasswordMgr() + password_mgr.add_password("proxy", "proxy.example.com:3128", + "user", "pass") + httpconn = MockHTTPTunnelClass(tunnel_code=403) + with self.assertRaises(urllib.error.URLError) as cm: + self._open_through_tunnel(httpconn, password_mgr) + self.assertNotIsInstance(cm.exception, urllib.error.HTTPError) + self.assertIsInstance(cm.exception.reason, + http.client.ProxyTunnelError) + self.assertEqual(cm.exception.reason.code, 403) + @unittest.skipUnless(os.name == "nt", "only relevant for Windows") def test_winreg_proxy_bypass(self): proxy_override = "www.example.com;*.example.net; 192.168.0.1" @@ -1894,6 +2067,37 @@ def test_digest_auth_empty_path(self): self.assertIn('uri="/"', auth) self.assertIn('response="%s"' % respdig, auth) + def test_proxy_auth_ignores_direct_407(self): + # gh-158907: a direct HTTP origin that returns 407 must not receive + # credentials registered for the same authority over HTTPS. + realm = "test-realm" + cases = [ + (urllib.request.ProxyBasicAuthHandler, + 'Basic realm="%s"' % realm), + (urllib.request.ProxyDigestAuthHandler, + 'Digest realm="%s", nonce="abc", qop="auth"' % realm), + ] + for handler_class, challenge in cases: + with self.subTest(handler=handler_class.__name__): + opener = OpenerDirector() + opener.add_handler(urllib.request.ProxyHandler({})) + password_manager = urllib.request.HTTPPasswordMgr() + auth_handler = handler_class(password_manager) + http_handler = MockHTTPHandlerRedirect( + 407, 'Proxy-Authenticate: %s\r\n\r\n' % challenge) + opener.add_handler(auth_handler) + opener.add_handler(http_handler) + + password_manager.add_password( + realm, "https://example.com/", + "victim-user", "victim-secret") + opener.open("http://example.com/resource") + + self.assertEqual(len(http_handler.requests), 1) + self.assertFalse( + http_handler.requests[0].has_header( + "Proxy-authorization")) + def test_basic_and_digest_auth_handlers(self): # HTTPDigestAuthHandler raised an exception if it couldn't handle a 40* # response (https://bugs.python.org/issue1479302), where it should instead diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index a5b7966c7780e9..37e172f7f2caca 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -917,9 +917,8 @@ def test_urlsplit_attributes(self): # Verify an illegal port raises ValueError url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag" - p = urllib.parse.urlsplit(url) with self.assertRaisesRegex(ValueError, "out of range"): - p.port + urllib.parse.urlsplit(url) def test_urlsplit_remove_unsafe_bytes(self): # Remove ASCII tabs and newlines from input @@ -1029,10 +1028,8 @@ def test_attributes_bad_port(self, bytes, parse, port): self.skipTest('non-ASCII bytes') netloc = str_encode(netloc) url = str_encode(url) - p = parse(url) - self.assertEqual(p.netloc, netloc) with self.assertRaises(ValueError): - p.port + parse(url) @support.subTests('bytes', (False, True)) @support.subTests('parse', (urllib.parse.urlsplit, urllib.parse.urlparse)) @@ -1255,6 +1252,12 @@ def test_mixed_types_rejected(self): with self.assertRaisesRegex(TypeError, "Cannot mix str"): urllib.parse.urljoin(b"http://python.org", "http://python.org") + def test_non_string_true_values_rejected(self): + # True values raise informative TypeErrors + msg = "Expected str, bytes or None, not " + with self.assertRaisesRegex(TypeError, msg): + urllib.parse.urlsplit(1, b'http') + def _check_result_type(self, str_type, str_args): bytes_type = str_type._encoded_counterpart self.assertIs(bytes_type._decoded_counterpart, str_type) @@ -1670,13 +1673,11 @@ def test_splitting_bracketed_hosts(self): def test_port_casting_failure_message(self): message = "Port could not be cast to integer value as 'oracle'" - p1 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle') with self.assertRaisesRegex(ValueError, message): - p1.port + urllib.parse.urlparse('http://Server=sde; Service=sde:oracle') - p2 = urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle') with self.assertRaisesRegex(ValueError, message): - p2.port + urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle') def test_telurl_params(self): p1 = urllib.parse.urlparse('tel:123-4;phone-context=+1-650-516') @@ -1992,6 +1993,23 @@ def test_to_bytes_deprecation(self): self.assertEqual(str(cm.warning), 'urllib.parse.to_bytes() is deprecated as of 3.8') + def test_falsey_deprecation(self): + cases = [ + (urllib.parse.urljoin, ['http://www.python.org', []]), + (urllib.parse.urljoin, [[], b'docs']), + (urllib.parse.urlparse, [b'www.python.org', {}]), + (urllib.parse.urlparse, [{}, '']), + (urllib.parse.urlsplit, [0, b'http']), + (urllib.parse.urlsplit, [b'http://www.python.org', 0]), + (urllib.parse.urldefrag, [{}]), + (urllib.parse.urlunparse, [[None, b'www.python.org', (), (), (), ()]]), + (urllib.parse.urlunsplit, [['http', 0, '', '', '']]), + ] + for func, args in cases: + with self.subTest(callable=func.__name__, args=args): + with self.assertWarnsRegex(DeprecationWarning, "false value"): + func(*args) + def str_encode(s): if s is None: diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 678615ea172aef..eded0e5040d148 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -302,6 +302,28 @@ def test_prefixes(self): self.assertEqual(pathlib.Path(out.strip().decode()), pathlib.Path(expected), prefix) + # gh-157917: the "\\?\" extended-length prefix must not leak into the prefix + @requireVenvCreate + @unittest.skipUnless(os.name == 'nt', 'only relevant on Windows') + def test_prefixes_extended_length_path(self): + """ + Test that invoking a venv through a "\\?\" extended-length path does + not leave that prefix behind in the prefixes. Windows does not + resolve relative components (such as ".." or "/") in such paths, so + they break tools that join them onto sysconfig-derived paths. + """ + rmtree(self.env_dir) + self.run_with_capture(venv.create, self.env_dir) + envpy = os.path.join('\\\\?\\' + self.env_dir, self.bindir, self.exe) + for prefix in ('prefix', 'exec_prefix'): + with self.subTest(prefix): + out, err = check_output( + [envpy, '-c', 'import sys; print(sys.%s)' % prefix], + encoding='utf-8') + self.assertFalse(out.strip().startswith('\\\\?\\'), out) + self.assertEqual(pathlib.Path(out.strip()), + pathlib.Path(self.env_dir), prefix) + @requireVenvCreate def test_sysconfig(self): """ @@ -628,6 +650,27 @@ def test_install_scripts_selinux(self): venv.create(self.env_dir) listxattr_mock.assert_not_called() + @unittest.skipIf(os.name == 'nt', 'POSIX-specific permission test') + def test_install_scripts_honors_umask(self): + """ + gh-127172: Activation scripts must inherit the process umask (and any + default ACL) like every other created file, rather than a fixed mode + copied from the source template. + """ + old_umask = os.umask(0o002) + try: + venv.create(self.env_dir, with_pip=False) + bindir = os.path.join(self.env_dir, self.bindir) + control = os.path.join(bindir, 'control') + with open(control, 'w'): + pass + expected = os.stat(control).st_mode & 0o777 + for name in ('activate', 'activate.csh', 'activate.fish'): + mode = os.stat(os.path.join(bindir, name)).st_mode & 0o777 + self.assertEqual(mode, expected) + finally: + os.umask(old_umask) + def test_overwrite_existing(self): """ Test creating environment in an existing directory. @@ -663,6 +706,24 @@ def test_unoverwritable_fails(self): self.assertRaises((ValueError, OSError), venv.create, self.env_dir) self.clear_directory(self.env_dir) + @unittest.skipUnless(can_symlink(), 'Needs symlinks') + def test_symlinked_directory_error(self): + """ + Test that venv creation fails when a directory inside the target + directory is a symlink. + """ + outside = tempfile.mkdtemp() + self.addCleanup(rmtree, outside) + for n in range(1, len(self.lib) + 1): + subdirs = self.lib[:n] + with self.subTest(subdirs=subdirs): + rmtree(self.env_dir) + os.makedirs(os.path.join(self.env_dir, *subdirs[:-1])) + os.symlink(outside, os.path.join(self.env_dir, *subdirs), + target_is_directory=True) + self.assertRaises(ValueError, venv.create, self.env_dir) + self.assertEqual(os.listdir(outside), []) + def test_upgrade(self): """ Test upgrading an existing environment directory. diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index bfb43025de1140..c069b81eb91c2e 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -2020,6 +2020,7 @@ class ThreadTests(BaseTest): def setUp(self): super().setUp() + self.enterContext(self.module.catch_warnings()) self.module.resetwarnings() @unittest.skipIf(not ENABLE_THREAD_TESTS, @@ -2069,6 +2070,29 @@ def run_threads(): run_threads() + @threading_helper.requires_working_threading() + def test_lock_not_held_while_showing(self): + # showwarning() must not run with the warnings lock held, otherwise + # another thread that needs the lock would block. + import threading + + def other_thread(): + self.module.filterwarnings("always", category=TestWarning) + + def showwarning(*args, **kwargs): + t = threading.Thread(target=other_thread) + t.start() + t.join(timeout=support.SHORT_TIMEOUT) + alive.append(t.is_alive()) + + alive = [] + with self.module.catch_warnings(): + self.module.simplefilter("always") + self.module.showwarning = showwarning + self.module.warn("test", TestWarning) + self.module.warn_explicit("test", TestWarning, "file.py", 1) + self.assertEqual(alive, [False, False]) + class CThreadTests(ThreadTests, unittest.TestCase): module = c_warnings diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index da4155d09f7328..5b4ace36c46143 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -1,4 +1,5 @@ import gc +import io import sys import doctest import unittest @@ -1056,6 +1057,29 @@ def __del__(self): del head self.assertEqual(found, []) + def test_ref_to_object_dying_after_finalizer(self): + cleared = [] + class Attr: + def __del__(self): + cleared.append(self.wr() is None) + + class SetWithDel(set): + __slots__ = ('a',) + def __del__(self): + pass + + class Raw(io.RawIOBase): + __slots__ = ('a',) + + for cls in (SetWithDel, Raw): + with self.subTest(cls=cls.__name__): + cleared.clear() + o = cls() + o.a = Attr() + o.a.wr = weakref.ref(o) + del o + self.assertEqual(cleared, [True]) + def test_callback_attribute(self): x = Object(1) callback = lambda ref: None diff --git a/Lib/test/test_winapi.py b/Lib/test/test_winapi.py index 60f7881a0b0ab9..9412960d2fa7aa 100644 --- a/Lib/test/test_winapi.py +++ b/Lib/test/test_winapi.py @@ -5,7 +5,7 @@ import pathlib import re import unittest -from test.support import import_helper, os_helper +from test.support import import_helper, os_helper, requires_resource _winapi = import_helper.import_module('_winapi', required_on=['win']) @@ -166,6 +166,39 @@ def test_namedpipe(self): pipe2.flush() self.assertEqual((b'testdata', 8), _winapi.PeekNamedPipe(pipe, 8)[:2]) + def test_console_mode_non_console_handle(self): + rfd, wfd = _winapi.CreatePipe(None, 0) + self.addCleanup(_winapi.CloseHandle, rfd) + self.addCleanup(_winapi.CloseHandle, wfd) + + with self.assertRaises(OSError): + _winapi.GetConsoleMode(rfd) + with self.assertRaises(OSError): + _winapi.SetConsoleMode(rfd, 0) + with self.assertRaises(OSError): + _winapi.FlushConsoleInputBuffer(rfd) + + @requires_resource('console') + def test_console_mode(self): + try: + handle = _winapi.CreateFile( + "CONIN$", + _winapi.GENERIC_READ | _winapi.GENERIC_WRITE, + 3, # 3=FILE_SHARE_READ|FILE_SHARE_WRITE + _winapi.NULL, + _winapi.OPEN_EXISTING, + 0, + _winapi.NULL) + except OSError: + self.skipTest("requires a console") + self.addCleanup(_winapi.CloseHandle, handle) + + mode = _winapi.GetConsoleMode(handle) + self.assertIsInstance(mode, int) + self.assertIsNone(_winapi.SetConsoleMode(handle, mode)) + self.assertEqual(_winapi.GetConsoleMode(handle), mode) + self.assertIsNone(_winapi.FlushConsoleInputBuffer(handle)) + def test_event_source_registration(self): source_name = "PythonTestEventSource" diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index f0cf7dffab0e8a..a5d4d5f65feb38 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -1324,6 +1324,11 @@ def test_parse_error_after_chunk_boundary(self): ET.fromstring('%s<' % ('x' * size)) self.assertEqual(cm.exception.position, (1, size + 4)) + def test_parse_error_msg_is_string(self): + with self.assertRaises(ET.ParseError) as cm: + ET.XML("<") + self.assertIsInstance(cm.exception.msg, str) + self.assertEqual(cm.exception.msg, "unclosed token: line 1, column 0") @support.subTests('sample,exception', [ (b' \xa1', UnicodeDecodeError), # crashed @@ -4064,6 +4069,19 @@ def test_bad_find(self): with self.assertRaisesRegex(SyntaxError, 'cannot use absolute path'): e.findall('/tag') + def test_find_invalid_path(self): + e = ET.XML(SAMPLE_XML) + paths = ('@', './x=y', './(', './)', './]', './!=', './::', + './()', "./'tag'", './"tag"', 'tag/@') + for obj in (e, ET.ElementTree(e)): + for method in ('find', 'findall', 'findtext', 'iterfind'): + for path in paths: + with self.subTest(type=type(obj).__name__, + method=method, path=path): + self.assertRaisesRegex( + SyntaxError, '^invalid path$', + getattr(obj, method), path) + def test_find_through_ElementTree(self): e = ET.XML(SAMPLE_XML) self.assertEqual(ET.ElementTree(e).find('tag').tag, 'tag') diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index deb2bd8d572581..cf616f300670a3 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -161,6 +161,35 @@ def test_xmlpullparser_leaks(self): del parser support.gc_collect() + def test_iter_tag_reentrant_compare(self): + destroyed = [] + compared = [] + + class Tag: + def __del__(self): + destroyed.append(True) + + def __eq__(self, other): + child.tag = "replaced" + return NotImplemented + + __hash__ = object.__hash__ + + class Sought: + def __eq__(self, other): + if isinstance(other, Tag): + compared.append(not destroyed) + return NotImplemented + + __hash__ = object.__hash__ + + root = cET.Element("root") + child = cET.SubElement(root, "x") + child.tag = Tag() + self.assertEqual(list(root.iter(Sought())), []) + self.assertEqual(compared, [True]) + self.assertEqual(child.tag, "replaced") + def test_dict_disappearing_during_get_item(self): # test fix for seg fault reported in issue 27946 class X: diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py index ee0e24f6e86ae3..78dbb5459cf6ae 100644 --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -252,6 +252,18 @@ def test_loads_unsupported(self): '') self.assertRaises(ResponseError, xmlrpclib.loads, data) + def test_loads_nested_scalars(self): + template = '%s' + # CDATA splits 'ab' into two fragments so that joining them again + # would allocate another string. str.join() can reuse a single fragment. + data = template % 'a' + values, _ = xmlrpclib.loads(data) + self.assertEqual(values, ('ab', '')) + + data = template % '12' + values, _ = xmlrpclib.loads(data) + self.assertEqual(values, (1, 2)) + def check_loads(self, s, value, **kwargs): dump = '%s' % s result, m = xmlrpclib.loads(dump, **kwargs) diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index 84000f472c4df9..ccb8b87023eb5e 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -58,6 +58,15 @@ def get_files(test): test.assertFalse(f.closed) +@contextlib.contextmanager +def save_restore_sys_path(): + old_path = sys.path.copy() + try: + yield + finally: + sys.path[:] = old_path + + class LazyImportTest(unittest.TestCase): @cpython_only def test_lazy_import(self): @@ -4406,6 +4415,17 @@ def test_bad_compression_mode(self): caught.""" self.assertRaises(NotImplementedError, zipfile.ZipFile, TESTFN, "w", -1) + def test_mismatched_compression_method(self): + fp = io.BytesIO() + with zipfile.ZipFile(fp, "w", zipfile.ZIP_STORED) as zipf: + zipf.writestr("foo.txt", b"hello") + + fp.seek(8) # Compression method in the local file header. + fp.write(struct.pack("' + + role = _accessible_attribute('role') + name = _accessible_attribute('name') + description = _accessible_attribute('description') + value = _accessible_attribute('value') + state = _accessible_attribute('state') + action = _accessible_attribute('action') + help = _accessible_attribute('help') + + def add_object(self): + """Register the widget with the platform accessibility API.""" + widget = self._widget + widget.tk.call('tk', 'accessible', 'add_acc_object', widget._w) + + def emit_selection_change(self): + """Notify the platform accessibility API that the selection + in the widget has changed.""" + widget = self._widget + widget.tk.call('tk', 'accessible', 'emit_selection_change', + widget._w) + + def emit_focus_change(self): + """Notify the platform accessibility API that the widget has + received focus. + + Not available on macOS.""" + widget = self._widget + widget.tk.call('tk', 'accessible', 'emit_focus_change', widget._w) + + class Wm: """Provides functions for the communication with the window manager.""" diff --git a/Lib/tkinter/constants.py b/Lib/tkinter/constants.py index 63eee33d24d6c7..dcc326673b5174 100644 --- a/Lib/tkinter/constants.py +++ b/Lib/tkinter/constants.py @@ -18,6 +18,16 @@ NSEW='nsew' CENTER='center' +# -labelanchor, in addition to the values above +EN='en' +ES='es' +WN='wn' +WS='ws' + +# -compound +TEXT='text' +IMAGE='image' + # -fill NONE='none' X='x' @@ -54,8 +64,38 @@ # -bordermode INSIDE='inside' +IGNORE='ignore' OUTSIDE='outside' +# -direction +ABOVE='above' +BELOW='below' +FLUSH='flush' + +# -stretch +ALWAYS='always' +MIDDLE='middle' +NEVER='never' + +# -tabstyle +TABULAR='tabular' +WORDPROCESSOR='wordprocessor' + +# -insertunfocussed (Tk 8.6+) +HOLLOW='hollow' + +# -mode +DETERMINATE='determinate' +INDETERMINATE='indeterminate' + +# -selecttype (Tk 9.0+) +ITEM='item' +CELL='cell' + +# -show +TREE='tree' +HEADINGS='headings' + # Special tags, marks and insert positions SEL='sel' SEL_FIRST='sel.first' @@ -72,6 +112,29 @@ ACTIVE='active' # Canvas state HIDDEN='hidden' +# Entry and ttk widget states +READONLY='readonly' +# Other ttk widget states +ALTERNATE='alternate' +BACKGROUND='background' +FOCUS='focus' +HOVER='hover' +INVALID='invalid' +PRESSED='pressed' +SELECTED='selected' +# ttk widget states for Treeview items and Notebook tabs (Tk 9.1+) +# FIRST='first' and LAST='last' are also used +LEAF='leaf' +OPEN='open' + +# -validate +# ALL='all', FOCUS='focus' and NONE='none' are also valid +KEY='key' +FOCUSIN='focusin' +FOCUSOUT='focusout' + +# Validation reasons, in addition to the values above +FORCED='forced' # Menu item types CASCADE='cascade' @@ -80,6 +143,10 @@ RADIOBUTTON='radiobutton' SEPARATOR='separator' +# -type for menus +MENUBAR='menubar' +TEAROFF='tearoff' + # Selection modes for list boxes SINGLE='single' BROWSE='browse' @@ -108,3 +175,4 @@ SCROLL='scroll' UNITS='units' PAGES='pages' +PIXELS='pixels' diff --git a/Lib/turtle.py b/Lib/turtle.py index fbd1ff4870003d..a060d3aa651013 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -1,4 +1,3 @@ -# # turtle.py: a Tkinter based turtle graphics module for Python # Version 1.1b - 4. 5. 2009 # @@ -115,7 +114,7 @@ def config_dict(filename): try: key, value = line.split("=") except ValueError: - print("Bad line in config-file %s:\n%s" % (filename,line)) + print(f"Bad line in config-file {filename}:\n{line}") continue key = key.strip() value = value.strip() @@ -226,7 +225,7 @@ def __repr__(self): ### May be replaced by an interface to some different graphics toolkit ### ############################################################################## -## helper functions for Scrolled Canvas, to forward Canvas-methods +## Helper functions for Scrolled Canvas, to forward Canvas-methods ## to ScrolledCanvas class def __methodDict(cls, _dict): @@ -271,11 +270,12 @@ def __forwardmethods(fromClass, toClass, toPart, exclude = ()): class ScrolledCanvas(TK.Frame): - """Modeled after the scrolled canvas class from Grayons's Tkinter book. + """Modeled after the scrolled canvas class from Grayson's Tkinter book. Used as the default canvas, which pops up automatically when using turtle graphics functions or the Turtle class. """ + def __init__(self, master, width=500, height=350, canvwidth=600, canvheight=500): TK.Frame.__init__(self, master, width=width, height=height) @@ -339,33 +339,27 @@ def onResize(self, event): self.adjustScrolls() def bbox(self, *args): - """ 'forward' method, which canvas itself has inherited... - """ + """Forward to the canvas method of the same name.""" return self._canvas.bbox(*args) def cget(self, *args, **kwargs): - """ 'forward' method, which canvas itself has inherited... - """ + """Forward to the canvas method of the same name.""" return self._canvas.cget(*args, **kwargs) def config(self, *args, **kwargs): - """ 'forward' method, which canvas itself has inherited... - """ + """Forward to the canvas method of the same name.""" self._canvas.config(*args, **kwargs) def bind(self, *args, **kwargs): - """ 'forward' method, which canvas itself has inherited... - """ + """Forward to the canvas method of the same name.""" self._canvas.bind(*args, **kwargs) def unbind(self, *args, **kwargs): - """ 'forward' method, which canvas itself has inherited... - """ + """Forward to the canvas method of the same name.""" self._canvas.unbind(*args, **kwargs) def focus_force(self): - """ 'forward' method, which canvas itself has inherited... - """ + """Forward to the canvas method of the same name.""" self._canvas.focus_force() __forwardmethods(ScrolledCanvas, TK.Canvas, '_canvas') @@ -373,6 +367,7 @@ def focus_force(self): class _Root(TK.Tk): """Root class for Screen based on Tkinter.""" + def __init__(self): TK.Tk.__init__(self) @@ -384,7 +379,7 @@ def _getcanvas(self): return self._canvas def set_geometry(self, width, height, startx, starty): - self.geometry("%dx%d%+d%+d"%(width, height, startx, starty)) + self.geometry("%dx%d%+d%+d" % (width, height, startx, starty)) def ondestroy(self, destroy): self.wm_protocol("WM_DELETE_WINDOW", destroy) @@ -400,10 +395,10 @@ def win_height(self): class TurtleScreenBase: """Provide the basic graphics functionality. - Interface between Tkinter and turtle.py. + Interface between Tkinter and turtle.py. - To port turtle.py to some different graphics toolkit - a corresponding TurtleScreenBase class has to be implemented. + To port turtle.py to a different graphics toolkit, + a corresponding TurtleScreenBase class must be implemented. """ def _blankimage(self): @@ -518,8 +513,7 @@ def _delay(self, delay): self.cv.after(delay) def _iscolorstring(self, color): - """Check if the string color is a legal Tkinter color string. - """ + """Check if the string color is a legal Tkinter color string.""" try: self.cv.winfo_rgb(color) ok = True @@ -558,31 +552,28 @@ def _onclick(self, item, fun, num=1, add=None): num, the number of the mouse-button defaults to 1 """ if fun is None: - self.cv.tag_unbind(item, "" % num) + self.cv.tag_unbind(item, f"") else: def eventfun(event): x, y = (self.cv.canvasx(event.x)/self.xscale, -self.cv.canvasy(event.y)/self.yscale) fun(x, y) - self.cv.tag_bind(item, "" % num, eventfun, add) + self.cv.tag_bind(item, f"", eventfun, add) def _onrelease(self, item, fun, num=1, add=None): """Bind fun to mouse-button-release event on turtle. fun must be a function with two arguments, the coordinates of the point on the canvas where mouse button is released. num, the number of the mouse-button defaults to 1 - - If a turtle is clicked, first _onclick-event will be performed, - then _onscreensclick-event. """ if fun is None: - self.cv.tag_unbind(item, "" % num) + self.cv.tag_unbind(item, f"") else: def eventfun(event): x, y = (self.cv.canvasx(event.x)/self.xscale, -self.cv.canvasy(event.y)/self.yscale) fun(x, y) - self.cv.tag_bind(item, "" % num, + self.cv.tag_bind(item, f"", eventfun, add) def _ondrag(self, item, fun, num=1, add=None): @@ -595,7 +586,7 @@ def _ondrag(self, item, fun, num=1, add=None): mouse-click event on that turtle. """ if fun is None: - self.cv.tag_unbind(item, "" % num) + self.cv.tag_unbind(item, f"") else: def eventfun(event): try: @@ -604,7 +595,7 @@ def eventfun(event): fun(x, y) except Exception: pass - self.cv.tag_bind(item, "" % num, eventfun, add) + self.cv.tag_bind(item, f"", eventfun, add) def _onscreenclick(self, fun, num=1, add=None): """Bind fun to mouse-click event on canvas. @@ -613,27 +604,28 @@ def _onscreenclick(self, fun, num=1, add=None): num, the number of the mouse-button defaults to 1 If a turtle is clicked, first _onclick-event will be performed, - then _onscreensclick-event. + then _onscreenclick-event. """ if fun is None: - self.cv.unbind("" % num) + self.cv.unbind(f"") else: def eventfun(event): x, y = (self.cv.canvasx(event.x)/self.xscale, -self.cv.canvasy(event.y)/self.yscale) fun(x, y) - self.cv.bind("" % num, eventfun, add) + self.cv.bind(f"", eventfun, add) def _onkeyrelease(self, fun, key): """Bind fun to key-release event of key. - Canvas must have focus. See method listen + + Canvas must have focus. See the listen() method. """ if fun is None: - self.cv.unbind("" % key, None) + self.cv.unbind(f"", None) else: def eventfun(event): fun() - self.cv.bind("" % key, eventfun) + self.cv.bind(f"", eventfun) def _onkeypress(self, fun, key=None): """If key is given, bind fun to key-press event of key. @@ -644,37 +636,32 @@ def _onkeypress(self, fun, key=None): if key is None: self.cv.unbind("", None) else: - self.cv.unbind("" % key, None) + self.cv.unbind(f"", None) else: def eventfun(event): fun() if key is None: self.cv.bind("", eventfun) else: - self.cv.bind("" % key, eventfun) + self.cv.bind(f"", eventfun) def _listen(self): - """Set focus on canvas (in order to collect key-events) - """ + """Set focus on canvas (in order to collect key-events).""" self.cv.focus_force() def _ontimer(self, fun, t): - """Install a timer, which calls fun after t milliseconds. - """ + """Install a timer, which calls fun after t milliseconds.""" if t == 0: self.cv.after_idle(fun) else: self.cv.after(t, fun) def _createimage(self, image): - """Create and return image item on canvas. - """ + """Create and return image item on canvas.""" return self.cv.create_image(0, 0, image=image) def _drawimage(self, item, pos, image): - """Configure image item as to draw image object - at position (x,y) on canvas) - """ + """Configure the canvas item to show image at position pos.""" x, y = pos self.cv.coords(item, (x * self.xscale, -y * self.yscale)) self.cv.itemconfig(item, image=image) @@ -683,14 +670,12 @@ def _setbgpic(self, item, image): """Configure image item as to draw image object at center of canvas. Set item to the first item in the displaylist, so it will be drawn below - any other item .""" + any other item.""" self.cv.itemconfig(item, image=image) self.cv.tag_lower(item) def _type(self, item): - """Return 'line' or 'polygon' or 'image' depending on - type of item. - """ + """Return 'line' or 'polygon' or 'image' depending on type of item.""" return self.cv.type(item) def _pointlist(self, item): @@ -747,7 +732,7 @@ def _window_size(self): return width, height def mainloop(self): - """Start the event loop by calling Tkinter's mainloop function. + """Start the event loop by calling Tkinter's mainloop method. Must be the last statement in a turtle graphics program. Must not be used if a script is run from within IDLE in -n mode (no @@ -777,10 +762,10 @@ def numinput(self, title, prompt, default=None, minval=None, maxval=None): title is the title of the dialog window and prompt is a text describing what numerical information to input. default is the default value, minval and maxval are the minimum and maximum - values for input. If these are given, the number entered must be - between minval and maxval. If not, a hint is issued and the dialog - remains open for correction. Return the number entered. - If the dialog is canceled, return None. + values for input. If these are given and the number entered is not + between minval and maxval, a hint is issued and the dialog remains + open for correction. Return the number entered. If the dialog is + canceled, return None. Arguments: title -- a string @@ -804,12 +789,11 @@ def numinput(self, title, prompt, default=None, minval=None, maxval=None): class Terminator(Exception): - """Will be raised in TurtleScreen.update, if _RUNNING becomes False. + """Raised by turtle commands after the turtle screen has been closed. - This stops execution of a turtle graphics script. - Main purpose: use in the Demo-Viewer turtle.Demo.py. + This stops the execution of a turtle graphics script. It is mainly + used by the turtledemo viewer. """ - pass class TurtleGraphicsError(Exception): @@ -825,6 +809,7 @@ class Shape: "compound" None, as a compound shape is built up using the addcomponent method. """ + def __init__(self, type_, data=None): self._type = type_ if type_ == "polygon": @@ -835,7 +820,7 @@ def __init__(self, type_, data=None): elif type_ == "compound": data = [] else: - raise TurtleGraphicsError("There is no shape type %s" % type_) + raise TurtleGraphicsError(f"There is no shape type {type_!r}") self._data = data def addcomponent(self, poly, fill, outline=None): @@ -857,8 +842,8 @@ def addcomponent(self, poly, fill, outline=None): >>> # ... add more components and then use register_shape() """ if self._type != "compound": - raise TurtleGraphicsError("Cannot add component to %s Shape" - % self._type) + raise TurtleGraphicsError(f"Cannot add component to {self._type!r} Shape") + if outline is None: outline = fill self._data.append([poly, fill, outline]) @@ -916,14 +901,14 @@ def __repr__(self): return str(self.buffer) + " " + str(self.ptr) - class TurtleScreen(TurtleScreenBase): - """Provides screen oriented methods like bgcolor etc. + """Provides the screen-oriented methods, such as bgcolor(). Only relies upon the methods of TurtleScreenBase and not upon components of the underlying graphics toolkit, which is Tkinter in this case. """ + _RUNNING = True def __init__(self, cv, mode=_CFG["mode"], @@ -979,9 +964,6 @@ def clear(self): As a function, this method is only available under the name clearscreen. The function clear is derived from the Turtle method clear. - - Example (for a TurtleScreen instance named screen): - >>> screen.clearscreen() """ self._delayvalue = _CFG["delay"] self._colormode = _CFG["colormode"] @@ -1169,7 +1151,7 @@ def colormode(self, cmode=None): >>> turtle.pencolor(240, 160, 80) Traceback (most recent call last): ... - TurtleGraphicsError: bad color sequence: (240, 160, 80) + turtle.TurtleGraphicsError: bad color sequence: (240, 160, 80) >>> screen.colormode() 1.0 >>> screen.colormode(255) @@ -1328,7 +1310,10 @@ def _incrementudc(self): self._updatecounter %= self._tracing def update(self): - """Perform a TurtleScreen update.""" + """Perform a TurtleScreen update. + + To be used when tracer() is turned off. + """ tracing = self._tracing self._tracing = True for t in self.turtles(): @@ -1383,7 +1368,7 @@ def onclick(self, fun, btn=1, add=None): canvas. If fun is None, existing bindings are removed. btn is the number of the mouse button and defaults to 1, the left mouse button. If add is True, a new binding is added, otherwise it - replaces a former binding. As a function, this method is only + replaces a previous binding. As a function, this method is only available under the name onscreenclick. The function onclick is derived from the Turtle method onclick. @@ -1401,27 +1386,28 @@ def onclick(self, fun, btn=1, add=None): self._onscreenclick(fun, btn, add) def onkey(self, fun, key): - """Bind fun to key-release event of key. + """Bind fun to the key-release event of key. - Arguments: - fun -- a function with no arguments - key -- a string: key (e.g. "a") or key-symbol (e.g. "space") + Aliases: onkey | onkeyrelease - In order to be able to register key-events, TurtleScreen - must have focus. (See method listen.) - - Example (for a TurtleScreen instance named screen): + key is a key, such as "a", or a key symbol, such as "space". If + fun is None, the event binding is removed. To be able to receive + key events, the TurtleScreen must have focus, see listen(). - >>> def f(): - ... fd(50) - ... lt(60) - ... - >>> screen.onkey(f, "Up") - >>> screen.listen() + Arguments: + fun -- a function with no arguments or None + key -- a string - Subsequently the turtle can be moved by repeatedly pressing - the up-arrow key, consequently drawing a hexagon + Example (for a TurtleScreen instance named screen): + >>> def f(): + ... fd(50) + ... lt(60) + ... + >>> screen.onkey(f, "Up") + >>> screen.listen() + The turtle can then be moved by repeatedly pressing the up-arrow + key, drawing a hexagon. """ if fun is None: if key in self._keys: @@ -1431,29 +1417,28 @@ def onkey(self, fun, key): self._onkeyrelease(fun, key) def onkeypress(self, fun, key=None): - """Bind fun to key-press event of key if key is given, - or to any key-press-event if no key is given. + """Bind fun to the key-press event of key. - Arguments: - fun -- a function with no arguments - key -- a string: key (e.g. "a") or key-symbol (e.g. "space") + key is a key, such as "a", or a key symbol, such as "space". If + key is not given, fun is bound to the key-press event of any key. + If fun is None, the event binding is removed. To be able to + receive key events, the TurtleScreen must have focus, see + listen(). - In order to be able to register key-events, TurtleScreen - must have focus. (See method listen.) - - Example (for a TurtleScreen instance named screen - and a Turtle instance named turtle): + Arguments: + fun -- a function with no arguments or None + key -- a string or None - >>> def f(): - ... fd(50) - ... lt(60) - ... - >>> screen.onkeypress(f, "Up") - >>> screen.listen() + Example (for a TurtleScreen instance named screen): + >>> def f(): + ... fd(50) + ... lt(60) + ... + >>> screen.onkeypress(f, "Up") + >>> screen.listen() - Subsequently the turtle can be moved by repeatedly pressing - the up-arrow key, or by keeping pressed the up-arrow key. - consequently drawing a hexagon. + The turtle can then be moved by pressing or holding down the + up-arrow key, drawing a hexagon. """ if fun is None: if key in self._keys: @@ -1463,54 +1448,54 @@ def onkeypress(self, fun, key=None): self._onkeypress(fun, key) def listen(self, xdummy=None, ydummy=None): - """Set focus on TurtleScreen (in order to collect key-events) + """Set focus on the TurtleScreen so that it receives key events. - No arguments. - Dummy arguments are provided in order - to be able to pass listen to the onclick method. + The two dummy arguments are only there so that listen() can be + passed to onclick(). - Example (for a TurtleScreen instance named screen): - >>> screen.listen() + Arguments: + xdummy -- ignored + ydummy -- ignored """ self._listen() def ontimer(self, fun, t=0): - """Install a timer, which calls fun after t milliseconds. + """Install a timer that calls fun after t milliseconds. Arguments: - fun -- a function with no arguments. - t -- a number >= 0 + fun -- a function with no arguments + t -- a nonnegative number Example (for a TurtleScreen instance named screen): - - >>> running = True - >>> def f(): - ... if running: - ... fd(50) - ... lt(60) - ... screen.ontimer(f, 250) - ... - >>> f() # makes the turtle marching around - >>> running = False + >>> running = True + >>> def f(): + ... if running: + ... fd(50) + ... lt(60) + ... screen.ontimer(f, 250) + ... + >>> f() # makes the turtle march around + >>> running = False """ self._ontimer(fun, t) def bgpic(self, picname=None): - """Set background image or return name of current backgroundimage. + """Set the background image or return the name of the current one. - Optional argument: - picname -- a string, name of an image file (PNG, GIF, PGM, and PPM) or "nopic". + If picname is the name of an image file (PNG, GIF, PGM, and PPM), + set that image as the background. If picname is "nopic", delete + the background image, if present. If picname is None, return the + filename of the current background image. - If picname is a filename, set the corresponding image as background. - If picname is "nopic", delete backgroundimage, if present. - If picname is None, return the filename of the current backgroundimage. + Argument: + picname -- a string or None Example (for a TurtleScreen instance named screen): - >>> screen.bgpic() - 'nopic' - >>> screen.bgpic("landscape.gif") - >>> screen.bgpic() - 'landscape.gif' + >>> screen.bgpic() + 'nopic' + >>> screen.bgpic("landscape.gif") + >>> screen.bgpic() + 'landscape.gif' """ if picname is None: return self._bgpicname @@ -1522,34 +1507,44 @@ def bgpic(self, picname=None): def screensize(self, canvwidth=None, canvheight=None, bg=None): """Resize the canvas the turtles are drawing on. - Optional arguments: - canvwidth -- positive integer, new width of canvas in pixels - canvheight -- positive integer, new height of canvas in pixels - bg -- colorstring or color-tuple, new backgroundcolor - If no arguments are given, return current (canvaswidth, canvasheight) + canvwidth and canvheight are the new width and height of the + canvas in pixels, and bg is the new background color. If no + arguments are given, return the current canvas width and height. - Do not alter the drawing window. To observe hidden parts of - the canvas use the scrollbars. (Can make visible those parts - of a drawing, which were outside the canvas before!) + The drawing window is not altered. To see hidden parts of the + canvas, use the scrollbars. This can make visible those parts of a + drawing which were outside the canvas before, for example to find + a turtle that has wandered off the canvas. - Example (for a Turtle instance named turtle): - >>> turtle.screensize(2000,1500) - >>> # e.g. to search for an erroneously escaped turtle ;-) + Arguments: + canvwidth -- a positive integer or None + canvheight -- a positive integer or None + bg -- a colorstring or None + + Example (for a TurtleScreen instance named screen): + >>> screen.screensize() + (400, 300) + >>> screen.screensize(2000,1500) + >>> screen.screensize() + (2000, 1500) """ return self._resize(canvwidth, canvheight, bg) def save(self, filename, *, overwrite=False): - """Save the drawing as a PostScript file + """Save the current drawing and the turtles as a PostScript file. - Arguments: - filename -- a string, the path of the created file. - Must end with '.ps' or '.eps'. + filename is the path of the file to create. It must end with + ".ps" or ".eps". If a file with that name already exists, a + FileExistsError is raised, unless overwrite is True. overwrite + must be given by name, as in overwrite=True. - Optional arguments: - overwrite -- boolean, if true, then existing files will be overwritten + Arguments: + filename -- a string + overwrite -- a boolean Example (for a TurtleScreen instance named screen): - >>> screen.save('my_drawing.eps') + >>> screen.save("my_drawing.ps") + >>> screen.save("my_drawing.ps", overwrite=True) """ filename = Path(filename) if not filename.parent.exists(): @@ -1571,17 +1566,21 @@ def save(self, filename, *, overwrite=False): postscript = self.cv.postscript() filename.write_text(postscript) + # Aliases for the commonly used commands: + onscreenclick = onclick resetscreen = reset clearscreen = clear addshape = register_shape onkeyrelease = onkey + class TNavigator: """The navigation part of RawTurtle. Implements methods for turtle movement. """ + START_ORIENTATION = { "standard": Vec2D(1.0, 0.0), "world" : Vec2D(1.0, 0.0), @@ -1819,10 +1818,10 @@ def xcor(self): return self._position[0] def ycor(self): - """Return the turtle's y coordinate + """Return the turtle's y coordinate. Example (for a Turtle instance named turtle): - >>> reset() + >>> turtle.home() >>> turtle.left(60) >>> turtle.forward(100) >>> print(round(turtle.ycor(), 5)) @@ -1841,7 +1840,7 @@ def goto(self, x, y=None): The turtle's heading does not change. Arguments: - x -- a number or a pair/vector of numbers + x -- a number or a pair of numbers y -- a number or None Example (for a Turtle instance named turtle): @@ -1924,7 +1923,7 @@ def distance(self, x, y=None): as returned by pos(), or another turtle. Arguments: - x -- a number or a pair/vector of numbers or a turtle instance + x -- a number or a pair of numbers or a turtle instance y -- a number if x is a number, else None Example (for a Turtle instance named turtle): @@ -1957,7 +1956,7 @@ def towards(self, x, y=None): see mode(). Arguments: - x -- a number or a pair/vector of numbers or a turtle instance + x -- a number or a pair of numbers or a turtle instance y -- a number if x is a number, else None Example (for a Turtle instance named turtle): @@ -2168,13 +2167,12 @@ def pensize(self, width=None): Example (for a Turtle instance named turtle): >>> turtle.pensize() 1 - >>> turtle.pensize(10) # from here on lines of width 10 are drawn + >>> turtle.pensize(10) # from here on lines of width 10 are drawn """ if width is None: return self._pensize self.pen(pensize=width) - def penup(self): """Lift the pen. @@ -2214,9 +2212,9 @@ def speed(self, speed=None): """Return or set the turtle's speed. If speed is not given, return the current speed. Otherwise set the - turtle's speed to an integer from 0 to 10. If speed is a number - greater than 10 or smaller than 0.5, the speed is set to 0. You - can also use these names instead of numbers: + turtle's speed to an integer from 0 to 10. A number above 0.5 and + below 10.5 is rounded to the nearest integer. Any other number sets + the speed to 0. You can also use these names instead of numbers: "fastest": 0 "fast": 10 @@ -2424,49 +2422,47 @@ def isvisible(self): def pen(self, pen=None, **pendict): """Return or set the pen's attributes. + The attributes are returned as a dictionary with the following + keys and values: + + - "shown": True or False + - "pendown": True or False + - "pencolor": a color string or a color tuple + - "fillcolor": a color string or a color tuple + - "pensize": a positive number + - "speed": a number from 0 to 10 + - "resizemode": "auto", "user" or "noresize" + - "stretchfactor": a pair of positive numbers + - "shearfactor": a number + - "outline": a positive number + - "tilt": a number + + Such a dictionary can be passed as pen to restore a former pen + state. The attributes can also be given as keyword arguments, + which can be used to set several of them in one statement. + Arguments: - pen -- a dictionary with some or all of the below listed keys. - **pendict -- one or more keyword-arguments with the below - listed keys as keywords. - - Return or set the pen's attributes in a 'pen-dictionary' - with the following key/value pairs: - "shown" : True/False - "pendown" : True/False - "pencolor" : color-string or color-tuple - "fillcolor" : color-string or color-tuple - "pensize" : positive number - "speed" : number in range 0..10 - "resizemode" : "auto" or "user" or "noresize" - "stretchfactor": (positive number, positive number) - "shearfactor": number - "outline" : positive number - "tilt" : number - - This dictionary can be used as argument for a subsequent - pen()-call to restore the former pen-state. Moreover one - or more of these attributes can be provided as keyword-arguments. - This can be used to set several pen attributes in one statement. - - - Examples (for a Turtle instance named turtle): - >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10) - >>> turtle.pen() - {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, - 'pencolor': 'red', 'pendown': True, 'fillcolor': 'black', - 'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0} - >>> penstate=turtle.pen() - >>> turtle.color("yellow","") - >>> turtle.penup() - >>> turtle.pen() - {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, - 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '', - 'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0} - >>> p.pen(penstate, fillcolor="green") - >>> p.pen() - {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, - 'pencolor': 'red', 'pendown': True, 'fillcolor': 'green', - 'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0} + pen -- a dictionary or None + pendict -- keyword arguments with the keys above + + Example (for a Turtle instance named turtle): + >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10) + >>> turtle.pen() + {'shown': True, 'pendown': True, 'pencolor': 'red', + 'fillcolor': 'black', 'pensize': 10, 'speed': 3, + 'resizemode': 'noresize', 'stretchfactor': (1.0, 1.0), + 'shearfactor': 0.0, 'outline': 1, 'tilt': 0.0} + >>> penstate = turtle.pen() + >>> turtle.color("yellow", "") + >>> turtle.penup() + >>> turtle.pen()["pencolor"] + 'yellow' + >>> # restore the saved pen state, but with a green fill + >>> turtle.pen(penstate, fillcolor="green") + >>> turtle.pen()["pencolor"] + 'red' + >>> turtle.pen()["fillcolor"] + 'green' """ _pd = {"shown" : self._shown, "pendown" : self._drawing, @@ -2625,7 +2621,7 @@ def __init__(self, canvas=None, self.screen = TurtleScreen(canvas) RawTurtle.screens.append(self.screen) else: - raise TurtleGraphicsError("bad canvas argument %s" % canvas) + raise TurtleGraphicsError(f"Bad canvas argument {canvas!r}") screen = self.screen TNavigator.__init__(self, screen.mode()) @@ -2692,8 +2688,8 @@ def undobufferentries(self): """Return the number of entries in the undo buffer. Example (for a Turtle instance named turtle): - >>> while undobufferentries(): - ... undo() + >>> while turtle.undobufferentries(): + ... turtle.undo() """ if self.undobuffer is None: return 0 @@ -2718,9 +2714,6 @@ def clear(self): The state and position of the turtle as well as the drawings of other turtles are not affected. - - Example (for a Turtle instance named turtle): - >>> turtle.clear() """ self._clear() self._update() @@ -2862,7 +2855,7 @@ def shape(self, name=None): """Set the turtle shape to the named shape. If name is not given, return the name of the current shape. The - shape must exist in the TurtleScreen's shape dictionary. Initially + shape must exist in the TurtleScreen's shape list. Initially there are the following shapes: "arrow", "turtle", "circle", "square", "triangle" and "classic". To add shapes, see the Screen method register_shape(). @@ -2939,55 +2932,60 @@ def shapesize(self, stretch_wid=None, stretch_len=None, outline=None): stretchfactor=stretchfactor, outline=outline) def shearfactor(self, shear=None): - """Set or return the current shearfactor. - - Optional argument: shear -- number, tangent of the shear angle - - Shear the turtleshape according to the given shearfactor shear, - which is the tangent of the shear angle. DO NOT change the - turtle's heading (direction of movement). - If shear is not given: return the current shearfactor, i. e. the - tangent of the shear angle, by which lines parallel to the - heading of the turtle are sheared. - - Examples (for a Turtle instance named turtle): - >>> turtle.shape("circle") - >>> turtle.shapesize(5,2) - >>> turtle.shearfactor(0.5) - >>> turtle.shearfactor() - >>> 0.5 + """Return or set the current shear factor. + + Shearing slants the turtle shape, like the slant of italic + letters. The shear factor says how much it is slanted: 0 is not + slanted at all, and the larger the number, the stronger the + slant. Negative numbers slant the other way. If shear is not + given, return the current shear factor. Otherwise slant the + turtle shape accordingly and set resizemode to "user". The + turtle's heading does not change. Precisely, the shear factor is + the tangent of the shear angle. + + Argument: + shear -- a number or None + + Example (for a Turtle instance named turtle): + >>> turtle.shape("circle") + >>> turtle.shapesize(5,2) + >>> turtle.shearfactor(0.5) + >>> turtle.shearfactor() + 0.5 """ if shear is None: return self._shearfactor self.pen(resizemode="user", shearfactor=shear) def tiltangle(self, angle=None): - """Set or return the current tilt-angle. - - Optional argument: angle -- number - - Rotate the turtleshape to point in the direction specified by angle, - regardless of its current tilt-angle. DO NOT change the turtle's - heading (direction of movement). - If angle is not given: return the current tilt-angle, i. e. the angle - between the orientation of the turtleshape and the heading of the - turtle (its direction of movement). - - Examples (for a Turtle instance named turtle): - >>> turtle.shape("circle") - >>> turtle.shapesize(5, 2) - >>> turtle.tiltangle() - 0.0 - >>> turtle.tiltangle(45) - >>> turtle.tiltangle() - 45.0 - >>> turtle.stamp() - >>> turtle.fd(50) - >>> turtle.tiltangle(-45) - >>> turtle.tiltangle() - 315.0 - >>> turtle.stamp() - >>> turtle.fd(50) + """Return or set the current tilt angle. + + The tilt angle is the angle between the direction the turtle shape + points in and the direction the turtle moves in. Normally the + shape points in the direction of movement, so the tilt angle is + 0. If angle is given, turn the turtle shape to that tilt angle, no + matter what it was before, and set resizemode to "user". The + turtle's heading does not change. If angle is not given, return + the current tilt angle. + + Argument: + angle -- a number or None + + Example (for a Turtle instance named turtle): + >>> turtle.shape("circle") + >>> turtle.shapesize(5, 2) + >>> turtle.tiltangle() + 0.0 + >>> turtle.tiltangle(45) + >>> turtle.tiltangle() + 45.0 + >>> stamp_id = turtle.stamp() + >>> turtle.fd(50) + >>> turtle.tiltangle(-45) + >>> turtle.tiltangle() + 315.0 + >>> stamp_id = turtle.stamp() + >>> turtle.fd(50) """ if angle is None: tilt = -math.degrees(self._tilt) * self._angleOrient @@ -2998,43 +2996,47 @@ def tiltangle(self, angle=None): self.pen(resizemode="user", tilt=tilt) def tilt(self, angle): - """Rotate the turtleshape by angle. + """Turn the turtle shape by angle from its current tilt angle. + + The turtle's heading does not change, so the turtle keeps moving + in the same direction. See tiltangle(). Argument: - angle - a number - - Rotate the turtleshape by angle from its current tilt-angle, - but do NOT change the turtle's heading (direction of movement). - - Examples (for a Turtle instance named turtle): - >>> turtle.shape("circle") - >>> turtle.shapesize(5,2) - >>> turtle.tilt(30) - >>> turtle.fd(50) - >>> turtle.tilt(30) - >>> turtle.fd(50) + angle -- a number + + Example (for a Turtle instance named turtle): + >>> turtle.shape("circle") + >>> turtle.shapesize(5,2) + >>> turtle.tilt(30) + >>> turtle.fd(50) + >>> turtle.tilt(30) + >>> turtle.fd(50) """ self.tiltangle(angle + self.tiltangle()) def shapetransform(self, t11=None, t12=None, t21=None, t22=None): - """Set or return the current transformation matrix of the turtle shape. - - Optional arguments: t11, t12, t21, t22 -- numbers. - - If none of the matrix elements are given, return the transformation - matrix. - Otherwise set the given elements and transform the turtleshape - according to the matrix consisting of first row t11, t12 and - second row t21, 22. - Modify stretchfactor, shearfactor and tiltangle according to the - given matrix. - - Examples (for a Turtle instance named turtle): - >>> turtle.shape("square") - >>> turtle.shapesize(4,2) - >>> turtle.shearfactor(-0.5) - >>> turtle.shapetransform() - (4.0, -1.0, -0.0, 2.0) + """Return or set the transformation matrix of the turtle shape. + + The matrix combines the stretch factors, the shear factor and the + tilt angle of the turtle shape into four numbers: t11 and t12 form + its first row, t21 and t22 its second. If none of them are given, + return the current matrix as a tuple of four numbers. Otherwise + set the given elements and update the stretch factors, shear + factor and tilt angle accordingly. t11 * t22 - t12 * t21 must not + be zero, otherwise an error is raised. + + Arguments: + t11 -- a number or None + t12 -- a number or None + t21 -- a number or None + t22 -- a number or None + + Example (for a Turtle instance named turtle): + >>> turtle.shape("square") + >>> turtle.shapesize(4,2) + >>> turtle.shearfactor(-0.5) + >>> turtle.shapetransform() + (4.0, -1.0, -0.0, 2.0) """ if t11 is t12 is t21 is t22 is None: return self._shapetrafo @@ -3069,21 +3071,21 @@ def _polytrafo(self, poly): for (x, y) in poly] def get_shapepoly(self): - """Return the current shape polygon as tuple of coordinate pairs. + """Return the points that form the outline of the current turtle shape + as a tuple of (x, y) coordinate pairs. - No argument. - - Examples (for a Turtle instance named turtle): - >>> turtle.shape("square") - >>> turtle.shapetransform(4, -1, 0, 2) - >>> turtle.get_shapepoly() - ((50, -20), (30, 20), (-50, 20), (-30, -20)) + These points can be used to define a new shape or parts of a + compound shape. + Example (for a Turtle instance named turtle): + >>> turtle.shape("square") + >>> turtle.shapetransform(4, -1, 0, 2) + >>> turtle.get_shapepoly() + ((50, -20), (30, 20), (-50, 20), (-30, -20)) """ shape = self.screen._shapes[self.turtle.shapeIndex] if shape._type == "polygon": return self._getshapepoly(shape._data, shape._type == "compound") - # else return None def _getshapepoly(self, polygon, compound=False): """Calculate transformed shape polygon according to resizemode @@ -3099,8 +3101,9 @@ def _getshapepoly(self, polygon, compound=False): return tuple((t11*x + t12*y, t21*x + t22*y) for (x, y) in polygon) def _drawturtle(self): - """Manages the correct rendering of the turtle with respect to - its shape, resizemode, stretch and tilt etc.""" + """Draw the turtle according to its shape, resizemode, stretch + factors and tilt, or hide it if it is not shown. + """ screen = self.screen shape = screen._shapes[self.turtle.shapeIndex] ttype = shape._type @@ -3139,19 +3142,15 @@ def _drawturtle(self): ############################## stamp stuff ############################### def stamp(self): - """Stamp a copy of the turtleshape onto the canvas and return its id. - - No argument. + """Stamp a copy of the turtle shape onto the canvas and return its id. - Stamp a copy of the turtle shape onto the canvas at the current - turtle position. Return a stamp_id for that stamp, which can be - used to delete it by calling clearstamp(stamp_id). + The stamp is placed at the current turtle position. The returned + id can be used to delete the stamp with clearstamp(). Example (for a Turtle instance named turtle): - >>> turtle.color("blue") - >>> turtle.stamp() - 13 - >>> turtle.fd(50) + >>> turtle.color("blue") + >>> stamp_id = turtle.stamp() + >>> turtle.fd(50) """ screen = self.screen shape = screen._shapes[self.turtle.shapeIndex] @@ -3159,9 +3158,12 @@ def stamp(self): tshape = shape._data if ttype == "polygon": stitem = screen._createpoly() - if self._resizemode == "noresize": w = 1 - elif self._resizemode == "auto": w = self._pensize - else: w =self._outlinewidth + if self._resizemode == "noresize": + w = 1 + elif self._resizemode == "auto": + w = self._pensize + else: + w =self._outlinewidth shape = self._polytrafo(self._getshapepoly(tshape)) fc, oc = self._fillcolor, self._pencolor screen._drawpoly(stitem, shape, fill=fc, outline=oc, @@ -3199,37 +3201,40 @@ def _clearstamp(self, stampid): self.undobuffer.remove(("stamp", stampid)) def clearstamp(self, stampid): - """Delete stamp with given stampid + """Delete the stamp with the given stampid. + + stampid must be the return value of a previous stamp() call. Argument: - stampid - an integer, must be return value of previous stamp() call. + stampid -- an integer Example (for a Turtle instance named turtle): - >>> turtle.color("blue") - >>> astamp = turtle.stamp() - >>> turtle.fd(50) - >>> turtle.clearstamp(astamp) + >>> turtle.color("blue") + >>> astamp = turtle.stamp() + >>> turtle.fd(50) + >>> turtle.clearstamp(astamp) """ self._clearstamp(stampid) self._update() def clearstamps(self, n=None): - """Delete all or first/last n of turtle's stamps. + """Delete all or the first or last n of the turtle's stamps. - Optional argument: - n -- an integer + If n is not given, delete all stamps. If n is positive, delete the + first n stamps. If n is negative, delete the last n stamps, so -2 + deletes the last two. - If n is None, delete all of pen's stamps, - else if n > 0 delete first n stamps - else if n < 0 delete last n stamps. + Argument: + n -- an integer or None Example (for a Turtle instance named turtle): - >>> for i in range(8): - ... turtle.stamp(); turtle.fd(30) - ... - >>> turtle.clearstamps(2) - >>> turtle.clearstamps(-2) - >>> turtle.clearstamps() + >>> for i in range(8): + ... unused_stamp_id = turtle.stamp() + ... turtle.fd(30) + ... + >>> turtle.clearstamps(2) + >>> turtle.clearstamps(-2) + >>> turtle.clearstamps() """ if n is None: toDelete = self.stampItems[:] @@ -3301,6 +3306,7 @@ def _undogoto(self, entry): cLI, cL, pl, items = coodata screen = self.screen if abs(self._position - new) > 0.5: + # XXX(Stan): Figure out what on earth this is. print ("undogoto: HALLO-DA-STIMMT-WAS-NICHT!") # restore former situation self.currentLineItem = cLI @@ -3359,7 +3365,7 @@ def _undogoto(self, entry): self._update() #count=True) def _rotate(self, angle): - """Turns pen clockwise by angle.""" + """Turn the turtle counterclockwise by angle.""" if self.undobuffer: self.undobuffer.push(("rot", angle, self._degreesPerAU)) angle *= self._degreesPerAU @@ -3406,15 +3412,19 @@ def filling(self): @contextmanager def fill(self): - """A context manager for filling a shape. - - Implicitly ensures the code block is wrapped with - begin_fill() and end_fill(). + """Fill the shape drawn in the with block. Example (for a Turtle instance named turtle): - >>> turtle.color("black", "red") - >>> with turtle.fill(): - ... turtle.circle(60) + >>> turtle.color("black", "red") + >>> with turtle.fill(): + ... turtle.circle(80) + + fill() is a context manager, so using it is the same as calling + begin_fill() before the block and end_fill() after it: + >>> turtle.color("black", "red") + >>> turtle.begin_fill() + >>> turtle.circle(80) + >>> turtle.end_fill() """ self.begin_fill() try: @@ -3423,15 +3433,15 @@ def fill(self): self.end_fill() def begin_fill(self): - """Called just before drawing a shape to be filled. + """Call just before drawing a shape to be filled. - No argument. + The shape is filled by the next call to end_fill(). Example (for a Turtle instance named turtle): - >>> turtle.color("black", "red") - >>> turtle.begin_fill() - >>> turtle.circle(60) - >>> turtle.end_fill() + >>> turtle.color("black", "red") + >>> turtle.begin_fill() + >>> turtle.circle(80) + >>> turtle.end_fill() """ if not self.filling(): self._fillitem = self.screen._createpoly() @@ -3443,15 +3453,17 @@ def begin_fill(self): self._update() def end_fill(self): - """Fill the shape drawn after the call begin_fill(). + """Fill the shape drawn after the last call to begin_fill(). - No argument. + If the shape crosses itself or several shapes overlap, whether the + overlapping parts are filled depends on the operating system, the + type of overlap and how many there are. Example (for a Turtle instance named turtle): - >>> turtle.color("black", "red") - >>> turtle.begin_fill() - >>> turtle.circle(60) - >>> turtle.end_fill() + >>> turtle.color("black", "red") + >>> turtle.begin_fill() + >>> turtle.circle(80) + >>> turtle.end_fill() """ if self.filling(): if len(self._fillpath) > 2: @@ -3503,8 +3515,7 @@ def dot(self, size=None, *color): self.pen(pen) def _write(self, txt, align, font): - """Performs the writing for write() - """ + """Performs the writing for write().""" item, end = self.screen._write(self._position, txt, align, font, self._pencolor) self._update() @@ -3516,21 +3527,21 @@ def _write(self, txt, align, font): def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")): """Write text at the current turtle position. - Arguments: - arg -- info, which is to be written to the TurtleScreen - move (optional) -- True/False - align (optional) -- one of the strings "left", "center" or right" - font (optional) -- a triple (fontname, fontsize, fonttype) + The text is the string representation of arg. align is one of + the strings "left", "center" or "right" and says how the text is + placed relative to the turtle position. font is a triple of font + name, font size and font type. If move is True, the pen is moved + to the bottom-right corner of the text. - Write text - the string representation of arg - at the current - turtle position according to align ("left", "center" or right") - and with the given font. - If move is True, the pen is moved to the bottom-right corner - of the text. By default, move is False. + Arguments: + arg -- an object + move -- a boolean + align -- a string + font -- a tuple of a string, an integer and a string Example (for a Turtle instance named turtle): - >>> turtle.write('Home = ', True, align="center") - >>> turtle.write((0,0), True) + >>> turtle.write("Home = ", True, align="center") + >>> turtle.write((0,0), True) """ with self._undo_sequence(): end = self._write(str(arg), align.lower(), font) @@ -3540,18 +3551,23 @@ def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")): @contextmanager def poly(self): - """A context manager for recording the vertices of a polygon. - - Implicitly ensures that the code block is wrapped with - begin_poly() and end_poly() - - Example (for a Turtle instance named turtle) where we create a - triangle as the polygon and move the turtle 100 steps forward: - >>> with turtle.poly(): - ... for side in range(3) - ... turtle.forward(50) - ... turtle.right(60) - >>> turtle.forward(100) + """Record the vertices of a polygon drawn in the with block. + + The first and last vertices will be connected. + + Example (for a Turtle instance named turtle): + >>> with turtle.poly(): + ... turtle.forward(100) + ... turtle.right(60) + ... turtle.forward(100) + + poly() is a context manager, so using it is the same as calling + begin_poly() before the block and end_poly() after it: + >>> turtle.begin_poly() + >>> turtle.forward(100) + >>> turtle.right(60) + >>> turtle.forward(100) + >>> turtle.end_poly() """ self.begin_poly() try: @@ -3641,7 +3657,7 @@ def onclick(self, fun, btn=1, add=None): canvas. If fun is None, existing bindings are removed. btn is the number of the mouse button and defaults to 1, the left mouse button. If add is True, a new binding is added, otherwise it - replaces a former binding. + replaces a previous binding. Arguments: fun -- a function with two arguments or None @@ -3659,47 +3675,57 @@ def onclick(self, fun, btn=1, add=None): self._update() def onrelease(self, fun, btn=1, add=None): - """Bind fun to mouse-button-release event on this turtle on canvas. + """Bind fun to mouse-button-release events on this turtle. + + fun is called with the coordinates of the point on the canvas + where the mouse button is released. If fun is None, existing + bindings are removed. btn is the number of the mouse button and + defaults to 1, the left mouse button. If add is True, a new + binding is added, otherwise it replaces a previous binding. Arguments: - fun -- a function with two arguments, to which will be assigned - the coordinates of the clicked point on the canvas. - btn -- number of the mouse-button defaults to 1 (left mouse button). + fun -- a function with two arguments or None + btn -- an integer + add -- a boolean or None Example (for a MyTurtle instance named joe): - >>> class MyTurtle(Turtle): - ... def glow(self,x,y): - ... self.fillcolor("red") - ... def unglow(self,x,y): - ... self.fillcolor("") - ... - >>> joe = MyTurtle() - >>> joe.onclick(joe.glow) - >>> joe.onrelease(joe.unglow) - - Clicking on joe turns fillcolor red, unclicking turns it to - transparent. + >>> class MyTurtle(Turtle): + ... def glow(self,x,y): + ... self.fillcolor("red") + ... + ... def unglow(self,x,y): + ... self.fillcolor("") + ... + >>> joe = MyTurtle() + >>> joe.onclick(joe.glow) + >>> joe.onrelease(joe.unglow) + + Clicking on joe turns its fill color red, releasing the mouse button + turns it transparent again. """ self.screen._onrelease(self.turtle._item, fun, btn, add) self._update() def ondrag(self, fun, btn=1, add=None): - """Bind fun to mouse-move event on this turtle on canvas. + """Bind fun to mouse-move events on this turtle. - Arguments: - fun -- a function with two arguments, to which will be assigned - the coordinates of the clicked point on the canvas. - btn -- number of the mouse-button defaults to 1 (left mouse button). + fun is called with the coordinates of the mouse pointer on the + canvas. If fun is None, existing bindings are removed. btn is the + number of the mouse button and defaults to 1, the left mouse + button. If add is True, a new binding is added, otherwise it + replaces a previous binding. Every sequence of mouse-move events on + a turtle is preceded by a mouse-click event on that turtle. - Every sequence of mouse-move-events on a turtle is preceded by a - mouse-click event on that turtle. + Arguments: + fun -- a function with two arguments or None + btn -- an integer + add -- a boolean or None Example (for a Turtle instance named turtle): - >>> turtle.ondrag(turtle.goto) + >>> turtle.ondrag(turtle.goto) - Subsequently clicking and dragging a Turtle will move it - across the screen thereby producing handdrawings (if pen is - down). + Clicking and dragging the turtle then moves it across the screen, + drawing by hand if the pen is down. """ self.screen._ondrag(self.turtle._item, fun, btn, add) @@ -3739,21 +3765,19 @@ def _undo(self, action, data): self.undobuffer.pop() def undo(self): - """undo (repeatedly) the last turtle action. + """Undo the last turtle action. - No argument. - - undo (repeatedly) the last turtle action. - Number of available undo actions is determined by the size of - the undobuffer. + Calling undo() repeatedly undoes one action at a time. The number + of actions that can be undone is set by the size of the undo + buffer, see setundobuffer(). Example (for a Turtle instance named turtle): - >>> for i in range(4): - ... turtle.fd(50); turtle.lt(80) - ... - >>> for i in range(8): - ... turtle.undo() - ... + >>> for i in range(4): + ... turtle.fd(50); turtle.lt(80) + ... + >>> for i in range(8): + ... turtle.undo() + ... """ if self.undobuffer is None: return @@ -3807,28 +3831,32 @@ def __init__(self): def setup(self, width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"]): - """ Set the size and position of the main window. + """Set the size and position of the main window. - Arguments: - width: as integer a size in pixels, as float a fraction of the screen. - Default is 50% of screen. - height: as integer the height in pixels, as float a fraction of the - screen. Default is 75% of screen. - startx: if positive, starting position in pixels from the left - edge of the screen, if negative from the right edge - Default, startx=None is to center window horizontally. - starty: if positive, starting position in pixels from the top - edge of the screen, if negative from the bottom edge - Default, starty=None is to center window vertically. + width and height are the size of the window. As integers they are + sizes in pixels, as floats they are fractions of the screen, so + 0.5 is half the screen. The defaults are 50% of the screen width + and 75% of its height. - Examples (for a Screen instance named screen): - >>> screen.setup (width=200, height=200, startx=0, starty=0) + startx and starty are the position of the window. If positive, + they are the distance in pixels from the left and top edges of the + screen, if negative from the right and bottom edges. If None, the + window is centered in that direction. - sets window to 200x200 pixels, in upper left of screen + The default values of all arguments are stored in the + configuration dictionary and can be changed with a turtle.cfg file. - >>> screen.setup(width=.75, height=0.5, startx=None, starty=None) + Arguments: + width -- an integer or a float + height -- an integer or a float + startx -- an integer or None + starty -- an integer or None - sets window to 75% of screen by 50% of screen and centers + Example (for a Screen instance named screen): + >>> screen.setup(width=200, height=200, startx=0, starty=0) + >>> # sets window to 200x200 pixels, in upper left of screen + >>> screen.setup(width=0.75, height=0.5) + >>> # sets window to 75% of screen by 50% of screen and centers """ if not hasattr(self._root, "set_geometry"): return @@ -3877,23 +3905,14 @@ def bye(self): self._destroy() def exitonclick(self): - """Go into mainloop until the mouse is clicked. - - No arguments. - - Bind bye() method to mouseclick on TurtleScreen. - If "using_IDLE" - value in configuration dictionary is False - (default value), enter mainloop. - If IDLE with -n switch (no subprocess) is used, this value should be - set to True in turtle.cfg. In this case IDLE's mainloop - is active also for the client script. + """Bind bye() to mouse clicks on the screen and start the event loop. - This is a method of the Screen-class and not available for - TurtleScreen instances. - - Example (for a Screen instance named screen): - >>> screen.exitonclick() + Clicking on the screen then closes the turtle graphics window. If + the configuration value using_IDLE is True, the event loop is not + started, see mainloop(). + This is a method of the Screen class. It is not available for + TurtleScreen objects. """ def exitGracefully(x, y): """Screen.bye() with two dummy-parameters""" @@ -3929,16 +3948,15 @@ def __init__(self, Pen = Turtle def write_docstringdict(filename="turtle_docstringdict"): - """Create and write docstring-dictionary to file. + """Write the docstring dictionary to a Python script. - Optional argument: - filename -- a string, used as filename - default value is turtle_docstringdict + The dictionary is written to the file filename.py. It is intended to + serve as a template for translating the docstrings into different + languages. This function has to be called explicitly. It is not used + by the turtle graphics classes. - Has to be called explicitly, (not used by the turtle-graphics classes) - The docstring dictionary will be written to the Python script .py - It is intended to serve as a template for translation of the docstrings - into different languages. + Argument: + filename -- a string """ docsdict = {} @@ -3998,10 +4016,10 @@ def read_docstrings(lang): def getmethparlist(ob): - """Get strings describing the arguments for the given object + """Get strings describing the arguments for the given object. Returns a pair of strings representing function parameter lists - including parenthesis. The first string is suitable for use in + including parentheses. The first string is suitable for use in function definition and the second is suitable for use in function call. The "self" parameter is not included. """ diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 4247b9a4b07fa3..f6c94060054e48 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -125,6 +125,13 @@ def _coerce_args(*args): # - encoding function otherwise str_input = None for arg in args: + if not (arg is None or isinstance(arg, str) or hasattr(arg, 'decode')): + if arg: + raise TypeError("Expected str, bytes or None, " + f"not {type(arg).__name__}") + warnings.warn(f"Accepting {type(arg).__name__} objects with false " + f"value in urllib.parse is deprecated as of 3.16", + DeprecationWarning, stacklevel=3) if arg: if str_input is None: str_input = isinstance(arg, str) @@ -140,6 +147,27 @@ def _coerce_args(*args): return args + (_noop,) return _decode_args(args) + (_encode_result,) +def _parse_hostinfo(netloc): + _, _, hostinfo = netloc.rpartition('@') + _, have_open_br, bracketed = hostinfo.partition('[') + if have_open_br: + hostname, _, port = bracketed.partition(']') + _, _, port = port.partition(':') + else: + hostname, _, port = hostinfo.partition(':') + if not port: + port = None + return hostname, port + +def _validate_port(port): + if port.isdigit() and port.isascii(): + port = int(port) + else: + raise ValueError(f"Port could not be cast to integer value as {port!r}") + if not (0 <= port <= 65535): + raise ValueError("Port out of range 0-65535") + return port + # Result objects are more helpful than simple tuples class _ResultMixinStr(object): """Standard approach to encoding parsed results from str to bytes""" @@ -198,12 +226,7 @@ def hostname(self): def port(self): port = self._hostinfo[1] if port is not None: - if port.isdigit() and port.isascii(): - port = int(port) - else: - raise ValueError(f"Port could not be cast to integer value as {port!r}") - if not (0 <= port <= 65535): - raise ValueError("Port out of range 0-65535") + port = _validate_port(port) return port __class_getitem__ = classmethod(types.GenericAlias) @@ -231,16 +254,7 @@ def _hostinfo(self): netloc = self.netloc if netloc is None: return None, None - _, _, hostinfo = netloc.rpartition('@') - _, have_open_br, bracketed = hostinfo.partition('[') - if have_open_br: - hostname, _, port = bracketed.partition(']') - _, _, port = port.partition(':') - else: - hostname, _, port = hostinfo.partition(':') - if not port: - port = None - return hostname, port + return _parse_hostinfo(netloc) class _NetlocResultMixinBytes(_NetlocResultMixinBase, _ResultMixinBytes): @@ -506,9 +520,7 @@ def _splitnetloc(url, start=0): delim = min(delim, wdelim) # use earliest delim position return url[start:delim], url[delim:] # return (domain, rest) -def _checknetloc(netloc): - if not netloc or netloc.isascii(): - return +def _checknetloc_nfkc(netloc): # looking for characters like \u2100 that expand to 'a/c' # IDNA uses NFKC equivalence, so normalize for this check import unicodedata @@ -524,6 +536,16 @@ def _checknetloc(netloc): raise ValueError("netloc '" + netloc + "' contains invalid " + "characters under NFKC normalization") +def _checknetloc(netloc): + if not netloc: + return + if not netloc.isascii(): + _checknetloc_nfkc(netloc) + + _, port = _parse_hostinfo(netloc) + if port is not None: + _validate_port(port) + def _check_bracketed_netloc(netloc): # Note that this function must mirror the splitting # done in NetlocResultMixins._hostinfo(). @@ -697,12 +719,12 @@ def _urlunsplit(scheme, netloc, url, query, fragment): def urljoin(base, url, allow_fragments=True): """Join a base URL and a possibly relative URL to form an absolute interpretation of the latter.""" + base, url, _coerce_result = _coerce_args(base, url) if not base: - return url + return _coerce_result(url) if not url: - return base + return _coerce_result(base) - base, url, _coerce_result = _coerce_args(base, url) bscheme, bnetloc, bpath, bquery, bfragment = \ _urlsplit(base, None, allow_fragments) scheme, netloc, path, query, fragment = \ diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index bcec9f795b0c9f..26c27fd5b2c3ab 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -101,7 +101,8 @@ from urllib.parse import ( urlparse, urlsplit, urljoin, unwrap, quote, unquote, _splittype, _splithost, _splitport, _splituser, _splitpasswd, - _splitattr, _splitvalue, _splittag, + _splitattr, _splitvalue, _splittag, _splitnetloc, + _WHATWG_C0_CONTROL_OR_SPACE, _UNSAFE_URL_BYTES_TO_REMOVE, unquote_to_bytes, urlunparse) from urllib.response import addinfourl, addclosehook @@ -356,8 +357,10 @@ def get_full_url(self): return self.full_url def set_proxy(self, host, type): - if self.type == 'https' and not self._tunnel_host: - self._tunnel_host = self.host + if self.type == 'https': + # A retried request keeps the tunnel host set on the first pass. + if not self._tunnel_host: + self._tunnel_host = self.host else: self.type= type self.selector = self.full_url @@ -1048,6 +1051,9 @@ class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler): auth_header = 'Proxy-authorization' def http_error_407(self, req, fp, code, msg, headers): + # gh-158907: a 407 from a direct origin is not a proxy challenge + if not req.has_proxy() and not req._tunnel_host: + return None # http_error_auth_reqed requires that there is no userinfo component in # authority. Assume there isn't one, since urllib.request does not (and # should not, RFC 3986 s. 3.2.1) support requests for URLs containing @@ -1227,10 +1233,19 @@ def get_authorization(self, req, chal): # HTTP proxy. RFC 7616, Section 3.4 allows both. An empty path is # sent as '/' by http.client, so use the same here. - uri = req.selector or '/' + if self.auth_header == 'Proxy-Authorization' and req._tunnel_host: + # For an https URL the proxy authenticates the CONNECT request, + # whose request-target is host:port, as sent by http.client. + method = 'CONNECT' + host, port = _splitport(req._tunnel_host) + uri = '%s:%s' % (host.encode('idna').decode('ascii'), + port or http.client.HTTPS_PORT) + else: + method = req.get_method() + uri = req.selector or '/' A1 = "%s:%s:%s" % (user, realm, pw) - A2 = "%s:%s" % (req.get_method(), uri) + A2 = "%s:%s" % (method, uri) # NOTE: As per RFC 2617, when server sends "auth,auth-int", the client could use either `auth` # or `auth-int` to the response back. we use `auth` to send the response back. if qop is None: @@ -1307,12 +1322,35 @@ class ProxyDigestAuthHandler(BaseHandler, AbstractDigestAuthHandler): handler_order = 490 # before Basic auth def http_error_407(self, req, fp, code, msg, headers): + # gh-158907: a 407 from a direct origin is not a proxy challenge + if not req.has_proxy() and not req._tunnel_host: + return None host = req.host retry = self.http_error_auth_reqed('proxy-authenticate', host, req, headers) self.reset_retry_count() return retry +class _ProxyTunnelResponse(io.BytesIO): + """The proxy's error response to CONNECT, as an empty-bodied response.""" + + def __init__(self, err, url): + super().__init__() + self.url = url + self.code = self.status = err.code + self.msg = self.reason = err.reason + self.headers = err.headers + + def info(self): + return self.headers + + def geturl(self): + return self.url + + def getcode(self): + return self.code + + class AbstractHTTPHandler(BaseHandler): def __init__(self, debuglevel=None): @@ -1406,6 +1444,12 @@ def do_open(self, http_class, req, **http_conn_args): try: h.request(req.get_method(), req.selector, req.data, headers, encode_chunked=req.has_header('Transfer-encoding')) + except http.client.ProxyTunnelError as err: + if err.code != 407: + raise URLError(err) + # Pass the proxy's challenge to CONNECT on to the + # http_error_407 handlers, like any other error response. + return _ProxyTunnelResponse(err, req.full_url) except OSError as err: # timeout error raise URLError(err) r = h.getresponse() @@ -1753,7 +1797,22 @@ def url2pathname(url, *, require_scheme=False, resolve_host=False): """ if not require_scheme: url = 'file:' + url - scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment. + # Match urlsplit()'s own control-character handling up front, so the + # except branch below can reuse this same cleaned-up string. + url = url.lstrip(_WHATWG_C0_CONTROL_OR_SPACE) + for b in _UNSAFE_URL_BYTES_TO_REMOVE: + url = url.replace(b, '') + try: + scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment. + except ValueError: + # The authority here is not a real host[:port] pair -- it may + # hold a Windows drive letter (e.g. 'C:\\') -- so urlsplit()'s + # eager port check doesn't apply; split without it instead. + scheme, _, url = url.partition(':') + scheme = scheme.lower() + authority = '' + if url[:2] == '//': + authority, url = _splitnetloc(url, 2) if scheme != 'file': raise URLError("URL is missing a 'file:' scheme") if os.name == 'nt': diff --git a/Lib/urllib/robotparser.py b/Lib/urllib/robotparser.py index 985333c7100438..d50cb70e03813e 100644 --- a/Lib/urllib/robotparser.py +++ b/Lib/urllib/robotparser.py @@ -30,6 +30,8 @@ class RobotFileParser: def __init__(self, url=''): self.entries = [] self.groups = {} + # Merged entries in self.groups -> number of agents sharing them. + self._merged_refs = {} self.sitemaps = [] self.default_entry = None self.disallow_all = False @@ -90,12 +92,32 @@ def read(self): def _add_entry(self, entry): self.entries.append(entry) + to_merge = {} for agent in entry.useragents: agent = agent.lower() - if agent not in self.groups: + old = self.groups.get(agent) + if old is None: self.groups[agent] = entry - else: - self.groups[agent] = merge_entries(self.groups[agent], entry) + elif old is not entry: + to_merge.setdefault(old, set()).add(agent) + if not to_merge: + return + refs = self._merged_refs + entry_agents = set(entry.useragents) + for old, agents in to_merge.items(): + if refs.get(old) == len(agents): + # No other agent shares this merged entry, so it can be + # extended in place instead of being copied again. + _merge_into(old, entry, entry_agents) + continue + new = merge_entries(old, entry, entry_agents) + for agent in agents: + self.groups[agent] = new + refs[new] = len(agents) + if old in refs: + refs[old] -= len(agents) + if not refs[old]: + del refs[old] def parse(self, lines): """Parse the input lines from a robots.txt file. @@ -174,7 +196,8 @@ def _find_entry(self, useragent): entry = self.groups.get(useragent.lower()) if entry is not None: return entry - for entry in self.groups.values(): + # Many agents can share the same entry; check each entry only once. + for entry in dict.fromkeys(self.groups.values()): if entry.applies_to(useragent): return entry return self.groups.get('*') @@ -361,10 +384,21 @@ def translate_pattern(path): parts[-1] = f'.*{parts[-1]}' return ''.join(parts) -def merge_entries(e1, e2): +def merge_entries(e1, e2, e2_agents=None): entry = Entry() - entry.useragents = list(filter(set(e2.useragents).__contains__, e1.useragents)) - entry.rulelines = e1.rulelines + e2.rulelines - entry.delay = e1.delay if e2.delay is None else e2.delay - entry.req_rate = e1.req_rate if e2.req_rate is None else e2.req_rate + entry.useragents = e1.useragents + entry.rulelines = e1.rulelines.copy() + entry.delay = e1.delay + entry.req_rate = e1.req_rate + _merge_into(entry, e2, e2_agents) return entry + +def _merge_into(e1, e2, e2_agents=None): + if e2_agents is None: + e2_agents = set(e2.useragents) + e1.useragents = list(filter(e2_agents.__contains__, e1.useragents)) + e1.rulelines.extend(e2.rulelines) + if e2.delay is not None: + e1.delay = e2.delay + if e2.req_rate is not None: + e1.req_rate = e2.req_rate diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index 620168be16862e..0e9ee18892b933 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -141,6 +141,12 @@ def ensure_directories(self, env_dir): """ def create_if_needed(d): + path = env_dir + for part in os.path.relpath(d, env_dir).split(os.sep)[:-1]: + path = os.path.join(path, part) + if os.path.islink(path) or os.path.isjunction(path): + raise ValueError(f'Refusing to create a venv in {env_dir} ' + f'because {path} is a link.') if not os.path.exists(d): os.makedirs(d) elif os.path.islink(d) or os.path.isfile(d): @@ -601,11 +607,10 @@ def skip_file(f): 'may be binary: %s', srcfile, e) continue if new_data == data: - shutil.copy(srcfile, dstfile) + shutil.copyfile(srcfile, dstfile) else: with open(dstfile, 'wb') as f: f.write(new_data) - shutil.copymode(srcfile, dstfile) def upgrade_dependencies(self, context): logger.debug( diff --git a/Lib/wsgiref/validate.py b/Lib/wsgiref/validate.py index 1a1853cd63a0d2..cc572e8a179d1e 100644 --- a/Lib/wsgiref/validate.py +++ b/Lib/wsgiref/validate.py @@ -334,7 +334,7 @@ def check_environ(environ): # @@: these need filling out: if environ['REQUEST_METHOD'] not in ( - 'GET', 'HEAD', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE', 'TRACE'): + 'GET', 'HEAD', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE', 'QUERY', 'TRACE'): warnings.warn( "Unknown REQUEST_METHOD: %r" % environ['REQUEST_METHOD'], WSGIWarning) diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index f83cd2898daa3e..fb6c1462fc5e9e 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -300,6 +300,27 @@ def __exit__(self, et, ev, tb): defproperty(Node, "localName", doc="Namespace-local name of this node.") +# PubidChar (see XML 1.0, 2.3, [13]). +_PUBID_CHARS = frozenset( + " \r\n-'()+,./:=?;!*#@$_%" + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz") + + +def _quote_literal(text): + """Quote a system literal or a public identifier. + + A literal is delimited with a quote which it does not contain + (see XML 1.0, 2.3, [11] and [12]). + """ + if "'" not in text: + return "'%s'" % text + if '"' not in text: + return '"%s"' % text + raise ValueError("a literal cannot contain both quotes: %r" % (text,)) + + def _check_name(name): if not xml.is_valid_name(name): raise xml.dom.InvalidCharacterErr( @@ -1228,7 +1249,20 @@ def _set_nodeName(self, value): nodeName = property(_get_nodeName, _set_nodeName) def writexml(self, writer, indent="", addindent="", newl=""): - writer.write("%s%s" % (indent,self.target, self.data, newl)) + target = self.target + # PITarget is a Name, except the names matching "xml" in any case + # (see XML 1.0, 2.6, [17]). + _check_name(target) + if target.lower() == "xml": + raise ValueError("%r is a reserved processing instruction target" + % (target,)) + data = self.data + # The data cannot contain "?>", which ends the instruction + # (see XML 1.0, 2.6, [16]). + if "?>" in data: + raise ValueError("'?>' is not allowed in a processing " + "instruction node") + writer.write("%s%s" % (indent, target, data, newl)) class CharacterData(Childless, Node): @@ -1423,9 +1457,14 @@ def __init__(self, data): self._data = data def writexml(self, writer, indent="", addindent="", newl=""): - if "--" in self.data: + data = self.data + # The data cannot contain "--", and cannot end with "-", which + # would be fused with the closing "-->" (see XML 1.0, 2.5, [15]). + if "--" in data: raise ValueError("'--' is not allowed in a comment node") - writer.write("%s%s" % (indent, self.data, newl)) + if data.endswith("-"): + raise ValueError("a comment node cannot end with '-'") + writer.write("%s%s" % (indent, data, newl)) class CDATASection(Text): @@ -1571,10 +1610,20 @@ def writexml(self, writer, indent="", addindent="", newl=""): writer.write(" zinfo._end_offset): if zinfo._end_offset == zinfo.header_offset: @@ -2319,7 +2326,9 @@ def _open_to_write(self, zinfo, force_zip64=False): zinfo.compress_size = 0 zinfo.CRC = 0 - zinfo.flag_bits = _MASK_UTF_FILENAME + zinfo.flag_bits &= _MASK_UTF_FILENAME + if not zinfo.filename.isascii(): + zinfo.flag_bits |= _MASK_UTF_FILENAME if zinfo.compress_type == ZIP_LZMA: # Compressed data includes an end-of-stream (EOS) marker zinfo.flag_bits |= _MASK_COMPRESS_OPTION_1 @@ -2926,6 +2935,10 @@ def _get_code(self, pathname, basename): def main(args=None): import argparse + import runpy + + # Prevent shadowing stdlib modules from extracted files + runpy._remove_sys_path0() description = 'A simple command-line interface for zipfile module.' parser = argparse.ArgumentParser(description=description) diff --git a/Makefile.pre.in b/Makefile.pre.in index e74deee353488f..c265f9b3e1e44c 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -135,7 +135,7 @@ PY_CORE_CFLAGS= $(PY_STDMODULE_CFLAGS) -DPy_BUILD_CORE # LDFLAGS used to build executables and shared targets. PY_CORE_LDFLAGS=$(PY_LDFLAGS) $(PY_LDFLAGS_NODIST) CONFIGURE_EXE_LDFLAGS=@EXE_LDFLAGS@ -PY_CORE_EXE_LDFLAGS:= $(if $(CONFIGURE_EXE_LDFLAGS), $(CONFIGURE_EXE_LDFLAGS) $(PY_LDFLAGS_NODIST), $(PY_CORE_LDFLAGS)) +PY_CORE_EXE_LDFLAGS= $(if $(CONFIGURE_EXE_LDFLAGS), $(CONFIGURE_EXE_LDFLAGS) $(PY_LDFLAGS_NODIST), $(PY_CORE_LDFLAGS)) # Strict or non-strict aliasing flags used to compile dtoa.c, see above CFLAGS_ALIASING=@CFLAGS_ALIASING@ # Compilation flags only for ceval.c. @@ -242,6 +242,7 @@ LIPO_INTEL64_FLAGS=@LIPO_INTEL64_FLAGS@ # Environment to run shared python without installed libraries RUNSHARED= @RUNSHARED@ +RUNSHARED_FOR_BUILD= @RUNSHARED_FOR_BUILD@ # ensurepip options ENSUREPIP= @ENSUREPIP@ @@ -331,6 +332,7 @@ HOSTRUNNER= @HOSTRUNNER@ PYTHON_FOR_REGEN?=@PYTHON_FOR_REGEN@ UPDATE_FILE=$(PYTHON_FOR_REGEN) $(srcdir)/Tools/build/update_file.py PYTHON_FOR_BUILD=@PYTHON_FOR_BUILD@ +MODULE_CHECK_COMMAND=@MODULE_CHECK_COMMAND@ # Single-platform builds depend on $(BUILDPYTHON). Cross builds use an # external "build Python" and have an empty PYTHON_FOR_BUILD_DEPS. PYTHON_FOR_BUILD_DEPS=@PYTHON_FOR_BUILD_DEPS@ @@ -1013,7 +1015,7 @@ $(BUILDPYTHON): Programs/python.o $(PLATFORM_MAIN_OBJS) $(LINK_PYTHON_DEPS) $(LINKCC) $(PY_CORE_EXE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(PLATFORM_MAIN_OBJS) $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS) platform: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt - $(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform # Create build directory and generate the sysconfig build-time data there. # pybuilddir.txt contains the name of the build dir and is used for @@ -1024,7 +1026,7 @@ platform: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt # or removed in case of failure. pybuilddir.txt: $(PYTHON_FOR_BUILD_DEPS) @echo "none" > ./pybuilddir.txt - $(RUNSHARED) $(PYTHON_FOR_BUILD) -S -X pathconfig_warnings=0 -m sysconfig --generate-posix-vars ;\ + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -S -X pathconfig_warnings=0 -m sysconfig --generate-posix-vars ;\ if test $$? -ne 0 ; then \ echo "generate-posix-vars failed" ; \ rm -f ./pybuilddir.txt ; \ @@ -1032,7 +1034,7 @@ pybuilddir.txt: $(PYTHON_FOR_BUILD_DEPS) fi $(BUILD_DETAILS): pybuilddir.txt - $(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/generate-build-details.py `cat pybuilddir.txt`/$(BUILD_DETAILS) + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/generate-build-details.py `cat pybuilddir.txt`/$(BUILD_DETAILS) # Build static library $(LIBRARY): $(LIBRARY_OBJS) @@ -1604,11 +1606,11 @@ sharedmods: $(SHAREDMODS) pybuilddir.txt .PHONY: checksharedmods checksharedmods: sharedmods $(PYTHON_FOR_BUILD_DEPS) $(BUILDPYTHON) @if [ -n "@MISSING_STDLIB_CONFIG@" ]; then \ - $(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-missing-stdlib-info --with-missing-stdlib-config="@MISSING_STDLIB_CONFIG@"; \ + $(MODULE_CHECK_COMMAND) --generate-missing-stdlib-info --with-missing-stdlib-config="@MISSING_STDLIB_CONFIG@"; \ else \ - $(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --generate-missing-stdlib-info; \ + $(MODULE_CHECK_COMMAND) --generate-missing-stdlib-info; \ fi - @$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py + @$(MODULE_CHECK_COMMAND) .PHONY: rundsymutil rundsymutil: sharedmods $(PYTHON_FOR_BUILD_DEPS) $(BUILDPYTHON) @@ -1915,6 +1917,7 @@ regen-unicodedata: # Regenerate all generated files # "clinic" is regenerated implicitly via "regen-global-objects". +# Keep the list of targets to run manually in sync with Doc/using/configure.rst. .PHONY: regen-all regen-all: regen-cases regen-slots \ regen-token regen-ast regen-keyword regen-sre regen-frozen \ @@ -2436,7 +2439,7 @@ install: @FRAMEWORKINSTALLFIRST@ @INSTALLTARGETS@ @FRAMEWORKINSTALLLAST@ upgrade) ensurepip="--upgrade" ;; \ install|*) ensurepip="" ;; \ esac; \ - $(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \ + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -m ensurepip \ $$ensurepip --root=$(DESTDIR)/ ; \ fi @@ -2447,7 +2450,7 @@ altinstall: commoninstall upgrade) ensurepip="--altinstall --upgrade" ;; \ install|*) ensurepip="--altinstall" ;; \ esac; \ - $(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \ + $(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -m ensurepip \ $$ensurepip --root=$(DESTDIR)/ ; \ fi @@ -2483,7 +2486,7 @@ sharedinstall: all # Install the interpreter with $(VERSION) affixed # This goes into $(exec_prefix) .PHONY: altbininstall -altbininstall: $(BUILDPYTHON) @FRAMEWORKPYTHONW@ +altbininstall: all @FRAMEWORKPYTHONW@ @for i in $(BINDIR) $(LIBDIR); \ do \ if test ! -d $(DESTDIR)$$i; then \ @@ -2913,12 +2916,12 @@ libinstall: all $(srcdir)/Modules/xxmodule.c patch --force --reject-file "$(abs_builddir)/app-store-compliance.rej" --strip 2 --directory "$(DESTDIR)$(LIBDEST)" --input "$(abs_srcdir)/$(APP_STORE_COMPLIANCE_PATCH)" || true ; \ fi @ # Build PYC files for the 3 optimization levels (0, 1, 2) - -PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \ + -PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED_FOR_BUILD) \ $(PYTHON_FOR_BUILD) -Wi $(DESTDIR)$(LIBDEST)/compileall.py \ -o 0 -o 1 -o 2 $(COMPILEALL_OPTS) -d $(LIBDEST) -f \ -x 'bad_coding|badsyntax|site-packages' \ $(DESTDIR)$(LIBDEST) - -PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \ + -PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED_FOR_BUILD) \ $(PYTHON_FOR_BUILD) -Wi $(DESTDIR)$(LIBDEST)/compileall.py \ -o 0 -o 1 -o 2 $(COMPILEALL_OPTS) -d $(LIBDEST)/site-packages -f \ -x badsyntax $(DESTDIR)$(LIBDEST)/site-packages @@ -3138,7 +3141,7 @@ frameworkinstallunversionedstructure: $(LDLIBRARY) rm -rf $(DESTDIR)$(PYTHONFRAMEWORKPREFIX)/include; \ fi $(INSTALL) -d -m $(DIRMODE) $(DESTDIR)$(PYTHONFRAMEWORKINSTALLDIR) - sed 's/%VERSION%/'"`$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import platform; print(platform.python_version())'`"'/g' < $(RESSRCDIR)/Info.plist > $(DESTDIR)$(PYTHONFRAMEWORKINSTALLDIR)/Info.plist + sed 's/%VERSION%/'"`$(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) -c 'import platform; print(platform.python_version())'`"'/g' < $(RESSRCDIR)/Info.plist > $(DESTDIR)$(PYTHONFRAMEWORKINSTALLDIR)/Info.plist $(INSTALL_SHARED) $(LDLIBRARY) $(DESTDIR)$(PYTHONFRAMEWORKPREFIX)/$(LDLIBRARY) $(INSTALL) -d -m $(DIRMODE) $(DESTDIR)$(LIBDIR) $(LN) -fs "../$(LDLIBRARY)" "$(DESTDIR)$(prefix)/lib/libpython$(LDVERSION).dylib" @@ -3524,7 +3527,7 @@ MODULE__CTYPES_MALLOC_CLOSURE=@MODULE__CTYPES_MALLOC_CLOSURE@ MODULE__ELEMENTTREE_DEPS=$(srcdir)/Modules/pyexpat.c @LIBEXPAT_INTERNAL@ MODULE__HASHLIB_DEPS=$(srcdir)/Modules/hashlib.h $(srcdir)/Modules/_openssl_mem.h MODULE__IO_DEPS=$(srcdir)/Modules/_io/_iomodule.h -MODULE__REMOTE_DEBUGGING_DEPS=$(srcdir)/Modules/_remote_debugging/_remote_debugging.h $(srcdir)/Modules/_remote_debugging/gc_stats.h +MODULE__REMOTE_DEBUGGING_DEPS=$(srcdir)/Modules/_remote_debugging/_remote_debugging.h $(srcdir)/Modules/_remote_debugging/binary_io.h $(srcdir)/Modules/_remote_debugging/gc_stats.h # HACL*-based cryptographic primitives MODULE__MD5_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_MD5_HEADERS) $(LIBHACL_MD5_LIB_@LIBHACL_LDEPS_LIBTYPE@) diff --git a/Misc/ACKS b/Misc/ACKS index 65f6f423eb7c42..c3ecfc1e315a59 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -117,6 +117,7 @@ Cesar Eduardo Barros Des Barry Emanuel Barry Ulf Bartelt +Tim Bartlett Campbell Barton Don Bashford Pior Bastida @@ -1270,6 +1271,7 @@ Rihaan Meher Bill van Melle Lucas Prado Melo Ezio Melotti +Ashwin Menon Doug Mennella Dimitri Merejkowsky Brian Merrell @@ -2048,6 +2050,7 @@ Sjoerd de Vries Jonas Wagner Daniel Wagner-Hall Niki W. Waibel +WakaTaira Wojtek Walczak Charles Waldman Richard Walker diff --git a/Misc/NEWS.d/3.16.0a2.rst b/Misc/NEWS.d/3.16.0a2.rst new file mode 100644 index 00000000000000..629953ebd57429 --- /dev/null +++ b/Misc/NEWS.d/3.16.0a2.rst @@ -0,0 +1,4357 @@ +.. cve: CVE-2026-115791 +.. date: 2026-11-07-15-56-47 +.. gh-issue: 161705 +.. nonce: OjDK2d +.. release date: 2026-11-10 +.. section: Security + +Remove ``sys.path[0]`` in :mod:`gzip`, :mod:`tarfile` and :mod:`zipfile` +command line interface to prevent shadowing standard library modules from +extracted files. Issue reported by Jelle Zijlstra, patch by Gregory P. Smith +and Victor Stinner. + +.. + +.. cve: CVE-2026-124249 +.. date: 2026-11-07-01-43-49 +.. gh-issue: 161569 +.. nonce: cookiej +.. section: Security + +Fix :class:`http.cookiejar.CookieJar` deleting a stored cookie when a +response from an unrelated host sets an already expired cookie with a +``Domain`` or ``Path`` attribute pointing at that cookie. The cookie policy +is now consulted before an expired cookie is allowed to delete an existing +one. + +.. + +.. cve: CVE-2026-19552 +.. date: 2026-11-06-16-22-47 +.. gh-issue: 161503 +.. nonce: FkIl87 +.. section: Security + +:func:`ssl.get_server_certificate` now verifies that the server hostname +matches its certificate when the caller specifies *ca_certs*. + +.. + +.. date: 2026-11-02-18-30-09 +.. gh-issue: 161192 +.. nonce: 2ScSRO +.. section: Security + +Update bundled `libexpat `_ to version 2.10.0 +for the fixes to :cve:`2026-113351`, :cve:`2026-115506`, :cve:`2026-117610` +and :cve:`2026-118370`. + +.. + +.. cve: CVE-2026-116566 +.. date: 2026-10-27-16-25-40 +.. gh-issue: 160898 +.. nonce: epi53X +.. section: Security + +:func:`socket.getaddrinfo` now rejects null bytes within *host* and *port* +parameters as this previously caused the values to be silently truncated. + +.. + +.. cve: CVE-2026-115559 +.. date: 2026-10-22-21-59-39 +.. gh-issue: 160369 +.. nonce: imapTLS +.. section: Security + +:meth:`imaplib.IMAP4.starttls` now rejects unexpected plaintext buffered +after the STARTTLS response, preventing it from being processed as +TLS-protected data. + +.. + +.. date: 2026-10-22-11-09-30 +.. gh-issue: 160279 +.. nonce: 9BtxeF +.. section: Security + +Prevent :mod:`venv` from importing modules from the root of an existing +virtual environment when installing or upgrading pip. + +.. + +.. date: 2026-10-22-08-08-57 +.. gh-issue: 160499 +.. nonce: pf9H9g +.. section: Security + +Fix unbounded memory growth in :mod:`asyncio` TLS connections when a peer +stops reading but keeps sending records that make OpenSSL generate its own +replies (such as TLS 1.2 renegotiation alerts). Reading from the peer is +now paused when this output accumulates, instead of only when application +data does. + +.. + +.. date: 2026-10-22-08-08-23 +.. gh-issue: 160506 +.. nonce: rN4dLq +.. section: Security + +Fix :func:`os.path.realpath` on Windows returning the wrong path for a +broken relative symbolic link reached through a symbolic link to a +directory. This allowed :mod:`tarfile` extraction with the ``'data'`` or +``'tar'`` filter to create a file outside the destination directory. + +.. + +.. date: 2026-10-22-08-07-52 +.. gh-issue: 161323 +.. nonce: q7Drv2 +.. section: Security + +On Windows, the ``'tar'`` and ``'data'`` :mod:`tarfile` extraction filters +now refuse member names and link targets that have a drive but are not +absolute, such as ``C:foo``, with :exc:`~tarfile.AbsolutePathError` or +:exc:`~tarfile.AbsoluteLinkError`. Such members could be extracted outside +the destination directory. Fixes :cve:`2026-107784`. + +.. + +.. date: 2026-10-22-08-05-32 +.. gh-issue: 160420 +.. nonce: tZ9dRv +.. section: Security + +Fix :class:`zoneinfo.ZoneInfo` on Windows accepting keys in which a +component after a ``/`` starts with a drive letter, such as +``America/C:\file``. Such a key could open a file outside the ``tzdata`` +package; it now raises :exc:`ValueError`. + +.. + +.. date: 2026-10-22-08-04-33 +.. gh-issue: 160300 +.. nonce: q7Zk2B +.. section: Security + +Fix :class:`http.cookiejar.DefaultCookiePolicy` accepting and returning +cookies with a negative ``Version`` attribute without checking their domain +against the request host. Such cookies are now ignored. + +.. + +.. cve: CVE-2026-114583 +.. date: 2026-10-21-19-58-53 +.. gh-issue: 160097 +.. nonce: -opfAL +.. section: Security + +:func:`tarfile.tar_filter` now rejects link members whose path resolves to +the destination directory itself, as :func:`tarfile.data_filter` already +did. + +.. + +.. cve: CVE-2026-115557 +.. date: 2026-10-21-16-55-07 +.. gh-issue: 160040 +.. nonce: 9PjFjC +.. section: Security + +:mod:`ssl` now rejects TLS certificate chains that contain any of a set of +compromised eMudhra intermediate CA keys. This is an extraordinary measure +and a last line of defense: removing trust is primarily the job of the +platform root store, and the built-in denylist only blocks connections while +that update has not yet reached the OS, a container image, or a bundled +trust store in various Python packages. Entries are added to the denylist +only under a very high threshold. The denylist matches on the public key +(the SHA-256 digest of the :rfc:`5280` ``SubjectPublicKeyInfo``), so it also +covers the cross-signed certificates that reuse one of the distrusted keys. + +.. + +.. date: 2026-10-20-13-15-00 +.. gh-issue: 159894 +.. nonce: 8Kp4mN +.. section: Security + +Bump the version of :pypi:`pip` bundled in :mod:`ensurepip` to version +26.2.2. + +.. + +.. date: 2026-10-15-03-47-08 +.. gh-issue: 159428 +.. nonce: XHRKwY +.. section: Security + +:mod:`mimetypes` no longer reads the files listed in +:data:`mimetypes.knownfiles` by default on Windows, and uses only the +registry. + +.. + +.. date: 2026-10-06-12-30-00 +.. gh-issue: 158928 +.. nonce: Zd9Ybv +.. section: Security + +Fix a buffer overflow when initializing a :class:`bytearray` from an +iterator. + +.. + +.. date: 2026-08-31-16-47-33 +.. gh-issue: 157953 +.. nonce: KxCFSN +.. section: Security + +Update bundled `libexpat `_ to version 2.9.0. + +.. + +.. date: 2026-11-09-14-30-00 +.. gh-issue: 160267 +.. nonce: sTfrEe +.. section: Core and Builtins + +Fix a crash when :mod:`profiling.sampling` profiles a process that creates +and destroys threads or subinterpreters quickly. Memory of deleted thread +and interpreter states is now reused for new states instead of being freed +until the runtime is finalized. + +.. + +.. date: 2026-11-07-09-30-00 +.. gh-issue: 161420 +.. nonce: mIdFre +.. section: Core and Builtins + +Restore ``mimalloc``'s previous behavior of processing delayed frees from +other threads on every generic malloc call, reverting the change introduced +in v2.2.2. + +.. + +.. date: 2026-11-06-21-21-04 +.. gh-issue: 161242 +.. nonce: d4vNgC +.. section: Core and Builtins + +Speed up :exc:`AttributeError`, :exc:`ImportError` and :exc:`NameError` by +enabling :ref:`vectorcall`. Unexpected keyword arguments are now reported by +name. + +.. + +.. date: 2026-11-06-15-15-00 +.. gh-issue: 161433 +.. nonce: A9b3Kq +.. section: Core and Builtins + +Add context notes to :exc:`TypeError` raised while hashing a :class:`tuple`, +identifying the index of the item whose hash failed. + +.. + +.. date: 2026-11-06-02-30-00 +.. gh-issue: 129724 +.. nonce: Mn8sLq +.. section: Core and Builtins + +Fix the :mod:`marshal` output of compiled code, and so ``.pyc`` files, +differing for the same source depending on what ran before in the process. +This affected private names in classes with non-ASCII names, and +:func:`compile` of an AST with names not created by :func:`ast.parse`. + +.. + +.. date: 2026-11-05-21-00-57 +.. gh-issue: 161366 +.. nonce: ouxAvv +.. section: Core and Builtins + +Fix a deadlock in the garbage collector of the :term:`free-threaded build` +when :data:`gc.DEBUG_COLLECTABLE` is set and another thread is writing to +:data:`sys.stderr`. The debug output is now written after the other threads +are resumed. + +.. + +.. date: 2026-11-05-10-19-48 +.. gh-issue: 161419 +.. nonce: wc4DV0 +.. section: Core and Builtins + +Fix a crash in the :term:`free-threaded build` where a weak reference could +revive an object whose finalizer had already run during deallocation, +causing it to be deallocated twice. + +.. + +.. date: 2026-11-03-20-28-15 +.. gh-issue: 161288 +.. nonce: LikC_U +.. section: Core and Builtins + +Fix a crash in the perf trampoline (:option:`-X perf <-X>`) on builds with +clang's ``-fsanitize=function``, which ``-fsanitize=undefined`` enables +since clang 17. + +.. + +.. date: 2026-11-03-13-44-26 +.. gh-issue: 159184 +.. nonce: X_sOdY +.. section: Core and Builtins + +Fix a leak of NaN constants in code objects created by :func:`marshal.loads` +on the free-threaded build. + +.. + +.. date: 2026-11-03-05-52-49 +.. gh-issue: 161229 +.. nonce: 8pOPOI +.. section: Core and Builtins + +:func:`!__lazy_import__` now treats ``None`` for *globals* and *locals* the +same as omitting them, matching its signature. + +.. + +.. date: 2026-11-02-05-50-35 +.. gh-issue: 160872 +.. nonce: x8NvzN +.. section: Core and Builtins + +Optimize division and remainder operations on large :class:`int` values. + +.. + +.. date: 2026-10-28-13-47-43 +.. gh-issue: 160773 +.. nonce: 7VFxkA +.. section: Core and Builtins + +Fix a crash in :func:`str` of an :exc:`OSError` when formatting one of its +attributes modifies the others. + +.. + +.. date: 2026-10-26-12-00-00 +.. gh-issue: 160682 +.. nonce: Q7mK2x +.. section: Core and Builtins + +Fix a bug in the :term:`free-threaded build` where garbage collection could +prematurely collect the function object of a running method. + +.. + +.. date: 2026-10-24-19-07-34 +.. gh-issue: 153568 +.. nonce: 6sVdqs +.. section: Core and Builtins + +Speed up :func:`ast.parse` by ~15.8% and reduce the memory used by ~51.6% by +storing node attributes as inline values instead of in a per-node +dictionary. + +.. + +.. date: 2026-10-23-20-15-24 +.. gh-issue: 160491 +.. nonce: yIQ_Vf +.. section: Core and Builtins + +Fix the location reported for invalid escape sequences in f-strings and +t-strings. The :exc:`SyntaxError` now points at the literal part that +contains the escape, instead of at the closing quote. + +.. + +.. date: 2026-10-23-19-37-00 +.. gh-issue: 160050 +.. nonce: aB3x9Q +.. section: Core and Builtins + +Use asymptotically faster :func:`divmod` implementation for integer modulo +of large integers. + +.. + +.. date: 2026-10-22-17-37-27 +.. gh-issue: 160268 +.. nonce: gJ2iDx +.. section: Core and Builtins + +Fix a crash in :mod:`tokenize` when a mismatched closing bracket follows the +opening brace of an f-string or t-string replacement field, as in +``f'{]a'``. It now raises the same "unmatched" error as the parser. + +.. + +.. date: 2026-10-22-14-45-00 +.. gh-issue: 160245 +.. nonce: mEmO24 +.. section: Core and Builtins + +Reduce the size of parser memo entries on 64-bit platforms by reordering +fields to remove padding. Patch by Shamil Abdulaev. + +.. + +.. date: 2026-10-22-12-30-11 +.. gh-issue: 106045 +.. nonce: fkMW34 +.. section: Core and Builtins + +Fix :mod:`venv` path discovery when the base executable resolves to a +location outside the installation pointed to by the ``home`` key in +``pyvenv.cfg``. The standard library is now found from ``home``, and +extension modules from the resolved executable's installation, instead of +falling back to the compiled-in prefix. + +.. + +.. date: 2026-10-22-00-30-21 +.. gh-issue: 160162 +.. nonce: lShf32 +.. section: Core and Builtins + +Fix a crash on 32-bit platforms when left shifting an :class:`int` by a +count close to ``sys.maxsize * sys.int_info.bits_per_digit``. +:exc:`OverflowError` is now raised, as for other too large counts. + +.. + +.. date: 2026-10-22-00-30-02 +.. gh-issue: 160179 +.. nonce: dQ2e7m +.. section: Core and Builtins + +Fix :meth:`!bytearray.__init__` leaving uninitialized memory in the object +when it fails on a :class:`list` or :class:`tuple` containing an integer not +in ``range(256)``. The object is now empty after the failed call. + +.. + +.. date: 2026-10-21-23-18-38 +.. gh-issue: 160132 +.. nonce: Nj4rQa +.. section: Core and Builtins + +Reduce memory usage during compilation by reordering the fields of the +internal basic block structure to reduce padding. Patch by Shamil Abdulaev. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: Zw43uX +.. section: Core and Builtins + +Raise :exc:`MemoryError` instead of returning an incorrect result if +:func:`round` runs out of memory while rounding a float. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: CbTE4N +.. section: Core and Builtins + +Fix a possible crash when assigning metadata to :class:`classmethod` or +:class:`staticmethod` objects. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: Aw61LR +.. section: Core and Builtins + +Fix a race in ``list.__sizeof__()`` when a list is concurrently resized on a +free-threaded build. + +.. + +.. date: 2026-10-21-19-55-00 +.. gh-issue: 159739 +.. nonce: tali01 +.. section: Core and Builtins + +Fix a reference leak when the value of a type alias is evaluated recursively +or concurrently. + +.. + +.. date: 2026-10-21-19-55-00 +.. gh-issue: 159739 +.. nonce: codc01 +.. section: Core and Builtins + +Fix a crash in free-threaded builds when :func:`codecs.unregister` is called +concurrently. + +.. + +.. date: 2026-10-21-18-00-00 +.. gh-issue: 160368 +.. nonce: sem-clockwait +.. section: Core and Builtins + +On Linux, allow timed semaphore waits to use the monotonic clock when +``sem_clockwait`` is available at runtime, even if Python was built with +older libc headers. + +.. + +.. date: 2026-10-21-14-00-00 +.. gh-issue: 159493 +.. nonce: sfLf1b +.. section: Core and Builtins + +Superinstructions are now formed after the compiler decides which +:opcode:`LOAD_FAST` instructions can borrow their reference. This fixes +reference count contention slowdown on the free-threaded build for +single-line loops and comprehensions. + +.. + +.. date: 2026-10-20-22-00-00 +.. gh-issue: 159963 +.. nonce: pLkHld +.. section: Core and Builtins + +Fix a regression in Python 3.15.0 where threads importing submodules of +packages for the first time could raise ``_DeadlockError`` or a spurious +"partially initialized module" :exc:`ImportError`, or block one another, +with no circular import involved. :func:`importlib.import_module` now +returns the partially initialized module in a cross-thread circular import, +as the :keyword:`import` statement does, instead of raising +``_DeadlockError``. + +.. + +.. date: 2026-10-20-20-38-38 +.. gh-issue: 159101 +.. nonce: vFwrZY +.. section: Core and Builtins + +Fix use-after-free when :meth:`~object.__del__` changes the base class. +Patch by Sergey Miryanov. + +.. + +.. date: 2026-10-20-10-00-00 +.. gh-issue: 158195 +.. nonce: UnpinTL +.. section: Core and Builtins + +Fix a deadlock in the free-threaded build between a thread that stops the +world while running Python code under the type lock (for example a custom +metaclass ``mro()`` or the ``__eq__`` of a :class:`str` subclass used as a +class ``__dict__`` key) and a thread that assigns to a special method or to +``__bases__`` of a class. The type lock is no longer held while waiting for +a stop-the-world; the prepared slot, MRO and bases updates are validated +with the world stopped and recomputed if they went stale. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: da0f04 +.. section: Core and Builtins + +Fix reference leaks when free-threaded garbage collection aborts early +because of an allocation error. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: d76e6d +.. section: Core and Builtins + +Prevent free-threaded builds from interning and immortalizing complex +subclasses in code object constants. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: bf569b +.. section: Core and Builtins + +Fix a reference leak in binary, ternary, and comparison operations on weak +reference proxies when another operand is a dead proxy. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: a3cc5d +.. section: Core and Builtins + +Fix a crash when constructing an error for invalid generic alias arguments. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: 848b18 +.. section: Core and Builtins + +Allow the garbage collector to break reference cycles involving property +accessors or names. + +.. + +.. date: 2026-10-19-21-30-00 +.. gh-issue: 159860 +.. nonce: qsbr128 +.. section: Core and Builtins + +Fix false sharing between the per-thread QSBR states in the +:term:`free-threaded build` on AArch64 CPUs with 128-byte cache lines, such +as Apple Silicon. CPU-bound threads that share no objects now scale like +processes there. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: fc0ed2 +.. section: Core and Builtins + +Avoid a crash if allocation fails while creating a +:class:`string.templatelib.Template` iterator. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: b62fc9 +.. section: Core and Builtins + +Propagate errors during generic class construction and avoid a crash after +failed generic alias substitution. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: 78e530 +.. section: Core and Builtins + +Raise :exc:`OverflowError` when the absolute value of a dynamic printf-style +formatting width is too large. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: 55fd08 +.. section: Core and Builtins + +Fix a reference leak in :func:`gc.get_objects` and :func:`gc.get_referrers` +when an internal allocation fails in the free-threaded build. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: f81734 +.. section: Core and Builtins + +Fix a crash in :func:`warnings.warn` when ``skip_file_prefixes`` is an empty +tuple subclass. Propagate allocation errors when creating the fallback +filename. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: c25c27 +.. section: Core and Builtins + +Fix memory leaks during interpreter startup when a path configuration file +is empty or too large, or when an audit hook rejects a command. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: a8775b +.. section: Core and Builtins + +Avoid a crash when an interpreter configuration string cannot be encoded as +UTF-8. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: 1fc3af +.. section: Core and Builtins + +Preserve the original :exc:`SyntaxError` if allocating optional error +metadata fails. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: 173d49 +.. section: Core and Builtins + +Avoid a crash if formatting an integer with the ``c`` presentation type runs +out of memory. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: 09590b +.. section: Core and Builtins + +Avoid a crash when a module filename cannot be encoded as UTF-8. + +.. + +.. date: 2026-10-19-16-38-39 +.. gh-issue: 159813 +.. nonce: wHfSOc +.. section: Core and Builtins + +Add a fast path for :const:`None` in :c:func:`PyUnicodeWriter_WriteRepr`. +Patch by Donghee Na. + +.. + +.. date: 2026-10-18-00-30-00 +.. gh-issue: 159652 +.. nonce: Qk3vLm +.. section: Core and Builtins + +Fix a hang when an import fails with :exc:`RecursionError` because the C +stack is nearly exhausted: releasing the import locks could itself fail and +leave the global import lock or a module lock held forever, so that later +imports in other threads blocked indefinitely. The import system now raises +:exc:`RecursionError` before taking an import lock when there is not enough +stack left to release it. + +.. + +.. date: 2026-10-17-02-47-26 +.. gh-issue: 159603 +.. nonce: OFagJz +.. section: Core and Builtins + +On FreeBSD, call ``pthread_attr_get_np()`` to get the stack size (and guard +size) of the current thread. Patch by Victor Stinner. + +.. + +.. date: 2026-10-16-18-25-48 +.. gh-issue: 159564 +.. nonce: WMRRFA +.. section: Core and Builtins + +Improve the performance and thread scaling of dereferencing weak references +in the :term:`free-threaded build`. Dereferencing a weak reference no +longer takes a lock. + +.. + +.. date: 2026-10-15-12-40-00 +.. gh-issue: 87613 +.. nonce: Sl1ceV +.. section: Core and Builtins + +Speed up calling :class:`slice` by adding a vectorcall implementation of the +constructor generated by Argument Clinic. Patch by Pieter Eendebak. + +.. + +.. date: 2026-10-15-12-36-09 +.. gh-issue: 130279 +.. nonce: N9N-sw +.. section: Core and Builtins + +Fix bug where compiler did not set the right exception handler for some jump +instructions. + +.. + +.. date: 2026-10-15-12-10-00 +.. gh-issue: 87613 +.. nonce: Wr4fvc +.. section: Core and Builtins + +Speed up creating :class:`weakref.ref` objects by adding a vectorcall +implementation of the constructor generated by Argument Clinic. Patch by +Pieter Eendebak. + +.. + +.. date: 2026-10-15-10-15-27 +.. gh-issue: 159449 +.. nonce: xsdzFf +.. section: Core and Builtins + +Fix a free-threaded asyncio race leading to segfaults. + +.. + +.. date: 2026-10-14-23-05-00 +.. gh-issue: 132042 +.. nonce: k7Qs2w +.. section: Core and Builtins + +Speed up class creation by resolving the special method slots of a new class +with one pass over each class dict along the MRO instead of one lookup per +slot name and per class. + +.. + +.. date: 2026-10-14-19-16-53 +.. gh-issue: 159434 +.. nonce: T4AsrQ +.. section: Core and Builtins + +Fix reading a function's :attr:`~function.__annotations__` using borrowed +references to :attr:`~function.__annotate__` and the internal annotations +tuple while running code that can replace them. If ``__annotations__`` is +set while ``__annotate__`` runs, the newly set value is now kept. + +.. + +.. date: 2026-10-14-18-45-35 +.. gh-issue: 159431 +.. nonce: mVTMsA +.. section: Core and Builtins + +Fix an assertion failure, or an immortal object becoming mortal, on the +:term:`free threaded ` build when an object passed to the +internal delayed decref mechanism is immortal or becomes immortal before the +delayed decref is processed by the garbage collector. + +.. + +.. date: 2026-10-14-11-40-06 +.. gh-issue: 159430 +.. nonce: OWpyPm +.. section: Core and Builtins + +Report the correct exception if looking up :meth:`~object.__bytes__` raises +an exception while performing %-formatting of :class:`bytes`. + +.. + +.. date: 2026-10-14-07-36-44 +.. gh-issue: 159399 +.. nonce: cjuE0N +.. section: Core and Builtins + +Fix a crash in :func:`codecs.encode` and :func:`codecs.decode` in the +:term:`free-threaded build` when another thread concurrently unregisters a +codec search function. Patch by Christian Aurich Zanettini Martins. + +.. + +.. date: 2026-10-13-16-38-00 +.. gh-issue: 159320 +.. nonce: _Yhg1s +.. section: Core and Builtins + +Add dynamic detection of stack sizes on Android, allowing safer deep +recursion. + +.. + +.. date: 2026-10-13-06-23-56 +.. gh-issue: 159294 +.. nonce: 9LTsNA +.. section: Core and Builtins + +Fix a crash in a debug build when creating an :exc:`ExceptionGroup` or +:exc:`BaseExceptionGroup` fails with :exc:`MemoryError`. Patch by Christian +Aurich Zanettini Martins. + +.. + +.. date: 2026-10-13-04-03-48 +.. gh-issue: 159288 +.. nonce: vhqBQG +.. section: Core and Builtins + +Fix a crash when walking a very long chain of lazy import placeholders, as +created by an aliased dotted import such as ``lazy import a.b.c as d``. +:exc:`RecursionError` is now raised instead. + +.. + +.. date: 2026-10-12-20-00-00 +.. gh-issue: 159136 +.. nonce: fnAttr +.. section: Core and Builtins + +Setting ``__name__``, ``__qualname__``, ``__doc__`` or ``__module__`` on a +function no longer stops all threads in the free-threaded build. The setters +now use the per-object lock and defer freeing the old value with QSBR, the +same approach used for generator ``__name__``. This fixes a large slowdown +in multi-threaded code using :func:`functools.wraps` since 3.14.7. Readers +of these attributes now use atomic loads, and argument error reporting reads +``__qualname__`` after keyword comparisons, which may execute Python code +and replace it. Previously this could use a freed object. + +.. + +.. date: 2026-10-12-16-00-34 +.. gh-issue: 159244 +.. nonce: ThiobE +.. section: Core and Builtins + +Fix a crash in :class:`dict` and :meth:`dict.update` in the +:term:`free-threaded build` when another thread concurrently mutates a list +used as a key/value pair. Patch by Christian Aurich Zanettini Martins. + +.. + +.. date: 2026-10-12-11-59-37 +.. gh-issue: 68241 +.. nonce: jVpLIJ +.. section: Core and Builtins + +Add EXIT_FAILURE and EXIT_SUCCESS in the sys module. + +.. + +.. date: 2026-10-11-06-36-25 +.. gh-issue: 159188 +.. nonce: Fq7wLm +.. section: Core and Builtins + +In the :term:`free-threaded build`, :func:`compile` no longer immortalizes +the constants of the code objects when it is given an AST object, as was +already the case for a source string. The output of :mod:`marshal` for such +code no longer depends on the code objects which were created before in the +process. + +.. + +.. date: 2026-10-10-20-21-13 +.. gh-issue: 159111 +.. nonce: XVf2k- +.. section: Core and Builtins + +Update mimalloc version from 2.1.2 to 2.5.2. Patch by Donghee Na. + +.. + +.. date: 2026-10-09-16-04-40 +.. gh-issue: 159041 +.. nonce: PUP8bU +.. section: Core and Builtins + +Fix a use-after-free in :mod:`asyncio` task bookkeeping when a finalizer +creates a task during thread shutdown. + +.. + +.. date: 2026-10-07-00-00-00 +.. gh-issue: 158973 +.. nonce: warnings-fork +.. section: Core and Builtins + +Reset the warnings lock in the child after :func:`os.fork`, before clearing +other threads' states, to avoid deadlocks when their finalizers issue +warnings. Preserve the lock's recursion depth if the forking thread owns it. + +.. + +.. date: 2026-10-05-13-19-49 +.. gh-issue: 87613 +.. nonce: JLTGBy +.. section: Core and Builtins + +Speed up :class:`bytes` and :class:`bytearray` construction by adding +:ref:`vectorcall` support. + +.. + +.. date: 2026-10-04-20-04-55 +.. gh-issue: 158816 +.. nonce: 9ulhWb +.. section: Core and Builtins + +Speed up deleting items from split dictionaries (for example the +materialized ``__dict__`` of an instance with inline values) by skipping a +redundant hash table probe. + +.. + +.. date: 2026-10-03-13-00-00 +.. gh-issue: 158197 +.. nonce: Qk7sVd +.. section: Core and Builtins + +Fix crashes and debug-build assertion failures in the :term:`free-threaded +build` when iterating over a split dictionary while another thread deletes +entries or clears it. Publish insertion-order updates atomically so readers +cannot observe an increased size before the corresponding order entry is +initialized. + +.. + +.. date: 2026-10-01-00-00-00 +.. gh-issue: 158525 +.. nonce: Xw3K9a +.. section: Core and Builtins + +Fix slow ``dict.copy()`` for dicts with many deleted entries in the +free-threaded build. + +.. + +.. date: 2026-09-30-21-29-54 +.. gh-issue: 158529 +.. nonce: kZO7mX +.. section: Core and Builtins + +Fixed crash on Android x86_64, API levels 27-28, caused by mimalloc calling +system calls directly. + +.. + +.. date: 2026-09-29-14-35-00 +.. gh-issue: 158121 +.. nonce: Kj4nQ8 +.. section: Core and Builtins + +Fix missing :mod:`sys.monitoring` ``INSTRUCTION`` events when a ``LINE`` +callback returns :data:`sys.monitoring.DISABLE`. + +.. + +.. date: 2026-09-28-10-12-25 +.. gh-issue: 158304 +.. nonce: 3pdcnJ +.. section: Core and Builtins + +Speed up iteration over asynchronous generators: values yielded to ``async +for`` no longer create and discard a :exc:`StopIteration` exception at every +nesting level. The ``async_generators`` pyperformance benchmark is about 30% +faster. Patch by Shamil Abdulaev. + +.. + +.. date: 2026-09-25-11-05-00 +.. gh-issue: 157746 +.. nonce: q7Lk3d +.. section: Core and Builtins + +Reduce the overhead of :c:func:`PyErr_CheckSignals` when no signal is +pending: the cheap flag checks are now done before the main-thread check and +the remote debugger configuration lookup. + +.. + +.. date: 2026-09-21-12-44-38 +.. gh-issue: 132414 +.. nonce: uwxmd5 +.. section: Core and Builtins + +Quote the invalid option in the error message for an unknown command-line +option: ``Invalid option '-z'`` instead of ``Unknown option: -z``. A stray +space, as passed by a shebang line like ``#!/usr/bin/python3 -q -i``, is now +reported as ``Invalid option '- '``. + +.. + +.. date: 2026-09-13-10-00-00 +.. gh-issue: 157361 +.. nonce: aiterpy +.. section: Core and Builtins + +Implement :func:`aiter` in Python instead of C, in the frozen +``_pybuiltins`` module. The async iterator returned by ``aiter(callable, +...)`` now has a plain coroutine ``__anext__()``, so introspection tools +such as :func:`asyncio.print_call_graph` can see through it into the awaited +callable result. + +.. + +.. date: 2026-09-08-17-40-00 +.. gh-issue: 157194 +.. nonce: 17Ffhk +.. section: Core and Builtins + +Fix a data race in the free-threaded build, reported by ThreadSanitizer, +between the specializer reading an instruction opcode and another thread +writing it. Only reachable when thread-local bytecode is disabled with ``-X +tlbc=0``, which makes threads share one copy of the bytecode. + +.. + +.. date: 2026-09-07-09-52-21 +.. gh-issue: 156443 +.. nonce: tT40sm +.. section: Core and Builtins + +Speed up :func:`float` conversion and true division of large :class:`int` +values. + +.. + +.. date: 2026-08-30-16-30-00 +.. gh-issue: 135385 +.. nonce: Wn7Rd4 +.. section: Core and Builtins + +Reduce the memory consumption of instances of classes which define +``__slots__`` and also have a ``__dict__``. Space for the attributes stored +in slots is no longer reserved in the inline values of every instance. + +.. + +.. date: 2026-08-27-17-26-21 +.. gh-issue: 106045 +.. nonce: wzTH7f +.. section: Core and Builtins + +Fix standard library discovery in virtual environments created from a Python +executable symlink located outside its base installation. + +.. + +.. date: 2026-08-27-00-00-00 +.. gh-issue: 156399 +.. nonce: aB3xYz +.. section: Core and Builtins + +Fix a regression where attribute access on a :class:`types.ModuleType` +subclass could bypass a data descriptor defined on the subclass. The +``LOAD_ATTR_MODULE`` specialization guarded on ``tp_getattro``, which +subclasses inherit, and then read the module dictionary directly. + +.. + +.. date: 2026-08-21-14-37-50 +.. gh-issue: 127716 +.. nonce: KrPth8 +.. section: Core and Builtins + +On the free-threaded build, :class:`memoryview` lifetime and bookkeeping +operations are now thread safe: an operation that reads the buffer holds the +managed buffer for its duration, so a concurrent :meth:`~memoryview.release` +can no longer free the buffer under it; slicing a memoryview from several +threads no longer loses buffer registrations and raises ``ValueError: +operation forbidden on released memoryview object``; and :func:`hash` of a +memoryview is no longer a data race. As a result :meth:`~memoryview.release` +may now return while an operation started by another thread is still +running, see :ref:`thread-safety-memoryview`. + +.. + +.. date: 2026-07-21-10-42-15 +.. gh-issue: 154318 +.. nonce: Kq3Wm7 +.. section: Core and Builtins + +Fix a crash when hashing a deeply nested :class:`tuple` or +:class:`frozendict`. A :exc:`RecursionError` is now raised instead of +overflowing the C stack. + +.. + +.. date: 2026-07-11-15-00-13 +.. gh-issue: 153568 +.. nonce: atomfp +.. section: Core and Builtins + +Speed up parsing of simple expressions by recognizing single-token atoms +without descending the full precedence rule chain. + +.. + +.. date: 2026-07-04-18-00-00 +.. gh-issue: 153014 +.. nonce: gCdBg1 +.. section: Core and Builtins + +Fix a data race on the garbage collector debug flag in free-threading +builds: :func:`gc.set_debug` and :func:`gc.get_debug` now access it +atomically. + +.. + +.. date: 2026-06-29-23-01-07 +.. gh-issue: 152631 +.. nonce: LKgCM_ +.. section: Core and Builtins + +Fix :func:`ast.parse` ability to parse lazy imports with *feature_version* +older than 3.15, now it properly raises :exc:`SyntaxError`. + +.. + +.. date: 2026-06-26-00-02-29 +.. gh-issue: 149816 +.. nonce: JKN-5B +.. section: Core and Builtins + +Fix a race in free-threaded builds where storing an instance attribute could +use a stale borrowed reference to an instance ``__dict__`` after another +thread replaced it. + +.. + +.. date: 2026-06-20-18-00-00 +.. gh-issue: 151818 +.. nonce: LsApDf +.. section: Core and Builtins + +Fix a double-free crash in ``list.append()`` (the specialized +``CALL_LIST_APPEND`` instruction) when growing the list fails with +:exc:`MemoryError`. + +.. + +.. date: 2026-06-07-22-03-20 +.. gh-issue: 151060 +.. nonce: tdexEE +.. section: Core and Builtins + +Fix a crash or memory corruption in :func:`sum` when summing a +:class:`float` subclass instance into a complex running total. + +.. + +.. date: 2026-05-21-14-21-06 +.. gh-issue: 150075 +.. nonce: dLI21Z +.. section: Core and Builtins + +tar.addfile() now sets member offsets in TarInfo objects + +.. + +.. date: 2026-04-23-08-36-47 +.. gh-issue: 148891 +.. nonce: AMMHow +.. section: Core and Builtins + +Fix a crash in :func:`contextvars.Context.run` where a GC invocation could +free the current HAMT during traverse. + +.. + +.. date: 2026-04-15-21-55-45 +.. gh-issue: 69605 +.. nonce: NwoP6l +.. section: Core and Builtins + +Fix :term:`REPL` completions inserted incorrectly when there was spaces in +import statements. + +.. + +.. date: 2026-04-12-13-48-26 +.. gh-issue: 69605 +.. nonce: T9HWOL +.. section: Core and Builtins + +Insert ``import`` after ``from x.y.z `` in the :term:`REPL` +auto-completion. + +.. + +.. date: 2026-03-14-11-20-52 +.. gh-issue: 145019 +.. nonce: ls2EXo +.. section: Core and Builtins + +Improve :exc:`SyntaxError` when :keyword:`match` alternative patterns bind +different names. Patch by Bénédikt Tran. + +.. + +.. date: 2025-08-19-16-10-26 +.. gh-issue: 137966 +.. nonce: nEgFAt +.. section: Core and Builtins + +Add support for additional special names to the built-in :func:`help` +function. Patch by Adam Hartz. + +.. + +.. date: 2024-10-27-22-23-57 +.. gh-issue: 125873 +.. nonce: BG97KU +.. section: Core and Builtins + +Improved error message when an environment variable is misconfigured. + +.. + +.. date: 2024-08-11-12-06-23 +.. gh-issue: 122901 +.. nonce: B2Huom +.. section: Core and Builtins + +Speed up creating :class:`str` objects from non-ASCII character data. + +.. + +.. date: 2024-03-07-22-38-07 +.. gh-issue: 116477 +.. nonce: AqwRAV +.. section: Core and Builtins + +Speed up the :class:`range` object constructor in the case of a single +argument. + +.. + +.. date: 2026-11-06-11-59-26 +.. gh-issue: 161475 +.. nonce: 82LWf4 +.. section: Library + +Fix :mod:`ctypes` structures containing arrays of floating-point type +returned by value on 64-bit Linux SPARC. + +.. + +.. date: 2026-11-06-05-42-11 +.. gh-issue: 71199 +.. nonce: qJN4cV +.. section: Library + +Change :mod:`os` module types such as :class:`~os.DirEntry` to report ``os`` +as their module on every platform. :ref:`Pickled ` objects +using the platform-specific qualified name, such as ``nt.times_result``, +still load. + +.. + +.. date: 2026-11-06-00-00-00 +.. gh-issue: 142782 +.. nonce: qU7nE4 +.. section: Library + +Fix a use-after-free in :class:`zoneinfo.ZoneInfo` when a key's ``__eq__`` +clears the cache during a lookup. Strong cache keys are now compared by +string contents. + +.. + +.. date: 2026-11-05-12-00-24 +.. gh-issue: 161415 +.. nonce: NJhrAR +.. section: Library + +Remove ``FILES`` and ``unicode`` from the related help topics listed by +:func:`help` for the ``TYPES`` and ``UNICODE`` topics. Neither exists in +Python 3, so asking for help on them failed. + +.. + +.. date: 2026-11-05-11-59-58 +.. gh-issue: 161414 +.. nonce: Xnxe8A +.. section: Library + +Remove the backquote from the symbols listed by :func:`help`, asking for +help on it reported that no documentation was found. + +.. + +.. date: 2026-11-05-11-56-50 +.. gh-issue: 161389 +.. nonce: EIl2E6 +.. section: Library + +Fixes a crash in :class:`decimal.Context` when passing invalid signaling +dicts as *flags* and *traps*. + +.. + +.. date: 2026-11-05-10-05-00 +.. gh-issue: 161387 +.. nonce: def456 +.. section: Library + +Fix a crash in :mod:`decimal` when the internal ``decimal_context`` +:class:`contextvars.ContextVar` holds a value that is not a context. + +.. + +.. date: 2026-11-03-21-26-09 +.. gh-issue: 161295 +.. nonce: IJqoJM +.. section: Library + +:class:`reprlib.Repr` objects now have a meaningful :meth:`~object.__repr__` +describing the instance attributes. + +.. + +.. date: 2026-11-03-18-56-28 +.. gh-issue: 161259 +.. nonce: iyZz6y +.. section: Library + +Improved the accuracy of :func:`statistics.correlation` for very small +inputs, where the product of the sums of squares is subnormal rather than +zero. + +.. + +.. date: 2026-11-03-12-00-00 +.. gh-issue: 93821 +.. nonce: aCcRsT +.. section: Library + +Fix :class:`asyncio.ProactorEventLoop` servers that stopped accepting new +connections on Windows when a client reset its connection before it was +accepted (``WinError 64``, ``ERROR_NETNAME_DELETED``). The listening socket +is now kept open and the server continues to accept connections. + +.. + +.. date: 2026-11-03-12-00-00 +.. gh-issue: 161076 +.. nonce: nOdLy1 +.. section: Library + +Fix :mod:`asyncio` leaving a half-created transport registered with the +event loop and the server when setting ``TCP_NODELAY`` on a socket fails. +Such a failure could make :meth:`asyncio.Server.wait_closed` hang and +prevent new connections from being served. Errors from +``setsockopt(TCP_NODELAY)`` are now ignored. + +.. + +.. date: 2026-11-03-10-33-33 +.. gh-issue: 161233 +.. nonce: _9VrfL +.. section: Library + +Fix unraisable :exc:`TypeError` when ``None`` was passed as a *timer* to +:class:`cProfile.Profile `. + +.. + +.. date: 2026-11-02-21-10-00 +.. gh-issue: 161212 +.. nonce: pip263 +.. section: Library + +Bump the version of :pypi:`pip` bundled in :mod:`ensurepip` to version 26.3. + +.. + +.. date: 2026-11-02-16-28-11 +.. gh-issue: 161178 +.. nonce: D3-23R +.. section: Library + +Fix :meth:`asyncio.Task.print_stack` to include frames from async generator +operations. Patch by Timofei Ivankov + +.. + +.. date: 2026-11-02-13-26-36 +.. gh-issue: 161179 +.. nonce: H8Yyaw +.. section: Library + +Fix the function names in the error messages of :func:`os.spawnve`, +:func:`os.preadv` and :meth:`tkinter.Wm.wm_attributes`. + +.. + +.. date: 2026-11-02-13-11-30 +.. gh-issue: 161166 +.. nonce: zfFDlW +.. section: Library + +Fix :meth:`asyncio.Task.print_stack` displaying an incorrect exception line +after a ``finally`` block. Patch by Timofei Ivankov. + +.. + +.. date: 2026-11-01-22-07-24 +.. gh-issue: 161124 +.. nonce: c2YmcV +.. section: Library + +Fix :func:`asyncio.print_call_graph` reporting event loop frames when +*depth* exceeds the task stack. Patch by Timofei Ivankov. + +.. + +.. date: 2026-11-01-19-38-33 +.. gh-issue: 161083 +.. nonce: PM21lv +.. section: Library + +Fix stale task nodes left by :func:`asyncio.gather` when argument validation +fails. Patch by Timofei Ivankov + +.. + +.. date: 2026-10-31-21-34-00 +.. gh-issue: 161039 +.. nonce: k7_pTD +.. section: Library + +Fix the :mod:`pydoc` pager not detecting ``less`` 706 and newer. + +.. + +.. date: 2026-10-30-12-00-00 +.. gh-issue: 137549 +.. nonce: Tm0Bxg +.. section: Library + +:func:`asyncio.timeout` now inserts :exc:`TimeoutError` into the context +chain of sub-exceptions of a :exc:`BaseExceptionGroup`, as it already did +for :exc:`ExceptionGroup`. + +.. + +.. date: 2026-10-30-10-36-30 +.. gh-issue: 160782 +.. nonce: n6JCUU +.. section: Library + +:mod:`venv`: a symbolic link or junction in place of a directory inside the +environment now raises :exc:`ValueError` instead of being followed. + +.. + +.. date: 2026-10-29-23-25-31 +.. gh-issue: 160782 +.. nonce: 1ZwCOr +.. section: Library + +:mod:`venv`: a backslash before a quote in the prompt or the path no longer +breaks ``activate.fish``. + +.. + +.. date: 2026-10-29-21-43-53 +.. gh-issue: 160429 +.. nonce: eWapMs +.. section: Library + +Fix :class:`asyncio.ProactorEventLoop` socket transports leaving the socket +open, and :meth:`asyncio.Server.wait_closed` hanging, when the peer resets +the connection while the transport is being closed. + +.. + +.. date: 2026-10-29-21-36-08 +.. gh-issue: 160929 +.. nonce: mK8n2Q +.. section: Library + +Fix reverse union with :class:`~collections.ChainMap` to preserve its lookup +semantics for defaulting mappings such as :class:`~collections.Counter` and +:class:`~collections.defaultdict`. + +.. + +.. date: 2026-10-29-20-52-57 +.. gh-issue: 50720 +.. nonce: Kd1VMR +.. section: Library + +:exc:`urllib.error.URLError` now sets the :attr:`~OSError.errno` and +:attr:`~OSError.strerror` attributes from +:attr:`~urllib.error.URLError.reason` when it is an :exc:`OSError`. On +Windows, :attr:`~OSError.winerror` is set as well. + +.. + +.. date: 2026-10-29-19-42-53 +.. gh-issue: 53089 +.. nonce: ewaIBvY6 +.. section: Library + +:class:`urllib.request.HTTPDigestAuthHandler` now sends ``uri="/"`` in the +Digest ``Authorization`` header for URLs with an empty path, such as +``http://example.com``, matching the request-target sent by +:mod:`http.client`. Previously it sent ``uri=""``, which servers could +reject. Patch by Senthil Kumaran. + +.. + +.. date: 2026-10-28-20-36-44 +.. gh-issue: 109564 +.. nonce: WqoyNE +.. section: Library + +Fix a race in :mod:`asyncio` where closing a server after it accepted a +connection, but before the connection's transport was created, reported an +:exc:`AssertionError` to the exception handler. Such connections are now +closed quietly. + +.. + +.. date: 2026-10-28-16-40-00 +.. gh-issue: 66333 +.. nonce: pdb-sigint +.. section: Library + +Allow Ctrl-C to interrupt execution resumed with the :mod:`pdb` ``next``, +``until``, and ``return`` commands without unwinding the debugged program, +as with ``continue``. + +.. + +.. date: 2026-10-28-13-23-28 +.. gh-issue: 59515 +.. nonce: wt31yNN4 +.. section: Library + +:class:`urllib.request.HTTPDigestAuthHandler` and +:class:`urllib.request.ProxyDigestAuthHandler` now parse all +``WWW-Authenticate`` (or ``Proxy-Authenticate``) headers and accept multiple +challenges per header: the first Digest challenge is used. + +.. + +.. date: 2026-10-28-12-00-00 +.. gh-issue: 53852 +.. nonce: Pr0xy4 +.. section: Library + +:class:`urllib.request.ProxyDigestAuthHandler` now raises +:exc:`~urllib.error.HTTPError` with status code 407 (Proxy Authentication +Required) instead of 401 when proxy authentication fails. + +.. + +.. date: 2026-10-28-08-12-33 +.. gh-issue: 160811 +.. nonce: wQ3xT7 +.. section: Library + +Fix :meth:`io.TextIOWrapper.tell` reading past the end of the buffer after +:meth:`~io.TextIOWrapper.seek`. + +.. + +.. date: 2026-10-28-07-34-05 +.. gh-issue: 160806 +.. nonce: ZZRkhi +.. section: Library + +Fix the pure-Python implementation of :class:`datetime.timedelta` losing +sub-second precision with large float arguments. It now gives the same +results as the C implementation. + +.. + +.. date: 2026-10-28-07-26-47 +.. gh-issue: 160804 +.. nonce: R5HeTQ +.. section: Library + +:mod:`asyncio` call graph introspection now includes frames of generator +based awaitables, such as generators returned by ``__await__`` and +:func:`types.coroutine` generators. + +.. + +.. date: 2026-10-27-15-42-16 +.. gh-issue: 160781 +.. nonce: VGLB9S +.. section: Library + +Fix :class:`io.BytesIO` implementation of :meth:`io.IOBase.truncate` +rejecting sizes larger than ``2**31 - 1`` on Windows. + +.. + +.. date: 2026-10-27-15-27-32 +.. gh-issue: 160781 +.. nonce: kARuHu +.. section: Library + +Fix :class:`io.StringIO` implementation of :meth:`io.IOBase.truncate` +rejecting sizes larger than ``2**31 - 1`` on Windows. + +.. + +.. date: 2026-10-27-15-26-53 +.. gh-issue: 160781 +.. nonce: 4VSeQK +.. section: Library + +Fix :class:`io.BytesIO` raising :exc:`SystemError` instead of +:exc:`OverflowError` when writing at a position close to +:data:`sys.maxsize`. + +.. + +.. date: 2026-10-26-21-37-42 +.. gh-issue: 158500 +.. nonce: zxyuq5 +.. section: Library + +:meth:`datetime.date.fromisoformat` now rejects strings with trailing +characters after a basic-format date, such as ``'2020010112'``. The +pure-Python :meth:`datetime.datetime.fromisoformat` now rejects a date +followed by a bare separator, such as ``'2020-01-01T'``, matching the C +implementation. + +.. + +.. date: 2026-10-26-16-17-45 +.. gh-issue: 160708 +.. nonce: w7z33g +.. section: Library + +Fix out-of-bounds memory access in :func:`bool` on :mod:`ctypes` simple +objects enlarged by :func:`ctypes.resize`, and when passing such objects to +a foreign function. + +.. + +.. date: 2026-10-26-16-15-29 +.. gh-issue: 160427 +.. nonce: 9G4EBb +.. section: Library + +Fix :attr:`~io.TextIOBase.buffer` of a detached :class:`io.TextIOWrapper` +raising :exc:`ValueError` instead of returning ``None``. + +.. + +.. date: 2026-10-24-22-32-23 +.. gh-issue: 160596 +.. nonce: RKll-Z +.. section: Library + +Fix the error message for invalid data passed to +:meth:`!asyncio.proactor_events.sendto`. + +.. + +.. date: 2026-10-24-07-58-00 +.. gh-issue: 160561 +.. nonce: b3Kq1z +.. section: Library + +Fix a descriptor leak in :func:`socket.fromfd` when the socket constructor +rejects its arguments. The duplicated descriptor was never closed. + +.. + +.. date: 2026-10-24-01-53-42 +.. gh-issue: 160433 +.. nonce: DSTlog +.. section: Library + +Fix incorrect backup names around daylight saving time changes in +:class:`logging.handlers.TimedRotatingFileHandler`, including delayed +rollovers. + +.. + +.. date: 2026-10-23-23-21-21 +.. gh-issue: 160549 +.. nonce: q7ZdMt +.. section: Library + +Fix a crash when a :class:`compression.zstd.ZstdCompressor` that uses worker +threads and a :class:`~compression.zstd.ZstdDict` is deallocated, or the +interpreter exits, while data passed to +:meth:`~compression.zstd.ZstdCompressor.compress` is still being compressed. + +.. + +.. date: 2026-10-23-22-34-55 +.. gh-issue: 160535 +.. nonce: Zk3dGc +.. section: Library + +Fix a crash in :mod:`compression.zstd` when the garbage collector frees a +:class:`~compression.zstd.ZstdCompressor` that uses worker threads together +with its :class:`~compression.zstd.ZstdDict` before the compression of +already submitted data has finished. + +.. + +.. date: 2026-10-23-21-52-40 +.. gh-issue: 60299 +.. nonce: Af6IzL +.. section: Library + +Fix :class:`urllib.request.ProxyDigestAuthHandler` failing to authenticate. +It looked up credentials using the target request URL instead of the proxy's +authority, so passwords registered via ``add_password()`` against the proxy +never matched. Patch by Senthil Kumaran. + +.. + +.. date: 2026-10-22-22-51-40 +.. gh-issue: 160386 +.. nonce: wYyboz +.. section: Library + +Applied changes to ``importlib.metadata`` from `importlib_metadata 9.1 +`_: +distribution discovery now prefers the greater of several metadata +directories supplied by one path, so stale metadata left by an unclean +upgrade no longer shadows the version in use; a missing required metadata +field now raises a ``KeyError`` naming both the field and the distribution; +``EntryPoint`` value validation is deferred until the value is used, so one +malformed entry point no longer prevents others from being selected; +``importlib.metadata.diagnose`` no longer crashes on a metadata directory +with no metadata file or no declared name; and ``SimplePath.joinpath`` and +``SimplePath.__truediv__`` take ``other`` positional-only. + +.. + +.. date: 2026-10-22-18-30-00 +.. gh-issue: 160520 +.. nonce: sSdTmo +.. section: Library + +Fix :mod:`asyncio` SSL transports not applying *ssl_shutdown_timeout* when +the peer has sent EOF but does not read the data still queued for it. +Closing such a connection could wait indefinitely; it is now aborted with +:exc:`TimeoutError` once the timeout expires. + +.. + +.. date: 2026-10-22-18-30-00 +.. gh-issue: 160326 +.. nonce: nAn7Tq +.. section: Library + +Raise :exc:`ValueError` for a NaN *timeout* in +:meth:`threading.Condition.wait` and the blocking calls built on it, such as +:meth:`threading.Event.wait`, :meth:`threading.Semaphore.acquire` and +:meth:`queue.Queue.get`, instead of returning at once or looping without +ever timing out. + +.. + +.. date: 2026-10-22-18-30-00 +.. gh-issue: 160314 +.. nonce: q7RkZd +.. section: Library + +Raise an error in :class:`lzma.LZMAFile`, :func:`lzma.decompress`, +:class:`bz2.BZ2File` and :func:`bz2.decompress` for a damaged ``.xz`` or +bzip2 stream that follows another stream, instead of silently ignoring it +and everything after it as trailing data. + +.. + +.. date: 2026-10-22-16-30-00 +.. gh-issue: 159739 +.. nonce: tRq417 +.. section: Library + +Fix crashes in :mod:`sqlite3` when a row or text factory is replaced during +a query, when cursor metadata is read during execution in a free-threaded +build, or when allocating cursor metadata fails. + +.. + +.. date: 2026-10-22-16-30-00 +.. gh-issue: 159739 +.. nonce: mFWntK +.. section: Library + +Fix crashes in :mod:`profiling.tracing` when a custom timer reinitializes +the profiler or its result clears or disables the profiler during conversion +or cleanup. + +.. + +.. date: 2026-10-22-16-23-01 +.. gh-issue: 159739 +.. nonce: XmlH3a +.. section: Library + +Fix crashes in :mod:`xml.parsers.expat` when a character data callback +disables buffering or clears another handler during a flush, or when +interning an attribute name changes +:attr:`~xml.parsers.expat.xmlparser.ordered_attributes`. + +.. + +.. date: 2026-10-22-16-23-01 +.. gh-issue: 159739 +.. nonce: CjkR5a +.. section: Library + +Fix the ``hz`` codec sometimes encoding ``~`` as a single ``~`` in text that +mixes ASCII and Chinese characters, and the incremental ``iso2022_*`` codecs +mishandling an escape sequence split across calls or keeping the previous +character set after :meth:`!reset`. Also fix possible crashes in the CJK +incremental and stream codecs when their ``errors`` attribute or their +class's ``codec`` attribute is replaced while they are in use. + +.. + +.. date: 2026-10-22-16-18-42 +.. gh-issue: 159739 +.. nonce: TioR3a +.. section: Library + +Fix crashes in :class:`io.TextIOWrapper` and +:class:`io.IncrementalNewlineDecoder` when a callback changes the active +codec or decoded buffer. :class:`io.StringIO` now raises :exc:`ValueError` +if converting the size passed to ``truncate()`` closes the stream. + +.. + +.. date: 2026-10-22-08-34-22 +.. gh-issue: 160310 +.. nonce: q7Zm2K +.. section: Library + +Fix quadratic time complexity of :func:`gzip.decompress`, +:func:`bz2.decompress`, :func:`lzma.decompress` and +:func:`compression.zstd.decompress` on data consisting of many concatenated +members. + +.. + +.. date: 2026-10-22-08-18-31 +.. gh-issue: 160522 +.. nonce: cJkSt8 +.. section: Library + +Fix the incremental encoders and stream writers of the stateful CJK codecs +(``iso2022_*`` and ``hz``) keeping a changed shift state after an +:meth:`!encode` or :meth:`!write` call failed. The next call could omit the +escape sequence, writing multibyte characters in ASCII mode. + +.. + +.. date: 2026-10-22-08-17-51 +.. gh-issue: 160316 +.. nonce: sRe9pQ +.. section: Library + +Fix :meth:`re.Pattern.match`, :meth:`re.Pattern.prefixmatch`, +:meth:`re.Pattern.fullmatch` and the ``match()`` and ``prefixmatch()`` +methods of scanner objects returning an empty match, or reading past the end +of the string, when *pos* is greater than *endpos*. They now return +``None``, as documented. + +.. + +.. date: 2026-10-22-08-15-56 +.. gh-issue: 160324 +.. nonce: sIoRlz +.. section: Library + +Fix a crash or garbage data when an :class:`io.StringIO` object is used +again after one of its methods failed with :exc:`MemoryError`. The object +now keeps its contents. + +.. + +.. date: 2026-10-22-08-09-29 +.. gh-issue: 160406 +.. nonce: q7Rk2d +.. section: Library + +Fix in-place filtering in :mod:`fileinput` deleting the file when it cannot +be opened or the output file cannot be created: the backup file is now kept. +The output file is now created exclusively, so :exc:`FileExistsError` is +raised if a file or symbolic link appears under the name of the input file. + +.. + +.. date: 2026-10-22-08-06-39 +.. gh-issue: 160513 +.. nonce: q7RkZd +.. section: Library + +Fix :meth:`asyncio.loop.start_tls` and +:meth:`asyncio.StreamWriter.start_tls` not calling the protocol's +:meth:`~asyncio.BaseProtocol.connection_lost` when the connection is lost, +the handshake times out or the call is cancelled during the TLS handshake. +:meth:`asyncio.StreamWriter.wait_closed` no longer waits forever after such +a failure. + +.. + +.. date: 2026-10-22-08-04-26 +.. gh-issue: 160297 +.. nonce: hL4kQz +.. section: Library + +Fix :meth:`tarfile.TarFile.extractall` and :meth:`tarfile.TarFile.extract` +changing the mode, modification time and owner of a file outside the +destination directory when a hard link member is extracted as a copy of a +symbolic link member, for example with the ``'tar'`` filter. + +.. + +.. date: 2026-10-22-05-24-34 +.. gh-issue: 161086 +.. nonce: FGPjqA +.. section: Library + +Fix the :mod:`gzip` command-line interface ignoring :option:`gzip --fast` +and :option:`gzip --best` when compressing files given as arguments. + +.. + +.. date: 2026-10-22-05-24-21 +.. gh-issue: 161112 +.. nonce: BFrZRw +.. section: Library + +Fix :func:`shutil.make_archive` adding the zip archive being created to +itself when it is written inside the directory being archived or a symbolic +link in that directory refers to it. + +.. + +.. date: 2026-10-22-05-23-40 +.. gh-issue: 161088 +.. nonce: _-HEWz +.. section: Library + +Fix the :mod:`gzip` command-line interface writing through a symbolic link +at the output file name. Like :program:`gzip`, it now replaces an existing +output file with a new one that has the permission bits of the input file. + +.. + +.. date: 2026-10-22-05-21-00 +.. gh-issue: 161094 +.. nonce: MDjCNm +.. section: Library + +Fix :func:`zipfile.is_zipfile` raising :exc:`ValueError` instead of +returning ``False`` for a file-like object, such as :class:`io.BytesIO`, +whose end of central directory record places the central directory before +the start of the file. + +.. + +.. date: 2026-10-22-05-20-52 +.. gh-issue: 161096 +.. nonce: vkhfyy +.. section: Library + +Fix :meth:`zoneinfo.ZoneInfo.from_file` accepting TZif data with a negative +or oversized abbreviation block, which could use hundreds of times the file +size in memory; it now raises :exc:`ValueError`. Abbreviation indexes above +127 now also give the correct time zone abbreviation. + +.. + +.. date: 2026-10-22-05-20-44 +.. gh-issue: 161106 +.. nonce: t0ux5q +.. section: Library + +Fix :func:`plistlib.dump` and :func:`plistlib.dumps` with +:data:`~plistlib.FMT_BINARY` holding on to memory proportional to the size +of large arrays and dictionaries after the call returns. + +.. + +.. date: 2026-10-22-05-20-27 +.. gh-issue: 161098 +.. nonce: voMVnd +.. section: Library + +Fix :class:`multiprocessing.shared_memory.ShareableList` leaving its newly +created shared memory block behind when a value cannot be stored, such as an +integer outside the signed 64-bit range. + +.. + +.. date: 2026-10-22-05-20-18 +.. gh-issue: 161108 +.. nonce: xOVsRA +.. section: Library + +Fix quadratic time complexity of :meth:`!clear` and :meth:`!popitem` on +:class:`configparser.ConfigParser` objects and their section proxies. +Clearing a section no longer fails part way with +:exc:`~configparser.InterpolationError` on a value it removes. + +.. + +.. date: 2026-10-22-05-20-11 +.. gh-issue: 161110 +.. nonce: miKrSQ +.. section: Library + +Fix :meth:`~xml.dom.Document.getElementById` in :mod:`xml.dom.minidom` so +that it returns the first element in document order when several elements +have the same ID. Previously, the result could differ between calls on an +unmodified document. + +.. + +.. date: 2026-10-22-05-20-06 +.. gh-issue: 161100 +.. nonce: pLgnlA +.. section: Library + +Fix :attr:`!ipaddress.IPv6Interface.is_unspecified` returning ``False`` for +the IPv4-mapped address ``::ffff:0.0.0.0``. It now gives the same result as +:attr:`ipaddress.IPv6Address.is_unspecified`. + +.. + +.. date: 2026-10-22-05-20-02 +.. gh-issue: 161102 +.. nonce: gqDOIM7X +.. section: Library + +Fix the incremental decoder of the :mod:`idna ` codec +decoding some dots twice when its input starts with empty labels, for +example ``b".example.com"``. Reading a run of dots through +:class:`io.TextIOWrapper` no longer produces quadratically more text than +the input holds. + +.. + +.. date: 2026-10-22-05-20-02 +.. gh-issue: 161092 +.. nonce: OtC5l6 +.. section: Library + +Fix the ``uu`` codec decoder taking any line that starts with ``begin``, +such as ``beginning of the report``, as the header of the encoded data. Like +the :mod:`email` package, it now requires ``begin`` followed by an octal +mode and skips other lines as leading text. + +.. + +.. date: 2026-10-22-05-19-56 +.. gh-issue: 161090 +.. nonce: pxZ1IA +.. section: Library + +Fix the ``punycode`` codec accepting a delimiter (``-``) that is not +preceded by any basic code point. Such input, for example ``b'-80a1acny'``, +now raises :exc:`UnicodeDecodeError`, as required by :rfc:`3492`. + +.. + +.. date: 2026-10-22-03-55-33 +.. gh-issue: 159739 +.. nonce: ce26e1 +.. section: Library + +Fix :mod:`time` functions returning a negative year or failing to accept +their own output for large timestamps on platforms that support them. Avoid +reporting an incorrect year in errors raised by :mod:`datetime` for those +timestamps. + +.. + +.. date: 2026-10-22-03-55-33 +.. gh-issue: 159739 +.. nonce: b4c371 +.. section: Library + +Fix a crash when a :mod:`ctypes` structure subclass cannot be created due to +an allocation failure. + +.. + +.. date: 2026-10-22-03-55-33 +.. gh-issue: 159739 +.. nonce: a8bf30 +.. section: Library + +Fix clearing a :class:`pickle.Unpickler` memo so that the unpickler can be +reused for independent records. + +.. + +.. date: 2026-10-22-00-33-42 +.. gh-issue: 160171 +.. nonce: 11l4om +.. section: Library + +On the server side, :meth:`ssl.SSLSocket.get_unverified_chain` now returns +the client's certificate when the session was resumed from a session ticket, +instead of an empty list, and returns an empty list instead of raising +:exc:`AttributeError` when the client sent no certificate. + +.. + +.. date: 2026-10-22-00-32-51 +.. gh-issue: 160181 +.. nonce: nnEeg1 +.. section: Library + +Fix :meth:`io.BytesIO.getvalue` sometimes returning a :class:`bytes` object +with the hash of earlier contents after the buffer was written to, which +made :class:`dict` and :class:`set` lookups with it fail. + +.. + +.. date: 2026-10-22-00-30-04 +.. gh-issue: 160187 +.. nonce: 9dcj2B +.. section: Library + +Fix the ISO-2022 decoders (``iso2022_jp``, ``iso2022_kr`` and related) +accepting escape sequences that designate a character set to the wrong code +element (G0, G1 or G2). Such sequences now raise :exc:`UnicodeDecodeError` +instead of producing wrong text or :exc:`RuntimeError`. + +.. + +.. date: 2026-10-22-00-29-55 +.. gh-issue: 160185 +.. nonce: BPz40E +.. section: Library + +Fix :meth:`codecs.StreamWriter.reset` of the stateful CJK codecs (``hz`` and +the ``iso2022_*`` family) not writing the sequence that returns the output +to ASCII. ``seek(0)`` of :class:`codecs.StreamWriter`, +:class:`codecs.StreamReaderWriter` and :class:`codecs.StreamRecoder` now +resets the writer before moving the stream position. + +.. + +.. date: 2026-10-22-00-29-50 +.. gh-issue: 160176 +.. nonce: tkNst1 +.. section: Library + +Fix a crash in :mod:`tkinter` when a deeply nested or recursive list or +tuple is passed to Tcl, or a deeply nested Tcl list is converted to Python. +These now raise :exc:`ValueError` (beyond 1000 levels) and +:exc:`RecursionError` respectively. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: yBc6Ei +.. section: Library + +Fix a possible crash in :class:`abc.ABCMeta` instance checks when a +subclass-checking callback changes the instance’s class. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: rVWF2a +.. section: Library + +Fix the ``truncate`` method on :class:`io.BytesIO` when converting its size +argument closes the stream or exports its buffer. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: k8uuGe +.. section: Library + +Raise :exc:`TypeError` if a string subclass returns a non-string during +newline translation in :class:`io.TextIOWrapper`. Recheck the stream state +if the replacement callback closes, detaches or reinitializes the stream. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: ghGctt +.. section: Library + +Fix :class:`contextvars.ContextVar` returning a stale value after a +reentrant finalizer changes it during :meth:`~contextvars.ContextVar.reset`. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: b4ueJ7 +.. section: Library + +Detect recursive ``_as_parameter_`` chains for :mod:`ctypes` calls without +argument types. + +.. + +.. date: 2026-10-21-21-26-10 +.. gh-issue: 159739 +.. nonce: JIqZRY +.. section: Library + +Fix a possible crash when a list of :mod:`sqlite3` parameters is modified +concurrently on a free-threaded build. + +.. + +.. date: 2026-10-21-20-05-00 +.. gh-issue: 160104 +.. nonce: BUAL5C +.. section: Library + +Synchronized :class:`zipfile.Path` with :pypi:`zipp` 4.1.2. +:meth:`zipfile.Path.iterdir` now raises :exc:`NotADirectoryError` instead of +:exc:`ValueError` when the path does not reference a directory. Character +classes in :meth:`!zipfile.Path.glob` and :meth:`!zipfile.Path.rglob` +patterns now follow shell-style semantics, treating a leading ``!`` as +negation and ``^`` as a literal, and no longer match path separators. + +.. + +.. date: 2026-10-21-19-55-00 +.. gh-issue: 159739 +.. nonce: tloc01 +.. section: Library + +Avoid a crash when a :class:`threading.local` subclass finalizer accesses +attributes after construction fails. + +.. + +.. date: 2026-10-21-19-55-00 +.. gh-issue: 159739 +.. nonce: sque01 +.. section: Library + +Avoid a crash when a :class:`queue.SimpleQueue` subclass finalizer adds an +item after construction fails. + +.. + +.. date: 2026-10-21-19-40-00 +.. gh-issue: 100609 +.. nonce: THXgRC +.. section: Library + +``str()`` of a :class:`zipfile.Path` whose underlying zipfile has no +filename, such as one constructed from an :class:`io.BytesIO`, now renders +that filename as ``:zipfile:`` instead of raising :exc:`TypeError`. The root +of a zipfile is also no longer rendered with a trailing slash. Synchronized +with :pypi:`zipp` 3.23.1. + +.. + +.. date: 2026-10-21-19-38-33 +.. gh-issue: 89721 +.. nonce: uMpxRw +.. section: Library + +Add ``copy_dir_stat`` parameter to :func:`shutil.copytree`: set it to false +to skip copying permissions and times of directories with +:func:`shutil.copystat`, e.g. when copying a read-only tree. + +.. + +.. date: 2026-10-21-15-42-29 +.. gh-issue: 159875 +.. nonce: p6j3yW +.. section: Library + +Fix :exc:`AttributeError` from the methods of an :mod:`asyncio` SSL +transport, such as :meth:`~asyncio.WriteTransport.write` and +:meth:`~asyncio.BaseTransport.set_protocol`, when the transport is closed +after its connection was lost. This also affected the fallback of +:meth:`loop.sendfile() ` over SSL when the peer went +away mid-transfer. The SSL context is still released once the connection is +lost. + +.. + +.. date: 2026-10-21-15-10-00 +.. gh-issue: 160125 +.. nonce: pdbpkg +.. section: Library + +Support setting breakpoints with the :mod:`pdb` ``break`` and ``tbreak`` +commands using ``package.module.function`` names when the module has not +been imported in the current namespace. + +.. + +.. date: 2026-10-20-17-29-14 +.. gh-issue: 138641 +.. nonce: FN6qM0 +.. section: Library + +Ignore :func:`pdb.set_trace` when called directly at the :mod:`pdb` prompt. + +.. + +.. date: 2026-10-20-16-00-00 +.. gh-issue: 159868 +.. nonce: qLhYkP +.. section: Library + +Allow :meth:`!collections.UserString.startswith` and +:meth:`!collections.UserString.endswith` to accept a +:class:`~collections.UserString` instance as the prefix or suffix argument. + +.. + +.. date: 2026-10-20-12-30-00 +.. gh-issue: 159864 +.. nonce: 4FZ6MR +.. section: Library + +Fix a crash in :func:`syslog.openlog` under the free-threaded build when +:data:`sys.argv` is modified concurrently. + +.. + +.. date: 2026-10-20-00-12-05 +.. gh-issue: 159690 +.. nonce: vZgLP6 +.. section: Library + +Fix :class:`logging.handlers.TimedRotatingFileHandler` permanently stopping +rollover after a rollover failed or its backup file already existed, for +example at a DST change. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: eb8987 +.. section: Library + +Avoid undefined behavior when :func:`struct.pack_into` copies a bytearray +into overlapping output. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: 9ef89c +.. section: Library + +Fix allocation error handling in the fallback implementation of +``getaddrinfo()``. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: 5d4269 +.. section: Library + +Fix a double free if remote unwinder initialization runs out of memory. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: 4c9783 +.. section: Library + +Fix :meth:`io.IOBase.readline` returning more bytes than requested when a +stream supports :meth:`!peek` and returns short reads. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: 3660a6 +.. section: Library + +Fix multibyte encoders silently consuming a non-BMP character when looking +up a two-character mapping. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: 35942a +.. section: Library + +Improve performance when parsing email address groups with many members. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: 1e4599 +.. section: Library + +Fix off-by-one output buffer checks in the fallback implementation of +``getnameinfo()``. + +.. + +.. date: 2026-10-20-00-00-00 +.. gh-issue: 159739 +.. nonce: 0c4ae9 +.. section: Library + +Fix reference cycles involving the types of grapheme iterators and segments +created by :func:`unicodedata.iter_graphemes`. + +.. + +.. date: 2026-10-19-23-25-29 +.. gh-issue: 159811 +.. nonce: cMB4DO +.. section: Library + +Fix a crash in :mod:`hashlib` when the OpenSSL routine used by +:meth:`hash.update() ` or by hash object constructors +fails on data larger than 2048 bytes. Patch by Bénédikt Tran. + +.. + +.. date: 2026-10-19-23-25-29 +.. gh-issue: 159811 +.. nonce: P6yy0z +.. section: Library + +Fix a crash in :mod:`hmac` when the OpenSSL routine used by +:meth:`hmac.HMAC.update` or by :func:`hmac.new` fails on data larger than +2048 bytes. Patch by Bénédikt Tran. + +.. + +.. date: 2026-10-19-21-20-28 +.. gh-issue: 159786 +.. nonce: fThehQ +.. section: Library + +Fix the error handling of the HACL* routine used by :func:`hmac.new` and +:meth:`hmac.HMAC.update`. If the maximum input length of the underlying hash +function is exceeded, an :exc:`OverflowError` is now raised on all builds, +instead of crashing on DEBUG builds and being silently ignored otherwise. +Code correctness. Realistically this can only happen at implausible sizes. +Patch by Bénédikt Tran. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: ff088c +.. section: Library + +Fix copying :class:`collections.deque` subclasses with a large maximum +length. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: bc65da +.. section: Library + +Allow cyclic garbage collection to reclaim group records returned by +:mod:`grp`. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: a81270 +.. section: Library + +Fix :class:`io.BytesIO` reads into overlapping views of the same buffer. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: 5a6e94 +.. section: Library + +Avoid a crash when memory allocation fails while creating a +:class:`collections.OrderedDict` items iterator. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: 243aa8 +.. section: Library + +Avoid reading past the end of an encoding name when the ``surrogatepass`` +error handler receives a truncated UTF-16 or UTF-32 name. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: 08caa7 +.. section: Library + +Fix incorrect :mod:`ctypes` scalar values after :func:`ctypes.resize` +enlarges the object. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: b5fbca +.. section: Library + +Fix a memory leak when Unicode normalization cannot grow its temporary +buffer. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: a1c3c7 +.. section: Library + +Fix resource leaks in :mod:`ssl` when certificate conversion or key log +setup fails. + +.. + +.. date: 2026-10-19-16-49-09 +.. gh-issue: 159706 +.. nonce: ushGrx +.. section: Library + +Fix the pure-Python :class:`zoneinfo.ZoneInfo` setting ``fold=0`` in +:meth:`~datetime.tzinfo.fromutc` for times in the fold created by the only +transition of a zone with a single transition. + +.. + +.. date: 2026-10-19-16-26-57 +.. gh-issue: 71990 +.. nonce: K3FlJ9 +.. section: Library + +:mod:`ctypes`: objects with an :attr:`!_as_parameter_` attribute are now +implicitly converted when passed for a :func:`~ctypes.POINTER` argument. + +.. + +.. date: 2026-10-19-16-00-00 +.. gh-issue: 139122 +.. nonce: bYtEs1 +.. section: Library + +:class:`uuid.UUID` now consistently raises :exc:`TypeError` when the *bytes* +or *bytes_le* argument is not a :class:`bytes` object. Previously, some +invalid inputs raised :exc:`AssertionError` or :exc:`ValueError`. + +.. + +.. date: 2026-10-19-12-00-00 +.. gh-issue: 78327 +.. nonce: Qk3hzA +.. section: Library + +:mod:`ctypes` data types now set :meth:`~object.__hash__` to ``None``, so +:class:`collections.abc.Hashable` checks report them correctly. + +.. + +.. date: 2026-10-19-12-00-00 +.. gh-issue: 159714 +.. nonce: j59fix +.. section: Library + +Fix :class:`zoneinfo.ZoneInfo` interpreting the Julian day rule ``J59`` as +February 29 in leap years instead of February 28. + +.. + +.. date: 2026-10-19-00-00-00 +.. gh-issue: 149015 +.. nonce: bdb +.. section: Library + +Fix :meth:`bdb.Bdb.clear_all_file_breaks` leaving breakpoints behind when +multiple breakpoints are set on the same line. + +.. + +.. date: 2026-10-18-16-09-24 +.. gh-issue: 159684 +.. nonce: VHVYjVuE +.. section: Library + +Update :mod:`configparser` and its tests to modern linter standards: +printf-style and :meth:`str.format` call sites are now f-strings, and a +handful of constructs have simpler spellings. No behavior change. + +.. + +.. date: 2026-10-18-12-20-00 +.. gh-issue: 159465 +.. nonce: partial-escapes +.. section: Library + +Preserve :exc:`SyntaxError` for malformed escapes in unfinished f-strings +and t-strings passed to :func:`codeop.compile_command`. + +.. + +.. date: 2026-10-18-08-30-10 +.. gh-issue: 155976 +.. nonce: feSum4_j +.. section: Library + +:func:`curses.set_term` and :meth:`curses.screen.use` now update +:data:`curses.LINES`, :data:`curses.COLS`, :data:`curses.COLORS` and +:data:`curses.COLOR_PAIRS` for the current screen. + +.. + +.. date: 2026-10-17-19-41-12 +.. gh-issue: 157210 +.. nonce: o9hycSeJ +.. section: Library + +Fix entering a combining character in the lower-right cell of +:class:`curses.textpad.Textbox`: it no longer takes a cell of its own, +pushing its base character off the window. In insert mode, a combining +character no longer shifts the following text. + +.. + +.. date: 2026-10-17-19-33-13 +.. gh-issue: 159643 +.. nonce: EJfe0IXetV +.. section: Library + +Fix :exc:`EOFError` raised by :mod:`tarfile` in stream mode with bz2, xz or +zstd compression when reading past the end of the compressed data, for +example with *ignore_zeros* or an archive without end-of-archive blocks. + +.. + +.. date: 2026-10-17-15-22-16 +.. gh-issue: 159620 +.. nonce: Ynsu5L +.. section: Library + +Fix ``python -m asyncio ps``, ``pstree`` and async-aware +:mod:`profiling.sampling` stacks stopping at asynchronous generators. Patch +by Timofei Ivankov. + +.. + +.. date: 2026-10-16-18-23-03 +.. gh-issue: 159558 +.. nonce: ctRelk +.. section: Library + +Speed up assignment to the ``value`` attribute of simple :mod:`ctypes` +types, to the ``contents`` attribute of :mod:`ctypes` pointers and to +:mod:`ctypes` structure fields, array items and pointer items on the +free-threaded build by not re-entering the critical section already held on +the object. + +.. + +.. date: 2026-10-16-13-00-00 +.. gh-issue: 159435 +.. nonce: OMNI +.. section: Library + +Fix an offset calculation in :mod:`tarfile` when a PAX header with a +``size`` record is followed by a GNU long-name header. Such archives could +duplicate or skip members while being read. + +.. + +.. date: 2026-10-16-10-25-22 +.. gh-issue: 159458 +.. nonce: q02EnY +.. section: Library + +Fix a ThreadSanitizer data race on the static cache in +``normalize_century()`` of the datetime module under the free-threaded +build. + +.. + +.. date: 2026-10-16-01-56-15 +.. gh-issue: 159503 +.. nonce: WgYiow +.. section: Library + +Fix quadratic copying in :mod:`asyncio` selector socket transports on +partial sends. A :class:`bytearray` passed to +:meth:`~asyncio.WriteTransport.write` now stays locked against resizing +until it has been sent. + +.. + +.. date: 2026-10-15-18-38-22 +.. gh-issue: 159500 +.. nonce: OjAQ8W +.. section: Library + +Speed up :meth:`asyncio.StreamReader.readline` and +:meth:`asyncio.StreamReader.readuntil` when the data is already buffered, +and stop :meth:`~asyncio.StreamReader.readuntil` with a tuple of separators +from scanning the whole buffer on every call. + +.. + +.. date: 2026-10-15-13-44-03 +.. gh-issue: 159465 +.. nonce: Ph-m8w +.. section: Library + +Improve incomplete input detection in :mod:`codeop` without an extra parser +pass, and preserve syntax and compiler errors. + +.. + +.. date: 2026-10-14-14-47-05 +.. gh-issue: 81954 +.. nonce: KvM0gY +.. section: Library + +Fix :class:`zipfile.ZipFile` not being closed at interpreter shutdown when +emitting its :exc:`ResourceWarning` failed with an :exc:`ImportError`. + +.. + +.. date: 2026-10-14-11-41-00 +.. gh-issue: 159430 +.. nonce: ZEW-mx +.. section: Library + +Report the correct exception if looking up ``__name__`` raises an exception +when calling :func:`repr` on a :class:`weakref.ref`. + +.. + +.. date: 2026-10-14-10-26-10 +.. gh-issue: 159387 +.. nonce: xMbQbB +.. section: Library + +Fix :mod:`ssl` reporting TLS 1.0, 1.1, and 1.2 as unavailable when built +with OpenSSL 4.0, despite support for these protocol versions. + +.. + +.. date: 2026-10-14-08-50-00 +.. gh-issue: 159398 +.. nonce: k2Xd7q +.. section: Library + +Fix the sampling profiler crashing when a string read from the target +process contains code points above ``U+10FFFF``. + +.. + +.. date: 2026-10-13-19-10-00 +.. gh-issue: 159322 +.. nonce: hP6k3A +.. section: Library + +Fix :mod:`tarfile` to NUL-terminate user and group names in PAX archive +headers and use extended headers for names longer than 31 characters. + +.. + +.. date: 2026-10-13-12-00-00 +.. gh-issue: 159304 +.. nonce: mFbYq2 +.. section: Library + +Fix :exc:`AttributeError` in :meth:`email.message.EmailMessage.get_body` +when a nested ``multipart`` part is malformed and its payload is a string. + +.. + +.. date: 2026-10-12-21-20-00 +.. gh-issue: 159225 +.. nonce: T8jlor +.. section: Library + +Prevent ``input`` calls in PyREPL from modifying readline history. + +.. + +.. date: 2026-10-12-18-21-06 +.. gh-issue: 159240 +.. nonce: a1b2c3 +.. section: Library + +Deprecate, and schedule for removal in Python 3.20, the ``__author__``, +``__credits__``, ``__copyright__`` and ``__date__`` attributes of +:mod:`bz2`, :mod:`concurrent.futures`, :mod:`inspect`, :mod:`io`, +:mod:`json`, :mod:`logging`, :mod:`optparse`, :mod:`platform`, :mod:`pydoc`, +:mod:`tarfile`, :mod:`tkinter.ttk`, :mod:`tokenize` and :mod:`uuid`. + +.. + +.. date: 2026-10-12-13-05-00 +.. gh-issue: 158501 +.. nonce: n7Qp3k +.. section: Library + +:meth:`datetime.time.fromisoformat` and +:meth:`datetime.datetime.fromisoformat` now reject stray text between the +time and a UTC offset. The C implementation previously accepted strings that +the pure-Python implementation rejected. + +.. + +.. date: 2026-10-12-12-00-00 +.. gh-issue: 72113 +.. nonce: njkm8v +.. section: Library + +Speed up creating a :mod:`ctypes` array from initializers, such as ``(c_int +* 3)(1, 2, 3)``, by roughly 3x. + +.. + +.. date: 2026-10-12-11-36-48 +.. gh-issue: 158856 +.. nonce: jLR-Lv +.. section: Library + +Improve large Enum creation performance. + +.. + +.. date: 2026-10-12-00-00-00 +.. gh-issue: 159044 +.. nonce: Tr8kXv +.. section: Library + +Restore the previous behaviour of +:meth:`http.client.HTTPConnection.set_tunnel` where header values that are +not strings (such as :const:`None`, :class:`int` or other objects) are +converted with :class:`str` instead of raising :exc:`AttributeError` when +the request is sent. + +.. + +.. date: 2026-10-11-13-40-44 +.. gh-issue: 159168 +.. nonce: z3KjB8 +.. section: Library + +Fix :exc:`RecursionError` in ``python -m asyncio pstree`` on a deep chain of +awaiting tasks. Patch by Timofei Ivankov. + +.. + +.. date: 2026-10-11-13-08-07 +.. gh-issue: 88037 +.. nonce: CzKcxh +.. section: Library + +:func:`urllib.parse.urlsplit` and :func:`urllib.parse.urlparse` now raise +:exc:`ValueError` for an invalid port when the URL is parsed, instead of +when the :attr:`!port` attribute is read. Patch by Miguel Brito and Senthil +Kumaran. + +.. + +.. date: 2026-10-11-08-42-00 +.. gh-issue: 159184 +.. nonce: Tq2nSe +.. section: Library + +The output of :func:`marshal.dumps` no longer depends on the order of the +elements of a set which have the same marshalled form, such as NaNs which +are distinct objects. + +.. + +.. date: 2026-10-10-22-29-13 +.. gh-issue: 159049 +.. nonce: eJUiKL +.. section: Library + +Fix reference cycles in :meth:`asyncio.loop.create_connection` and +:meth:`asyncio.loop.start_tls` after a failed TLS handshake. Patch by +Timofei Ivankov. + +.. + +.. date: 2026-10-10-22-14-22 +.. gh-issue: 150751 +.. nonce: JInuGL +.. section: Library + +:mod:`http.client` now ignores optional whitespace around the +``Content-Length`` header value when validating it, as required by +:rfc:`9112`. Previously a value with trailing whitespace (for example +``Content-Length: 5`` followed by a space) was treated as missing and the +response body was read until the connection was closed. + +.. + +.. date: 2026-10-08-21-02-38 +.. gh-issue: 158998 +.. nonce: DZYJI7 +.. section: Library + +Fix :func:`os.statx` ignoring the *follow_symlinks* argument. + +.. + +.. date: 2026-10-08-11-58-52 +.. gh-issue: 158628 +.. nonce: NJxwuw +.. section: Library + +Implement :data:`sys._wasi_info` for exposing WASI-specific system details. + +.. + +.. date: 2026-10-07-17-30-00 +.. gh-issue: 158287 +.. nonce: xY9zQp +.. section: Library + +Fix a bug in :mod:`asyncio` where stopping a child process (such as with +``SIGSTOP``) on macOS caused the event loop to block indefinitely. + +.. + +.. date: 2026-10-07-10-59-27 +.. gh-issue: 155233 +.. nonce: YGuQGK +.. section: Library + +Fix :meth:`asyncio.Barrier.wait` returning duplicate indices when a waiting +task is cancelled before the barrier is released.``` + +.. + +.. date: 2026-10-05-14-58-12 +.. gh-issue: 158839 +.. nonce: wYd6fI +.. section: Library + +The log functions in the :mod:`logging` module and the +:class:`logging.Logger` class now normalize falsy ``exc_info`` arguments to +``None`` before passing them to :meth:`logging.Logger.makeRecord` and +subsequently to :class:`logging.LogRecord`. This aligns +``logging.LogRecord.exc_info`` with the documentation. + +.. + +.. date: 2026-10-05-08-35-10 +.. gh-issue: 130698 +.. nonce: iibO9V +.. section: Library + +Fix PyREPL to convert :data:`sys.ps1` and :data:`sys.ps2` to strings before +use, and fall back to the default prompts if the conversion raises. + +.. + +.. date: 2026-09-29-19-34-30 +.. gh-issue: 158192 +.. nonce: 6wPjiU +.. section: Library + +Expose the future being waited on by a ``FutureIter`` as ``fi_future``. + +.. + +.. date: 2026-09-28-00-00-00 +.. gh-issue: 158370 +.. nonce: ctxmgr +.. section: Library + +:class:`concurrent.interpreters.Interpreter` objects now support the +:term:`context manager` protocol. + +.. + +.. date: 2026-09-27-14-00-00 +.. gh-issue: 158277 +.. nonce: aBcDeF +.. section: Library + +Fix :mod:`sqlite3` CLI completer to include view columns in column-name +completion. + +.. + +.. date: 2026-09-26-05-18-21 +.. gh-issue: 144650 +.. nonce: zqHLOW +.. section: Library + +Use Argument Clinic vectorcall support for the :class:`decimal.Decimal` +type. This offers 1.10x speedup on creation of small instances. + +.. + +.. date: 2026-09-25-17-04-45 +.. gh-issue: 158159 +.. nonce: lbEalG +.. section: Library + +Fix a use-after-free in :meth:`asyncio.Task.get_name` when formatting the +default task name triggers a garbage collection whose finalizers call +:meth:`~asyncio.Task.get_name` on the same task. + +.. + +.. date: 2026-09-25-01-29-55 +.. gh-issue: 158126 +.. nonce: nWmFnq +.. section: Library + +Speed up :func:`pickle.dumps` and creating a :class:`pickle.Pickler` by +avoiding the creation of an :exc:`AttributeError` when looking up the +optional ``reducer_override`` and ``dispatch_table`` attributes. + +.. + +.. date: 2026-09-24-14-59-08 +.. gh-issue: 158095 +.. nonce: lcKOT4 +.. section: Library + +Fix a memory leak in :func:`re.finditer` and regular expression scanners +when their input participates in a reference cycle. + +.. + +.. date: 2026-09-23-23-34-30 +.. gh-issue: 144812 +.. nonce: ujptbVOI +.. section: Library + +Make :class:`argparse.ArgumentGroup` and +:class:`argparse.MutuallyExclusiveGroup` public. The private +``_ArgumentGroup`` and ``_MutuallyExclusiveGroup`` names remain as aliases. + +.. + +.. date: 2026-09-23-19-59-03 +.. gh-issue: 158004 +.. nonce: p68swW +.. section: Library + +Fix :func:`asyncio.as_completed` to continue yielding every awaitable after +a pending iteration step is cancelled. + +.. + +.. date: 2026-09-23-09-21-25 +.. gh-issue: 89977 +.. nonce: fmia18 +.. section: Library + +Avoid releasing the GIL for :meth:`socket.socket.setblocking`, +:meth:`socket.socket.getsockname`, :meth:`socket.socket.getpeername`, and +:func:`socket.gethostname`. + +.. + +.. date: 2026-09-21-21-07-12 +.. gh-issue: 157928 +.. nonce: ZPlNYC +.. section: Library + +Correctly detect and handle OSC sequences in PyREPL. + +.. + +.. date: 2026-09-21-14-41-07 +.. gh-issue: 157899 +.. nonce: s6vBQC +.. section: Library + +:func:`os.readlink` now accepts a file descriptor referring to a symlink on +Linux, Android, macOS and iOS. + +.. + +.. date: 2026-09-20-21-24-05 +.. gh-issue: 157880 +.. nonce: aE7wIu +.. section: Library + +Fix :class:`mailbox.Babyl` to write the visible headers of a +:class:`mailbox.BabylMessage` to the mailbox file. + +.. + +.. date: 2026-09-20-12-00-00 +.. gh-issue: 157856 +.. nonce: wq4xuF +.. section: Library + +Fix :func:`asyncio.as_completed` raising :exc:`RuntimeError` ("no running +event loop") when the iterator is created outside of a running event loop +and driven with :meth:`loop.run_until_complete() +`. + +.. + +.. date: 2026-09-18-00-00-00 +.. gh-issue: 49960 +.. nonce: MDT3jE +.. section: Library + +:mod:`ctypes` callback functions can now return structures and unions by +value. Previously only "simple" types could be returned, and any other +result type raised :exc:`TypeError`. + +.. + +.. date: 2026-09-17-20-36-01 +.. gh-issue: 157290 +.. nonce: NfFmt +.. section: Library + +Add the ``%Nf`` format code (where *N* is from 1 to 6) to +:meth:`datetime.datetime.strftime`, :meth:`datetime.time.strftime`, +:meth:`datetime.date.strftime`, :meth:`datetime.datetime.strptime` and +:meth:`datetime.time.strptime`. + +.. + +.. date: 2026-09-17-10-30-00 +.. gh-issue: 158252 +.. nonce: strptime +.. section: Library + +Speed up :func:`time.strptime`, :meth:`datetime.date.strptime`, +:meth:`datetime.time.strptime`, and :meth:`datetime.datetime.strptime` for +common numeric formats by parsing fields and constructing the result in C. +Other inputs continue to use the Python parser. + +.. + +.. date: 2026-09-05-17-29-50 +.. gh-issue: 160837 +.. nonce: W6kHva +.. section: Library + +:func:`asyncio.print_call_graph` no longer truncates the call stack of a +task suspended inside an asynchronous generator. + +.. + +.. date: 2026-09-01-00-00-00 +.. gh-issue: 156784 +.. nonce: 8gvtt4 +.. section: Library + +:class:`asyncio.ProactorEventLoop` no longer silently ignores +:exc:`ConnectionResetError` on UDP sockets, matching +:class:`asyncio.SelectorEventLoop`. + +.. + +.. date: 2026-08-29-14-28-46 +.. gh-issue: 156568 +.. nonce: mkxLY8 +.. section: Library + +:func:`zipapp.create_archive` now correctly sets the executable bit on the +target archive when copying a zipapp where the target is a path-like object. + +.. + +.. date: 2026-08-29-13-41-56 +.. gh-issue: 154675 +.. nonce: cYvE-Z +.. section: Library + +Improve import time of :mod:`pdb`, :mod:`bdb` and :mod:`code` by lazily +importing several dependencies. + +.. + +.. date: 2026-08-23-16-33-00 +.. gh-issue: 151126 +.. nonce: GUvtG7 +.. section: Library + +Fix a crash caused by failing to set :exc:`MemoryError` on allocation +failure when first using a Chinese, Japanese or Korean codec. + +.. + +.. date: 2026-08-21-06-00-00 +.. gh-issue: 156057 +.. nonce: tarfile-wrap-comp-err +.. section: Library + +Wrap the underlying compression library's exception in +:exc:`tarfile.ReadError` when a member's compressed data is corrupt past its +header, for all of :mod:`tarfile`'s supported compression formats (gzip, +bzip2, lzma, zstd). Previously the raw exception (e.g. :exc:`zlib.error`) +could leak through :meth:`tarfile.TarFile.extractfile`. Patch by Jason Mak. + +.. + +.. date: 2026-08-20-09-56-45 +.. gh-issue: 156078 +.. nonce: -E7gZc +.. section: Library + +:meth:`zipfile.ZipFile.mkdir` now raises :exc:`ValueError` when the archive +is closed or when a writable member handle is open. + +.. + +.. date: 2026-08-15-12-08-33 +.. gh-issue: 155833 +.. nonce: Pi2Yjp +.. section: Library + +Ensure that MD5 and SHA* objects are properly GC-untracked before freeing +their internal HACL* state. Patch by Bénédikt Tran. + +.. + +.. date: 2026-08-15-11-49-30 +.. gh-issue: 155830 +.. nonce: cY5xgK +.. section: Library + +:mod:`!_blake2`: Ensure that BLAKE-2 objects are correctly initialized +before being tracked by the garbage collector. Patch by Bénédikt Tran. + +.. + +.. date: 2026-08-15-11-30-42 +.. gh-issue: 155782 +.. nonce: JUmDP4 +.. section: Library + +:mod:`ssl`: prevent a crash in :meth:`~ssl.SSLSocket.group` for session-less +objects on OpenSSL 3.2 and later. Patch by Bénédikt Tran. + +.. + +.. date: 2026-08-09-14-26-48 +.. gh-issue: 155428 +.. nonce: wi0_fI +.. section: Library + +Ensure that :func:`hmac.digest` and :meth:`hmac.HMAC.digest` output +identical values when using a digest's block size smaller than 16. Patch by +Bénédikt Tran. + +.. + +.. date: 2026-08-09-14-06-27 +.. gh-issue: 155426 +.. nonce: zZVPMd +.. section: Library + +Make sure that the pure Python fallback of :func:`hmac.digest` reject keys +that are neither :class:`bytes` or :class:`bytearray`. Previously, an +:exc:`AttributeError` was raised instead of :exc:`TypeError`. Patch by +Bénédikt Tran. + +.. + +.. date: 2026-08-05-10-13-23 +.. gh-issue: 153711 +.. nonce: 9g9dVz +.. section: Library + +On iOS, add :func:`os.pipe2`, and use the syscalls :manpage:`pipe2 (2)` and +:manpage:`dup3 (2)` internally, when available at both build-time and +run-time. + +.. + +.. date: 2026-08-04-16-38-59 +.. gh-issue: 155152 +.. nonce: qN1EEN +.. section: Library + +Improve performance of some :mod:`zipimport` archives that use Zstandard +compression. + +.. + +.. date: 2026-07-21-00-02-11 +.. gh-issue: 148228 +.. nonce: D3rP8m +.. section: Library + +The :term:`REPL` now displays error messages when editing commands cannot be +performed, such as when tab completion finds no matches or a history search +fails. Patch by Bartosz Sławecki. + +.. + +.. date: 2026-07-16-21-30-00 +.. gh-issue: 74876 +.. nonce: wR3fXk +.. section: Library + +Weak references (:class:`weakref.ref`), :class:`weakref.WeakMethod`, +:class:`weakref.WeakSet`, :class:`weakref.WeakValueDictionary` and +:class:`weakref.WeakKeyDictionary` can now be pickled if the weakly +referenced objects are strongly referenced elsewhere in the same pickle; +otherwise pickling raises :exc:`pickle.PicklingError`. +:class:`weakref.WeakMethod` and :class:`weakref.WeakSet` can now also be +copied. + +.. + +.. date: 2026-07-13-00-10-00 +.. gh-issue: 153679 +.. nonce: RJ2WGB +.. section: Library + +:class:`zipimport.zipimporter` now raises :exc:`zipimport.ZipImportError` +instead of :exc:`UnicodeDecodeError` for a UTF-8-flagged file name that is +not valid UTF-8. Patch by tonghuaroot. + +.. + +.. date: 2026-07-11-16-00-00 +.. gh-issue: 153605 +.. nonce: Rk7Xz3 +.. section: Library + +Fix :class:`gettext.GNUTranslations` raising :exc:`struct.error` instead of +:exc:`OSError` when reading a malformed ``.mo`` file. Patch by tonghuaroot. + +.. + +.. date: 2026-07-04-12-52-22 +.. gh-issue: 149816 +.. nonce: XQOutB +.. section: Library + +:mod:`hashlib`: fix a crash arising from a race condition when looking up an +OpenSSL hash algorithm by name the first time. Patch by Bénédikt Tran. + +.. + +.. date: 2026-06-30-08-39-07 +.. gh-issue: 62222 +.. nonce: Zq7x2P +.. section: Library + +Fix :func:`binascii.a2b_qp` and :mod:`quopri` discarding the character after +a ``=`` that does not start a valid escape: ``b'==41'`` now decodes to +``b'=A'``, as in the :mod:`email` package. Add a keyword-only *strip_ws* +parameter to :func:`binascii.a2b_qp`, :func:`quopri.decode` and +:func:`quopri.decodestring` to strip trailing whitespace from each line, as +required by :rfc:`2045`. + +.. + +.. date: 2026-06-28-14-37-01 +.. gh-issue: 152490 +.. nonce: A8qLbF +.. section: Library + +Fix :exc:`TypeError` when formatting a :exc:`SyntaxError` raised with +:data:`NotImplemented` as its argument, which caused unexpected REPL exit +and IDLE Shell restart. + +.. + +.. date: 2026-06-27-08-31-30 +.. gh-issue: 152373 +.. nonce: 3cd0aa +.. section: Library + +Fix the :mod:`pdb` ``until`` command not stepping over a list comprehension +when a breakpoint is set on it. Patch by tonghuaroot. + +.. + +.. date: 2026-06-18-11-26-00 +.. gh-issue: 151633 +.. nonce: uJzHdc +.. section: Library + +Fix a use-after-free under free-threading when multiple threads update the +same :class:`~collections.Counter` concurrently. + +.. + +.. date: 2026-05-21-16-15-01 +.. gh-issue: 105578 +.. nonce: oq-0pg +.. section: Library + +Deprecate :data:`typing.AnyStr` type variable and raise a warning when it is +imported. + +.. + +.. date: 2026-05-09-12-58-04 +.. gh-issue: 149527 +.. nonce: hX36Yh +.. section: Library + +:mod:`multiprocessing`: fix creation of temporary socket files following +:gh:`137335` (`PR#148578 `__ +and corresponding backports). Patch by Bénédikt Tran. + +.. + +.. date: 2026-05-03-22-52-07 +.. gh-issue: 149309 +.. nonce: gXhk9Y +.. section: Library + +:exc:`bdb.BdbQuit` is now a subclass of :exc:`BaseException`, instead of +:exc:`Exception`, to allow it to be used for cleanly exiting the debugger +without being accidentally caught by user code. + +.. + +.. date: 2026-04-27-20-30-16 +.. gh-issue: 149083 +.. nonce: xcmpSO +.. section: Library + +:data:`functools.Placeholder` is now a :class:`sentinel`. It is no longer an +instance of its own singleton type. + +.. + +.. date: 2026-03-28-14-34-18 +.. gh-issue: 143376 +.. nonce: FvWTwi +.. section: Library + +:mod:`concurrent.interpreters`: fix a crash when internal cross-interpreter +channel ends have incorrect channel IDs. Patch by Bénédikt Tran. + +.. + +.. date: 2026-03-28-10-27-46 +.. gh-issue: 146169 +.. nonce: RBF1xp +.. section: Library + +:mod:`xml.parsers.expat`: raise :exc:`RuntimeError` when an Expat handler +calls :meth:`parser.Parse ` or +:meth:`parser.ParseFile ` on the +parser that called the handler. Patch by Bénédikt Tran. + +.. + +.. date: 2026-03-23-00-04-13 +.. gh-issue: 146065 +.. nonce: FIdn8D +.. section: Library + +Fix a crash in asyncio.Future iterator when send() is called after throw() +or close(). + +.. + +.. date: 2026-01-06-09-13-53 +.. gh-issue: 84644 +.. nonce: V_cYP3 +.. section: Library + +Deprecate using a return annotation to infer the dispatch type for +:func:`functools.singledispatch` and +:class:`functools.singledispatchmethod`. It will raise :exc:`TypeError` in +Python 3.21. Annotate the dispatch parameter or pass the dispatch type +explicitly instead. Patch by Bartosz Sławecki. + +.. + +.. date: 2025-10-20-13-08-04 +.. gh-issue: 84027 +.. nonce: HjBO0B +.. section: Library + +:mod:`shutil` now can unpack wheel packages using the ``whl`` format. + +.. + +.. date: 2025-10-13-15-43-09 +.. gh-issue: 140036 +.. nonce: b_59uN +.. section: Library + +Avoid hanging in floor division of pure Python :class:`decimal.Decimal` +instances when the context precision is very large. Patch by Bénédikt Tran. + +.. + +.. date: 2025-09-18-18-03-32 +.. gh-issue: 139105 +.. nonce: 1QQ_W4 +.. section: Library + +Correct the EOF key shown in the :mod:`sqlite3` CLI ``.help`` text on +Windows. + +.. + +.. date: 2025-09-18-14-13-00 +.. gh-issue: 139122 +.. nonce: m3lp66 +.. section: Library + +:mod:`uuid`: Reimplement :class:`~uuid.UUID` objects, :func:`~uuid.uuid4`, +and :func:`~uuid.uuid7` in C. + +.. + +.. date: 2025-06-12-15-39-48 +.. gh-issue: 89730 +.. nonce: Lu35Fu +.. section: Library + +Add support for Encrypted Client Hello (ECH) to the :mod:`ssl` module with +OpenSSL 4.0 or later. + +.. + +.. date: 2025-03-13-20-21-00 +.. gh-issue: 115810 +.. nonce: qwrb231 +.. section: Library + +Add :attr:`datetime.timezone.name`, the *name* passed to the constructor, or +``None`` if no name was passed. + +.. + +.. date: 2025-02-04-09-01-06 +.. gh-issue: 129605 +.. nonce: wKGNXQ +.. section: Library + +Improve exception handling when computing suggestions in traceback.py. This +fixes a bug that could cause PyREPL to exit unexpectedly. + +.. + +.. date: 2024-12-27-15-41-55 +.. gh-issue: 128289 +.. nonce: -hCzEc +.. section: Library + +Add *eager_tasks* parameter to :func:`asyncio.run` function and +:class:`asyncio.Runner` class constructor. + +.. + +.. date: 2023-01-26-09-36-24 +.. gh-issue: 61448 +.. nonce: SOAkvt +.. section: Library + +Fix :func:`inspect.formatargvalues` raising :exc:`KeyError` for deleted +arguments, including ``*args`` and ``**kwargs``, by displaying +````. Patch by Ezio Melotti and Furkan Onder. + +.. + +.. bpo: 19094 +.. date: 2021-06-11-20-00-16 +.. nonce: rMRoIL +.. section: Library + +The :mod:`urllib.parse` URL parsing functions now raise :exc:`TypeError` +instead of :exc:`AttributeError` when passed an object which is not a +string, bytes, or ``None``. Accepting objects with false values (like ``0`` +and ``[]``) except empty strings, byte-like objects and ``None`` is now +deprecated, extending the deprecation made in Python 3.14 for +:func:`~urllib.parse.parse_qsl` and :func:`~urllib.parse.parse_qs`. + +.. + +.. bpo: 44170 +.. date: 2021-05-24-14-41-29 +.. nonce: _v28mv +.. section: Library + +Fix UnicodeDecodeError with multibyte utf8 characters in ShareableList. + +.. + +.. date: 2025-08-31-02-28-21 +.. gh-issue: 138277 +.. nonce: kkORph +.. section: Documentation + +Rearranged documentation search results to emphasize built-in types and +functions. Patch by Adam Hartz. + +.. + +.. date: 2023-09-04-16-01-20 +.. gh-issue: 107689 +.. nonce: LFLFtG +.. section: Documentation + +Added docstrings to abstract ctypes. + +.. + +.. date: 2026-11-07-01-45-00 +.. gh-issue: 160334 +.. nonce: F2n8qA +.. section: Tests + +Fix the sampling profiler integration test for special frames without file +and line information. + +.. + +.. date: 2026-11-06-23-15-23 +.. gh-issue: 161517 +.. nonce: LkAlDt +.. section: Tests + +Fix test_gdb when Python is built out-of-tree and Python is run from a +virtual environment. Previously, test_gdb was just skipped. Patch by Victor +Stinner. + +.. + +.. date: 2026-11-05-16-20-23 +.. gh-issue: 161430 +.. nonce: M7b8N6 +.. section: Tests + +No longer list tests expected to be skipped in the final report. Patch by +Victor Stinner. + +.. + +.. date: 2026-11-03-13-12-50 +.. gh-issue: 161263 +.. nonce: Voe8VM +.. section: Tests + +Skip tests which use MD5 algorithm for WWW-Authenticate challenges when +libssl is in FIPS mode. + +.. + +.. date: 2026-10-28-20-40-00 +.. gh-issue: 158646 +.. nonce: k2Xw7q +.. section: Tests + +Fix a race in ``test_asyncio.test_sslproto`` where the server side of +``test_shutdown_corrupted_ssl_sends_close_notify`` could fail with a +connection reset on Windows. + +.. + +.. date: 2026-10-19-17-05-00 +.. gh-issue: 107862 +.. nonce: qH3sTa +.. section: Tests + +Add ``hypothesis.stateful`` and the ``timezones()`` and ``ip_addresses()`` +strategies to the Hypothesis stubs used when Hypothesis is not installed. + +.. + +.. date: 2026-10-13-13-34-00 +.. gh-issue: 159293 +.. nonce: Lr8q2N +.. section: Tests + +Restore the recursion limit after the free-threading recursion-limit race +test to avoid failures in subsequent tests run in the same process. + +.. + +.. date: 2026-10-07-12-00-58 +.. gh-issue: 158975 +.. nonce: yR8q2L +.. section: Tests + +Replace fixed sleeps used for synchronization in multiprocessing tests with +bounded waits to avoid failures on slow or overloaded systems. Ensure pool +cleanup runs even when assertions fail in the imap buffer tests. + +.. + +.. date: 2026-10-05-22-36-06 +.. gh-issue: 158895 +.. nonce: 3Dmc4J +.. section: Tests + +When run in GitHub Actions, :mod:`test.regrtest` now annotates each test +failure in the job log and writes a summary of the failures to the job +summary. + +.. + +.. date: 2026-11-06-09-16-30 +.. gh-issue: 161467 +.. nonce: oSuzx_ +.. section: Build + +Enable the use of ``__attribute__((hot))`` when building CPython with Clang +and other compilers that support it. + +.. + +.. date: 2026-11-04-15-00-00 +.. gh-issue: 160924 +.. nonce: WQgVVh +.. section: Build + +Fix cross-compilation failures with ``--enable-shared`` caused by the build +Python loading the target's ``libpython`` through the shared-library search +path (such as musl from glibc). + +.. + +.. date: 2026-11-04-14-37-13 +.. gh-issue: 75929 +.. nonce: nELdwE +.. section: Build + +Fix a ``configure`` probe that could omit functions such as +:func:`os.fchdir` from builds using ``-Wall -Werror``. + +.. + +.. date: 2026-10-26-21-00-00 +.. gh-issue: 160704 +.. nonce: ZRYLKF +.. section: Build + +Fix C11-C17 builds with old C library headers that do not define +``static_assert``, including glibc versions older than 2.16. + +.. + +.. date: 2026-10-25-15-30-00 +.. gh-issue: 160689 +.. nonce: bootsp +.. section: Build + +The build helper programs ``Programs/_freeze_module`` and +``_bootstrap_python`` are now linked only with the modules of +:file:`Modules/Setup.bootstrap`, instead of with every static module. +Builtin modules added via :file:`Modules/Setup.local` or +``MODULE_BUILDTYPE=static`` can now depend on frozen modules or on +``getpath``. + +.. + +.. date: 2026-10-22-18-57-02 +.. gh-issue: 128884 +.. nonce: 3zR5Yp +.. section: Build + +Report a configuration error when ``--enable-bolt`` is used with build flags +that explicitly request the Cortex-A53 erratum 843419 workaround. Document +how to disable the workaround for builds that will not run on affected +processors. + +.. + +.. date: 2026-10-21-18-00-00 +.. gh-issue: 128883 +.. nonce: perf-trampoline +.. section: Build + +Fix crashes with ``-X perf`` and ``-X perf_jit`` in BOLT builds. The ELF +perf trampoline templates now use read-only data sections. + +.. + +.. date: 2026-10-21-14-52-56 +.. gh-issue: 128884 +.. nonce: Xs5jK- +.. section: Build + +Fix ``--enable-bolt`` build failures on AArch64 with recent LLVM: pass +``-frame-opt=hot`` only on x86. Patch by Donghee Na. + +.. + +.. date: 2026-10-20-23-30-00 +.. gh-issue: 160004 +.. nonce: CfgPth +.. section: Build + +Fix the paths in the :pep:`739` ``build-details.json`` file generated by +:file:`Tools/build/generate-build-details.py` with both ``--relative-paths`` +and ``--config-file-path``. Previously, every path except ``base_prefix`` +was wrong and depended on the directory the script was run from. + +.. + +.. date: 2026-10-20-23-00-00 +.. gh-issue: 159998 +.. nonce: FwDyn1 +.. section: Build + +Fix ``libpython.dynamic`` in the :pep:`739` ``build-details.json`` file for +macOS and iOS framework builds. It now points at the framework's library +instead of a nonexistent path under :file:`lib/`. + +.. + +.. date: 2026-10-20-20-00-00 +.. gh-issue: 144942 +.. nonce: MdLibs +.. section: Build + +Fix the macOS shared library and framework builds not linking the libraries +required by built-in modules, such as the HACL* objects needed by +``_blake2`` when it is built in via :file:`Modules/Setup.local`. + +.. + +.. date: 2026-10-20-17-50-00 +.. gh-issue: 143632 +.. nonce: PrCtlM +.. section: Build + +Fix the check that disables ``PR_SET_VMA_ANON_NAME`` on musl libc: it tested +an unset variable, so it never had any effect and the build could fail with +a redefinition of ``struct prctl_mm_map``. + +.. + +.. date: 2026-10-20-12-06-22 +.. gh-issue: 131999 +.. nonce: OWoYGM +.. section: Build + +Platform support for Apple's tvOS, watchOS and visionOS was added. + +.. + +.. date: 2026-10-20-00-56-59 +.. gh-issue: 153195 +.. nonce: 35cRE- +.. section: Build + +The PEG parser generator now supports splitting a grammar into several files +with the ``@include`` meta, and generates a separate C file for the rules of +each included file. The rules for specialized syntax errors moved from +:file:`Grammar/python.gram` to :file:`Grammar/invalid.gram`, which generates +the new :file:`Parser/parser_invalid.c`. + +.. + +.. date: 2026-10-19-17-05-00 +.. gh-issue: 159819 +.. nonce: pyCfgSp +.. section: Build + +Fix the shell version of :program:`python3-config` (used on all POSIX +platforms except macOS) when the installation prefix contains spaces or +other characters special to the shell or :program:`sed`: it printed an empty +``--prefix`` and wrong ``--includes``/``--ldflags`` paths while exiting +successfully. The script now also exits with status 1 if it cannot determine +its installation prefix. + +.. + +.. date: 2026-10-19-13-49-39 +.. gh-issue: 54767 +.. nonce: IdrGm6 +.. section: Build + +The ``--enable-loadable-sqlite-extensions=no`` and +``--disable-loadable-sqlite-extensions`` configure options now disable +loadable SQLite extensions instead of enabling them. + +.. + +.. date: 2026-10-18-12-00-00 +.. gh-issue: 159759 +.. nonce: LibStA +.. section: Build + +Fix the ``libpython.static`` path in :pep:`739` ``build-details.json``, +which pointed to a file that is not installed when building with +:option:`--enable-shared`, and no longer list a static library for framework +builds, which do not install one. + +.. + +.. date: 2026-10-15-19-09-31 +.. gh-issue: 75382 +.. nonce: DSrcra +.. section: Build + +Make :program:`configure` fail early with a clear error when a +``--with-*-sanitizer`` option is used but a program cannot be linked with +the corresponding ``-fsanitize`` flag, for example because the sanitizer +runtime library is not installed. + +.. + +.. date: 2026-10-15-17-19-41 +.. gh-issue: 159452 +.. nonce: oJ1saO +.. section: Build + +``Tools/build/regen-configure.sh`` no longer leaves root-owned files in the +source tree. + +.. + +.. date: 2026-10-15-11-20-48 +.. gh-issue: 159440 +.. nonce: uIz6ms +.. section: Build + +The version string reported by ``sys.version`` no longer carries a spurious +``-dirty`` suffix when CPython is built out-of-tree from a clean git +checkout: the build stamp now checks the source tree instead of the build +directory. + +.. + +.. date: 2026-09-22-09-13-06 +.. gh-issue: 157949 +.. nonce: EZ8avp +.. section: Build + +Remove dead build and preprocessor code for SCO UnixWare, OpenUNIX, SCO +OpenServer and ReliantUNIX. + +.. + +.. date: 2026-09-12-15-33-26 +.. gh-issue: 157354 +.. nonce: 2hw6fe +.. section: Build + +Fix the :samp:`--with-{name}-sanitizer=no` and +:samp:`--without-{name}-sanitizer` :program:`configure` options enabling the +sanitizer instead of disabling it. + +.. + +.. date: 2026-09-01-23-32-22 +.. gh-issue: 156780 +.. nonce: Qw3nRt +.. section: Build + +Emscripten ``libpython.a`` can now be linked into a program statically. + +.. + +.. date: 2026-08-27-23-24-29 +.. gh-issue: 155813 +.. nonce: LxRQu9 +.. section: Build + +On macOS, :option:`LIBFFI_CFLAGS` and :option:`LIBFFI_LIBS` are no longer +ignored when the SDK provides ``libffi``, so :mod:`ctypes` can again be +built against a different ``libffi``. Patch by Advit Arora. + +.. + +.. date: 2026-07-11-01-53-58 +.. gh-issue: 153285 +.. nonce: YmSox4 +.. section: Build + +Fix incorrect executable linking on macOS with ``--with-lto`` caused by +premature expansion of Make's ``$@`` target variable. + +.. + +.. date: 2026-10-22-14-04-17 +.. gh-issue: 160239 +.. nonce: A1b2C3 +.. section: Windows + +Remove dead and broken ``_repr_info()`` methods from +``asyncio.windows_events``; they called ``Future._repr_info()``, which no +longer exists. + +.. + +.. date: 2026-10-20-21-37-34 +.. gh-issue: 159880 +.. nonce: eKBiLr +.. section: Windows + +Fix a key handle leak in :func:`winreg.CreateKey`, +:func:`winreg.CreateKeyEx` and :func:`winreg.OpenKey` when the +``winreg.OpenKey/result`` audit hook fails. + +.. + +.. date: 2026-10-15-18-45-00 +.. gh-issue: 159118 +.. nonce: wmiMTA +.. section: Windows + +Fix a crash (``RPC_NT_INTERNAL_ERROR``) and intermittent window class +failures in :mod:`!_wmi` under high concurrency and load by initializing COM +as multi-threaded (MTA) instead of apartment-threaded (STA). + +.. + +.. date: 2026-09-14-16-49-44 +.. gh-issue: 157418 +.. nonce: r2IggA +.. section: Windows + +Correct the third parameter name of ``IocpProactor.recvfrom_into()`` from +*flags* to *nbytes* to reflect that it specifies the number of bytes to +receive. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: aaf575 +.. section: macOS + +Propagate errors that occur while converting macOS system proxy settings. + +.. + +.. date: 2026-11-04-16-34-48 +.. gh-issue: 161367 +.. nonce: bO5MGy +.. section: WASI + +Add a ``test`` command to :file:`Platforms/WASI` with ``--fast-ci`` and +``--slow-ci`` to select fast CI or slow Buildbot tests and ``--target`` to +test the build, gathered distribution, or release archive. + +.. + +.. date: 2026-10-21-15-26-12 +.. gh-issue: 160135 +.. nonce: UP-DlO +.. section: WASI + +Support wasm32-wasip3. + +.. + +.. date: 2026-11-06-17-12-40 +.. gh-issue: 161477 +.. nonce: smXIKC +.. section: IDLE + +Fix ``repr()`` of ``idlelib.redirector.WidgetRedirector`` and +``idlelib.redirector.OriginalCommand``, which displayed a spurious +one-element tuple. + +.. + +.. date: 2026-10-16-15-12-36 +.. gh-issue: 159517 +.. nonce: uzRtAd +.. section: IDLE + +Fix the completion list and the call tip grabbing the focus on macOS with +Tcl/Tk 9.1. + +.. + +.. date: 2026-10-21-11-53-40 +.. gh-issue: 87613 +.. nonce: tmKBlg +.. section: Tools/Demos + +Extend Argument Clinic :ref:`vectorcall` to support :ref:`heap-types` with +zero argument construction. + +.. + +.. date: 2026-10-20-12-50-00 +.. gh-issue: 114258 +.. nonce: slotNx +.. section: Tools/Demos + +Argument Clinic now supports the ``__next__`` type slot (``tp_iternext``), +including ``@critical_section``. + +.. + +.. date: 2026-10-14-09-48-26 +.. gh-issue: 159385 +.. nonce: 0aWOA9 +.. section: Tools/Demos + +iOS builds can now be configured to generate free-threading or debug output. + +.. + +.. date: 2025-03-25-18-00-00 +.. gh-issue: 131725 +.. nonce: qwfh321 +.. section: Tools/Demos + +``Tools/i18n/msgfmt.py`` now writes a hash table to the generated ``.mo`` +file, as GNU :program:`msgfmt` does. + +.. + +.. date: 2026-11-05-16-56-57 +.. gh-issue: 160109 +.. nonce: Fvs4m2 +.. section: C API + +Remove the unused ``FVS_MASK`` and ``FVS_HAVE_SPEC`` macros. They were used +by the ``FORMAT_VALUE`` opcode, which was removed in Python 3.13. + +.. + +.. date: 2026-11-05-10-51-17 +.. gh-issue: 161407 +.. nonce: rVoqaQ +.. section: C API + +Fix a crash at exit when an audit hook was added with +:c:func:`PySys_AddAuditHook` before :c:func:`Py_Initialize` and the raw +memory allocator changed during startup, for example with +``PYTHONMALLOC=mimalloc`` or in the :ref:`Python Development Mode +`. + +.. + +.. date: 2026-11-03-18-27-42 +.. gh-issue: 160962 +.. nonce: 06816I +.. section: C API + +Fix :c:func:`PyInitConfig_SetStr` when setting an optional string option to +``NULL``. Previously, the function crashed. Patch by Victor Stinner. + +.. + +.. date: 2026-11-03-01-24-01 +.. gh-issue: 161226 +.. nonce: MPrJKp +.. section: C API + +:c:func:`_PyTuple_Resize` now fails with :exc:`SystemError` if the hash +value was already computed. Patch by Victor Stinner. + +.. + +.. date: 2026-11-02-15-17-50 +.. gh-issue: 161185 +.. nonce: 4bpyDC +.. section: C API + +Fix a strict aliasing issue in the :c:func:`!memcpy` implementation of +:c:macro:`Py_SETREF`, :c:macro:`Py_XSETREF` and :c:macro:`Py_CLEAR` macros. +Patch by Nathan Goldbaum and Victor Stinner. + +.. + +.. date: 2026-11-02-13-15-22 +.. gh-issue: 161172 +.. nonce: U6kpuN +.. section: C API + +Add the :ref:`PyUnicodeWriter API ` to the limited C API. +Patch by Victor Stinner. + +.. + +.. date: 2026-10-21-19-55-00 +.. gh-issue: 159739 +.. nonce: ctxt01 +.. section: C API + +Avoid a crash if a context watcher is cleared while other context watchers +are being notified. + +.. + +.. date: 2026-10-19-21-05-12 +.. gh-issue: 159739 +.. nonce: Rk3fTq +.. section: C API + +Fix a crash in :c:func:`_PyTuple_Resize` when resizing a non-empty tuple to +a negative or too large size. It now raises :exc:`SystemError` or +:exc:`MemoryError` respectively. + +.. + +.. date: 2026-10-19-20-38-17 +.. gh-issue: 159739 +.. nonce: eb9cdc +.. section: C API + +:c:func:`PyInitConfig_SetStr` now releases a previously configured value and +preserves it if conversion of the replacement fails. + +.. + +.. date: 2026-10-19-19-33-35 +.. gh-issue: 159739 +.. nonce: dee9a0 +.. section: C API + +Fix a reference leak when :c:func:`_PyTuple_Resize` fails. + +.. + +.. date: 2026-09-12-18-37-22 +.. gh-issue: 157384 +.. nonce: uFbdle +.. section: C API + +:c:func:`PyImport_CreateModuleFromInitfunc` now accepts non-ASCII module +names for multi-phase init modules, instead of raising +:exc:`UnicodeEncodeError`. + +.. + +.. date: 2026-08-20-12-00-00 +.. gh-issue: 156133 +.. nonce: GetLoc +.. section: C API + +Add :c:func:`PyUnstable_InterpreterFrame_GetLocal` to read a local variable +of an internal interpreter frame by its localsplus index, and +:c:func:`PyUnstable_Code_GetLocalPlusNames` to get the matching variable +names. + +.. + +.. date: 2026-07-09-13-52-17 +.. gh-issue: 153410 +.. nonce: neaDgY +.. section: C API + +Add :c:func:`PyDict_AsFrozenDictAndClear` C-API function to create +:class:`frozendict` from existing :class:`dict` object and clear the input +dict instance. diff --git a/Misc/NEWS.d/next/Build/2026-08-27-23-24-29.gh-issue-155813.LxRQu9.rst b/Misc/NEWS.d/next/Build/2026-08-27-23-24-29.gh-issue-155813.LxRQu9.rst deleted file mode 100644 index c614826ba4c19a..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-08-27-23-24-29.gh-issue-155813.LxRQu9.rst +++ /dev/null @@ -1,3 +0,0 @@ -On macOS, :option:`LIBFFI_CFLAGS` and :option:`LIBFFI_LIBS` are no longer -ignored when the SDK provides ``libffi``, so :mod:`ctypes` can again be built -against a different ``libffi``. Patch by Advit Arora. diff --git a/Misc/NEWS.d/next/Build/2026-09-01-23-32-22.gh-issue-156780.Qw3nRt.rst b/Misc/NEWS.d/next/Build/2026-09-01-23-32-22.gh-issue-156780.Qw3nRt.rst deleted file mode 100644 index fe664df6909740..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-09-01-23-32-22.gh-issue-156780.Qw3nRt.rst +++ /dev/null @@ -1 +0,0 @@ -Emscripten ``libpython.a`` can now be linked into a program statically. diff --git a/Misc/NEWS.d/next/Build/2026-09-12-15-33-26.gh-issue-157354.2hw6fe.rst b/Misc/NEWS.d/next/Build/2026-09-12-15-33-26.gh-issue-157354.2hw6fe.rst deleted file mode 100644 index 5bc35aaf9870c2..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-09-12-15-33-26.gh-issue-157354.2hw6fe.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix the :samp:`--with-{name}-sanitizer=no` and :samp:`--without-{name}-sanitizer` -:program:`configure` options enabling the sanitizer instead of disabling it. diff --git a/Misc/NEWS.d/next/Build/2026-09-22-09-13-06.gh-issue-157949.EZ8avp.rst b/Misc/NEWS.d/next/Build/2026-09-22-09-13-06.gh-issue-157949.EZ8avp.rst deleted file mode 100644 index 0b1034807b7dd1..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-09-22-09-13-06.gh-issue-157949.EZ8avp.rst +++ /dev/null @@ -1,2 +0,0 @@ -Remove dead build and preprocessor code for SCO UnixWare, OpenUNIX, SCO -OpenServer and ReliantUNIX. diff --git a/Misc/NEWS.d/next/Build/2026-10-15-11-20-48.gh-issue-159440.uIz6ms.rst b/Misc/NEWS.d/next/Build/2026-10-15-11-20-48.gh-issue-159440.uIz6ms.rst deleted file mode 100644 index 6c6cc43fd2e60d..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-15-11-20-48.gh-issue-159440.uIz6ms.rst +++ /dev/null @@ -1,3 +0,0 @@ -The version string reported by ``sys.version`` no longer carries a spurious -``-dirty`` suffix when CPython is built out-of-tree from a clean git checkout: -the build stamp now checks the source tree instead of the build directory. diff --git a/Misc/NEWS.d/next/Build/2026-10-15-19-09-31.gh-issue-75382.DSrcra.rst b/Misc/NEWS.d/next/Build/2026-10-15-19-09-31.gh-issue-75382.DSrcra.rst deleted file mode 100644 index eb295fd292a611..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-15-19-09-31.gh-issue-75382.DSrcra.rst +++ /dev/null @@ -1,4 +0,0 @@ -Make :program:`configure` fail early with a clear error when a -``--with-*-sanitizer`` option is used but a program cannot be linked with -the corresponding ``-fsanitize`` flag, for example because the sanitizer -runtime library is not installed. diff --git a/Misc/NEWS.d/next/Build/2026-10-18-12-00-00.gh-issue-159759.LibStA.rst b/Misc/NEWS.d/next/Build/2026-10-18-12-00-00.gh-issue-159759.LibStA.rst deleted file mode 100644 index 1fb6a51fded6dc..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-18-12-00-00.gh-issue-159759.LibStA.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix the ``libpython.static`` path in :pep:`739` ``build-details.json``, which -pointed to a file that is not installed when building with -:option:`--enable-shared`, and no longer list a static library for framework -builds, which do not install one. diff --git a/Misc/NEWS.d/next/Build/2026-10-19-13-49-39.gh-issue-54767.IdrGm6.rst b/Misc/NEWS.d/next/Build/2026-10-19-13-49-39.gh-issue-54767.IdrGm6.rst deleted file mode 100644 index 6154892709ee12..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-19-13-49-39.gh-issue-54767.IdrGm6.rst +++ /dev/null @@ -1,3 +0,0 @@ -The ``--enable-loadable-sqlite-extensions=no`` and -``--disable-loadable-sqlite-extensions`` configure options now disable -loadable SQLite extensions instead of enabling them. diff --git a/Misc/NEWS.d/next/Build/2026-10-19-17-05-00.gh-issue-159819.pyCfgSp.rst b/Misc/NEWS.d/next/Build/2026-10-19-17-05-00.gh-issue-159819.pyCfgSp.rst deleted file mode 100644 index a7fcdaa05471eb..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-19-17-05-00.gh-issue-159819.pyCfgSp.rst +++ /dev/null @@ -1,6 +0,0 @@ -Fix the shell version of :program:`python3-config` (used on all POSIX -platforms except macOS) when the installation prefix contains spaces or -other characters special to the shell or :program:`sed`: it printed an -empty ``--prefix`` and wrong ``--includes``/``--ldflags`` paths while -exiting successfully. The script now also exits with status 1 if it cannot -determine its installation prefix. diff --git a/Misc/NEWS.d/next/Build/2026-10-20-00-56-59.gh-issue-153195.35cRE-.rst b/Misc/NEWS.d/next/Build/2026-10-20-00-56-59.gh-issue-153195.35cRE-.rst deleted file mode 100644 index 75cc37cbe62d6a..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-20-00-56-59.gh-issue-153195.35cRE-.rst +++ /dev/null @@ -1,5 +0,0 @@ -The PEG parser generator now supports splitting a grammar into several files -with the ``@include`` meta, and generates a separate C file for the rules of -each included file. The rules for specialized syntax errors moved from -:file:`Grammar/python.gram` to :file:`Grammar/invalid.gram`, which generates -the new :file:`Parser/parser_invalid.c`. diff --git a/Misc/NEWS.d/next/Build/2026-10-20-12-06-22.gh-issue-131999.OWoYGM.rst b/Misc/NEWS.d/next/Build/2026-10-20-12-06-22.gh-issue-131999.OWoYGM.rst deleted file mode 100644 index 8f282fa2d14c89..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-20-12-06-22.gh-issue-131999.OWoYGM.rst +++ /dev/null @@ -1 +0,0 @@ -Platform support for Apple's tvOS, watchOS and visionOS was added. diff --git a/Misc/NEWS.d/next/Build/2026-10-20-17-50-00.gh-issue-143632.PrCtlM.rst b/Misc/NEWS.d/next/Build/2026-10-20-17-50-00.gh-issue-143632.PrCtlM.rst deleted file mode 100644 index 5a2c7b8208cd36..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-20-17-50-00.gh-issue-143632.PrCtlM.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix the check that disables ``PR_SET_VMA_ANON_NAME`` on musl libc: it tested -an unset variable, so it never had any effect and the build could fail with -a redefinition of ``struct prctl_mm_map``. diff --git a/Misc/NEWS.d/next/Build/2026-10-20-20-00-00.gh-issue-144942.MdLibs.rst b/Misc/NEWS.d/next/Build/2026-10-20-20-00-00.gh-issue-144942.MdLibs.rst deleted file mode 100644 index 94b25d67af2fe4..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-20-20-00-00.gh-issue-144942.MdLibs.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix the macOS shared library and framework builds not linking the libraries -required by built-in modules, such as the HACL* objects needed by ``_blake2`` -when it is built in via :file:`Modules/Setup.local`. diff --git a/Misc/NEWS.d/next/Build/2026-10-20-23-00-00.gh-issue-159998.FwDyn1.rst b/Misc/NEWS.d/next/Build/2026-10-20-23-00-00.gh-issue-159998.FwDyn1.rst deleted file mode 100644 index 33a5c6414936e7..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-20-23-00-00.gh-issue-159998.FwDyn1.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix ``libpython.dynamic`` in the :pep:`739` ``build-details.json`` file for -macOS and iOS framework builds. It now points at the framework's library -instead of a nonexistent path under :file:`lib/`. diff --git a/Misc/NEWS.d/next/Build/2026-10-20-23-30-00.gh-issue-160004.CfgPth.rst b/Misc/NEWS.d/next/Build/2026-10-20-23-30-00.gh-issue-160004.CfgPth.rst deleted file mode 100644 index 247f4677fc27c6..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-20-23-30-00.gh-issue-160004.CfgPth.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix the paths in the :pep:`739` ``build-details.json`` file generated by -:file:`Tools/build/generate-build-details.py` with both ``--relative-paths`` -and ``--config-file-path``. Previously, every path except ``base_prefix`` was -wrong and depended on the directory the script was run from. diff --git a/Misc/NEWS.d/next/Build/2026-10-21-14-52-56.gh-issue-128884.Xs5jK-.rst b/Misc/NEWS.d/next/Build/2026-10-21-14-52-56.gh-issue-128884.Xs5jK-.rst deleted file mode 100644 index 53233d06c5053a..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-21-14-52-56.gh-issue-128884.Xs5jK-.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix ``--enable-bolt`` build failures on AArch64 with recent LLVM: pass -``-frame-opt=hot`` only on x86. Patch by Donghee Na. diff --git a/Misc/NEWS.d/next/Build/2026-10-21-18-00-00.gh-issue-128883.perf-trampoline.rst b/Misc/NEWS.d/next/Build/2026-10-21-18-00-00.gh-issue-128883.perf-trampoline.rst deleted file mode 100644 index e250415e72af14..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-21-18-00-00.gh-issue-128883.perf-trampoline.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix crashes with ``-X perf`` and ``-X perf_jit`` in BOLT builds. -The ELF perf trampoline templates now use read-only data sections. diff --git a/Misc/NEWS.d/next/Build/2026-10-22-18-57-02.gh-issue-128884.3zR5Yp.rst b/Misc/NEWS.d/next/Build/2026-10-22-18-57-02.gh-issue-128884.3zR5Yp.rst deleted file mode 100644 index 8d0227cf684144..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-22-18-57-02.gh-issue-128884.3zR5Yp.rst +++ /dev/null @@ -1,3 +0,0 @@ -Report a configuration error when ``--enable-bolt`` is used with build flags -that explicitly request the Cortex-A53 erratum 843419 workaround. Document how to -disable the workaround for builds that will not run on affected processors. diff --git a/Misc/NEWS.d/next/Build/2026-10-25-15-30-00.gh-issue-160689.bootsp.rst b/Misc/NEWS.d/next/Build/2026-10-25-15-30-00.gh-issue-160689.bootsp.rst deleted file mode 100644 index e83c9708142b39..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-25-15-30-00.gh-issue-160689.bootsp.rst +++ /dev/null @@ -1,5 +0,0 @@ -The build helper programs ``Programs/_freeze_module`` and -``_bootstrap_python`` are now linked only with the modules of -:file:`Modules/Setup.bootstrap`, instead of with every static module. Builtin -modules added via :file:`Modules/Setup.local` or ``MODULE_BUILDTYPE=static`` -can now depend on frozen modules or on ``getpath``. diff --git a/Misc/NEWS.d/next/Build/2026-10-26-21-00-00.gh-issue-160704.ZRYLKF.rst b/Misc/NEWS.d/next/Build/2026-10-26-21-00-00.gh-issue-160704.ZRYLKF.rst deleted file mode 100644 index 330415c155cd89..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-10-26-21-00-00.gh-issue-160704.ZRYLKF.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix C11-C17 builds with old C library headers that do not define -``static_assert``, including glibc versions older than 2.16. diff --git a/Misc/NEWS.d/next/Build/2026-11-04-14-37-13.gh-issue-75929.nELdwE.rst b/Misc/NEWS.d/next/Build/2026-11-04-14-37-13.gh-issue-75929.nELdwE.rst deleted file mode 100644 index 8ec7f8a0147399..00000000000000 --- a/Misc/NEWS.d/next/Build/2026-11-04-14-37-13.gh-issue-75929.nELdwE.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a ``configure`` probe that could omit functions such -as :func:`os.fchdir` from builds using ``-Wall -Werror``. diff --git a/Misc/NEWS.d/next/Build/2026-11-06-12-00-00.gh-issue-140266.K3hP9z.rst b/Misc/NEWS.d/next/Build/2026-11-06-12-00-00.gh-issue-140266.K3hP9z.rst new file mode 100644 index 00000000000000..399aca8a9bcff6 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-06-12-00-00.gh-issue-140266.K3hP9z.rst @@ -0,0 +1,3 @@ +Fix ``"platform": ""`` in ``build-details.json`` for native builds configured +with :option:`--with-build-python`: :envvar:`!_PYTHON_HOST_PLATFORM` is now +only passed to the build interpreter when cross compiling. diff --git a/Misc/NEWS.d/next/Build/2026-11-06-12-00-01.gh-issue-136267.Qv7mRs.rst b/Misc/NEWS.d/next/Build/2026-11-06-12-00-01.gh-issue-136267.Qv7mRs.rst new file mode 100644 index 00000000000000..43cc054a04ac7a --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-06-12-00-01.gh-issue-136267.Qv7mRs.rst @@ -0,0 +1,8 @@ +``build-details.json`` now describes the interpreter being built rather than +the interpreter running :program:`generate-build-details.py`: ``abi.flags``, +``suffixes.extensions``, ``abi.stable_abi_suffix`` and +``implementation.supports_isolated_interpreters`` are derived from the +target's configuration, which fixes cross builds and builds that use +:option:`--with-build-python`. ``abi.extension_suffix`` and +``abi.stable_abi_suffix`` are now also present when libpython is only built +as a static library. diff --git a/Misc/NEWS.d/next/Build/2026-11-06-12-00-02.gh-issue-136267.Vt4pLm.rst b/Misc/NEWS.d/next/Build/2026-11-06-12-00-02.gh-issue-136267.Vt4pLm.rst new file mode 100644 index 00000000000000..d99ae1c4213340 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-06-12-00-02.gh-issue-136267.Vt4pLm.rst @@ -0,0 +1,4 @@ +``build-details.json`` now takes ``language.version_info``, +``implementation.version`` and ``implementation.hexversion`` from +:file:`Include/patchlevel.h` instead of the interpreter running the generator, +which can be a different patch release under :option:`--with-build-python`. diff --git a/Misc/NEWS.d/next/Build/2026-11-11-11-36-01.gh-issue-161821.9wjElMQP.rst b/Misc/NEWS.d/next/Build/2026-11-11-11-36-01.gh-issue-161821.9wjElMQP.rst new file mode 100644 index 00000000000000..2cfece1209c6e9 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-11-11-36-01.gh-issue-161821.9wjElMQP.rst @@ -0,0 +1,4 @@ +When building with ``--with-lto`` and Clang, :program:`configure` now checks +that the linker can link LTO objects and falls back to ``lld`` +(``-fuse-ld=lld``) if it cannot. This fixes LTO builds on systems where the +default GNU linker lacks the ``LLVMgold.so`` plugin, such as Debian 13. diff --git a/Misc/NEWS.d/next/Build/2026-11-17-20-12-03.gh-issue-162170.0SpcRZ.rst b/Misc/NEWS.d/next/Build/2026-11-17-20-12-03.gh-issue-162170.0SpcRZ.rst new file mode 100644 index 00000000000000..caa4b13ce30339 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-17-20-12-03.gh-issue-162170.0SpcRZ.rst @@ -0,0 +1,3 @@ +``make install`` now depends on the ``all`` target, so running it directly +after ``./configure --enable-optimizations`` installs the PGO-optimized +interpreter rather than an unoptimized build. diff --git a/Misc/NEWS.d/next/Build/2026-11-19-17-34-03.gh-issue-153668.A7mK2q.rst b/Misc/NEWS.d/next/Build/2026-11-19-17-34-03.gh-issue-153668.A7mK2q.rst new file mode 100644 index 00000000000000..95de6504bdbd07 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-19-17-34-03.gh-issue-153668.A7mK2q.rst @@ -0,0 +1,2 @@ +Disable ``unicodectype.c`` optimization in regular Windows ARM64 Release +builds due to a compiler bug in MSVC 14.50, 14.51, and 14.52. PGO builds are not affected. diff --git a/Misc/NEWS.d/next/Build/2026-11-20-16-30-00.gh-issue-162308.Mk7pQ2.rst b/Misc/NEWS.d/next/Build/2026-11-20-16-30-00.gh-issue-162308.Mk7pQ2.rst new file mode 100644 index 00000000000000..90a734551591f8 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-20-16-30-00.gh-issue-162308.Mk7pQ2.rst @@ -0,0 +1,4 @@ +Use the newly built interpreter to check extension modules in native builds +configured with :option:`--with-build-python`. This also applies when +generating missing standard library module information. Cross builds continue +to use the build interpreter without importing target extension modules. diff --git a/Misc/NEWS.d/next/C_API/2026-07-09-13-52-17.gh-issue-153410.neaDgY.rst b/Misc/NEWS.d/next/C_API/2026-07-09-13-52-17.gh-issue-153410.neaDgY.rst deleted file mode 100644 index 259981100b5bf1..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-07-09-13-52-17.gh-issue-153410.neaDgY.rst +++ /dev/null @@ -1,3 +0,0 @@ -Add :c:func:`PyDict_AsFrozenDictAndClear` C-API function to create -:class:`frozendict` from existing :class:`dict` object and clear the input -dict instance. diff --git a/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst b/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst new file mode 100644 index 00000000000000..17c2a58555e7ae --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst @@ -0,0 +1,2 @@ +In ``pyport.h`` and ``pyatomic.h`` don't use ``_Py_STDC_VERSION`` when it is +not defined, fixing build errors for C or C++ code built with ``-Werror=undef``. diff --git a/Misc/NEWS.d/next/C_API/2026-08-20-12-00-00.gh-issue-156133.GetLoc.rst b/Misc/NEWS.d/next/C_API/2026-08-20-12-00-00.gh-issue-156133.GetLoc.rst deleted file mode 100644 index be780cba1214d3..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-08-20-12-00-00.gh-issue-156133.GetLoc.rst +++ /dev/null @@ -1,3 +0,0 @@ -Add :c:func:`PyUnstable_InterpreterFrame_GetLocal` to read a local variable of -an internal interpreter frame by its localsplus index, and -:c:func:`PyUnstable_Code_GetLocalPlusNames` to get the matching variable names. diff --git a/Misc/NEWS.d/next/C_API/2026-08-20-15-21-46.gh-issue-155628.9QggKx.rst b/Misc/NEWS.d/next/C_API/2026-08-20-15-21-46.gh-issue-155628.9QggKx.rst new file mode 100644 index 00000000000000..023cad615ecdfc --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-08-20-15-21-46.gh-issue-155628.9QggKx.rst @@ -0,0 +1,6 @@ +Add the internal ``_Py_atomic_add_*_relaxed`` and +``_Py_atomic_compare_exchange_*_relaxed`` functions. CPython uses relaxed +addition to assign module indices. Free-threaded builds also use the new +operations to update the garbage collector's allocation count, track +:func:`functools.lru_cache` hits and misses, and generate default +:mod:`asyncio` task names. diff --git a/Misc/NEWS.d/next/C_API/2026-09-12-18-37-22.gh-issue-157384.uFbdle.rst b/Misc/NEWS.d/next/C_API/2026-09-12-18-37-22.gh-issue-157384.uFbdle.rst deleted file mode 100644 index 0ba43518705c50..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-09-12-18-37-22.gh-issue-157384.uFbdle.rst +++ /dev/null @@ -1,3 +0,0 @@ -:c:func:`PyImport_CreateModuleFromInitfunc` now accepts non-ASCII module -names for multi-phase init modules, instead of raising -:exc:`UnicodeEncodeError`. diff --git a/Misc/NEWS.d/next/C_API/2026-10-19-19-33-35.gh-issue-159739.dee9a0.rst b/Misc/NEWS.d/next/C_API/2026-10-19-19-33-35.gh-issue-159739.dee9a0.rst deleted file mode 100644 index 891cf551730af2..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-10-19-19-33-35.gh-issue-159739.dee9a0.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a reference leak when :c:func:`_PyTuple_Resize` fails. diff --git a/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst b/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst deleted file mode 100644 index e3ca598d37fdce..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst +++ /dev/null @@ -1,2 +0,0 @@ -:c:func:`PyInitConfig_SetStr` now releases a previously configured value and -preserves it if conversion of the replacement fails. diff --git a/Misc/NEWS.d/next/C_API/2026-10-19-21-05-12.gh-issue-159739.Rk3fTq.rst b/Misc/NEWS.d/next/C_API/2026-10-19-21-05-12.gh-issue-159739.Rk3fTq.rst deleted file mode 100644 index 2d510c63ac07a8..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-10-19-21-05-12.gh-issue-159739.Rk3fTq.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash in :c:func:`_PyTuple_Resize` when resizing a non-empty tuple to a -negative or too large size. It now raises :exc:`SystemError` or -:exc:`MemoryError` respectively. diff --git a/Misc/NEWS.d/next/C_API/2026-10-21-19-55-00.gh-issue-159739.ctxt01.rst b/Misc/NEWS.d/next/C_API/2026-10-21-19-55-00.gh-issue-159739.ctxt01.rst deleted file mode 100644 index 10c268e1043c9e..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-10-21-19-55-00.gh-issue-159739.ctxt01.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid a crash if a context watcher is cleared while other context watchers -are being notified. diff --git a/Misc/NEWS.d/next/C_API/2026-11-02-13-15-22.gh-issue-161172.U6kpuN.rst b/Misc/NEWS.d/next/C_API/2026-11-02-13-15-22.gh-issue-161172.U6kpuN.rst deleted file mode 100644 index 638290ef247f37..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-11-02-13-15-22.gh-issue-161172.U6kpuN.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add the :ref:`PyUnicodeWriter API ` to the limited C API. -Patch by Victor Stinner. diff --git a/Misc/NEWS.d/next/C_API/2026-11-02-15-17-50.gh-issue-161185.4bpyDC.rst b/Misc/NEWS.d/next/C_API/2026-11-02-15-17-50.gh-issue-161185.4bpyDC.rst deleted file mode 100644 index 92887e84ade4cd..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-11-02-15-17-50.gh-issue-161185.4bpyDC.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a strict aliasing issue in the :c:func:`!memcpy` implementation of -:c:macro:`Py_SETREF`, :c:macro:`Py_XSETREF` and :c:macro:`Py_CLEAR` macros. -Patch by Nathan Goldbaum and Victor Stinner. diff --git a/Misc/NEWS.d/next/C_API/2026-11-03-01-24-01.gh-issue-161226.MPrJKp.rst b/Misc/NEWS.d/next/C_API/2026-11-03-01-24-01.gh-issue-161226.MPrJKp.rst deleted file mode 100644 index 601db29c66cd9b..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-11-03-01-24-01.gh-issue-161226.MPrJKp.rst +++ /dev/null @@ -1,2 +0,0 @@ -:c:func:`_PyTuple_Resize` now fails with :exc:`SystemError` if the hash value -was already computed. Patch by Victor Stinner. diff --git a/Misc/NEWS.d/next/C_API/2026-11-03-18-27-42.gh-issue-160962.06816I.rst b/Misc/NEWS.d/next/C_API/2026-11-03-18-27-42.gh-issue-160962.06816I.rst deleted file mode 100644 index 3b043a8d495f1c..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-11-03-18-27-42.gh-issue-160962.06816I.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :c:func:`PyInitConfig_SetStr` when setting an optional string option to -``NULL``. Previously, the function crashed. Patch by Victor Stinner. diff --git a/Misc/NEWS.d/next/C_API/2026-11-04-14-24-36.gh-issue-160436.WmZEYT.rst b/Misc/NEWS.d/next/C_API/2026-11-04-14-24-36.gh-issue-160436.WmZEYT.rst new file mode 100644 index 00000000000000..4cef63faaa4cdf --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-04-14-24-36.gh-issue-160436.WmZEYT.rst @@ -0,0 +1,4 @@ +Fix a spurious ``-Wdeprecated-declarations`` warning when a +:c:type:`PyStatus` is copied in C++ with clang 21 and older: +:c:member:`PyStatus.exitcode` is no longer marked as deprecated for C++ on +those compilers. diff --git a/Misc/NEWS.d/next/C_API/2026-11-05-16-56-57.gh-issue-160109.Fvs4m2.rst b/Misc/NEWS.d/next/C_API/2026-11-05-16-56-57.gh-issue-160109.Fvs4m2.rst deleted file mode 100644 index 3f41492e70c8d9..00000000000000 --- a/Misc/NEWS.d/next/C_API/2026-11-05-16-56-57.gh-issue-160109.Fvs4m2.rst +++ /dev/null @@ -1,2 +0,0 @@ -Remove the unused ``FVS_MASK`` and ``FVS_HAVE_SPEC`` macros. They were used by -the ``FORMAT_VALUE`` opcode, which was removed in Python 3.13. diff --git a/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst b/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst new file mode 100644 index 00000000000000..e82a0a267cfe49 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-12-21-34-55.gh-issue-161694.71tZlp.rst @@ -0,0 +1,2 @@ +:c:func:`!_PyObject_VAR_SIZE` now returns ``SIZE_MAX`` on integer overflow (if +the result would be greater than ``PY_SSIZE_T_MAX``). Patch by Victor Stinner. diff --git a/Misc/NEWS.d/next/C_API/2026-11-15-12-00-00.gh-issue-162054.vSnPrf.rst b/Misc/NEWS.d/next/C_API/2026-11-15-12-00-00.gh-issue-162054.vSnPrf.rst new file mode 100644 index 00000000000000..116aaabcd4e84e --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-15-12-00-00.gh-issue-162054.vSnPrf.rst @@ -0,0 +1,4 @@ +:c:func:`PyOS_vsnprintf` now calls the standard C11 :c:func:`!vsnprintf` on Windows, instead of +the non-standard :c:func:`!_vsnprintf`. When the output is truncated, it now +returns the number of bytes that would have been written (excluding the null byte), as on other +platforms, instead of a negative value. diff --git a/Misc/NEWS.d/next/C_API/2026-11-16-17-25-12.gh-issue-158401.ADBBRG.rst b/Misc/NEWS.d/next/C_API/2026-11-16-17-25-12.gh-issue-158401.ADBBRG.rst new file mode 100644 index 00000000000000..f1b59a75095b2a --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-16-17-25-12.gh-issue-158401.ADBBRG.rst @@ -0,0 +1,4 @@ +:c:func:`Py_INCREF`, :c:func:`Py_XINCREF`, :c:func:`Py_DECREF` and +:c:func:`Py_XDECREF` are now implemented as function calls on all limited C +API versions. Previously, they were only implemented as function calls on +the limited C API version 3.12 or newer. Patch by Victor Stinner: diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2023-01-02-14-54-12.gh-issue-100687.yenIag.rst b/Misc/NEWS.d/next/Core_and_Builtins/2023-01-02-14-54-12.gh-issue-100687.yenIag.rst new file mode 100644 index 00000000000000..f4f63584b1659e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2023-01-02-14-54-12.gh-issue-100687.yenIag.rst @@ -0,0 +1,2 @@ +Reduce the memory used by the result of adding two large :class:`int` +values. Such additions are also slightly faster for most sizes. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst deleted file mode 100644 index 420912ace188fa..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-03-07-22-38-07.gh-issue-116477.AqwRAV.rst +++ /dev/null @@ -1 +0,0 @@ -Speed up the :class:`range` object constructor in the case of a single argument. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-27-22-23-57.gh-issue-125873.BG97KU.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-27-22-23-57.gh-issue-125873.BG97KU.rst deleted file mode 100644 index fc4818781ee132..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-27-22-23-57.gh-issue-125873.BG97KU.rst +++ /dev/null @@ -1 +0,0 @@ -Improved error message when an environment variable is misconfigured. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-19-16-10-26.gh-issue-137966.nEgFAt.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-19-16-10-26.gh-issue-137966.nEgFAt.rst deleted file mode 100644 index 6a2ce5e3318fdb..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-19-16-10-26.gh-issue-137966.nEgFAt.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add support for additional special names to the built-in :func:`help` function. -Patch by Adam Hartz. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-14-11-20-52.gh-issue-145019.ls2EXo.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-14-11-20-52.gh-issue-145019.ls2EXo.rst deleted file mode 100644 index c249c07b212f77..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-14-11-20-52.gh-issue-145019.ls2EXo.rst +++ /dev/null @@ -1,2 +0,0 @@ -Improve :exc:`SyntaxError` when :keyword:`match` alternative patterns bind -different names. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-12-13-48-26.gh-issue-69605.T9HWOL.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-12-13-48-26.gh-issue-69605.T9HWOL.rst deleted file mode 100644 index 5011debb72e07a..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-12-13-48-26.gh-issue-69605.T9HWOL.rst +++ /dev/null @@ -1,2 +0,0 @@ -Insert ``import`` after ``from x.y.z `` in the :term:`REPL` -auto-completion. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-21-55-45.gh-issue-69605.NwoP6l.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-21-55-45.gh-issue-69605.NwoP6l.rst deleted file mode 100644 index 77491c6e5f6207..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-21-55-45.gh-issue-69605.NwoP6l.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :term:`REPL` completions inserted incorrectly when there was spaces in -import statements. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-21-14-21-06.gh-issue-150075.dLI21Z.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-21-14-21-06.gh-issue-150075.dLI21Z.rst deleted file mode 100644 index 25213e3283f169..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-21-14-21-06.gh-issue-150075.dLI21Z.rst +++ /dev/null @@ -1 +0,0 @@ -tar.addfile() now sets member offsets in TarInfo objects diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-07-22-03-20.gh-issue-151060.tdexEE.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-07-22-03-20.gh-issue-151060.tdexEE.rst deleted file mode 100644 index 1efde8a536c093..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-07-22-03-20.gh-issue-151060.tdexEE.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash or memory corruption in :func:`sum` when summing a -:class:`float` subclass instance into a complex running total. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-18-00-00.gh-issue-151818.LsApDf.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-18-00-00.gh-issue-151818.LsApDf.rst deleted file mode 100644 index f1e56c81f2ff02..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-18-00-00.gh-issue-151818.LsApDf.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a double-free crash in ``list.append()`` (the specialized -``CALL_LIST_APPEND`` instruction) when growing the list fails with -:exc:`MemoryError`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-00-02-29.gh-issue-149816.JKN-5B.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-00-02-29.gh-issue-149816.JKN-5B.rst deleted file mode 100644 index 255507548703b1..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-00-02-29.gh-issue-149816.JKN-5B.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a race in free-threaded builds where storing an instance attribute could -use a stale borrowed reference to an instance ``__dict__`` after another thread -replaced it. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-11-34-50.gh-issue-152254.yK6TGp.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-11-34-50.gh-issue-152254.yK6TGp.rst new file mode 100644 index 00000000000000..206b88a2affb2b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-11-34-50.gh-issue-152254.yK6TGp.rst @@ -0,0 +1,2 @@ +Fix a crash during the clean up of managed dict objects under a memory +pressure in :c:func:`PyObject_ClearManagedDict`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-29-23-01-07.gh-issue-152631.LKgCM_.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-29-23-01-07.gh-issue-152631.LKgCM_.rst deleted file mode 100644 index 51208108f10647..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-29-23-01-07.gh-issue-152631.LKgCM_.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :func:`ast.parse` ability to parse lazy imports with *feature_version* -older than 3.15, now it properly raises :exc:`SyntaxError`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-04-18-00-00.gh-issue-153014.gCdBg1.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-04-18-00-00.gh-issue-153014.gCdBg1.rst deleted file mode 100644 index dec0bfc572118d..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-04-18-00-00.gh-issue-153014.gCdBg1.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a data race on the garbage collector debug flag in free-threading builds: -:func:`gc.set_debug` and :func:`gc.get_debug` now access it atomically. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-11-15-00-13.gh-issue-153568.atomfp.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-11-15-00-13.gh-issue-153568.atomfp.rst deleted file mode 100644 index 32ee3f72d6ab05..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-11-15-00-13.gh-issue-153568.atomfp.rst +++ /dev/null @@ -1,2 +0,0 @@ -Speed up parsing of simple expressions by recognizing single-token atoms -without descending the full precedence rule chain. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-22-56-27.gh-issue-152125.sEPvJf.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-22-56-27.gh-issue-152125.sEPvJf.rst new file mode 100644 index 00000000000000..ae66f69f206217 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-22-56-27.gh-issue-152125.sEPvJf.rst @@ -0,0 +1,3 @@ +Fix a crash in the :term:`free-threaded build` where :c:func:`PyList_New` could leave +a newly allocated list's item buffer pointer uninitialized when the backing +array allocation failed under low memory, leading to an invalid free. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-21-10-42-15.gh-issue-154318.Kq3Wm7.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-21-10-42-15.gh-issue-154318.Kq3Wm7.rst deleted file mode 100644 index b530d8cf06357b..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-21-10-42-15.gh-issue-154318.Kq3Wm7.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash when hashing a deeply nested :class:`tuple` or :class:`frozendict`. -A :exc:`RecursionError` is now raised instead of overflowing the C stack. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-05-01-10-20.gh-issue-155181.jj8jhk.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-05-01-10-20.gh-issue-155181.jj8jhk.rst new file mode 100644 index 00000000000000..1acf8fda95e465 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-05-01-10-20.gh-issue-155181.jj8jhk.rst @@ -0,0 +1,3 @@ +:class:`frozenset` objects created from a set literal or a set comprehension +are now untracked by the garbage collector when none of their elements can +be tracked. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-05-19-10-00.gh-issue-155247.extKey.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-05-19-10-00.gh-issue-155247.extKey.rst new file mode 100644 index 00000000000000..1e08323008d5b7 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-05-19-10-00.gh-issue-155247.extKey.rst @@ -0,0 +1,4 @@ +Fix importing an extension module whose path contains characters unencodable +in UTF-8, e.g. undecodable bytes of a file name. Previously it failed with +:exc:`UnicodeEncodeError`, which made it impossible to build or run Python in +a directory with such name. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-21-14-37-50.gh-issue-127716.KrPth8.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-21-14-37-50.gh-issue-127716.KrPth8.rst deleted file mode 100644 index ca2e9bf7a892f2..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-21-14-37-50.gh-issue-127716.KrPth8.rst +++ /dev/null @@ -1,10 +0,0 @@ -On the free-threaded build, :class:`memoryview` lifetime and bookkeeping -operations are now thread safe: -an operation that reads the buffer holds the managed buffer for its duration, -so a concurrent :meth:`~memoryview.release` can no longer free the buffer under -it; slicing a memoryview from several threads no longer loses buffer -registrations and raises ``ValueError: operation forbidden on released -memoryview object``; and :func:`hash` of a memoryview is no longer a data race. -As a result :meth:`~memoryview.release` may now return while an operation -started by another thread is still running, see -:ref:`thread-safety-memoryview`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-23-16-31-00.gh-issue-151126.phAWIG.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-23-16-31-00.gh-issue-151126.phAWIG.rst new file mode 100644 index 00000000000000..b1459de1c1596d --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-23-16-31-00.gh-issue-151126.phAWIG.rst @@ -0,0 +1 @@ +Fix a crash when allocation failure occurs during :ref:`interp-object-sharing`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-00-00-00.gh-issue-156399.aB3xYz.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-00-00-00.gh-issue-156399.aB3xYz.rst deleted file mode 100644 index 7680a16bfe6fd7..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-00-00-00.gh-issue-156399.aB3xYz.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix a regression where attribute access on a :class:`types.ModuleType` -subclass could bypass a data descriptor defined on the subclass. The -``LOAD_ATTR_MODULE`` specialization guarded on ``tp_getattro``, which -subclasses inherit, and then read the module dictionary directly. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-17-26-21.gh-issue-106045.wzTH7f.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-17-26-21.gh-issue-106045.wzTH7f.rst deleted file mode 100644 index a092b72d2ecdaa..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-27-17-26-21.gh-issue-106045.wzTH7f.rst +++ /dev/null @@ -1 +0,0 @@ -Fix standard library discovery in virtual environments created from a Python executable symlink located outside its base installation. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-28-10-15-00.gh-issue-144446.pQ7wXn.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-28-10-15-00.gh-issue-144446.pQ7wXn.rst new file mode 100644 index 00000000000000..2718a4cc7487f0 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-28-10-15-00.gh-issue-144446.pQ7wXn.rst @@ -0,0 +1,2 @@ +Fix thread safety of :attr:`frame.f_locals` and :c:func:`PyFrame_GetVar` on +frames executing on another thread in the free-threaded build. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-30-16-30-00.gh-issue-135385.Wn7Rd4.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-30-16-30-00.gh-issue-135385.Wn7Rd4.rst deleted file mode 100644 index 43b72921fe9e45..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-30-16-30-00.gh-issue-135385.Wn7Rd4.rst +++ /dev/null @@ -1,3 +0,0 @@ -Reduce the memory consumption of instances of classes which define -``__slots__`` and also have a ``__dict__``. Space for the attributes stored -in slots is no longer reserved in the inline values of every instance. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-07-09-52-21.gh-issue-156443.tT40sm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-07-09-52-21.gh-issue-156443.tT40sm.rst deleted file mode 100644 index 54c345aad025ce..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-07-09-52-21.gh-issue-156443.tT40sm.rst +++ /dev/null @@ -1 +0,0 @@ -Speed up :func:`float` conversion and true division of large :class:`int` values. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-08-17-40-00.gh-issue-157194.17Ffhk.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-08-17-40-00.gh-issue-157194.17Ffhk.rst deleted file mode 100644 index c9817f876cd79f..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-08-17-40-00.gh-issue-157194.17Ffhk.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix a data race in the free-threaded build, reported by ThreadSanitizer, -between the specializer reading an instruction opcode and another thread -writing it. Only reachable when thread-local bytecode is disabled with -``-X tlbc=0``, which makes threads share one copy of the bytecode. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-13-10-00-00.gh-issue-157361.aiterpy.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-13-10-00-00.gh-issue-157361.aiterpy.rst deleted file mode 100644 index b0a3c584da911f..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-13-10-00-00.gh-issue-157361.aiterpy.rst +++ /dev/null @@ -1,5 +0,0 @@ -Implement :func:`aiter` in Python instead of C, in the frozen ``_pybuiltins`` -module. The async iterator returned by ``aiter(callable, ...)`` now has a -plain coroutine ``__anext__()``, so introspection tools such as -:func:`asyncio.print_call_graph` can see through it into the awaited -callable result. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-21-12-44-38.gh-issue-132414.uwxmd5.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-21-12-44-38.gh-issue-132414.uwxmd5.rst deleted file mode 100644 index 5f795e987650af..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-21-12-44-38.gh-issue-132414.uwxmd5.rst +++ /dev/null @@ -1,4 +0,0 @@ -Quote the invalid option in the error message for an unknown command-line -option: ``Invalid option '-z'`` instead of ``Unknown option: -z``. A stray -space, as passed by a shebang line like ``#!/usr/bin/python3 -q -i``, is now -reported as ``Invalid option '- '``. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-25-11-05-00.gh-issue-157746.q7Lk3d.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-25-11-05-00.gh-issue-157746.q7Lk3d.rst deleted file mode 100644 index eb9e95defa3d6c..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-25-11-05-00.gh-issue-157746.q7Lk3d.rst +++ /dev/null @@ -1,3 +0,0 @@ -Reduce the overhead of :c:func:`PyErr_CheckSignals` when no signal is -pending: the cheap flag checks are now done before the main-thread check and -the remote debugger configuration lookup. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-28-10-12-25.gh-issue-158304.3pdcnJ.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-28-10-12-25.gh-issue-158304.3pdcnJ.rst deleted file mode 100644 index 394367d0dba8eb..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-28-10-12-25.gh-issue-158304.3pdcnJ.rst +++ /dev/null @@ -1,4 +0,0 @@ -Speed up iteration over asynchronous generators: values yielded to -``async for`` no longer create and discard a :exc:`StopIteration` exception -at every nesting level. The ``async_generators`` pyperformance benchmark is -about 30% faster. Patch by Shamil Abdulaev. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-29-14-35-00.gh-issue-158121.Kj4nQ8.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-09-29-14-35-00.gh-issue-158121.Kj4nQ8.rst deleted file mode 100644 index 3256ce99a84952..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-09-29-14-35-00.gh-issue-158121.Kj4nQ8.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix missing :mod:`sys.monitoring` ``INSTRUCTION`` events when a ``LINE`` -callback returns :data:`sys.monitoring.DISABLE`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-03-13-00-00.gh-issue-158197.Qk7sVd.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-03-13-00-00.gh-issue-158197.Qk7sVd.rst deleted file mode 100644 index 10fdabcb1f2fd8..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-03-13-00-00.gh-issue-158197.Qk7sVd.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix crashes and debug-build assertion failures in the :term:`free-threaded -build` when iterating over a split dictionary while another thread deletes -entries or clears it. Publish insertion-order updates atomically so readers -cannot observe an increased size before the corresponding order entry is -initialized. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-04-20-04-55.gh-issue-158816.9ulhWb.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-04-20-04-55.gh-issue-158816.9ulhWb.rst deleted file mode 100644 index fd9997afb3e1d3..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-04-20-04-55.gh-issue-158816.9ulhWb.rst +++ /dev/null @@ -1,3 +0,0 @@ -Speed up deleting items from split dictionaries (for example the -materialized ``__dict__`` of an instance with inline values) by skipping a -redundant hash table probe. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-05-13-19-49.gh-issue-87613.JLTGBy.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-05-13-19-49.gh-issue-87613.JLTGBy.rst deleted file mode 100644 index 830e8ef1dc23b2..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-05-13-19-49.gh-issue-87613.JLTGBy.rst +++ /dev/null @@ -1,2 +0,0 @@ -Speed up :class:`bytes` and :class:`bytearray` construction by adding -:ref:`vectorcall` support. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-07-00-00-00.gh-issue-158973.warnings-fork.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-07-00-00-00.gh-issue-158973.warnings-fork.rst deleted file mode 100644 index 9a98cf6e033384..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-07-00-00-00.gh-issue-158973.warnings-fork.rst +++ /dev/null @@ -1,3 +0,0 @@ -Reset the warnings lock in the child after :func:`os.fork`, before clearing -other threads' states, to avoid deadlocks when their finalizers issue warnings. -Preserve the lock's recursion depth if the forking thread owns it. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-08-19-03-14.gh-issue-159037.nbQ5wk.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-08-19-03-14.gh-issue-159037.nbQ5wk.rst new file mode 100644 index 00000000000000..a27cc2782d0b7c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-08-19-03-14.gh-issue-159037.nbQ5wk.rst @@ -0,0 +1 @@ +Improve performance of :meth:`str.join`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-16-04-40.gh-issue-159041.PUP8bU.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-16-04-40.gh-issue-159041.PUP8bU.rst deleted file mode 100644 index 4dd3338e41a4df..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-16-04-40.gh-issue-159041.PUP8bU.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a use-after-free in :mod:`asyncio` task bookkeeping when a finalizer -creates a task during thread shutdown. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-18-27-12.gh-issue-159054.YUCTvG.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-18-27-12.gh-issue-159054.YUCTvG.rst new file mode 100644 index 00000000000000..eaa01aa05435a7 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-09-18-27-12.gh-issue-159054.YUCTvG.rst @@ -0,0 +1,2 @@ +Fixed a crash when an allocation failure occurred while compiling code +containing cold blocks. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-10-20-21-13.gh-issue-159111.XVf2k-.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-10-20-21-13.gh-issue-159111.XVf2k-.rst deleted file mode 100644 index 3433eaa6ea65b9..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-10-20-21-13.gh-issue-159111.XVf2k-.rst +++ /dev/null @@ -1 +0,0 @@ -Update mimalloc version from 2.1.2 to 2.5.2. Patch by Donghee Na. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-11-06-36-25.gh-issue-159188.Fq7wLm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-11-06-36-25.gh-issue-159188.Fq7wLm.rst deleted file mode 100644 index 2637fb8e5326e3..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-11-06-36-25.gh-issue-159188.Fq7wLm.rst +++ /dev/null @@ -1,5 +0,0 @@ -In the :term:`free-threaded build`, :func:`compile` no longer immortalizes -the constants of the code objects when it is given an AST object, as was -already the case for a source string. The output of :mod:`marshal` for -such code no longer depends on the code objects which were created before -in the process. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-11-59-37.gh-issue-68241.jVpLIJ.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-11-59-37.gh-issue-68241.jVpLIJ.rst deleted file mode 100644 index 3dd3e44d721078..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-11-59-37.gh-issue-68241.jVpLIJ.rst +++ /dev/null @@ -1 +0,0 @@ -Add EXIT_FAILURE and EXIT_SUCCESS in the sys module. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-16-00-34.gh-issue-159244.ThiobE.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-16-00-34.gh-issue-159244.ThiobE.rst deleted file mode 100644 index 954cccf38c4367..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-16-00-34.gh-issue-159244.ThiobE.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash in :class:`dict` and :meth:`dict.update` in the -:term:`free-threaded build` when another thread concurrently mutates a list -used as a key/value pair. Patch by Christian Aurich Zanettini Martins. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-20-00-00.gh-issue-159136.fnAttr.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-20-00-00.gh-issue-159136.fnAttr.rst deleted file mode 100644 index 21fe270bbc1474..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-12-20-00-00.gh-issue-159136.fnAttr.rst +++ /dev/null @@ -1,8 +0,0 @@ -Setting ``__name__``, ``__qualname__``, ``__doc__`` or ``__module__`` on a -function no longer stops all threads in the free-threaded build. The setters -now use the per-object lock and defer freeing the old value with QSBR, the -same approach used for generator ``__name__``. This fixes a large slowdown -in multi-threaded code using :func:`functools.wraps` since 3.14.7. Readers -of these attributes now use atomic loads, and argument error reporting reads -``__qualname__`` after keyword comparisons, which may execute Python code and -replace it. Previously this could use a freed object. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-04-03-48.gh-issue-159288.vhqBQG.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-04-03-48.gh-issue-159288.vhqBQG.rst deleted file mode 100644 index 2d969e8cbac8c4..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-04-03-48.gh-issue-159288.vhqBQG.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash when walking a very long chain of lazy import placeholders, as -created by an aliased dotted import such as ``lazy import a.b.c as d``. -:exc:`RecursionError` is now raised instead. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-06-23-56.gh-issue-159294.9LTsNA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-06-23-56.gh-issue-159294.9LTsNA.rst deleted file mode 100644 index 9504f1f753cbec..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-06-23-56.gh-issue-159294.9LTsNA.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash in a debug build when creating an :exc:`ExceptionGroup` or -:exc:`BaseExceptionGroup` fails with :exc:`MemoryError`. Patch by Christian -Aurich Zanettini Martins. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-16-38-00.gh-issue-159320._Yhg1s.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-16-38-00.gh-issue-159320._Yhg1s.rst deleted file mode 100644 index 25e8745ccc02bb..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-16-38-00.gh-issue-159320._Yhg1s.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add dynamic detection of stack sizes on Android, allowing safer deep -recursion. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-07-36-44.gh-issue-159399.cjuE0N.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-07-36-44.gh-issue-159399.cjuE0N.rst deleted file mode 100644 index f7020fee0cbc86..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-07-36-44.gh-issue-159399.cjuE0N.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash in :func:`codecs.encode` and :func:`codecs.decode` in the -:term:`free-threaded build` when another thread concurrently unregisters a -codec search function. Patch by Christian Aurich Zanettini Martins. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-11-40-06.gh-issue-159430.OWpyPm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-11-40-06.gh-issue-159430.OWpyPm.rst deleted file mode 100644 index fb4a6d6da4568a..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-11-40-06.gh-issue-159430.OWpyPm.rst +++ /dev/null @@ -1,2 +0,0 @@ -Report the correct exception if looking up :meth:`~object.__bytes__` raises -an exception while performing %-formatting of :class:`bytes`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-18-45-35.gh-issue-159431.mVTMsA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-18-45-35.gh-issue-159431.mVTMsA.rst deleted file mode 100644 index 7ed642e4c70791..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-18-45-35.gh-issue-159431.mVTMsA.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix an assertion failure, or an immortal object becoming mortal, on the -:term:`free threaded ` build when an object passed to the -internal delayed decref mechanism is immortal or becomes immortal before the -delayed decref is processed by the garbage collector. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-19-16-53.gh-issue-159434.T4AsrQ.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-19-16-53.gh-issue-159434.T4AsrQ.rst deleted file mode 100644 index f7d7fbffa3371e..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-19-16-53.gh-issue-159434.T4AsrQ.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix reading a function's :attr:`~function.__annotations__` using borrowed -references to :attr:`~function.__annotate__` and the internal annotations -tuple while running code that can replace them. If ``__annotations__`` is -set while ``__annotate__`` runs, the newly set value is now kept. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-23-05-00.gh-issue-132042.k7Qs2w.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-23-05-00.gh-issue-132042.k7Qs2w.rst deleted file mode 100644 index 887df4fca00a78..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-14-23-05-00.gh-issue-132042.k7Qs2w.rst +++ /dev/null @@ -1,3 +0,0 @@ -Speed up class creation by resolving the special method slots of a new class -with one pass over each class dict along the MRO instead of one lookup per -slot name and per class. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-10-15-27.gh-issue-159449.xsdzFf.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-10-15-27.gh-issue-159449.xsdzFf.rst deleted file mode 100644 index f592e76fdbff70..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-10-15-27.gh-issue-159449.xsdzFf.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a free-threaded asyncio race leading to segfaults. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-10-00.gh-issue-87613.Wr4fvc.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-10-00.gh-issue-87613.Wr4fvc.rst deleted file mode 100644 index 8a57b1443365e7..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-10-00.gh-issue-87613.Wr4fvc.rst +++ /dev/null @@ -1 +0,0 @@ -Speed up creating :class:`weakref.ref` objects by adding a vectorcall implementation of the constructor generated by Argument Clinic. Patch by Pieter Eendebak. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-36-09.gh-issue-130279.N9N-sw.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-36-09.gh-issue-130279.N9N-sw.rst deleted file mode 100644 index 996eb7020f5344..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-36-09.gh-issue-130279.N9N-sw.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix bug where compiler did not set the right exception handler for some jump -instructions. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-40-00.gh-issue-87613.Sl1ceV.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-40-00.gh-issue-87613.Sl1ceV.rst deleted file mode 100644 index bd96edc0b9f177..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-15-12-40-00.gh-issue-87613.Sl1ceV.rst +++ /dev/null @@ -1 +0,0 @@ -Speed up calling :class:`slice` by adding a vectorcall implementation of the constructor generated by Argument Clinic. Patch by Pieter Eendebak. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-16-18-25-48.gh-issue-159564.WMRRFA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-16-18-25-48.gh-issue-159564.WMRRFA.rst deleted file mode 100644 index 8e59f52fa4090e..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-16-18-25-48.gh-issue-159564.WMRRFA.rst +++ /dev/null @@ -1,3 +0,0 @@ -Improve the performance and thread scaling of dereferencing weak references -in the :term:`free-threaded build`. Dereferencing a weak reference no longer -takes a lock. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-17-02-47-26.gh-issue-159603.OFagJz.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-17-02-47-26.gh-issue-159603.OFagJz.rst deleted file mode 100644 index bd46b85509dbc2..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-17-02-47-26.gh-issue-159603.OFagJz.rst +++ /dev/null @@ -1,2 +0,0 @@ -On FreeBSD, call ``pthread_attr_get_np()`` to get the stack size (and guard -size) of the current thread. Patch by Victor Stinner. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-18-00-30-00.gh-issue-159652.Qk3vLm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-18-00-30-00.gh-issue-159652.Qk3vLm.rst deleted file mode 100644 index 2b213c983b8718..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-18-00-30-00.gh-issue-159652.Qk3vLm.rst +++ /dev/null @@ -1,6 +0,0 @@ -Fix a hang when an import fails with :exc:`RecursionError` because the C -stack is nearly exhausted: releasing the import locks could itself fail and -leave the global import lock or a module lock held forever, so that later -imports in other threads blocked indefinitely. The import system now raises -:exc:`RecursionError` before taking an import lock when there is not enough -stack left to release it. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-16-38-39.gh-issue-159813.wHfSOc.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-16-38-39.gh-issue-159813.wHfSOc.rst deleted file mode 100644 index 7d5f0c8f2f5354..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-16-38-39.gh-issue-159813.wHfSOc.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add a fast path for :const:`None` in :c:func:`PyUnicodeWriter_WriteRepr`. -Patch by Donghee Na. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.09590b.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.09590b.rst deleted file mode 100644 index 3231be417aa80e..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.09590b.rst +++ /dev/null @@ -1 +0,0 @@ -Avoid a crash when a module filename cannot be encoded as UTF-8. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.173d49.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.173d49.rst deleted file mode 100644 index a552d06ff7bb89..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.173d49.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid a crash if formatting an integer with the ``c`` presentation type runs -out of memory. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.1fc3af.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.1fc3af.rst deleted file mode 100644 index a02631f2da2f9b..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.1fc3af.rst +++ /dev/null @@ -1,2 +0,0 @@ -Preserve the original :exc:`SyntaxError` if allocating optional error metadata -fails. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.a8775b.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.a8775b.rst deleted file mode 100644 index 73c2e9bd5d36b9..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.a8775b.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid a crash when an interpreter configuration string cannot be encoded as -UTF-8. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.c25c27.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.c25c27.rst deleted file mode 100644 index b48d1e76954193..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.c25c27.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix memory leaks during interpreter startup when a path configuration file is -empty or too large, or when an audit hook rejects a command. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst deleted file mode 100644 index 0342bdef3e8699..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash in :func:`warnings.warn` when ``skip_file_prefixes`` is an empty tuple subclass. -Propagate allocation errors when creating the fallback filename. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.55fd08.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.55fd08.rst deleted file mode 100644 index e574bab861b740..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.55fd08.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a reference leak in :func:`gc.get_objects` and :func:`gc.get_referrers` -when an internal allocation fails in the free-threaded build. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.78e530.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.78e530.rst deleted file mode 100644 index 999efdac87f974..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.78e530.rst +++ /dev/null @@ -1,2 +0,0 @@ -Raise :exc:`OverflowError` when the absolute value of a dynamic printf-style -formatting width is too large. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst deleted file mode 100644 index 64d84ae260bb3b..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.b62fc9.rst +++ /dev/null @@ -1,2 +0,0 @@ -Propagate errors during generic class construction and avoid a crash after -failed generic alias substitution. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.fc0ed2.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.fc0ed2.rst deleted file mode 100644 index 6a549a6ded5e6a..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.fc0ed2.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid a crash if allocation fails while creating a -:class:`string.templatelib.Template` iterator. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-21-30-00.gh-issue-159860.qsbr128.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-21-30-00.gh-issue-159860.qsbr128.rst deleted file mode 100644 index fc0d6fca4f7996..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-21-30-00.gh-issue-159860.qsbr128.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix false sharing between the per-thread QSBR states in the -:term:`free-threaded build` on AArch64 CPUs with 128-byte cache lines, such -as Apple Silicon. CPU-bound threads that share no objects now scale like -processes there. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.848b18.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.848b18.rst deleted file mode 100644 index 07f842471b4d0f..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.848b18.rst +++ /dev/null @@ -1,2 +0,0 @@ -Allow the garbage collector to break reference cycles involving property -accessors or names. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.a3cc5d.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.a3cc5d.rst deleted file mode 100644 index 69c53e84e216b3..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.a3cc5d.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a crash when constructing an error for invalid generic alias arguments. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.bf569b.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.bf569b.rst deleted file mode 100644 index 797d2de2240ef3..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.bf569b.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a reference leak in binary, ternary, and comparison operations on weak -reference proxies when another operand is a dead proxy. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.d76e6d.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.d76e6d.rst deleted file mode 100644 index a027883426be19..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.d76e6d.rst +++ /dev/null @@ -1,2 +0,0 @@ -Prevent free-threaded builds from interning and immortalizing complex -subclasses in code object constants. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.da0f04.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.da0f04.rst deleted file mode 100644 index 386eb5535b7025..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.da0f04.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix reference leaks when free-threaded garbage collection aborts early because -of an allocation error. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-10-00-00.gh-issue-158195.UnpinTL.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-10-00-00.gh-issue-158195.UnpinTL.rst deleted file mode 100644 index f189c7c563ca58..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-10-00-00.gh-issue-158195.UnpinTL.rst +++ /dev/null @@ -1,7 +0,0 @@ -Fix a deadlock in the free-threaded build between a thread that stops the -world while running Python code under the type lock (for example a custom -metaclass ``mro()`` or the ``__eq__`` of a :class:`str` subclass used as a -class ``__dict__`` key) and a thread that assigns to a special method or to -``__bases__`` of a class. The type lock is no longer held while waiting -for a stop-the-world; the prepared slot, MRO and bases updates are -validated with the world stopped and recomputed if they went stale. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-22-00-00.gh-issue-159963.pLkHld.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-22-00-00.gh-issue-159963.pLkHld.rst deleted file mode 100644 index 5c3bd8f63f1d4f..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-22-00-00.gh-issue-159963.pLkHld.rst +++ /dev/null @@ -1,6 +0,0 @@ -Fix a regression in Python 3.15.0 where threads importing submodules of -packages for the first time could raise ``_DeadlockError`` or a spurious -"partially initialized module" :exc:`ImportError`, or block one another, with -no circular import involved. :func:`importlib.import_module` now returns the -partially initialized module in a cross-thread circular import, as the -:keyword:`import` statement does, instead of raising ``_DeadlockError``. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-14-00-00.gh-issue-159493.sfLf1b.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-14-00-00.gh-issue-159493.sfLf1b.rst deleted file mode 100644 index 35289d995b3d35..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-14-00-00.gh-issue-159493.sfLf1b.rst +++ /dev/null @@ -1,3 +0,0 @@ -Superinstructions are now formed after the compiler decides which -:opcode:`LOAD_FAST` instructions can borrow their reference. -This fixes reference count contention slowdown on the free-threaded build for single-line loops and comprehensions. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-18-00-00.gh-issue-160368.sem-clockwait.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-18-00-00.gh-issue-160368.sem-clockwait.rst deleted file mode 100644 index 11d7467b38413a..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-18-00-00.gh-issue-160368.sem-clockwait.rst +++ /dev/null @@ -1,3 +0,0 @@ -On Linux, allow timed semaphore waits to use the monotonic clock when -``sem_clockwait`` is available at runtime, even if Python was built with -older libc headers. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-19-55-00.gh-issue-159739.codc01.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-19-55-00.gh-issue-159739.codc01.rst deleted file mode 100644 index 985d4264befae2..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-19-55-00.gh-issue-159739.codc01.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash in free-threaded builds when :func:`codecs.unregister` is -called concurrently. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-19-55-00.gh-issue-159739.tali01.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-19-55-00.gh-issue-159739.tali01.rst deleted file mode 100644 index a4f672fed87cfe..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-19-55-00.gh-issue-159739.tali01.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a reference leak when the value of a type alias is evaluated -recursively or concurrently. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Aw61LR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Aw61LR.rst deleted file mode 100644 index 5de64814b1ca55..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Aw61LR.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a race in ``list.__sizeof__()`` when a list is concurrently resized on a free-threaded build. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.CbTE4N.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.CbTE4N.rst deleted file mode 100644 index 20072791808dd1..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.CbTE4N.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a possible crash when assigning metadata to :class:`classmethod` or :class:`staticmethod` objects. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Zw43uX.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Zw43uX.rst deleted file mode 100644 index 628a146b059a43..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-21-26-10.gh-issue-159739.Zw43uX.rst +++ /dev/null @@ -1 +0,0 @@ -Raise :exc:`MemoryError` instead of returning an incorrect result if :func:`round` runs out of memory while rounding a float. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-23-18-38.gh-issue-160132.Nj4rQa.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-23-18-38.gh-issue-160132.Nj4rQa.rst deleted file mode 100644 index 156f387f7a6998..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-21-23-18-38.gh-issue-160132.Nj4rQa.rst +++ /dev/null @@ -1,3 +0,0 @@ -Reduce memory usage during compilation by reordering the fields of the -internal basic block structure to reduce padding. -Patch by Shamil Abdulaev. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-00-30-02.gh-issue-160179.dQ2e7m.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-00-30-02.gh-issue-160179.dQ2e7m.rst deleted file mode 100644 index 5377e59a60ea48..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-00-30-02.gh-issue-160179.dQ2e7m.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :meth:`!bytearray.__init__` leaving uninitialized memory in the object -when it fails on a :class:`list` or :class:`tuple` containing an integer not -in ``range(256)``. The object is now empty after the failed call. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-00-30-21.gh-issue-160162.lShf32.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-00-30-21.gh-issue-160162.lShf32.rst deleted file mode 100644 index 5aa2021bddd44e..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-00-30-21.gh-issue-160162.lShf32.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash on 32-bit platforms when left shifting an :class:`int` by a -count close to ``sys.maxsize * sys.int_info.bits_per_digit``. -:exc:`OverflowError` is now raised, as for other too large counts. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-12-30-11.gh-issue-106045.fkMW34.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-12-30-11.gh-issue-106045.fkMW34.rst deleted file mode 100644 index 98abf1debe08d6..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-12-30-11.gh-issue-106045.fkMW34.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix :mod:`venv` path discovery when the base executable resolves to a -location outside the installation pointed to by the ``home`` key in -``pyvenv.cfg``. The standard library is now found from ``home``, and -extension modules from the resolved executable's installation, instead of -falling back to the compiled-in prefix. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-14-45-00.gh-issue-160245.mEmO24.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-14-45-00.gh-issue-160245.mEmO24.rst deleted file mode 100644 index f62d937f556caa..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-14-45-00.gh-issue-160245.mEmO24.rst +++ /dev/null @@ -1,2 +0,0 @@ -Reduce the size of parser memo entries on 64-bit platforms by reordering -fields to remove padding. Patch by Shamil Abdulaev. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-15-40-00.gh-issue-160303.q7Zb2K.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-15-40-00.gh-issue-160303.q7Zb2K.rst new file mode 100644 index 00000000000000..ba72afc400bd0a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-15-40-00.gh-issue-160303.q7Zb2K.rst @@ -0,0 +1,5 @@ +Fix :class:`bytearray` reading and writing memory outside of its buffer +after ``del b[:n]`` or ``b[:n] = shorter`` failed with :exc:`MemoryError`. +The bytes are now removed even if shrinking the memory block fails; +:exc:`MemoryError` is still raised. Also fix the missing null byte after the +data when shrinking fails for a slice which is not at the front. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-17-37-27.gh-issue-160268.gJ2iDx.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-17-37-27.gh-issue-160268.gJ2iDx.rst deleted file mode 100644 index 7533a25e0b57a5..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-17-37-27.gh-issue-160268.gJ2iDx.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a crash in :mod:`tokenize` when a mismatched closing bracket follows the opening brace of an f-string or t-string replacement field, as in ``f'{]a'``. It now raises the same "unmatched" error as the parser. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-20-18-44.gh-issue-160247.WC8hRU.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-20-18-44.gh-issue-160247.WC8hRU.rst new file mode 100644 index 00000000000000..97755e25aac7c2 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-22-20-18-44.gh-issue-160247.WC8hRU.rst @@ -0,0 +1,2 @@ +Reduce padding in the internal instruction sequence structure, saving 8 bytes +per instance on 64-bit platforms. Patch by Shamil Abdulaev. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-08-01-14.gh-issue-159580.lEl21_.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-08-01-14.gh-issue-159580.lEl21_.rst new file mode 100644 index 00000000000000..5acc2d3283d5ad --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-08-01-14.gh-issue-159580.lEl21_.rst @@ -0,0 +1 @@ +Improve performance of :func:`isinstance` when the check fails. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-20-15-24.gh-issue-160491.yIQ_Vf.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-20-15-24.gh-issue-160491.yIQ_Vf.rst deleted file mode 100644 index 2227224ba0fa2c..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-20-15-24.gh-issue-160491.yIQ_Vf.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix the location reported for invalid escape sequences in f-strings and -t-strings. The :exc:`SyntaxError` now points at the literal part that -contains the escape, instead of at the closing quote. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-24-19-07-34.gh-issue-153568.6sVdqs.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-24-19-07-34.gh-issue-153568.6sVdqs.rst deleted file mode 100644 index 98628f6a74208f..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-24-19-07-34.gh-issue-153568.6sVdqs.rst +++ /dev/null @@ -1,3 +0,0 @@ -Speed up :func:`ast.parse` by ~15.8% and reduce the memory used by ~51.6% by -storing node attributes as inline values instead of in a per-node -dictionary. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-24-19-40-00.gh-issue-160573.sT9kQe.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-24-19-40-00.gh-issue-160573.sT9kQe.rst new file mode 100644 index 00000000000000..7a69a476920485 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-24-19-40-00.gh-issue-160573.sT9kQe.rst @@ -0,0 +1,4 @@ +Fix a crash when a :class:`set` iterator that is shared between threads reaches +its end in a free-threaded build: every thread that walked off the end of the +table released the iterator's reference to the set, so the set was over-released. +The iterator now keeps its reference to the set until it is deallocated. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-25-12-30-00.gh-issue-112564.gcDbgW.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-25-12-30-00.gh-issue-112564.gcDbgW.rst new file mode 100644 index 00000000000000..5ff3cb147e71c0 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-25-12-30-00.gh-issue-112564.gcDbgW.rst @@ -0,0 +1,5 @@ +Fix a crash in the garbage collector when :data:`gc.DEBUG_COLLECTABLE` or +:data:`gc.DEBUG_UNCOLLECTABLE` is set and writing the debug output to +:data:`sys.stderr` frees one of the objects being reported. Object addresses +in this output now use the same ``0x``-prefixed lowercase format on all +platforms, including Windows. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-10-18-26.gh-issue-160697.qSbR1f.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-10-18-26.gh-issue-160697.qSbR1f.rst new file mode 100644 index 00000000000000..55649732e8f4b2 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-10-18-26.gh-issue-160697.qSbR1f.rst @@ -0,0 +1,4 @@ +Fix crashes and hangs in the :term:`free-threaded build` when a destructor +ran in the middle of a :class:`dict` resize, a :class:`list` resize or a type +cache update. The thread's queue of delayed frees is now processed only from +the eval breaker, instead of synchronously when the queue fills up. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-12-00-00.gh-issue-160682.Q7mK2x.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-12-00-00.gh-issue-160682.Q7mK2x.rst deleted file mode 100644 index 5c783665e78965..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-26-12-00-00.gh-issue-160682.Q7mK2x.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a bug in the :term:`free-threaded build` where garbage collection could -prematurely collect the function object of a running method. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-28-13-47-43.gh-issue-160773.7VFxkA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-28-13-47-43.gh-issue-160773.7VFxkA.rst deleted file mode 100644 index 09d282e1dbeee7..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-28-13-47-43.gh-issue-160773.7VFxkA.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash in :func:`str` of an :exc:`OSError` when formatting one of its -attributes modifies the others. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-01-16-02-42.gh-issue-161145.5vLdrm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-01-16-02-42.gh-issue-161145.5vLdrm.rst new file mode 100644 index 00000000000000..5d809624e25f42 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-01-16-02-42.gh-issue-161145.5vLdrm.rst @@ -0,0 +1,2 @@ +Speed up UTF-8 decoding of long ASCII runs, such as in :meth:`bytes.decode`, +by up to 1.5x. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst deleted file mode 100644 index 3a9eae1e4af91b..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-05-52-49.gh-issue-161229.8pOPOI.rst +++ /dev/null @@ -1,2 +0,0 @@ -:func:`!__lazy_import__` now treats ``None`` for *globals* and *locals* the -same as omitting them, matching its signature. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-13-44-26.gh-issue-159184.X_sOdY.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-13-44-26.gh-issue-159184.X_sOdY.rst deleted file mode 100644 index 651ac1d0b9bae9..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-13-44-26.gh-issue-159184.X_sOdY.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a leak of NaN constants in code objects created by :func:`marshal.loads` -on the free-threaded build. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-20-28-15.gh-issue-161288.LikC_U.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-20-28-15.gh-issue-161288.LikC_U.rst deleted file mode 100644 index 4cf8201d02ce7c..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-03-20-28-15.gh-issue-161288.LikC_U.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash in the perf trampoline (:option:`-X perf <-X>`) on builds with -clang's ``-fsanitize=function``, which ``-fsanitize=undefined`` enables -since clang 17. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-09-12-00-00.gh-issue-161693.Sp1tKy.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-09-12-00-00.gh-issue-161693.Sp1tKy.rst new file mode 100644 index 00000000000000..ed820f6d29efe9 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-09-12-00-00.gh-issue-161693.Sp1tKy.rst @@ -0,0 +1,3 @@ +Fix a crash in :meth:`dict.setdefault` when called on an instance +dictionary with a :class:`str` subclass key equal to an attribute name +that is not set on that instance. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-04-58-51.gh-issue-161792.42EcBI8x.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-04-58-51.gh-issue-161792.42EcBI8x.rst new file mode 100644 index 00000000000000..a4b685d96bc896 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-04-58-51.gh-issue-161792.42EcBI8x.rst @@ -0,0 +1,4 @@ +Fix crashes in the callable iterator returned by :func:`iter` with a +sentinel, when :meth:`!__setstate__` is called while the iterator is in use, +or when the iterator is shared between threads in the :term:`free-threaded +build`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-21-52-02.gh-issue-161827.3PD3RK.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-21-52-02.gh-issue-161827.3PD3RK.rst new file mode 100644 index 00000000000000..2ae6517db14f15 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-11-21-52-02.gh-issue-161827.3PD3RK.rst @@ -0,0 +1,4 @@ +Keep ``_PyEval_EvalFrameDefault`` as a separate frame in the C call stack +when Python is built with the tail-calling interpreter +(:option:`--with-tail-call-interp`), so that stack unwinders, profilers and +other tools that walk the stack can find the interpreter loop. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-37-21.gh-issue-161875.W-nGSw.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-37-21.gh-issue-161875.W-nGSw.rst new file mode 100644 index 00000000000000..693f39a2bef917 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-37-21.gh-issue-161875.W-nGSw.rst @@ -0,0 +1,3 @@ +The :opcode:`GET_AWAITABLE` instruction now also pushes the ``NULL`` and +``None`` that start the :opcode:`SEND` loop, so ``await`` no longer needs +separate :opcode:`PUSH_NULL` and ``LOAD_CONST None`` instructions. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-46-10.gh-issue-161875.oOjR9w.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-46-10.gh-issue-161875.oOjR9w.rst new file mode 100644 index 00000000000000..9afa682ea8b6ff --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-12-14-46-10.gh-issue-161875.oOjR9w.rst @@ -0,0 +1,4 @@ +The :opcode:`GET_ANEXT` instruction now also pushes the ``NULL`` and +``None`` that start the :opcode:`SEND` loop, so each :keyword:`async for` +iteration no longer needs separate :opcode:`PUSH_NULL` and ``LOAD_CONST None`` +instructions. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-00-00.gh-issue-161968.ytuf5z.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-00-00.gh-issue-161968.ytuf5z.rst new file mode 100644 index 00000000000000..12d61b2eeee74e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-00-00-00.gh-issue-161968.ytuf5z.rst @@ -0,0 +1,2 @@ +Fix ``x *= seq`` raising :exc:`TypeError` when ``x`` is an instance of an +:class:`int` subclass or of a class defining :meth:`~object.__index__`. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-08-43-30.gh-issue-161849.m5BmNR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-08-43-30.gh-issue-161849.m5BmNR.rst new file mode 100644 index 00000000000000..744a5abed698f4 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-13-08-43-30.gh-issue-161849.m5BmNR.rst @@ -0,0 +1,4 @@ +Fix a crash and a :exc:`SystemError` when compiling a class whose body binds +``__classdict__`` (for example ``__classdict__ = 1`` or ``global +__classdict__``) and also contains an annotated function, a generic class or +function, or a generic type alias. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-23-17-18.gh-issue-161751.-silGa.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-23-17-18.gh-issue-161751.-silGa.rst new file mode 100644 index 00000000000000..0c39e484386dc5 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-23-17-18.gh-issue-161751.-silGa.rst @@ -0,0 +1,2 @@ +Fix race in mimalloc abandoned segment visit in free-threaded GC. Patch by +Donghee Na. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-20-12-00-00.gh-issue-152276.bRcImm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-20-12-00-00.gh-issue-152276.bRcImm.rst new file mode 100644 index 00000000000000..9cc5d98dae0d0f --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-20-12-00-00.gh-issue-152276.bRcImm.rst @@ -0,0 +1,2 @@ +For the :term:`free-threaded build`, avoid an assertion failure when an object +becomes immortal while queued for biased reference counting (BRC) merging. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-21-12-00-00.gh-issue-162227.fMtScl.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-21-12-00-00.gh-issue-162227.fMtScl.rst new file mode 100644 index 00000000000000..06b6570b2cd26c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-21-12-00-00.gh-issue-162227.fMtScl.rst @@ -0,0 +1,4 @@ +Improve multi-threaded scaling of :func:`format`, f-strings and +:meth:`str.format` with a format specification on the :term:`free-threaded +build` by avoiding the creation of a temporary bound method and reference +count contention on the shared ``__format__`` method. diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-23-09-17-41.gh-issue-162477.TEcSpB.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-23-09-17-41.gh-issue-162477.TEcSpB.rst new file mode 100644 index 00000000000000..9bfe0a710fc669 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-23-09-17-41.gh-issue-162477.TEcSpB.rst @@ -0,0 +1,2 @@ +Speed up :c:func:`PyFloat_AsDouble` for exact :class:`int` arguments. Patch by +Maurycy Pawłowski-Wieroński. diff --git a/Misc/NEWS.d/next/Documentation/2023-09-04-16-01-20.gh-issue-107689.LFLFtG.rst b/Misc/NEWS.d/next/Documentation/2023-09-04-16-01-20.gh-issue-107689.LFLFtG.rst deleted file mode 100644 index 5c60e29bc0b44a..00000000000000 --- a/Misc/NEWS.d/next/Documentation/2023-09-04-16-01-20.gh-issue-107689.LFLFtG.rst +++ /dev/null @@ -1 +0,0 @@ -Added docstrings to abstract ctypes. diff --git a/Misc/NEWS.d/next/Documentation/2025-08-31-02-28-21.gh-issue-138277.kkORph.rst b/Misc/NEWS.d/next/Documentation/2025-08-31-02-28-21.gh-issue-138277.kkORph.rst deleted file mode 100644 index 198d1de3121c5b..00000000000000 --- a/Misc/NEWS.d/next/Documentation/2025-08-31-02-28-21.gh-issue-138277.kkORph.rst +++ /dev/null @@ -1,2 +0,0 @@ -Rearranged documentation search results to emphasize built-in types and -functions. Patch by Adam Hartz. diff --git a/Misc/NEWS.d/next/IDLE/2026-10-16-15-12-36.gh-issue-159517.uzRtAd.rst b/Misc/NEWS.d/next/IDLE/2026-10-16-15-12-36.gh-issue-159517.uzRtAd.rst deleted file mode 100644 index f5e506c3e1f96e..00000000000000 --- a/Misc/NEWS.d/next/IDLE/2026-10-16-15-12-36.gh-issue-159517.uzRtAd.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix the completion list and the call tip grabbing the focus on macOS with -Tcl/Tk 9.1. diff --git a/Misc/NEWS.d/next/Library/2021-05-24-14-41-29.bpo-44170._v28mv.rst b/Misc/NEWS.d/next/Library/2021-05-24-14-41-29.bpo-44170._v28mv.rst deleted file mode 100644 index db8366f6ef0f09..00000000000000 --- a/Misc/NEWS.d/next/Library/2021-05-24-14-41-29.bpo-44170._v28mv.rst +++ /dev/null @@ -1 +0,0 @@ -Fix UnicodeDecodeError with multibyte utf8 characters in ShareableList. diff --git a/Misc/NEWS.d/next/Library/2023-01-26-09-36-24.gh-issue-61448.SOAkvt.rst b/Misc/NEWS.d/next/Library/2023-01-26-09-36-24.gh-issue-61448.SOAkvt.rst deleted file mode 100644 index 6ea99ba3310b7c..00000000000000 --- a/Misc/NEWS.d/next/Library/2023-01-26-09-36-24.gh-issue-61448.SOAkvt.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :func:`inspect.formatargvalues` raising :exc:`KeyError` for deleted -arguments, including ``*args`` and ``**kwargs``, by displaying ````. -Patch by Ezio Melotti and Furkan Onder. diff --git a/Misc/NEWS.d/next/Library/2023-10-18-13-31-10.gh-issue-110941.UVrYGE.rst b/Misc/NEWS.d/next/Library/2023-10-18-13-31-10.gh-issue-110941.UVrYGE.rst new file mode 100644 index 00000000000000..5824b593460e9b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-18-13-31-10.gh-issue-110941.UVrYGE.rst @@ -0,0 +1,2 @@ +Fix ``json.dump`` and ``json.dumps`` encoding certain dict subclasses as +empty. diff --git a/Misc/NEWS.d/next/Library/2024-12-27-15-41-55.gh-issue-128289.-hCzEc.rst b/Misc/NEWS.d/next/Library/2024-12-27-15-41-55.gh-issue-128289.-hCzEc.rst deleted file mode 100644 index 015512afad6ed3..00000000000000 --- a/Misc/NEWS.d/next/Library/2024-12-27-15-41-55.gh-issue-128289.-hCzEc.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add *eager_tasks* parameter to :func:`asyncio.run` function and -:class:`asyncio.Runner` class constructor. diff --git a/Misc/NEWS.d/next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst b/Misc/NEWS.d/next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst deleted file mode 100644 index 9dcf05913870b9..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-02-04-09-01-06.gh-issue-129605.wKGNXQ.rst +++ /dev/null @@ -1 +0,0 @@ -Improve exception handling when computing suggestions in traceback.py. This fixes a bug that could cause PyREPL to exit unexpectedly. diff --git a/Misc/NEWS.d/next/Library/2025-03-13-20-21-00.gh-issue-115810.qwrb231.rst b/Misc/NEWS.d/next/Library/2025-03-13-20-21-00.gh-issue-115810.qwrb231.rst deleted file mode 100644 index 253f4a5de95738..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-03-13-20-21-00.gh-issue-115810.qwrb231.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add :attr:`datetime.timezone.name`, the *name* passed to the constructor, -or ``None`` if no name was passed. diff --git a/Misc/NEWS.d/next/Library/2025-06-12-15-39-48.gh-issue-89730.Lu35Fu.rst b/Misc/NEWS.d/next/Library/2025-06-12-15-39-48.gh-issue-89730.Lu35Fu.rst deleted file mode 100644 index bf4f8cfc476d2f..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-06-12-15-39-48.gh-issue-89730.Lu35Fu.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add support for Encrypted Client Hello (ECH) to the :mod:`ssl` module with -OpenSSL 4.0 or later. diff --git a/Misc/NEWS.d/next/Library/2025-09-18-14-13-00.gh-issue-139122.m3lp66.rst b/Misc/NEWS.d/next/Library/2025-09-18-14-13-00.gh-issue-139122.m3lp66.rst deleted file mode 100644 index cb656aaf793c63..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-09-18-14-13-00.gh-issue-139122.m3lp66.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`uuid`: Reimplement :class:`~uuid.UUID` objects, :func:`~uuid.uuid4`, -and :func:`~uuid.uuid7` in C. diff --git a/Misc/NEWS.d/next/Library/2025-09-18-18-03-32.gh-issue-139105.1QQ_W4.rst b/Misc/NEWS.d/next/Library/2025-09-18-18-03-32.gh-issue-139105.1QQ_W4.rst deleted file mode 100644 index 0494a66b094eee..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-09-18-18-03-32.gh-issue-139105.1QQ_W4.rst +++ /dev/null @@ -1 +0,0 @@ -Correct the EOF key shown in the :mod:`sqlite3` CLI ``.help`` text on Windows. diff --git a/Misc/NEWS.d/next/Library/2025-10-13-15-43-09.gh-issue-140036.b_59uN.rst b/Misc/NEWS.d/next/Library/2025-10-13-15-43-09.gh-issue-140036.b_59uN.rst deleted file mode 100644 index 4bd92b4d735225..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-10-13-15-43-09.gh-issue-140036.b_59uN.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid hanging in floor division of pure Python :class:`decimal.Decimal` -instances when the context precision is very large. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2025-10-20-13-08-04.gh-issue-84027.HjBO0B.rst b/Misc/NEWS.d/next/Library/2025-10-20-13-08-04.gh-issue-84027.HjBO0B.rst deleted file mode 100644 index 060eeabd903bbb..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-10-20-13-08-04.gh-issue-84027.HjBO0B.rst +++ /dev/null @@ -1 +0,0 @@ -:mod:`shutil` now can unpack wheel packages using the ``whl`` format. diff --git a/Misc/NEWS.d/next/Library/2025-11-08-13-22-33.gh-issue-140494.L_cKec.rst b/Misc/NEWS.d/next/Library/2025-11-08-13-22-33.gh-issue-140494.L_cKec.rst new file mode 100644 index 00000000000000..142cdac28f70f8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-08-13-22-33.gh-issue-140494.L_cKec.rst @@ -0,0 +1,2 @@ +Require that Tcl is always built with thread support. +This requirement only affects Tcl 8.x, as Tcl 9.x is always thread-enabled. diff --git a/Misc/NEWS.d/next/Library/2026-01-06-09-13-53.gh-issue-84644.V_cYP3.rst b/Misc/NEWS.d/next/Library/2026-01-06-09-13-53.gh-issue-84644.V_cYP3.rst deleted file mode 100644 index a1f4a598c0e070..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-01-06-09-13-53.gh-issue-84644.V_cYP3.rst +++ /dev/null @@ -1,5 +0,0 @@ -Deprecate using a return annotation to infer the dispatch type for -:func:`functools.singledispatch` and -:class:`functools.singledispatchmethod`. It will raise :exc:`TypeError` in -Python 3.21. Annotate the dispatch parameter or pass the dispatch type -explicitly instead. Patch by Bartosz Sławecki. diff --git a/Misc/NEWS.d/next/Library/2026-01-10-17-39-09.gh-issue-143663.9LEFAf.rst b/Misc/NEWS.d/next/Library/2026-01-10-17-39-09.gh-issue-143663.9LEFAf.rst new file mode 100644 index 00000000000000..8428770d51d734 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-10-17-39-09.gh-issue-143663.9LEFAf.rst @@ -0,0 +1,2 @@ +:meth:`tarfile.TarFile.extractall` now uses OS-specific separators to call +lstat. diff --git a/Misc/NEWS.d/next/Library/2026-03-23-00-04-13.gh-issue-146065.FIdn8D.rst b/Misc/NEWS.d/next/Library/2026-03-23-00-04-13.gh-issue-146065.FIdn8D.rst deleted file mode 100644 index db91ce76198c01..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-03-23-00-04-13.gh-issue-146065.FIdn8D.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash in asyncio.Future iterator when send() is -called after throw() or close(). diff --git a/Misc/NEWS.d/next/Library/2026-03-28-10-27-46.gh-issue-146169.RBF1xp.rst b/Misc/NEWS.d/next/Library/2026-03-28-10-27-46.gh-issue-146169.RBF1xp.rst deleted file mode 100644 index d9460540a5a7d3..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-03-28-10-27-46.gh-issue-146169.RBF1xp.rst +++ /dev/null @@ -1,4 +0,0 @@ -:mod:`xml.parsers.expat`: raise :exc:`RuntimeError` when an Expat handler -calls :meth:`parser.Parse ` or -:meth:`parser.ParseFile ` on the parser -that called the handler. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-03-28-14-34-18.gh-issue-143376.FvWTwi.rst b/Misc/NEWS.d/next/Library/2026-03-28-14-34-18.gh-issue-143376.FvWTwi.rst deleted file mode 100644 index 981e0ede58fde8..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-03-28-14-34-18.gh-issue-143376.FvWTwi.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`concurrent.interpreters`: fix a crash when internal cross-interpreter -channel ends have incorrect channel IDs. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-04-27-20-30-16.gh-issue-149083.xcmpSO.rst b/Misc/NEWS.d/next/Library/2026-04-27-20-30-16.gh-issue-149083.xcmpSO.rst deleted file mode 100644 index 38b8c904a68e25..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-04-27-20-30-16.gh-issue-149083.xcmpSO.rst +++ /dev/null @@ -1,2 +0,0 @@ -:data:`functools.Placeholder` is now a :class:`sentinel`. It is no longer an -instance of its own singleton type. diff --git a/Misc/NEWS.d/next/Library/2026-05-03-22-52-07.gh-issue-149309.gXhk9Y.rst b/Misc/NEWS.d/next/Library/2026-05-03-22-52-07.gh-issue-149309.gXhk9Y.rst deleted file mode 100644 index 69df9f1f6daff3..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-05-03-22-52-07.gh-issue-149309.gXhk9Y.rst +++ /dev/null @@ -1 +0,0 @@ -:exc:`bdb.BdbQuit` is now a subclass of :exc:`BaseException`, instead of :exc:`Exception`, to allow it to be used for cleanly exiting the debugger without being accidentally caught by user code. diff --git a/Misc/NEWS.d/next/Library/2026-05-09-12-58-04.gh-issue-149527.hX36Yh.rst b/Misc/NEWS.d/next/Library/2026-05-09-12-58-04.gh-issue-149527.hX36Yh.rst deleted file mode 100644 index d75b285554c0fa..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-05-09-12-58-04.gh-issue-149527.hX36Yh.rst +++ /dev/null @@ -1,3 +0,0 @@ -:mod:`multiprocessing`: fix creation of temporary socket files following -:gh:`137335` (`PR#148578 `__ -and corresponding backports). Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-05-21-16-15-01.gh-issue-105578.oq-0pg.rst b/Misc/NEWS.d/next/Library/2026-05-21-16-15-01.gh-issue-105578.oq-0pg.rst deleted file mode 100644 index 98b6f5abaeb94b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-05-21-16-15-01.gh-issue-105578.oq-0pg.rst +++ /dev/null @@ -1,2 +0,0 @@ -Deprecate :data:`typing.AnyStr` type variable and raise a warning when it -is imported. diff --git a/Misc/NEWS.d/next/Library/2026-06-27-08-31-30.gh-issue-152373.3cd0aa.rst b/Misc/NEWS.d/next/Library/2026-06-27-08-31-30.gh-issue-152373.3cd0aa.rst deleted file mode 100644 index bf6a70c1374a0d..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-06-27-08-31-30.gh-issue-152373.3cd0aa.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix the :mod:`pdb` ``until`` command not stepping over a list comprehension -when a breakpoint is set on it. Patch by tonghuaroot. diff --git a/Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst b/Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst deleted file mode 100644 index 70429574990997..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst +++ /dev/null @@ -1 +0,0 @@ -Fix :exc:`TypeError` when formatting a :exc:`SyntaxError` raised with :data:`NotImplemented` as its argument, which caused unexpected REPL exit and IDLE Shell restart. diff --git a/Misc/NEWS.d/next/Library/2026-06-30-08-39-07.gh-issue-62222.Zq7x2P.rst b/Misc/NEWS.d/next/Library/2026-06-30-08-39-07.gh-issue-62222.Zq7x2P.rst deleted file mode 100644 index d70b183b7cfa50..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-06-30-08-39-07.gh-issue-62222.Zq7x2P.rst +++ /dev/null @@ -1,6 +0,0 @@ -Fix :func:`binascii.a2b_qp` and :mod:`quopri` discarding the character after -a ``=`` that does not start a valid escape: ``b'==41'`` now decodes to -``b'=A'``, as in the :mod:`email` package. Add a keyword-only *strip_ws* -parameter to :func:`binascii.a2b_qp`, :func:`quopri.decode` and -:func:`quopri.decodestring` to strip trailing whitespace from each line, as -required by :rfc:`2045`. diff --git a/Misc/NEWS.d/next/Library/2026-07-04-12-52-22.gh-issue-149816.XQOutB.rst b/Misc/NEWS.d/next/Library/2026-07-04-12-52-22.gh-issue-149816.XQOutB.rst deleted file mode 100644 index eb273865894aae..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-07-04-12-52-22.gh-issue-149816.XQOutB.rst +++ /dev/null @@ -1,3 +0,0 @@ -:mod:`hashlib`: fix a crash arising from a race condition when looking -up an OpenSSL hash algorithm by name the first time. Patch by Bénédikt Tran. - diff --git a/Misc/NEWS.d/next/Library/2026-07-06-14-38-02.gh-issue-145578.aX11yQ.rst b/Misc/NEWS.d/next/Library/2026-07-06-14-38-02.gh-issue-145578.aX11yQ.rst new file mode 100644 index 00000000000000..d32d6a80238fdd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-06-14-38-02.gh-issue-145578.aX11yQ.rst @@ -0,0 +1,7 @@ +Added :mod:`tkinter` support for the Tk 9.1 accessibility API: the +:attr:`~tkinter.Misc.accessible` property of widgets returns an +:class:`~tkinter.Accessible` object whose attributes are exposed to +assistive technologies such as screen readers and whose methods register +custom widgets with the platform accessibility API, and the +:meth:`~tkinter.Misc.tk_check_screenreader` method reports whether a +screen reader is running. diff --git a/Misc/NEWS.d/next/Library/2026-07-08-09-05-01.gh-issue-153309.g94LSO.rst b/Misc/NEWS.d/next/Library/2026-07-08-09-05-01.gh-issue-153309.g94LSO.rst new file mode 100644 index 00000000000000..9245c1442b06f8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-08-09-05-01.gh-issue-153309.g94LSO.rst @@ -0,0 +1 @@ +:mod:`http`: Add the HTTP QUERY method (:rfc:`10008`) to :class:`~http.HTTPMethod`. diff --git a/Misc/NEWS.d/next/Library/2026-07-11-16-00-00.gh-issue-153605.Rk7Xz3.rst b/Misc/NEWS.d/next/Library/2026-07-11-16-00-00.gh-issue-153605.Rk7Xz3.rst deleted file mode 100644 index a08fde3045c077..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-07-11-16-00-00.gh-issue-153605.Rk7Xz3.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :class:`gettext.GNUTranslations` raising :exc:`struct.error` instead of -:exc:`OSError` when reading a malformed ``.mo`` file. Patch by tonghuaroot. diff --git a/Misc/NEWS.d/next/Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst b/Misc/NEWS.d/next/Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst deleted file mode 100644 index d612903f225021..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst +++ /dev/null @@ -1,3 +0,0 @@ -:class:`zipimport.zipimporter` now raises :exc:`zipimport.ZipImportError` -instead of :exc:`UnicodeDecodeError` for a UTF-8-flagged file name that is not -valid UTF-8. Patch by tonghuaroot. diff --git a/Misc/NEWS.d/next/Library/2026-07-16-21-30-00.gh-issue-74876.wR3fXk.rst b/Misc/NEWS.d/next/Library/2026-07-16-21-30-00.gh-issue-74876.wR3fXk.rst deleted file mode 100644 index aef5cea67047fc..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-07-16-21-30-00.gh-issue-74876.wR3fXk.rst +++ /dev/null @@ -1,8 +0,0 @@ -Weak references (:class:`weakref.ref`), :class:`weakref.WeakMethod`, -:class:`weakref.WeakSet`, :class:`weakref.WeakValueDictionary` and -:class:`weakref.WeakKeyDictionary` can now be pickled -if the weakly referenced objects are strongly referenced -elsewhere in the same pickle; -otherwise pickling raises :exc:`pickle.PicklingError`. -:class:`weakref.WeakMethod` and :class:`weakref.WeakSet` -can now also be copied. diff --git a/Misc/NEWS.d/next/Library/2026-07-17-14-00-00.gh-issue-153840.FgXs3e.rst b/Misc/NEWS.d/next/Library/2026-07-17-14-00-00.gh-issue-153840.FgXs3e.rst new file mode 100644 index 00000000000000..43316035d9b2c2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-17-14-00-00.gh-issue-153840.FgXs3e.rst @@ -0,0 +1,3 @@ +Escape special characters in ``profiling.sampling`` flamegraph function +names to prevent markup injection in the generated HTML report. Patch by +tonghuaroot. diff --git a/Misc/NEWS.d/next/Library/2026-07-20-09-35-32.gh-issue-153144.0a2AoO.rst b/Misc/NEWS.d/next/Library/2026-07-20-09-35-32.gh-issue-153144.0a2AoO.rst new file mode 100644 index 00000000000000..0d0aac646285e7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-20-09-35-32.gh-issue-153144.0a2AoO.rst @@ -0,0 +1,4 @@ +:func:`math.atan2` and :func:`math.atan2pi` no longer raise :exc:`ValueError`. +:func:`cmath.phase` no longer raises :exc:`OverflowError` or :exc:`ValueError`, +and the values of ``math.atan2(b, a)`` and ``cmath.phase(complex(a, b))`` always +match. Contributed by High Performance Kernels LLC. diff --git a/Misc/NEWS.d/next/Library/2026-07-21-00-02-11.gh-issue-148228.D3rP8m.rst b/Misc/NEWS.d/next/Library/2026-07-21-00-02-11.gh-issue-148228.D3rP8m.rst deleted file mode 100644 index d00d072fb2da08..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-07-21-00-02-11.gh-issue-148228.D3rP8m.rst +++ /dev/null @@ -1,3 +0,0 @@ -The :term:`REPL` now displays error messages when editing commands cannot be -performed, such as when tab completion finds no matches or a history search -fails. Patch by Bartosz Sławecki. diff --git a/Misc/NEWS.d/next/Library/2026-07-29-12-20-39.gh-issue-127172.Kf3Xa9.rst b/Misc/NEWS.d/next/Library/2026-07-29-12-20-39.gh-issue-127172.Kf3Xa9.rst new file mode 100644 index 00000000000000..c4bf1f07a2ea57 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-29-12-20-39.gh-issue-127172.Kf3Xa9.rst @@ -0,0 +1,4 @@ +Creating a virtual environment with :mod:`venv` now writes the activation +scripts using permissions derived from the process umask (and any default +ACL), consistent with the other files it creates, instead of a fixed mode +copied from the packaged templates. Patch by tonghuaroot. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.35942a.rst b/Misc/NEWS.d/next/Library/2026-08-01-10-54-39.gh-issue-155031.sTB_n8.rst similarity index 77% rename from Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.35942a.rst rename to Misc/NEWS.d/next/Library/2026-08-01-10-54-39.gh-issue-155031.sTB_n8.rst index 548ac837589f07..954e4cc2e54ac0 100644 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.35942a.rst +++ b/Misc/NEWS.d/next/Library/2026-08-01-10-54-39.gh-issue-155031.sTB_n8.rst @@ -1 +1,2 @@ -Improve performance when parsing email address groups with many members. +Propagate to user any permission errors from subprocess attach commands in +Tachyon diff --git a/Misc/NEWS.d/next/Library/2026-08-02-08-43-20.gh-issue-155003.VVy9Z_.rst b/Misc/NEWS.d/next/Library/2026-08-02-08-43-20.gh-issue-155003.VVy9Z_.rst new file mode 100644 index 00000000000000..c41ce1879488b7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-02-08-43-20.gh-issue-155003.VVy9Z_.rst @@ -0,0 +1,5 @@ +Fix :class:`multiprocessing.shared_memory.SharedMemory` leaking its file +descriptor and printing an ignored :exc:`BufferError` when garbage collected +while its :attr:`~multiprocessing.shared_memory.SharedMemory.buf` was still in +use, for example by a NumPy array. :attr:`!buf` now also stays usable after +the :class:`!SharedMemory` object is garbage collected. diff --git a/Misc/NEWS.d/next/Library/2026-08-02-19-08-17.gh-issue-155074.Vj8qL2.rst b/Misc/NEWS.d/next/Library/2026-08-02-19-08-17.gh-issue-155074.Vj8qL2.rst new file mode 100644 index 00000000000000..70489c9d62f526 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-02-19-08-17.gh-issue-155074.Vj8qL2.rst @@ -0,0 +1,2 @@ +Fix the pure Python implementation of ``BufferedReader.readinto()`` raising +:exc:`ValueError` after partially filling the destination buffer. diff --git a/Misc/NEWS.d/next/Library/2026-08-04-16-38-59.gh-issue-155152.qN1EEN.rst b/Misc/NEWS.d/next/Library/2026-08-04-16-38-59.gh-issue-155152.qN1EEN.rst deleted file mode 100644 index c7cff1866cc51f..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-04-16-38-59.gh-issue-155152.qN1EEN.rst +++ /dev/null @@ -1 +0,0 @@ -Improve performance of some :mod:`zipimport` archives that use Zstandard compression. diff --git a/Misc/NEWS.d/next/Library/2026-08-05-10-13-23.gh-issue-153711.9g9dVz.rst b/Misc/NEWS.d/next/Library/2026-08-05-10-13-23.gh-issue-153711.9g9dVz.rst deleted file mode 100644 index ca825ce6917db9..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-05-10-13-23.gh-issue-153711.9g9dVz.rst +++ /dev/null @@ -1,3 +0,0 @@ -On iOS, add :func:`os.pipe2`, and use the syscalls :manpage:`pipe2 (2)` and -:manpage:`dup3 (2)` internally, when available at both build-time and -run-time. diff --git a/Misc/NEWS.d/next/Library/2026-08-05-20-30-00.gh-issue-75221.runpyM.rst b/Misc/NEWS.d/next/Library/2026-08-05-20-30-00.gh-issue-75221.runpyM.rst new file mode 100644 index 00000000000000..77326f3f98bf94 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-05-20-30-00.gh-issue-75221.runpyM.rst @@ -0,0 +1,4 @@ +:func:`runpy.run_path` and the execution of a directory or a zipfile now +search the ``__main__`` module only in that directory or zipfile. Previously +an existing ``__main__`` module located elsewhere on :data:`sys.path` could be +executed instead. diff --git a/Misc/NEWS.d/next/Library/2026-08-08-23-30-00.gh-issue-85681.ntabs.rst b/Misc/NEWS.d/next/Library/2026-08-08-23-30-00.gh-issue-85681.ntabs.rst new file mode 100644 index 00000000000000..b673a69a70620b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-08-23-30-00.gh-issue-85681.ntabs.rst @@ -0,0 +1,3 @@ +:func:`!ntpath.abspath` on non-Windows platforms now strips trailing dots and +spaces like the Windows path normalization does. This makes +:func:`!ntpath.relpath` and other functions based on it platform independent. diff --git a/Misc/NEWS.d/next/Library/2026-08-09-14-06-27.gh-issue-155426.zZVPMd.rst b/Misc/NEWS.d/next/Library/2026-08-09-14-06-27.gh-issue-155426.zZVPMd.rst deleted file mode 100644 index f1839defbe5674..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-09-14-06-27.gh-issue-155426.zZVPMd.rst +++ /dev/null @@ -1,4 +0,0 @@ -Make sure that the pure Python fallback of :func:`hmac.digest` reject -keys that are neither :class:`bytes` or :class:`bytearray`. Previously, -an :exc:`AttributeError` was raised instead of :exc:`TypeError`. -Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-08-09-14-26-48.gh-issue-155428.wi0_fI.rst b/Misc/NEWS.d/next/Library/2026-08-09-14-26-48.gh-issue-155428.wi0_fI.rst deleted file mode 100644 index f0c6201cbe592e..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-09-14-26-48.gh-issue-155428.wi0_fI.rst +++ /dev/null @@ -1,3 +0,0 @@ -Ensure that :func:`hmac.digest` and :meth:`hmac.HMAC.digest` output -identical values when using a digest's block size smaller than 16. Patch by -Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-08-15-11-30-42.gh-issue-155782.JUmDP4.rst b/Misc/NEWS.d/next/Library/2026-08-15-11-30-42.gh-issue-155782.JUmDP4.rst deleted file mode 100644 index cafa887044fa44..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-15-11-30-42.gh-issue-155782.JUmDP4.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`ssl`: prevent a crash in :meth:`~ssl.SSLSocket.group` -for session-less objects on OpenSSL 3.2 and later. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-08-15-11-49-30.gh-issue-155830.cY5xgK.rst b/Misc/NEWS.d/next/Library/2026-08-15-11-49-30.gh-issue-155830.cY5xgK.rst deleted file mode 100644 index 9d6c2a82903d02..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-15-11-49-30.gh-issue-155830.cY5xgK.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`!_blake2`: Ensure that BLAKE-2 objects are correctly initialized -before being tracked by the garbage collector. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-08-15-12-08-33.gh-issue-155833.Pi2Yjp.rst b/Misc/NEWS.d/next/Library/2026-08-15-12-08-33.gh-issue-155833.Pi2Yjp.rst deleted file mode 100644 index 96bf5de44bd836..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-15-12-08-33.gh-issue-155833.Pi2Yjp.rst +++ /dev/null @@ -1,2 +0,0 @@ -Ensure that MD5 and SHA* objects are properly GC-untracked before freeing -their internal HACL* state. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-08-17-06-07-49.gh-issue-155935.lbMPKW.rst b/Misc/NEWS.d/next/Library/2026-08-17-06-07-49.gh-issue-155935.lbMPKW.rst new file mode 100644 index 00000000000000..21d7ba6a4f758c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-17-06-07-49.gh-issue-155935.lbMPKW.rst @@ -0,0 +1 @@ +Fix :meth:`!importlib.machinery.FileFinder.discover` looping forever when the underlying directory scan keeps failing with :exc:`OSError`. A failing scan now ends the listing instead of being retried. diff --git a/Misc/NEWS.d/next/Library/2026-08-20-09-56-45.gh-issue-156078.-E7gZc.rst b/Misc/NEWS.d/next/Library/2026-08-20-09-56-45.gh-issue-156078.-E7gZc.rst deleted file mode 100644 index d2a3a219fa8958..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-20-09-56-45.gh-issue-156078.-E7gZc.rst +++ /dev/null @@ -1,2 +0,0 @@ -:meth:`zipfile.ZipFile.mkdir` now raises :exc:`ValueError` when the archive -is closed or when a writable member handle is open. diff --git a/Misc/NEWS.d/next/Library/2026-08-21-06-00-00.gh-issue-156057.tarfile-wrap-comp-err.rst b/Misc/NEWS.d/next/Library/2026-08-21-06-00-00.gh-issue-156057.tarfile-wrap-comp-err.rst deleted file mode 100644 index 1c8d8839dbf7bd..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-21-06-00-00.gh-issue-156057.tarfile-wrap-comp-err.rst +++ /dev/null @@ -1,5 +0,0 @@ -Wrap the underlying compression library's exception in :exc:`tarfile.ReadError` -when a member's compressed data is corrupt past its header, for all of -:mod:`tarfile`'s supported compression formats (gzip, bzip2, lzma, zstd). -Previously the raw exception (e.g. :exc:`zlib.error`) could leak through -:meth:`tarfile.TarFile.extractfile`. Patch by Jason Mak. diff --git a/Misc/NEWS.d/next/Library/2026-08-23-16-33-00.gh-issue-151126.GUvtG7.rst b/Misc/NEWS.d/next/Library/2026-08-23-16-33-00.gh-issue-151126.GUvtG7.rst deleted file mode 100644 index 01a49138469598..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-23-16-33-00.gh-issue-151126.GUvtG7.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash caused by failing to set :exc:`MemoryError` on allocation -failure when first using a Chinese, Japanese or Korean codec. diff --git a/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst b/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst new file mode 100644 index 00000000000000..54c04dcc1ecc7b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-25-09-15-00.gh-issue-154923.tKcr8Q.rst @@ -0,0 +1,2 @@ +Fix a crash when creating Tcl interpreters concurrently in different +threads in the :term:`free-threaded build`. diff --git a/Misc/NEWS.d/next/Library/2026-08-29-13-41-56.gh-issue-154675.cYvE-Z.rst b/Misc/NEWS.d/next/Library/2026-08-29-13-41-56.gh-issue-154675.cYvE-Z.rst deleted file mode 100644 index 02d4996702b458..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-29-13-41-56.gh-issue-154675.cYvE-Z.rst +++ /dev/null @@ -1,2 +0,0 @@ -Improve import time of :mod:`pdb`, :mod:`bdb` and :mod:`code` by lazily -importing several dependencies. diff --git a/Misc/NEWS.d/next/Library/2026-08-29-14-28-46.gh-issue-156568.mkxLY8.rst b/Misc/NEWS.d/next/Library/2026-08-29-14-28-46.gh-issue-156568.mkxLY8.rst deleted file mode 100644 index 3efd5a0481874c..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-08-29-14-28-46.gh-issue-156568.mkxLY8.rst +++ /dev/null @@ -1,2 +0,0 @@ -:func:`zipapp.create_archive` now correctly sets the executable bit on the -target archive when copying a zipapp where the target is a path-like object. diff --git a/Misc/NEWS.d/next/Library/2026-09-01-00-00-00.gh-issue-156784.8gvtt4.rst b/Misc/NEWS.d/next/Library/2026-09-01-00-00-00.gh-issue-156784.8gvtt4.rst deleted file mode 100644 index 699448059114ec..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-01-00-00-00.gh-issue-156784.8gvtt4.rst +++ /dev/null @@ -1,3 +0,0 @@ -:class:`asyncio.ProactorEventLoop` no longer silently ignores -:exc:`ConnectionResetError` on UDP sockets, matching -:class:`asyncio.SelectorEventLoop`. diff --git a/Misc/NEWS.d/next/Library/2026-09-05-17-29-50.gh-issue-160837.W6kHva.rst b/Misc/NEWS.d/next/Library/2026-09-05-17-29-50.gh-issue-160837.W6kHva.rst deleted file mode 100644 index 701e42548d5d26..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-05-17-29-50.gh-issue-160837.W6kHva.rst +++ /dev/null @@ -1,2 +0,0 @@ -:func:`asyncio.print_call_graph` no longer truncates the call stack of a -task suspended inside an asynchronous generator. diff --git a/Misc/NEWS.d/next/Library/2026-09-11-12-43-04.gh-issue-157299.HQ3I4F.rst b/Misc/NEWS.d/next/Library/2026-09-11-12-43-04.gh-issue-157299.HQ3I4F.rst new file mode 100644 index 00000000000000..fe9483325829c4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-11-12-43-04.gh-issue-157299.HQ3I4F.rst @@ -0,0 +1,4 @@ +Fix eagerly started :class:`asyncio.Task` objects missing their awaited-by +edge during the first step. During that step, even a plain +:func:`asyncio.create_task` call reports the creating task as an awaiter. +Patch by Timofey Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-09-15-10-30-00.gh-issue-92810.Kx3abc.rst b/Misc/NEWS.d/next/Library/2026-09-15-10-30-00.gh-issue-92810.Kx3abc.rst new file mode 100644 index 00000000000000..45fc06791f6950 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-15-10-30-00.gh-issue-92810.Kx3abc.rst @@ -0,0 +1,4 @@ +Speed up :func:`isinstance` and :func:`issubclass` checks against +:class:`abc.ABCMeta` classes, fix quadratic time and memory usage for large +class hierarchies, and improve their scaling across threads in the +free-threaded build. Patch by Kumar Aditya. diff --git a/Misc/NEWS.d/next/Library/2026-09-17-10-30-00.gh-issue-158252.strptime.rst b/Misc/NEWS.d/next/Library/2026-09-17-10-30-00.gh-issue-158252.strptime.rst deleted file mode 100644 index 9d3eeb245023a9..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-17-10-30-00.gh-issue-158252.strptime.rst +++ /dev/null @@ -1,4 +0,0 @@ -Speed up :func:`time.strptime`, :meth:`datetime.date.strptime`, -:meth:`datetime.time.strptime`, and :meth:`datetime.datetime.strptime` for -common numeric formats by parsing fields and constructing the result in C. -Other inputs continue to use the Python parser. diff --git a/Misc/NEWS.d/next/Library/2026-09-17-20-36-01.gh-issue-157290.NfFmt.rst b/Misc/NEWS.d/next/Library/2026-09-17-20-36-01.gh-issue-157290.NfFmt.rst deleted file mode 100644 index 7a001e8784b2be..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-17-20-36-01.gh-issue-157290.NfFmt.rst +++ /dev/null @@ -1,4 +0,0 @@ -Add the ``%Nf`` format code (where *N* is from 1 to 6) to -:meth:`datetime.datetime.strftime`, :meth:`datetime.time.strftime`, -:meth:`datetime.date.strftime`, :meth:`datetime.datetime.strptime` and -:meth:`datetime.time.strptime`. diff --git a/Misc/NEWS.d/next/Library/2026-09-18-00-00-00.gh-issue-49960.MDT3jE.rst b/Misc/NEWS.d/next/Library/2026-09-18-00-00-00.gh-issue-49960.MDT3jE.rst deleted file mode 100644 index 9793c1143c7d0d..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-18-00-00-00.gh-issue-49960.MDT3jE.rst +++ /dev/null @@ -1,3 +0,0 @@ -:mod:`ctypes` callback functions can now return structures and unions by -value. Previously only "simple" types could be returned, and any other result -type raised :exc:`TypeError`. diff --git a/Misc/NEWS.d/next/Library/2026-09-20-12-00-00.gh-issue-157856.wq4xuF.rst b/Misc/NEWS.d/next/Library/2026-09-20-12-00-00.gh-issue-157856.wq4xuF.rst deleted file mode 100644 index 9f221fa657d429..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-20-12-00-00.gh-issue-157856.wq4xuF.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :func:`asyncio.as_completed` raising :exc:`RuntimeError` ("no running -event loop") when the iterator is created outside of a running event loop and -driven with :meth:`loop.run_until_complete() `. diff --git a/Misc/NEWS.d/next/Library/2026-09-20-21-24-05.gh-issue-157880.aE7wIu.rst b/Misc/NEWS.d/next/Library/2026-09-20-21-24-05.gh-issue-157880.aE7wIu.rst deleted file mode 100644 index 7fbb1080047989..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-20-21-24-05.gh-issue-157880.aE7wIu.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :class:`mailbox.Babyl` to write the visible headers of a -:class:`mailbox.BabylMessage` to the mailbox file. diff --git a/Misc/NEWS.d/next/Library/2026-09-21-14-41-07.gh-issue-157899.s6vBQC.rst b/Misc/NEWS.d/next/Library/2026-09-21-14-41-07.gh-issue-157899.s6vBQC.rst deleted file mode 100644 index d70cef97fb2076..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-21-14-41-07.gh-issue-157899.s6vBQC.rst +++ /dev/null @@ -1,2 +0,0 @@ -:func:`os.readlink` now accepts a file descriptor referring to a -symlink on Linux, Android, macOS and iOS. diff --git a/Misc/NEWS.d/next/Library/2026-09-21-21-07-12.gh-issue-157928.ZPlNYC.rst b/Misc/NEWS.d/next/Library/2026-09-21-21-07-12.gh-issue-157928.ZPlNYC.rst deleted file mode 100644 index dfa7a122a9e22e..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-21-21-07-12.gh-issue-157928.ZPlNYC.rst +++ /dev/null @@ -1 +0,0 @@ -Correctly detect and handle OSC sequences in PyREPL. diff --git a/Misc/NEWS.d/next/Library/2026-09-23-09-21-25.gh-issue-89977.fmia18.rst b/Misc/NEWS.d/next/Library/2026-09-23-09-21-25.gh-issue-89977.fmia18.rst deleted file mode 100644 index fcce4b6923dd3e..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-23-09-21-25.gh-issue-89977.fmia18.rst +++ /dev/null @@ -1,3 +0,0 @@ -Avoid releasing the GIL for :meth:`socket.socket.setblocking`, -:meth:`socket.socket.getsockname`, :meth:`socket.socket.getpeername`, and -:func:`socket.gethostname`. diff --git a/Misc/NEWS.d/next/Library/2026-09-23-19-59-03.gh-issue-158004.p68swW.rst b/Misc/NEWS.d/next/Library/2026-09-23-19-59-03.gh-issue-158004.p68swW.rst deleted file mode 100644 index 0db4a3d4ca4c4a..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-23-19-59-03.gh-issue-158004.p68swW.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :func:`asyncio.as_completed` to continue yielding every awaitable after -a pending iteration step is cancelled. diff --git a/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst b/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst new file mode 100644 index 00000000000000..0e0a565519fa8e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst @@ -0,0 +1,2 @@ +Fix a crash in :meth:`xml.etree.ElementTree.Element.iter` when a tag's +equality comparison replaces that tag. diff --git a/Misc/NEWS.d/next/Library/2026-09-23-23-34-30.gh-issue-144812.ujptbVOI.rst b/Misc/NEWS.d/next/Library/2026-09-23-23-34-30.gh-issue-144812.ujptbVOI.rst deleted file mode 100644 index d24f028fdbeaae..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-23-23-34-30.gh-issue-144812.ujptbVOI.rst +++ /dev/null @@ -1,3 +0,0 @@ -Make :class:`argparse.ArgumentGroup` and -:class:`argparse.MutuallyExclusiveGroup` public. The private -``_ArgumentGroup`` and ``_MutuallyExclusiveGroup`` names remain as aliases. diff --git a/Misc/NEWS.d/next/Library/2026-09-24-14-59-08.gh-issue-158095.lcKOT4.rst b/Misc/NEWS.d/next/Library/2026-09-24-14-59-08.gh-issue-158095.lcKOT4.rst deleted file mode 100644 index 4fe92b8c8097d8..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-24-14-59-08.gh-issue-158095.lcKOT4.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a memory leak in :func:`re.finditer` and regular expression scanners when -their input participates in a reference cycle. diff --git a/Misc/NEWS.d/next/Library/2026-09-25-01-29-55.gh-issue-158126.nWmFnq.rst b/Misc/NEWS.d/next/Library/2026-09-25-01-29-55.gh-issue-158126.nWmFnq.rst deleted file mode 100644 index 82bf49faef56d4..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-25-01-29-55.gh-issue-158126.nWmFnq.rst +++ /dev/null @@ -1,3 +0,0 @@ -Speed up :func:`pickle.dumps` and creating a :class:`pickle.Pickler` by -avoiding the creation of an :exc:`AttributeError` when looking up the -optional ``reducer_override`` and ``dispatch_table`` attributes. diff --git a/Misc/NEWS.d/next/Library/2026-09-25-17-04-45.gh-issue-158159.lbEalG.rst b/Misc/NEWS.d/next/Library/2026-09-25-17-04-45.gh-issue-158159.lbEalG.rst deleted file mode 100644 index a0d365111d92ff..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-25-17-04-45.gh-issue-158159.lbEalG.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a use-after-free in :meth:`asyncio.Task.get_name` when formatting the -default task name triggers a garbage collection whose finalizers call -:meth:`~asyncio.Task.get_name` on the same task. diff --git a/Misc/NEWS.d/next/Library/2026-09-26-05-18-21.gh-issue-144650.zqHLOW.rst b/Misc/NEWS.d/next/Library/2026-09-26-05-18-21.gh-issue-144650.zqHLOW.rst deleted file mode 100644 index ef4323571e5998..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-26-05-18-21.gh-issue-144650.zqHLOW.rst +++ /dev/null @@ -1,2 +0,0 @@ -Use Argument Clinic vectorcall support for the :class:`decimal.Decimal` type. This offers -1.10x speedup on creation of small instances. diff --git a/Misc/NEWS.d/next/Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.rst b/Misc/NEWS.d/next/Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.rst new file mode 100644 index 00000000000000..528160a6709369 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.rst @@ -0,0 +1 @@ +Fix the type of the ``msg`` attribute of :exc:`xml.etree.ElementTree.ParseError` in the pure Python implementation: it is now a string, as in the C implementation. diff --git a/Misc/NEWS.d/next/Library/2026-09-27-13-04-02.gh-issue-42550.sqYlvB.rst b/Misc/NEWS.d/next/Library/2026-09-27-13-04-02.gh-issue-42550.sqYlvB.rst new file mode 100644 index 00000000000000..af8fe2630ba440 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-27-13-04-02.gh-issue-42550.sqYlvB.rst @@ -0,0 +1,3 @@ +:class:`http.client.HTTPConnection` now waits for a ``100 Continue`` +response before sending body content if the ``Expect: 100-Continue`` header +is set. diff --git a/Misc/NEWS.d/next/Library/2026-09-27-14-00-00.gh-issue-158277.aBcDeF.rst b/Misc/NEWS.d/next/Library/2026-09-27-14-00-00.gh-issue-158277.aBcDeF.rst deleted file mode 100644 index 7f0434aeea6708..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-27-14-00-00.gh-issue-158277.aBcDeF.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :mod:`sqlite3` CLI completer to include view columns in -column-name completion. - diff --git a/Misc/NEWS.d/next/Library/2026-09-28-00-00-00.gh-issue-158370.ctxmgr.rst b/Misc/NEWS.d/next/Library/2026-09-28-00-00-00.gh-issue-158370.ctxmgr.rst deleted file mode 100644 index f4b9e2469daa3e..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-28-00-00-00.gh-issue-158370.ctxmgr.rst +++ /dev/null @@ -1,2 +0,0 @@ -:class:`concurrent.interpreters.Interpreter` objects now support the -:term:`context manager` protocol. diff --git a/Misc/NEWS.d/next/Library/2026-09-29-19-34-30.gh-issue-158192.6wPjiU.rst b/Misc/NEWS.d/next/Library/2026-09-29-19-34-30.gh-issue-158192.6wPjiU.rst deleted file mode 100644 index b0634084c6af8a..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-09-29-19-34-30.gh-issue-158192.6wPjiU.rst +++ /dev/null @@ -1 +0,0 @@ -Expose the future being waited on by a ``FutureIter`` as ``fi_future``. diff --git a/Misc/NEWS.d/next/Library/2026-10-02-22-32-10.gh-issue-156145.9l_tZb.rst b/Misc/NEWS.d/next/Library/2026-10-02-22-32-10.gh-issue-156145.9l_tZb.rst new file mode 100644 index 00000000000000..b29e6b8be6dc88 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-02-22-32-10.gh-issue-156145.9l_tZb.rst @@ -0,0 +1,3 @@ +The following functions no longer set ``errno`` to zero: :func:`cmath.phase`, +:func:`math.atan2`, :func:`math.atan2pi`, and :func:`math.copysign`. +Contributed by High Performance Kernels LLC. diff --git a/Misc/NEWS.d/next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst b/Misc/NEWS.d/next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst deleted file mode 100644 index 9aef7647e64e41..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix PyREPL to convert :data:`sys.ps1` and :data:`sys.ps2` to strings before -use, and fall back to the default prompts if the conversion raises. diff --git a/Misc/NEWS.d/next/Library/2026-10-05-14-58-12.gh-issue-158839.wYd6fI.rst b/Misc/NEWS.d/next/Library/2026-10-05-14-58-12.gh-issue-158839.wYd6fI.rst deleted file mode 100644 index 9bdf648f42783f..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-05-14-58-12.gh-issue-158839.wYd6fI.rst +++ /dev/null @@ -1,5 +0,0 @@ -The log functions in the :mod:`logging` module and the -:class:`logging.Logger` class now normalize falsy ``exc_info`` arguments to -``None`` before passing them to :meth:`logging.Logger.makeRecord` and -subsequently to :class:`logging.LogRecord`. This aligns -``logging.LogRecord.exc_info`` with the documentation. diff --git a/Misc/NEWS.d/next/Library/2026-10-07-10-59-27.gh-issue-155233.YGuQGK.rst b/Misc/NEWS.d/next/Library/2026-10-07-10-59-27.gh-issue-155233.YGuQGK.rst deleted file mode 100644 index ebcd4a4bc29a5c..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-07-10-59-27.gh-issue-155233.YGuQGK.rst +++ /dev/null @@ -1 +0,0 @@ -Fix :meth:`asyncio.Barrier.wait` returning duplicate indices when a waiting task is cancelled before the barrier is released.``` diff --git a/Misc/NEWS.d/next/Library/2026-10-07-17-30-00.gh-issue-158287.xY9zQp.rst b/Misc/NEWS.d/next/Library/2026-10-07-17-30-00.gh-issue-158287.xY9zQp.rst deleted file mode 100644 index d7432faa0b7f92..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-07-17-30-00.gh-issue-158287.xY9zQp.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a bug in :mod:`asyncio` where stopping a child process (such as with ``SIGSTOP``) on macOS caused the event loop to block indefinitely. diff --git a/Misc/NEWS.d/next/Library/2026-10-07-21-54-29.gh-issue-158955.Ey4Iei.rst b/Misc/NEWS.d/next/Library/2026-10-07-21-54-29.gh-issue-158955.Ey4Iei.rst new file mode 100644 index 00000000000000..e7e613ec77e694 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-07-21-54-29.gh-issue-158955.Ey4Iei.rst @@ -0,0 +1 @@ +Fixed PyREPL rendering on WSL by avoiding the unnecessary INS terminal sequence when appending a character at the end of a line. INS remains used for true mid-line insertions, with regression coverage added. diff --git a/Misc/NEWS.d/next/Library/2026-10-08-01-33-46.gh-issue-158986.iXoUCo.rst b/Misc/NEWS.d/next/Library/2026-10-08-01-33-46.gh-issue-158986.iXoUCo.rst new file mode 100644 index 00000000000000..f234a601a97103 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-08-01-33-46.gh-issue-158986.iXoUCo.rst @@ -0,0 +1,4 @@ +Fix :mod:`profiling.sampling` reporting modules in a ``site-packages`` +directory located inside the standard library directory as standard library +modules. The heatmap now lists them under "Site Packages" and flamegraph +labels no longer start with ``site-packages.``. diff --git a/Misc/NEWS.d/next/Library/2026-10-08-11-58-52.gh-issue-158628.NJxwuw.rst b/Misc/NEWS.d/next/Library/2026-10-08-11-58-52.gh-issue-158628.NJxwuw.rst deleted file mode 100644 index eb1e0f1d3271f8..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-08-11-58-52.gh-issue-158628.NJxwuw.rst +++ /dev/null @@ -1 +0,0 @@ -Implement :data:`sys._wasi_info` for exposing WASI-specific system details. diff --git a/Misc/NEWS.d/next/Library/2026-10-08-21-02-38.gh-issue-158998.DZYJI7.rst b/Misc/NEWS.d/next/Library/2026-10-08-21-02-38.gh-issue-158998.DZYJI7.rst deleted file mode 100644 index 65ce9201fb0135..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-08-21-02-38.gh-issue-158998.DZYJI7.rst +++ /dev/null @@ -1 +0,0 @@ -Fix :func:`os.statx` ignoring the *follow_symlinks* argument. diff --git a/Misc/NEWS.d/next/Library/2026-10-10-13-35-08.gh-issue-149584.Yq2bF7.rst b/Misc/NEWS.d/next/Library/2026-10-10-13-35-08.gh-issue-149584.Yq2bF7.rst new file mode 100644 index 00000000000000..7f92a1b19877cd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-10-13-35-08.gh-issue-149584.Yq2bF7.rst @@ -0,0 +1,2 @@ +Improve the performance of the Tachyon profiler by avoiding unnecessary remote +reads of the base frame sentinel. Patch by Maurycy Pawłowski-Wieroński. diff --git a/Misc/NEWS.d/next/Library/2026-10-10-18-56-26.gh-issue-159130.3AIHqh.rst b/Misc/NEWS.d/next/Library/2026-10-10-18-56-26.gh-issue-159130.3AIHqh.rst new file mode 100644 index 00000000000000..f4ff555a25e701 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-10-18-56-26.gh-issue-159130.3AIHqh.rst @@ -0,0 +1,2 @@ +Fix :mod:`!_remote_debugging` returning a different thread when the selected +thread does not match the profiling mode with ``only_active_thread=True``. diff --git a/Misc/NEWS.d/next/Library/2026-10-10-22-14-22.gh-issue-150751.JInuGL.rst b/Misc/NEWS.d/next/Library/2026-10-10-22-14-22.gh-issue-150751.JInuGL.rst deleted file mode 100644 index 40c8d606aa6773..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-10-22-14-22.gh-issue-150751.JInuGL.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`http.client` now ignores optional whitespace around the -``Content-Length`` header value when validating it, as required by -:rfc:`9112`. Previously a value with trailing whitespace (for example -``Content-Length: 5`` followed by a space) was treated as missing and the -response body was read until the connection was closed. diff --git a/Misc/NEWS.d/next/Library/2026-10-10-22-29-13.gh-issue-159049.eJUiKL.rst b/Misc/NEWS.d/next/Library/2026-10-10-22-29-13.gh-issue-159049.eJUiKL.rst deleted file mode 100644 index 96f1cc3c0520d5..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-10-22-29-13.gh-issue-159049.eJUiKL.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix reference cycles in :meth:`asyncio.loop.create_connection` and -:meth:`asyncio.loop.start_tls` after a failed TLS handshake. -Patch by Timofei Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-10-11-08-42-00.gh-issue-159184.Tq2nSe.rst b/Misc/NEWS.d/next/Library/2026-10-11-08-42-00.gh-issue-159184.Tq2nSe.rst deleted file mode 100644 index 240738671ffaaf..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-11-08-42-00.gh-issue-159184.Tq2nSe.rst +++ /dev/null @@ -1,3 +0,0 @@ -The output of :func:`marshal.dumps` no longer depends on the order of the -elements of a set which have the same marshalled form, such as NaNs which -are distinct objects. diff --git a/Misc/NEWS.d/next/Library/2026-10-11-13-40-44.gh-issue-159168.z3KjB8.rst b/Misc/NEWS.d/next/Library/2026-10-11-13-40-44.gh-issue-159168.z3KjB8.rst deleted file mode 100644 index 54b8738e06c270..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-11-13-40-44.gh-issue-159168.z3KjB8.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :exc:`RecursionError` in ``python -m asyncio pstree`` on a deep chain of -awaiting tasks. Patch by Timofei Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-10-12-00-00-00.gh-issue-159044.Tr8kXv.rst b/Misc/NEWS.d/next/Library/2026-10-12-00-00-00.gh-issue-159044.Tr8kXv.rst deleted file mode 100644 index ef51dc8eca1066..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-12-00-00-00.gh-issue-159044.Tr8kXv.rst +++ /dev/null @@ -1,4 +0,0 @@ -Restore the previous behaviour of :meth:`http.client.HTTPConnection.set_tunnel` -where header values that are not strings (such as :const:`None`, :class:`int` -or other objects) are converted with :class:`str` instead of raising -:exc:`AttributeError` when the request is sent. diff --git a/Misc/NEWS.d/next/Library/2026-10-12-11-36-48.gh-issue-158856.jLR-Lv.rst b/Misc/NEWS.d/next/Library/2026-10-12-11-36-48.gh-issue-158856.jLR-Lv.rst deleted file mode 100644 index a95c7b0cb44af3..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-12-11-36-48.gh-issue-158856.jLR-Lv.rst +++ /dev/null @@ -1 +0,0 @@ -Improve large Enum creation performance. diff --git a/Misc/NEWS.d/next/Library/2026-10-12-12-00-00.gh-issue-72113.njkm8v.rst b/Misc/NEWS.d/next/Library/2026-10-12-12-00-00.gh-issue-72113.njkm8v.rst deleted file mode 100644 index 9057f3246b3e2b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-12-12-00-00.gh-issue-72113.njkm8v.rst +++ /dev/null @@ -1,2 +0,0 @@ -Speed up creating a :mod:`ctypes` array from initializers, such as -``(c_int * 3)(1, 2, 3)``, by roughly 3x. diff --git a/Misc/NEWS.d/next/Library/2026-10-12-13-05-00.gh-issue-158501.n7Qp3k.rst b/Misc/NEWS.d/next/Library/2026-10-12-13-05-00.gh-issue-158501.n7Qp3k.rst deleted file mode 100644 index e47c5da38c8e86..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-12-13-05-00.gh-issue-158501.n7Qp3k.rst +++ /dev/null @@ -1,4 +0,0 @@ -:meth:`datetime.time.fromisoformat` and -:meth:`datetime.datetime.fromisoformat` now reject stray text between the -time and a UTC offset. The C implementation previously accepted strings -that the pure-Python implementation rejected. diff --git a/Misc/NEWS.d/next/Library/2026-10-12-18-21-06.gh-issue-159240.a1b2c3.rst b/Misc/NEWS.d/next/Library/2026-10-12-18-21-06.gh-issue-159240.a1b2c3.rst deleted file mode 100644 index 85439b77aaa8da..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-12-18-21-06.gh-issue-159240.a1b2c3.rst +++ /dev/null @@ -1,6 +0,0 @@ -Deprecate, and schedule for removal in Python 3.20, the ``__author__``, -``__credits__``, ``__copyright__`` and ``__date__`` attributes of :mod:`bz2`, -:mod:`concurrent.futures`, -:mod:`inspect`, :mod:`io`, :mod:`json`, :mod:`logging`, :mod:`optparse`, -:mod:`platform`, :mod:`pydoc`, :mod:`tarfile`, :mod:`tkinter.ttk`, -:mod:`tokenize` and :mod:`uuid`. diff --git a/Misc/NEWS.d/next/Library/2026-10-12-21-20-00.gh-issue-159225.T8jlor.rst b/Misc/NEWS.d/next/Library/2026-10-12-21-20-00.gh-issue-159225.T8jlor.rst deleted file mode 100644 index a9ee0e6a7eb97d..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-12-21-20-00.gh-issue-159225.T8jlor.rst +++ /dev/null @@ -1 +0,0 @@ -Prevent ``input`` calls in PyREPL from modifying readline history. diff --git a/Misc/NEWS.d/next/Library/2026-10-13-11-54-07.gh-issue-159301.Kq3Vx8.rst b/Misc/NEWS.d/next/Library/2026-10-13-11-54-07.gh-issue-159301.Kq3Vx8.rst new file mode 100644 index 00000000000000..067d1d5eac553c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-13-11-54-07.gh-issue-159301.Kq3Vx8.rst @@ -0,0 +1,3 @@ +Add symbolic constants for Tk option values which were missing in +:mod:`!tkinter.constants`, such as ``READONLY``, ``TABULAR``, +``DETERMINATE``, ``MENUBAR`` and the names of the themed widget states. diff --git a/Misc/NEWS.d/next/Library/2026-10-13-12-00-00.gh-issue-159304.mFbYq2.rst b/Misc/NEWS.d/next/Library/2026-10-13-12-00-00.gh-issue-159304.mFbYq2.rst deleted file mode 100644 index 8f5b61efe03959..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-13-12-00-00.gh-issue-159304.mFbYq2.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :exc:`AttributeError` in :meth:`email.message.EmailMessage.get_body` when a -nested ``multipart`` part is malformed and its payload is a string. diff --git a/Misc/NEWS.d/next/Library/2026-10-13-19-10-00.gh-issue-159322.hP6k3A.rst b/Misc/NEWS.d/next/Library/2026-10-13-19-10-00.gh-issue-159322.hP6k3A.rst deleted file mode 100644 index f9c3d1a98acc35..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-13-19-10-00.gh-issue-159322.hP6k3A.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :mod:`tarfile` to NUL-terminate user and group names in PAX archive -headers and use extended headers for names longer than 31 characters. diff --git a/Misc/NEWS.d/next/Library/2026-10-14-08-50-00.gh-issue-159398.k2Xd7q.rst b/Misc/NEWS.d/next/Library/2026-10-14-08-50-00.gh-issue-159398.k2Xd7q.rst deleted file mode 100644 index 65e10f4ba79284..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-14-08-50-00.gh-issue-159398.k2Xd7q.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix the sampling profiler crashing when a string read from the target process -contains code points above ``U+10FFFF``. diff --git a/Misc/NEWS.d/next/Library/2026-10-14-10-26-10.gh-issue-159387.xMbQbB.rst b/Misc/NEWS.d/next/Library/2026-10-14-10-26-10.gh-issue-159387.xMbQbB.rst deleted file mode 100644 index fddef6e097ba5c..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-14-10-26-10.gh-issue-159387.xMbQbB.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :mod:`ssl` reporting TLS 1.0, 1.1, and 1.2 as unavailable when built -with OpenSSL 4.0, despite support for these protocol versions. diff --git a/Misc/NEWS.d/next/Library/2026-10-14-11-41-00.gh-issue-159430.ZEW-mx.rst b/Misc/NEWS.d/next/Library/2026-10-14-11-41-00.gh-issue-159430.ZEW-mx.rst deleted file mode 100644 index 5b9041891431c0..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-14-11-41-00.gh-issue-159430.ZEW-mx.rst +++ /dev/null @@ -1,2 +0,0 @@ -Report the correct exception if looking up ``__name__`` raises an exception -when calling :func:`repr` on a :class:`weakref.ref`. diff --git a/Misc/NEWS.d/next/Library/2026-10-14-14-47-05.gh-issue-81954.KvM0gY.rst b/Misc/NEWS.d/next/Library/2026-10-14-14-47-05.gh-issue-81954.KvM0gY.rst deleted file mode 100644 index 5ced3b94d223b2..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-14-14-47-05.gh-issue-81954.KvM0gY.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :class:`zipfile.ZipFile` not being closed at interpreter shutdown when -emitting its :exc:`ResourceWarning` failed with an :exc:`ImportError`. diff --git a/Misc/NEWS.d/next/Library/2026-10-15-13-44-03.gh-issue-159465.Ph-m8w.rst b/Misc/NEWS.d/next/Library/2026-10-15-13-44-03.gh-issue-159465.Ph-m8w.rst deleted file mode 100644 index 21199b550f9a54..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-15-13-44-03.gh-issue-159465.Ph-m8w.rst +++ /dev/null @@ -1,2 +0,0 @@ -Improve incomplete input detection in :mod:`codeop` without an extra parser -pass, and preserve syntax and compiler errors. diff --git a/Misc/NEWS.d/next/Library/2026-10-15-18-38-22.gh-issue-159500.OjAQ8W.rst b/Misc/NEWS.d/next/Library/2026-10-15-18-38-22.gh-issue-159500.OjAQ8W.rst deleted file mode 100644 index 2cfaec1e5999a6..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-15-18-38-22.gh-issue-159500.OjAQ8W.rst +++ /dev/null @@ -1,4 +0,0 @@ -Speed up :meth:`asyncio.StreamReader.readline` and -:meth:`asyncio.StreamReader.readuntil` when the data is already buffered, -and stop :meth:`~asyncio.StreamReader.readuntil` with a tuple of separators from -scanning the whole buffer on every call. diff --git a/Misc/NEWS.d/next/Library/2026-10-16-01-56-15.gh-issue-159503.WgYiow.rst b/Misc/NEWS.d/next/Library/2026-10-16-01-56-15.gh-issue-159503.WgYiow.rst deleted file mode 100644 index 158968d0c2a1ce..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-16-01-56-15.gh-issue-159503.WgYiow.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix quadratic copying in :mod:`asyncio` selector socket transports on partial -sends. A :class:`bytearray` passed to :meth:`~asyncio.WriteTransport.write` -now stays locked against resizing until it has been sent. diff --git a/Misc/NEWS.d/next/Library/2026-10-16-10-25-22.gh-issue-159458.q02EnY.rst b/Misc/NEWS.d/next/Library/2026-10-16-10-25-22.gh-issue-159458.q02EnY.rst deleted file mode 100644 index 6565f4265b5877..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-16-10-25-22.gh-issue-159458.q02EnY.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a ThreadSanitizer data race on the static cache in ``normalize_century()`` of the datetime module under the free-threaded build. diff --git a/Misc/NEWS.d/next/Library/2026-10-16-13-00-00.gh-issue-159435.OMNI.rst b/Misc/NEWS.d/next/Library/2026-10-16-13-00-00.gh-issue-159435.OMNI.rst deleted file mode 100644 index 3f4c71c2c2e0a6..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-16-13-00-00.gh-issue-159435.OMNI.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix an offset calculation in :mod:`tarfile` when a PAX header with a ``size`` -record is followed by a GNU long-name header. Such archives could duplicate or -skip members while being read. diff --git a/Misc/NEWS.d/next/Library/2026-10-16-18-23-03.gh-issue-159558.ctRelk.rst b/Misc/NEWS.d/next/Library/2026-10-16-18-23-03.gh-issue-159558.ctRelk.rst deleted file mode 100644 index a8b8570365f3f4..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-16-18-23-03.gh-issue-159558.ctRelk.rst +++ /dev/null @@ -1,4 +0,0 @@ -Speed up assignment to the ``value`` attribute of simple :mod:`ctypes` types, to -the ``contents`` attribute of :mod:`ctypes` pointers and to :mod:`ctypes` -structure fields, array items and pointer items on the free-threaded build by -not re-entering the critical section already held on the object. diff --git a/Misc/NEWS.d/next/Library/2026-10-17-15-22-16.gh-issue-159620.Ynsu5L.rst b/Misc/NEWS.d/next/Library/2026-10-17-15-22-16.gh-issue-159620.Ynsu5L.rst deleted file mode 100644 index 2cbc3461cfaa7d..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-17-15-22-16.gh-issue-159620.Ynsu5L.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix ``python -m asyncio ps``, ``pstree`` and async-aware -:mod:`profiling.sampling` stacks stopping at asynchronous generators. -Patch by Timofei Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-10-17-19-41-12.gh-issue-157210.o9hycSeJ.rst b/Misc/NEWS.d/next/Library/2026-10-17-19-41-12.gh-issue-157210.o9hycSeJ.rst deleted file mode 100644 index 0c399257b497ff..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-17-19-41-12.gh-issue-157210.o9hycSeJ.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix entering a combining character in the lower-right cell of -:class:`curses.textpad.Textbox`: -it no longer takes a cell of its own, -pushing its base character off the window. -In insert mode, a combining character no longer shifts the following text. diff --git a/Misc/NEWS.d/next/Library/2026-10-18-08-30-10.gh-issue-155976.feSum4_j.rst b/Misc/NEWS.d/next/Library/2026-10-18-08-30-10.gh-issue-155976.feSum4_j.rst deleted file mode 100644 index 64aa76725de723..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-18-08-30-10.gh-issue-155976.feSum4_j.rst +++ /dev/null @@ -1,3 +0,0 @@ -:func:`curses.set_term` and :meth:`curses.screen.use` now update -:data:`curses.LINES`, :data:`curses.COLS`, :data:`curses.COLORS` -and :data:`curses.COLOR_PAIRS` for the current screen. diff --git a/Misc/NEWS.d/next/Library/2026-10-18-12-20-00.gh-issue-159465.partial-escapes.rst b/Misc/NEWS.d/next/Library/2026-10-18-12-20-00.gh-issue-159465.partial-escapes.rst deleted file mode 100644 index ecddc0f540761d..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-18-12-20-00.gh-issue-159465.partial-escapes.rst +++ /dev/null @@ -1,2 +0,0 @@ -Preserve :exc:`SyntaxError` for malformed escapes in unfinished f-strings and -t-strings passed to :func:`codeop.compile_command`. diff --git a/Misc/NEWS.d/next/Library/2026-10-18-16-09-24.gh-issue-159684.VHVYjVuE.rst b/Misc/NEWS.d/next/Library/2026-10-18-16-09-24.gh-issue-159684.VHVYjVuE.rst deleted file mode 100644 index 43f648af4784d9..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-18-16-09-24.gh-issue-159684.VHVYjVuE.rst +++ /dev/null @@ -1,3 +0,0 @@ -Update :mod:`configparser` and its tests to modern linter standards: -printf-style and :meth:`str.format` call sites are now f-strings, and a -handful of constructs have simpler spellings. No behavior change. diff --git a/Misc/NEWS.d/next/Library/2026-10-18-19-18-20.gh-issue-159645.IOXcpT.rst b/Misc/NEWS.d/next/Library/2026-10-18-19-18-20.gh-issue-159645.IOXcpT.rst new file mode 100644 index 00000000000000..fde97e6f131cb5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-18-19-18-20.gh-issue-159645.IOXcpT.rst @@ -0,0 +1,2 @@ +Fix :mod:`profiling.sampling` ``--async-aware`` profiles showing event loop +frames under tasks. Patch by Timofei Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-00-00-00.gh-issue-149015.bdb.rst b/Misc/NEWS.d/next/Library/2026-10-19-00-00-00.gh-issue-149015.bdb.rst deleted file mode 100644 index a7bcbc4b1aa322..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-00-00-00.gh-issue-149015.bdb.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :meth:`bdb.Bdb.clear_all_file_breaks` leaving breakpoints behind when -multiple breakpoints are set on the same line. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-12-00-00.gh-issue-159714.j59fix.rst b/Misc/NEWS.d/next/Library/2026-10-19-12-00-00.gh-issue-159714.j59fix.rst deleted file mode 100644 index 7b61ca79cbd2b5..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-12-00-00.gh-issue-159714.j59fix.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :class:`zoneinfo.ZoneInfo` interpreting the Julian day rule ``J59`` as -February 29 in leap years instead of February 28. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-12-00-00.gh-issue-78327.Qk3hzA.rst b/Misc/NEWS.d/next/Library/2026-10-19-12-00-00.gh-issue-78327.Qk3hzA.rst deleted file mode 100644 index b0d4107bcadf93..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-12-00-00.gh-issue-78327.Qk3hzA.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`ctypes` data types now set :meth:`~object.__hash__` to ``None``, so -:class:`collections.abc.Hashable` checks report them correctly. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-16-00-00.gh-issue-139122.bYtEs1.rst b/Misc/NEWS.d/next/Library/2026-10-19-16-00-00.gh-issue-139122.bYtEs1.rst deleted file mode 100644 index 256b36941aa910..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-16-00-00.gh-issue-139122.bYtEs1.rst +++ /dev/null @@ -1,3 +0,0 @@ -:class:`uuid.UUID` now consistently raises :exc:`TypeError` when the *bytes* -or *bytes_le* argument is not a :class:`bytes` object. Previously, some invalid -inputs raised :exc:`AssertionError` or :exc:`ValueError`. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-16-26-57.gh-issue-71990.K3FlJ9.rst b/Misc/NEWS.d/next/Library/2026-10-19-16-26-57.gh-issue-71990.K3FlJ9.rst deleted file mode 100644 index 87276db65054a0..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-16-26-57.gh-issue-71990.K3FlJ9.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`ctypes`: objects with an :attr:`!_as_parameter_` attribute are now -implicitly converted when passed for a :func:`~ctypes.POINTER` argument. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-16-49-09.gh-issue-159706.ushGrx.rst b/Misc/NEWS.d/next/Library/2026-10-19-16-49-09.gh-issue-159706.ushGrx.rst deleted file mode 100644 index ee90605ed0af2b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-16-49-09.gh-issue-159706.ushGrx.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix the pure-Python :class:`zoneinfo.ZoneInfo` setting ``fold=0`` in -:meth:`~datetime.tzinfo.fromutc` for times in the fold created by the only -transition of a zone with a single transition. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.a1c3c7.rst b/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.a1c3c7.rst deleted file mode 100644 index 2d723061b9369d..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.a1c3c7.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix resource leaks in :mod:`ssl` when certificate conversion or key log setup -fails. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst b/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst deleted file mode 100644 index d4a662b10d9f4f..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-19-33-35.gh-issue-159739.b5fbca.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a memory leak when Unicode normalization cannot grow its temporary buffer. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.08caa7.rst b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.08caa7.rst deleted file mode 100644 index 3574193b51f343..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.08caa7.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix incorrect :mod:`ctypes` scalar values after :func:`ctypes.resize` enlarges -the object. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.243aa8.rst b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.243aa8.rst deleted file mode 100644 index a9452184e7838f..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.243aa8.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid reading past the end of an encoding name when the ``surrogatepass`` -error handler receives a truncated UTF-16 or UTF-32 name. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.5a6e94.rst b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.5a6e94.rst deleted file mode 100644 index e7eac98810cf48..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.5a6e94.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid a crash when memory allocation fails while creating a -:class:`collections.OrderedDict` items iterator. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.a81270.rst b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.a81270.rst deleted file mode 100644 index 413fc0d7f18f68..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.a81270.rst +++ /dev/null @@ -1 +0,0 @@ -Fix :class:`io.BytesIO` reads into overlapping views of the same buffer. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.bc65da.rst b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.bc65da.rst deleted file mode 100644 index fd831b6907e777..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.bc65da.rst +++ /dev/null @@ -1,2 +0,0 @@ -Allow cyclic garbage collection to reclaim group records returned by -:mod:`grp`. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.ff088c.rst b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.ff088c.rst deleted file mode 100644 index af8b06fecbfc3f..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.ff088c.rst +++ /dev/null @@ -1 +0,0 @@ -Fix copying :class:`collections.deque` subclasses with a large maximum length. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-21-20-28.gh-issue-159786.fThehQ.rst b/Misc/NEWS.d/next/Library/2026-10-19-21-20-28.gh-issue-159786.fThehQ.rst deleted file mode 100644 index 3f901326c87f68..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-21-20-28.gh-issue-159786.fThehQ.rst +++ /dev/null @@ -1,6 +0,0 @@ -Fix the error handling of the HACL* routine used by :func:`hmac.new` and -:meth:`hmac.HMAC.update`. If the maximum input length of the underlying hash -function is exceeded, an :exc:`OverflowError` is now raised on all builds, -instead of crashing on DEBUG builds and being silently ignored otherwise. -Code correctness. Realistically this can only happen at implausible sizes. -Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-23-25-29.gh-issue-159811.P6yy0z.rst b/Misc/NEWS.d/next/Library/2026-10-19-23-25-29.gh-issue-159811.P6yy0z.rst deleted file mode 100644 index bcd76f209e5264..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-23-25-29.gh-issue-159811.P6yy0z.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash in :mod:`hmac` when the OpenSSL routine used by -:meth:`hmac.HMAC.update` or by :func:`hmac.new` fails on data larger -than 2048 bytes. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-10-19-23-25-29.gh-issue-159811.cMB4DO.rst b/Misc/NEWS.d/next/Library/2026-10-19-23-25-29.gh-issue-159811.cMB4DO.rst deleted file mode 100644 index f63cd928a6a33c..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-19-23-25-29.gh-issue-159811.cMB4DO.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash in :mod:`hashlib` when the OpenSSL routine used by -:meth:`hash.update() ` or by hash object constructors -fails on data larger than 2048 bytes. Patch by Bénédikt Tran. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.0c4ae9.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.0c4ae9.rst deleted file mode 100644 index 3392832f714adc..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.0c4ae9.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix reference cycles involving the types of grapheme iterators and segments -created by :func:`unicodedata.iter_graphemes`. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.1e4599.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.1e4599.rst deleted file mode 100644 index 3122ca29c7c49b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.1e4599.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix off-by-one output buffer checks in the fallback implementation of -``getnameinfo()``. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.3660a6.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.3660a6.rst deleted file mode 100644 index 56d11c767319cc..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.3660a6.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix multibyte encoders silently consuming a non-BMP character when looking up -a two-character mapping. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.4c9783.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.4c9783.rst deleted file mode 100644 index b65ac4cede106b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.4c9783.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :meth:`io.IOBase.readline` returning more bytes than requested when a -stream supports :meth:`!peek` and returns short reads. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.5d4269.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.5d4269.rst deleted file mode 100644 index 94ee3cf4790112..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.5d4269.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a double free if remote unwinder initialization runs out of memory. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.9ef89c.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.9ef89c.rst deleted file mode 100644 index 1afbadc1cf4e91..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.9ef89c.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix allocation error handling in the fallback implementation of -``getaddrinfo()``. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.eb8987.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.eb8987.rst deleted file mode 100644 index 11c88411bb9125..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.eb8987.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid undefined behavior when :func:`struct.pack_into` copies a bytearray into -overlapping output. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-12-05.gh-issue-159690.vZgLP6.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-12-05.gh-issue-159690.vZgLP6.rst deleted file mode 100644 index b94d79501b9abe..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-00-12-05.gh-issue-159690.vZgLP6.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :class:`logging.handlers.TimedRotatingFileHandler` permanently stopping -rollover after a rollover failed or its backup file already existed, for -example at a DST change. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-12-00-00.gh-issue-87027.PrEvNw.rst b/Misc/NEWS.d/next/Library/2026-10-20-12-00-00.gh-issue-87027.PrEvNw.rst new file mode 100644 index 00000000000000..504cd054ca464c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-20-12-00-00.gh-issue-87027.PrEvNw.rst @@ -0,0 +1,2 @@ +Add :meth:`~ipaddress.IPv4Network.prev_network` and +:meth:`~ipaddress.IPv6Network.prev_network`. Patch by Faisal Mahmood. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst b/Misc/NEWS.d/next/Library/2026-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst deleted file mode 100644 index e5b9b02a27cb97..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-12-30-00.gh-issue-159864.4FZ6MR.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash in :func:`syslog.openlog` under the free-threaded build when -:data:`sys.argv` is modified concurrently. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-12-49-32.gh-issue-159207.Lccezg.rst b/Misc/NEWS.d/next/Library/2026-10-20-12-49-32.gh-issue-159207.Lccezg.rst new file mode 100644 index 00000000000000..a96098fd211abc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-20-12-49-32.gh-issue-159207.Lccezg.rst @@ -0,0 +1,3 @@ +The :mod:`profiling.sampling` binary output format now stores samples in +independent blocks. This is a breaking format change: Python 3.16 does not +support reading profiles written in the previous format. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-16-00-00.gh-issue-159868.qLhYkP.rst b/Misc/NEWS.d/next/Library/2026-10-20-16-00-00.gh-issue-159868.qLhYkP.rst deleted file mode 100644 index fa664aa31d42da..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-16-00-00.gh-issue-159868.qLhYkP.rst +++ /dev/null @@ -1,3 +0,0 @@ -Allow :meth:`!collections.UserString.startswith` and -:meth:`!collections.UserString.endswith` to accept a -:class:`~collections.UserString` instance as the prefix or suffix argument. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst b/Misc/NEWS.d/next/Library/2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst deleted file mode 100644 index 6d9ac174282839..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-20-17-29-14.gh-issue-138641.FN6qM0.rst +++ /dev/null @@ -1 +0,0 @@ -Ignore :func:`pdb.set_trace` when called directly at the :mod:`pdb` prompt. diff --git a/Misc/NEWS.d/next/Library/2026-10-20-23-30-00.gh-issue-159973.Ks9nLm.rst b/Misc/NEWS.d/next/Library/2026-10-20-23-30-00.gh-issue-159973.Ks9nLm.rst new file mode 100644 index 00000000000000..506c71ca82bdba --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-20-23-30-00.gh-issue-159973.Ks9nLm.rst @@ -0,0 +1,3 @@ +Fix soft keyword highlighting after blank lines, comment-only lines, and +dedents in the :ref:`interactive shell `. Patch by Bartosz +Sławecki. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-15-10-00.gh-issue-160125.pdbpkg.rst b/Misc/NEWS.d/next/Library/2026-10-21-15-10-00.gh-issue-160125.pdbpkg.rst deleted file mode 100644 index 822d0dd3d79e91..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-15-10-00.gh-issue-160125.pdbpkg.rst +++ /dev/null @@ -1,3 +0,0 @@ -Support setting breakpoints with the :mod:`pdb` ``break`` and ``tbreak`` -commands using ``package.module.function`` names when the module has not been -imported in the current namespace. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-15-42-29.gh-issue-159875.p6j3yW.rst b/Misc/NEWS.d/next/Library/2026-10-21-15-42-29.gh-issue-159875.p6j3yW.rst deleted file mode 100644 index 824fa9567d7911..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-15-42-29.gh-issue-159875.p6j3yW.rst +++ /dev/null @@ -1,7 +0,0 @@ -Fix :exc:`AttributeError` from the methods of an :mod:`asyncio` SSL transport, -such as :meth:`~asyncio.WriteTransport.write` and -:meth:`~asyncio.BaseTransport.set_protocol`, when the transport is closed after -its connection was lost. This also affected the fallback of -:meth:`loop.sendfile() ` over SSL when the peer went -away mid-transfer. The SSL context is still released once the connection is -lost. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-19-38-33.gh-issue-89721.uMpxRw.rst b/Misc/NEWS.d/next/Library/2026-10-21-19-38-33.gh-issue-89721.uMpxRw.rst deleted file mode 100644 index 519cf44beb54c9..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-19-38-33.gh-issue-89721.uMpxRw.rst +++ /dev/null @@ -1,3 +0,0 @@ -Add ``copy_dir_stat`` parameter to :func:`shutil.copytree`: set it to -false to skip copying permissions and times of directories with -:func:`shutil.copystat`, e.g. when copying a read-only tree. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-19-40-00.gh-issue-100609.THXgRC.rst b/Misc/NEWS.d/next/Library/2026-10-21-19-40-00.gh-issue-100609.THXgRC.rst deleted file mode 100644 index 4a793a0fdcd91e..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-19-40-00.gh-issue-100609.THXgRC.rst +++ /dev/null @@ -1,5 +0,0 @@ -``str()`` of a :class:`zipfile.Path` whose underlying zipfile has no -filename, such as one constructed from an :class:`io.BytesIO`, now renders -that filename as ``:zipfile:`` instead of raising :exc:`TypeError`. The root -of a zipfile is also no longer rendered with a trailing slash. Synchronized -with :pypi:`zipp` 3.23.1. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-19-55-00.gh-issue-159739.sque01.rst b/Misc/NEWS.d/next/Library/2026-10-21-19-55-00.gh-issue-159739.sque01.rst deleted file mode 100644 index 120caf1e27c6cb..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-19-55-00.gh-issue-159739.sque01.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid a crash when a :class:`queue.SimpleQueue` subclass finalizer adds an -item after construction fails. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-19-55-00.gh-issue-159739.tloc01.rst b/Misc/NEWS.d/next/Library/2026-10-21-19-55-00.gh-issue-159739.tloc01.rst deleted file mode 100644 index 9641d11deb84d6..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-19-55-00.gh-issue-159739.tloc01.rst +++ /dev/null @@ -1,2 +0,0 @@ -Avoid a crash when a :class:`threading.local` subclass finalizer accesses -attributes after construction fails. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-20-05-00.gh-issue-160104.BUAL5C.rst b/Misc/NEWS.d/next/Library/2026-10-21-20-05-00.gh-issue-160104.BUAL5C.rst deleted file mode 100644 index bab414b247ff25..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-20-05-00.gh-issue-160104.BUAL5C.rst +++ /dev/null @@ -1,6 +0,0 @@ -Synchronized :class:`zipfile.Path` with :pypi:`zipp` 4.1.2. -:meth:`zipfile.Path.iterdir` now raises :exc:`NotADirectoryError` instead of -:exc:`ValueError` when the path does not reference a directory. Character -classes in :meth:`!zipfile.Path.glob` and :meth:`!zipfile.Path.rglob` patterns -now follow shell-style semantics, treating a leading ``!`` as negation and -``^`` as a literal, and no longer match path separators. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.JIqZRY.rst b/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.JIqZRY.rst deleted file mode 100644 index fa000802ef713b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.JIqZRY.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a possible crash when a list of :mod:`sqlite3` parameters is modified concurrently on a free-threaded build. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.b4ueJ7.rst b/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.b4ueJ7.rst deleted file mode 100644 index 6230801bbc0927..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.b4ueJ7.rst +++ /dev/null @@ -1 +0,0 @@ -Detect recursive ``_as_parameter_`` chains for :mod:`ctypes` calls without argument types. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.ghGctt.rst b/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.ghGctt.rst deleted file mode 100644 index dd9dd5dce88f43..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.ghGctt.rst +++ /dev/null @@ -1 +0,0 @@ -Fix :class:`contextvars.ContextVar` returning a stale value after a reentrant finalizer changes it during :meth:`~contextvars.ContextVar.reset`. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.k8uuGe.rst b/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.k8uuGe.rst deleted file mode 100644 index 24dd0b7c7776e3..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.k8uuGe.rst +++ /dev/null @@ -1,3 +0,0 @@ -Raise :exc:`TypeError` if a string subclass returns a non-string during newline -translation in :class:`io.TextIOWrapper`. Recheck the stream state if the -replacement callback closes, detaches or reinitializes the stream. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.rVWF2a.rst b/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.rVWF2a.rst deleted file mode 100644 index f2e13ee926eaa1..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.rVWF2a.rst +++ /dev/null @@ -1 +0,0 @@ -Fix the ``truncate`` method on :class:`io.BytesIO` when converting its size argument closes the stream or exports its buffer. diff --git a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.yBc6Ei.rst b/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.yBc6Ei.rst deleted file mode 100644 index e299599e9763f0..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-21-21-26-10.gh-issue-159739.yBc6Ei.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a possible crash in :class:`abc.ABCMeta` instance checks when a subclass-checking callback changes the instance’s class. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-00-29-50.gh-issue-160176.tkNst1.rst b/Misc/NEWS.d/next/Library/2026-10-22-00-29-50.gh-issue-160176.tkNst1.rst deleted file mode 100644 index 5f11a8237ba81a..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-00-29-50.gh-issue-160176.tkNst1.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix a crash in :mod:`tkinter` when a deeply nested or recursive list or -tuple is passed to Tcl, or a deeply nested Tcl list is converted to Python. -These now raise :exc:`ValueError` (beyond 1000 levels) and -:exc:`RecursionError` respectively. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-00-29-55.gh-issue-160185.BPz40E.rst b/Misc/NEWS.d/next/Library/2026-10-22-00-29-55.gh-issue-160185.BPz40E.rst deleted file mode 100644 index a9d7aef03214a3..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-00-29-55.gh-issue-160185.BPz40E.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix :meth:`codecs.StreamWriter.reset` of the stateful CJK codecs (``hz`` and -the ``iso2022_*`` family) not writing the sequence that returns the output to -ASCII. ``seek(0)`` of :class:`codecs.StreamWriter`, -:class:`codecs.StreamReaderWriter` and :class:`codecs.StreamRecoder` now -resets the writer before moving the stream position. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-00-30-04.gh-issue-160187.9dcj2B.rst b/Misc/NEWS.d/next/Library/2026-10-22-00-30-04.gh-issue-160187.9dcj2B.rst deleted file mode 100644 index 74ee6c1fa7bbb9..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-00-30-04.gh-issue-160187.9dcj2B.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix the ISO-2022 decoders (``iso2022_jp``, ``iso2022_kr`` and related) -accepting escape sequences that designate a character set to the wrong code -element (G0, G1 or G2). Such sequences now raise :exc:`UnicodeDecodeError` -instead of producing wrong text or :exc:`RuntimeError`. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-00-32-51.gh-issue-160181.nnEeg1.rst b/Misc/NEWS.d/next/Library/2026-10-22-00-32-51.gh-issue-160181.nnEeg1.rst deleted file mode 100644 index 5644b99cce1720..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-00-32-51.gh-issue-160181.nnEeg1.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :meth:`io.BytesIO.getvalue` sometimes returning a :class:`bytes` object -with the hash of earlier contents after the buffer was written to, which made -:class:`dict` and :class:`set` lookups with it fail. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-00-33-42.gh-issue-160171.11l4om.rst b/Misc/NEWS.d/next/Library/2026-10-22-00-33-42.gh-issue-160171.11l4om.rst deleted file mode 100644 index 73c51e66ea26cf..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-00-33-42.gh-issue-160171.11l4om.rst +++ /dev/null @@ -1,4 +0,0 @@ -On the server side, :meth:`ssl.SSLSocket.get_unverified_chain` now returns -the client's certificate when the session was resumed from a session ticket, -instead of an empty list, and returns an empty list instead of raising -:exc:`AttributeError` when the client sent no certificate. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.a8bf30.rst b/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.a8bf30.rst deleted file mode 100644 index 7052cdb6bd9682..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.a8bf30.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix clearing a :class:`pickle.Unpickler` memo so that the unpickler can be -reused for independent records. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.b4c371.rst b/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.b4c371.rst deleted file mode 100644 index a3682d0cea74dc..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-03-55-33.gh-issue-159739.b4c371.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a crash when a :mod:`ctypes` structure subclass cannot be created due -to an allocation failure. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-19-56.gh-issue-161090.pxZ1IA.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-19-56.gh-issue-161090.pxZ1IA.rst deleted file mode 100644 index 40b506b39cc433..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-19-56.gh-issue-161090.pxZ1IA.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix the ``punycode`` codec accepting a delimiter (``-``) that is not preceded -by any basic code point. Such input, for example ``b'-80a1acny'``, now raises -:exc:`UnicodeDecodeError`, as required by :rfc:`3492`. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161092.OtC5l6.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161092.OtC5l6.rst deleted file mode 100644 index abdc68c7f7a5db..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161092.OtC5l6.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix the ``uu`` codec decoder taking any line that starts with ``begin``, such -as ``beginning of the report``, as the header of the encoded data. Like the -:mod:`email` package, it now requires ``begin`` followed by an octal mode and -skips other lines as leading text. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161102.gqDOIM7X.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161102.gqDOIM7X.rst deleted file mode 100644 index e3089a0560bcbf..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161102.gqDOIM7X.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix the incremental decoder of the :mod:`idna ` codec -decoding some dots twice when its input starts with empty labels, for example -``b".example.com"``. Reading a run of dots through :class:`io.TextIOWrapper` -no longer produces quadratically more text than the input holds. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-20-06.gh-issue-161100.pLgnlA.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-20-06.gh-issue-161100.pLgnlA.rst deleted file mode 100644 index b23de8a22ca968..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-20-06.gh-issue-161100.pLgnlA.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :attr:`!ipaddress.IPv6Interface.is_unspecified` returning ``False`` for -the IPv4-mapped address ``::ffff:0.0.0.0``. It now gives the same result as -:attr:`ipaddress.IPv6Address.is_unspecified`. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-20-11.gh-issue-161110.miKrSQ.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-20-11.gh-issue-161110.miKrSQ.rst deleted file mode 100644 index e71214d60f1cd1..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-20-11.gh-issue-161110.miKrSQ.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :meth:`~xml.dom.Document.getElementById` in :mod:`xml.dom.minidom` so -that it returns the first element in document order when several elements -have the same ID. Previously, the result could differ between calls on an -unmodified document. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-20-18.gh-issue-161108.xOVsRA.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-20-18.gh-issue-161108.xOVsRA.rst deleted file mode 100644 index d58214c823a552..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-20-18.gh-issue-161108.xOVsRA.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix quadratic time complexity of :meth:`!clear` and :meth:`!popitem` on -:class:`configparser.ConfigParser` objects and their section proxies. -Clearing a section no longer fails part way with -:exc:`~configparser.InterpolationError` on a value it removes. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-20-27.gh-issue-161098.voMVnd.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-20-27.gh-issue-161098.voMVnd.rst deleted file mode 100644 index 73849a0136aafc..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-20-27.gh-issue-161098.voMVnd.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :class:`multiprocessing.shared_memory.ShareableList` leaving its newly -created shared memory block behind when a value cannot be stored, such as an -integer outside the signed 64-bit range. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-20-44.gh-issue-161106.t0ux5q.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-20-44.gh-issue-161106.t0ux5q.rst deleted file mode 100644 index 78ab254e8fd353..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-20-44.gh-issue-161106.t0ux5q.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :func:`plistlib.dump` and :func:`plistlib.dumps` with -:data:`~plistlib.FMT_BINARY` holding on to memory proportional to the size of -large arrays and dictionaries after the call returns. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-20-52.gh-issue-161096.vkhfyy.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-20-52.gh-issue-161096.vkhfyy.rst deleted file mode 100644 index bae5465f42c6a1..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-20-52.gh-issue-161096.vkhfyy.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :meth:`zoneinfo.ZoneInfo.from_file` accepting TZif data with a negative or -oversized abbreviation block, which could use hundreds of times the file size -in memory; it now raises :exc:`ValueError`. Abbreviation indexes above 127 now -also give the correct time zone abbreviation. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-21-00.gh-issue-161094.MDjCNm.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-21-00.gh-issue-161094.MDjCNm.rst deleted file mode 100644 index 716da00504cc59..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-21-00.gh-issue-161094.MDjCNm.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :func:`zipfile.is_zipfile` raising :exc:`ValueError` instead of returning -``False`` for a file-like object, such as :class:`io.BytesIO`, whose end of -central directory record places the central directory before the start of the -file. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-23-04.gh-issue-161104.rvNPjJ.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-23-04.gh-issue-161104.rvNPjJ.rst new file mode 100644 index 00000000000000..acae526043a297 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-22-05-23-04.gh-issue-161104.rvNPjJ.rst @@ -0,0 +1,4 @@ +Fix :meth:`http.client.HTTPConnection.set_tunnel` accepting a tunnel host +containing a non-ASCII character that IDNA encoding maps to a space, such as +U+00A0 NO-BREAK SPACE. Connecting now raises :exc:`ValueError`, as it already +does for an ASCII space. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-23-40.gh-issue-161088._-HEWz.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-23-40.gh-issue-161088._-HEWz.rst deleted file mode 100644 index cd8820baffa08f..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-23-40.gh-issue-161088._-HEWz.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix the :mod:`gzip` command-line interface writing through a symbolic link -at the output file name. Like :program:`gzip`, it now replaces an existing -output file with a new one that has the permission bits of the input file. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-24-21.gh-issue-161112.BFrZRw.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-24-21.gh-issue-161112.BFrZRw.rst deleted file mode 100644 index 73dc245dcb76dd..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-24-21.gh-issue-161112.BFrZRw.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :func:`shutil.make_archive` adding the zip archive being created to itself -when it is written inside the directory being archived or a symbolic link in -that directory refers to it. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst b/Misc/NEWS.d/next/Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst deleted file mode 100644 index d94fadf8f8a652..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-05-24-34.gh-issue-161086.FGPjqA.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix the :mod:`gzip` command-line interface ignoring :option:`gzip --fast` -and :option:`gzip --best` when compressing files given as arguments. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-08-04-26.gh-issue-160297.hL4kQz.rst b/Misc/NEWS.d/next/Library/2026-10-22-08-04-26.gh-issue-160297.hL4kQz.rst deleted file mode 100644 index 1419910948fe15..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-08-04-26.gh-issue-160297.hL4kQz.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :meth:`tarfile.TarFile.extractall` and :meth:`tarfile.TarFile.extract` -changing the mode, modification time and owner of a file outside the -destination directory when a hard link member is extracted as a copy of a -symbolic link member, for example with the ``'tar'`` filter. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-08-06-39.gh-issue-160513.q7RkZd.rst b/Misc/NEWS.d/next/Library/2026-10-22-08-06-39.gh-issue-160513.q7RkZd.rst deleted file mode 100644 index 1b020723366cd6..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-08-06-39.gh-issue-160513.q7RkZd.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix :meth:`asyncio.loop.start_tls` and :meth:`asyncio.StreamWriter.start_tls` -not calling the protocol's :meth:`~asyncio.BaseProtocol.connection_lost` -when the connection is lost, the handshake times out or the call is -cancelled during the TLS handshake. :meth:`asyncio.StreamWriter.wait_closed` -no longer waits forever after such a failure. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-08-09-29.gh-issue-160406.q7Rk2d.rst b/Misc/NEWS.d/next/Library/2026-10-22-08-09-29.gh-issue-160406.q7Rk2d.rst deleted file mode 100644 index 42f1774b829eee..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-08-09-29.gh-issue-160406.q7Rk2d.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix in-place filtering in :mod:`fileinput` deleting the file when it cannot -be opened or the output file cannot be created: the backup file is now kept. -The output file is now created exclusively, so :exc:`FileExistsError` is -raised if a file or symbolic link appears under the name of the input file. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-08-15-56.gh-issue-160324.sIoRlz.rst b/Misc/NEWS.d/next/Library/2026-10-22-08-15-56.gh-issue-160324.sIoRlz.rst deleted file mode 100644 index d54069128e2c0e..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-08-15-56.gh-issue-160324.sIoRlz.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a crash or garbage data when an :class:`io.StringIO` object is used again -after one of its methods failed with :exc:`MemoryError`. The object now keeps -its contents. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-08-17-51.gh-issue-160316.sRe9pQ.rst b/Misc/NEWS.d/next/Library/2026-10-22-08-17-51.gh-issue-160316.sRe9pQ.rst deleted file mode 100644 index 0e52137bde01ed..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-08-17-51.gh-issue-160316.sRe9pQ.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix :meth:`re.Pattern.match`, :meth:`re.Pattern.prefixmatch`, -:meth:`re.Pattern.fullmatch` and the ``match()`` and ``prefixmatch()`` methods -of scanner objects returning an empty match, or reading past the end of the -string, when *pos* is greater than *endpos*. They now return ``None``, as -documented. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-08-18-31.gh-issue-160522.cJkSt8.rst b/Misc/NEWS.d/next/Library/2026-10-22-08-18-31.gh-issue-160522.cJkSt8.rst deleted file mode 100644 index ae93269f871476..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-08-18-31.gh-issue-160522.cJkSt8.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix the incremental encoders and stream writers of the stateful CJK codecs -(``iso2022_*`` and ``hz``) keeping a changed shift state after an -:meth:`!encode` or :meth:`!write` call failed. The next call could omit the -escape sequence, writing multibyte characters in ASCII mode. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-08-34-22.gh-issue-160310.q7Zm2K.rst b/Misc/NEWS.d/next/Library/2026-10-22-08-34-22.gh-issue-160310.q7Zm2K.rst deleted file mode 100644 index 82e62d73bb32ec..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-08-34-22.gh-issue-160310.q7Zm2K.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix quadratic time complexity of :func:`gzip.decompress`, -:func:`bz2.decompress`, :func:`lzma.decompress` and -:func:`compression.zstd.decompress` on data consisting of many concatenated -members. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-16-18-42.gh-issue-159739.TioR3a.rst b/Misc/NEWS.d/next/Library/2026-10-22-16-18-42.gh-issue-159739.TioR3a.rst deleted file mode 100644 index 69f90a8d30e1dd..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-16-18-42.gh-issue-159739.TioR3a.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix crashes in :class:`io.TextIOWrapper` and -:class:`io.IncrementalNewlineDecoder` when a callback changes the active -codec or decoded buffer. :class:`io.StringIO` now raises :exc:`ValueError` -if converting the size passed to ``truncate()`` closes the stream. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-16-23-01.gh-issue-159739.CjkR5a.rst b/Misc/NEWS.d/next/Library/2026-10-22-16-23-01.gh-issue-159739.CjkR5a.rst deleted file mode 100644 index 78a3ecc46247c4..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-16-23-01.gh-issue-159739.CjkR5a.rst +++ /dev/null @@ -1,6 +0,0 @@ -Fix the ``hz`` codec sometimes encoding ``~`` as a single ``~`` in text that -mixes ASCII and Chinese characters, and the incremental ``iso2022_*`` codecs -mishandling an escape sequence split across calls or keeping the previous -character set after :meth:`!reset`. Also fix possible crashes in the CJK -incremental and stream codecs when their ``errors`` attribute or their class's -``codec`` attribute is replaced while they are in use. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-16-23-01.gh-issue-159739.XmlH3a.rst b/Misc/NEWS.d/next/Library/2026-10-22-16-23-01.gh-issue-159739.XmlH3a.rst deleted file mode 100644 index bda00a7f01f276..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-16-23-01.gh-issue-159739.XmlH3a.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix crashes in :mod:`xml.parsers.expat` when a character data callback -disables buffering or clears another handler during a flush, or when interning -an attribute name changes :attr:`~xml.parsers.expat.xmlparser.ordered_attributes`. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-16-30-00.gh-issue-159739.mFWntK.rst b/Misc/NEWS.d/next/Library/2026-10-22-16-30-00.gh-issue-159739.mFWntK.rst deleted file mode 100644 index 73e7f5e0c1c15f..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-16-30-00.gh-issue-159739.mFWntK.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix crashes in :mod:`profiling.tracing` when a custom timer reinitializes the -profiler or its result clears or disables the profiler during conversion or -cleanup. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-16-30-00.gh-issue-159739.tRq417.rst b/Misc/NEWS.d/next/Library/2026-10-22-16-30-00.gh-issue-159739.tRq417.rst deleted file mode 100644 index 1e174fbfc25959..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-16-30-00.gh-issue-159739.tRq417.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix crashes in :mod:`sqlite3` when a row or text factory is replaced during a -query, when cursor metadata is read during execution in a free-threaded build, -or when allocating cursor metadata fails. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160314.q7RkZd.rst b/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160314.q7RkZd.rst deleted file mode 100644 index 3694e255546f68..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160314.q7RkZd.rst +++ /dev/null @@ -1,4 +0,0 @@ -Raise an error in :class:`lzma.LZMAFile`, :func:`lzma.decompress`, -:class:`bz2.BZ2File` and :func:`bz2.decompress` for a damaged ``.xz`` or bzip2 -stream that follows another stream, instead of silently ignoring it and -everything after it as trailing data. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160326.nAn7Tq.rst b/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160326.nAn7Tq.rst deleted file mode 100644 index 4349a7380c377d..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160326.nAn7Tq.rst +++ /dev/null @@ -1,5 +0,0 @@ -Raise :exc:`ValueError` for a NaN *timeout* in -:meth:`threading.Condition.wait` and the blocking calls built on it, such as -:meth:`threading.Event.wait`, :meth:`threading.Semaphore.acquire` and -:meth:`queue.Queue.get`, instead of returning at once or looping without ever -timing out. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160520.sSdTmo.rst b/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160520.sSdTmo.rst deleted file mode 100644 index 241087806c6209..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-18-30-00.gh-issue-160520.sSdTmo.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :mod:`asyncio` SSL transports not applying *ssl_shutdown_timeout* when -the peer has sent EOF but does not read the data still queued for it. -Closing such a connection could wait indefinitely; it is now aborted with -:exc:`TimeoutError` once the timeout expires. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-21-19-51.gh-issue-160397.E-RwgUgm.rst b/Misc/NEWS.d/next/Library/2026-10-22-21-19-51.gh-issue-160397.E-RwgUgm.rst new file mode 100644 index 00000000000000..bd880cfe1eaff3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-22-21-19-51.gh-issue-160397.E-RwgUgm.rst @@ -0,0 +1,5 @@ +The C implementation of :mod:`warnings` no longer takes the warnings lock +when emitting a warning, so user code such as a custom +:func:`~warnings.showwarning` no longer runs with the lock held. On the +:term:`free-threaded build`, the filter search and the registry updates use +per-object critical sections instead. diff --git a/Misc/NEWS.d/next/Library/2026-10-22-22-51-40.gh-issue-160386.wYyboz.rst b/Misc/NEWS.d/next/Library/2026-10-22-22-51-40.gh-issue-160386.wYyboz.rst deleted file mode 100644 index 51da0478ee6455..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-22-22-51-40.gh-issue-160386.wYyboz.rst +++ /dev/null @@ -1,11 +0,0 @@ -Applied changes to ``importlib.metadata`` from `importlib_metadata 9.1 -`_: -distribution discovery now prefers the greater of several metadata -directories supplied by one path, so stale metadata left by an unclean -upgrade no longer shadows the version in use; a missing required metadata -field now raises a ``KeyError`` naming both the field and the distribution; -``EntryPoint`` value validation is deferred until the value is used, so one -malformed entry point no longer prevents others from being selected; -``importlib.metadata.diagnose`` no longer crashes on a metadata directory -with no metadata file or no declared name; and ``SimplePath.joinpath`` and -``SimplePath.__truediv__`` take ``other`` positional-only. diff --git a/Misc/NEWS.d/next/Library/2026-10-23-21-52-40.gh-issue-60299.Af6IzL.rst b/Misc/NEWS.d/next/Library/2026-10-23-21-52-40.gh-issue-60299.Af6IzL.rst deleted file mode 100644 index c23e516e131f9c..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-23-21-52-40.gh-issue-60299.Af6IzL.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :class:`urllib.request.ProxyDigestAuthHandler` failing to authenticate. It -looked up credentials using the target request URL instead of the proxy's -authority, so passwords registered via ``add_password()`` against the proxy -never matched. Patch by Senthil Kumaran. diff --git a/Misc/NEWS.d/next/Library/2026-10-23-22-34-55.gh-issue-160535.Zk3dGc.rst b/Misc/NEWS.d/next/Library/2026-10-23-22-34-55.gh-issue-160535.Zk3dGc.rst deleted file mode 100644 index ea2082716d64ac..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-23-22-34-55.gh-issue-160535.Zk3dGc.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix a crash in :mod:`compression.zstd` when the garbage collector frees a -:class:`~compression.zstd.ZstdCompressor` that uses worker threads together -with its :class:`~compression.zstd.ZstdDict` before the compression of -already submitted data has finished. diff --git a/Misc/NEWS.d/next/Library/2026-10-23-23-21-21.gh-issue-160549.q7ZdMt.rst b/Misc/NEWS.d/next/Library/2026-10-23-23-21-21.gh-issue-160549.q7ZdMt.rst deleted file mode 100644 index f2c21f06f8613e..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-23-23-21-21.gh-issue-160549.q7ZdMt.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix a crash when a :class:`compression.zstd.ZstdCompressor` that uses worker -threads and a :class:`~compression.zstd.ZstdDict` is deallocated, or the -interpreter exits, while data passed to -:meth:`~compression.zstd.ZstdCompressor.compress` is still being compressed. diff --git a/Misc/NEWS.d/next/Library/2026-10-24-01-53-42.gh-issue-160433.DSTlog.rst b/Misc/NEWS.d/next/Library/2026-10-24-01-53-42.gh-issue-160433.DSTlog.rst deleted file mode 100644 index d6779cf60249d0..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-24-01-53-42.gh-issue-160433.DSTlog.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix incorrect backup names around daylight saving time changes in -:class:`logging.handlers.TimedRotatingFileHandler`, including delayed rollovers. diff --git a/Misc/NEWS.d/next/Library/2026-10-24-07-58-00.gh-issue-160561.b3Kq1z.rst b/Misc/NEWS.d/next/Library/2026-10-24-07-58-00.gh-issue-160561.b3Kq1z.rst deleted file mode 100644 index c49cd6a83e90bf..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-24-07-58-00.gh-issue-160561.b3Kq1z.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a descriptor leak in :func:`socket.fromfd` when the socket constructor rejects its arguments. The duplicated descriptor was never closed. diff --git a/Misc/NEWS.d/next/Library/2026-10-24-18-00-00.gh-issue-160601.Utf8Bin.rst b/Misc/NEWS.d/next/Library/2026-10-24-18-00-00.gh-issue-160601.Utf8Bin.rst new file mode 100644 index 00000000000000..b0b5264ea341c4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-24-18-00-00.gh-issue-160601.Utf8Bin.rst @@ -0,0 +1,3 @@ +Fix a :exc:`UnicodeEncodeError` in :mod:`profiling.sampling` binary mode +when a sampled filename or function name contains surrogate characters. +These characters are now written as backslash escapes in the binary file. diff --git a/Misc/NEWS.d/next/Library/2026-10-24-22-32-23.gh-issue-160596.RKll-Z.rst b/Misc/NEWS.d/next/Library/2026-10-24-22-32-23.gh-issue-160596.RKll-Z.rst deleted file mode 100644 index c1db616f01d17a..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-24-22-32-23.gh-issue-160596.RKll-Z.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix the error message for invalid data passed to -:meth:`!asyncio.proactor_events.sendto`. diff --git a/Misc/NEWS.d/next/Library/2026-10-26-16-15-29.gh-issue-160427.9G4EBb.rst b/Misc/NEWS.d/next/Library/2026-10-26-16-15-29.gh-issue-160427.9G4EBb.rst deleted file mode 100644 index d57dd02a6d576f..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-26-16-15-29.gh-issue-160427.9G4EBb.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :attr:`~io.TextIOBase.buffer` of a detached :class:`io.TextIOWrapper` -raising :exc:`ValueError` instead of returning ``None``. diff --git a/Misc/NEWS.d/next/Library/2026-10-26-16-17-45.gh-issue-160708.w7z33g.rst b/Misc/NEWS.d/next/Library/2026-10-26-16-17-45.gh-issue-160708.w7z33g.rst deleted file mode 100644 index 7a88edad93f7c2..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-26-16-17-45.gh-issue-160708.w7z33g.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix out-of-bounds memory access in :func:`bool` on :mod:`ctypes` simple -objects enlarged by :func:`ctypes.resize`, and when passing such objects to -a foreign function. diff --git a/Misc/NEWS.d/next/Library/2026-10-26-21-14-17.gh-issue-160735.IIUifI.rst b/Misc/NEWS.d/next/Library/2026-10-26-21-14-17.gh-issue-160735.IIUifI.rst new file mode 100644 index 00000000000000..cf64610a9e6259 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-26-21-14-17.gh-issue-160735.IIUifI.rst @@ -0,0 +1,2 @@ +Fix Tachyon's Gecko export closing open markers too early when replaying a +binary profile whose thread samples arrive out of time order. diff --git a/Misc/NEWS.d/next/Library/2026-10-27-15-26-53.gh-issue-160781.4VSeQK.rst b/Misc/NEWS.d/next/Library/2026-10-27-15-26-53.gh-issue-160781.4VSeQK.rst deleted file mode 100644 index f6463a5d531369..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-27-15-26-53.gh-issue-160781.4VSeQK.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :class:`io.BytesIO` raising :exc:`SystemError` instead of -:exc:`OverflowError` when writing at a position close to -:data:`sys.maxsize`. diff --git a/Misc/NEWS.d/next/Library/2026-10-27-15-27-32.gh-issue-160781.kARuHu.rst b/Misc/NEWS.d/next/Library/2026-10-27-15-27-32.gh-issue-160781.kARuHu.rst deleted file mode 100644 index 1f1e25d8b461b4..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-27-15-27-32.gh-issue-160781.kARuHu.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :class:`io.StringIO` implementation of :meth:`io.IOBase.truncate` -rejecting sizes larger than ``2**31 - 1`` on Windows. diff --git a/Misc/NEWS.d/next/Library/2026-10-27-15-42-16.gh-issue-160781.VGLB9S.rst b/Misc/NEWS.d/next/Library/2026-10-27-15-42-16.gh-issue-160781.VGLB9S.rst deleted file mode 100644 index 73c8c177fc8828..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-27-15-42-16.gh-issue-160781.VGLB9S.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :class:`io.BytesIO` implementation of :meth:`io.IOBase.truncate` rejecting -sizes larger than ``2**31 - 1`` on Windows. diff --git a/Misc/NEWS.d/next/Library/2026-10-28-07-26-47.gh-issue-160804.R5HeTQ.rst b/Misc/NEWS.d/next/Library/2026-10-28-07-26-47.gh-issue-160804.R5HeTQ.rst deleted file mode 100644 index 36fa06dbb2202b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-28-07-26-47.gh-issue-160804.R5HeTQ.rst +++ /dev/null @@ -1,3 +0,0 @@ -:mod:`asyncio` call graph introspection now includes frames of generator -based awaitables, such as generators returned by ``__await__`` and -:func:`types.coroutine` generators. diff --git a/Misc/NEWS.d/next/Library/2026-10-28-07-34-05.gh-issue-160806.ZZRkhi.rst b/Misc/NEWS.d/next/Library/2026-10-28-07-34-05.gh-issue-160806.ZZRkhi.rst deleted file mode 100644 index 1eb916ce8e9843..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-28-07-34-05.gh-issue-160806.ZZRkhi.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix the pure-Python implementation of :class:`datetime.timedelta` losing -sub-second precision with large float arguments. It now gives the same -results as the C implementation. diff --git a/Misc/NEWS.d/next/Library/2026-10-28-12-00-00.gh-issue-53852.Pr0xy4.rst b/Misc/NEWS.d/next/Library/2026-10-28-12-00-00.gh-issue-53852.Pr0xy4.rst deleted file mode 100644 index 6b6c7fa1e17513..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-28-12-00-00.gh-issue-53852.Pr0xy4.rst +++ /dev/null @@ -1,3 +0,0 @@ -:class:`urllib.request.ProxyDigestAuthHandler` now raises -:exc:`~urllib.error.HTTPError` with status code 407 (Proxy Authentication -Required) instead of 401 when proxy authentication fails. diff --git a/Misc/NEWS.d/next/Library/2026-10-28-13-23-28.gh-issue-59515.wt31yNN4.rst b/Misc/NEWS.d/next/Library/2026-10-28-13-23-28.gh-issue-59515.wt31yNN4.rst deleted file mode 100644 index 7c4d5b7269acea..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-28-13-23-28.gh-issue-59515.wt31yNN4.rst +++ /dev/null @@ -1,4 +0,0 @@ -:class:`urllib.request.HTTPDigestAuthHandler` and -:class:`urllib.request.ProxyDigestAuthHandler` now parse all -``WWW-Authenticate`` (or ``Proxy-Authenticate``) headers and accept -multiple challenges per header: the first Digest challenge is used. diff --git a/Misc/NEWS.d/next/Library/2026-10-28-20-36-44.gh-issue-109564.WqoyNE.rst b/Misc/NEWS.d/next/Library/2026-10-28-20-36-44.gh-issue-109564.WqoyNE.rst deleted file mode 100644 index 66821f5cdc6e91..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-28-20-36-44.gh-issue-109564.WqoyNE.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix a race in :mod:`asyncio` where closing a server after it accepted a -connection, but before the connection's transport was created, reported an -:exc:`AssertionError` to the exception handler. Such connections are now -closed quietly. diff --git a/Misc/NEWS.d/next/Library/2026-10-29-19-42-53.gh-issue-53089.ewaIBvY6.rst b/Misc/NEWS.d/next/Library/2026-10-29-19-42-53.gh-issue-53089.ewaIBvY6.rst deleted file mode 100644 index 6fe3aace25ffe1..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-29-19-42-53.gh-issue-53089.ewaIBvY6.rst +++ /dev/null @@ -1,5 +0,0 @@ -:class:`urllib.request.HTTPDigestAuthHandler` now sends ``uri="/"`` in the -Digest ``Authorization`` header for URLs with an empty path, such as -``http://example.com``, matching the request-target sent by :mod:`http.client`. -Previously it sent ``uri=""``, which servers could reject. -Patch by Senthil Kumaran. diff --git a/Misc/NEWS.d/next/Library/2026-10-29-20-52-57.gh-issue-50720.Kd1VMR.rst b/Misc/NEWS.d/next/Library/2026-10-29-20-52-57.gh-issue-50720.Kd1VMR.rst deleted file mode 100644 index 263b7ac8958576..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-29-20-52-57.gh-issue-50720.Kd1VMR.rst +++ /dev/null @@ -1,4 +0,0 @@ -:exc:`urllib.error.URLError` now sets the :attr:`~OSError.errno` and -:attr:`~OSError.strerror` attributes from -:attr:`~urllib.error.URLError.reason` when it is an :exc:`OSError`. On -Windows, :attr:`~OSError.winerror` is set as well. diff --git a/Misc/NEWS.d/next/Library/2026-10-29-21-36-08.gh-issue-160929.mK8n2Q.rst b/Misc/NEWS.d/next/Library/2026-10-29-21-36-08.gh-issue-160929.mK8n2Q.rst deleted file mode 100644 index 09bd1e568e7e9b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-29-21-36-08.gh-issue-160929.mK8n2Q.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix reverse union with :class:`~collections.ChainMap` to preserve its lookup -semantics for defaulting mappings such as :class:`~collections.Counter` and -:class:`~collections.defaultdict`. diff --git a/Misc/NEWS.d/next/Library/2026-10-29-21-43-53.gh-issue-160429.eWapMs.rst b/Misc/NEWS.d/next/Library/2026-10-29-21-43-53.gh-issue-160429.eWapMs.rst deleted file mode 100644 index cd8bebd8c09acb..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-29-21-43-53.gh-issue-160429.eWapMs.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :class:`asyncio.ProactorEventLoop` socket transports leaving the socket -open, and :meth:`asyncio.Server.wait_closed` hanging, when the peer resets -the connection while the transport is being closed. diff --git a/Misc/NEWS.d/next/Library/2026-10-29-23-25-31.gh-issue-160782.1ZwCOr.rst b/Misc/NEWS.d/next/Library/2026-10-29-23-25-31.gh-issue-160782.1ZwCOr.rst deleted file mode 100644 index 57ff63c1f6d3f7..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-29-23-25-31.gh-issue-160782.1ZwCOr.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`venv`: a backslash before a quote in the prompt or the path no longer -breaks ``activate.fish``. diff --git a/Misc/NEWS.d/next/Library/2026-10-30-12-00-00.gh-issue-137549.Tm0Bxg.rst b/Misc/NEWS.d/next/Library/2026-10-30-12-00-00.gh-issue-137549.Tm0Bxg.rst deleted file mode 100644 index 32c27840f5b08b..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-30-12-00-00.gh-issue-137549.Tm0Bxg.rst +++ /dev/null @@ -1,3 +0,0 @@ -:func:`asyncio.timeout` now inserts :exc:`TimeoutError` into the context -chain of sub-exceptions of a :exc:`BaseExceptionGroup`, as it already did for -:exc:`ExceptionGroup`. diff --git a/Misc/NEWS.d/next/Library/2026-10-30-19-04-37.gh-issue-160900.007288.rst b/Misc/NEWS.d/next/Library/2026-10-30-19-04-37.gh-issue-160900.007288.rst new file mode 100644 index 00000000000000..4bc5737ccaf8bd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-30-19-04-37.gh-issue-160900.007288.rst @@ -0,0 +1 @@ +Allow :func:`fnmatch.filter` and :func:`fnmatch.filterfalse` to accept :term:`path-like objects `. diff --git a/Misc/NEWS.d/next/Library/2026-10-30-23-05-00.gh-issue-160976.GeCk0P.rst b/Misc/NEWS.d/next/Library/2026-10-30-23-05-00.gh-issue-160976.GeCk0P.rst new file mode 100644 index 00000000000000..142d56140b0ce1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-30-23-05-00.gh-issue-160976.GeCk0P.rst @@ -0,0 +1,2 @@ +Fix the Gecko reporter (``--gecko``) in :mod:`profiling.sampling` to record +full source file paths. diff --git a/Misc/NEWS.d/next/Library/2026-10-31-21-34-00.gh-issue-161039.k7_pTD.rst b/Misc/NEWS.d/next/Library/2026-10-31-21-34-00.gh-issue-161039.k7_pTD.rst deleted file mode 100644 index 3b697fb155b7ac..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-31-21-34-00.gh-issue-161039.k7_pTD.rst +++ /dev/null @@ -1 +0,0 @@ -Fix the :mod:`pydoc` pager not detecting ``less`` 706 and newer. diff --git a/Misc/NEWS.d/next/Library/2026-11-01-05-05-38.gh-issue-161060.YOZsVl.rst b/Misc/NEWS.d/next/Library/2026-11-01-05-05-38.gh-issue-161060.YOZsVl.rst new file mode 100644 index 00000000000000..8d55e0162c452e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-01-05-05-38.gh-issue-161060.YOZsVl.rst @@ -0,0 +1,5 @@ +Fix a crash in :mod:`xml.parsers.expat` when a handler raises an exception, +or the :meth:`~xml.parsers.expat.xmlparser.DefaultHandler` or +:meth:`~xml.parsers.expat.xmlparser.DefaultHandlerExpand` handler is unset, +while a token longer than 1024 bytes, such as a comment, is being reported to +the default handler from a document that is not encoded in UTF-8. diff --git a/Misc/NEWS.d/next/Library/2026-11-01-22-07-24.gh-issue-161124.c2YmcV.rst b/Misc/NEWS.d/next/Library/2026-11-01-22-07-24.gh-issue-161124.c2YmcV.rst deleted file mode 100644 index 32cfb56adb8316..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-01-22-07-24.gh-issue-161124.c2YmcV.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :func:`asyncio.print_call_graph` reporting event loop frames when -*depth* exceeds the task stack. Patch by Timofei Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-11-02-13-11-30.gh-issue-161166.zfFDlW.rst b/Misc/NEWS.d/next/Library/2026-11-02-13-11-30.gh-issue-161166.zfFDlW.rst deleted file mode 100644 index 4d66a399f5e655..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-02-13-11-30.gh-issue-161166.zfFDlW.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :meth:`asyncio.Task.print_stack` displaying an incorrect exception line -after a ``finally`` block. Patch by Timofei Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-11-02-13-26-36.gh-issue-161179.H8Yyaw.rst b/Misc/NEWS.d/next/Library/2026-11-02-13-26-36.gh-issue-161179.H8Yyaw.rst deleted file mode 100644 index 81d12633029959..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-02-13-26-36.gh-issue-161179.H8Yyaw.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix the function names in the error messages of :func:`os.spawnve`, -:func:`os.preadv` and :meth:`tkinter.Wm.wm_attributes`. diff --git a/Misc/NEWS.d/next/Library/2026-11-02-16-28-11.gh-issue-161178.D3-23R.rst b/Misc/NEWS.d/next/Library/2026-11-02-16-28-11.gh-issue-161178.D3-23R.rst deleted file mode 100644 index 1130fbe95ba019..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-02-16-28-11.gh-issue-161178.D3-23R.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix :meth:`asyncio.Task.print_stack` to include frames from async generator -operations. Patch by Timofei Ivankov diff --git a/Misc/NEWS.d/next/Library/2026-11-02-21-10-00.gh-issue-161212.pip263.rst b/Misc/NEWS.d/next/Library/2026-11-02-21-10-00.gh-issue-161212.pip263.rst deleted file mode 100644 index af137800454588..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-02-21-10-00.gh-issue-161212.pip263.rst +++ /dev/null @@ -1 +0,0 @@ -Bump the version of :pypi:`pip` bundled in :mod:`ensurepip` to version 26.3. diff --git a/Misc/NEWS.d/next/Library/2026-11-03-08-21-06.gh-issue-161184.PIhcD9.rst b/Misc/NEWS.d/next/Library/2026-11-03-08-21-06.gh-issue-161184.PIhcD9.rst new file mode 100644 index 00000000000000..3b15925773b99e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-03-08-21-06.gh-issue-161184.PIhcD9.rst @@ -0,0 +1,6 @@ +Speed up attribute access on instances of :mod:`io` classes and their +subclasses by storing instance attributes and weak references in the object +pre-header. The ``__dictoffset__`` and ``__weakrefoffset__`` attributes +of these classes now report the managed offsets, and C extension types that +subclass an :mod:`io` class can no longer declare their own ``__dictoffset__`` +or ``__weaklistoffset__`` member. diff --git a/Misc/NEWS.d/next/Library/2026-11-03-10-33-33.gh-issue-161233._9VrfL.rst b/Misc/NEWS.d/next/Library/2026-11-03-10-33-33.gh-issue-161233._9VrfL.rst deleted file mode 100644 index 96e9f5e39df3ec..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-03-10-33-33.gh-issue-161233._9VrfL.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix unraisable :exc:`TypeError` when ``None`` was passed as a *timer* to -:class:`cProfile.Profile `. diff --git a/Misc/NEWS.d/next/Library/2026-11-03-12-00-00.gh-issue-161076.nOdLy1.rst b/Misc/NEWS.d/next/Library/2026-11-03-12-00-00.gh-issue-161076.nOdLy1.rst deleted file mode 100644 index bc286d0a676c02..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-03-12-00-00.gh-issue-161076.nOdLy1.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix :mod:`asyncio` leaving a half-created transport registered with the -event loop and the server when setting ``TCP_NODELAY`` on a socket fails. -Such a failure could make :meth:`asyncio.Server.wait_closed` hang and -prevent new connections from being served. Errors from -``setsockopt(TCP_NODELAY)`` are now ignored. diff --git a/Misc/NEWS.d/next/Library/2026-11-03-12-00-00.gh-issue-93821.aCcRsT.rst b/Misc/NEWS.d/next/Library/2026-11-03-12-00-00.gh-issue-93821.aCcRsT.rst deleted file mode 100644 index a8a0da86e6abdb..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-03-12-00-00.gh-issue-93821.aCcRsT.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :class:`asyncio.ProactorEventLoop` servers that stopped accepting new -connections on Windows when a client reset its connection before it was -accepted (``WinError 64``, ``ERROR_NETNAME_DELETED``). The listening socket is -now kept open and the server continues to accept connections. diff --git a/Misc/NEWS.d/next/Library/2026-11-03-17-54-07.gh-issue-159637.0p15UL.rst b/Misc/NEWS.d/next/Library/2026-11-03-17-54-07.gh-issue-159637.0p15UL.rst new file mode 100644 index 00000000000000..cd820a2868f52e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-03-17-54-07.gh-issue-159637.0p15UL.rst @@ -0,0 +1,4 @@ +Fix :meth:`Document.renameNode() ` in +:mod:`xml.dom.minidom` rejecting a valid rename of an attribute to a +prefixed name, and removing the attribute from its element when a rename +is rejected. diff --git a/Misc/NEWS.d/next/Library/2026-11-03-21-26-09.gh-issue-161295.IJqoJM.rst b/Misc/NEWS.d/next/Library/2026-11-03-21-26-09.gh-issue-161295.IJqoJM.rst deleted file mode 100644 index f2ede87899e940..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-03-21-26-09.gh-issue-161295.IJqoJM.rst +++ /dev/null @@ -1,2 +0,0 @@ -:class:`reprlib.Repr` objects now have a meaningful :meth:`~object.__repr__` -describing the instance attributes. diff --git a/Misc/NEWS.d/next/Library/2026-11-05-11-56-50.gh-issue-161389.EIl2E6.rst b/Misc/NEWS.d/next/Library/2026-11-05-11-56-50.gh-issue-161389.EIl2E6.rst deleted file mode 100644 index 62fd6fe712f6df..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-05-11-56-50.gh-issue-161389.EIl2E6.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fixes a crash in :class:`decimal.Context` when passing invalid signaling -dicts as *flags* and *traps*. diff --git a/Misc/NEWS.d/next/Library/2026-11-05-11-59-58.gh-issue-161414.Xnxe8A.rst b/Misc/NEWS.d/next/Library/2026-11-05-11-59-58.gh-issue-161414.Xnxe8A.rst deleted file mode 100644 index 7536c5c25a13f5..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-05-11-59-58.gh-issue-161414.Xnxe8A.rst +++ /dev/null @@ -1,2 +0,0 @@ -Remove the backquote from the symbols listed by :func:`help`, asking for help on it reported that no -documentation was found. diff --git a/Misc/NEWS.d/next/Library/2026-11-05-22-26-44.gh-issue-161452.YfFfjq.rst b/Misc/NEWS.d/next/Library/2026-11-05-22-26-44.gh-issue-161452.YfFfjq.rst new file mode 100644 index 00000000000000..7ac5f49096c540 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-05-22-26-44.gh-issue-161452.YfFfjq.rst @@ -0,0 +1,3 @@ +Fix the :mod:`random` :ref:`command-line interface ` +printing a random float for a zero or negative integer input instead of +failing like ``--integer`` does. diff --git a/Misc/NEWS.d/next/Library/2026-11-06-12-00-00.gh-issue-160978.Kx7mQa.rst b/Misc/NEWS.d/next/Library/2026-11-06-12-00-00.gh-issue-160978.Kx7mQa.rst new file mode 100644 index 00000000000000..be5cc945f4eb61 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-06-12-00-00.gh-issue-160978.Kx7mQa.rst @@ -0,0 +1,3 @@ +Fix :mod:`ctypes` handing out distinct array types for the same item type +and length when the array type is created concurrently from several +threads. diff --git a/Misc/NEWS.d/next/Library/2026-11-06-20-12-44.gh-issue-161502.rYZtwK.rst b/Misc/NEWS.d/next/Library/2026-11-06-20-12-44.gh-issue-161502.rYZtwK.rst new file mode 100644 index 00000000000000..f284c37bd73894 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-06-20-12-44.gh-issue-161502.rYZtwK.rst @@ -0,0 +1,3 @@ +Fix missing tasks in ``python -m asyncio pstree`` output when tasks are +connected through an intermediate :class:`asyncio.Future`. Patch by Timofei +Ivankov diff --git a/Misc/NEWS.d/next/Library/2026-11-07-12-00-00.gh-issue-51540.Tunn07.rst b/Misc/NEWS.d/next/Library/2026-11-07-12-00-00.gh-issue-51540.Tunn07.rst new file mode 100644 index 00000000000000..9efd6dd0cca7db --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-07-12-00-00.gh-issue-51540.Tunn07.rst @@ -0,0 +1,8 @@ +:class:`urllib.request.ProxyBasicAuthHandler` and +:class:`urllib.request.ProxyDigestAuthHandler` now handle a ``407 Proxy +Authentication Required`` response to the CONNECT request for an ``https`` +URL. :mod:`http.client` raises the new :exc:`~http.client.ProxyTunnelError`, +a subclass of :exc:`OSError`, when a proxy rejects a CONNECT request. +For such a proxy, :class:`~urllib.request.ProxyDigestAuthHandler` computes +the digest over the CONNECT request (``CONNECT host:port``) instead of the +request sent to the origin server. diff --git a/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst b/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst new file mode 100644 index 00000000000000..804b8a494ca432 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst @@ -0,0 +1,3 @@ +Fix a crash in :func:`curses.tparm` for capabilities with parameters +that they format as strings, such as ``Ms`` of xterm. +Bytes objects are now accepted for these parameters. diff --git a/Misc/NEWS.d/next/Library/2026-11-08-19-29-54.gh-issue-141998.8vum0w.rst b/Misc/NEWS.d/next/Library/2026-11-08-19-29-54.gh-issue-141998.8vum0w.rst new file mode 100644 index 00000000000000..633c27d55175a3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-08-19-29-54.gh-issue-141998.8vum0w.rst @@ -0,0 +1,2 @@ +Do not syntax highlight text typed in response to :func:`input` in the +:term:`REPL`. diff --git a/Misc/NEWS.d/next/Library/2026-11-10-13-28-10.gh-issue-161746.HxgY9R.rst b/Misc/NEWS.d/next/Library/2026-11-10-13-28-10.gh-issue-161746.HxgY9R.rst new file mode 100644 index 00000000000000..293bf5fb27a755 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-10-13-28-10.gh-issue-161746.HxgY9R.rst @@ -0,0 +1,3 @@ +Speed up :meth:`datetime.datetime.fromordinal` by about 15% (30% in the +pure-Python implementation) by using the `Neri–Schneider algorithm +`__. diff --git a/Misc/NEWS.d/next/Library/2026-11-10-23-51-25.gh-issue-161788.J8vQmK.rst b/Misc/NEWS.d/next/Library/2026-11-10-23-51-25.gh-issue-161788.J8vQmK.rst new file mode 100644 index 00000000000000..34d3cff85123c7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-10-23-51-25.gh-issue-161788.J8vQmK.rst @@ -0,0 +1 @@ +:func:`shutil.which` now accepts path-like objects for the command. diff --git a/Misc/NEWS.d/next/Library/2026-11-11-12-00-00.gh-issue-135893.cTyp3s.rst b/Misc/NEWS.d/next/Library/2026-11-11-12-00-00.gh-issue-135893.cTyp3s.rst new file mode 100644 index 00000000000000..bddd3d817ab1cb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-11-12-00-00.gh-issue-135893.cTyp3s.rst @@ -0,0 +1,3 @@ +Fix a regression where importing :mod:`ctypes` could leave an extra file +descriptor open on some Linux systems, or hang in x86-64 macOS applications +signed with the hardened runtime. diff --git a/Misc/NEWS.d/next/Library/2026-11-11-14-29-41.gh-issue-161808.S9dK2p.rst b/Misc/NEWS.d/next/Library/2026-11-11-14-29-41.gh-issue-161808.S9dK2p.rst new file mode 100644 index 00000000000000..76626ca60b5b5a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-11-14-29-41.gh-issue-161808.S9dK2p.rst @@ -0,0 +1,2 @@ +Raise :exc:`SyntaxError` instead of :exc:`KeyError` for unsupported XPath +tokens in :mod:`xml.etree.ElementTree` path searches. diff --git a/Misc/NEWS.d/next/Library/2026-11-11-20-50-03.gh-issue-156812.nqPw30.rst b/Misc/NEWS.d/next/Library/2026-11-11-20-50-03.gh-issue-156812.nqPw30.rst new file mode 100644 index 00000000000000..741a2d9d42cf16 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-11-20-50-03.gh-issue-156812.nqPw30.rst @@ -0,0 +1,10 @@ +Serializing a document type declaration, a comment or a processing +instruction in :mod:`xml.dom.minidom` now checks the structural rules of XML. +A system literal is now quoted with the quote which it does not contain, +and :exc:`ValueError` is raised for an invalid public identifier, +for a literal containing both quotes, +for a public identifier without a system identifier, +for a comment ending with ``-``, +and for a processing instruction containing ``?>`` in the data +or using the reserved target ``xml``. +The characters are still not checked. diff --git a/Misc/NEWS.d/next/Library/2026-11-12-08-38-09.gh-issue-161840.YcEVjO.rst b/Misc/NEWS.d/next/Library/2026-11-12-08-38-09.gh-issue-161840.YcEVjO.rst new file mode 100644 index 00000000000000..f1e63af06fef87 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-12-08-38-09.gh-issue-161840.YcEVjO.rst @@ -0,0 +1,3 @@ +Propagate exceptions raised while getting the length of the assigned value +in :mod:`ctypes` array slice assignment instead of replacing them with a +:exc:`ValueError`. diff --git a/Misc/NEWS.d/next/Library/2026-11-12-17-53-13.gh-issue-161861.Kf7Qm2.rst b/Misc/NEWS.d/next/Library/2026-11-12-17-53-13.gh-issue-161861.Kf7Qm2.rst new file mode 100644 index 00000000000000..febea87b27d5a1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-12-17-53-13.gh-issue-161861.Kf7Qm2.rst @@ -0,0 +1,2 @@ +Preserve the active request's ``continue_timeout`` when a second +:meth:`~http.client.HTTPConnection.putrequest` call is rejected. diff --git a/Misc/NEWS.d/next/Library/2026-11-13-09-49-13.gh-issue-152845.ZipFlags.rst b/Misc/NEWS.d/next/Library/2026-11-13-09-49-13.gh-issue-152845.ZipFlags.rst new file mode 100644 index 00000000000000..70cf5134d3fe9f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-13-09-49-13.gh-issue-152845.ZipFlags.rst @@ -0,0 +1,3 @@ +Fix :mod:`zipfile` dropping the UTF-8 (EFS) flag for existing entries +when rewriting an archive in append mode, and honor an explicitly set +flag on new entries. Patch by Kasra Ghodsi. diff --git a/Misc/NEWS.d/next/Library/2026-11-13-12-01-53.gh-issue-161945.X8oFTq.rst b/Misc/NEWS.d/next/Library/2026-11-13-12-01-53.gh-issue-161945.X8oFTq.rst new file mode 100644 index 00000000000000..95adcdb8c25ba1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-13-12-01-53.gh-issue-161945.X8oFTq.rst @@ -0,0 +1,3 @@ +:meth:`zipfile.ZipFile.open` now raises :exc:`~zipfile.BadZipFile` when a +member's compression method differs between the central directory and the +local file header. diff --git a/Misc/NEWS.d/next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst b/Misc/NEWS.d/next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst new file mode 100644 index 00000000000000..561bb62d653707 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst @@ -0,0 +1,2 @@ +Fix a crash in :meth:`concurrent.interpreters.Interpreter.exec` when the +uncaught exception's ``__str__`` raises. diff --git a/Misc/NEWS.d/next/Library/2026-11-15-02-06-21.gh-issue-162028.jAnDAg.rst b/Misc/NEWS.d/next/Library/2026-11-15-02-06-21.gh-issue-162028.jAnDAg.rst new file mode 100644 index 00000000000000..a52a970039d3df --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-15-02-06-21.gh-issue-162028.jAnDAg.rst @@ -0,0 +1,2 @@ +Fix the position reported by the pure Python :mod:`json` decoder for an +invalid escape. It now points to the backslash, like the C decoder. diff --git a/Misc/NEWS.d/next/Library/2026-11-15-03-10-31.gh-issue-162031.2k-3CG.rst b/Misc/NEWS.d/next/Library/2026-11-15-03-10-31.gh-issue-162031.2k-3CG.rst new file mode 100644 index 00000000000000..01f1311c735d9b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-15-03-10-31.gh-issue-162031.2k-3CG.rst @@ -0,0 +1,3 @@ +Fix :meth:`io.BufferedIOBase.readinto` and :meth:`~io.BufferedIOBase.readinto1` +raising :exc:`TypeError` instead of returning ``None`` when called on a +non-blocking stream with no data available. diff --git a/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst b/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst new file mode 100644 index 00000000000000..5fa107c8c64271 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-15-17-30-00.gh-issue-162016.pNbStd.rst @@ -0,0 +1,4 @@ +Fix :meth:`subprocess.Popen.communicate` hanging or blocking past its +*timeout* while writing to the child's stdin on macOS and FreeBSD. This +works around a kernel bug most likely seen on loaded hosts with many pipes +in active use at once. diff --git a/Misc/NEWS.d/next/Library/2026-11-16-16-10-42.gh-issue-157579.6choQ_.rst b/Misc/NEWS.d/next/Library/2026-11-16-16-10-42.gh-issue-157579.6choQ_.rst new file mode 100644 index 00000000000000..1bd89a77b5995c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-16-16-10-42.gh-issue-157579.6choQ_.rst @@ -0,0 +1,3 @@ +The error handler :class:`~tempfile.TemporaryDirectory` uses to clean up +unreadable and inaccessible files no longer fails for temporary directories +created using :class:`bytes` paths. diff --git a/Misc/NEWS.d/next/Library/2026-11-18-12-18-53.gh-issue-149584.rDchNk.rst b/Misc/NEWS.d/next/Library/2026-11-18-12-18-53.gh-issue-149584.rDchNk.rst new file mode 100644 index 00000000000000..2d0e9d203cf6b3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-18-12-18-53.gh-issue-149584.rDchNk.rst @@ -0,0 +1,3 @@ +Read stack chunks directly in :mod:`profiling.sampling`, without the page +cache. This removes a redundant retry on macOS that can read a replacement +chunk after the target process reuses the same address. diff --git a/Misc/NEWS.d/next/Library/2026-11-18-18-28-37.gh-issue-162230.ezR86S.rst b/Misc/NEWS.d/next/Library/2026-11-18-18-28-37.gh-issue-162230.ezR86S.rst new file mode 100644 index 00000000000000..00b96c972ece6f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-18-18-28-37.gh-issue-162230.ezR86S.rst @@ -0,0 +1,3 @@ +Speed up :meth:`!io.BufferedRWPair.readline`. If a non-blocking read would +block, return the bytes read so far (possibly ``b''``). Previously, it raised +:exc:`OSError`. diff --git a/Misc/NEWS.d/next/Library/2026-11-19-17-55-31.gh-issue-162282.1WW-xb.rst b/Misc/NEWS.d/next/Library/2026-11-19-17-55-31.gh-issue-162282.1WW-xb.rst new file mode 100644 index 00000000000000..0d72db13e0c2be --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-17-55-31.gh-issue-162282.1WW-xb.rst @@ -0,0 +1 @@ +Fix quadratic-time cost in :meth:`urllib.robotparser.RobotFileParser.parse` when a single group contains many identical ``User-agent:`` lines. diff --git a/Misc/NEWS.d/next/Library/2026-11-19-19-44-33.gh-issue-162285.wnH4aQ.rst b/Misc/NEWS.d/next/Library/2026-11-19-19-44-33.gh-issue-162285.wnH4aQ.rst new file mode 100644 index 00000000000000..ff955a30053d27 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-19-44-33.gh-issue-162285.wnH4aQ.rst @@ -0,0 +1,3 @@ +Fix quadratic memory use in :mod:`xmlrpc.client` when decoding malformed +XML-RPC packets containing nested string elements. This can cause ``loads()`` +to raise on malformed packets that were previously silently accepted. diff --git a/Misc/NEWS.d/next/Library/2026-11-19-22-01-29.gh-issue-162246.y3L8nP.rst b/Misc/NEWS.d/next/Library/2026-11-19-22-01-29.gh-issue-162246.y3L8nP.rst new file mode 100644 index 00000000000000..702b38215ed6ef --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-22-01-29.gh-issue-162246.y3L8nP.rst @@ -0,0 +1,2 @@ +Fix :class:`io.BufferedRWPair` losing buffered writes on deallocation when the +reader and writer share a raw stream. diff --git a/Misc/NEWS.d/next/Library/2026-11-20-09-30-00.gh-issue-162217.wC7sHd.rst b/Misc/NEWS.d/next/Library/2026-11-20-09-30-00.gh-issue-162217.wC7sHd.rst new file mode 100644 index 00000000000000..be12568cb8f7fe --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-20-09-30-00.gh-issue-162217.wC7sHd.rst @@ -0,0 +1,4 @@ +Fix :exc:`asyncio.InvalidStateError` being logged when the standard input of +an :mod:`asyncio` subprocess is closed after a task waiting in +:meth:`proc.stdin.wait_closed() ` was +cancelled. diff --git a/Misc/NEWS.d/next/Library/2026-11-21-11-11-34.gh-issue-162246.jN-ORE.rst b/Misc/NEWS.d/next/Library/2026-11-21-11-11-34.gh-issue-162246.jN-ORE.rst new file mode 100644 index 00000000000000..d596bd87ad31e9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-21-11-11-34.gh-issue-162246.jN-ORE.rst @@ -0,0 +1,2 @@ +Fix a reference leak in :class:`io.BufferedRWPair` when :meth:`~object.__init__` +is called more than once: the previous reader and writer are now released. diff --git a/Misc/NEWS.d/next/Library/2026-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst b/Misc/NEWS.d/next/Library/2026-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst new file mode 100644 index 00000000000000..db5b2e79b5b2db --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst @@ -0,0 +1,2 @@ +Fix the asyncio call graph to show which task awaits :func:`asyncio.gather`. +Patch by Timofei Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst b/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst new file mode 100644 index 00000000000000..563686f8561aa7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst @@ -0,0 +1,2 @@ +Fix the asyncio call graph to show which task awaits the future returned by +:func:`asyncio.shield`. Patch by Timofei Ivankov. diff --git a/Misc/NEWS.d/next/Library/2026-11-22-15-53-58.gh-issue-162350.2xG9ZI.rst b/Misc/NEWS.d/next/Library/2026-11-22-15-53-58.gh-issue-162350.2xG9ZI.rst new file mode 100644 index 00000000000000..df101c5386cb45 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-22-15-53-58.gh-issue-162350.2xG9ZI.rst @@ -0,0 +1,3 @@ +Add ``@vectorcall`` for :class:`asyncio.Future` and :class:`asyncio.Task` +constructors. The performance boost is 33% for the future creation and 15% +for the task. diff --git a/Misc/NEWS.d/next/Library/2026-11-22-15-56-36.gh-issue-162282.QjKIag.rst b/Misc/NEWS.d/next/Library/2026-11-22-15-56-36.gh-issue-162282.QjKIag.rst new file mode 100644 index 00000000000000..70f534e8f9215e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-22-15-56-36.gh-issue-162282.QjKIag.rst @@ -0,0 +1,3 @@ +Fix quadratic-time cost in :mod:`urllib.robotparser` when many groups share +a ``User-agent:``, and when a group with many user agents is checked for +a user agent that is not listed explicitly. diff --git a/Misc/NEWS.d/next/Library/2026-11-23-02-10-10.gh-issue-162467.6hMRI-.rst b/Misc/NEWS.d/next/Library/2026-11-23-02-10-10.gh-issue-162467.6hMRI-.rst new file mode 100644 index 00000000000000..fccff39902eb5e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-23-02-10-10.gh-issue-162467.6hMRI-.rst @@ -0,0 +1,3 @@ +Speed up :meth:`datetime.timedelta.total_seconds` and +:meth:`datetime.datetime.timestamp` on aware datetimes. Patch by Maurycy +Pawłowski-Wieroński. diff --git a/Misc/NEWS.d/next/Library/2026-11-24-12-31-49.gh-issue-159865.aB3eGx.rst b/Misc/NEWS.d/next/Library/2026-11-24-12-31-49.gh-issue-159865.aB3eGx.rst new file mode 100644 index 00000000000000..aea5534e1dffc1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-24-12-31-49.gh-issue-159865.aB3eGx.rst @@ -0,0 +1,2 @@ +Fix :mod:`profiling.sampling` dropping exception-mode samples when a thread +blocks inside an exception handler. diff --git a/Misc/NEWS.d/next/Library/2026-11-24-15-30-00.gh-issue-84256.Fz7Rk2.rst b/Misc/NEWS.d/next/Library/2026-11-24-15-30-00.gh-issue-84256.Fz7Rk2.rst new file mode 100644 index 00000000000000..85b5245b68f7e5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-24-15-30-00.gh-issue-84256.Fz7Rk2.rst @@ -0,0 +1,3 @@ +Fix a crash in :mod:`tkinter` on Windows when a Tk event handler, such as +a ```` binding, is called while another event loop is running, +for example when the window is resized while a native dialog is shown. diff --git a/Misc/NEWS.d/next/Library/2026-11-24-17-00-00.gh-issue-140488.Bw4tIq.rst b/Misc/NEWS.d/next/Library/2026-11-24-17-00-00.gh-issue-140488.Bw4tIq.rst new file mode 100644 index 00000000000000..ad13320d20a82d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-24-17-00-00.gh-issue-140488.Bw4tIq.rst @@ -0,0 +1,3 @@ +The :mod:`tkinter` event loop running at the interactive prompt no longer +polls on Unix if Tcl is threaded. It now waits for events or input, so the +process does not wake up every few milliseconds while idle. diff --git a/Misc/NEWS.d/next/Library/2026-11-24-17-56-46.gh-issue-159207.3PCgei.rst b/Misc/NEWS.d/next/Library/2026-11-24-17-56-46.gh-issue-159207.3PCgei.rst new file mode 100644 index 00000000000000..b80c2f7d2a3e93 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-24-17-56-46.gh-issue-159207.3PCgei.rst @@ -0,0 +1,4 @@ +Reduce the memory used by :mod:`profiling.sampling` when replaying binary +profiles on Linux and Windows. Sample data is now read one block at a time +instead of being loaded into memory in full, so the memory used for samples +stays small even for very long captures. diff --git a/Misc/NEWS.d/next/Library/2026-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst b/Misc/NEWS.d/next/Library/2026-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst new file mode 100644 index 00000000000000..7c2a81de7cf8bb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst @@ -0,0 +1,6 @@ +Fix a crash in :mod:`tkinter` at the interactive prompt if the first Tcl +interpreter was created in a thread which has since exited. The event hook +which runs Tcl events while waiting for input is also no longer removed when +an interpreter is deleted in other thread while other interpreters still +exist, and its state no longer has data races in the :term:`free-threaded +build`. diff --git a/Misc/NEWS.d/next/Library/2026-11-25-20-49-22.gh-issue-162622.qAs2pK.rst b/Misc/NEWS.d/next/Library/2026-11-25-20-49-22.gh-issue-162622.qAs2pK.rst new file mode 100644 index 00000000000000..1f6c1b8cf6fe3e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-25-20-49-22.gh-issue-162622.qAs2pK.rst @@ -0,0 +1,2 @@ +Preserve literal braces in f-string and t-string format specifiers when +using :func:`ast.unparse`. diff --git a/Misc/NEWS.d/next/Library/2026-11-25-21-00-00.gh-issue-162647.Qm4Tsx.rst b/Misc/NEWS.d/next/Library/2026-11-25-21-00-00.gh-issue-162647.Qm4Tsx.rst new file mode 100644 index 00000000000000..cf069c8985d871 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-25-21-00-00.gh-issue-162647.Qm4Tsx.rst @@ -0,0 +1,4 @@ +Fix :mod:`tkinter` when Tcl interpreters run :meth:`!mainloop` in different +threads. :meth:`!quit` no longer stops the main loop of other interpreter, +and stops the main loop even if it waits for events. An exception raised in +a callback is no longer raised by the main loop in other thread. diff --git a/Misc/NEWS.d/next/Library/2026-11-26-12-00-00.gh-issue-162650.Nm8Dsq.rst b/Misc/NEWS.d/next/Library/2026-11-26-12-00-00.gh-issue-162650.Nm8Dsq.rst new file mode 100644 index 00000000000000..0f4108e07b76a5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-26-12-00-00.gh-issue-162650.Nm8Dsq.rst @@ -0,0 +1,3 @@ +Fix :mod:`tkinter` calls from other threads failing with :exc:`RuntimeError` +after a nested :meth:`!mainloop` exited, for example after +:meth:`!SimpleDialog.go` returned. diff --git a/Misc/NEWS.d/next/Security/2026-08-31-16-47-33.gh-issue-157953.KxCFSN.rst b/Misc/NEWS.d/next/Security/2026-08-31-16-47-33.gh-issue-157953.KxCFSN.rst deleted file mode 100644 index 3220f4613a91af..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-08-31-16-47-33.gh-issue-157953.KxCFSN.rst +++ /dev/null @@ -1 +0,0 @@ -Update bundled `libexpat `_ to version 2.9.0. diff --git a/Misc/NEWS.d/next/Security/2026-10-06-12-30-00.gh-issue-158928.Zd9Ybv.rst b/Misc/NEWS.d/next/Security/2026-10-06-12-30-00.gh-issue-158928.Zd9Ybv.rst deleted file mode 100644 index 2b9597c5ed94f3..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-06-12-30-00.gh-issue-158928.Zd9Ybv.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a buffer overflow when initializing a :class:`bytearray` from an -iterator. diff --git a/Misc/NEWS.d/next/Security/2026-10-07-13-30-00.gh-issue-158907.k4Qm2p.rst b/Misc/NEWS.d/next/Security/2026-10-07-13-30-00.gh-issue-158907.k4Qm2p.rst new file mode 100644 index 00000000000000..67bbd3da1cbbe8 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-10-07-13-30-00.gh-issue-158907.k4Qm2p.rst @@ -0,0 +1,5 @@ +:class:`~urllib.request.ProxyBasicAuthHandler` and +:class:`~urllib.request.ProxyDigestAuthHandler` no longer send credentials +in response to ``407`` from a direct origin. A scheme-less lookup against +the origin host could match passwords stored for an HTTPS URL and retry +the cleartext request with ``Proxy-Authorization``. diff --git a/Misc/NEWS.d/next/Security/2026-10-15-03-47-08.gh-issue-159428.XHRKwY.rst b/Misc/NEWS.d/next/Security/2026-10-15-03-47-08.gh-issue-159428.XHRKwY.rst deleted file mode 100644 index d481ed2b74a20e..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-15-03-47-08.gh-issue-159428.XHRKwY.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`mimetypes` no longer reads the files listed in :data:`mimetypes.knownfiles` -by default on Windows, and uses only the registry. diff --git a/Misc/NEWS.d/next/Security/2026-10-20-13-15-00.gh-issue-159894.8Kp4mN.rst b/Misc/NEWS.d/next/Security/2026-10-20-13-15-00.gh-issue-159894.8Kp4mN.rst deleted file mode 100644 index 0f27a475b5883e..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-20-13-15-00.gh-issue-159894.8Kp4mN.rst +++ /dev/null @@ -1 +0,0 @@ -Bump the version of :pypi:`pip` bundled in :mod:`ensurepip` to version 26.2.2. diff --git a/Misc/NEWS.d/next/Security/2026-10-21-16-55-07.gh-issue-160040.9PjFjC.rst b/Misc/NEWS.d/next/Security/2026-10-21-16-55-07.gh-issue-160040.9PjFjC.rst deleted file mode 100644 index 7bcec7f5808cac..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-21-16-55-07.gh-issue-160040.9PjFjC.rst +++ /dev/null @@ -1,12 +0,0 @@ -.. cve: CVE-2026-115557 - -:mod:`ssl` now rejects TLS certificate chains that contain any of a set of -compromised eMudhra intermediate CA keys. This is an extraordinary measure and -a last line of defense: removing trust is primarily the job of the platform root -store, and the built-in denylist only blocks connections while that update has -not yet reached the OS, a container image, or a bundled trust store in various -Python packages. Entries are added to the denylist only under a very high -threshold. -The denylist matches on the public key (the SHA-256 digest of the :rfc:`5280` -``SubjectPublicKeyInfo``), so it also covers the cross-signed certificates that -reuse one of the distrusted keys. diff --git a/Misc/NEWS.d/next/Security/2026-10-21-19-58-53.gh-issue-160097.-opfAL.rst b/Misc/NEWS.d/next/Security/2026-10-21-19-58-53.gh-issue-160097.-opfAL.rst deleted file mode 100644 index b48c8f7213ee82..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-21-19-58-53.gh-issue-160097.-opfAL.rst +++ /dev/null @@ -1,5 +0,0 @@ -.. cve: CVE-2026-114583 - -:func:`tarfile.tar_filter` now rejects link members whose path resolves to -the destination directory itself, as :func:`tarfile.data_filter` already -did. diff --git a/Misc/NEWS.d/next/Security/2026-10-22-08-04-33.gh-issue-160300.q7Zk2B.rst b/Misc/NEWS.d/next/Security/2026-10-22-08-04-33.gh-issue-160300.q7Zk2B.rst deleted file mode 100644 index 3cadc6c2c4fd1f..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-22-08-04-33.gh-issue-160300.q7Zk2B.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix :class:`http.cookiejar.DefaultCookiePolicy` accepting and returning -cookies with a negative ``Version`` attribute without checking their domain -against the request host. Such cookies are now ignored. diff --git a/Misc/NEWS.d/next/Security/2026-10-22-08-05-32.gh-issue-160420.tZ9dRv.rst b/Misc/NEWS.d/next/Security/2026-10-22-08-05-32.gh-issue-160420.tZ9dRv.rst deleted file mode 100644 index 2255056dac577e..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-22-08-05-32.gh-issue-160420.tZ9dRv.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :class:`zoneinfo.ZoneInfo` on Windows accepting keys in which a component -after a ``/`` starts with a drive letter, such as ``America/C:\file``. Such a -key could open a file outside the ``tzdata`` package; it now raises -:exc:`ValueError`. diff --git a/Misc/NEWS.d/next/Security/2026-10-22-08-07-52.gh-issue-161323.q7Drv2.rst b/Misc/NEWS.d/next/Security/2026-10-22-08-07-52.gh-issue-161323.q7Drv2.rst deleted file mode 100644 index 95949e93576c6c..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-22-08-07-52.gh-issue-161323.q7Drv2.rst +++ /dev/null @@ -1,5 +0,0 @@ -On Windows, the ``'tar'`` and ``'data'`` :mod:`tarfile` extraction filters -now refuse member names and link targets that have a drive but are not -absolute, such as ``C:foo``, with :exc:`~tarfile.AbsolutePathError` or -:exc:`~tarfile.AbsoluteLinkError`. Such members could be extracted outside -the destination directory. Fixes :cve:`2026-107784`. diff --git a/Misc/NEWS.d/next/Security/2026-10-22-08-08-23.gh-issue-160506.rN4dLq.rst b/Misc/NEWS.d/next/Security/2026-10-22-08-08-23.gh-issue-160506.rN4dLq.rst deleted file mode 100644 index 07ba8fdbff2cef..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-22-08-08-23.gh-issue-160506.rN4dLq.rst +++ /dev/null @@ -1,4 +0,0 @@ -Fix :func:`os.path.realpath` on Windows returning the wrong path for a broken -relative symbolic link reached through a symbolic link to a directory. This -allowed :mod:`tarfile` extraction with the ``'data'`` or ``'tar'`` filter to -create a file outside the destination directory. diff --git a/Misc/NEWS.d/next/Security/2026-10-22-08-08-57.gh-issue-160499.pf9H9g.rst b/Misc/NEWS.d/next/Security/2026-10-22-08-08-57.gh-issue-160499.pf9H9g.rst deleted file mode 100644 index 4fcfc9edd90b78..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-22-08-08-57.gh-issue-160499.pf9H9g.rst +++ /dev/null @@ -1,5 +0,0 @@ -Fix unbounded memory growth in :mod:`asyncio` TLS connections when a peer -stops reading but keeps sending records that make OpenSSL generate its own -replies (such as TLS 1.2 renegotiation alerts). Reading from the peer is now -paused when this output accumulates, instead of only when application data -does. diff --git a/Misc/NEWS.d/next/Security/2026-10-22-11-09-30.gh-issue-160279.9BtxeF.rst b/Misc/NEWS.d/next/Security/2026-10-22-11-09-30.gh-issue-160279.9BtxeF.rst deleted file mode 100644 index d143e742f52b0e..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-22-11-09-30.gh-issue-160279.9BtxeF.rst +++ /dev/null @@ -1,2 +0,0 @@ -Prevent :mod:`venv` from importing modules from the root of an existing -virtual environment when installing or upgrading pip. diff --git a/Misc/NEWS.d/next/Security/2026-10-27-16-25-40.gh-issue-160898.epi53X.rst b/Misc/NEWS.d/next/Security/2026-10-27-16-25-40.gh-issue-160898.epi53X.rst deleted file mode 100644 index 2eb04454cbd9a7..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-10-27-16-25-40.gh-issue-160898.epi53X.rst +++ /dev/null @@ -1,4 +0,0 @@ -.. cve: CVE-2026-116566 - -:func:`socket.getaddrinfo` now rejects null bytes within *host* and *port* -parameters as this previously caused the values to be silently truncated. diff --git a/Misc/NEWS.d/next/Security/2026-11-02-18-30-09.gh-issue-161192.2ScSRO.rst b/Misc/NEWS.d/next/Security/2026-11-02-18-30-09.gh-issue-161192.2ScSRO.rst deleted file mode 100644 index 14682058abb235..00000000000000 --- a/Misc/NEWS.d/next/Security/2026-11-02-18-30-09.gh-issue-161192.2ScSRO.rst +++ /dev/null @@ -1,3 +0,0 @@ -Update bundled `libexpat `_ to version 2.10.0 -for the fixes to :cve:`2026-113351`, :cve:`2026-115506`, :cve:`2026-117610` -and :cve:`2026-118370`. diff --git a/Misc/NEWS.d/next/Security/2026-11-09-17-19-29.gh-issue-161707.xWzwqQ.rst b/Misc/NEWS.d/next/Security/2026-11-09-17-19-29.gh-issue-161707.xWzwqQ.rst new file mode 100644 index 00000000000000..63731daafff41e --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-09-17-19-29.gh-issue-161707.xWzwqQ.rst @@ -0,0 +1,5 @@ +.. cve: CVE-2026-16052 + +:mod:`http.cookies` now raises :exc:`~http.cookies.CookieError` on any attempt +to set a value string that contains the reserved separator character ``;`` to +any attribute except :attr:`~http.cookies.Morsel.comment`. diff --git a/Misc/NEWS.d/next/Security/2026-11-23-19-30-04.gh-issue-162493.QeSfGU.rst b/Misc/NEWS.d/next/Security/2026-11-23-19-30-04.gh-issue-162493.QeSfGU.rst new file mode 100644 index 00000000000000..79314ee2650da6 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-23-19-30-04.gh-issue-162493.QeSfGU.rst @@ -0,0 +1,3 @@ +Update bundled `libexpat `_ to version 2.11.0 +for the fixes to :cve:`2026-124317`, :cve:`2026-128241`, and +:cve:`2026-134703`. diff --git a/Misc/NEWS.d/next/Security/2026-11-24-11-39-37.gh-issue-162577.kgsLAF.rst b/Misc/NEWS.d/next/Security/2026-11-24-11-39-37.gh-issue-162577.kgsLAF.rst new file mode 100644 index 00000000000000..ea55b58ddec910 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-24-11-39-37.gh-issue-162577.kgsLAF.rst @@ -0,0 +1,2 @@ +Update ``zstd`` to v1.6.0 in Android, iOS, and Windows builds to address +:cve:`2026-124851`. diff --git a/Misc/NEWS.d/next/Tests/2023-09-03-12-53-53.gh-issue-108828.zoWIyX.rst b/Misc/NEWS.d/next/Tests/2023-09-03-12-53-53.gh-issue-108828.zoWIyX.rst new file mode 100644 index 00000000000000..2b9fbdd48dd450 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-09-03-12-53-53.gh-issue-108828.zoWIyX.rst @@ -0,0 +1,9 @@ +Add support of labels in tests. The ``@test.support.mark('label')`` +decorator adds a label to a test method or class. +``test.support.mark_module('label')`` adds a label to the whole module. +Many :mod:`test.support` decorators like :func:`~test.support.requires_resource`, +:func:`~test.support.cpython_only` or :func:`~test.support.bigmemtest` add +labels automatically. A label can have a value (``True`` by default). Tests +which have or have not the specified label can be filtered by options +``--label`` and ``--no-label``; ``--label name=value`` matches a specific +value, while ``--label name`` matches any value. diff --git a/Misc/NEWS.d/next/Tests/2026-09-16-14-37-28.gh-issue-126253.9FOKPp.rst b/Misc/NEWS.d/next/Tests/2026-09-16-14-37-28.gh-issue-126253.9FOKPp.rst new file mode 100644 index 00000000000000..370d822f883aa9 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-09-16-14-37-28.gh-issue-126253.9FOKPp.rst @@ -0,0 +1 @@ +Fix ``test_os.test_attributes()`` on Windows NTFS: ignore unknown file attributes. diff --git a/Misc/NEWS.d/next/Tests/2026-10-05-22-36-06.gh-issue-158895.3Dmc4J.rst b/Misc/NEWS.d/next/Tests/2026-10-05-22-36-06.gh-issue-158895.3Dmc4J.rst deleted file mode 100644 index a1c9e648e5c744..00000000000000 --- a/Misc/NEWS.d/next/Tests/2026-10-05-22-36-06.gh-issue-158895.3Dmc4J.rst +++ /dev/null @@ -1,3 +0,0 @@ -When run in GitHub Actions, :mod:`test.regrtest` now annotates each test -failure in the job log and writes a summary of the failures to the job -summary. diff --git a/Misc/NEWS.d/next/Tests/2026-10-07-12-00-58.gh-issue-158975.yR8q2L.rst b/Misc/NEWS.d/next/Tests/2026-10-07-12-00-58.gh-issue-158975.yR8q2L.rst deleted file mode 100644 index 450af29aea272b..00000000000000 --- a/Misc/NEWS.d/next/Tests/2026-10-07-12-00-58.gh-issue-158975.yR8q2L.rst +++ /dev/null @@ -1,3 +0,0 @@ -Replace fixed sleeps used for synchronization in multiprocessing tests with -bounded waits to avoid failures on slow or overloaded systems. Ensure pool -cleanup runs even when assertions fail in the imap buffer tests. diff --git a/Misc/NEWS.d/next/Tests/2026-10-13-13-34-00.gh-issue-159293.Lr8q2N.rst b/Misc/NEWS.d/next/Tests/2026-10-13-13-34-00.gh-issue-159293.Lr8q2N.rst deleted file mode 100644 index 2a122343a3fe2a..00000000000000 --- a/Misc/NEWS.d/next/Tests/2026-10-13-13-34-00.gh-issue-159293.Lr8q2N.rst +++ /dev/null @@ -1,2 +0,0 @@ -Restore the recursion limit after the free-threading recursion-limit race test -to avoid failures in subsequent tests run in the same process. diff --git a/Misc/NEWS.d/next/Tests/2026-10-19-17-05-00.gh-issue-107862.qH3sTa.rst b/Misc/NEWS.d/next/Tests/2026-10-19-17-05-00.gh-issue-107862.qH3sTa.rst deleted file mode 100644 index 624fd82d50041c..00000000000000 --- a/Misc/NEWS.d/next/Tests/2026-10-19-17-05-00.gh-issue-107862.qH3sTa.rst +++ /dev/null @@ -1,2 +0,0 @@ -Add ``hypothesis.stateful`` and the ``timezones()`` and ``ip_addresses()`` -strategies to the Hypothesis stubs used when Hypothesis is not installed. diff --git a/Misc/NEWS.d/next/Tests/2026-10-28-20-40-00.gh-issue-158646.k2Xw7q.rst b/Misc/NEWS.d/next/Tests/2026-10-28-20-40-00.gh-issue-158646.k2Xw7q.rst deleted file mode 100644 index 93a7c26c632140..00000000000000 --- a/Misc/NEWS.d/next/Tests/2026-10-28-20-40-00.gh-issue-158646.k2Xw7q.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a race in ``test_asyncio.test_sslproto`` where the server side of -``test_shutdown_corrupted_ssl_sends_close_notify`` could fail with a -connection reset on Windows. diff --git a/Misc/NEWS.d/next/Tests/2026-11-03-13-12-50.gh-issue-161263.Voe8VM.rst b/Misc/NEWS.d/next/Tests/2026-11-03-13-12-50.gh-issue-161263.Voe8VM.rst deleted file mode 100644 index 98c631cc2f663a..00000000000000 --- a/Misc/NEWS.d/next/Tests/2026-11-03-13-12-50.gh-issue-161263.Voe8VM.rst +++ /dev/null @@ -1,2 +0,0 @@ -Skip tests which use MD5 algorithm for WWW-Authenticate challenges when -libssl is in FIPS mode. diff --git a/Misc/NEWS.d/next/Tests/2026-11-10-14-49-01.gh-issue-161739.NZTbYC.rst b/Misc/NEWS.d/next/Tests/2026-11-10-14-49-01.gh-issue-161739.NZTbYC.rst new file mode 100644 index 00000000000000..a480b9788f7574 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-11-10-14-49-01.gh-issue-161739.NZTbYC.rst @@ -0,0 +1,2 @@ +``test_hashlibtest_pbkdf2_hmac_c`` from ``test_haslib`` no longer fails with +``ValueError: [Provider routines] invalid salt length`` with FIPS enabled. diff --git a/Misc/NEWS.d/next/Tests/2026-11-15-09-43-15.gh-issue-162034.egx5QV.rst b/Misc/NEWS.d/next/Tests/2026-11-15-09-43-15.gh-issue-162034.egx5QV.rst new file mode 100644 index 00000000000000..1b7d2c4ad5f27c --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-11-15-09-43-15.gh-issue-162034.egx5QV.rst @@ -0,0 +1,2 @@ +The Apple testbed projects now build when the path of the project contains a +space. diff --git a/Misc/NEWS.d/next/Tests/2026-11-17-00-39-51.gh-issue-162034.FmiuNi.rst b/Misc/NEWS.d/next/Tests/2026-11-17-00-39-51.gh-issue-162034.FmiuNi.rst new file mode 100644 index 00000000000000..1764fa2a458e59 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-11-17-00-39-51.gh-issue-162034.FmiuNi.rst @@ -0,0 +1,2 @@ +Fix the Apple testbed's extension module processing for an XCframework or +package path with a space, or a build path with brackets or a backslash. diff --git a/Misc/NEWS.d/next/Tests/2026-11-17-15-57-41.gh-issue-162144.pL5k29.rst b/Misc/NEWS.d/next/Tests/2026-11-17-15-57-41.gh-issue-162144.pL5k29.rst new file mode 100644 index 00000000000000..bbd4a9cf54752e --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-11-17-15-57-41.gh-issue-162144.pL5k29.rst @@ -0,0 +1,3 @@ +Skip ``@support.nomemtest`` tests when using a thread sanitizer (TSan). +``set_nomemory()`` and ``call_with_nomemory()`` functions of the +:mod:`!_testcapi` extension are not thread safe. Patch by Victor Stinner. diff --git a/Misc/NEWS.d/next/Tools-Demos/2026-10-14-09-48-26.gh-issue-159385.0aWOA9.rst b/Misc/NEWS.d/next/Tools-Demos/2026-10-14-09-48-26.gh-issue-159385.0aWOA9.rst deleted file mode 100644 index e76a5e666df236..00000000000000 --- a/Misc/NEWS.d/next/Tools-Demos/2026-10-14-09-48-26.gh-issue-159385.0aWOA9.rst +++ /dev/null @@ -1 +0,0 @@ -iOS builds can now be configured to generate free-threading or debug output. diff --git a/Misc/NEWS.d/next/Tools-Demos/2026-10-20-12-50-00.gh-issue-114258.slotNx.rst b/Misc/NEWS.d/next/Tools-Demos/2026-10-20-12-50-00.gh-issue-114258.slotNx.rst deleted file mode 100644 index afaeda209e1bd2..00000000000000 --- a/Misc/NEWS.d/next/Tools-Demos/2026-10-20-12-50-00.gh-issue-114258.slotNx.rst +++ /dev/null @@ -1,3 +0,0 @@ -Argument Clinic now supports the ``__next__`` type slot (``tp_iternext``), -including ``@critical_section``. - diff --git a/Misc/NEWS.d/next/Tools-Demos/2026-10-21-11-53-40.gh-issue-87613.tmKBlg.rst b/Misc/NEWS.d/next/Tools-Demos/2026-10-21-11-53-40.gh-issue-87613.tmKBlg.rst deleted file mode 100644 index b52d7ea7fe5e8d..00000000000000 --- a/Misc/NEWS.d/next/Tools-Demos/2026-10-21-11-53-40.gh-issue-87613.tmKBlg.rst +++ /dev/null @@ -1,2 +0,0 @@ -Extend Argument Clinic :ref:`vectorcall` to support :ref:`heap-types` with -zero argument construction. diff --git a/Misc/NEWS.d/next/WASI/2026-10-21-15-26-12.gh-issue-160135.UP-DlO.rst b/Misc/NEWS.d/next/WASI/2026-10-21-15-26-12.gh-issue-160135.UP-DlO.rst deleted file mode 100644 index 2f3b76136afd24..00000000000000 --- a/Misc/NEWS.d/next/WASI/2026-10-21-15-26-12.gh-issue-160135.UP-DlO.rst +++ /dev/null @@ -1 +0,0 @@ -Support wasm32-wasip3. diff --git a/Misc/NEWS.d/next/Windows/2026-07-29-13-05-00.gh-issue-140774.wA3ttr.rst b/Misc/NEWS.d/next/Windows/2026-07-29-13-05-00.gh-issue-140774.wA3ttr.rst new file mode 100644 index 00000000000000..aaf120efb992e7 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-07-29-13-05-00.gh-issue-140774.wA3ttr.rst @@ -0,0 +1,3 @@ +Fix :func:`os.chmod` with ``follow_symlinks=True``, :func:`os.fchmod` and +:meth:`pathlib.Path.chmod` on Windows: clearing the read-only attribute of a +file which has no other attribute set is no longer silently ignored. diff --git a/Misc/NEWS.d/next/Windows/2026-09-14-16-49-44.gh-issue-157418.r2IggA.rst b/Misc/NEWS.d/next/Windows/2026-09-14-16-49-44.gh-issue-157418.r2IggA.rst deleted file mode 100644 index b1a048c46ab83c..00000000000000 --- a/Misc/NEWS.d/next/Windows/2026-09-14-16-49-44.gh-issue-157418.r2IggA.rst +++ /dev/null @@ -1,3 +0,0 @@ -Correct the third parameter name of ``IocpProactor.recvfrom_into()`` from -*flags* to *nbytes* to reflect that it specifies the number of bytes to -receive. diff --git a/Misc/NEWS.d/next/Windows/2026-09-30-00-00-00.gh-issue-157917.48oa0L.rst b/Misc/NEWS.d/next/Windows/2026-09-30-00-00-00.gh-issue-157917.48oa0L.rst new file mode 100644 index 00000000000000..2d5e6a14a31d53 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-09-30-00-00-00.gh-issue-157917.48oa0L.rst @@ -0,0 +1,9 @@ +Fix :attr:`sys.prefix`, :attr:`sys.exec_prefix` and :attr:`sys.executable` +retaining the ``\\?\`` extended-length path prefix in a virtual environment +when the interpreter is invoked through such a path on Windows. The venv +launcher reports its own path through ``GetModuleFileNameW``, which preserves +the prefix, and that value was used verbatim. Windows does not resolve +relative path components in extended-length paths, so the leaked prefix made +tools that join ``..`` components onto :mod:`sysconfig` paths fail with +``OSError: [Errno 22]``. The prefix is now stripped again, except for paths +that are still too long to be used without it. diff --git a/Misc/NEWS.d/next/Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst b/Misc/NEWS.d/next/Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst new file mode 100644 index 00000000000000..dba12665deddd9 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-09-30-01-12-58.gh-issue-158414.738yJa.rst @@ -0,0 +1 @@ +Fix a hang when writing an incomplete UTF-8 sequence to the Windows console. diff --git a/Misc/NEWS.d/next/Windows/2026-10-15-18-45-00.gh-issue-159118.wmiMTA.rst b/Misc/NEWS.d/next/Windows/2026-10-15-18-45-00.gh-issue-159118.wmiMTA.rst deleted file mode 100644 index 561a5005b75975..00000000000000 --- a/Misc/NEWS.d/next/Windows/2026-10-15-18-45-00.gh-issue-159118.wmiMTA.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a crash (``RPC_NT_INTERNAL_ERROR``) and intermittent window class failures in :mod:`!_wmi` under high concurrency and load by initializing COM as multi-threaded (MTA) instead of apartment-threaded (STA). diff --git a/Misc/NEWS.d/next/Windows/2026-10-20-21-37-34.gh-issue-159880.eKBiLr.rst b/Misc/NEWS.d/next/Windows/2026-10-20-21-37-34.gh-issue-159880.eKBiLr.rst deleted file mode 100644 index ec3c28c4c4303a..00000000000000 --- a/Misc/NEWS.d/next/Windows/2026-10-20-21-37-34.gh-issue-159880.eKBiLr.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a key handle leak in :func:`winreg.CreateKey`, -:func:`winreg.CreateKeyEx` and :func:`winreg.OpenKey` when the -``winreg.OpenKey/result`` audit hook fails. diff --git a/Misc/NEWS.d/next/Windows/2026-10-22-14-04-17.gh-issue-160239.A1b2C3.rst b/Misc/NEWS.d/next/Windows/2026-10-22-14-04-17.gh-issue-160239.A1b2C3.rst deleted file mode 100644 index 6ca8aace90e676..00000000000000 --- a/Misc/NEWS.d/next/Windows/2026-10-22-14-04-17.gh-issue-160239.A1b2C3.rst +++ /dev/null @@ -1 +0,0 @@ -Remove dead and broken ``_repr_info()`` methods from ``asyncio.windows_events``; they called ``Future._repr_info()``, which no longer exists. diff --git a/Misc/NEWS.d/next/Windows/2026-11-11-10-44-20.gh-issue-161735.MZLRnD.rst b/Misc/NEWS.d/next/Windows/2026-11-11-10-44-20.gh-issue-161735.MZLRnD.rst new file mode 100644 index 00000000000000..a824533a8caa9d --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-11-11-10-44-20.gh-issue-161735.MZLRnD.rst @@ -0,0 +1 @@ +Updated bundled version of ``zlib-ng`` to v2.2.5. diff --git a/Misc/NEWS.d/next/macOS/2026-10-19-19-33-35.gh-issue-159739.aaf575.rst b/Misc/NEWS.d/next/macOS/2026-10-19-19-33-35.gh-issue-159739.aaf575.rst deleted file mode 100644 index 0e29daaa307e22..00000000000000 --- a/Misc/NEWS.d/next/macOS/2026-10-19-19-33-35.gh-issue-159739.aaf575.rst +++ /dev/null @@ -1 +0,0 @@ -Propagate errors that occur while converting macOS system proxy settings. diff --git a/Misc/externals.spdx.json b/Misc/externals.spdx.json index 0800347e34016d..b35ed3720943ab 100644 --- a/Misc/externals.spdx.json +++ b/Misc/externals.spdx.json @@ -175,42 +175,42 @@ "checksums": [ { "algorithm": "SHA256", - "checksumValue": "00bbd88709bc416cb96160ab61d3e1c8f76e106799af7328d0fe434dc7dd5004" + "checksumValue": "a9734c94080792af0b50d9c51c2cc8845c2f8ecfa4db87f6462a9b260af67f96" } ], - "downloadLocation": "https://github.com/python/cpython-source-deps/archive/refs/tags/zlib-ng-2.2.4.tar.gz", + "downloadLocation": "https://github.com/python/cpython-source-deps/archive/refs/tags/zlib-ng-2.2.5.tar.gz", "externalRefs": [ { "referenceCategory": "SECURITY", - "referenceLocator": "cpe:2.3:a:zlib-ng:zlib-ng:2.2.4:*:*:*:*:*:*:*", + "referenceLocator": "cpe:2.3:a:zlib-ng:zlib-ng:2.2.5:*:*:*:*:*:*:*", "referenceType": "cpe23Type" } ], "licenseConcluded": "NOASSERTION", "name": "zlib-ng", "primaryPackagePurpose": "SOURCE", - "versionInfo": "2.2.4" + "versionInfo": "2.2.5" }, { "SPDXID": "SPDXRef-PACKAGE-zstd", "checksums": [ { "algorithm": "SHA256", - "checksumValue": "f24b52470d12f466e9fa4fcc94e6c530625ada51d7b36de7fdc6ed7e6f499c8e" + "checksumValue": "eb34eed79af88b8dd7c07481d55192520b9545f865416c31c828b96b01d335d3" } ], - "downloadLocation": "https://github.com/python/cpython-source-deps/archive/refs/tags/zstd-1.5.7.tar.gz", + "downloadLocation": "https://github.com/python/cpython-source-deps/archive/refs/tags/zstd-1.6.0.tar.gz", "externalRefs": [ { "referenceCategory": "SECURITY", - "referenceLocator": "cpe:2.3:a:facebook:zstandard:1.5.7:*:*:*:*:*:*:*", + "referenceLocator": "cpe:2.3:a:facebook:zstandard:1.6.0:*:*:*:*:*:*:*", "referenceType": "cpe23Type" } ], "licenseConcluded": "NOASSERTION", "name": "zstd", "primaryPackagePurpose": "SOURCE", - "versionInfo": "1.5.7" + "versionInfo": "1.6.0" } ], "spdxVersion": "SPDX-2.3" diff --git a/Misc/sbom.spdx.json b/Misc/sbom.spdx.json index 68630ad7b45dec..225f2a5d186389 100644 --- a/Misc/sbom.spdx.json +++ b/Misc/sbom.spdx.json @@ -62,11 +62,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "9a53ad014b4be691f12e9011175117e7dc5cdcf8" + "checksumValue": "2037910f6aacebac08a376d5c2b88cdba7309864" }, { "algorithm": "SHA256", - "checksumValue": "71be1f9e24a8edf1c27038a28441c947c3d75f19cf36d3a17ce59f8bb7298317" + "checksumValue": "8dfdb5b7d0392391e74cd9cdb74455fcbebfdcfa75c5d7c6ac59fb5855af31fe" } ], "fileName": "Modules/expat/expat.h" @@ -132,11 +132,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "a99cd29b07e030956bda16e9bf11dee5166417e2" + "checksumValue": "df8adcb0ea779b2b720d84a687684a08f3bf72c8" }, { "algorithm": "SHA256", - "checksumValue": "2a2821fc7a2982978a8630e26cf3b8158ab21a2c2f2d06354f8c0c6547dded44" + "checksumValue": "959b292f428b4ba899a620ae9c214e5a731396982aa445a997705bd8a420a6c8" } ], "fileName": "Modules/expat/internal.h" @@ -146,11 +146,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "8d31198775c89582e37cd0dcac4e87a718cc17cb" + "checksumValue": "2e37879c51aec484072d504d89e963bda5b553e5" }, { "algorithm": "SHA256", - "checksumValue": "b0daf8a75c690e895421f765ddb2ae2fbc1ba14694e06a3215dba84f1ff3427d" + "checksumValue": "dfc8d79ebdb977a3fd760591c6a92b7c7ae1eb76cd79003ba386015bb92c2b3d" } ], "fileName": "Modules/expat/latin1tab.h" @@ -174,11 +174,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "19ecc1cf2a899fe1b80de263c8b09d6de451c6b8" + "checksumValue": "737800daf4e21db84a3c4ff2a0578b6fb3c3b3ad" }, { "algorithm": "SHA256", - "checksumValue": "8f78e64f11e2b23df8b32cb5528cfeda6215acd6abf6bcf1ad59fdc7b1ffd105" + "checksumValue": "19a2e7c6ed53a66df77bcc1576a21f20e729f74997566347c050b97fd6244c95" } ], "fileName": "Modules/expat/nametab.h" @@ -258,11 +258,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "12420b9bec13114125947340555120b02f074288" + "checksumValue": "0d49c8bd830acb84dcf87e3c156d980680dadc78" }, { "algorithm": "SHA256", - "checksumValue": "47a6dafa356754fada8a53cf4fba7f9fbc1bcaf374c6f5f81aa37de5521e17d4" + "checksumValue": "c5b0e430fd8e9e9a6b0fe8a3bf3bfc1ebfbedf50a366f923110dcddaabae4c88" } ], "fileName": "Modules/expat/xmlparse.c" @@ -300,11 +300,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "1a98bb41885c1795b5d66da1cd8ab4babc645670" + "checksumValue": "6a5bca82fb56189cc67136d17c9236c16999cdf2" }, { "algorithm": "SHA256", - "checksumValue": "44baab2a4970749f6f29992bbbc1b3c62239ef9ec2a20de96332ef4659b9e32d" + "checksumValue": "14cc3ff861c70528c7807a1fccf794c7e0c5c00185462e5066092e61e3e1d23c" } ], "fileName": "Modules/expat/xmltok.c" @@ -314,11 +314,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "aa4a4a6b024455f1475535b79f34dfcd9230ac8c" + "checksumValue": "d17abcb8cdf7e91fd2bc57ca692ead71cb81f464" }, { "algorithm": "SHA256", - "checksumValue": "83dcfa0416240e070eee2217703faf9d28aa2c56372e307e712b7ee80520d9de" + "checksumValue": "25c3d829eab7bcd5b64bafca3bdd1cc368bc71c7b8ebdae757671e4f641b5e64" } ], "fileName": "Modules/expat/xmltok.h" @@ -328,11 +328,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "ad7531e46b116d1f9f29c579cf701161d55eea27" + "checksumValue": "e7da04f93f0bc0268b881da7b20d5a9ea6f000da" }, { "algorithm": "SHA256", - "checksumValue": "dd11f257be67c2a069a91774bcf16acc2fd5052f9abf50d0819b0acb28d4803c" + "checksumValue": "2e0e61c0125f165c98b310888c642e36b49068a33b93662e5989a07094cb20bc" } ], "fileName": "Modules/expat/xmltok_impl.c" @@ -356,11 +356,11 @@ "checksums": [ { "algorithm": "SHA1", - "checksumValue": "b2dd2b7fcb57fcecd95679b5b53cc92256eb0190" + "checksumValue": "0fc32be2ca285ea118dfafe1a2534d4dc1ebfc4f" }, { "algorithm": "SHA256", - "checksumValue": "ea723a3470d3eed5c4a4e873bec2f996c605326fad25709b45452664261c5465" + "checksumValue": "d2b1a0c17a8113d3323331a8acd980f22c1e8de316dff27d7fce23a6069e1487" } ], "fileName": "Modules/expat/xmltok_ns.c" @@ -1086,14 +1086,14 @@ "checksums": [ { "algorithm": "SHA256", - "checksumValue": "5466a341f9c692ef7a958a6881311be597f8e1ce345984cf78414f54793dea4c" + "checksumValue": "b3e3fcbb629e4d357a9cc0af5b146b85959bcf1cefe87e529d2c0a9234be737f" } ], - "downloadLocation": "https://github.com/libexpat/libexpat/releases/download/R_2_10_0/expat-2.10.0.tar.gz", + "downloadLocation": "https://github.com/libexpat/libexpat/releases/download/R_2_11_0/expat-2.11.0.tar.gz", "externalRefs": [ { "referenceCategory": "SECURITY", - "referenceLocator": "cpe:2.3:a:libexpat_project:libexpat:2.10.0:*:*:*:*:*:*:*", + "referenceLocator": "cpe:2.3:a:libexpat_project:libexpat:2.11.0:*:*:*:*:*:*:*", "referenceType": "cpe23Type" } ], @@ -1101,7 +1101,7 @@ "name": "expat", "originator": "Organization: Expat development team", "primaryPackagePurpose": "SOURCE", - "versionInfo": "2.10.0" + "versionInfo": "2.11.0" }, { "SPDXID": "SPDXRef-PACKAGE-hacl-star", diff --git a/Modules/Setup.bootstrap.in b/Modules/Setup.bootstrap.in index 65a1fefe72e92e..1d1de43fd8f05b 100644 --- a/Modules/Setup.bootstrap.in +++ b/Modules/Setup.bootstrap.in @@ -1,5 +1,9 @@ # -*- makefile -*- +# Mark bootstrap modules explicitly so older Makefile recipes work +# without passing -b to makesetup (gh-161364). +*bootstrap* + # --- # Built-in modules required to get a functioning interpreter; # cannot be built as shared! diff --git a/Modules/_abc.c b/Modules/_abc.c index 054cff517115f5..79904513c49b64 100644 --- a/Modules/_abc.c +++ b/Modules/_abc.c @@ -4,11 +4,17 @@ #endif #include "Python.h" -#include "pycore_moduleobject.h" // _PyModule_GetState() -#include "pycore_object.h" // _PyType_GetSubclasses() -#include "pycore_runtime.h" // _Py_ID() -#include "pycore_setobject.h" // _PySet_NextEntry() -#include "pycore_weakref.h" // _PyWeakref_GET_REF() +#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() +#include "pycore_moduleobject.h" // _PyModule_GetState() +#include "pycore_object.h" // _PyType_LookupStackRefAndVersion() +#include "pycore_object_deferred.h" // _PyObject_SetDeferredRefcount() +#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_ACQUIRE() +#include "pycore_pymem.h" // _PyMem_FreeDelayed() +#include "pycore_pystate.h" // _PyThreadState_GET() +#include "pycore_runtime.h" // _Py_ID() +#include "pycore_stackref.h" // _PyStackRef +#include "pycore_typeobject.h" // _PyType_GetSubclasses() +#include "pycore_pyhash.h" #include "clinic/_abc.c.h" /*[clinic input] @@ -22,6 +28,14 @@ PyDoc_STRVAR(_abc__doc__, typedef struct { PyTypeObject *_abc_data_type; uint64_t abc_invalidation_counter; + // Default implementations, compared by identity to skip the call + // when a class doesn't override them. + PyObject *object_class_descr; + PyObject *object_subclasshook; + PyObject *type_subclasses; + // ABCMeta.__subclasscheck__, set by abc.py on import. Classes using it + // are checked directly instead of going through issubclass(). + PyObject *subclasscheck_func; } _abcmodule_state; static inline _abcmodule_state* @@ -35,57 +49,288 @@ get_abc_state(PyObject *module) static inline uint64_t get_invalidation_counter(_abcmodule_state *state) { -#ifdef Py_GIL_DISABLED - return _Py_atomic_load_uint64(&state->abc_invalidation_counter); -#else - return state->abc_invalidation_counter; -#endif + return FT_ATOMIC_LOAD_UINT64_RELAXED(state->abc_invalidation_counter); } static inline void increment_invalidation_counter(_abcmodule_state *state) { -#ifdef Py_GIL_DISABLED - _Py_atomic_add_uint64(&state->abc_invalidation_counter, 1); -#else - state->abc_invalidation_counter++; -#endif + FT_ATOMIC_ADD_UINT64(state->abc_invalidation_counter, 1); +} + + +// The registry and the caches of an ABC are sets of classes, stored as +// open addressed hash tables keyed by the address of the class. +// +// Each entry also holds a weakref to the class. It is only used to check +// that the class is still alive, as the address of a dead class can be +// reused by a new one. The weakrefs have no callback, so the same weakref +// is shared by all the tables a class is in, and dead entries are only +// removed when the table is rebuilt. +typedef struct { + PyObject *key; /* borrowed ref of a class */ + PyObject *ref; /* weakref to key */ +} abc_entry; + +typedef struct { + size_t mask; /* number of entries - 1 */ + size_t used; /* entries with a key, live or dead */ + abc_entry entries[]; +} abc_table; + +#define TABLE_MIN_SIZE 8 +#define PERTURB_SHIFT 5 + +// Shared empty table, so that lookups don't need a NULL check. +// It is never modified or freed. +static const abc_table empty_table_struct = { + 0, /* mask */ + 0, /* used */ + {{NULL, NULL}}, /* entries */ +}; + +#define EMPTY_TABLE ((abc_table *)&empty_table_struct) + +static inline size_t +abc_table_nbytes(size_t size) +{ + return offsetof(abc_table, entries) + size * sizeof(abc_entry); +} + +static abc_table * +abc_table_new(size_t size) +{ + assert((size & (size - 1)) == 0 && size >= TABLE_MIN_SIZE); + abc_table *table = PyMem_Calloc(1, abc_table_nbytes(size)); + if (table == NULL) { + PyErr_NoMemory(); + return NULL; + } + table->mask = size - 1; + table->used = 0; + return table; +} + +// Free a table and its weakrefs. Use delayed if other threads +// can still be reading the table. +static void +abc_table_release(abc_table *table, int delayed) +{ + if (table == EMPTY_TABLE) { + return; + } + for (size_t i = 0; i <= table->mask; i++) { + PyObject *ref = table->entries[i].ref; + if (delayed) { + _PyObject_XDecRefDelayed(ref); + } + else { + Py_XDECREF(ref); + } + } + if (delayed) { + _PyMem_FreeDelayed(table, abc_table_nbytes(table->mask + 1)); + } + else { + PyMem_Free(table); + } +} + +static inline size_t +abc_hash(PyObject *key) +{ + return (size_t)_Py_HashPointerRaw(key); +} + +static inline int +abc_entry_is_live(PyObject *ref, PyObject *key) +{ + // The caller holds a strong reference to key, so a weakref pointing + // to key cannot be of a dead class which was at the same address. + // wr_object only changes when key dies, so a relaxed load is enough. + assert(ref != NULL); + PyWeakReference *wr = (PyWeakReference *)ref; + return FT_ATOMIC_LOAD_PTR_RELAXED(wr->wr_object) == key; +} + +static abc_entry * +abc_table_find(abc_table *table, PyObject *key, PyObject **found) +{ + size_t mask = table->mask; + size_t perturb = abc_hash(key); + size_t i = perturb & mask; + for (;;) { + abc_entry *entry = &table->entries[i]; + PyObject *k = FT_ATOMIC_LOAD_PTR_ACQUIRE(entry->key); + if (k == NULL || k == key) { + *found = k; + return entry; + } + perturb >>= PERTURB_SHIFT; + i = (i * 5 + perturb + 1) & mask; + } +} + +// Lock-free, the caller must hold a strong reference to key. +static int +abc_table_contains(abc_table *table, PyObject *key) +{ + PyObject *found; + abc_entry *entry = abc_table_find(table, key, &found); + if (found == NULL) { + return 0; + } + PyObject *ref = FT_ATOMIC_LOAD_PTR_ACQUIRE(entry->ref); + return abc_entry_is_live(ref, key); +} + +static abc_table * +abc_table_rebuild(abc_table *old, size_t extra) +{ + size_t live = 0; + for (size_t i = 0; i <= old->mask; i++) { + abc_entry *entry = &old->entries[i]; + if (entry->key != NULL && abc_entry_is_live(entry->ref, entry->key)) { + live++; + } + } + size_t size = TABLE_MIN_SIZE; + while (size < (live + extra) * 2) { + size <<= 1; + } + abc_table *table = abc_table_new(size); + if (table == NULL) { + return NULL; + } + for (size_t i = 0; i <= old->mask; i++) { + abc_entry *entry = &old->entries[i]; + if (entry->key == NULL) { + continue; + } + if (abc_entry_is_live(entry->ref, entry->key)) { + PyObject *found; + abc_entry *dst = abc_table_find(table, entry->key, &found); + assert(found == NULL); + dst->key = entry->key; + dst->ref = entry->ref; + table->used++; + } + else { + _PyObject_XDecRefDelayed(entry->ref); + } + } + return table; +} + +static int +abc_table_add(abc_table **ptable, PyObject *key, PyObject *ref) +{ + abc_table *table = *ptable; + PyObject *found; + abc_entry *entry = abc_table_find(table, key, &found); + if (found == key) { + // The existing weakref can be of a dead class at the same address. + PyObject *old = entry->ref; + if (old == ref) { + Py_DECREF(ref); + } + else { + FT_ATOMIC_STORE_PTR_RELEASE(entry->ref, ref); + _PyObject_XDecRefDelayed(old); + } + return 0; + } + // Always true for EMPTY_TABLE. + if ((table->used + 1) * 3 > (table->mask + 1) * 2) { + abc_table *newtable = abc_table_rebuild(table, 1); + if (newtable == NULL) { + Py_DECREF(ref); + return -1; + } + FT_ATOMIC_STORE_PTR_RELEASE(*ptable, newtable); + if (table != EMPTY_TABLE) { + // The weakrefs were moved to newtable. + _PyMem_FreeDelayed(table, abc_table_nbytes(table->mask + 1)); + } + table = newtable; + entry = abc_table_find(table, key, &found); + } + assert(found == NULL); + FT_ATOMIC_STORE_PTR_RELAXED(entry->ref, ref); + FT_ATOMIC_STORE_PTR_RELEASE(entry->key, key); + table->used++; + return 0; +} + +static void +abc_table_clear(abc_table **ptable) +{ + abc_table *table = *ptable; + if (table != EMPTY_TABLE) { + FT_ATOMIC_STORE_PTR_RELAXED(*ptable, EMPTY_TABLE); + abc_table_release(table, 1); + } +} + +static PyObject * +abc_table_snapshot(abc_table *table) +{ + PyObject *list = PyList_New(0); + if (list == NULL) { + return NULL; + } + for (size_t i = 0; i <= table->mask; i++) { + abc_entry *entry = &table->entries[i]; + if (entry->key == NULL) { + continue; + } + PyObject *obj; + if (PyWeakref_GetRef(entry->ref, &obj) < 0) { + Py_DECREF(list); + return NULL; + } + if (obj == NULL) { + continue; + } + int rc = PyList_Append(list, obj); + Py_DECREF(obj); + if (rc < 0) { + Py_DECREF(list); + return NULL; + } + } + return list; } -/* This object stores internal state for ABCs. - Note that we can use normal sets for caches, - since they are never iterated over. */ + +// Internal state of an ABC, stored in its _abc_impl attribute. + typedef struct { PyObject_HEAD - /* These sets of weak references are lazily created. Once created, they - will point to the same sets until the ABCMeta object is destroyed or - cleared, both of which will only happen while the object is visible to a - single thread. */ - PyObject *_abc_registry; - PyObject *_abc_cache; - PyObject *_abc_negative_cache; - uint64_t _abc_negative_cache_version; + abc_table *registry; + abc_table *cache; + abc_table *negative_cache; + uint64_t negative_cache_version; } _abc_data; #define _abc_data_CAST(op) ((_abc_data *)(op)) +// The version is updated after the negative cache is cleared and read +// before the negative cache is, so a reader which sees the new version +// never sees the old entries. static inline uint64_t get_cache_version(_abc_data *impl) { -#ifdef Py_GIL_DISABLED - return _Py_atomic_load_uint64(&impl->_abc_negative_cache_version); -#else - return impl->_abc_negative_cache_version; -#endif + return FT_ATOMIC_LOAD_UINT64_ACQUIRE(impl->negative_cache_version); } static inline void set_cache_version(_abc_data *impl, uint64_t version) { #ifdef Py_GIL_DISABLED - _Py_atomic_store_uint64(&impl->_abc_negative_cache_version, version); + _Py_atomic_store_uint64_release(&impl->negative_cache_version, version); #else - impl->_abc_negative_cache_version = version; + impl->negative_cache_version = version; #endif } @@ -93,10 +338,9 @@ static int abc_data_traverse(PyObject *op, visitproc visit, void *arg) { _abc_data *self = _abc_data_CAST(op); + // The tables only hold weakrefs without callbacks which cannot + // be part of a reference cycle. Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->_abc_registry); - Py_VISIT(self->_abc_cache); - Py_VISIT(self->_abc_negative_cache); return 0; } @@ -104,9 +348,15 @@ static int abc_data_clear(PyObject *op) { _abc_data *self = _abc_data_CAST(op); - Py_CLEAR(self->_abc_registry); - Py_CLEAR(self->_abc_cache); - Py_CLEAR(self->_abc_negative_cache); + abc_table *registry = self->registry; + abc_table *cache = self->cache; + abc_table *negative_cache = self->negative_cache; + self->registry = EMPTY_TABLE; + self->cache = EMPTY_TABLE; + self->negative_cache = EMPTY_TABLE; + abc_table_release(registry, 0); + abc_table_release(cache, 0); + abc_table_release(negative_cache, 0); return 0; } @@ -135,10 +385,12 @@ abc_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; } - self->_abc_registry = NULL; - self->_abc_cache = NULL; - self->_abc_negative_cache = NULL; - self->_abc_negative_cache_version = get_invalidation_counter(state); + self->registry = EMPTY_TABLE; + self->cache = EMPTY_TABLE; + self->negative_cache = EMPTY_TABLE; + self->negative_cache_version = get_invalidation_counter(state); + // Avoid refcount contention between threads. + _PyObject_SetDeferredRefcount((PyObject *)self); return (PyObject *) self; } @@ -162,100 +414,135 @@ static PyType_Spec _abc_data_type_spec = { }; static _abc_data * -_get_impl(PyObject *module, PyObject *self) +lookup_impl(_abcmodule_state *state, PyObject *self, _PyCStackRef *cref) { - _abcmodule_state *state = get_abc_state(module); - PyObject *impl = PyObject_GetAttr(self, &_Py_ID(_abc_impl)); - if (impl == NULL) { + if (!PyType_Check(self)) { return NULL; } + _PyType_LookupStackRefAndVersion((PyTypeObject *)self, &_Py_ID(_abc_impl), + &cref->ref); + if (PyStackRef_IsNull(cref->ref)) { + return NULL; + } + PyObject *impl = PyStackRef_AsPyObjectBorrow(cref->ref); if (!Py_IS_TYPE(impl, state->_abc_data_type)) { - PyErr_SetString(PyExc_TypeError, "_abc_impl is set to a wrong type"); - Py_DECREF(impl); + PyStackRef_CLOSE(cref->ref); + cref->ref = PyStackRef_NULL; return NULL; } return (_abc_data *)impl; } +// Like lookup_impl() but falls back to a full attribute lookup, +// and sets an exception on failure. +static _abc_data * +get_impl(_abcmodule_state *state, PyObject *self, _PyCStackRef *cref) +{ + _abc_data *impl = lookup_impl(state, self, cref); + if (impl != NULL) { + return impl; + } + cref->ref = _PyObject_GetAttrStackRef(self, &_Py_ID(_abc_impl)); + if (PyStackRef_IsNull(cref->ref)) { + return NULL; + } + impl = (_abc_data *)PyStackRef_AsPyObjectBorrow(cref->ref); + if (!Py_IS_TYPE(impl, state->_abc_data_type)) { + PyErr_SetString(PyExc_TypeError, "_abc_impl is set to a wrong type"); + PyStackRef_CLOSE(cref->ref); + cref->ref = PyStackRef_NULL; + return NULL; + } + return impl; +} + static int -_in_weak_set(_abc_data *impl, PyObject **pset, PyObject *obj) +type_lookup_is(PyTypeObject *type, PyObject *name, PyObject *expected) { - PyObject *set; - Py_BEGIN_CRITICAL_SECTION(impl); - set = *pset; - Py_END_CRITICAL_SECTION(); - if (set == NULL || PySet_GET_SIZE(set) == 0) { - return 0; + _PyStackRef ref; + _PyType_LookupStackRefAndVersion(type, name, &ref); + if (PyStackRef_IsNull(ref)) { + return expected == NULL; } - PyObject *ref = PyWeakref_NewRef(obj, NULL); + int res = (PyStackRef_AsPyObjectBorrow(ref) == expected); + PyStackRef_CLOSE(ref); + return res; +} + +static int +add_to_table(_abc_data *impl, abc_table **ptable, PyObject *cls) +{ + // PyWeakref_NewRef() can lock cls, call it outside the critical section. + PyObject *ref = PyWeakref_NewRef(cls, NULL); if (ref == NULL) { - if (PyErr_ExceptionMatches(PyExc_TypeError)) { - PyErr_Clear(); - return 0; - } return -1; } - int res = PySet_Contains(set, ref); - Py_DECREF(ref); + int res; + Py_BEGIN_CRITICAL_SECTION(impl); + res = abc_table_add(ptable, cls, ref); + Py_END_CRITICAL_SECTION(); return res; } -static PyObject * -_destroy(PyObject *setweakref, PyObject *objweakref) +// Add cls to the negative cache if it is still valid for counter. +static int +add_to_negative_cache(_abc_data *impl, PyObject *cls, uint64_t counter) { - PyObject *set = _PyWeakref_GET_REF(setweakref); - if (set == NULL) { - Py_RETURN_NONE; + PyObject *ref = PyWeakref_NewRef(cls, NULL); + if (ref == NULL) { + return -1; } - if (PySet_Discard(set, objweakref) < 0) { - Py_DECREF(set); - return NULL; + int res = 0; + Py_BEGIN_CRITICAL_SECTION(impl); + if (impl->negative_cache_version == counter) { + res = abc_table_add(&impl->negative_cache, cls, ref); } - Py_DECREF(set); - Py_RETURN_NONE; + else { + Py_DECREF(ref); + } + Py_END_CRITICAL_SECTION(); + return res; } -static PyMethodDef _destroy_def = { - "_destroy", _destroy, METH_O -}; +typedef enum { + CACHE_UNKNOWN = -1, // not cached or stale + CACHE_NEGATIVE = 0, // in the negative cache + CACHE_POSITIVE = 1, // in the positive cache +} cache_result; -static int -_add_to_weak_set(_abc_data *impl, PyObject **pset, PyObject *obj) +static cache_result +lookup_caches(_abcmodule_state *state, _abc_data *impl, PyObject *subclass) { - PyObject *set; - Py_BEGIN_CRITICAL_SECTION(impl); - set = *pset; - if (set == NULL) { - set = *pset = PySet_New(NULL); - } - Py_END_CRITICAL_SECTION(); - if (set == NULL) { - return -1; + abc_table *table = FT_ATOMIC_LOAD_PTR_ACQUIRE(impl->cache); + if (abc_table_contains(table, subclass)) { + return CACHE_POSITIVE; + } + if (get_cache_version(impl) == get_invalidation_counter(state)) { + table = FT_ATOMIC_LOAD_PTR_ACQUIRE(impl->negative_cache); + if (abc_table_contains(table, subclass)) { + return CACHE_NEGATIVE; + } } + return CACHE_UNKNOWN; +} - PyObject *ref, *wr; - PyObject *destroy_cb; - wr = PyWeakref_NewRef(set, NULL); - if (wr == NULL) { - return -1; - } - destroy_cb = PyCFunction_NewEx(&_destroy_def, wr, NULL); - if (destroy_cb == NULL) { - Py_DECREF(wr); - return -1; +// Clear the negative cache if register() was called since it was last +// validated. +static void +validate_negative_cache(_abc_data *impl, uint64_t counter) +{ + if (get_cache_version(impl) >= counter) { + return; } - ref = PyWeakref_NewRef(obj, destroy_cb); - Py_DECREF(destroy_cb); - if (ref == NULL) { - Py_DECREF(wr); - return -1; + Py_BEGIN_CRITICAL_SECTION(impl); + if (impl->negative_cache_version < counter) { + abc_table_clear(&impl->negative_cache); + set_cache_version(impl, counter); } - int ret = PySet_Add(set, ref); - Py_DECREF(wr); - Py_DECREF(ref); - return ret; + Py_END_CRITICAL_SECTION(); } + /*[clinic input] _abc._reset_registry @@ -271,19 +558,22 @@ static PyObject * _abc__reset_registry(PyObject *module, PyObject *self) /*[clinic end generated code: output=92d591a43566cc10 input=12a0b7eb339ac35c]*/ { - _abc_data *impl = _get_impl(module, self); - if (impl == NULL) { - return NULL; + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _abc_data *impl = get_impl(get_abc_state(module), self, &cref); + if (impl != NULL) { + Py_BEGIN_CRITICAL_SECTION(impl); + abc_table_clear(&impl->registry); + // Registered classes are also in the cache, and register() + // does nothing for a class which is cached as a subclass. + abc_table_clear(&impl->cache); + Py_END_CRITICAL_SECTION(); } - PyObject *registry; - Py_BEGIN_CRITICAL_SECTION(impl); - registry = impl->_abc_registry; - Py_END_CRITICAL_SECTION(); - if (registry != NULL && PySet_Clear(registry) < 0) { - Py_DECREF(impl); + _PyThreadState_PopCStackRef(tstate, &cref); + if (impl == NULL) { return NULL; } - Py_DECREF(impl); Py_RETURN_NONE; } @@ -302,26 +592,44 @@ static PyObject * _abc__reset_caches(PyObject *module, PyObject *self) /*[clinic end generated code: output=f296f0d5c513f80c input=c0ac616fd8acfb6f]*/ { - _abc_data *impl = _get_impl(module, self); + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _abc_data *impl = get_impl(get_abc_state(module), self, &cref); + if (impl != NULL) { + Py_BEGIN_CRITICAL_SECTION(impl); + abc_table_clear(&impl->cache); + abc_table_clear(&impl->negative_cache); + Py_END_CRITICAL_SECTION(); + } + _PyThreadState_PopCStackRef(tstate, &cref); if (impl == NULL) { return NULL; } - PyObject *cache, *negative_cache; - Py_BEGIN_CRITICAL_SECTION(impl); - cache = impl->_abc_cache; - negative_cache = impl->_abc_negative_cache; - Py_END_CRITICAL_SECTION(); - if (cache != NULL && PySet_Clear(cache) < 0) { - Py_DECREF(impl); + Py_RETURN_NONE; +} + +static PyObject * +weakref_set_from_list(PyObject *list) +{ + PyObject *set = PySet_New(NULL); + if (set == NULL) { return NULL; } - /* also the second cache */ - if (negative_cache != NULL && PySet_Clear(negative_cache) < 0) { - Py_DECREF(impl); - return NULL; + for (Py_ssize_t i = 0; i < PyList_GET_SIZE(list); i++) { + PyObject *ref = PyWeakref_NewRef(PyList_GET_ITEM(list, i), NULL); + if (ref == NULL) { + Py_DECREF(set); + return NULL; + } + int rc = PySet_Add(set, ref); + Py_DECREF(ref); + if (rc < 0) { + Py_DECREF(set); + return NULL; + } } - Py_DECREF(impl); - Py_RETURN_NONE; + return set; } /*[clinic input] @@ -341,19 +649,41 @@ static PyObject * _abc__get_dump(PyObject *module, PyObject *self) /*[clinic end generated code: output=9d9569a8e2c1c443 input=2c5deb1bfe9e3c79]*/ { - _abc_data *impl = _get_impl(module, self); + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _abc_data *impl = get_impl(get_abc_state(module), self, &cref); if (impl == NULL) { + _PyThreadState_PopCStackRef(tstate, &cref); return NULL; } - PyObject *res; + PyObject *lists[3] = {NULL, NULL, NULL}; + PyObject *sets[3] = {NULL, NULL, NULL}; + PyObject *res = NULL; + uint64_t version; Py_BEGIN_CRITICAL_SECTION(impl); - res = Py_BuildValue("NNNK", - PySet_New(impl->_abc_registry), - PySet_New(impl->_abc_cache), - PySet_New(impl->_abc_negative_cache), - get_cache_version(impl)); + lists[0] = abc_table_snapshot(impl->registry); + lists[1] = abc_table_snapshot(impl->cache); + lists[2] = abc_table_snapshot(impl->negative_cache); + version = get_cache_version(impl); Py_END_CRITICAL_SECTION(); - Py_DECREF(impl); + _PyThreadState_PopCStackRef(tstate, &cref); + for (int i = 0; i < 3; i++) { + if (lists[i] == NULL) { + goto done; + } + sets[i] = weakref_set_from_list(lists[i]); + if (sets[i] == NULL) { + goto done; + } + } + res = Py_BuildValue("OOOK", sets[0], sets[1], sets[2], + (unsigned long long)version); +done: + for (int i = 0; i < 3; i++) { + Py_XDECREF(lists[i]); + Py_XDECREF(sets[i]); + } return res; } @@ -580,18 +910,20 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass) if (result < 0) { return NULL; } - _abc_data *impl = _get_impl(module, self); - if (impl == NULL) { - return NULL; - } - if (_add_to_weak_set(impl, &impl->_abc_registry, subclass) < 0) { - Py_DECREF(impl); + _abcmodule_state *state = get_abc_state(module); + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _abc_data *impl = get_impl(state, self, &cref); + int err = (impl == NULL + || add_to_table(impl, &impl->registry, subclass) < 0); + _PyThreadState_PopCStackRef(tstate, &cref); + if (err) { return NULL; } - Py_DECREF(impl); /* Invalidate negative cache */ - increment_invalidation_counter(get_abc_state(module)); + increment_invalidation_counter(state); /* Set Py_TPFLAGS_SEQUENCE or Py_TPFLAGS_MAPPING flag */ if (PyType_Check(self)) { @@ -607,6 +939,16 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass) } +// True if instance.__class__ is type(instance) and the lookup +// doesn't run any Python code. +static inline int +has_default_class_attr(_abcmodule_state *state, PyObject *instance) +{ + PyTypeObject *tp = Py_TYPE(instance); + return tp->tp_getattro == PyObject_GenericGetAttr + && type_lookup_is(tp, &_Py_ID(__class__), state->object_class_descr); +} + /*[clinic input] @permit_long_summary _abc._abc_instancecheck @@ -623,52 +965,52 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self, PyObject *instance) /*[clinic end generated code: output=b8b5148f63b6b56f input=0bbc8da0ea346719]*/ { - PyObject *subtype, *result = NULL, *subclass = NULL; - _abc_data *impl = _get_impl(module, self); - if (impl == NULL) { - return NULL; - } + _abcmodule_state *state = get_abc_state(module); + PyObject *subclass_owned = NULL; + PyObject *result = NULL; - if (PyObject_GetOptionalAttr(instance, &_Py_ID(__class__), &subclass) < 0) { - Py_DECREF(impl); - return NULL; - } - if (subclass == NULL) { - /* Fall back to the type when the instance has no __class__, matching - the behaviour of the built-in isinstance() (gh-153772). */ - subclass = Py_NewRef((PyObject *)Py_TYPE(instance)); - } - /* Inline the cache checking. */ - int incache = _in_weak_set(impl, &impl->_abc_cache, subclass); - if (incache < 0) { - goto end; + if (!has_default_class_attr(state, instance)) { + if (PyObject_GetOptionalAttr(instance, &_Py_ID(__class__), + &subclass_owned) < 0) { + return NULL; + } } - if (incache > 0) { - result = Py_NewRef(Py_True); - goto end; + // The __class__ lookup can change the type of the instance. + PyObject *subtype = (PyObject *)Py_TYPE(instance); + // subclass is either subtype, which is kept alive by instance, or + // subclass_owned. The fast path doesn't incref subtype to avoid + // refcount contention between threads. + /* Fall back to the type when the instance has no __class__, + matching the behaviour of the built-in isinstance() + (gh-153772). */ + PyObject *subclass = subclass_owned != NULL ? subclass_owned : subtype; + + // Fast path: cache hits don't need any locking. + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _abc_data *impl = get_impl(state, self, &cref); + cache_result incache = CACHE_UNKNOWN; + if (impl != NULL) { + incache = lookup_caches(state, impl, subclass); + } + _PyThreadState_PopCStackRef(tstate, &cref); + if (impl == NULL) { + Py_XDECREF(subclass_owned); + return NULL; } - subtype = (PyObject *)Py_TYPE(instance); - if (subtype == subclass) { - if (get_cache_version(impl) == get_invalidation_counter(get_abc_state(module))) { - incache = _in_weak_set(impl, &impl->_abc_negative_cache, subclass); - if (incache < 0) { - goto end; - } - if (incache > 0) { - result = Py_NewRef(Py_False); - goto end; - } - } - /* Fall back to the subclass check. */ - result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__), - subclass); - goto end; + if (incache == CACHE_POSITIVE + || (incache == CACHE_NEGATIVE && subtype == subclass)) + { + Py_XDECREF(subclass_owned); + return PyBool_FromLong(incache); } + /* A subclass check can change the instance's actual type. */ Py_INCREF(subtype); result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__), subclass); - if (result == NULL) { + if (result == NULL || subtype == subclass) { Py_DECREF(subtype); goto end; } @@ -690,232 +1032,262 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self, Py_DECREF(subtype); end: - Py_XDECREF(impl); - Py_XDECREF(subclass); + Py_XDECREF(subclass_owned); return result; } -// Return -1 when exception occurred. -// Return 1 when result is set. -// Return 0 otherwise. -static int subclasscheck_check_registry(_abc_data *impl, PyObject *subclass, - PyObject **result); +static int subclasscheck(_abcmodule_state *state, _abc_data *impl, + PyObject *self, PyObject *subclass, int nested); -/*[clinic input] -@permit_long_summary -_abc._abc_subclasscheck - - self: object - subclass: object - / - -Internal ABC helper for subclasss checks. Should be never used outside abc module. -[clinic start generated code]*/ - -static PyObject * -_abc__abc_subclasscheck_impl(PyObject *module, PyObject *self, - PyObject *subclass) -/*[clinic end generated code: output=b56c9e4a530e3894 input=5bf1ef712f5d3610]*/ +// Like PyObject_IsSubclass(subclass, cls), but doesn't add subclass to the +// negative cache of cls if cls uses the default ABCMeta.__subclasscheck__. +static int +issubclass_nested(_abcmodule_state *state, PyObject *subclass, PyObject *cls) { - if (!PyType_Check(subclass)) { - PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class"); - return NULL; + assert(PyType_Check(cls)); + if (type_lookup_is(Py_TYPE(cls), &_Py_ID(__subclasscheck__), + state->subclasscheck_func)) + { + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _abc_data *impl = lookup_impl(state, cls, &cref); + int res = -1; + if (impl != NULL) { + if (!Py_EnterRecursiveCall(" in __subclasscheck__")) { + res = subclasscheck(state, impl, cls, subclass, 1); + Py_LeaveRecursiveCall(); + } + } + _PyThreadState_PopCStackRef(tstate, &cref); + if (impl != NULL) { + return res; + } } + return PyObject_IsSubclass(subclass, cls); +} - PyObject *ok, *subclasses = NULL, *result = NULL; - _abcmodule_state *state = NULL; - Py_ssize_t pos; - int incache; - _abc_data *impl = _get_impl(module, self); - if (impl == NULL) { - return NULL; +// Check if subclass is a subclass of a registered class. +// Returns -1 on error. +static int +check_registry(_abcmodule_state *state, _abc_data *impl, PyObject *subclass) +{ + abc_table *registry = FT_ATOMIC_LOAD_PTR_ACQUIRE(impl->registry); + // Fast path: subclass is registered directly. + if (abc_table_contains(registry, subclass)) { + return 1; } - - /* 1. Check cache. */ - incache = _in_weak_set(impl, &impl->_abc_cache, subclass); - if (incache < 0) { - goto end; + // The registry can be modified by the checks, iterate over a copy. + PyObject *classes; + Py_BEGIN_CRITICAL_SECTION(impl); + classes = abc_table_snapshot(impl->registry); + Py_END_CRITICAL_SECTION(); + if (classes == NULL) { + return -1; } - if (incache > 0) { - result = Py_True; - goto end; + int res = 0; + for (Py_ssize_t i = 0; i < PyList_GET_SIZE(classes); i++) { + res = PyObject_IsSubclass(subclass, PyList_GET_ITEM(classes, i)); + if (res != 0) { + break; + } } + Py_DECREF(classes); + return res; +} - state = get_abc_state(module); - /* 2. Check negative cache; may have to invalidate. */ - uint64_t invalidation_counter = get_invalidation_counter(state); - if (get_cache_version(impl) < invalidation_counter) { - /* Invalidate the negative cache. */ - PyObject *negative_cache; - Py_BEGIN_CRITICAL_SECTION(impl); - negative_cache = impl->_abc_negative_cache; - Py_END_CRITICAL_SECTION(); - if (negative_cache != NULL && PySet_Clear(negative_cache) < 0) { - goto end; - } - set_cache_version(impl, invalidation_counter); +// Check if subclass is a subclass of a subclass of self. +// Returns -1 on error. +static int +check_subclasses(_abcmodule_state *state, PyObject *self, PyObject *subclass) +{ + PyObject *subclasses; + int native = 0; + if (PyType_Check(self) + && type_lookup_is((PyTypeObject *)self, &_Py_ID(__subclasses__), NULL) + && type_lookup_is(Py_TYPE(self), &_Py_ID(__subclasses__), + state->type_subclasses)) + { + // __subclasses__() is not overridden, get the subclasses directly. + subclasses = _PyType_GetSubclasses((PyTypeObject *)self); + native = 1; } else { - incache = _in_weak_set(impl, &impl->_abc_negative_cache, subclass); - if (incache < 0) { - goto end; + subclasses = PyObject_CallMethodNoArgs(self, &_Py_ID(__subclasses__)); + if (subclasses != NULL && !PyList_Check(subclasses)) { + PyErr_SetString(PyExc_TypeError, "__subclasses__() must return a list"); + Py_DECREF(subclasses); + return -1; + } + } + if (subclasses == NULL) { + return -1; + } + int res = 0; + for (Py_ssize_t i = 0; i < PyList_GET_SIZE(subclasses); i++) { + PyObject *scls = PyList_GetItemRef(subclasses, i); + if (scls == NULL) { + res = -1; + break; + } + if (native) { + res = issubclass_nested(state, subclass, scls); } - if (incache > 0) { - result = Py_False; - goto end; + else { + res = PyObject_IsSubclass(subclass, scls); + } + Py_DECREF(scls); + if (res != 0) { + break; } } + Py_DECREF(subclasses); + return res; +} - /* 3. Check the subclass hook. */ - ok = PyObject_CallMethodOneArg( - (PyObject *)self, &_Py_ID(__subclasshook__), subclass); - if (ok == NULL) { - goto end; +// Implementation of ABCMeta.__subclasscheck__. Returns -1 on error. +// +// nested is set when checking the subclasses of another ABC. Negative +// results are not cached then, otherwise every class in the hierarchy +// would cache them which is quadratic in the number of classes. +static int +subclasscheck(_abcmodule_state *state, _abc_data *impl, + PyObject *self, PyObject *subclass, int nested) +{ + uint64_t counter = get_invalidation_counter(state); + + /* 1. Check cache. 2. Check negative cache; may have to invalidate. */ + validate_negative_cache(impl, counter); + cache_result incache = lookup_caches(state, impl, subclass); + if (incache != CACHE_UNKNOWN) { + return incache; } - if (ok == Py_True) { - Py_DECREF(ok); - if (_add_to_weak_set(impl, &impl->_abc_cache, subclass) < 0) { - goto end; + + /* 3. Check the subclass hook. */ + // The default hook always returns NotImplemented, skip the call. + if (!(PyType_Check(self) + && type_lookup_is((PyTypeObject *)self, &_Py_ID(__subclasshook__), + state->object_subclasshook) + && type_lookup_is(Py_TYPE(self), &_Py_ID(__subclasshook__), + state->object_subclasshook))) + { + PyObject *ok = PyObject_CallMethodOneArg( + self, &_Py_ID(__subclasshook__), subclass); + if (ok == NULL) { + return -1; } - result = Py_True; - goto end; - } - if (ok == Py_False) { - Py_DECREF(ok); - if (_add_to_weak_set(impl, &impl->_abc_negative_cache, subclass) < 0) { - goto end; + if (ok == Py_True) { + goto positive; + } + if (ok == Py_False) { + goto negative; + } + if (ok != Py_NotImplemented) { + Py_DECREF(ok); + PyErr_SetString(PyExc_AssertionError, "__subclasshook__ must return either" + " False, True, or NotImplemented"); + return -1; } - result = Py_False; - goto end; - } - if (ok != Py_NotImplemented) { - Py_DECREF(ok); - PyErr_SetString(PyExc_AssertionError, "__subclasshook__ must return either" - " False, True, or NotImplemented"); - goto end; } - Py_DECREF(ok); /* 4. Check if it's a direct subclass. */ if (PyType_IsSubtype((PyTypeObject *)subclass, (PyTypeObject *)self)) { - if (_add_to_weak_set(impl, &impl->_abc_cache, subclass) < 0) { - goto end; - } - result = Py_True; - goto end; + goto positive; } /* 5. Check if it's a subclass of a registered class (recursive). */ - if (subclasscheck_check_registry(impl, subclass, &result)) { - // Exception occurred or result is set. - goto end; + int res = check_registry(state, impl, subclass); + if (res < 0) { + return -1; + } + if (res > 0) { + goto positive; } /* 6. Check if it's a subclass of a subclass (recursive). */ - subclasses = PyObject_CallMethod(self, "__subclasses__", NULL); - if (subclasses == NULL) { - goto end; - } - if (!PyList_Check(subclasses)) { - PyErr_SetString(PyExc_TypeError, "__subclasses__() must return a list"); - goto end; + res = check_subclasses(state, self, subclass); + if (res < 0) { + return -1; } - for (pos = 0; pos < PyList_GET_SIZE(subclasses); pos++) { - PyObject *scls = PyList_GetItemRef(subclasses, pos); - if (scls == NULL) { - goto end; - } - int r = PyObject_IsSubclass(subclass, scls); - Py_DECREF(scls); - if (r > 0) { - if (_add_to_weak_set(impl, &impl->_abc_cache, subclass) < 0) { - goto end; - } - result = Py_True; - goto end; - } - if (r < 0) { - goto end; - } + if (res > 0) { + goto positive; } +negative: /* No dice; update negative cache. */ - if (_add_to_weak_set(impl, &impl->_abc_negative_cache, subclass) < 0) { - goto end; + if (!nested && add_to_negative_cache(impl, subclass, counter) < 0) { + return -1; } - result = Py_False; + return 0; -end: - Py_DECREF(impl); - Py_XDECREF(subclasses); - return Py_XNewRef(result); +positive: + if (add_to_table(impl, &impl->cache, subclass) < 0) { + return -1; + } + return 1; } +/*[clinic input] +@permit_long_summary +_abc._abc_subclasscheck -static int -subclasscheck_check_registry(_abc_data *impl, PyObject *subclass, - PyObject **result) + self: object + subclass: object + / + +Internal ABC helper for subclasss checks. Should be never used outside abc module. +[clinic start generated code]*/ + +static PyObject * +_abc__abc_subclasscheck_impl(PyObject *module, PyObject *self, + PyObject *subclass) +/*[clinic end generated code: output=b56c9e4a530e3894 input=5bf1ef712f5d3610]*/ { - // Fast path: check subclass is in weakref directly. - int ret = _in_weak_set(impl, &impl->_abc_registry, subclass); - if (ret < 0) { - *result = NULL; - return -1; - } - if (ret > 0) { - *result = Py_True; - return 1; + if (!PyType_Check(subclass)) { + PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class"); + return NULL; } - PyObject *registry_shared; - Py_BEGIN_CRITICAL_SECTION(impl); - registry_shared = impl->_abc_registry; - Py_END_CRITICAL_SECTION(); - if (registry_shared == NULL) { - return 0; + _abcmodule_state *state = get_abc_state(module); + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _abc_data *impl = get_impl(state, self, &cref); + int res = -1; + if (impl != NULL) { + res = lookup_caches(state, impl, subclass); + if (res == CACHE_UNKNOWN) { + res = subclasscheck(state, impl, self, subclass, 0); + } } - - // Make a local copy of the registry to protect against concurrent - // modifications of _abc_registry. - PyObject *registry = PyFrozenSet_New(registry_shared); - if (registry == NULL) { - return -1; + _PyThreadState_PopCStackRef(tstate, &cref); + if (res < 0) { + return NULL; } - PyObject *key; - Py_ssize_t pos = 0; - Py_hash_t hash; + return PyBool_FromLong(res); +} - while (_PySet_NextEntry(registry, &pos, &key, &hash)) { - PyObject *rkey; - if (PyWeakref_GetRef(key, &rkey) < 0) { - // Someone inject non-weakref type in the registry. - ret = -1; - break; - } +/*[clinic input] +_abc._set_subclasscheck - if (rkey == NULL) { - continue; - } - int r = PyObject_IsSubclass(subclass, rkey); - Py_DECREF(rkey); - if (r < 0) { - ret = -1; - break; - } - if (r > 0) { - if (_add_to_weak_set(impl, &impl->_abc_cache, subclass) < 0) { - ret = -1; - break; - } - *result = Py_True; - ret = 1; - break; - } - } + func: object + / - Py_DECREF(registry); - return ret; +Internal ABC helper to register ABCMeta.__subclasscheck__. + +Classes whose metaclass resolves __subclasscheck__ to func are checked +natively when walking the subclass tree of an ABC. +[clinic start generated code]*/ + +static PyObject * +_abc__set_subclasscheck(PyObject *module, PyObject *func) +/*[clinic end generated code: output=1dc657738311dea5 input=c2e47dd519524e44]*/ +{ + _abcmodule_state *state = get_abc_state(module); + // abc can be reloaded. + Py_XSETREF(state->subclasscheck_func, Py_NewRef(func)); + Py_RETURN_NONE; } /*[clinic input] @@ -945,9 +1317,23 @@ static struct PyMethodDef _abcmodule_methods[] = { _ABC__ABC_REGISTER_METHODDEF _ABC__ABC_INSTANCECHECK_METHODDEF _ABC__ABC_SUBCLASSCHECK_METHODDEF + _ABC__SET_SUBCLASSCHECK_METHODDEF {NULL, NULL} /* sentinel */ }; +static int +get_type_dict_item(PyTypeObject *type, PyObject *name, PyObject **result) +{ + if (PyDict_GetItemRef(_PyType_GetDict(type), name, result) < 0) { + return -1; + } + if (*result == NULL) { + PyErr_Format(PyExc_SystemError, "%s has no %U", type->tp_name, name); + return -1; + } + return 0; +} + static int _abcmodule_exec(PyObject *module) { @@ -957,7 +1343,15 @@ _abcmodule_exec(PyObject *module) if (state->_abc_data_type == NULL) { return -1; } - + if (get_type_dict_item(&PyBaseObject_Type, &_Py_ID(__class__), + &state->object_class_descr) < 0 + || get_type_dict_item(&PyBaseObject_Type, &_Py_ID(__subclasshook__), + &state->object_subclasshook) < 0 + || get_type_dict_item(&PyType_Type, &_Py_ID(__subclasses__), + &state->type_subclasses) < 0) + { + return -1; + } return 0; } @@ -966,6 +1360,10 @@ _abcmodule_traverse(PyObject *module, visitproc visit, void *arg) { _abcmodule_state *state = get_abc_state(module); Py_VISIT(state->_abc_data_type); + Py_VISIT(state->object_class_descr); + Py_VISIT(state->object_subclasshook); + Py_VISIT(state->type_subclasses); + Py_VISIT(state->subclasscheck_func); return 0; } @@ -974,6 +1372,10 @@ _abcmodule_clear(PyObject *module) { _abcmodule_state *state = get_abc_state(module); Py_CLEAR(state->_abc_data_type); + Py_CLEAR(state->object_class_descr); + Py_CLEAR(state->object_subclasshook); + Py_CLEAR(state->type_subclasses); + Py_CLEAR(state->subclasscheck_func); return 0; } diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index a8293f39a7f8c7..34594f515e6412 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -872,6 +872,7 @@ future_cancel(asyncio_state *state, FutureObj *fut, PyObject *msg) } /*[clinic input] +@vectorcall _asyncio.Future.__init__ * @@ -893,7 +894,7 @@ This class is *almost* compatible with concurrent.futures.Future. static int _asyncio_Future___init___impl(FutureObj *self, PyObject *loop) -/*[clinic end generated code: output=9ed75799eaccb5d6 input=89af317082bc0bf8]*/ +/*[clinic end generated code: output=9ed75799eaccb5d6 input=28ff6d3b6c3aa828]*/ { return future_init(self, loop); @@ -1734,6 +1735,7 @@ static PyType_Slot Future_slots[] = { {Py_tp_methods, FutureType_methods}, {Py_tp_getset, FutureType_getsetlist}, {Py_tp_init, _asyncio_Future___init__}, + {Py_tp_vectorcall, _asyncio_Future_vectorcall}, {Py_tp_new, PyType_GenericNew}, {Py_tp_finalize, FutureObj_finalize}, @@ -2290,6 +2292,7 @@ swap_current_task(_PyThreadStateImpl *ts, PyObject *loop, PyObject *task) /* ----- Task */ /*[clinic input] +@vectorcall _asyncio.Task.__init__ coro: object @@ -2306,7 +2309,7 @@ static int _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, PyObject *name, PyObject *context, int eager_start) -/*[clinic end generated code: output=7aced2d27836f1a1 input=18e3f113a51b829d]*/ +/*[clinic end generated code: output=7aced2d27836f1a1 input=de87c52b0800ab4b]*/ { if (future_init((FutureObj*)self, loop)) { return -1; @@ -2353,7 +2356,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, // store the task counter as PyLong in the name // for deferred formatting in get_name #ifdef Py_GIL_DISABLED - unsigned long long counter = _Py_atomic_add_uint64(&state->task_name_counter, 1) + 1; + unsigned long long counter = _Py_atomic_add_uint64_relaxed(&state->task_name_counter, 1) + 1; #else unsigned long long counter = ++state->task_name_counter; #endif @@ -2952,6 +2955,7 @@ static PyType_Slot Task_slots[] = { {Py_tp_methods, TaskType_methods}, {Py_tp_getset, TaskType_getsetlist}, {Py_tp_init, _asyncio_Task___init__}, + {Py_tp_vectorcall, _asyncio_Task_vectorcall}, {Py_tp_new, PyType_GenericNew}, {Py_tp_finalize, TaskObj_finalize}, @@ -3491,17 +3495,36 @@ task_eager_start(_PyThreadStateImpl *ts, asyncio_state *state, TaskObj *task) int retval = 0; + // gh-157299: the eager step must record the awaited_by edge + int eager_edge = TaskOrFuture_Check(state, prevtask); PyObject *stepres; Py_BEGIN_CRITICAL_SECTION(task); - stepres = task_step_impl(state, task, NULL); - Py_END_CRITICAL_SECTION(); + if (eager_edge && + future_awaited_by_add(state, (FutureObj *)task, prevtask) < 0) + { + eager_edge = 0; + task->task_log_destroy_pending = 0; + unregister_task(task); + stepres = NULL; + } + else { + stepres = task_step_impl(state, task, NULL); + } + + PyObject *exc = stepres == NULL ? PyErr_GetRaisedException() : NULL; + if (eager_edge) { + int res = future_awaited_by_discard(state, (FutureObj *)task, prevtask); + if (res) { + retval = -1; + } + } if (stepres == NULL) { - PyObject *exc = PyErr_GetRaisedException(); _PyErr_ChainExceptions1(exc); retval = -1; } else { Py_DECREF(stepres); } + Py_END_CRITICAL_SECTION(); PyObject *curtask = swap_current_task(ts, task->task_loop, prevtask); Py_DECREF(prevtask); @@ -4195,6 +4218,23 @@ _asyncio_future_discard_from_awaited_by_impl(PyObject *module, PyObject *fut, Py_RETURN_NONE; } +/*[clinic input] +_asyncio._clear_caches + +Clear internal caches. Used for internal tests. +[clinic start generated code]*/ + +static PyObject * +_asyncio__clear_caches_impl(PyObject *module) +/*[clinic end generated code: output=9691a293f83d4303 input=6e1ae31e51dc906c]*/ +{ + asyncio_state *state = get_asyncio_state(module); + if (PySet_Clear(state->iscoroutine_typecache) < 0) { + return NULL; + } + Py_RETURN_NONE; +} + static int module_traverse(PyObject *mod, visitproc visit, void *arg) { @@ -4366,6 +4406,7 @@ static PyMethodDef asyncio_methods[] = { _ASYNCIO_ALL_TASKS_METHODDEF _ASYNCIO_FUTURE_ADD_TO_AWAITED_BY_METHODDEF _ASYNCIO_FUTURE_DISCARD_FROM_AWAITED_BY_METHODDEF + _ASYNCIO__CLEAR_CACHES_METHODDEF {NULL, NULL} }; diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 0626cc6a70bc8f..c63dc0661bd69e 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1,6 +1,7 @@ #include "Python.h" #include "pycore_call.h" // _PyObject_CallNoArgs() -#include "pycore_dict.h" // _PyDict_GetItem_KnownHash() +#include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED() +#include "pycore_dict.h" // _PyDict_GetItemRef_KnownHash_LockHeld() #include "pycore_long.h" // _PyLong_GetZero() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_pyatomic_ft_wrappers.h" @@ -2525,6 +2526,66 @@ static PyType_Spec defdict_spec = { /* helper function for Counter *********************************************/ +/* Fast path advantages: + 1. Eliminate double hashing + (by re-using the same hash for both the get and set) + 2. Avoid argument overhead of PyObject_CallFunctionObjArgs + (argument tuple creation and parsing) + 3. Avoid indirection through a bound method object + (creates another argument tuple) + 4. Avoid initial increment from zero + (reuse an existing one-object instead) +*/ +static int +_count_elements_dict_lock_held(PyDictObject *mapping, PyObject *it) +{ + _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(mapping); + PyObject *one = _PyLong_GetOne(); // borrowed reference + + while (1) { + Py_hash_t hash; + PyObject *oldval; + int status; + + PyObject *key = PyIter_Next(it); + if (key == NULL) { + return PyErr_Occurred() ? -1 : 0; + } + + hash = _PyObject_HashDictKey(key); + if (hash == -1) { + Py_DECREF(key); + return -1; + } + + int found = _PyDict_GetItemRef_KnownHash_LockHeld(mapping, key, hash, + &oldval); + if (found < 0) { + Py_DECREF(key); + return -1; + } + if (found == 0) { + status = _PyDict_SetItem_KnownHash_LockHeld(mapping, key, one, + hash); + } + else { + PyObject *newval = PyNumber_Add(oldval, one); + Py_DECREF(oldval); + if (newval == NULL) { + Py_DECREF(key); + return -1; + } + status = _PyDict_SetItem_KnownHash_LockHeld(mapping, key, newval, + hash); + Py_DECREF(newval); + } + Py_DECREF(key); + if (status < 0) { + return -1; + } + } +} + /*[clinic input] _collections._count_elements @@ -2541,7 +2602,6 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping, /*[clinic end generated code: output=7e0c1789636b3d8f input=e79fad04534a0b45]*/ { PyObject *it, *oldval; - PyObject *newval = NULL; PyObject *key = NULL; PyObject *bound_get = NULL; PyObject *mapping_get; @@ -2566,48 +2626,12 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping, mapping_setitem != NULL && mapping_setitem == dict_setitem && PyDict_Check(mapping)) { - while (1) { - /* Fast path advantages: - 1. Eliminate double hashing - (by re-using the same hash for both the get and set) - 2. Avoid argument overhead of PyObject_CallFunctionObjArgs - (argument tuple creation and parsing) - 3. Avoid indirection through a bound method object - (creates another argument tuple) - 4. Avoid initial increment from zero - (reuse an existing one-object instead) - */ - Py_hash_t hash; - - key = PyIter_Next(it); - if (key == NULL) - break; - - hash = _PyObject_HashDictKey(key); - if (hash == -1) { - goto done; - } - - oldval = _PyDict_GetItem_KnownHash(mapping, key, hash); - if (oldval == NULL) { - if (PyErr_Occurred()) - goto done; - if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) < 0) - goto done; - } else { - /* oldval is a borrowed reference. Keep it alive across - PyNumber_Add(), which can execute arbitrary user code and - mutate (or even clear) the underlying dict. */ - Py_INCREF(oldval); - newval = PyNumber_Add(oldval, one); - Py_DECREF(oldval); - if (newval == NULL) - goto done; - if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0) - goto done; - Py_CLEAR(newval); - } - Py_DECREF(key); + int status; + Py_BEGIN_CRITICAL_SECTION(mapping); + status = _count_elements_dict_lock_held((PyDictObject *)mapping, it); + Py_END_CRITICAL_SECTION(); + if (status < 0) { + goto done; } } else { @@ -2623,6 +2647,7 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping, oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL); if (oldval == NULL) break; + PyObject *newval; if (oldval == zero) { newval = Py_NewRef(one); } else { @@ -2631,9 +2656,10 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping, Py_DECREF(oldval); if (newval == NULL) break; - if (PyObject_SetItem(mapping, key, newval) < 0) + int status = PyObject_SetItem(mapping, key, newval); + Py_DECREF(newval); + if (status < 0) break; - Py_CLEAR(newval); Py_DECREF(key); } } @@ -2643,7 +2669,6 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping, Py_XDECREF(mapping_setitem); Py_DECREF(it); Py_XDECREF(key); - Py_XDECREF(newval); Py_XDECREF(bound_get); if (PyErr_Occurred()) return NULL; diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 56d39e29f140dd..301c28d2eb2115 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -186,7 +186,23 @@ _DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw) { DictRemoverObject *self = _DictRemoverObject_CAST(myself); if (self->key && self->dict) { - if (-1 == PyDict_DelItem(self->dict, self->key)) { + /* Only remove the entry if it is still the proxy that died: the + key may have been reused for a new object in the meantime. */ + PyObject *proxy = PyTuple_GET_SIZE(args) == 1 + ? PyTuple_GET_ITEM(args, 0) : NULL; + PyObject *current; + int rc; + Py_BEGIN_CRITICAL_SECTION(self->dict); + rc = PyDict_GetItemRef(self->dict, self->key, ¤t); + if (rc == 1) { + rc = 0; + if (current == proxy) { + rc = PyDict_DelItem(self->dict, self->key); + } + Py_DECREF(current); + } + Py_END_CRITICAL_SECTION(); + if (rc < 0) { PyErr_FormatUnraisable("Exception ignored while " "calling _ctypes.DictRemover"); } @@ -215,32 +231,72 @@ static PyType_Spec dictremover_spec = { .slots = dictremover_slots, }; -int -PyDict_SetItemProxy(ctypes_state *st, PyObject *dict, PyObject *key, PyObject *item) +/* Cache `item` in `dict` under `key` through a weakref proxy, unless a live + entry already exists. On success *presult is a new reference to whatever + is cached for `key` afterwards: `item` itself, or the object another + thread cached first. + Returns 0 on success, -1 with an exception set on failure. */ +static int +_PyDict_SetDefaultItemProxy(ctypes_state *st, PyObject *dict, PyObject *key, + PyObject *item, PyObject **presult) { - PyObject *obj; - DictRemoverObject *remover; - PyObject *proxy; - int result; - - obj = _PyObject_CallNoArgs((PyObject *)st->DictRemover_Type); - if (obj == NULL) + PyObject *obj = _PyObject_CallNoArgs((PyObject *)st->DictRemover_Type); + if (obj == NULL) { return -1; + } - remover = (DictRemoverObject *)obj; + DictRemoverObject *remover = (DictRemoverObject *)obj; assert(remover->key == NULL); assert(remover->dict == NULL); remover->key = Py_NewRef(key); remover->dict = Py_NewRef(dict); - proxy = PyWeakref_NewProxy(item, obj); + PyObject *proxy = PyWeakref_NewProxy(item, obj); Py_DECREF(obj); - if (proxy == NULL) + if (proxy == NULL) { return -1; + } - result = PyDict_SetItem(dict, key, proxy); + PyObject *stored; + int present = PyDict_SetDefaultRef(dict, key, proxy, &stored); + if (present < 0) { + Py_DECREF(proxy); + return -1; + } + if (present == 0) { + /* Our proxy was inserted; `item` is the canonical object. */ + Py_DECREF(stored); + Py_DECREF(proxy); + *presult = Py_NewRef(item); + return 0; + } + + /* Another thread cached an object for this key first. Prefer it, so that + every caller gets the same object. */ + PyObject *existing = NULL; + int rc = 0; + if (PyWeakref_CheckProxy(stored)) { + rc = PyWeakref_GetRef(stored, &existing); + } + Py_DECREF(stored); + if (rc < 0) { + Py_DECREF(proxy); + return -1; + } + if (existing != NULL) { + Py_DECREF(proxy); + *presult = existing; + return 0; + } + + /* The cached entry had already been cleared; install ours instead. */ + rc = PyDict_SetItem(dict, key, proxy); Py_DECREF(proxy); - return result; + if (rc < 0) { + return -1; + } + *presult = Py_NewRef(item); + return 0; } static int @@ -5154,7 +5210,6 @@ Array_subscript_impl(PyObject *myself, PyObject *item) if (iteminfo->getfunc == _ctypes_get_fielddesc("c")->getfunc) { char *ptr = (char *)self->b_ptr; - char *dest; if (slicelen <= 0) return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); @@ -5164,19 +5219,17 @@ Array_subscript_impl(PyObject *myself, PyObject *item) slicelen); return res; } - dest = (char *)PyMem_Malloc(slicelen); - - if (dest == NULL) - return PyErr_NoMemory(); - + PyBytesWriter *writer = PyBytesWriter_Create(slicelen); + if (writer == NULL) { + return NULL; + } + char *dest = PyBytesWriter_GetData(writer); for (cur = start, i = 0; i < slicelen; cur += step, i++) { dest[i] = ptr[cur]; } - np = PyBytes_FromStringAndSize(dest, slicelen); - PyMem_Free(dest); - return np; + return PyBytesWriter_Finish(writer); } if (iteminfo->getfunc == _ctypes_get_fielddesc("u")->getfunc) { wchar_t *ptr = (wchar_t *)self->b_ptr; @@ -5320,6 +5373,9 @@ Array_ass_subscript_impl(PyObject *myself, PyObject *item, PyObject *value) stop = start; otherlen = PySequence_Length(value); + if (otherlen < 0) { + return -1; + } if (otherlen != slicelen) { PyErr_SetString(PyExc_ValueError, "Can only assign sequence of same size"); @@ -5437,13 +5493,20 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length) Py_DECREF(key); return NULL; } - if (PyDict_SetItemProxy(st, st->array_cache, key, result) < 0) { + PyObject *cached; + int rc; + Py_BEGIN_CRITICAL_SECTION(st->array_cache); + rc = _PyDict_SetDefaultItemProxy(st, st->array_cache, key, result, + &cached); + Py_END_CRITICAL_SECTION(); + if (rc < 0) { Py_DECREF(key); Py_DECREF(result); return NULL; } Py_DECREF(key); - return result; + Py_DECREF(result); + return cached; } @@ -5964,8 +6027,6 @@ Pointer_subscript(PyObject *myself, PyObject *item) } assert(iteminfo); if (iteminfo->getfunc == _ctypes_get_fielddesc("c")->getfunc) { - char *dest; - if (len <= 0) return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); if (step == 1) { @@ -5977,18 +6038,18 @@ Pointer_subscript(PyObject *myself, PyObject *item) Py_END_CRITICAL_SECTION(); return res; } - dest = (char *)PyMem_Malloc(len); - if (dest == NULL) - return PyErr_NoMemory(); + PyBytesWriter *writer = PyBytesWriter_Create(len); + if (writer == NULL) { + return NULL; + } + char *dest = PyBytesWriter_GetData(writer); Py_BEGIN_CRITICAL_SECTION(self); char *ptr = *(void **)self->b_ptr; for (cur = start, i = 0; i < len; cur += step, i++) { dest[i] = ptr[cur]; } Py_END_CRITICAL_SECTION(); - np = PyBytes_FromStringAndSize(dest, len); - PyMem_Free(dest); - return np; + return PyBytesWriter_Finish(writer); } if (iteminfo->getfunc == _ctypes_get_fielddesc("u")->getfunc) { wchar_t *dest; @@ -6553,18 +6614,6 @@ _ctypes_add_version_constants(PyObject *mod) static int _ctypes_mod_exec(PyObject *mod) { - // See https://github.com/python/cpython/issues/128485 - // This allocates some memory and then frees it to ensure that the - // the dlmalloc allocator initializes itself to avoid data races - // in free-threading. - void *codeloc = NULL; - void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc); - if (ptr == NULL) { - PyErr_NoMemory(); - return -1; - } - Py_ffi_closure_free(ptr); - _ctypes_init_fielddesc(); ctypes_state *st = get_module_state(mod); diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index 8395e9eaf3505e..e54111b8a4fab4 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -9,6 +9,7 @@ #endif #include "pycore_call.h" // _PyObject_CallNoArgs() +#include "pycore_lock.h" // _PyOnceFlag_CallOnce() #include "pycore_runtime.h" // _Py_ID() #include @@ -412,6 +413,27 @@ static CThunkObject* CThunkObject_new(ctypes_state *st, Py_ssize_t nargs) return p; } +static _PyOnceFlag closure_alloc_init_once = {0}; + +// This is done lazily with a once flag rather than in the module exec function, +// because allocating a closure can have some side effects that affect the whole +// process (gh-135893). +static int +closure_alloc_init(void *Py_UNUSED(arg)) +{ + // See https://github.com/python/cpython/issues/128485 + // This allocates some memory and then frees it to ensure that libffi's + // dlmalloc allocator initializes itself exactly once, avoiding data races + // between threads that don't share a GIL. + void *codeloc = NULL; + void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc); + if (ptr == NULL) { + return -1; + } + Py_ffi_closure_free(ptr); + return 0; +} + CThunkObject *_ctypes_alloc_callback(ctypes_state *st, PyObject *callable, PyObject *converters, @@ -431,6 +453,12 @@ CThunkObject *_ctypes_alloc_callback(ctypes_state *st, assert(CThunk_CheckExact(st, (PyObject *)p)); + if (_PyOnceFlag_CallOnce(&closure_alloc_init_once, + closure_alloc_init, NULL) < 0) { + PyErr_NoMemory(); + goto error; + } + p->pcl_write = Py_ffi_closure_alloc(sizeof(ffi_closure), &p->pcl_exec); if (p->pcl_write == NULL) { PyErr_NoMemory(); diff --git a/Modules/_ctypes/ctypes.h b/Modules/_ctypes/ctypes.h index 248559aa364a19..98d8b660850b0d 100644 --- a/Modules/_ctypes/ctypes.h +++ b/Modules/_ctypes/ctypes.h @@ -24,7 +24,7 @@ // Do we support C99 complex types in ffi? // For Apple's libffi, this must be determined at runtime (see gh-128156). #if defined(_Py_FFI_SUPPORT_C_COMPLEX) -# if USING_APPLE_OS_LIBFFI && defined(__has_builtin) +# if defined(USING_APPLE_OS_LIBFFI) && defined(__has_builtin) # if __has_builtin(__builtin_available) # define Py_FFI_COMPLEX_AVAILABLE __builtin_available(macOS 10.15, *) # else diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index fc47844104876e..7920c87ebe3cef 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -683,10 +683,22 @@ _replace_array_elements(ctypes_state *st, PyObject *layout_fields, structs[struct_index].type = FFI_TYPE_STRUCT; structs[struct_index].elements = &dummy_types[dummy_index]; ++struct_index; + ffi_type *etype = &einfo->ffi_type_pointer; +#if defined(__sparc__) && defined(__arch64__) && !defined(__sun) + /* On Linux, GCC and clang pass arrays of floating-point type + in a structure in integer registers; only Solaris follows + the vendor ABI here (GCC PR target/114416). */ + if (etype->type == FFI_TYPE_FLOAT) { + etype = &ffi_type_uint32; + } + else if (etype->type == FFI_TYPE_DOUBLE) { + etype = &ffi_type_uint64; + } +#endif /* Copy over the element's type, length times. */ while (length > 0) { assert(dummy_index < (num_ffi_type_pointers)); - dummy_types[dummy_index++] = &einfo->ffi_type_pointer; + dummy_types[dummy_index++] = etype; length--; } assert(dummy_index < (num_ffi_type_pointers)); diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 52b87e7cc0e8b9..94cad7f02e31bb 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1936,10 +1936,10 @@ Window_NoArgTrueFalseFunction(is_scrollok) Window_NoArgTrueFalseFunction(is_subwin) Window_NoArgTrueFalseFunction(is_syncok) #endif -#if defined(HAVE_CURSES_IS_KEYPAD) || defined(PDCURSES) +#if defined(_Py__Py_HAVE_CURSES_IS_KEYPAD) || defined(PDCURSES) Window_NoArgTrueFalseFunction(is_keypad) #endif -#if defined(HAVE_CURSES_IS_LEAVEOK) || defined(PDCURSES) +#if defined(_Py_HAVE_CURSES_IS_LEAVEOK) || defined(PDCURSES) Window_NoArgTrueFalseFunction(is_leaveok) #endif #if defined(HAVE_CURSES_IS_PAD) || defined(PDCURSES) @@ -2199,7 +2199,7 @@ static int curses_wattr_save(PyCursesWindowObject *self, attr_t *attrs, int *pair, const char *funcname) { -#if defined(HAVE_CURSES_WATTR_GET) && defined(HAVE_CURSES_WATTR_SET) +#if defined(_Py_HAVE_CURSES_WATTR_GET) && defined(_Py_HAVE_CURSES_WATTR_SET) int rtn; #if _NCURSES_EXTENDED_COLOR_FUNCS short legacy_pair; @@ -2222,7 +2222,7 @@ curses_wattr_save(PyCursesWindowObject *self, attr_t *attrs, int *pair, /* Put the rendition back. The name of the curses function used is _CURSES_WATTR_RESTORE_FUNC, for the caller to name it in an error. */ -#if defined(HAVE_CURSES_WATTR_GET) && defined(HAVE_CURSES_WATTR_SET) +#if defined(_Py_HAVE_CURSES_WATTR_GET) && defined(_Py_HAVE_CURSES_WATTR_SET) #define _CURSES_WATTR_RESTORE_FUNC "wattr_set" #else #define _CURSES_WATTR_RESTORE_FUNC "wattrset" @@ -2231,7 +2231,7 @@ curses_wattr_save(PyCursesWindowObject *self, attr_t *attrs, int *pair, static int curses_wattr_restore(PyCursesWindowObject *self, attr_t attrs, int pair) { -#if defined(HAVE_CURSES_WATTR_GET) && defined(HAVE_CURSES_WATTR_SET) +#if defined(_Py_HAVE_CURSES_WATTR_GET) && defined(_Py_HAVE_CURSES_WATTR_SET) #if _NCURSES_EXTENDED_COLOR_FUNCS return wattr_set(self->win, attrs, 0, &pair); #else @@ -2558,7 +2558,7 @@ _curses_window_attrset_impl(PyCursesWindowObject *self, attr_t attr) return curses_window_check_err(self, rtn, "wattrset", "attrset"); } -#ifdef HAVE_CURSES_WATTR_GET +#ifdef _Py_HAVE_CURSES_WATTR_GET /*[clinic input] _curses.window.attr_get @@ -2584,9 +2584,9 @@ _curses_window_attr_get_impl(PyCursesWindowObject *self) } return Py_BuildValue("(ki)", (unsigned long)attrs, (int)pair); } -#endif /* HAVE_CURSES_WATTR_GET */ +#endif /* _Py_HAVE_CURSES_WATTR_GET */ -#ifdef HAVE_CURSES_WATTR_SET +#ifdef _Py_HAVE_CURSES_WATTR_SET /*[clinic input] _curses.window.attr_set @@ -2610,9 +2610,9 @@ _curses_window_attr_set_impl(PyCursesWindowObject *self, attr_t attr, #endif return curses_window_check_err(self, rtn, "wattr_set", "attr_set"); } -#endif /* HAVE_CURSES_WATTR_SET */ +#endif /* _Py_HAVE_CURSES_WATTR_SET */ -#ifdef HAVE_CURSES_WATTR_ON +#ifdef _Py_HAVE_CURSES_WATTR_ON /*[clinic input] _curses.window.attr_on @@ -2629,9 +2629,9 @@ _curses_window_attr_on_impl(PyCursesWindowObject *self, attr_t attr) int rtn = wattr_on(self->win, attr, NULL); return curses_window_check_err(self, rtn, "wattr_on", "attr_on"); } -#endif /* HAVE_CURSES_WATTR_ON */ +#endif /* _Py_HAVE_CURSES_WATTR_ON */ -#ifdef HAVE_CURSES_WATTR_OFF +#ifdef _Py_HAVE_CURSES_WATTR_OFF /*[clinic input] _curses.window.attr_off @@ -2648,9 +2648,9 @@ _curses_window_attr_off_impl(PyCursesWindowObject *self, attr_t attr) int rtn = wattr_off(self->win, attr, NULL); return curses_window_check_err(self, rtn, "wattr_off", "attr_off"); } -#endif /* HAVE_CURSES_WATTR_OFF */ +#endif /* _Py_HAVE_CURSES_WATTR_OFF */ -#ifdef HAVE_CURSES_WCOLOR_SET +#ifdef _Py_HAVE_CURSES_WCOLOR_SET /*[clinic input] _curses.window.color_set @@ -2672,7 +2672,7 @@ _curses_window_color_set_impl(PyCursesWindowObject *self, int pair) #endif return curses_window_check_err(self, rtn, "wcolor_set", "color_set"); } -#endif /* HAVE_CURSES_WCOLOR_SET */ +#endif /* _Py_HAVE_CURSES_WCOLOR_SET */ /*[clinic input] _curses.window.getattrs @@ -5035,7 +5035,7 @@ static PyType_Spec PyCursesComplexStr_Type_spec = { }; #undef clinic_state -#if defined(HAVE_CURSES_USE_SCREEN) || defined(HAVE_CURSES_USE_WINDOW) +#if defined(_Py_HAVE_CURSES_USE_SCREEN) || defined(_Py_HAVE_CURSES_USE_WINDOW) /* Shared trampoline for window.use()/screen.use(): call func(obj, *extra, **kwargs) and store the result (NULL on exception) in data->result. */ @@ -5085,7 +5085,7 @@ curses_use_parse(PyObject *args, PyObject **func, PyObject **extra) } #endif -#ifdef HAVE_CURSES_USE_WINDOW +#ifdef _Py_HAVE_CURSES_USE_WINDOW static int curses_use_window_cb(WINDOW *Py_UNUSED(win), void *data) { @@ -5111,7 +5111,7 @@ PyCursesWindow_use(PyObject *self, PyObject *args, PyObject *kwargs) Py_DECREF(extra); return data.result; } -#endif /* HAVE_CURSES_USE_WINDOW */ +#endif /* _Py_HAVE_CURSES_USE_WINDOW */ static PyMethodDef PyCursesWindow_methods[] = { _CURSES_WINDOW_ADDCH_METHODDEF @@ -5250,12 +5250,12 @@ static PyMethodDef PyCursesWindow_methods[] = { "is_syncok($self, /)\n--\n\n" "Return the current value set by syncok()."}, #endif -#if defined(HAVE_CURSES_IS_KEYPAD) || defined(PDCURSES) +#if defined(_Py__Py_HAVE_CURSES_IS_KEYPAD) || defined(PDCURSES) {"is_keypad", PyCursesWindow_is_keypad, METH_NOARGS, "is_keypad($self, /)\n--\n\n" "Return the current value set by keypad()."}, #endif -#if defined(HAVE_CURSES_IS_LEAVEOK) || defined(PDCURSES) +#if defined(_Py_HAVE_CURSES_IS_LEAVEOK) || defined(PDCURSES) {"is_leaveok", PyCursesWindow_is_leaveok, METH_NOARGS, "is_leaveok($self, /)\n--\n\n" "Return the current value set by leaveok()."}, @@ -5336,7 +5336,7 @@ static PyMethodDef PyCursesWindow_methods[] = { "untouchwin($self, /)\n--\n\n" "Mark all lines in the window as unchanged since last refresh()."}, _CURSES_WINDOW_VLINE_METHODDEF -#ifdef HAVE_CURSES_USE_WINDOW +#ifdef _Py_HAVE_CURSES_USE_WINDOW {"use", _PyCFunction_CAST(PyCursesWindow_use), METH_VARARGS | METH_KEYWORDS, PyCursesWindow_use__doc__}, #endif @@ -5478,7 +5478,7 @@ static PyGetSetDef PyCursesScreen_getsets[] = { {NULL, NULL, NULL, NULL, NULL} /* sentinel */ }; -#ifdef HAVE_CURSES_USE_SCREEN +#ifdef _Py_HAVE_CURSES_USE_SCREEN static int curses_use_screen_cb(SCREEN *Py_UNUSED(sp), void *data) { @@ -5533,7 +5533,7 @@ PyCursesScreen_use(PyObject *self, PyObject *args, PyObject *kwargs) } return data.result; } -#endif /* HAVE_CURSES_USE_SCREEN */ +#endif /* _Py_HAVE_CURSES_USE_SCREEN */ PyDoc_STRVAR(PyCursesScreen_close__doc__, "close($self, /)\n--\n\n" @@ -5552,7 +5552,7 @@ PyCursesScreen_close(PyObject *self, PyObject *Py_UNUSED(ignored)) static PyMethodDef PyCursesScreen_methods[] = { {"close", PyCursesScreen_close, METH_NOARGS, PyCursesScreen_close__doc__}, -#ifdef HAVE_CURSES_USE_SCREEN +#ifdef _Py_HAVE_CURSES_USE_SCREEN {"use", _PyCFunction_CAST(PyCursesScreen_use), METH_VARARGS | METH_KEYWORDS, PyCursesScreen_use__doc__}, #endif @@ -5704,7 +5704,7 @@ _curses_filter_impl(PyObject *module) } #endif -#ifdef HAVE_CURSES_NOFILTER +#ifdef _Py_HAVE_CURSES_NOFILTER /*[clinic input] _curses.nofilter @@ -6269,7 +6269,7 @@ _curses_getwin(PyObject *module, PyObject *file) return res; } -#ifdef HAVE_CURSES_SCR_DUMP +#ifdef _Py_HAVE_CURSES_SCR_DUMP /*[clinic input] _curses.scr_dump @@ -6324,7 +6324,7 @@ _curses_scr_init(PyObject *module, PyObject *filename) /*[clinic end generated code: output=2e861d381d710419 input=81c45e4702124ef6]*/ ScreenDumpFunctionBody(scr_init) -#ifdef HAVE_CURSES_SCR_SET +#ifdef _Py_HAVE_CURSES_SCR_SET /*[clinic input] _curses.scr_set @@ -6341,8 +6341,8 @@ static PyObject * _curses_scr_set(PyObject *module, PyObject *filename) /*[clinic end generated code: output=6056fdec12c5935f input=d248c20543cc289b]*/ ScreenDumpFunctionBody(scr_set) -#endif /* HAVE_CURSES_SCR_SET */ -#endif /* HAVE_CURSES_SCR_DUMP */ +#endif /* _Py_HAVE_CURSES_SCR_SET */ +#endif /* _Py_HAVE_CURSES_SCR_DUMP */ /*[clinic input] _curses.halfdelay @@ -6421,7 +6421,7 @@ _curses_has_key_impl(PyObject *module, int key) } #endif -#ifdef HAVE_CURSES_DEFINE_KEY +#ifdef _Py_HAVE_CURSES_DEFINE_KEY /*[clinic input] _curses.define_key @@ -6447,9 +6447,9 @@ _curses_define_key_impl(PyObject *module, const char *definition, return curses_check_err(module, define_key(definition, keycode), "define_key", NULL); } -#endif /* HAVE_CURSES_DEFINE_KEY */ +#endif /* _Py_HAVE_CURSES_DEFINE_KEY */ -#ifdef HAVE_CURSES_KEY_DEFINED +#ifdef _Py_HAVE_CURSES_KEY_DEFINED /*[clinic input] _curses.key_defined @@ -6471,9 +6471,9 @@ _curses_key_defined_impl(PyObject *module, const char *definition) return PyLong_FromLong(key_defined(definition)); } -#endif /* HAVE_CURSES_KEY_DEFINED */ +#endif /* _Py_HAVE_CURSES_KEY_DEFINED */ -#ifdef HAVE_CURSES_KEYOK +#ifdef _Py_HAVE_CURSES_KEYOK /*[clinic input] _curses.keyok @@ -6494,7 +6494,7 @@ _curses_keyok_impl(PyObject *module, int keycode, int enable) return curses_check_err(module, keyok(keycode, enable), "keyok", NULL); } -#endif /* HAVE_CURSES_KEYOK */ +#endif /* _Py_HAVE_CURSES_KEYOK */ /*[clinic input] _curses.init_color @@ -7283,7 +7283,7 @@ _curses_set_term(PyObject *module, PyObject *screen) return prev != NULL ? prev : Py_NewRef(Py_None); } -#ifdef HAVE_CURSES_NEW_PRESCR +#ifdef _Py_HAVE_CURSES_NEW_PRESCR /*[clinic input] _curses.new_prescr @@ -7317,7 +7317,7 @@ _curses_new_prescr_impl(PyObject *module) state->prescreen = Py_NewRef(screenobj); return screenobj; } -#endif /* HAVE_CURSES_NEW_PRESCR */ +#endif /* _Py_HAVE_CURSES_NEW_PRESCR */ #ifdef HAVE_CURSES_ESCDELAY /*[clinic input] @@ -7573,7 +7573,7 @@ _curses_meta_impl(PyObject *module, int yes) } #if defined(HAVE_CURSES_GETMOUSE) || defined(PDCURSES) -#if defined(HAVE_CURSES_HAS_MOUSE) || defined(PDCURSES) +#if defined(_Py_HAVE_CURSES_HAS_MOUSE) || defined(PDCURSES) /*[clinic input] _curses.has_mouse @@ -7588,7 +7588,7 @@ _curses_has_mouse_impl(PyObject *module) return PyBool_FromLong(has_mouse()); } -#endif /* HAVE_CURSES_HAS_MOUSE || PDCURSES */ +#endif /* _Py_HAVE_CURSES_HAS_MOUSE || PDCURSES */ /*[clinic input] _curses.mouseinterval @@ -8272,7 +8272,7 @@ _curses_termattrs_impl(PyObject *module) return PyLong_FromUnsignedLong((unsigned long)(chtype)termattrs()); } -#ifdef HAVE_CURSES_TERM_ATTRS +#ifdef _Py_HAVE_CURSES_TERM_ATTRS /*[clinic input] _curses.term_attrs @@ -8290,7 +8290,7 @@ _curses_term_attrs_impl(PyObject *module) return PyLong_FromUnsignedLongLong(term_attrs()); } -#endif /* HAVE_CURSES_TERM_ATTRS */ +#endif /* _Py_HAVE_CURSES_TERM_ATTRS */ /*[clinic input] @permit_long_summary @@ -8374,35 +8374,155 @@ _curses_tigetstr_impl(PyObject *module, const char *capname) return PyBytes_FromString( capname ); } +#ifndef NCURSES_TPARM_ARG +# define NCURSES_TPARM_ARG long +#endif + +/* Return a bit mask of the parameters of a terminfo string that curses takes + as strings: those that %s or %l follows directly after %p, as in ncurses' + _nc_tparm_analyze(). The others are numbers. */ +static unsigned int +curses_tparm_string_params(const char *str) +{ + unsigned int mask = 0; + int last = 0; /* the parameter pushed last, or 0 */ + + while (*str) { + if (*str++ != '%') { + continue; + } + /* Skip the flags, width and precision of a conversion. */ + int allowminus = 0; + while (*str) { + if (*str == ':') { + allowminus = 1; + } + else if (!(*str == '#' || *str == ' ' || *str == '.' + || Py_ISDIGIT(*str) || (*str == '-' && allowminus))) + { + break; + } + str++; + } + switch (*str) { + case 'p': + if (str[1] >= '1' && str[1] <= '9') { + last = str[1] - '0'; + } + str++; + break; + case 's': + case 'l': + if (last > 0) { + mask |= 1u << (last - 1); + } + break; + case 'd': case 'o': case 'x': case 'X': case 'c': + case '+': case '-': case '*': case '/': case 'm': + case '&': case '|': case '^': case '=': case '<': case '>': + case 'A': case 'O': case '!': case '~': + last = 0; + break; + case '\'': + str += 2; + last = 0; + break; + case '\0': + return mask; + default: + /* %P, %g, %{, %i, %?, %t, %e, %; and %% keep it. */ + break; + } + str++; + } + return mask; +} + +/* Convert a tparm() parameter: a string for those that the terminfo string + takes as strings (passing a number there would crash curses), otherwise a + number. An omitted one is 0, or NULL, which curses takes as "". */ +static int +curses_tparm_param(PyObject *arg, int argnum, int is_string, + NCURSES_TPARM_ARG *result) +{ + if (arg == NULL) { + *result = 0; + return 0; + } + if (is_string) { + if (!PyBytes_Check(arg)) { + PyErr_Format(PyExc_TypeError, + "tparm() argument %d must be bytes, not %T", + argnum, arg); + return -1; + } + const char *value = PyBytes_AS_STRING(arg); + if (strlen(value) != (size_t)PyBytes_GET_SIZE(arg)) { + PyErr_SetString(PyExc_ValueError, "embedded null byte"); + return -1; + } + *result = (NCURSES_TPARM_ARG)(intptr_t)value; + return 0; + } + if (PyBytes_Check(arg)) { + PyErr_Format(PyExc_TypeError, + "tparm() argument %d must be int, not bytes", argnum); + return -1; + } + int value = PyLong_AsInt(arg); + if (value == -1 && PyErr_Occurred()) { + return -1; + } + *result = value; + return 0; +} + /*[clinic input] _curses.tparm - str: str(accept={robuffer}) + str as string: object Parameterized byte string obtained from the terminfo database. - i1: int = 0 - i2: int = 0 - i3: int = 0 - i4: int = 0 - i5: int = 0 - i6: int = 0 - i7: int = 0 - i8: int = 0 - i9: int = 0 / + *params: tuple + Up to nine parameters. Instantiate the specified byte string with the supplied parameters. + +The parameters are integers, except in a few cases where they must be +bytes objects. [clinic start generated code]*/ static PyObject * -_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3, - int i4, int i5, int i6, int i7, int i8, int i9) -/*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/ +_curses_tparm_impl(PyObject *module, PyObject *string, PyObject *params) +/*[clinic end generated code: output=3439bb1b37a13d5b input=4889c0e3a5eab5b5]*/ { char* result = NULL; + NCURSES_TPARM_ARG values[9]; + Py_ssize_t count = PyTuple_GET_SIZE(params); + const char *str; + if (!PyArg_Parse(string, "y:tparm", &str)) { + return NULL; + } + + if (count > 9) { + PyErr_Format(PyExc_TypeError, + "tparm() takes at most 9 parameters (%zd given)", count); + return NULL; + } PyCursesStatefulSetupTermCalled(module); - result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9); + unsigned int strings = curses_tparm_string_params(str); + for (int i = 0; i < 9; i++) { + PyObject *arg = i < count ? PyTuple_GET_ITEM(params, i) : NULL; + if (curses_tparm_param(arg, i + 2, strings & (1u << i), + &values[i]) < 0) + { + return NULL; + } + } + result = tparm((char *)str, values[0], values[1], values[2], values[3], + values[4], values[5], values[6], values[7], values[8]); if (!result) { curses_set_null_error(module, "tparm", NULL); return NULL; @@ -8877,7 +8997,7 @@ _curses_slk_attr_impl(PyObject *module) } #endif -#ifdef HAVE_CURSES_SLK_ATTR_ON +#ifdef _Py_HAVE_CURSES_SLK_ATTR_ON /*[clinic input] _curses.slk_attr_on @@ -8895,9 +9015,9 @@ _curses_slk_attr_on_impl(PyObject *module, attr_t attr) return curses_check_err(module, slk_attr_on(attr, NULL), "slk_attr_on", NULL); } -#endif /* HAVE_CURSES_SLK_ATTR_ON */ +#endif /* _Py_HAVE_CURSES_SLK_ATTR_ON */ -#ifdef HAVE_CURSES_SLK_ATTR_OFF +#ifdef _Py_HAVE_CURSES_SLK_ATTR_OFF /*[clinic input] _curses.slk_attr_off @@ -8915,9 +9035,9 @@ _curses_slk_attr_off_impl(PyObject *module, attr_t attr) return curses_check_err(module, slk_attr_off(attr, NULL), "slk_attr_off", NULL); } -#endif /* HAVE_CURSES_SLK_ATTR_OFF */ +#endif /* _Py_HAVE_CURSES_SLK_ATTR_OFF */ -#ifdef HAVE_CURSES_SLK_ATTR_SET +#ifdef _Py_HAVE_CURSES_SLK_ATTR_SET /*[clinic input] _curses.slk_attr_set @@ -8941,9 +9061,9 @@ _curses_slk_attr_set_impl(PyObject *module, attr_t attr, int pair) #endif return curses_check_err(module, rtn, "slk_attr_set", NULL); } -#endif /* HAVE_CURSES_SLK_ATTR_SET */ +#endif /* _Py_HAVE_CURSES_SLK_ATTR_SET */ -#ifdef HAVE_CURSES_SLK_COLOR +#ifdef _Py_HAVE_CURSES_SLK_COLOR /*[clinic input] _curses.slk_color @@ -8966,7 +9086,7 @@ _curses_slk_color_impl(PyObject *module, int pair) #endif return curses_check_err(module, rtn, "slk_color", NULL); } -#endif /* HAVE_CURSES_SLK_COLOR */ +#endif /* _Py_HAVE_CURSES_SLK_COLOR */ #ifdef HAVE_CURSES_USE_ENV /*[clinic input] @@ -9178,7 +9298,7 @@ static PyMethodDef cursesmodule_methods[] = { _CURSES_NEWPAD_METHODDEF _CURSES_NEWTERM_METHODDEF _CURSES_NEWWIN_METHODDEF -#ifdef HAVE_CURSES_NEW_PRESCR +#ifdef _Py_HAVE_CURSES_NEW_PRESCR _CURSES_NEW_PRESCR_METHODDEF #endif _CURSES_NL_METHODDEF diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 959abcf5e421cf..a06b867049097e 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -471,88 +471,35 @@ days_before_month(int year, int month) #define DI100Y 36524 /* _PyTime_DaysBeforeYear(101); days in 100 years */ #define DI400Y 146097 /* _PyTime_DaysBeforeYear(401); days in 400 years */ -/* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ +/* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. + * + * Neri and Schneider's algorithm, https://doi.org/10.1002/spe.3172; see + * _ord2ymd() in Lib/_pydatetime.py for an explanation of the steps. + * + * The intermediate values only fit in 32 bits for small ordinals; + * iso_to_ymd() passes the largest, MAXORDINAL + 2 (10000-01-02). + */ static void ord_to_ymd(int ordinal, int *year, int *month, int *day) { - int n, n1, n4, n100, n400, leapyear, preceding; - - /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of - * leap years repeats exactly every 400 years. The basic strategy is - * to find the closest 400-year boundary at or before ordinal, then - * work with the offset from that boundary to ordinal. Life is much - * clearer if we subtract 1 from ordinal first -- then the values - * of ordinal at 400-year boundaries are exactly those divisible - * by DI400Y: - * - * D M Y n n-1 - * -- --- ---- ---------- ---------------- - * 31 Dec -400 -DI400Y -DI400Y -1 - * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary - * ... - * 30 Dec 000 -1 -2 - * 31 Dec 000 0 -1 - * 1 Jan 001 1 0 400-year boundary - * 2 Jan 001 2 1 - * 3 Jan 001 3 2 - * ... - * 31 Dec 400 DI400Y DI400Y -1 - * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary - */ - assert(ordinal >= 1); - --ordinal; - n400 = ordinal / DI400Y; - n = ordinal % DI400Y; - *year = n400 * 400 + 1; - - /* Now n is the (non-negative) offset, in days, from January 1 of - * year, to the desired date. Now compute how many 100-year cycles - * precede n. - * Note that it's possible for n100 to equal 4! In that case 4 full - * 100-year cycles precede the desired day, which implies the - * desired day is December 31 at the end of a 400-year cycle. - */ - n100 = n / DI100Y; - n = n % DI100Y; - - /* Now compute how many 4-year cycles precede it. */ - n4 = n / DI4Y; - n = n % DI4Y; - - /* And now how many single years. Again n1 can be 4, and again - * meaning that the desired day is December 31 at the end of the - * 4-year cycle. - */ - n1 = n / 365; - n = n % 365; - - *year += n100 * 100 + n4 * 4 + n1; - if (n1 == 4 || n100 == 4) { - assert(n == 0); - *year -= 1; - *month = 12; - *day = 31; - return; + assert(1 <= ordinal && ordinal <= MAXORDINAL + 2); + const uint32_t days = (uint32_t)ordinal + 305; + const uint32_t century = (4 * days + 3) / DI400Y; + const uint32_t day_of_century = (4 * days + 3) % DI400Y / 4; + const uint32_t year_of_century = (4 * day_of_century + 3) / DI4Y; + const uint32_t day_of_year = (4 * day_of_century + 3) % DI4Y / 4; + const uint32_t m = (5 * day_of_year + 461) / 153; + const uint32_t d = (5 * day_of_year + 461) % 153 / 5 + 1; + const uint32_t y = 100 * century + year_of_century; + if (m > 12) { /* January or February */ + *year = (int)(y + 1); + *month = (int)(m - 12); } - - /* Now the year is correct, and n is the offset from January 1. We - * find the month via an estimate that's either exact or one too - * large. - */ - leapyear = n1 == 3 && (n4 != 24 || n100 == 3); - assert(leapyear == is_leap(*year)); - *month = (n + 50) >> 5; - preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); - if (preceding > n) { - /* estimate is too large */ - *month -= 1; - preceding -= days_in_month(*year, *month); + else { + *year = (int)y; + *month = (int)m; } - n -= preceding; - assert(0 <= n); - assert(n < days_in_month(*year, *month)); - - *day = n + 1; + *day = (int)d; } /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ @@ -668,6 +615,18 @@ check_date_args(int year, int month, int day) return 0; } +static int +year_from_tm(const struct tm *tm, int *year) +{ + if (tm->tm_year > INT_MAX - 1900) { + PyErr_Format(PyExc_ValueError, "year must be in %d..%d, not %lld", + MINYEAR, MAXYEAR, (long long)tm->tm_year + 1900); + return -1; + } + *year = tm->tm_year + 1900; + return 0; +} + /* Check that time arguments are in range. Return 0 if they are. If they * aren't, raise ValueError and return -1. */ @@ -977,6 +936,10 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month, iso_day = 1; } + if ((size_t)(p - dtstr) != len) { + return -1; + } + int rv = iso_to_ymd(*year, iso_week, iso_day, year, month, day); if (rv) { return -3 + rv; @@ -997,6 +960,9 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month, if (p == NULL) { return -1; } + if ((size_t)(p - dtstr) != len) { + return -1; + } return 0; } @@ -1851,12 +1817,17 @@ static PyObject * make_freplacement(PyObject *object) { char freplacement[64]; - if (PyTime_Check(object)) - sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); - else if (PyDateTime_Check(object)) - sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); - else - sprintf(freplacement, "%06d", 0); + if (PyTime_Check(object)) { + PyOS_snprintf(freplacement, sizeof(freplacement), + "%06d", TIME_GET_MICROSECOND(object)); + } + else if (PyDateTime_Check(object)) { + PyOS_snprintf(freplacement, sizeof(freplacement), + "%06d", DATE_GET_MICROSECOND(object)); + } + else { + PyOS_snprintf(freplacement, sizeof(freplacement), "%06d", 0); + } return PyUnicode_FromString(freplacement); } @@ -2130,15 +2101,25 @@ diff_to_bool(int diff, int op) /* Convert a timedelta to a number of us, * (24*3600*self.days + self.seconds)*1000000 + self.microseconds * as a Python int. - * Doing mixed-radix arithmetic by hand instead is excruciating in C, - * due to ubiquitous overflow possibilities. + * The number of whole seconds always fits in int64_t, and so does the + * number of microseconds unless the delta is very large, so compute it + * in C when possible and fall back to Python int arithmetic otherwise. + * One second is reserved at the limit so that adding the microseconds + * field cannot overflow. */ static PyObject * delta_to_microseconds(PyDateTime_Delta *self) { + const int64_t max_seconds = INT64_MAX / 1000000 - 1; + int64_t seconds = (int64_t)GET_TD_DAYS(self) * (24 * 3600) + + GET_TD_SECONDS(self); + if (-max_seconds <= seconds && seconds <= max_seconds) { + return PyLong_FromInt64(seconds * 1000000 + + GET_TD_MICROSECONDS(self)); + } + PyObject *x1 = NULL; PyObject *x2 = NULL; - PyObject *x3 = NULL; PyObject *result = NULL; PyObject *current_mod; @@ -2147,42 +2128,23 @@ delta_to_microseconds(PyDateTime_Delta *self) return NULL; } - x1 = PyLong_FromLong(GET_TD_DAYS(self)); + x1 = PyLong_FromInt64(seconds); /* days and seconds in seconds */ if (x1 == NULL) goto Done; - x2 = PyNumber_Multiply(x1, CONST_SEC_PER_DAY(st)); /* days in seconds */ + x2 = PyNumber_Multiply(x1, CONST_US_PER_SECOND(st)); /* us */ if (x2 == NULL) goto Done; - Py_SETREF(x1, NULL); - - /* x2 has days in seconds */ - x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ - if (x1 == NULL) - goto Done; - x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ - if (x3 == NULL) - goto Done; - Py_DECREF(x1); - Py_DECREF(x2); - /* x1 = */ x2 = NULL; - /* x3 has days+seconds in seconds */ - x1 = PyNumber_Multiply(x3, CONST_US_PER_SECOND(st)); /* us */ + /* x2 has days+seconds in us */ + Py_SETREF(x1, PyLong_FromLong(GET_TD_MICROSECONDS(self))); if (x1 == NULL) goto Done; - Py_SETREF(x3, NULL); - - /* x1 has days+seconds in us */ - x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); - if (x2 == NULL) - goto Done; - result = PyNumber_Add(x1, x2); + result = PyNumber_Add(x2, x1); assert(result == NULL || PyLong_CheckExact(result)); Done: Py_XDECREF(x1); Py_XDECREF(x2); - Py_XDECREF(x3); RELEASE_CURRENT_STATE(st, current_mod); return result; } @@ -3290,14 +3252,18 @@ date_fromtimestamp(PyTypeObject *cls, PyObject *obj) { struct tm tm; time_t t; + int year; if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) return NULL; if (_PyTime_localtime(t, &tm) != 0) return NULL; + if (year_from_tm(&tm, &year) < 0) { + return NULL; + } - return new_date_subclass_ex(tm.tm_year + 1900, + return new_date_subclass_ex(year, tm.tm_mon + 1, tm.tm_mday, cls); @@ -3416,7 +3382,16 @@ datetime_date_fromordinal_impl(PyTypeObject *type, int ordinal) PyErr_SetString(PyExc_ValueError, "ordinal must be >= 1"); return NULL; } - ord_to_ymd(ordinal, &year, &month, &day); + if (ordinal <= MAXORDINAL) { + ord_to_ymd(ordinal, &year, &month, &day); + } + else { + /* Out of range, but compute the year for the error message. + * The calendar repeats every 400 years (DI400Y days). */ + int cycles = (ordinal - 1) / DI400Y; + ord_to_ymd(ordinal - cycles * DI400Y, &year, &month, &day); + year += 400 * cycles; + } return new_date_subclass_ex(year, month, day, type); } @@ -5668,7 +5643,9 @@ datetime_from_timet_and_us(PyTypeObject *cls, TM_FUNC f, time_t timet, int us, if (f(timet, &tm) != 0) return NULL; - year = tm.tm_year + 1900; + if (year_from_tm(&tm, &year) < 0) { + return NULL; + } month = tm.tm_mon + 1; day = tm.tm_mday; hour = tm.tm_hour; diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index 098522b130ec87..3a128a13222f9b 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -2003,6 +2003,13 @@ current_context(decimal_state *state) } if (tl_context != NULL) { + if (!PyDecContext_Check(state, tl_context)) { + PyErr_Format(PyExc_TypeError, + "current decimal context must be a Context " + "instance, got %T", tl_context); + Py_DECREF(tl_context); + return NULL; + } return tl_context; } diff --git a/Modules/_decimal/tests/README.txt b/Modules/_decimal/tests/README.txt index 97b6ff68a56fb7..9ea9ef8c802ac2 100644 --- a/Modules/_decimal/tests/README.txt +++ b/Modules/_decimal/tests/README.txt @@ -1,9 +1,4 @@ - - -This directory contains extended tests and a benchmark against decimal.py: - - bench.py -> Benchmark for small and large precisions. - Usage: ../../../python bench.py +This directory contains extended tests against decimal.py: formathelper.py -> randdec.py -> Generate test cases for deccheck.py. @@ -11,5 +6,3 @@ This directory contains extended tests and a benchmark against decimal.py: deccheck.py -> Run extended tests. Usage: ../../../python deccheck.py [--short|--medium|--long|--all] - - diff --git a/Modules/_decimal/tests/bench.py b/Modules/_decimal/tests/bench.py deleted file mode 100644 index 6605e9a92e2dde..00000000000000 --- a/Modules/_decimal/tests/bench.py +++ /dev/null @@ -1,150 +0,0 @@ -# -# Copyright (C) 2001 Python Software Foundation. All Rights Reserved. -# Modified and extended by Stefan Krah. -# - -# Usage: ../../../python bench.py - - -import time -import sys -from functools import wraps -from test.support.import_helper import import_fresh_module - -C = import_fresh_module('decimal', fresh=['_decimal']) -P = import_fresh_module('decimal', blocked=['_decimal']) - -# -# NOTE: This is the pi function from the decimal documentation, modified -# for benchmarking purposes. Since floats do not have a context, the higher -# intermediate precision from the original is NOT used, so the modified -# algorithm only gives an approximation to the correctly rounded result. -# For serious use, refer to the documentation or the appropriate literature. -# -def pi_float(): - """native float""" - lasts, t, s, n, na, d, da = 0, 3.0, 3, 1, 0, 0, 24 - while s != lasts: - lasts = s - n, na = n+na, na+8 - d, da = d+da, da+32 - t = (t * n) / d - s += t - return s - -def pi_cdecimal(): - """cdecimal""" - D = C.Decimal - lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24) - while s != lasts: - lasts = s - n, na = n+na, na+8 - d, da = d+da, da+32 - t = (t * n) / d - s += t - return s - -def pi_decimal(): - """decimal""" - D = P.Decimal - lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24) - while s != lasts: - lasts = s - n, na = n+na, na+8 - d, da = d+da, da+32 - t = (t * n) / d - s += t - return s - -def factorial(n, m): - if (n > m): - return factorial(m, n) - elif m == 0: - return 1 - elif n == m: - return n - else: - return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m) - -# Fix failed test cases caused by CVE-2020-10735 patch. -# See gh-95778 for details. -def increase_int_max_str_digits(maxdigits): - def _increase_int_max_str_digits(func, maxdigits=maxdigits): - @wraps(func) - def wrapper(*args, **kwargs): - previous_int_limit = sys.get_int_max_str_digits() - sys.set_int_max_str_digits(maxdigits) - ans = func(*args, **kwargs) - sys.set_int_max_str_digits(previous_int_limit) - return ans - return wrapper - return _increase_int_max_str_digits - -def test_calc_pi(): - print("\n# ======================================================================") - print("# Calculating pi, 10000 iterations") - print("# ======================================================================\n") - - to_benchmark = [pi_float, pi_decimal] - if C is not None: - to_benchmark.insert(1, pi_cdecimal) - - for prec in [9, 19]: - print("\nPrecision: %d decimal digits\n" % prec) - for func in to_benchmark: - start = time.time() - if C is not None: - C.getcontext().prec = prec - P.getcontext().prec = prec - for i in range(10000): - x = func() - print("%s:" % func.__name__.replace("pi_", "")) - print("result: %s" % str(x)) - print("time: %fs\n" % (time.time()-start)) - -@increase_int_max_str_digits(maxdigits=10000000) -def test_factorial(): - print("\n# ======================================================================") - print("# Factorial") - print("# ======================================================================\n") - - if C is not None: - c = C.getcontext() - c.prec = C.MAX_PREC - c.Emax = C.MAX_EMAX - c.Emin = C.MIN_EMIN - - for n in [100000, 1000000]: - - print("n = %d\n" % n) - - if C is not None: - # C version of decimal - start_calc = time.time() - x = factorial(C.Decimal(n), 0) - end_calc = time.time() - start_conv = time.time() - sx = str(x) - end_conv = time.time() - print("cdecimal:") - print("calculation time: %fs" % (end_calc-start_calc)) - print("conversion time: %fs\n" % (end_conv-start_conv)) - - # Python integers - start_calc = time.time() - y = factorial(n, 0) - end_calc = time.time() - start_conv = time.time() - sy = str(y) - end_conv = time.time() - - print("int:") - print("calculation time: %fs" % (end_calc-start_calc)) - print("conversion time: %fs\n\n" % (end_conv-start_conv)) - - if C is not None: - assert(sx == sy) - -if __name__ == "__main__": - test_calc_pi() - test_factorial() diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index f8f661a7341891..2195010261678f 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -640,11 +640,11 @@ _elementtree_SubElement_impl(PyObject *module, PyObject *parent, attrib = PyDict_Copy(attrib); if (!attrib) return NULL; - if (PyDict_Update(attrib, extra) < 0) { + if (extra && PyDict_Update(attrib, extra) < 0) { Py_DECREF(attrib); return NULL; } - } else if (PyDict_GET_SIZE(extra)) { + } else if (extra && PyDict_GET_SIZE(extra)) { /* have keyword args */ attrib = get_attrib_from_keywords(extra); if (!attrib) @@ -2391,7 +2391,9 @@ elementiter_next(PyObject *op) if (it->sought_tag == Py_None) return (PyObject *)elem; - rc = PyObject_RichCompareBool(elem->tag, it->sought_tag, Py_EQ); + PyObject *tag = Py_NewRef(elem->tag); + rc = PyObject_RichCompareBool(tag, it->sought_tag, Py_EQ); + Py_DECREF(tag); if (rc > 0) return (PyObject *)elem; diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 75b93d87e90a99..7c02d8d7b3ee8a 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -1227,7 +1227,7 @@ uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd { PyObject *result; - FT_ATOMIC_ADD_SSIZE(self->misses, 1); + FT_ATOMIC_ADD_SSIZE_RELAXED(self->misses, 1); result = PyObject_Call(self->func, args, kwds); if (!result) return NULL; @@ -1249,7 +1249,7 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd } int res = _PyDict_GetItemRef_KnownHash((PyDictObject *)self->cache, key, hash, &result); if (res > 0) { - FT_ATOMIC_ADD_SSIZE(self->hits, 1); + FT_ATOMIC_ADD_SSIZE_RELAXED(self->hits, 1); Py_DECREF(key); return result; } @@ -1257,7 +1257,7 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd Py_DECREF(key); return NULL; } - FT_ATOMIC_ADD_SSIZE(self->misses, 1); + FT_ATOMIC_ADD_SSIZE_RELAXED(self->misses, 1); result = PyObject_Call(self->func, args, kwds); if (!result) { Py_DECREF(key); @@ -1353,7 +1353,7 @@ bounded_lru_cache_get_lock_held(lru_cache_object *self, PyObject *args, PyObject lru_cache_extract_link(link); lru_cache_append_link(self, link); *result = link->result; - FT_ATOMIC_ADD_SSIZE(self->hits, 1); + FT_ATOMIC_ADD_SSIZE_RELAXED(self->hits, 1); Py_INCREF(link->result); Py_DECREF(link); Py_DECREF(key_); @@ -1363,7 +1363,7 @@ bounded_lru_cache_get_lock_held(lru_cache_object *self, PyObject *args, PyObject Py_DECREF(key_); return -1; } - FT_ATOMIC_ADD_SSIZE(self->misses, 1); + FT_ATOMIC_ADD_SSIZE_RELAXED(self->misses, 1); return 0; } diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c index 6630d5f6fe2d33..454db063d18b6a 100644 --- a/Modules/_interpretersmodule.c +++ b/Modules/_interpretersmodule.c @@ -828,7 +828,7 @@ _interpreters_new_config_impl(PyObject *module, const char *name, return NULL; } - if (PyDict_GET_SIZE(overrides) > 0) { + if (overrides != NULL && PyDict_GET_SIZE(overrides) > 0) { if (_PyInterpreterConfig_UpdateFromDict(&config, overrides) < 0) { return NULL; } diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 03e6fbe08889d4..0d28fe24fa0b3a 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -94,10 +94,7 @@ mode. Other common values are 'w' for writing (truncating the file if it already exists), 'x' for creating and writing to a new file, 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: locale.getencoding() 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: +The available modes are: ========= ========================================================== Character Meaning @@ -122,8 +119,7 @@ even when the underlying operating system doesn't. Files opened in binary mode (appending 'b' to the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is appended to the mode argument), the contents of the file are -returned as strings, the bytes having been first decoded using a -platform-dependent encoding or using the specified encoding if given. +decoded using the encoding described below and returned as strings. buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to @@ -140,10 +136,11 @@ argument is given, the default buffering policy works as follows: use line buffering. Other text files use the policy described above for binary files. -encoding is the name of the encoding used to decode or encode the -file. This should only be used in text mode. The default encoding is -platform dependent, but any encoding supported by Python can be -passed. See the codecs module for the list of supported encodings. +encoding is the name of the encoding used to decode or encode the file. +This should only be used in text mode. The default encoding is UTF-8; +if UTF-8 Mode is disabled, the encoding returned by +locale.getencoding() is used instead. See the codecs module for the +list of supported encodings. errors is an optional string that specifies how encoding errors are to be handled---this argument should not be used in binary mode. Pass @@ -200,7 +197,7 @@ static PyObject * _io_open_impl(PyObject *module, PyObject *file, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd, PyObject *opener) -/*[clinic end generated code: output=aefafc4ce2b46dc0 input=b3cefa70bef404b3]*/ +/*[clinic end generated code: output=aefafc4ce2b46dc0 input=5a005681afa02242]*/ { size_t i; diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 44c3f56caee18b..9e5a7216ee013b 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -9,11 +9,11 @@ #include "Python.h" #include "pycore_call.h" // _PyObject_CallNoArgs() +#include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED() #include "pycore_fileutils.h" // _PyFile_Flush #include "pycore_object.h" // _PyObject_GC_UNTRACK() #include "pycore_pyerrors.h" // _Py_FatalErrorFormat() #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() -#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include "_iomodule.h" @@ -60,6 +60,11 @@ _bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readint if (data == NULL) return NULL; + /* Non-blocking stream with no data available */ + if (data == Py_None) { + return data; + } + if (!PyBytes_Check(data)) { Py_DECREF(data); PyErr_SetString(PyExc_TypeError, "read() should return bytes"); @@ -260,8 +265,6 @@ typedef struct { Py_ssize_t buffer_size; Py_ssize_t buffer_mask; - PyObject *dict; - PyObject *weakreflist; } buffered; #define buffered_CAST(op) ((buffered *)(op)) @@ -417,7 +420,7 @@ buffered_clear(PyObject *op) buffered *self = buffered_CAST(op); self->ok = 0; Py_CLEAR(self->raw); - Py_CLEAR(self->dict); + PyObject_ClearManagedDict(op); return 0; } @@ -431,7 +434,7 @@ buffered_dealloc(PyObject *op) return; _PyObject_GC_UNTRACK(self); self->ok = 0; - FT_CLEAR_WEAKREFS(op, self->weakreflist); + PyObject_ClearWeakRefs(op); if (self->buffer) { PyMem_Free(self->buffer); self->buffer = NULL; @@ -467,8 +470,7 @@ buffered_traverse(PyObject *op, visitproc visit, void *arg) buffered *self = buffered_CAST(op); Py_VISIT(Py_TYPE(self)); Py_VISIT(self->raw); - Py_VISIT(self->dict); - return 0; + return PyObject_VisitManagedDict(op, visit, arg); } /* Because this can call arbitrary code, it shouldn't be called when @@ -2250,8 +2252,6 @@ typedef struct { PyObject_HEAD buffered *reader; buffered *writer; - PyObject *dict; - PyObject *weakreflist; } rwpair; #define rwpair_CAST(op) ((rwpair *)(op)) @@ -2287,20 +2287,25 @@ _io_BufferedRWPair___init___impl(rwpair *self, PyObject *reader, return -1; } - self->reader = (buffered *) PyObject_CallFunction( + buffered *new_reader = (buffered *) PyObject_CallFunction( (PyObject *)state->PyBufferedReader_Type, "On", reader, buffer_size); - if (self->reader == NULL) + if (new_reader == NULL) { return -1; + } - self->writer = (buffered *) PyObject_CallFunction( + buffered *new_writer = (buffered *) PyObject_CallFunction( (PyObject *)state->PyBufferedWriter_Type, "On", writer, buffer_size); - if (self->writer == NULL) { - Py_CLEAR(self->reader); + if (new_writer == NULL) { + Py_DECREF(new_reader); return -1; } + /* Release a previous writer before a previous reader, see + bufferedrwpair_clear(). */ + Py_XSETREF(self->writer, new_writer); + Py_XSETREF(self->reader, new_reader); return 0; } @@ -2309,19 +2314,19 @@ bufferedrwpair_traverse(PyObject *op, visitproc visit, void *arg) { rwpair *self = rwpair_CAST(op); Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->dict); Py_VISIT(self->reader); Py_VISIT(self->writer); - return 0; + return PyObject_VisitManagedDict(op, visit, arg); } static int bufferedrwpair_clear(PyObject *op) { rwpair *self = rwpair_CAST(op); - Py_CLEAR(self->reader); + /* Let the writer flush before the reader closes a shared raw stream. */ Py_CLEAR(self->writer); - Py_CLEAR(self->dict); + Py_CLEAR(self->reader); + PyObject_ClearManagedDict(op); return 0; } @@ -2331,7 +2336,7 @@ bufferedrwpair_dealloc(PyObject *op) rwpair *self = rwpair_CAST(op); PyTypeObject *tp = Py_TYPE(self); _PyObject_GC_UNTRACK(self); - FT_CLEAR_WEAKREFS(op, self->weakreflist); + PyObject_ClearWeakRefs(op); (void)bufferedrwpair_clear(op); tp->tp_free(self); Py_DECREF(tp); @@ -2394,6 +2399,25 @@ _io_BufferedRWPair_read1_impl(rwpair *self, PyObject *size) return _forward_call(self->reader, &_Py_ID(read1), size); } +/*[clinic input] +_io.BufferedRWPair.readline + size: object(c_default="NULL") = -1 + / + +Read and return a line from the stream. + +If size is specified, at most size bytes will be read. + +The line terminator is always b'\n'. +[clinic start generated code]*/ + +static PyObject * +_io_BufferedRWPair_readline_impl(rwpair *self, PyObject *size) +/*[clinic end generated code: output=ee5bb2364231e6e1 input=1c552272a69db1ad]*/ +{ + return _forward_call(self->reader, &_Py_ID(readline), size); +} + /*[clinic input] _io.BufferedRWPair.readinto buffer: object @@ -2638,8 +2662,6 @@ static PyMethodDef bufferedreader_methods[] = { static PyMemberDef bufferedreader_members[] = { {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY}, {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0}, - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY}, {NULL} }; @@ -2697,8 +2719,6 @@ static PyMethodDef bufferedwriter_methods[] = { static PyMemberDef bufferedwriter_members[] = { {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY}, {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0}, - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY}, {NULL} }; @@ -2735,6 +2755,7 @@ static PyMethodDef bufferedrwpair_methods[] = { _IO_BUFFEREDRWPAIR_READ_METHODDEF _IO_BUFFEREDRWPAIR_PEEK_METHODDEF _IO_BUFFEREDRWPAIR_READ1_METHODDEF + _IO_BUFFEREDRWPAIR_READLINE_METHODDEF _IO_BUFFEREDRWPAIR_READINTO_METHODDEF _IO_BUFFEREDRWPAIR_READINTO1_METHODDEF @@ -2750,12 +2771,6 @@ static PyMethodDef bufferedrwpair_methods[] = { {NULL, NULL} }; -static PyMemberDef bufferedrwpair_members[] = { - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(rwpair, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(rwpair, dict), Py_READONLY}, - {NULL} -}; - static PyGetSetDef bufferedrwpair_getset[] = { _IO_BUFFEREDRWPAIR_CLOSED_GETSETDEF {NULL} @@ -2767,7 +2782,6 @@ static PyType_Slot bufferedrwpair_slots[] = { {Py_tp_traverse, bufferedrwpair_traverse}, {Py_tp_clear, bufferedrwpair_clear}, {Py_tp_methods, bufferedrwpair_methods}, - {Py_tp_members, bufferedrwpair_members}, {Py_tp_getset, bufferedrwpair_getset}, {Py_tp_init, _io_BufferedRWPair___init__}, {0, NULL}, @@ -2814,8 +2828,6 @@ static PyMethodDef bufferedrandom_methods[] = { static PyMemberDef bufferedrandom_members[] = { {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY}, {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0}, - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY}, {NULL} }; diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c index 2571da4538363f..fa828f21892899 100644 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -5,7 +5,6 @@ #include "pycore_object.h" #include "pycore_pyatomic_ft_wrappers.h" #include "pycore_sysmodule.h" // _PySys_GetSizeOf() -#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include // offsetof() #include "_iomodule.h" @@ -21,8 +20,6 @@ typedef struct { PyObject *buf; Py_ssize_t pos; Py_ssize_t string_size; - PyObject *dict; - PyObject *weakreflist; Py_ssize_t exports; #ifdef Py_GIL_DISABLED int buf_shared; @@ -974,11 +971,16 @@ _io_BytesIO___getstate___impl(bytesio *self) if (initvalue == NULL) return NULL; - if (self->dict == NULL) { + if (_PyObject_IsInstanceDictEmpty((PyObject *)self)) { dict = Py_NewRef(Py_None); } else { - dict = PyDict_Copy(self->dict); + dict = PyObject_GenericGetDict((PyObject *)self, NULL); + if (dict == NULL) { + Py_DECREF(initvalue); + return NULL; + } + Py_SETREF(dict, PyDict_Copy(dict)); if (dict == NULL) { Py_DECREF(initvalue); return NULL; @@ -1061,16 +1063,16 @@ _io_BytesIO___setstate___impl(bytesio *self, PyObject *state) Py_TYPE(dict)->tp_name); return NULL; } - if (self->dict) { - /* Alternatively, we could replace the internal dictionary - completely. However, it seems more practical to just update it. */ - if (PyDict_Update(self->dict, dict) < 0) - return NULL; + /* Alternatively, we could replace the internal dictionary + completely. However, it seems more practical to just update it. */ + PyObject *self_dict = PyObject_GenericGetDict((PyObject *)self, NULL); + if (self_dict == NULL) { + return NULL; } - else { - /* The LOAD_ATTR specializations read the dict slot lock-free - with an acquire load, so pair it with a release store. */ - FT_ATOMIC_STORE_PTR_RELEASE(self->dict, Py_NewRef(dict)); + int rc = PyDict_Update(self_dict, dict); + Py_DECREF(self_dict); + if (rc < 0) { + return NULL; } } @@ -1089,8 +1091,8 @@ bytesio_dealloc(PyObject *op) PyErr_Print(); } Py_CLEAR(self->buf); - Py_CLEAR(self->dict); - FT_CLEAR_WEAKREFS(op, self->weakreflist); + PyObject_ClearManagedDict(op); + PyObject_ClearWeakRefs(op); tp->tp_free(self); Py_DECREF(tp); } @@ -1118,6 +1120,7 @@ bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } /*[clinic input] +@vectorcall @critical_section _io.BytesIO.__init__ initial_bytes as initvalue: object(c_default="NULL") = b'' @@ -1127,7 +1130,7 @@ Buffered I/O implementation using an in-memory bytes buffer. static int _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue) -/*[clinic end generated code: output=65c0c51e24c5b621 input=3da5a74ee4c4f1ac]*/ +/*[clinic end generated code: output=65c0c51e24c5b621 input=a6dc38082204df28]*/ { if (FT_ATOMIC_LOAD_SSIZE_RELAXED(self->exports) > 0) { PyErr_SetString(PyExc_BufferError, @@ -1186,16 +1189,15 @@ bytesio_traverse(PyObject *op, visitproc visit, void *arg) { bytesio *self = bytesio_CAST(op); Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->dict); Py_VISIT(self->buf); - return 0; + return PyObject_VisitManagedDict(op, visit, arg); } static int bytesio_clear(PyObject *op) { bytesio *self = bytesio_CAST(op); - Py_CLEAR(self->dict); + PyObject_ClearManagedDict(op); if (FT_ATOMIC_LOAD_SSIZE_RELAXED(self->exports) == 0) { Py_CLEAR(self->buf); } @@ -1238,12 +1240,6 @@ static struct PyMethodDef bytesio_methods[] = { {NULL, NULL} /* sentinel */ }; -static PyMemberDef bytesio_members[] = { - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(bytesio, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(bytesio, dict), Py_READONLY}, - {NULL} -}; - static PyType_Slot bytesio_slots[] = { {Py_tp_dealloc, bytesio_dealloc}, {Py_tp_doc, (void *)_io_BytesIO___init____doc__}, @@ -1252,10 +1248,10 @@ static PyType_Slot bytesio_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, bytesio_iternext}, {Py_tp_methods, bytesio_methods}, - {Py_tp_members, bytesio_members}, {Py_tp_getset, bytesio_getsetlist}, {Py_tp_init, _io_BytesIO___init__}, {Py_tp_new, bytesio_new}, + {Py_tp_vectorcall, _io_BytesIO_vectorcall}, {0, NULL}, }; diff --git a/Modules/_io/clinic/_iomodule.c.h b/Modules/_io/clinic/_iomodule.c.h index 0547982cad48cc..462597078b77d9 100644 --- a/Modules/_io/clinic/_iomodule.c.h +++ b/Modules/_io/clinic/_iomodule.c.h @@ -28,10 +28,7 @@ PyDoc_STRVAR(_io_open__doc__, "it already exists), \'x\' for creating and writing to a new file, and\n" "\'a\' for appending (which on some Unix systems, means that all writes\n" "append to the end of the file regardless of the current seek position).\n" -"In text mode, if encoding is not specified the encoding used is platform\n" -"dependent: locale.getencoding() is called to get the current locale\n" -"encoding. (For reading and writing raw bytes use binary mode and leave\n" -"encoding unspecified.) The available modes are:\n" +"The available modes are:\n" "\n" "========= ==========================================================\n" "Character Meaning\n" @@ -56,8 +53,7 @@ PyDoc_STRVAR(_io_open__doc__, "binary mode (appending \'b\' to the mode argument) return contents as\n" "bytes objects without any decoding. In text mode (the default, or when\n" "\'t\' is appended to the mode argument), the contents of the file are\n" -"returned as strings, the bytes having been first decoded using a\n" -"platform-dependent encoding or using the specified encoding if given.\n" +"decoded using the encoding described below and returned as strings.\n" "\n" "buffering is an optional integer used to set the buffering policy.\n" "Pass 0 to switch buffering off (only allowed in binary mode), 1 to\n" @@ -74,10 +70,11 @@ PyDoc_STRVAR(_io_open__doc__, " use line buffering. Other text files use the policy described above\n" " for binary files.\n" "\n" -"encoding is the name of the encoding used to decode or encode the\n" -"file. This should only be used in text mode. The default encoding is\n" -"platform dependent, but any encoding supported by Python can be\n" -"passed. See the codecs module for the list of supported encodings.\n" +"encoding is the name of the encoding used to decode or encode the file.\n" +"This should only be used in text mode. The default encoding is UTF-8;\n" +"if UTF-8 Mode is disabled, the encoding returned by\n" +"locale.getencoding() is used instead. See the codecs module for the\n" +"list of supported encodings.\n" "\n" "errors is an optional string that specifies how encoding errors are to\n" "be handled---this argument should not be used in binary mode. Pass\n" @@ -392,4 +389,4 @@ _io_open_code(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec exit: return return_value; } -/*[clinic end generated code: output=4570f4466d711d60 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=517d977e17d38fce input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/bufferedio.c.h b/Modules/_io/clinic/bufferedio.c.h index a85dd6a20bc2c1..0cedf4a9948e9d 100644 --- a/Modules/_io/clinic/bufferedio.c.h +++ b/Modules/_io/clinic/bufferedio.c.h @@ -1197,6 +1197,42 @@ _io_BufferedRWPair_read1(PyObject *self, PyObject *const *args, Py_ssize_t nargs return return_value; } +PyDoc_STRVAR(_io_BufferedRWPair_readline__doc__, +"readline($self, size=-1, /)\n" +"--\n" +"\n" +"Read and return a line from the stream.\n" +"\n" +"If size is specified, at most size bytes will be read.\n" +"\n" +"The line terminator is always b\'\\n\'."); + +#define _IO_BUFFEREDRWPAIR_READLINE_METHODDEF \ + {"readline", _PyCFunction_CAST(_io_BufferedRWPair_readline), METH_FASTCALL, _io_BufferedRWPair_readline__doc__}, + +static PyObject * +_io_BufferedRWPair_readline_impl(rwpair *self, PyObject *size); + +static PyObject * +_io_BufferedRWPair_readline(PyObject *self, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *size = NULL; + + if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) { + goto exit; + } + if (nargs < 1) { + goto skip_optional; + } + size = args[0]; +skip_optional: + return_value = _io_BufferedRWPair_readline_impl((rwpair *)self, size); + +exit: + return return_value; +} + PyDoc_STRVAR(_io_BufferedRWPair_readinto__doc__, "readinto($self, buffer, /)\n" "--\n" @@ -1441,4 +1477,4 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs) #define _IO_BUFFEREDRWPAIR_CLOSED_GETSETDEF {"closed", (getter)_io_BufferedRWPair_closed_get, (setter)NULL, NULL}, -/*[clinic end generated code: output=d9e1eb0efa4a2431 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=1596bb353f00a1ea input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/bytesio.c.h b/Modules/_io/clinic/bytesio.c.h index b2305e35ca81e7..e48d71397367da 100644 --- a/Modules/_io/clinic/bytesio.c.h +++ b/Modules/_io/clinic/bytesio.c.h @@ -4,11 +4,11 @@ preserve #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) # include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() #endif #include "pycore_abstract.h" // _Py_convert_optional_to_ssize_t() #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_CheckPositional() +#include "pycore_runtime.h" // _Py_SINGLETON() PyDoc_STRVAR(_io_BytesIO_closed__doc__, "True if the file is closed."); @@ -716,7 +716,8 @@ static int _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue); static int -_io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) +_io_BytesIO___init___helper(PyObject *self, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { int return_value = -1; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -748,11 +749,10 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[1]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; + Py_ssize_t noptargs = nargs + nkw - 0; PyObject *initvalue = NULL; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -770,6 +770,67 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) return return_value; } +static int +_io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + return _io_BytesIO___init___helper(self, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +_io_BytesIO_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *self; + int _result; + PyObject *initvalue = NULL; + + assert(_PyType_CAST(type)->tp_init == _io_BytesIO___init__); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs > 1) { + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + return NULL; + } + _result = _io_BytesIO___init___helper(self, args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + if (_result != 0) { + Py_DECREF(self); + return NULL; + } + return self; + } + if (nargs < 1) { + goto skip_optional; + } + initvalue = args[0]; +skip_optional: + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + goto exit; + } + Py_BEGIN_CRITICAL_SECTION(self); + _result = _io_BytesIO___init___impl((bytesio *)self, initvalue); + Py_END_CRITICAL_SECTION(); + if (_result != 0) { + Py_DECREF(self); + goto exit; + } + return_value = self; + +exit: + return return_value; +} + PyDoc_STRVAR(_io_BytesIO___sizeof____doc__, "__sizeof__($self, /)\n" "--\n" @@ -795,4 +856,4 @@ _io_BytesIO___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored)) } #define _IO_BYTESIO_CLOSED_GETSETDEF {"closed", (getter)_io_BytesIO_closed_get, (setter)NULL, _io_BytesIO_closed__doc__}, -/*[clinic end generated code: output=a2bc96c358d6e240 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=b9d367ad8e0b44d2 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/textio.c.h b/Modules/_io/clinic/textio.c.h index 51e75d05061799..954fee5cd2d4bf 100644 --- a/Modules/_io/clinic/textio.c.h +++ b/Modules/_io/clinic/textio.c.h @@ -4,11 +4,11 @@ preserve #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) # include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() #endif #include "pycore_abstract.h" // _Py_convert_optional_to_ssize_t() #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_CheckPositional() +#include "pycore_runtime.h" // _Py_SINGLETON() #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL() PyDoc_STRVAR(_io__TextIOBase_detach__doc__, @@ -225,7 +225,8 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self, PyObject *errors); static int -_io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject *kwargs) +_io_IncrementalNewlineDecoder___init___helper(PyObject *self, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { int return_value = -1; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -257,13 +258,12 @@ _io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject #undef KWTUPLE PyObject *argsbuf[3]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2; + Py_ssize_t noptargs = nargs + nkw - 2; PyObject *decoder; int translate; PyObject *errors = NULL; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -284,6 +284,72 @@ _io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject return return_value; } +static int +_io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + return _io_IncrementalNewlineDecoder___init___helper(self, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +_io_IncrementalNewlineDecoder_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *self; + int _result; + PyObject *decoder; + int translate; + PyObject *errors = NULL; + + assert(_PyType_CAST(type)->tp_init == _io_IncrementalNewlineDecoder___init__); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 2 || nargs > 3) { + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + return NULL; + } + _result = _io_IncrementalNewlineDecoder___init___helper(self, args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + if (_result != 0) { + Py_DECREF(self); + return NULL; + } + return self; + } + decoder = args[0]; + translate = PyObject_IsTrue(args[1]); + if (translate < 0) { + goto exit; + } + if (nargs < 3) { + goto skip_optional; + } + errors = args[2]; +skip_optional: + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + goto exit; + } + _result = _io_IncrementalNewlineDecoder___init___impl((nldecoder_object *)self, decoder, translate, errors); + if (_result != 0) { + Py_DECREF(self); + goto exit; + } + return_value = self; + +exit: + return return_value; +} + PyDoc_STRVAR(_io_IncrementalNewlineDecoder_decode__doc__, "decode($self, /, input, final=False)\n" "--\n" @@ -441,7 +507,10 @@ PyDoc_STRVAR(_io_TextIOWrapper___init____doc__, "Character and line based layer over a BufferedIOBase object, buffer.\n" "\n" "encoding gives the name of the encoding that the stream will be\n" -"decoded or encoded with. It defaults to locale.getencoding().\n" +"decoded or encoded with. It defaults to UTF-8; if UTF-8 Mode is\n" +"disabled, the encoding returned by locale.getencoding() is used\n" +"instead. See the codecs module for the list of supported\n" +"encodings.\n" "\n" "errors determines the strictness of encoding and decoding (see\n" "help(codecs.Codec) or the documentation for codecs.register) and\n" @@ -1261,4 +1330,4 @@ _io_TextIOWrapper_buffer_get(PyObject *self, void *Py_UNUSED(context)) #define _IO_TEXTIOWRAPPER_BUFFER_GETSETDEF {"buffer", (getter)_io_TextIOWrapper_buffer_get, (setter)NULL, NULL}, -/*[clinic end generated code: output=f3dd3fff36cd2142 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d96cd0f7c06f36f5 input=a9049054013a1b77]*/ diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 732eb28e51ba8c..a954bc5cd407d2 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -5,7 +5,6 @@ #include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include "pycore_object.h" // _PyObject_GC_UNTRACK() #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() -#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include // bool #ifdef HAVE_UNISTD_H @@ -81,8 +80,6 @@ typedef struct { modified outside of the fileio object / Python (ex. gh-90102, GH-121941, gh-109523). */ struct _Py_stat_struct *stat_atopen; - PyObject *weakreflist; - PyObject *dict; } fileio; #define PyFileIO_Check(state, op) (PyObject_TypeCheck((op), state->PyFileIO_Type)) @@ -221,7 +218,6 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) self->truncate = 0; self->stat_atopen = NULL; self->closefd = 1; - self->weakreflist = NULL; return (PyObject *) self; } @@ -562,15 +558,13 @@ fileio_traverse(PyObject *op, visitproc visit, void *arg) { fileio *self = PyFileIO_CAST(op); Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->dict); - return 0; + return PyObject_VisitManagedDict(op, visit, arg); } static int fileio_clear(PyObject *op) { - fileio *self = PyFileIO_CAST(op); - Py_CLEAR(self->dict); + PyObject_ClearManagedDict(op); return 0; } @@ -588,7 +582,7 @@ fileio_dealloc(PyObject *op) PyMem_Free(self->stat_atopen); self->stat_atopen = NULL; } - FT_CLEAR_WEAKREFS(op, self->weakreflist); + PyObject_ClearWeakRefs(op); (void)fileio_clear(op); PyTypeObject *tp = Py_TYPE(op); @@ -1370,8 +1364,6 @@ static PyGetSetDef fileio_getsetlist[] = { static PyMemberDef fileio_members[] = { {"_finalizing", Py_T_BOOL, offsetof(fileio, finalizing), 0}, - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(fileio, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(fileio, dict), Py_READONLY}, {NULL} }; diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c index 2b482687e85c5b..13de04d3b9ce77 100644 --- a/Modules/_io/iobase.c +++ b/Modules/_io/iobase.c @@ -14,7 +14,6 @@ #include "pycore_long.h" // _PyLong_GetOne() #include "pycore_object.h" // _PyType_HasFeature() #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() -#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include // offsetof() #include "_iomodule.h" @@ -33,8 +32,6 @@ class _io._RawIOBase "PyObject *" "clinic_state()->PyRawIOBase_Type" typedef struct { PyObject_HEAD - PyObject *dict; - PyObject *weakreflist; } iobase; #define iobase_CAST(op) ((iobase *)(op)) @@ -383,15 +380,13 @@ iobase_traverse(PyObject *op, visitproc visit, void *arg) { iobase *self = iobase_CAST(op); Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->dict); - return 0; + return PyObject_VisitManagedDict(op, visit, arg); } static int iobase_clear(PyObject *op) { - iobase *self = iobase_CAST(op); - Py_CLEAR(self->dict); + PyObject_ClearManagedDict(op); return 0; } @@ -400,8 +395,8 @@ iobase_clear(PyObject *op) static void iobase_dealloc(PyObject *op) { - /* NOTE: since IOBaseObject has its own dict, Python-defined attributes - are still available here for close() to use. + /* NOTE: since the managed dict is only cleared below, Python-defined + attributes are still available here for close() to use. However, if the derived class declares a __slots__, those slots are already gone. */ @@ -416,8 +411,8 @@ iobase_dealloc(PyObject *op) } PyTypeObject *tp = Py_TYPE(self); _PyObject_GC_UNTRACK(self); - FT_CLEAR_WEAKREFS(op, self->weakreflist); - Py_CLEAR(self->dict); + PyObject_ClearWeakRefs(op); + PyObject_ClearManagedDict(op); tp->tp_free(self); Py_DECREF(tp); } @@ -907,12 +902,6 @@ static PyGetSetDef iobase_getset[] = { {NULL} }; -static struct PyMemberDef iobase_members[] = { - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(iobase, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(iobase, dict), Py_READONLY}, - {NULL}, -}; - static PyType_Slot iobase_slots[] = { {Py_tp_dealloc, iobase_dealloc}, @@ -922,7 +911,6 @@ static PyType_Slot iobase_slots[] = { {Py_tp_iter, iobase_iter}, {Py_tp_iternext, iobase_iternext}, {Py_tp_methods, iobase_methods}, - {Py_tp_members, iobase_members}, {Py_tp_getset, iobase_getset}, {Py_tp_finalize, iobase_finalize}, {0, NULL}, @@ -932,7 +920,8 @@ PyType_Spec _Py_iobase_spec = { .name = "_io._IOBase", .basicsize = sizeof(iobase), .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_IMMUTABLETYPE), + Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_DICT | + Py_TPFLAGS_MANAGED_WEAKREF), .slots = iobase_slots, }; diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c index 25ebe890927848..a5687207d65e08 100644 --- a/Modules/_io/stringio.c +++ b/Modules/_io/stringio.c @@ -1,8 +1,8 @@ #include "Python.h" #include // offsetof() +#include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED() #include "pycore_object.h" #include "pycore_unicodeobject.h" // _PyUnicode_ResizeCompact() -#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include "_iomodule.h" /* Implementation note: the buffer is always at least one character longer @@ -42,8 +42,6 @@ typedef struct { PyObject *readnl; PyObject *writenl; - PyObject *dict; - PyObject *weakreflist; _PyIO_State *module_state; } stringio; @@ -675,8 +673,7 @@ stringio_traverse(PyObject *op, visitproc visit, void *arg) Py_VISIT(self->readnl); Py_VISIT(self->writenl); Py_VISIT(self->decoder); - Py_VISIT(self->dict); - return 0; + return PyObject_VisitManagedDict(op, visit, arg); } static int @@ -686,7 +683,7 @@ stringio_clear(PyObject *op) Py_CLEAR(self->readnl); Py_CLEAR(self->writenl); Py_CLEAR(self->decoder); - Py_CLEAR(self->dict); + PyObject_ClearManagedDict(op); return 0; } @@ -703,7 +700,7 @@ stringio_dealloc(PyObject *op) } PyUnicodeWriter_Discard(self->writer); (void)stringio_clear(op); - FT_CLEAR_WEAKREFS(op, self->weakreflist); + PyObject_ClearWeakRefs(op); tp->tp_free(self); Py_DECREF(tp); } @@ -930,11 +927,16 @@ _io_StringIO___getstate___impl(stringio *self) if (initvalue == NULL) return NULL; - if (self->dict == NULL) { + if (_PyObject_IsInstanceDictEmpty((PyObject *)self)) { dict = Py_NewRef(Py_None); } else { - dict = PyDict_Copy(self->dict); + dict = PyObject_GenericGetDict((PyObject *)self, NULL); + if (dict == NULL) { + Py_DECREF(initvalue); + return NULL; + } + Py_SETREF(dict, PyDict_Copy(dict)); if (dict == NULL) { Py_DECREF(initvalue); return NULL; @@ -1045,14 +1047,16 @@ _io_StringIO___setstate___impl(stringio *self, PyObject *state) Py_TYPE(dict)->tp_name); return NULL; } - if (self->dict) { - /* Alternatively, we could replace the internal dictionary - completely. However, it seems more practical to just update it. */ - if (PyDict_Update(self->dict, dict) < 0) - return NULL; + /* Alternatively, we could replace the internal dictionary + completely. However, it seems more practical to just update it. */ + PyObject *self_dict = PyObject_GenericGetDict((PyObject *)self, NULL); + if (self_dict == NULL) { + return NULL; } - else { - self->dict = Py_NewRef(dict); + int rc = PyDict_Update(self_dict, dict); + Py_DECREF(self_dict); + if (rc < 0) { + return NULL; } } @@ -1138,12 +1142,6 @@ static PyGetSetDef stringio_getset[] = { {NULL} }; -static struct PyMemberDef stringio_members[] = { - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(stringio, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(stringio, dict), Py_READONLY}, - {NULL}, -}; - static PyType_Slot stringio_slots[] = { {Py_tp_dealloc, stringio_dealloc}, {Py_tp_doc, (void *)_io_StringIO___init____doc__}, @@ -1151,7 +1149,6 @@ static PyType_Slot stringio_slots[] = { {Py_tp_clear, stringio_clear}, {Py_tp_iternext, stringio_iternext}, {Py_tp_methods, stringio_methods}, - {Py_tp_members, stringio_members}, {Py_tp_getset, stringio_getset}, {Py_tp_init, _io_StringIO___init__}, {Py_tp_new, stringio_new}, diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index b6f3f4f1711928..049f999349f0d5 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -16,7 +16,6 @@ #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() #include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_unicodeobject.h" // _PyUnicode_AsASCIIString() -#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include "_iomodule.h" @@ -229,6 +228,7 @@ struct nldecoder_object { #define nldecoder_object_CAST(op) ((nldecoder_object *)(op)) /*[clinic input] +@vectorcall _io.IncrementalNewlineDecoder.__init__ decoder: object translate: bool @@ -248,7 +248,7 @@ static int _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self, PyObject *decoder, int translate, PyObject *errors) -/*[clinic end generated code: output=fbd04d443e764ec2 input=ed547aa257616b0e]*/ +/*[clinic end generated code: output=fbd04d443e764ec2 input=7fd0efad15bc0b69]*/ { if (errors == NULL) { @@ -731,8 +731,6 @@ struct textio /* Cache raw object if it's a FileIO object */ PyObject *raw; - PyObject *weakreflist; - PyObject *dict; _PyIO_State *state; }; @@ -1153,7 +1151,10 @@ _io.TextIOWrapper.__init__ Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be -decoded or encoded with. It defaults to locale.getencoding(). +decoded or encoded with. It defaults to UTF-8; if UTF-8 Mode is +disabled, the encoding returned by locale.getencoding() is used +instead. See the codecs module for the list of supported +encodings. errors determines the strictness of encoding and decoding (see help(codecs.Codec) or the documentation for codecs.register) and @@ -1185,7 +1186,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, const char *encoding, PyObject *errors, const char *newline, int line_buffering, int write_through) -/*[clinic end generated code: output=72267c0c01032ed2 input=0f077220214c40a4]*/ +/*[clinic end generated code: output=72267c0c01032ed2 input=4f0bee5a0b2302fd]*/ { PyObject *raw, *codec_info = NULL; PyObject *res; @@ -1542,7 +1543,7 @@ textiowrapper_clear(PyObject *op) Py_CLEAR(self->errors); Py_CLEAR(self->raw); - Py_CLEAR(self->dict); + PyObject_ClearManagedDict(op); return 0; } @@ -1556,7 +1557,7 @@ textiowrapper_dealloc(PyObject *op) return; self->ok = 0; _PyObject_GC_UNTRACK(self); - FT_CLEAR_WEAKREFS(op, self->weakreflist); + PyObject_ClearWeakRefs(op); (void)textiowrapper_clear(op); tp->tp_free(self); Py_DECREF(tp); @@ -1578,8 +1579,7 @@ textiowrapper_traverse(PyObject *op, visitproc visit, void *arg) Py_VISIT(self->errors); Py_VISIT(self->raw); - Py_VISIT(self->dict); - return 0; + return PyObject_VisitManagedDict(op, visit, arg); } static PyObject * @@ -1752,95 +1752,14 @@ textiowrapper_check_write(textio *self) return Py_None; } -/*[clinic input] -@critical_section -_io.TextIOWrapper.write - text: unicode - / -[clinic start generated code]*/ - -static PyObject * -_io_TextIOWrapper_write_impl(textio *self, PyObject *text) -/*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/ +/* Consume b, which is either bytes or an ASCII string. */ +static int +textiowrapper_write_bytes(textio *self, PyObject *b, + int needflush, int text_needflush) { - PyObject *ret; - PyObject *b; - Py_ssize_t textlen; - int haslf = 0; - int needflush = 0, text_needflush = 0; - - if (textiowrapper_check_write(self) == NULL) { - return NULL; - } - - Py_INCREF(text); - - textlen = PyUnicode_GET_LENGTH(text); - - if ((self->writetranslate && self->writenl != NULL) || self->line_buffering) - if (PyUnicode_FindChar(text, '\n', 0, PyUnicode_GET_LENGTH(text), 1) != -1) - haslf = 1; - - if (haslf && self->writetranslate && self->writenl != NULL) { - PyObject *newtext = _PyObject_CallMethod(text, &_Py_ID(replace), - "ss", "\n", self->writenl); - Py_DECREF(text); - if (newtext == NULL) - return NULL; - if (!PyUnicode_Check(newtext)) { - PyErr_Format(PyExc_TypeError, - "replace() should return a str, not %T", newtext); - Py_DECREF(newtext); - return NULL; - } - /* replace() may have closed, detached or reinitialized the stream. */ - if (textiowrapper_check_write(self) == NULL) { - Py_DECREF(newtext); - return NULL; - } - text = newtext; - } - - if (self->write_through) - text_needflush = 1; - if (self->line_buffering && - (haslf || - PyUnicode_FindChar(text, '\r', 0, PyUnicode_GET_LENGTH(text), 1) != -1)) - needflush = 1; - - /* XXX What if we were just reading? */ - if (self->encodefunc != NULL) { - if (PyUnicode_IS_ASCII(text) && - // See bpo-43260 - PyUnicode_GET_LENGTH(text) <= self->chunk_size && - is_asciicompat_encoding(self->encodefunc)) { - b = Py_NewRef(text); - } - else { - b = (*self->encodefunc)((PyObject *) self, text); - } - self->encoding_start_of_stream = 0; - } - else { - /* A codec callback may reconfigure the stream. */ - PyObject *encoder = Py_NewRef(self->encoder); - b = PyObject_CallMethodOneArg(encoder, &_Py_ID(encode), text); - Py_DECREF(encoder); - } - - Py_DECREF(text); - if (b == NULL) - return NULL; - if (b != text && !PyBytes_Check(b)) { - PyErr_Format(PyExc_TypeError, - "encoder should return a bytes object, not '%.200s'", - Py_TYPE(b)->tp_name); - Py_DECREF(b); - return NULL; - } - Py_ssize_t bytes_len; - if (b == text) { + if (PyUnicode_Check(b)) { + assert(PyUnicode_IS_ASCII(b)); bytes_len = PyUnicode_GET_LENGTH(b); } else { @@ -1860,7 +1779,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) while (self->pending_bytes != NULL) { if (_textiowrapper_writeflush(self) < 0) { Py_DECREF(b); - return NULL; + return -1; } } } @@ -1874,7 +1793,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) PyObject *list = PyList_New(2); if (list == NULL) { Py_DECREF(b); - return NULL; + return -1; } // Since Python 3.12, allocating GC object won't trigger GC and release // GIL. See https://github.com/python/cpython/issues/97922 @@ -1886,7 +1805,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) else { if (PyList_Append(self->pending_bytes, b) < 0) { Py_DECREF(b); - return NULL; + return -1; } Py_DECREF(b); } @@ -1900,18 +1819,18 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) if (self->pending_bytes_count >= self->chunk_size || needflush || text_needflush) { if (_textiowrapper_writeflush(self) < 0) - return NULL; + return -1; } if (needflush) { PyObject *buffer = buffer_access_safe(self); if (buffer == NULL) { - return NULL; + return -1; } int res = _PyFile_Flush(buffer); Py_DECREF(buffer); if (res < 0) { - return NULL; + return -1; } } @@ -1921,15 +1840,112 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) } if (self->decoder) { - ret = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); + PyObject *ret = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); if (ret == NULL) - return NULL; + return -1; Py_DECREF(ret); } + return 0; +} + +static PyObject * +textiowrapper_write_text(textio *self, PyObject *text) +{ + PyObject *b; + Py_ssize_t textlen; + int haslf = 0; + int needflush = 0, text_needflush = 0; + + Py_INCREF(text); + + textlen = PyUnicode_GET_LENGTH(text); + + if ((self->writetranslate && self->writenl != NULL) || self->line_buffering) + if (PyUnicode_FindChar(text, '\n', 0, PyUnicode_GET_LENGTH(text), 1) != -1) + haslf = 1; + + if (haslf && self->writetranslate && self->writenl != NULL) { + PyObject *newtext = _PyObject_CallMethod(text, &_Py_ID(replace), + "ss", "\n", self->writenl); + Py_DECREF(text); + if (newtext == NULL) + return NULL; + if (!PyUnicode_Check(newtext)) { + PyErr_Format(PyExc_TypeError, + "replace() should return a str, not %T", newtext); + Py_DECREF(newtext); + return NULL; + } + /* replace() may have closed, detached or reinitialized the stream. */ + if (textiowrapper_check_write(self) == NULL) { + Py_DECREF(newtext); + return NULL; + } + text = newtext; + } + + if (self->write_through) + text_needflush = 1; + if (self->line_buffering && + (haslf || + PyUnicode_FindChar(text, '\r', 0, PyUnicode_GET_LENGTH(text), 1) != -1)) + needflush = 1; + + /* XXX What if we were just reading? */ + if (self->encodefunc != NULL) { + if (PyUnicode_IS_ASCII(text) && + // See bpo-43260 + PyUnicode_GET_LENGTH(text) <= self->chunk_size && + is_asciicompat_encoding(self->encodefunc)) { + b = Py_NewRef(text); + } + else { + b = (*self->encodefunc)((PyObject *) self, text); + } + self->encoding_start_of_stream = 0; + } + else { + /* A codec callback may reconfigure the stream. */ + PyObject *encoder = Py_NewRef(self->encoder); + b = PyObject_CallMethodOneArg(encoder, &_Py_ID(encode), text); + Py_DECREF(encoder); + } + + Py_DECREF(text); + if (b == NULL) + return NULL; + if (b != text && !PyBytes_Check(b)) { + PyErr_Format(PyExc_TypeError, + "encoder should return a bytes object, not '%.200s'", + Py_TYPE(b)->tp_name); + Py_DECREF(b); + return NULL; + } + + if (textiowrapper_write_bytes(self, b, needflush, text_needflush) < 0) { + return NULL; + } return PyLong_FromSsize_t(textlen); } +/*[clinic input] +@critical_section +_io.TextIOWrapper.write + text: unicode + / +[clinic start generated code]*/ + +static PyObject * +_io_TextIOWrapper_write_impl(textio *self, PyObject *text) +/*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/ +{ + if (textiowrapper_check_write(self) == NULL) { + return NULL; + } + return textiowrapper_write_text(self, text); +} + /* Steal a reference to chars and store it in the decoded_char buffer; */ static void @@ -2982,9 +2998,16 @@ _io_TextIOWrapper_tell_impl(textio *self) /* Fast search for an acceptable start point, close to our current pos */ - skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip); + double skip_estimate = self->b2cratio * chars_to_skip; + if (skip_estimate >= 0 && + skip_estimate <= PyBytes_GET_SIZE(next_input)) + { + skip_bytes = (Py_ssize_t)skip_estimate; + } + else { + skip_bytes = 0; + } skip_back = 1; - assert(skip_bytes <= PyBytes_GET_SIZE(next_input)); input = PyBytes_AS_STRING(next_input); while (skip_bytes > 0) { /* Decode up to temptative start point */ @@ -3502,6 +3525,7 @@ static PyType_Slot nldecoder_slots[] = { {Py_tp_traverse, incrementalnewlinedecoder_traverse}, {Py_tp_clear, incrementalnewlinedecoder_clear}, {Py_tp_init, _io_IncrementalNewlineDecoder___init__}, + {Py_tp_vectorcall, _io_IncrementalNewlineDecoder_vectorcall}, {0, NULL}, }; @@ -3542,8 +3566,6 @@ static PyMemberDef textiowrapper_members[] = { {"line_buffering", Py_T_BOOL, offsetof(textio, line_buffering), Py_READONLY}, {"write_through", Py_T_BOOL, offsetof(textio, write_through), Py_READONLY}, {"_finalizing", Py_T_BOOL, offsetof(textio, finalizing), 0}, - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(textio, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(textio, dict), Py_READONLY}, {NULL} }; diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c index 3da4f1df1708a7..a6e884fb9c5fc8 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -10,7 +10,6 @@ #include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include "pycore_object.h" // _PyObject_GC_UNTRACK() #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() -#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #ifdef HAVE_WINDOWS_CONSOLE_IO @@ -216,8 +215,6 @@ typedef struct { unsigned int closefd : 1; char finalizing; unsigned int blksize; - PyObject *weakreflist; - PyObject *dict; char buf[SMALLBUF]; wchar_t wbuf; } winconsoleio; @@ -300,7 +297,6 @@ winconsoleio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) self->writable = 0; self->closefd = 0; self->blksize = 0; - self->weakreflist = NULL; } return (PyObject *) self; @@ -498,15 +494,13 @@ winconsoleio_traverse(PyObject *op, visitproc visit, void *arg) { winconsoleio *self = winconsoleio_CAST(op); Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->dict); - return 0; + return PyObject_VisitManagedDict(op, visit, arg); } static int winconsoleio_clear(PyObject *op) { - winconsoleio *self = winconsoleio_CAST(op); - Py_CLEAR(self->dict); + PyObject_ClearManagedDict(op); return 0; } @@ -519,8 +513,8 @@ winconsoleio_dealloc(PyObject *op) if (_PyIOBase_finalize(op) < 0) return; _PyObject_GC_UNTRACK(self); - FT_CLEAR_WEAKREFS(op, self->weakreflist); - Py_CLEAR(self->dict); + PyObject_ClearWeakRefs(op); + PyObject_ClearManagedDict(op); tp->tp_free(self); Py_DECREF(tp); } @@ -1097,7 +1091,10 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls, len = Py_MIN(len, max_wlen * 3); while (1) { /* Fix for github issues gh-110913 and gh-82052. */ - len = _find_last_utf8_boundary(b->buf, len); + DWORD boundary = _find_last_utf8_boundary(b->buf, len); + if (boundary != 0) { + len = boundary; + } wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0); if (wlen <= max_wlen) { break; @@ -1258,8 +1255,6 @@ static PyGetSetDef winconsoleio_getsetlist[] = { static PyMemberDef winconsoleio_members[] = { {"_blksize", Py_T_UINT, offsetof(winconsoleio, blksize), 0}, {"_finalizing", Py_T_BOOL, offsetof(winconsoleio, finalizing), 0}, - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(winconsoleio, weakreflist), Py_READONLY}, - {"__dictoffset__", Py_T_PYSSIZET, offsetof(winconsoleio, dict), Py_READONLY}, {NULL} }; diff --git a/Modules/_json.c b/Modules/_json.c index 3a724a3e72b185..e79b697f06c994 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1802,7 +1802,10 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *ident = NULL; bool first = true; - if (PyDict_GET_SIZE(dct) == 0) { + // Only take the fast path for exact dicts: a subclass may keep its + // contents outside the dict storage, so PyDict_GET_SIZE could be 0 while + // the mapping is non-empty (gh-110941). + if (PyAnyDict_CheckExact(dct) && PyDict_GET_SIZE(dct) == 0) { /* Fast path */ return PyUnicodeWriter_WriteASCII(writer, "{}", 2); } diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c index 415e0afbe88125..8ad01414ad8075 100644 --- a/Modules/_posixsubprocess.c +++ b/Modules/_posixsubprocess.c @@ -373,10 +373,10 @@ _close_range_except(int start_fd, return 0; } -#if defined(HAVE_GETDENTS64) \ +#if defined(_Py_HAVE_GETDENTS64) \ || (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) -#ifdef HAVE_GETDENTS64 +#ifdef _Py_HAVE_GETDENTS64 # define py_dirent64 dirent64 #else /* It doesn't matter if d_name has room for NAME_MAX chars; we're using this @@ -392,7 +392,7 @@ struct py_dirent64 { unsigned char d_type; char d_name[256]; /* Filename (null-terminated) */ }; -#endif // !HAVE_GETDENTS64 +#endif // !_Py_HAVE_GETDENTS64 static int _brute_force_closer(int first, int last) @@ -435,7 +435,7 @@ _close_open_fds_safe(int start_fd, int *fds_to_keep, Py_ssize_t fds_to_keep_len) char buffer[sizeof(struct py_dirent64)]; Py_ssize_t bytes; while (1) { -#ifdef HAVE_GETDENTS64 +#ifdef _Py_HAVE_GETDENTS64 /* musl <1.2.4 headers expose `getdents64` as an alias for `getdents`, whose buffer parameter * is `struct dirent *`. We use `void *` to support both that interface and the glibc declaration * without an incompatible-pointer diagnostic. diff --git a/Modules/_queuemodule.c b/Modules/_queuemodule.c index 6779193e402fac..8ab5c7d5f2b88b 100644 --- a/Modules/_queuemodule.c +++ b/Modules/_queuemodule.c @@ -239,6 +239,7 @@ simplequeue_traverse(PyObject *op, visitproc visit, void *arg) } /*[clinic input] +@vectorcall @classmethod _queue.SimpleQueue.__new__ as simplequeue_new @@ -247,7 +248,7 @@ Simple, unbounded, reentrant FIFO queue. static PyObject * simplequeue_new_impl(PyTypeObject *type) -/*[clinic end generated code: output=ba97740608ba31cd input=a0674a1643e3e2fb]*/ +/*[clinic end generated code: output=ba97740608ba31cd input=9113a69fd48c8e74]*/ { simplequeueobject *self; @@ -568,6 +569,7 @@ static PyType_Slot simplequeue_slots[] = { {Py_tp_members, simplequeue_members}, {Py_tp_methods, simplequeue_methods}, {Py_tp_new, simplequeue_new}, + {Py_tp_vectorcall, simplequeue_vectorcall}, {0, NULL}, }; diff --git a/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 79fa4a92e745bd..532fed28741738 100644 --- a/Modules/_remote_debugging/_remote_debugging.h +++ b/Modules/_remote_debugging/_remote_debugging.h @@ -56,7 +56,6 @@ extern "C" { # if TARGET_OS_OSX # include # include -# define MAX_NATIVE_THREADS 4096 # endif #endif @@ -104,18 +103,7 @@ typedef struct { #ifdef MS_WINDOWS #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) -#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) #define STATUS_NO_MORE_ENTRIES ((NTSTATUS)0x8000001AL) -typedef enum _WIN32_THREADSTATE { - WIN32_THREADSTATE_INITIALIZED = 0, - WIN32_THREADSTATE_READY = 1, - WIN32_THREADSTATE_RUNNING = 2, - WIN32_THREADSTATE_STANDBY = 3, - WIN32_THREADSTATE_TERMINATED = 4, - WIN32_THREADSTATE_WAITING = 5, - WIN32_THREADSTATE_TRANSITION = 6, - WIN32_THREADSTATE_UNKNOWN = 7 -} WIN32_THREADSTATE; #endif /* ============================================================================ @@ -133,7 +121,6 @@ typedef enum _WIN32_THREADSTATE { #define SIZEOF_GEN_OBJ sizeof(PyGenObject) #define SIZEOF_INTERP_FRAME sizeof(_PyInterpreterFrame) #define SIZEOF_LLIST_NODE sizeof(struct llist_node) -#define SIZEOF_PAGE_CACHE_ENTRY sizeof(page_cache_entry_t) #define SIZEOF_PYOBJECT sizeof(PyObject) #define SIZEOF_SET_OBJ sizeof(PySetObject) #define SIZEOF_TASK_OBJ 4096 @@ -387,14 +374,6 @@ typedef struct { #ifdef Py_GIL_DISABLED uint32_t tlbc_generation; _Py_hashtable_t *tlbc_cache; -#endif -#ifdef __APPLE__ - uint64_t thread_id_offset; - int thread_id_offset_initialized; -#endif -#ifdef MS_WINDOWS - PVOID win_process_buffer; - ULONG win_process_buffer_size; #endif // Thread stopping state (only on platforms that support it) #ifdef Py_REMOTE_DEBUG_SUPPORTS_BLOCKING @@ -595,7 +574,6 @@ extern int parse_frame_object( RemoteUnwinderObject *unwinder, PyObject** result, uintptr_t address, - uintptr_t* address_of_code_object, uintptr_t* previous_frame ); @@ -604,7 +582,6 @@ extern int parse_frame_from_chunks( PyObject **result, uintptr_t address, uintptr_t *previous_frame, - uintptr_t *stackpointer, StackChunkList *chunks ); @@ -681,8 +658,6 @@ extern int find_running_frame( uintptr_t *frame ); -extern int get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread_id); - extern PyObject* unwind_stack_for_thread( RemoteUnwinderObject *unwinder, uintptr_t *current_tstate, diff --git a/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index d31f783186a2c3..165d32ad31308b 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -22,7 +22,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle) address = search_windows_map_for_section(handle, "AsyncioD", L"_asyncio", NULL); if (address == 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { + if (!_Py_RemoteDebug_IsFatalReadError()) { PyObject *exc = PyErr_GetRaisedException(); PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process."); _PyErr_ChainExceptions1(exc); @@ -33,23 +33,16 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle) address = search_linux_map_for_section(handle, "AsyncioDebug", "python", NULL); if (address == 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { + if (!_Py_RemoteDebug_IsFatalReadError()) { PyObject *exc = PyErr_GetRaisedException(); PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process."); _PyErr_ChainExceptions1(exc); } } #elif defined(__APPLE__) && TARGET_OS_OSX - // On macOS, try libpython first, then fall back to python - address = search_map_for_section(handle, "AsyncioDebug", "libpython", - NULL); - if (address == 0 && !_Py_RemoteDebug_HasPermissionError()) { - PyErr_Clear(); - address = search_map_for_section(handle, "AsyncioDebug", "python", - NULL); - } + address = search_loaded_images_for_section(handle, "AsyncioDebug", NULL); if (address == 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { + if (!_Py_RemoteDebug_IsFatalReadError()) { PyObject *exc = PyErr_GetRaisedException(); PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process."); _PyErr_ChainExceptions1(exc); @@ -99,12 +92,22 @@ ensure_async_debug_offsets(RemoteUnwinderObject *unwinder) return -1; } if (result < 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { - PyErr_Clear(); - PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available"); - set_exception_cause(unwinder, PyExc_RuntimeError, - "AsyncioDebug section unavailable - asyncio module may not be loaded in target process"); + if (_Py_RemoteDebug_IsFatalReadError()) { + return -1; + } + PyErr_Clear(); + /* Image discovery can fail without reporting that the target exited. + Read a known runtime address before reporting unavailable asyncio. */ + char runtime_byte; + if (_Py_RemoteDebug_ReadRemoteMemory(&unwinder->handle, + unwinder->runtime_start_address, sizeof(runtime_byte), + &runtime_byte) < 0) + { + return -1; } + PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available"); + set_exception_cause(unwinder, PyExc_RuntimeError, + "AsyncioDebug section unavailable - asyncio module may not be loaded in target process"); return -1; } @@ -383,8 +386,7 @@ parse_coro_chain( // Parse the previous frame using the gi_iframe from local copy uintptr_t prev_frame; uintptr_t gi_iframe_addr = coro_address + (uintptr_t)unwinder->debug_offsets.gen_object.gi_iframe; - uintptr_t address_of_code_object = 0; - if (parse_frame_object(unwinder, &name, gi_iframe_addr, &address_of_code_object, &prev_frame) < 0) { + if (parse_frame_object(unwinder, &name, gi_iframe_addr, &prev_frame) < 0) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse frame object in coro chain"); return -1; } @@ -419,13 +421,12 @@ parse_coro_chain( static PyObject* create_task_result( RemoteUnwinderObject *unwinder, - uintptr_t task_address + uintptr_t task_address, + char is_task ) { PyObject* result = NULL; PyObject *call_stack = NULL; PyObject *tn = NULL; - char task_obj[SIZEOF_TASK_OBJ]; - uintptr_t coro_addr; // Create call_stack first since it's the first tuple element call_stack = PyList_New(0); @@ -441,25 +442,27 @@ create_task_result( goto error; } - // Parse coroutine chain - if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address, - (size_t)unwinder->async_debug_offsets.asyncio_task_object.size, - task_obj) < 0) { - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object for coro chain"); - goto error; - } - - coro_addr = GET_MEMBER_NO_TAG(uintptr_t, task_obj, unwinder->async_debug_offsets.asyncio_task_object.task_coro); - - if ((void*)coro_addr != NULL) { - if (parse_coro_chain(unwinder, coro_addr, call_stack) < 0) { - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse coroutine chain"); + if (is_task) { + char task_obj[SIZEOF_TASK_OBJ]; + if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address, + (size_t)unwinder->async_debug_offsets.asyncio_task_object.size, + task_obj) < 0) { + set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object for coro chain"); goto error; } - if (PyList_Reverse(call_stack)) { - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to reverse call stack"); - goto error; + uintptr_t coro_addr = GET_MEMBER_NO_TAG(uintptr_t, task_obj, unwinder->async_debug_offsets.asyncio_task_object.task_coro); + + if ((void*)coro_addr != NULL) { + if (parse_coro_chain(unwinder, coro_addr, call_stack) < 0) { + set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse coroutine chain"); + goto error; + } + + if (PyList_Reverse(call_stack)) { + set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to reverse call stack"); + goto error; + } } } @@ -503,33 +506,10 @@ parse_task( goto error; } - if (is_task) { - result = create_task_result(unwinder, task_address); - if (!result) { - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task result"); - goto error; - } - } else { - // Create an empty CoroInfo for non-task objects - RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder); - result = PyStructSequence_New(state->CoroInfo_Type); - if (result == NULL) { - set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create empty CoroInfo"); - goto error; - } - PyObject *empty_list = PyList_New(0); - if (empty_list == NULL) { - set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create empty list"); - goto error; - } - PyObject *task_name = PyLong_FromUnsignedLongLong(task_address); - if (task_name == NULL) { - Py_DECREF(empty_list); - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task name"); - goto error; - } - PyStructSequence_SetItem(result, 0, empty_list); // This steals the reference - PyStructSequence_SetItem(result, 1, task_name); // This steals the reference + result = create_task_result(unwinder, task_address, is_task); + if (result == NULL) { + set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create task result"); + goto error; } if (PyList_Append(render_to, result)) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append task result to render list"); @@ -554,10 +534,11 @@ parse_task_awaited_by( uintptr_t task_address, PyObject *awaited_by ) { - // Read the entire TaskObj at once char task_obj[SIZEOF_TASK_OBJ]; + size_t size = unwinder->async_debug_offsets.asyncio_task_object.task_awaited_by_is_set + + sizeof(char); if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, task_address, - (size_t)unwinder->async_debug_offsets.asyncio_task_object.size, + size, task_obj) < 0) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read task object"); return -1; @@ -591,7 +572,16 @@ process_single_task_node( PyObject *result_item = NULL; PyObject *coroutine_stack = NULL; - tn = parse_task_name(unwinder, task_addr); + char is_task; + // task_is_task = 1 => task + // task_is_task = 0 => future + if (read_char(unwinder, + task_addr + unwinder->async_debug_offsets.asyncio_task_object.task_is_task, + &is_task) < 0) { + goto error; + } + tn = is_task ? parse_task_name(unwinder, task_addr) + : PyUnicode_FromFormat("Future-%p", (void *)task_addr); if (tn == NULL) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse task name in single task node"); goto error; @@ -632,6 +622,7 @@ process_single_task_node( PyStructSequence_SetItem(result_item, 1, tn); // steals ref PyStructSequence_SetItem(result_item, 2, coroutine_stack); // steals ref PyStructSequence_SetItem(result_item, 3, current_awaited_by); // steals ref + PyStructSequence_SetItem(result_item, 4, PyBool_FromLong(!is_task)); // References transferred to tuple task_id = NULL; @@ -672,37 +663,68 @@ process_single_task_node( static int process_task_waiters( RemoteUnwinderObject *unwinder, - PyObject *result + PyObject *result, + bool futures_only ) { + Py_ssize_t initial_count = futures_only ? PyList_GET_SIZE(result) : 0; + PyObject *visited = PySet_New(NULL); + if (visited == NULL) { + return -1; + } for (Py_ssize_t i = 0; i < PyList_GET_SIZE(result); i++) { PyObject *task_info = PyList_GET_ITEM(result, i); + if (PySet_Add(visited, PyStructSequence_GET_ITEM(task_info, 0)) < 0) { + goto error; + } PyObject *waiters = PyStructSequence_GET_ITEM(task_info, 3); for (Py_ssize_t j = 0; j < PyList_GET_SIZE(waiters); j++) { - if (PyList_GET_SIZE(result) >= MAX_TASK_WAITER_WALK_TASKS) { - PyErr_SetString(PyExc_RuntimeError, - "Too many task waiters (possible infinite loop)"); - set_exception_cause(unwinder, PyExc_RuntimeError, - "Task waiter walk size limit exceeded"); - return -1; - } PyObject *waiter = PyList_GET_ITEM(waiters, j); - // CoroInfo item 1 holds the waiter task address stored by parse_task(). PyObject *task_id = PyStructSequence_GET_ITEM(waiter, 1); + int seen = PySet_Contains(visited, task_id); + if (seen > 0) { + continue; + } + if (seen < 0 || PySet_Add(visited, task_id) < 0) { + goto error; + } void *task_ptr = PyLong_AsVoidPtr(task_id); if (task_ptr == NULL && PyErr_Occurred()) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse waiter task ID"); - return -1; + goto error; + } + if (futures_only) { + char is_task; + if (read_char(unwinder, + (uintptr_t)task_ptr + unwinder->async_debug_offsets.asyncio_task_object.task_is_task, + &is_task) < 0) { + goto error; + } + if (is_task) { + continue; + } + } + if (PyList_GET_SIZE(result) - initial_count >= MAX_TASK_WAITER_WALK_TASKS) { + PyErr_SetString(PyExc_RuntimeError, + "Too many task waiters (possible infinite loop)"); + set_exception_cause(unwinder, PyExc_RuntimeError, + "Task waiter walk size limit exceeded"); + goto error; } if (process_single_task_node( unwinder, (uintptr_t)task_ptr, NULL, result) < 0) { - return -1; + goto error; } } } + Py_DECREF(visited); return 0; + +error: + Py_DECREF(visited); + return -1; } /* ============================================================================ @@ -765,6 +787,10 @@ get_task_frame(RemoteUnwinderObject *unwinder, uintptr_t task_addr, uintptr_t *f if (get_coro_like_address(unwinder, running_coro_addr, &running_coro_addr) < 0) { return -1; } + if (running_coro_addr == 0) { + *frame_addr = 0; + return 0; + } *frame_addr = running_coro_addr + (uintptr_t)unwinder->debug_offsets.gen_object.gi_iframe; return 0; @@ -781,6 +807,7 @@ parse_async_frame_chain( uintptr_t address_of_thread, uintptr_t running_task_frame ) { + Py_ssize_t coro_chain_size = PyList_GET_SIZE(calls); uintptr_t address_of_current_frame; if (find_running_frame(unwinder, address_of_thread, &address_of_current_frame) < 0) { set_exception_cause(unwinder, PyExc_RuntimeError, "Running frame search failed in async chain"); @@ -795,14 +822,12 @@ parse_async_frame_chain( return -1; } PyObject* frame_info = NULL; - uintptr_t address_of_code_object; uintptr_t this_frame = address_of_current_frame; int res = parse_frame_object( unwinder, &frame_info, address_of_current_frame, - &address_of_code_object, &address_of_current_frame ); @@ -826,11 +851,12 @@ parse_async_frame_chain( // Stop at the task's own frame. Code objects are shared by // recursive calls, so they cannot identify it. if (this_frame == running_task_frame) { - break; + return PyList_SetSlice(calls, 0, coro_chain_size, NULL); } } - return 0; + // gh-159645: keep the coroutine chain if the task's frame was not found. + return PyList_SetSlice(calls, coro_chain_size, PY_SSIZE_T_MAX, NULL); } /* ============================================================================ @@ -891,11 +917,11 @@ append_awaited_by_for_thread( return 0; } -int -append_awaited_by( +static int +append_awaited_info( RemoteUnwinderObject *unwinder, unsigned long tid, - uintptr_t head_addr, + PyObject *task_list, PyObject *result) { PyObject *tid_py = PyLong_FromUnsignedLong(tid); @@ -904,24 +930,16 @@ append_awaited_by( return -1; } - PyObject* awaited_by_for_thread = PyList_New(0); - if (awaited_by_for_thread == NULL) { - Py_DECREF(tid_py); - set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create awaited_by thread list"); - return -1; - } - RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder); PyObject *result_item = PyStructSequence_New(state->AwaitedInfo_Type); if (result_item == NULL) { Py_DECREF(tid_py); - Py_DECREF(awaited_by_for_thread); set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create AwaitedInfo"); return -1; } - PyStructSequence_SetItem(result_item, 0, tid_py); // steals ref - PyStructSequence_SetItem(result_item, 1, awaited_by_for_thread); // steals ref + PyStructSequence_SetItem(result_item, 0, tid_py); + PyStructSequence_SetItem(result_item, 1, Py_NewRef(task_list)); if (PyList_Append(result, result_item)) { Py_DECREF(result_item); set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append awaited_by result item"); @@ -929,13 +947,35 @@ append_awaited_by( } Py_DECREF(result_item); - if (append_awaited_by_for_thread(unwinder, head_addr, awaited_by_for_thread)) - { + return 0; +} + +int +append_awaited_by( + RemoteUnwinderObject *unwinder, + unsigned long tid, + uintptr_t head_addr, + PyObject *result) +{ + PyObject *task_list = PyList_New(0); + if (task_list == NULL) { + set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create awaited_by thread list"); + return -1; + } + + if (append_awaited_info(unwinder, tid, task_list, result) < 0) { + Py_DECREF(task_list); + return -1; + } + + int err = append_awaited_by_for_thread(unwinder, head_addr, task_list); + Py_DECREF(task_list); + if (err < 0) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append awaited_by for thread"); return -1; } - return 0; + return process_task_waiters(unwinder, task_list, true); } /* ============================================================================ @@ -972,6 +1012,11 @@ process_running_task_chain( return -1; } + // gh-159645: non-native coroutines have no frame to look for. + if (running_task_frame == 0) { + return process_task_waiters(unwinder, result, false); + } + // Get the chain from the current frame to this task PyObject *coro_chain = PyStructSequence_GET_ITEM(task_info, 2); assert(coro_chain != NULL); @@ -987,19 +1032,13 @@ process_running_task_chain( PyObject *frame_chain = PyStructSequence_GET_ITEM(coro_info, 0); assert(frame_chain != NULL); - // Clear the coro_chain - if (PyList_Clear(frame_chain) < 0) { - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to clear coroutine chain"); - return -1; - } - // Add the chain from the current frame to this task if (parse_async_frame_chain(unwinder, frame_chain, thread_state_addr, running_task_frame) < 0) { return -1; } // Now find all tasks that are waiting for this task and process them - if (process_task_waiters(unwinder, result) < 0) { + if (process_task_waiters(unwinder, result, false) < 0) { return -1; } @@ -1035,32 +1074,9 @@ process_thread_for_async_stack_trace( return -1; } - // Create AwaitedInfo structure for this thread - PyObject *tid_py = PyLong_FromUnsignedLong(tid); - if (tid_py == NULL) { - Py_DECREF(task_list); - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create thread ID"); - return -1; - } - - RemoteDebuggingState *state = RemoteDebugging_GetStateFromObject((PyObject*)unwinder); - PyObject *awaited_info = PyStructSequence_New(state->AwaitedInfo_Type); - if (awaited_info == NULL) { - Py_DECREF(tid_py); - Py_DECREF(task_list); - set_exception_cause(unwinder, PyExc_MemoryError, "Failed to create AwaitedInfo"); - return -1; - } - - PyStructSequence_SetItem(awaited_info, 0, tid_py); // steals ref - PyStructSequence_SetItem(awaited_info, 1, task_list); // steals ref - - if (PyList_Append(result, awaited_info)) { - Py_DECREF(awaited_info); - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to append AwaitedInfo to result"); - return -1; - } - Py_DECREF(awaited_info); + int err = append_awaited_info(unwinder, tid, task_list, result); + Py_DECREF(task_list); + return err; } return 0; diff --git a/Modules/_remote_debugging/binary_io.h b/Modules/_remote_debugging/binary_io.h index 6a2c5b795823e1..b89bb9bbedf67d 100644 --- a/Modules/_remote_debugging/binary_io.h +++ b/Modules/_remote_debugging/binary_io.h @@ -2,7 +2,7 @@ * Python Remote Debugging Module - Binary I/O Header * * This header provides declarations for high-performance binary file I/O - * for profiling data with optional zstd streaming compression. + * for profiling data with optional zstd compression. ******************************************************************************/ #ifndef Py_BINARY_IO_H @@ -23,7 +23,7 @@ extern "C" { #define BINARY_FORMAT_MAGIC 0x54414348 /* "TACH" (Tachyon) in native byte order */ #define BINARY_FORMAT_MAGIC_SWAPPED 0x48434154 /* Byte-swapped magic for endianness detection */ -#define BINARY_FORMAT_VERSION 1 +#define BINARY_FORMAT_VERSION 2 /* Sentinel values for optional frame fields */ #define OPCODE_NONE 255 /* No opcode captured (u8 sentinel) */ @@ -67,17 +67,32 @@ extern "C" { static_assert(FILE_HEADER_SIZE <= FILE_HEADER_PLACEHOLDER_SIZE, "FILE_HEADER_SIZE exceeds FILE_HEADER_PLACEHOLDER_SIZE"); -/* Sample header field offsets and sizes */ -#define SMP_OFF_THREAD_ID 0 -#define SMP_SIZE_THREAD_ID sizeof(uint64_t) -#define SMP_OFF_INTERPRETER_ID (SMP_OFF_THREAD_ID + SMP_SIZE_THREAD_ID) -#define SMP_SIZE_INTERPRETER_ID sizeof(uint32_t) -#define SMP_OFF_ENCODING (SMP_OFF_INTERPRETER_ID + SMP_SIZE_INTERPRETER_ID) -#define SMP_SIZE_ENCODING sizeof(uint8_t) -#define SAMPLE_HEADER_FIXED_SIZE (SMP_OFF_ENCODING + SMP_SIZE_ENCODING) - -static_assert(SAMPLE_HEADER_FIXED_SIZE == 13, - "SAMPLE_HEADER_FIXED_SIZE must remain 13"); +/* Sample blocks */ +#define BLOCK_MAGIC "TBLK" +#define BLOCK_MAGIC_SIZE (sizeof(BLOCK_MAGIC) - 1) +#define MAX_BLOCK_SIZE (1024 * 1024) +#define MAX_BLOCK_STREAMS 4096 + +/* Block columns */ +enum { + COLUMN_STREAM_TABLE, + COLUMN_SAMPLE_STREAM_IDS, + COLUMN_TIMESTAMP_RESIDUALS, + COLUMN_STATUS_BITMAPS, + COLUMN_STACK_INSTRUCTIONS, + COLUMN_FRAME_IDS, + COLUMN_COUNT +}; + +/* Stream table entry offsets and sizes */ +#define STM_OFF_THREAD_ID 0 +#define STM_SIZE_THREAD_ID sizeof(uint64_t) +#define STM_OFF_INTERPRETER_ID (STM_OFF_THREAD_ID + STM_SIZE_THREAD_ID) +#define STM_SIZE_INTERPRETER_ID sizeof(uint32_t) +#define STREAM_ENTRY_SIZE (STM_OFF_INTERPRETER_ID + STM_SIZE_INTERPRETER_ID) + +static_assert(STREAM_ENTRY_SIZE == 12, + "STREAM_ENTRY_SIZE must remain 12"); /* Footer field offsets and sizes */ #define FTR_OFF_STRINGS 0 @@ -128,14 +143,10 @@ static_assert(PROFILE_STATS_SIZE == 56, #define MIN_STRING_ENTRY_SIZE 1 #define MIN_FRAME_ENTRY_SIZE 7 -/* Buffer sizes: 512KB balances syscall amortization against memory use, - * and aligns well with filesystem block sizes and zstd dictionary windows */ -#define WRITE_BUFFER_SIZE (512 * 1024) -#define COMPRESSED_BUFFER_SIZE (512 * 1024) - /* Compression types */ #define COMPRESSION_NONE 0 #define COMPRESSION_ZSTD 1 +#define COLUMN_CONSTANT 2 /* Column stored as one repeated byte */ /* Profiling configuration. The mode occupies the low three bits and is * stored plus one so zero remains compatible with files written before this @@ -152,17 +163,24 @@ static_assert(PROFILE_STATS_SIZE == 56, /* Stack encoding types for delta compression */ #define STACK_REPEAT 0x00 /* RLE: identical to previous, with count */ -#define STACK_FULL 0x01 /* Full stack (first sample or no match) */ -#define STACK_SUFFIX 0x02 /* Shares N frames from bottom */ -#define STACK_POP_PUSH 0x03 /* Remove M frames, add N frames */ +#define STACK_REPLACE_SUFFIX 0x01 /* Keep outer frames, append new ones */ +#define STACK_REUSE 0x02 /* Stack stored earlier in the block */ /* Maximum stack depth we'll buffer for delta encoding */ #define MAX_STACK_DEPTH 256 +/* Writer's stack cache size per block */ +#define STACK_CACHE_SIZE 4096 +#define STACK_CACHE_BUCKET_SIZE 2 +#define STACK_CACHE_BUCKET_COUNT \ + (STACK_CACHE_SIZE / STACK_CACHE_BUCKET_SIZE) + /* Initial capacities for dynamic arrays - sized to reduce reallocations */ #define INITIAL_STRING_CAPACITY 4096 #define INITIAL_FRAME_CAPACITY 4096 #define INITIAL_THREAD_CAPACITY 256 +#define INITIAL_BLOCK_CAPACITY 256 +#define INITIAL_BLOCK_STREAM_CAPACITY 16 /* ============================================================================ * STATISTICS STRUCTURES @@ -174,7 +192,7 @@ typedef struct { uint64_t repeat_samples; /* Total samples encoded via RLE */ uint64_t full_records; /* Number of full stack records */ uint64_t suffix_records; /* Number of suffix match records */ - uint64_t pop_push_records; /* Number of pop-push records */ + uint64_t reuse_records; /* Number of stack reuse records */ uint64_t total_frames_written;/* Total frame indices written */ uint64_t frames_saved; /* Frames avoided due to delta encoding */ uint64_t bytes_written; /* Total bytes written (before compression) */ @@ -186,7 +204,7 @@ typedef struct { uint64_t repeat_samples; /* Samples decoded from RLE */ uint64_t full_records; /* Full stack records decoded */ uint64_t suffix_records; /* Suffix match records decoded */ - uint64_t pop_push_records; /* Pop-push records decoded */ + uint64_t reuse_records; /* Stack reuse records decoded */ uint64_t total_samples; /* Total samples reconstructed */ uint64_t stack_reconstructions; /* Number of stack array reconstructions */ } BinaryReaderStats; @@ -245,8 +263,6 @@ typedef struct { #else void *cctx; /* Placeholder */ #endif - uint8_t *compressed_buffer; - size_t compressed_buffer_size; } ZstdCompressor; /* Frame entry - combines all frame data for better cache locality. @@ -273,22 +289,25 @@ typedef struct { uint8_t opcode; } FrameKey; -/* Thread entry - tracks per-thread state for delta encoding */ +/* One entry per (thread, interpreter) pair seen in the file */ typedef struct { uint64_t thread_id; - uint64_t prev_timestamp; uint32_t interpreter_id; + /* Stream index in the current block, or UINT32_MAX */ + uint32_t block_stream_index; +} ThreadEntry; + +/* Delta encoding state of a stream, reset with each block */ +typedef struct { + uint64_t prev_timestamp; + size_t thread_index; + uint32_t current_repeat; + /* Previous stack for delta encoding (frame indices, innermost first) */ - uint32_t *prev_stack; size_t prev_stack_depth; - size_t prev_stack_capacity; - - /* RLE pending buffer - samples waiting to be written as a repeat group */ - uint8_t *pending_rle; - size_t pending_rle_bytes; - size_t pending_rle_samples; -} ThreadEntry; + uint32_t prev_stack[MAX_STACK_DEPTH]; +} BlockStream; /* Limit errors occur before emitting an incomplete sample. Other write * failures may leave partial records and must prevent finalization. */ @@ -298,15 +317,65 @@ typedef enum { BINARY_WRITER_BROKEN, } BinaryWriterState; +/* Byte buffer for block data */ +typedef struct { + uint8_t *data; + size_t size; + size_t capacity; +} ByteBuffer; + +/* Column size and encoding */ +typedef struct { + uint32_t decoded_size; + uint32_t stored_size; + uint8_t encoding; +} ColumnDescriptor; + +/* Block header as stored in the file */ +typedef struct { + uint32_t sample_count; + uint64_t base_timestamp; + uint8_t frame_width; + uint8_t common_status_flags; + uint8_t status_bitmap_flags; + ColumnDescriptor columns[COLUMN_COUNT]; +} BlockHeader; + +/* Complete stack that later samples can REUSE */ +typedef struct { + Py_uhash_t hash; + uint32_t offset; + uint32_t depth; +} StackCacheEntry; + +/* REPEAT instruction position and count */ +typedef struct { + uint32_t offset; + uint32_t count; +} Repeat; + +/* Samples buffered until the next flush */ +typedef struct { + ByteBuffer columns[COLUMN_COUNT]; + ByteBuffer repeats; + ByteBuffer transformed; + ByteBuffer compressed; + ByteBuffer output; + StackCacheEntry cache[STACK_CACHE_BUCKET_COUNT][STACK_CACHE_BUCKET_SIZE]; + BlockStream *streams; + size_t stream_capacity; + uint64_t base_timestamp; + uint32_t sample_count; + uint32_t stream_count; + uint32_t max_frame; + size_t size; +} SampleBlock; + /* Main binary writer structure */ typedef struct { FILE *fp; BinaryWriterState state; - - /* Write buffer for batched I/O */ - uint8_t *write_buffer; - size_t buffer_pos; - size_t buffer_size; + SampleBlock *block; /* Compression */ int compression_type; @@ -353,34 +422,57 @@ typedef struct { * BINARY READER STRUCTURES * ============================================================================ */ -/* Per-thread state for stack reconstruction during replay */ +typedef struct { + uint32_t frame_index; + PyObject *frame_info; +} ReaderFrameCacheEntry; + +/* Decoded sample block */ +typedef struct { + BlockHeader header; + uint32_t stream_count; + uint32_t frame_count; + ByteBuffer columns[COLUMN_COUNT]; + const uint32_t *stream_ids; + const uint64_t *timestamps; + const uint8_t *statuses; + const uint32_t *frames; + size_t instruction_offset; + uint32_t frame_offset; +} DecodedBlock; + +/* Per-stream replay state */ typedef struct { uint64_t thread_id; uint32_t interpreter_id; uint64_t prev_timestamp; - - /* Reconstructed stack buffer (frame indices, innermost first) */ - uint32_t *current_stack; + uint32_t current_stack[MAX_STACK_DEPTH]; size_t current_stack_depth; - size_t current_stack_capacity; -} ReaderThreadState; + uint32_t pending_repeats; + int has_stack; + PyObject *batch; + uint8_t batch_status; +} ReaderBlockStream; /* Main binary reader structure */ typedef struct { #if USE_MMAP uint8_t *mapped_data; size_t mapped_size; + size_t page_size; #else FILE *fp; - uint8_t *file_data; size_t file_size; + uint8_t *table_data; + ByteBuffer sample_window; + size_t sample_window_start; #endif /* Decompression state */ int compression_type; - /* Note: ZSTD_DCtx is not stored - created/freed during decompression */ - uint8_t *decompressed_data; - size_t decompressed_size; +#ifdef HAVE_ZSTD + ZSTD_DCtx *dctx; +#endif /* Header metadata */ uint8_t py_major; @@ -409,13 +501,24 @@ typedef struct { /* Parsed frame table: array of FrameEntry structures */ FrameEntry *frames; uint32_t frames_count; + ReaderFrameCacheEntry *frame_info_cache; + size_t frame_info_cache_size; /* Sample data region */ uint8_t *sample_data; size_t sample_data_size; - /* Per-thread state for stack reconstruction (used during replay) */ - ReaderThreadState *thread_states; + ByteBuffer block_data; + ByteBuffer block_stream_ids; + ByteBuffer block_timestamps; + ByteBuffer block_statuses; + ByteBuffer block_frames; + ReaderBlockStream *block_streams; + size_t block_stream_capacity; + int replaying; + + /* Threads seen during replay */ + ThreadEntry *thread_states; size_t thread_state_count; size_t thread_state_capacity; @@ -462,6 +565,14 @@ encode_varint_i32(uint8_t *buf, int32_t value) return encode_varint_u32(buf, zigzag); } +/* Encode signed 64-bit varint (zigzag encoding). Returns bytes written. */ +static inline size_t +encode_varint_i64(uint8_t *buf, int64_t value) +{ + uint64_t zigzag = ((uint64_t)value << 1) ^ (uint64_t)(value >> 63); + return encode_varint_u64(buf, zigzag); +} + /* Decode unsigned 64-bit varint (LEB128). Updates offset only on success. * On error (overflow or incomplete), offset is NOT updated, allowing callers * to detect errors via (offset == prev_offset) check. Sets PyErr on error. */ @@ -525,10 +636,64 @@ decode_varint_i32(const uint8_t *data, size_t *offset, size_t max_size) return (int32_t)((zigzag >> 1) ^ -(int32_t)(zigzag & 1)); } +/* Decode signed 64-bit varint (zigzag encoding). */ +static inline int64_t +decode_varint_i64(const uint8_t *data, size_t *offset, size_t max_size) +{ + size_t saved_offset = *offset; + uint64_t zigzag = decode_varint_u64(data, offset, max_size); + if (*offset == saved_offset) { + return 0; /* decode_varint_u64 already set PyErr */ + } + return (int64_t)((zigzag >> 1) ^ (0 - (zigzag & 1))); +} + /* ============================================================================ * SHARED UTILITY FUNCTIONS * ============================================================================ */ +/* Grow a byte buffer to hold at least size bytes */ +static inline int +buffer_reserve(ByteBuffer *buffer, size_t size) +{ + if (size <= buffer->capacity) { + return 0; + } + size_t capacity = buffer->capacity ? buffer->capacity + : INITIAL_BLOCK_CAPACITY; + while (capacity < size) { + if (capacity > SIZE_MAX / 2) { + PyErr_NoMemory(); + return -1; + } + capacity *= 2; + } + uint8_t *data = PyMem_Realloc(buffer->data, capacity); + if (data == NULL) { + PyErr_NoMemory(); + return -1; + } + buffer->data = data; + buffer->capacity = capacity; + return 0; +} + +static inline void +buffer_clear(ByteBuffer *buffer) +{ + PyMem_Free(buffer->data); + buffer->data = NULL; + buffer->size = 0; + buffer->capacity = 0; +} + +/* Bytes in one status bitmap */ +static inline size_t +status_bitmap_size(uint32_t sample_count) +{ + return ((size_t)sample_count + CHAR_BIT - 1) / CHAR_BIT; +} + /* Generic array growth - returns new pointer or NULL (sets PyErr_NoMemory) * Includes overflow checking for capacity doubling and allocation size. */ static inline void * @@ -716,6 +881,12 @@ void binary_reader_close(BinaryReader *reader); */ PyObject *binary_writer_get_stats(BinaryWriter *writer); +/* + * Decoded bytes of the samples buffered in the current block, not yet + * counted in the writer's bytes_written statistic. + */ +size_t binary_writer_buffered_size(BinaryWriter *writer); + /* * Get reader statistics as a Python dict. * diff --git a/Modules/_remote_debugging/binary_io_reader.c b/Modules/_remote_debugging/binary_io_reader.c index 80627db913ea21..e627feeb53bd49 100644 --- a/Modules/_remote_debugging/binary_io_reader.c +++ b/Modules/_remote_debugging/binary_io_reader.c @@ -27,15 +27,21 @@ * CONSTANTS FOR BINARY FORMAT SIZES * ============================================================================ */ -/* File structure sizes */ -#define MIN_DECOMPRESS_BUFFER_SIZE (64 * 1024) /* Minimum decompression buffer */ - /* Progress callback frequency */ #define PROGRESS_CALLBACK_INTERVAL 1000 /* Cap per-batch RLE samples to bound the timestamp list (gh-151378) */ #define MAX_RLE_BATCH_SAMPLES 8192 +#define FRAME_INFO_CACHE_SIZE 1024 + +#if !USE_MMAP +/* The block parser limits stored columns to MAX_BLOCK_SIZE */ +#define MAX_STORED_BLOCK_SIZE (MAX_BLOCK_SIZE + 1024) +/* Replay reads samples through a window that fits any block */ +#define SAMPLE_WINDOW_SIZE (2 * MAX_BLOCK_SIZE) +#endif + /* ============================================================================ * BINARY READER IMPLEMENTATION * ============================================================================ */ @@ -230,119 +236,62 @@ reader_parse_profile_stats(BinaryReader *reader, const uint8_t *data, return 0; } -#ifdef HAVE_ZSTD -/* Maximum decompression buffer size to prevent memory exhaustion (1GB) */ -#define MAX_DECOMPRESS_SIZE (1ULL << 30) - -static inline int -reader_decompress_samples(BinaryReader *reader, const uint8_t *data) +static int +reader_decompress(BinaryReader *reader, const uint8_t *compressed_data, + size_t compressed_size, ByteBuffer *output, + size_t decoded_size) { - size_t compressed_size = reader->string_table_offset - FILE_HEADER_PLACEHOLDER_SIZE; - const uint8_t *compressed_data = data + FILE_HEADER_PLACEHOLDER_SIZE; - - /* Validate compressed data region */ - if (reader->string_table_offset < FILE_HEADER_PLACEHOLDER_SIZE) { - PyErr_SetString(PyExc_ValueError, "Invalid string table offset"); - return -1; - } - - ZSTD_DCtx *dctx = ZSTD_createDCtx(); - if (!dctx) { - PyErr_SetString(PyExc_MemoryError, "Failed to create zstd decompression context"); +#ifdef HAVE_ZSTD + /* The data must be exactly one zstd frame */ + size_t frame_size = ZSTD_findFrameCompressedSize(compressed_data, + compressed_size); + if (ZSTD_isError(frame_size) || frame_size != compressed_size) { + PyErr_SetString(PyExc_ValueError, "Corrupted zstd frame header"); return -1; } - /* Try to get exact decompressed size from frame header for optimal allocation */ + /* Check the decompressed size recorded in the frame header before + * allocating for it */ unsigned long long frame_content_size = ZSTD_getFrameContentSize(compressed_data, compressed_size); - size_t alloc_size; - if (frame_content_size == ZSTD_CONTENTSIZE_ERROR) { /* Corrupted frame header - fail early */ - ZSTD_freeDCtx(dctx); PyErr_SetString(PyExc_ValueError, "Corrupted zstd frame header"); return -1; - } else if (frame_content_size != ZSTD_CONTENTSIZE_UNKNOWN && - frame_content_size <= SIZE_MAX && - frame_content_size <= MAX_DECOMPRESS_SIZE) { - alloc_size = (size_t)frame_content_size; - } else { - alloc_size = ZSTD_DStreamOutSize() * 4; - if (alloc_size < MIN_DECOMPRESS_BUFFER_SIZE) { - alloc_size = MIN_DECOMPRESS_BUFFER_SIZE; - } } - - reader->decompressed_data = PyMem_Malloc(alloc_size); - if (!reader->decompressed_data) { - ZSTD_freeDCtx(dctx); - PyErr_NoMemory(); + if (frame_content_size != decoded_size) { + PyErr_SetString(PyExc_ValueError, + "zstd frame size does not match the decoded size"); return -1; } - ZSTD_inBuffer input = { compressed_data, compressed_size, 0 }; - size_t total_output = 0; - size_t last_result = 0; - - while (input.pos < input.size) { - if (total_output >= alloc_size) { - /* Check for overflow before doubling */ - if (alloc_size > SIZE_MAX / 2 || alloc_size * 2 > MAX_DECOMPRESS_SIZE) { - PyMem_Free(reader->decompressed_data); - reader->decompressed_data = NULL; - ZSTD_freeDCtx(dctx); - PyErr_SetString(PyExc_MemoryError, "Decompressed data exceeds maximum size"); - return -1; - } - size_t new_size = alloc_size * 2; - uint8_t *new_buf = PyMem_Realloc(reader->decompressed_data, new_size); - if (!new_buf) { - PyMem_Free(reader->decompressed_data); - reader->decompressed_data = NULL; - ZSTD_freeDCtx(dctx); - PyErr_NoMemory(); - return -1; - } - reader->decompressed_data = new_buf; - alloc_size = new_size; - } - - ZSTD_outBuffer output = { - reader->decompressed_data + total_output, - alloc_size - total_output, - 0 - }; - - last_result = ZSTD_decompressStream(dctx, &output, &input); - if (ZSTD_isError(last_result)) { - PyMem_Free(reader->decompressed_data); - reader->decompressed_data = NULL; - ZSTD_freeDCtx(dctx); - PyErr_Format(PyExc_ValueError, "zstd decompression error: %s", - ZSTD_getErrorName(last_result)); - return -1; - } - - _Py_MSAN_UNPOISON(output.dst, output.pos); - total_output += output.pos; + /* Append the decoded data to the output buffer */ + if (buffer_reserve(output, output->size + decoded_size) < 0) { + return -1; + } + uint8_t *decoded = output->data + output->size; + size_t result = ZSTD_decompressDCtx(reader->dctx, decoded, decoded_size, + compressed_data, compressed_size); + if (ZSTD_isError(result)) { + PyErr_Format(PyExc_ValueError, "zstd decompression error: %s", + ZSTD_getErrorName(result)); + return -1; } - /* Verify decompression is complete (last_result == 0 means frame is complete) */ - if (last_result != 0) { - PyMem_Free(reader->decompressed_data); - reader->decompressed_data = NULL; - ZSTD_freeDCtx(dctx); + /* Verify decompression is complete */ + if (result != decoded_size) { PyErr_SetString(PyExc_ValueError, "Incomplete zstd frame: data may be truncated"); return -1; } - ZSTD_freeDCtx(dctx); - reader->decompressed_size = total_output; - reader->sample_data = reader->decompressed_data; - reader->sample_data_size = reader->decompressed_size; - + _Py_MSAN_UNPOISON(decoded, decoded_size); + output->size += decoded_size; return 0; -} +#else + PyErr_SetString(PyExc_RuntimeError, + "File uses zstd compression but zstd support not compiled in"); + return -1; #endif +} /* Reject a table/run count whose entries cannot fit in the bytes still * available; a malicious file could otherwise drive a huge allocation. @@ -362,10 +311,10 @@ reader_validate_count(const char *what, uint32_t count, } static inline int -reader_parse_string_table(BinaryReader *reader, const uint8_t *data, size_t file_size) +reader_parse_string_table(BinaryReader *reader, const uint8_t *data, + size_t size, size_t offset) { - if (reader_validate_count("string", reader->strings_count, - file_size - reader->string_table_offset, + if (reader_validate_count("string", reader->strings_count, size - offset, MIN_STRING_ENTRY_SIZE) < 0) { return -1; } @@ -376,15 +325,14 @@ reader_parse_string_table(BinaryReader *reader, const uint8_t *data, size_t file return -1; } - size_t offset = reader->string_table_offset; for (uint32_t i = 0; i < reader->strings_count; i++) { size_t prev_offset = offset; - uint32_t str_len = decode_varint_u32(data, &offset, file_size); + uint32_t str_len = decode_varint_u32(data, &offset, size); if (offset == prev_offset) { PyErr_SetString(PyExc_ValueError, "Malformed varint in string table"); return -1; } - if (offset > file_size || str_len > file_size - offset) { + if (offset > size || str_len > size - offset) { PyErr_SetString(PyExc_ValueError, "String table overflow"); return -1; } @@ -400,7 +348,8 @@ reader_parse_string_table(BinaryReader *reader, const uint8_t *data, size_t file } static inline int -reader_parse_frame_table(BinaryReader *reader, const uint8_t *data, size_t file_size) +reader_parse_frame_table(BinaryReader *reader, const uint8_t *data, + size_t size, size_t offset) { /* Check for integer overflow in allocation size calculation. */ #if SIZEOF_SIZE_T < 8 @@ -410,8 +359,7 @@ reader_parse_frame_table(BinaryReader *reader, const uint8_t *data, size_t file_ } #endif - if (reader_validate_count("frame", reader->frames_count, - file_size - reader->frame_table_offset, + if (reader_validate_count("frame", reader->frames_count, size - offset, MIN_FRAME_ENTRY_SIZE) < 0) { return -1; } @@ -423,34 +371,33 @@ reader_parse_frame_table(BinaryReader *reader, const uint8_t *data, size_t file_ return -1; } - size_t offset = reader->frame_table_offset; for (uint32_t i = 0; i < reader->frames_count; i++) { FrameEntry *frame = &reader->frames[i]; size_t prev_offset; prev_offset = offset; - frame->filename_idx = decode_varint_u32(data, &offset, file_size); + frame->filename_idx = decode_varint_u32(data, &offset, size); if (offset == prev_offset) { PyErr_SetString(PyExc_ValueError, "Malformed varint in frame table (filename)"); return -1; } prev_offset = offset; - frame->funcname_idx = decode_varint_u32(data, &offset, file_size); + frame->funcname_idx = decode_varint_u32(data, &offset, size); if (offset == prev_offset) { PyErr_SetString(PyExc_ValueError, "Malformed varint in frame table (funcname)"); return -1; } prev_offset = offset; - frame->lineno = decode_varint_i32(data, &offset, file_size); + frame->lineno = decode_varint_i32(data, &offset, size); if (offset == prev_offset) { PyErr_SetString(PyExc_ValueError, "Malformed varint in frame table (lineno)"); return -1; } prev_offset = offset; - int32_t end_lineno_delta = decode_varint_i32(data, &offset, file_size); + int32_t end_lineno_delta = decode_varint_i32(data, &offset, size); if (offset == prev_offset) { PyErr_SetString(PyExc_ValueError, "Malformed varint in frame table (end_lineno_delta)"); return -1; @@ -463,14 +410,14 @@ reader_parse_frame_table(BinaryReader *reader, const uint8_t *data, size_t file_ } prev_offset = offset; - frame->column = decode_varint_i32(data, &offset, file_size); + frame->column = decode_varint_i32(data, &offset, size); if (offset == prev_offset) { PyErr_SetString(PyExc_ValueError, "Malformed varint in frame table (column)"); return -1; } prev_offset = offset; - int32_t end_column_delta = decode_varint_i32(data, &offset, file_size); + int32_t end_column_delta = decode_varint_i32(data, &offset, size); if (offset == prev_offset) { PyErr_SetString(PyExc_ValueError, "Malformed varint in frame table (end_column_delta)"); return -1; @@ -483,7 +430,7 @@ reader_parse_frame_table(BinaryReader *reader, const uint8_t *data, size_t file_ } /* Read opcode byte */ - if (offset >= file_size) { + if (offset >= size) { PyErr_SetString(PyExc_ValueError, "Unexpected end of frame table (opcode)"); return -1; } @@ -493,6 +440,63 @@ reader_parse_frame_table(BinaryReader *reader, const uint8_t *data, size_t file_ return 0; } +#if USE_MMAP && defined(__linux__) && defined(MADV_DONTNEED) +static size_t +reader_discard_mapped_pages(BinaryReader *reader, size_t start, size_t end) +{ + start = _Py_SIZE_ROUND_UP(start, reader->page_size); + end = _Py_SIZE_ROUND_DOWN(end, reader->page_size); + if (start >= end) { + return start; + } + (void)madvise(reader->mapped_data + start, end - start, MADV_DONTNEED); + return end; +} +#else +static inline size_t +reader_discard_mapped_pages(BinaryReader *reader, size_t start, size_t end) +{ + return end; +} +#endif + +#if !USE_MMAP +/* Checked fread with GIL release - returns 0 on success, -1 on error */ +static int +fread_checked_allow_threads(void *data, size_t size, FILE *fp) +{ + size_t nread; + Py_BEGIN_ALLOW_THREADS + nread = fread(data, 1, size, fp); + Py_END_ALLOW_THREADS + if (nread != size) { + int err = errno; + if (ferror(fp) && err != 0) { + errno = err; + PyErr_SetFromErrno(PyExc_IOError); + } + else { + PyErr_Format(PyExc_ValueError, + "Unexpected end of file: read %zu of %zu bytes", + nread, size); + } + return -1; + } + return 0; +} + +static int +reader_read_at(BinaryReader *reader, size_t offset, uint8_t *buffer, + size_t size) +{ + if (FSEEK64(reader->fp, (file_offset_t)offset, SEEK_SET) != 0) { + PyErr_SetFromErrno(PyExc_IOError); + return -1; + } + return fread_checked_allow_threads(buffer, size, reader->fp); +} +#endif + BinaryReader * binary_reader_open(PyObject *path) { @@ -533,32 +537,19 @@ binary_reader_open(PyObject *path) goto error; } - /* Map the file into memory. - * MAP_POPULATE (Linux-only) pre-faults all pages at mmap time, which: - * - Catches issues (e.g., file truncation) immediately rather than as SIGBUS during reads - * - Eliminates page faults during subsequent reads for better performance - */ -#ifdef __linux__ - reader->mapped_data = mmap(NULL, reader->mapped_size, PROT_READ, - MAP_PRIVATE | MAP_POPULATE, fd, 0); -#else reader->mapped_data = mmap(NULL, reader->mapped_size, PROT_READ, MAP_PRIVATE, fd, 0); -#endif if (reader->mapped_data == MAP_FAILED) { reader->mapped_data = NULL; PyErr_SetFromErrno(PyExc_IOError); Py_fclose(fp); goto error; } + reader->page_size = (size_t)sysconf(_SC_PAGESIZE); /* Hint sequential access pattern - failures are non-fatal */ (void)madvise(reader->mapped_data, reader->mapped_size, MADV_SEQUENTIAL); - /* Pre-fetch pages into memory - failures are non-fatal. - * Complements MAP_POPULATE on Linux, provides benefit on macOS. */ - (void)madvise(reader->mapped_data, reader->mapped_size, MADV_WILLNEED); - /* Use transparent huge pages for large files to reduce TLB misses. * Only beneficial for files >= 32MB where TLB pressure matters. */ #ifdef MADV_HUGEPAGE @@ -570,9 +561,6 @@ binary_reader_open(PyObject *path) /* Add file descriptor-level hints for better kernel I/O scheduling */ #if defined(__linux__) && defined(POSIX_FADV_SEQUENTIAL) (void)posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); - if (reader->mapped_size > (64 * 1024 * 1024)) { - (void)posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED); - } #endif if (Py_fclose(fp) != 0) { @@ -583,7 +571,8 @@ binary_reader_open(PyObject *path) uint8_t *data = reader->mapped_data; size_t file_size = reader->mapped_size; #else - /* Use stdio on Windows */ + /* Use stdio on Windows. Read the header and tables now, samples during + * replay. */ reader->fp = Py_fopen(path, "rb"); if (!reader->fp) { goto error; @@ -607,46 +596,17 @@ binary_reader_open(PyObject *path) PyErr_SetString(PyExc_ValueError, "File too small for header"); goto error; } - if (FSEEK64(reader->fp, 0, SEEK_SET) != 0) { - PyErr_SetFromErrno(PyExc_IOError); - goto error; - } - - reader->file_data = PyMem_Malloc(reader->file_size); - if (!reader->file_data) { - PyErr_NoMemory(); - goto error; - } - - size_t nread = fread(reader->file_data, 1, reader->file_size, reader->fp); - if (nread != reader->file_size) { - int err = errno; - if (ferror(reader->fp) && err != 0) { - errno = err; - PyErr_SetFromErrno(PyExc_IOError); - } - else { - PyErr_Format(PyExc_ValueError, - "Unexpected end of file: read %zu of %zu bytes", - nread, reader->file_size); - } + uint8_t data[FILE_HEADER_PLACEHOLDER_SIZE]; + size_t file_size = reader->file_size; + if (reader_read_at(reader, 0, data, + Py_MIN(file_size, sizeof(data))) < 0) { goto error; } - - uint8_t *data = reader->file_data; - size_t file_size = reader->file_size; #endif - /* Parse header and footer */ if (reader_parse_header(reader, data, file_size) < 0) { goto error; } - if (reader_parse_footer(reader, data, file_size) < 0) { - goto error; - } - if (reader_parse_profile_stats(reader, data, file_size) < 0) { - goto error; - } /* Validate table offsets are within file bounds */ if (reader->string_table_offset > file_size) { @@ -681,10 +641,11 @@ binary_reader_open(PyObject *path) goto error; } - /* Handle compressed data */ if (reader->compression_type == COMPRESSION_ZSTD) { #ifdef HAVE_ZSTD - if (reader_decompress_samples(reader, data) < 0) { + reader->dctx = ZSTD_createDCtx(); + if (!reader->dctx) { + PyErr_SetString(PyExc_MemoryError, "Failed to create zstd decompression context"); goto error; } #else @@ -692,18 +653,46 @@ binary_reader_open(PyObject *path) "File uses zstd compression but zstd support not compiled in"); goto error; #endif - } else { - reader->sample_data = data + FILE_HEADER_PLACEHOLDER_SIZE; - reader->sample_data_size = reader->string_table_offset - FILE_HEADER_PLACEHOLDER_SIZE; } + reader->sample_data_size = + (size_t)reader->string_table_offset - FILE_HEADER_PLACEHOLDER_SIZE; - /* Parse string and frame tables */ - if (reader_parse_string_table(reader, data, file_size) < 0) { + /* Tables, statistics and footer follow the samples. The footer is + * always the last bytes of the file. */ + size_t tail_offset = Py_MIN((size_t)reader->string_table_offset, + file_size - FILE_FOOTER_SIZE); + size_t tail_size = file_size - tail_offset; +#if USE_MMAP + reader->sample_data = data + FILE_HEADER_PLACEHOLDER_SIZE; + const uint8_t *tail = data + tail_offset; +#else + reader->table_data = PyMem_Malloc(tail_size); + if (!reader->table_data) { + PyErr_NoMemory(); + goto error; + } + if (reader_read_at(reader, tail_offset, reader->table_data, + tail_size) < 0) { goto error; } - if (reader_parse_frame_table(reader, data, file_size) < 0) { + const uint8_t *tail = reader->table_data; +#endif + if (reader_parse_footer(reader, tail, tail_size) < 0 || + reader_parse_profile_stats(reader, tail, tail_size) < 0 || + reader_parse_string_table( + reader, tail, tail_size, + (size_t)reader->string_table_offset - tail_offset) < 0 || + reader_parse_frame_table( + reader, tail, tail_size, + (size_t)reader->frame_table_offset - tail_offset) < 0) { goto error; } +#if USE_MMAP + (void)reader_discard_mapped_pages(reader, tail_offset, file_size); +#else + PyMem_Free(reader->table_data); + reader->table_data = NULL; +#endif return reader; @@ -713,7 +702,7 @@ binary_reader_open(PyObject *path) } /* Get or create reader thread state for stack reconstruction */ -static ReaderThreadState * +static ThreadEntry * reader_get_or_create_thread_state(BinaryReader *reader, uint64_t thread_id, uint32_t interpreter_id) { @@ -735,192 +724,104 @@ reader_get_or_create_thread_state(BinaryReader *reader, uint64_t thread_id, if (!reader->thread_states) { reader->thread_state_capacity = 16; - reader->thread_states = PyMem_Calloc(reader->thread_state_capacity, sizeof(ReaderThreadState)); + reader->thread_states = PyMem_Calloc(reader->thread_state_capacity, sizeof(ThreadEntry)); if (!reader->thread_states) { PyErr_NoMemory(); return NULL; } - } else if (reader->thread_state_count >= reader->thread_state_capacity) { - ReaderThreadState *new_states = grow_array(reader->thread_states, - &reader->thread_state_capacity, - sizeof(ReaderThreadState)); - if (!new_states) { - return NULL; - } - reader->thread_states = new_states; + } + else if (GROW_ARRAY(reader->thread_states, reader->thread_state_count, + reader->thread_state_capacity, ThreadEntry) < 0) { + return NULL; } - ReaderThreadState *ts = &reader->thread_states[reader->thread_state_count]; - memset(ts, 0, sizeof(ReaderThreadState)); + ThreadEntry *ts = &reader->thread_states[reader->thread_state_count]; + memset(ts, 0, sizeof(ThreadEntry)); ts->thread_id = thread_id; ts->interpreter_id = interpreter_id; - ts->prev_timestamp = reader->start_time_us; - ts->current_stack_capacity = MAX_STACK_DEPTH; - ts->current_stack = PyMem_Malloc(ts->current_stack_capacity * sizeof(uint32_t)); - if (!ts->current_stack) { - PyErr_NoMemory(); - return NULL; - } - // Increment count only after successful allocation to avoid - // leaving a half-initialized entry visible to future lookups reader->thread_state_count++; return ts; } -/* ============================================================================ - * STACK DECODING HELPERS - * ============================================================================ */ - -/* Validate that final_depth fits in the stack buffer. - * Uses uint64_t to prevent overflow on 32-bit platforms. */ -static inline int -validate_stack_depth(ReaderThreadState *ts, uint64_t final_depth) -{ - if (final_depth > ts->current_stack_capacity) { - PyErr_Format(PyExc_ValueError, - "Final stack depth %llu exceeds capacity %zu", - (unsigned long long)final_depth, ts->current_stack_capacity); - return -1; - } - return 0; -} - -/* Decode a full stack from sample data. - * Updates ts->current_stack and ts->current_stack_depth. - * Returns 0 on success, -1 on error (bounds violation). */ -static inline int -decode_stack_full(ReaderThreadState *ts, const uint8_t *data, - size_t *offset, size_t max_size) +static PyObject * +reader_get_frame_info(RemoteDebuggingState *state, BinaryReader *reader, + uint32_t frame_index) { - size_t prev_offset = *offset; - uint32_t depth = decode_varint_u32(data, offset, max_size); - if (*offset == prev_offset) { - return -1; - } - - /* Validate depth against capacity to prevent buffer overflow */ - if (depth > ts->current_stack_capacity) { - PyErr_Format(PyExc_ValueError, - "Stack depth %u exceeds capacity %zu", depth, ts->current_stack_capacity); - return -1; + if (frame_index >= reader->frames_count) { + PyErr_Format(PyExc_ValueError, "Invalid frame index: %u", frame_index); + return NULL; } - - ts->current_stack_depth = depth; - for (uint32_t i = 0; i < depth; i++) { - size_t prev_offset = *offset; - ts->current_stack[i] = decode_varint_u32(data, offset, max_size); - if (*offset == prev_offset) { - return -1; + if (reader->frame_info_cache == NULL) { + size_t cache_size = Py_MIN(reader->frames_count, + (uint32_t)FRAME_INFO_CACHE_SIZE); + reader->frame_info_cache = PyMem_Calloc( + cache_size, sizeof(*reader->frame_info_cache)); + if (reader->frame_info_cache == NULL) { + PyErr_NoMemory(); + return NULL; } + reader->frame_info_cache_size = cache_size; } - return 0; -} - -/* Decode a suffix-encoded stack from sample data. - * The suffix encoding shares frames from the bottom of the previous stack. - * Returns 0 on success, -1 on error (bounds violation). */ -static inline int -decode_stack_suffix(ReaderThreadState *ts, const uint8_t *data, - size_t *offset, size_t max_size) -{ - size_t prev_offset = *offset; - uint32_t shared = decode_varint_u32(data, offset, max_size); - if (*offset == prev_offset) { - return -1; - } - prev_offset = *offset; - uint32_t new_count = decode_varint_u32(data, offset, max_size); - if (*offset == prev_offset) { - return -1; - } - - /* Validate shared doesn't exceed current stack depth */ - if (shared > ts->current_stack_depth) { - PyErr_Format(PyExc_ValueError, - "Shared count %u exceeds current stack depth %zu", - shared, ts->current_stack_depth); - return -1; + ReaderFrameCacheEntry *entry = &reader->frame_info_cache[ + frame_index % reader->frame_info_cache_size]; + if (entry->frame_info != NULL && entry->frame_index == frame_index) { + return Py_NewRef(entry->frame_info); } - /* Use uint64_t to prevent overflow on 32-bit platforms */ - uint64_t final_depth = (uint64_t)shared + new_count; - if (validate_stack_depth(ts, final_depth) < 0) { - return -1; + FrameEntry *frame = &reader->frames[frame_index]; + if (frame->filename_idx >= reader->strings_count || + frame->funcname_idx >= reader->strings_count) { + PyErr_SetString(PyExc_ValueError, "Invalid string index in frame"); + return NULL; } - /* Move shared frames (from bottom of stack) to make room for new frames at the top */ - if (new_count > 0 && shared > 0) { - /* Defensive check: ensure subtraction won't underflow. - * This should already be guaranteed by the check above, but we add - * this assertion as defense-in-depth against stack corruption. */ - if (ts->current_stack_depth < shared) { - PyErr_SetString(PyExc_ValueError, - "Internal error: stack corruption detected in suffix decoding"); - return -1; - } - size_t prev_shared_start = ts->current_stack_depth - shared; - memmove(&ts->current_stack[new_count], - &ts->current_stack[prev_shared_start], - shared * sizeof(uint32_t)); + PyObject *frame_info = PyStructSequence_New(state->FrameInfo_Type); + if (!frame_info) { + return NULL; } - for (uint32_t i = 0; i < new_count; i++) { - size_t prev_offset = *offset; - ts->current_stack[i] = decode_varint_u32(data, offset, max_size); - if (*offset == prev_offset) { - return -1; + /* Build location tuple with full position info */ + PyObject *location; + if (frame->lineno != LOCATION_NOT_AVAILABLE) { + location = Py_BuildValue("(iiii)", + frame->lineno, + frame->end_lineno, + frame->column, + frame->end_column); + if (!location) { + Py_DECREF(frame_info); + return NULL; } } - ts->current_stack_depth = final_depth; - return 0; -} - -/* Decode a pop-push encoded stack from sample data. - * Pops frames from the top and pushes new frames. - * Returns 0 on success, -1 on error (bounds violation). */ -static inline int -decode_stack_pop_push(ReaderThreadState *ts, const uint8_t *data, - size_t *offset, size_t max_size) -{ - size_t prev_offset = *offset; - uint32_t pop = decode_varint_u32(data, offset, max_size); - if (*offset == prev_offset) { - return -1; - } - prev_offset = *offset; - uint32_t push = decode_varint_u32(data, offset, max_size); - if (*offset == prev_offset) { - return -1; + else { + location = Py_NewRef(Py_None); } - size_t keep = (ts->current_stack_depth > pop) ? ts->current_stack_depth - pop : 0; - /* Use uint64_t to prevent overflow on 32-bit platforms */ - uint64_t final_depth = (uint64_t)keep + push; - if (validate_stack_depth(ts, final_depth) < 0) { - return -1; + /* Build opcode object */ + PyObject *opcode_obj; + if (frame->opcode != OPCODE_NONE) { + opcode_obj = PyLong_FromLong(frame->opcode); + if (!opcode_obj) { + Py_DECREF(location); + Py_DECREF(frame_info); + return NULL; + } } - - /* Move kept frames (from bottom of stack) to make room for new frames at the top. - * Even when push == 0, we need to move kept frames to index 0 if pop > 0. */ - if (keep > 0) { - memmove(&ts->current_stack[push], - &ts->current_stack[pop], - keep * sizeof(uint32_t)); + else { + opcode_obj = Py_NewRef(Py_None); } - for (uint32_t i = 0; i < push; i++) { - size_t prev_offset = *offset; - ts->current_stack[i] = decode_varint_u32(data, offset, max_size); - /* If offset didn't advance, varint decoding failed */ - if (*offset == prev_offset) { - return -1; - } - } - ts->current_stack_depth = final_depth; - return 0; + PyStructSequence_SetItem(frame_info, 0, Py_NewRef(reader->strings[frame->filename_idx])); + PyStructSequence_SetItem(frame_info, 1, location); + PyStructSequence_SetItem(frame_info, 2, Py_NewRef(reader->strings[frame->funcname_idx])); + PyStructSequence_SetItem(frame_info, 3, opcode_obj); + Py_XSETREF(entry->frame_info, Py_NewRef(frame_info)); + entry->frame_index = frame_index; + return frame_info; } -/* Build a Python list of FrameInfo objects from frame indices */ +/* Build a Python list of FrameInfo objects from frame indices. Stacks are + * kept outermost first, but collectors expect innermost first. */ static PyObject * build_frame_list(RemoteDebuggingState *state, BinaryReader *reader, const uint32_t *frame_indices, size_t stack_depth) @@ -931,60 +832,11 @@ build_frame_list(RemoteDebuggingState *state, BinaryReader *reader, } for (size_t k = 0; k < stack_depth; k++) { - uint32_t frame_idx = frame_indices[k]; - if (frame_idx >= reader->frames_count) { - PyErr_Format(PyExc_ValueError, "Invalid frame index: %u", frame_idx); + PyObject *frame_info = reader_get_frame_info( + state, reader, frame_indices[stack_depth - 1 - k]); + if (frame_info == NULL) { goto error; } - - FrameEntry *frame = &reader->frames[frame_idx]; - - if (frame->filename_idx >= reader->strings_count || - frame->funcname_idx >= reader->strings_count) { - PyErr_SetString(PyExc_ValueError, "Invalid string index in frame"); - goto error; - } - - PyObject *frame_info = PyStructSequence_New(state->FrameInfo_Type); - if (!frame_info) { - goto error; - } - - /* Build location tuple with full position info */ - PyObject *location; - if (frame->lineno != LOCATION_NOT_AVAILABLE) { - location = Py_BuildValue("(iiii)", - frame->lineno, - frame->end_lineno, - frame->column, - frame->end_column); - if (!location) { - Py_DECREF(frame_info); - goto error; - } - } - else { - location = Py_NewRef(Py_None); - } - - /* Build opcode object */ - PyObject *opcode_obj; - if (frame->opcode != OPCODE_NONE) { - opcode_obj = PyLong_FromLong(frame->opcode); - if (!opcode_obj) { - Py_DECREF(location); - Py_DECREF(frame_info); - goto error; - } - } - else { - opcode_obj = Py_NewRef(Py_None); - } - - PyStructSequence_SetItem(frame_info, 0, Py_NewRef(reader->strings[frame->filename_idx])); - PyStructSequence_SetItem(frame_info, 1, location); - PyStructSequence_SetItem(frame_info, 2, Py_NewRef(reader->strings[frame->funcname_idx])); - PyStructSequence_SetItem(frame_info, 3, opcode_obj); PyList_SET_ITEM(frame_list, k, frame_info); } @@ -1101,251 +953,820 @@ invoke_progress_callback(PyObject *callback, Py_ssize_t current, uint64_t total) return 0; } -Py_ssize_t -binary_reader_replay(BinaryReader *reader, PyObject *collector, PyObject *progress_callback) +/* ============================================================================ + * SAMPLE BLOCKS + * ============================================================================ */ + +static int +block_error(const char *message) { - PyObject *collect_method; - int has_collect = PyObject_GetOptionalAttrString(collector, "collect", &collect_method); - if (has_collect < 0) { - return -1; + PyErr_Format(PyExc_ValueError, "Invalid sample block: %s", message); + return -1; +} + +static inline int +block_read_varint(const ByteBuffer *column, size_t *offset, uint64_t *value) +{ + size_t prev_offset = *offset; + *value = decode_varint_u64(column->data, offset, column->size); + return *offset == prev_offset ? -1 : 0; +} + +static int +block_read_varint_u32(const ByteBuffer *column, size_t *offset, + uint32_t *value) +{ + size_t prev_offset = *offset; + *value = decode_varint_u32(column->data, offset, column->size); + return *offset == prev_offset ? -1 : 0; +} + +static int +reader_parse_block_header(BinaryReader *reader, const ByteBuffer *section, + size_t *offset, BlockHeader *header) +{ + /* Block magic "TBLK" */ + if (section->size - *offset < BLOCK_MAGIC_SIZE) { + return block_error("truncated header"); } - if (has_collect == 0) { - PyErr_SetString(PyExc_TypeError, "Collector must have a collect() method"); - return -1; + if (memcmp(section->data + *offset, BLOCK_MAGIC, BLOCK_MAGIC_SIZE) != 0) { + return block_error("wrong magic"); } - Py_DECREF(collect_method); + *offset += BLOCK_MAGIC_SIZE; - /* Get module state for struct sequence types */ - PyObject *module = PyImport_ImportModule("_remote_debugging"); - if (!module) { + /* Sample count and base timestamp (varints) */ + if (block_read_varint_u32(section, offset, &header->sample_count) < 0 || + block_read_varint(section, offset, &header->base_timestamp) < 0) { return -1; } - RemoteDebuggingState *state = RemoteDebugging_GetState(module); - Py_DECREF(module); - if (!state) { - PyErr_SetString(PyExc_RuntimeError, "Failed to get module state"); - return -1; + /* Frame ID width and the two status flag masks (1 byte each) */ + if (section->size - *offset < 3) { + return block_error("truncated header"); } - - size_t offset = 0; - Py_ssize_t replayed = 0; - - /* Initial progress callback at 0% */ - if (invoke_progress_callback(progress_callback, 0, reader->sample_count) < 0) { - return -1; + header->frame_width = section->data[(*offset)++]; + header->common_status_flags = section->data[(*offset)++]; + header->status_bitmap_flags = section->data[(*offset)++]; + if (header->sample_count == 0) { + return block_error("no samples"); + } + if (header->frame_width < 1 || header->frame_width > sizeof(uint32_t)) { + return block_error("frame ID width out of range"); + } + if (header->common_status_flags & header->status_bitmap_flags) { + return block_error("common and varying status flags overlap"); } - while (offset < reader->sample_data_size) { - /* Read thread_id (8 bytes) + interpreter_id (4 bytes) + encoding byte */ - if (reader->sample_data_size - offset < SAMPLE_HEADER_FIXED_SIZE) { - PyErr_Format(PyExc_ValueError, - "Truncated sample data: %zu trailing bytes", - reader->sample_data_size - offset); + size_t decoded_total = 0; + size_t stored_total = 0; + for (unsigned column = 0; column < COLUMN_COUNT; column++) { + ColumnDescriptor *descriptor = &header->columns[column]; + /* Decoded size (varint) and encoding (1 byte) */ + if (block_read_varint_u32(section, offset, + &descriptor->decoded_size) < 0) { return -1; } - - /* Use memcpy to avoid strict aliasing violations, then byte-swap if needed */ - uint64_t thread_id_raw; - uint32_t interpreter_id_raw; - memcpy(&thread_id_raw, &reader->sample_data[offset], SMP_SIZE_THREAD_ID); - offset += SMP_SIZE_THREAD_ID; - - memcpy(&interpreter_id_raw, &reader->sample_data[offset], SMP_SIZE_INTERPRETER_ID); - offset += SMP_SIZE_INTERPRETER_ID; - - uint64_t thread_id = SWAP64_IF(reader->needs_swap, thread_id_raw); - uint32_t interpreter_id = SWAP32_IF(reader->needs_swap, interpreter_id_raw); - - /* Get or create thread state for reconstruction */ - ReaderThreadState *ts = reader_get_or_create_thread_state(reader, thread_id, interpreter_id); - if (!ts) { - return -1; + if (*offset >= section->size) { + return block_error("truncated column descriptor"); } + descriptor->encoding = section->data[(*offset)++]; - /* Read encoding byte */ - uint8_t encoding = reader->sample_data[offset++]; - - switch (encoding) { - case STACK_REPEAT: { - /* RLE repeat: [count: varint] [delta: varint, status: 1]... */ - size_t prev_offset = offset; - uint32_t count = decode_varint_u32(reader->sample_data, &offset, reader->sample_data_size); - /* Detect varint decode failure */ - if (offset == prev_offset) { - PyErr_SetString(PyExc_ValueError, "Malformed varint for RLE count"); - return -1; - } - - /* Reject a count larger than the remaining bytes can hold; each - * RLE sample needs at least 2 bytes (1-byte min varint + status). */ - if (reader_validate_count("RLE", count, - reader->sample_data_size - offset, 2) < 0) { - return -1; + /* Stored size: implied for raw and constant, a varint for zstd */ + switch (descriptor->encoding) { + case COMPRESSION_NONE: + descriptor->stored_size = descriptor->decoded_size; + break; + case COLUMN_CONSTANT: + descriptor->stored_size = 1; + break; + case COMPRESSION_ZSTD: + if (reader->compression_type != COMPRESSION_ZSTD) { + return block_error("zstd column in an uncompressed file"); } - if ((uint64_t)count > (uint64_t)PY_SSIZE_T_MAX - (uint64_t)replayed) { - PyErr_SetString(PyExc_OverflowError, - "Sample count exceeds Py_ssize_t maximum"); + if (block_read_varint_u32(section, offset, + &descriptor->stored_size) < 0) { return -1; } + break; + default: + return block_error("unknown column encoding"); + } - reader->stats.repeat_records++; - reader->stats.repeat_samples += count; - - /* Process RLE samples, batching by status */ - PyObject *timestamps_list = NULL; - uint8_t batch_status = 0; - Py_ssize_t batch_idx = 0; - - for (uint32_t i = 0; i < count; i++) { - size_t delta_prev_offset = offset; - uint64_t delta = decode_varint_u64(reader->sample_data, &offset, reader->sample_data_size); - if (offset == delta_prev_offset) { - Py_XDECREF(timestamps_list); - PyErr_SetString(PyExc_ValueError, "Malformed varint in RLE sample data"); - return -1; - } - if (offset >= reader->sample_data_size) { - Py_XDECREF(timestamps_list); - PyErr_SetString(PyExc_ValueError, "Unexpected end of sample data in RLE"); - return -1; - } - uint8_t status = reader->sample_data[offset++]; - ts->prev_timestamp += delta; - - /* Start new batch on first sample or status change */ - if (i == 0 || status != batch_status - || batch_idx >= MAX_RLE_BATCH_SAMPLES) { - if (timestamps_list) { - int rc = emit_sample(state, collector, thread_id, interpreter_id, - batch_status, ts->current_stack, ts->current_stack_depth, - reader, timestamps_list); - Py_DECREF(timestamps_list); - if (rc < 0) { - return -1; - } - } - timestamps_list = PyList_New(0); - if (!timestamps_list) { - return -1; - } - batch_status = status; - batch_idx = 0; - } - - PyObject *ts_obj = PyLong_FromUnsignedLongLong(ts->prev_timestamp); - if (!ts_obj) { - Py_DECREF(timestamps_list); - return -1; - } - int append_rc = PyList_Append(timestamps_list, ts_obj); - Py_DECREF(ts_obj); - if (append_rc < 0) { - Py_DECREF(timestamps_list); - return -1; - } - batch_idx++; - } - - /* Emit final batch */ - if (timestamps_list) { - int rc = emit_sample(state, collector, thread_id, interpreter_id, - batch_status, ts->current_stack, ts->current_stack_depth, - reader, timestamps_list); - Py_DECREF(timestamps_list); - if (rc < 0) { - return -1; - } - } + if (descriptor->encoding != COMPRESSION_NONE && + descriptor->decoded_size == 0) { + return block_error("encoded column is empty"); + } + if (descriptor->decoded_size > MAX_BLOCK_SIZE - decoded_total) { + return block_error("decoded columns exceed the block limit"); + } + if (descriptor->stored_size > MAX_BLOCK_SIZE - stored_total) { + return block_error("stored columns exceed the block limit"); + } + decoded_total += descriptor->decoded_size; + stored_total += descriptor->stored_size; + } - replayed += count; - reader->stats.total_samples += count; + /* Each sample needs at least one byte of stream index and residual */ + if (header->sample_count > + header->columns[COLUMN_SAMPLE_STREAM_IDS].decoded_size || + header->sample_count > + header->columns[COLUMN_TIMESTAMP_RESIDUALS].decoded_size) { + return block_error("more samples than stored stream indices"); + } + return 0; +} - /* Progress callback after batch */ - if (replayed % PROGRESS_CALLBACK_INTERVAL < count) { - if (invoke_progress_callback(progress_callback, replayed, reader->sample_count) < 0) { - return -1; - } - } - break; +/* A constant stream index or residual column that holds the same one-byte + * varint for every sample is read from its stored byte, not expanded. */ +static int +constant_varint_column(const DecodedBlock *block, unsigned column, + uint8_t value) +{ + const ColumnDescriptor *descriptor = &block->header.columns[column]; + return (column == COLUMN_SAMPLE_STREAM_IDS || + column == COLUMN_TIMESTAMP_RESIDUALS) && + descriptor->encoding == COLUMN_CONSTANT && + descriptor->decoded_size == block->header.sample_count && + value < 0x80; +} + +static int +decode_block_columns(BinaryReader *reader, const ByteBuffer *section, + size_t *offset, DecodedBlock *block) +{ + const ColumnDescriptor *descriptors = block->header.columns; + /* Find where each column is stored and how much of the shared buffer + * the expanded and decompressed columns need. The buffer is reused + * across blocks and reserved up front so decoded columns never move. */ + uint8_t *stored[COLUMN_COUNT]; + size_t buffered_total = 0; + for (unsigned column = 0; column < COLUMN_COUNT; column++) { + const ColumnDescriptor *descriptor = &descriptors[column]; + if (descriptor->stored_size > section->size - *offset) { + return block_error("column extends past the sample section"); + } + stored[column] = section->data + *offset; + *offset += descriptor->stored_size; + if (descriptor->encoding == COMPRESSION_ZSTD || + (descriptor->encoding == COLUMN_CONSTANT && + !constant_varint_column(block, column, stored[column][0]))) { + buffered_total += descriptor->decoded_size; } + } + ByteBuffer *buffer = &reader->block_data; + if (buffer_reserve(buffer, buffered_total) < 0) { + return -1; + } - case STACK_FULL: - case STACK_SUFFIX: - case STACK_POP_PUSH: { - /* All three encodings share: [delta: varint] [status: 1] ... */ - size_t prev_offset = offset; - uint64_t delta = decode_varint_u64(reader->sample_data, &offset, reader->sample_data_size); - /* Detect varint decode failure: offset unchanged means error */ - if (offset == prev_offset) { - PyErr_SetString(PyExc_ValueError, "Malformed varint in sample data"); - return -1; + buffer->size = 0; + for (unsigned column = 0; column < COLUMN_COUNT; column++) { + const ColumnDescriptor *descriptor = &descriptors[column]; + uint8_t *stored_data = stored[column]; + ByteBuffer *decoded = &block->columns[column]; + decoded->size = descriptor->decoded_size; + switch (descriptor->encoding) { + case COMPRESSION_NONE: + /* Raw columns point into the file data */ + decoded->data = stored_data; + break; + case COLUMN_CONSTANT: + if (constant_varint_column(block, column, stored_data[0])) { + decoded->data = stored_data; + decoded->size = 1; + break; } - if (offset >= reader->sample_data_size) { - PyErr_SetString(PyExc_ValueError, "Unexpected end of sample data"); + decoded->data = buffer->data + buffer->size; + memset(decoded->data, stored_data[0], decoded->size); + buffer->size += decoded->size; + break; + case COMPRESSION_ZSTD: + decoded->data = buffer->data + buffer->size; + if (reader_decompress(reader, stored_data, + descriptor->stored_size, buffer, + decoded->size) < 0) { return -1; } - uint8_t status = reader->sample_data[offset++]; - ts->prev_timestamp += delta; - - if (encoding == STACK_FULL) { - if (decode_stack_full(ts, reader->sample_data, &offset, reader->sample_data_size) < 0) { - return -1; - } - reader->stats.full_records++; - } else if (encoding == STACK_SUFFIX) { - if (decode_stack_suffix(ts, reader->sample_data, &offset, reader->sample_data_size) < 0) { - return -1; - } - reader->stats.suffix_records++; - } else { /* STACK_POP_PUSH */ - if (decode_stack_pop_push(ts, reader->sample_data, &offset, reader->sample_data_size) < 0) { - return -1; - } - reader->stats.pop_push_records++; - } - reader->stats.stack_reconstructions++; + break; + default: + return block_error("unknown column encoding"); + } + } + return 0; +} - /* Build single-element timestamp list */ - PyObject *ts_obj = PyLong_FromUnsignedLongLong(ts->prev_timestamp); - if (!ts_obj) { - return -1; - } - PyObject *timestamps_list = PyList_New(1); - if (!timestamps_list) { - Py_DECREF(ts_obj); - return -1; - } - PyList_SET_ITEM(timestamps_list, 0, ts_obj); +static int +validate_block_columns(DecodedBlock *block) +{ + size_t stream_table_size = block->columns[COLUMN_STREAM_TABLE].size; + if (stream_table_size % STREAM_ENTRY_SIZE != 0) { + return block_error("stream table has a partial entry"); + } + size_t stream_count = stream_table_size / STREAM_ENTRY_SIZE; + if (stream_count == 0 || stream_count > MAX_BLOCK_STREAMS) { + return block_error("stream count out of range"); + } + block->stream_count = (uint32_t)stream_count; + + size_t frame_ids_size = block->columns[COLUMN_FRAME_IDS].size; + if (frame_ids_size % block->header.frame_width != 0) { + return block_error("frame ID column has a partial value"); + } + block->frame_count = + (uint32_t)(frame_ids_size / block->header.frame_width); + + size_t plane_size = status_bitmap_size(block->header.sample_count); + size_t plane_count = + (size_t)_Py_popcount32(block->header.status_bitmap_flags); + const ByteBuffer *statuses = &block->columns[COLUMN_STATUS_BITMAPS]; + if (statuses->size != plane_size * plane_count) { + return block_error("status bitmaps have the wrong size"); + } + unsigned used_bits = block->header.sample_count % CHAR_BIT; + for (size_t plane = 0; used_bits && plane < plane_count; plane++) { + if (statuses->data[(plane + 1) * plane_size - 1] >> used_bits) { + return block_error("status bitmap padding is not zero"); + } + } + return 0; +} - if (emit_sample(state, collector, thread_id, interpreter_id, status, - ts->current_stack, ts->current_stack_depth, reader, - timestamps_list) < 0) { - Py_DECREF(timestamps_list); - return -1; - } - Py_DECREF(timestamps_list); - if (replayed == PY_SSIZE_T_MAX) { - PyErr_SetString(PyExc_OverflowError, - "Sample count exceeds Py_ssize_t maximum"); +static int +reserve_block_streams(BinaryReader *reader, uint32_t stream_count) +{ + if (stream_count <= reader->block_stream_capacity) { + return 0; + } + ReaderBlockStream *streams = PyMem_Realloc( + reader->block_streams, stream_count * sizeof(ReaderBlockStream)); + if (streams == NULL) { + PyErr_NoMemory(); + return -1; + } + reader->block_streams = streams; + reader->block_stream_capacity = stream_count; + return 0; +} + +static int +reader_parse_stream_table(BinaryReader *reader, const DecodedBlock *block) +{ + const uint8_t *stream_table = block->columns[COLUMN_STREAM_TABLE].data; + uint64_t initial_prev_timestamp = + block->header.base_timestamp - reader->sample_interval_us; + memset(reader->block_streams, 0, + block->stream_count * sizeof(ReaderBlockStream)); + for (uint32_t i = 0; i < block->stream_count; i++) { + /* Read thread_id (8 bytes) + interpreter_id (4 bytes) */ + const uint8_t *entry = stream_table + (size_t)i * STREAM_ENTRY_SIZE; + uint64_t thread_id; + uint32_t interpreter_id; + memcpy(&thread_id, entry + STM_OFF_THREAD_ID, STM_SIZE_THREAD_ID); + memcpy(&interpreter_id, entry + STM_OFF_INTERPRETER_ID, + STM_SIZE_INTERPRETER_ID); + ThreadEntry *thread = reader_get_or_create_thread_state( + reader, SWAP64_IF(reader->needs_swap, thread_id), + SWAP32_IF(reader->needs_swap, interpreter_id)); + if (thread == NULL) { + return -1; + } + /* Each thread may appear only once in a block. An index left over + * from an earlier block points at a different thread here. */ + uint32_t previous_index = thread->block_stream_index; + if (previous_index < i && + reader->block_streams[previous_index].thread_id == + thread->thread_id && + reader->block_streams[previous_index].interpreter_id == + thread->interpreter_id) { + return block_error("stream table repeats a thread"); + } + thread->block_stream_index = i; + + /* Each stream's first expected timestamp is the base timestamp */ + ReaderBlockStream *stream = &reader->block_streams[i]; + stream->thread_id = thread->thread_id; + stream->interpreter_id = thread->interpreter_id; + stream->prev_timestamp = initial_prev_timestamp; + } + return 0; +} + +static int +decode_samples(BinaryReader *reader, DecodedBlock *block) +{ + size_t sample_count = block->header.sample_count; + if (buffer_reserve(&reader->block_stream_ids, + sample_count * sizeof(uint32_t)) < 0 || + buffer_reserve(&reader->block_timestamps, + sample_count * sizeof(uint64_t)) < 0) { + return -1; + } + uint32_t *stream_ids = (uint32_t *)reader->block_stream_ids.data; + uint64_t *timestamps = (uint64_t *)reader->block_timestamps.data; + const ByteBuffer *stream_id_column = + &block->columns[COLUMN_SAMPLE_STREAM_IDS]; + const ByteBuffer *residual_column = + &block->columns[COLUMN_TIMESTAMP_RESIDUALS]; + int constant_stream_ids = constant_varint_column( + block, COLUMN_SAMPLE_STREAM_IDS, stream_id_column->data[0]); + int constant_residuals = constant_varint_column( + block, COLUMN_TIMESTAMP_RESIDUALS, residual_column->data[0]); + size_t stream_id_offset = 0; + size_t residual_offset = 0; + int64_t constant_residual = 0; + if (constant_residuals) { + constant_residual = decode_varint_i64(residual_column->data, + &residual_offset, 1); + } + for (uint32_t sample = 0; sample < block->header.sample_count; sample++) { + uint64_t stream_id; + if (constant_stream_ids) { + stream_id = stream_id_column->data[0]; + } + else if (block_read_varint(stream_id_column, &stream_id_offset, + &stream_id) < 0) { + return -1; + } + if (stream_id >= block->stream_count) { + return block_error("sample refers to an unknown stream"); + } + int64_t residual = constant_residual; + if (!constant_residuals) { + size_t prev_offset = residual_offset; + residual = decode_varint_i64(residual_column->data, + &residual_offset, + residual_column->size); + if (residual_offset == prev_offset) { return -1; } - replayed++; - reader->stats.total_samples++; + } + + /* Timestamp is the expected time plus the stored residual */ + ReaderBlockStream *stream = &reader->block_streams[stream_id]; + stream->prev_timestamp += + reader->sample_interval_us + (uint64_t)residual; + + stream_ids[sample] = (uint32_t)stream_id; + timestamps[sample] = stream->prev_timestamp; + } + if (!constant_stream_ids && stream_id_offset != stream_id_column->size) { + return block_error("stream index column has trailing data"); + } + if (residual_offset != residual_column->size) { + return block_error("timestamp column has trailing data"); + } + block->stream_ids = stream_ids; + block->timestamps = timestamps; + return 0; +} + +static int +decode_statuses(BinaryReader *reader, DecodedBlock *block) +{ + if (buffer_reserve(&reader->block_statuses, + block->header.sample_count) < 0) { + return -1; + } + /* Start from the common flags, then add each varying flag's bitmap */ + uint8_t *statuses = reader->block_statuses.data; + memset(statuses, block->header.common_status_flags, + block->header.sample_count); + const uint8_t *plane = block->columns[COLUMN_STATUS_BITMAPS].data; + size_t plane_size = status_bitmap_size(block->header.sample_count); + for (unsigned bit = 0; bit < CHAR_BIT; bit++) { + if (!(block->header.status_bitmap_flags & (1U << bit))) { + continue; + } + for (uint32_t i = 0; i < block->header.sample_count; i++) { + statuses[i] |= ((plane[i / CHAR_BIT] >> (i % CHAR_BIT)) & 1U) + << bit; + } + plane += plane_size; + } + block->statuses = statuses; + return 0; +} + +static int +decode_frame_ids(BinaryReader *reader, DecodedBlock *block) +{ + size_t frame_count = block->frame_count; + size_t frames_size = frame_count * sizeof(uint32_t); + if (buffer_reserve(&reader->block_frames, frames_size) < 0) { + return -1; + } + uint32_t *frames = (uint32_t *)reader->block_frames.data; + block->frames = frames; + if (frame_count == 0) { + return 0; + } + + /* Combine byte b of every frame index from plane b, low byte first */ + memset(frames, 0, frames_size); + const uint8_t *plane = block->columns[COLUMN_FRAME_IDS].data; + for (unsigned byte = 0; byte < block->header.frame_width; byte++) { + for (size_t i = 0; i < frame_count; i++) { + frames[i] |= (uint32_t)plane[i] << (byte * CHAR_BIT); + } + plane += frame_count; + } + for (size_t i = 0; i < frame_count; i++) { + if (frames[i] >= reader->frames_count) { + return block_error("frame index out of range"); + } + } + return 0; +} + +static int +decode_block(BinaryReader *reader, const ByteBuffer *section, + size_t *offset, DecodedBlock *block) +{ + block->instruction_offset = 0; + block->frame_offset = 0; + if (reader_parse_block_header(reader, section, offset, + &block->header) < 0 || + decode_block_columns(reader, section, offset, block) < 0 || + validate_block_columns(block) < 0 || + reserve_block_streams(reader, block->stream_count) < 0 || + reader_parse_stream_table(reader, block) < 0 || + decode_samples(reader, block) < 0 || + decode_statuses(reader, block) < 0 || + decode_frame_ids(reader, block) < 0) { + return -1; + } + return 0; +} + +static int +flush_stream_batch(BinaryReader *reader, RemoteDebuggingState *state, + PyObject *collector, ReaderBlockStream *stream) +{ + if (stream->batch == NULL) { + return 0; + } + int result = emit_sample(state, collector, stream->thread_id, + stream->interpreter_id, stream->batch_status, + stream->current_stack, + stream->current_stack_depth, reader, + stream->batch); + Py_CLEAR(stream->batch); + return result; +} + +static int +decode_stack_repeat(BinaryReader *reader, DecodedBlock *block, + ReaderBlockStream *stream) +{ + const ByteBuffer *instructions = + &block->columns[COLUMN_STACK_INSTRUCTIONS]; + size_t *offset = &block->instruction_offset; + uint64_t count; + if (block_read_varint(instructions, offset, &count) < 0) { + return -1; + } + if (!stream->has_stack) { + return block_error("first stack of a stream cannot repeat"); + } + if (count == 0 || count > block->header.sample_count) { + return block_error("repeat count out of range"); + } + stream->pending_repeats = (uint32_t)(count - 1); + reader->stats.repeat_records++; + reader->stats.repeat_samples++; + return 0; +} + +static int +decode_stack_replace_suffix(BinaryReader *reader, DecodedBlock *block, + ReaderBlockStream *stream) +{ + const ByteBuffer *instructions = + &block->columns[COLUMN_STACK_INSTRUCTIONS]; + size_t *offset = &block->instruction_offset; + uint64_t keep, count; + if (block_read_varint(instructions, offset, &keep) < 0 || + block_read_varint(instructions, offset, &count) < 0) { + return -1; + } + if (keep > stream->current_stack_depth || count > MAX_STACK_DEPTH - keep) { + return block_error("stack prefix out of range"); + } + if (count > block->frame_count - block->frame_offset) { + return block_error("stack uses more frame IDs than stored"); + } + if (count > 0) { + memcpy(&stream->current_stack[keep], + &block->frames[block->frame_offset], count * sizeof(uint32_t)); + } + block->frame_offset += (uint32_t)count; + stream->current_stack_depth = (size_t)(keep + count); + if (keep == 0) { + reader->stats.full_records++; + } + else { + reader->stats.suffix_records++; + } + return 0; +} + +static int +decode_stack_reuse(BinaryReader *reader, DecodedBlock *block, + ReaderBlockStream *stream) +{ + const ByteBuffer *instructions = + &block->columns[COLUMN_STACK_INSTRUCTIONS]; + size_t *offset = &block->instruction_offset; + uint64_t start, count; + if (block_read_varint(instructions, offset, &start) < 0 || + block_read_varint(instructions, offset, &count) < 0) { + return -1; + } + if (count > MAX_STACK_DEPTH) { + return block_error("stack is too deep"); + } + if (start > block->frame_offset || count > block->frame_offset - start) { + return block_error("reused stack is not stored earlier"); + } + if (count > 0) { + memcpy(stream->current_stack, &block->frames[start], + count * sizeof(uint32_t)); + } + stream->current_stack_depth = (size_t)count; + reader->stats.reuse_records++; + return 0; +} + +/* Return 1 if a new stack was decoded, 0 if the stack repeats, -1 on error. */ +static int +decode_stack(BinaryReader *reader, RemoteDebuggingState *state, + PyObject *collector, DecodedBlock *block, + ReaderBlockStream *stream) +{ + /* An earlier REPEAT still covers this sample */ + if (stream->pending_repeats > 0) { + stream->pending_repeats--; + reader->stats.repeat_samples++; + return 0; + } + uint64_t encoding; + if (block_read_varint(&block->columns[COLUMN_STACK_INSTRUCTIONS], + &block->instruction_offset, &encoding) < 0) { + return -1; + } + if (encoding == STACK_REPEAT) { + return decode_stack_repeat(reader, block, stream); + } + + /* The pending batch was collected with the stack being replaced. */ + if (flush_stream_batch(reader, state, collector, stream) < 0) { + return -1; + } + int result; + switch (encoding) { + case STACK_REPLACE_SUFFIX: + result = decode_stack_replace_suffix(reader, block, stream); + break; + case STACK_REUSE: + result = decode_stack_reuse(reader, block, stream); + break; + default: + return block_error("unknown stack instruction"); + } + if (result < 0) { + return -1; + } + stream->has_stack = 1; + reader->stats.stack_reconstructions++; + return 1; +} + +static int +replay_sample(BinaryReader *reader, RemoteDebuggingState *state, + PyObject *collector, DecodedBlock *block, uint32_t sample) +{ + ReaderBlockStream *stream = + &reader->block_streams[block->stream_ids[sample]]; + uint8_t status = block->statuses[sample]; + int new_stack = decode_stack(reader, state, collector, block, stream); + if (new_stack < 0) { + return -1; + } + + /* Start a new batch when the status changes or the batch is full */ + if (stream->batch == NULL || status != stream->batch_status || + PyList_GET_SIZE(stream->batch) >= MAX_RLE_BATCH_SAMPLES) { + if (flush_stream_batch(reader, state, collector, stream) < 0) { + return -1; + } + stream->batch = PyList_New(0); + if (stream->batch == NULL) { + return -1; + } + stream->batch_status = status; + } + PyObject *timestamp = + PyLong_FromUnsignedLongLong(block->timestamps[sample]); + if (timestamp == NULL) { + return -1; + } + int result = PyList_Append(stream->batch, timestamp); + Py_DECREF(timestamp); + if (result < 0) { + return -1; + } + /* Send a new stack's sample immediately so a stream's first sample is + * not held back behind samples from other streams. */ + if (new_stack) { + return flush_stream_batch(reader, state, collector, stream); + } + return 0; +} + +static int +validate_block_consumed(BinaryReader *reader, const DecodedBlock *block) +{ + if (block->instruction_offset != + block->columns[COLUMN_STACK_INSTRUCTIONS].size) { + return block_error("stack instruction column has trailing data"); + } + if (block->frame_offset != block->frame_count) { + return block_error("frame ID column has unused values"); + } + for (uint32_t i = 0; i < block->stream_count; i++) { + if (!reader->block_streams[i].has_stack) { + return block_error("stream table has an unused entry"); + } + if (reader->block_streams[i].pending_repeats != 0) { + return block_error("repeat count extends past the block"); + } + } + return 0; +} + +static int +replay_block(BinaryReader *reader, RemoteDebuggingState *state, + PyObject *collector, PyObject *progress_callback, + DecodedBlock *block, Py_ssize_t *replayed) +{ + if ((uint64_t)block->header.sample_count > + reader->sample_count - (uint64_t)*replayed) { + return block_error("more samples than the file header declares"); + } + int result = 0; + for (uint32_t sample = 0; sample < block->header.sample_count; sample++) { + if (replay_sample(reader, state, collector, block, sample) < 0) { + result = -1; + break; + } + (*replayed)++; + reader->stats.total_samples++; + if (*replayed % PROGRESS_CALLBACK_INTERVAL == 0 && + invoke_progress_callback(progress_callback, *replayed, + reader->sample_count) < 0) { + result = -1; break; } + } + if (result == 0) { + result = validate_block_consumed(reader, block); + } - default: - PyErr_Format(PyExc_ValueError, "Unknown stack encoding: %u", encoding); + /* Send the remaining batches, or drop them after an error. */ + for (uint32_t i = 0; i < block->stream_count; i++) { + ReaderBlockStream *stream = &reader->block_streams[i]; + if (result == 0 && + flush_stream_batch(reader, state, collector, stream) < 0) { + result = -1; + } + Py_CLEAR(stream->batch); + } + + return result; +} + +/* Set section and offset to the block at sample offset pos */ +#if USE_MMAP +static int +reader_load_samples(BinaryReader *reader, size_t pos, ByteBuffer *section, + size_t *offset) +{ + *section = (ByteBuffer){reader->sample_data, reader->sample_data_size, 0}; + *offset = pos; + return 0; +} +#else +static int +reader_load_samples(BinaryReader *reader, size_t pos, ByteBuffer *section, + size_t *offset) +{ + ByteBuffer *window = &reader->sample_window; + if (pos == 0) { + /* A new replay starts at the first block */ + if (buffer_reserve(window, Py_MIN(SAMPLE_WINDOW_SIZE, + reader->sample_data_size)) < 0) { return -1; } + if (FSEEK64(reader->fp, FILE_HEADER_PLACEHOLDER_SIZE, SEEK_SET) != 0) { + PyErr_SetFromErrno(PyExc_IOError); + return -1; + } + window->size = 0; + reader->sample_window_start = 0; + } + + size_t start = reader->sample_window_start; + size_t end = start + window->size; + size_t needed = Py_MIN(MAX_STORED_BLOCK_SIZE, + reader->sample_data_size - pos); + if (end - pos < needed) { + /* Keep the unread bytes and refill the rest */ + size_t kept = end - pos; + memmove(window->data, window->data + (pos - start), kept); + size_t size = Py_MIN(SAMPLE_WINDOW_SIZE - kept, + reader->sample_data_size - end); + if (fread_checked_allow_threads(window->data + kept, size, + reader->fp) < 0) { + return -1; + } + reader->sample_window_start = pos; + window->size = kept + size; + } + *section = (ByteBuffer){window->data, window->size, 0}; + *offset = pos - reader->sample_window_start; + return 0; +} +#endif - /* Progress callback */ - if (replayed % PROGRESS_CALLBACK_INTERVAL == 0) { - if (invoke_progress_callback(progress_callback, replayed, reader->sample_count) < 0) { - return -1; - } +static int +replay_blocks(BinaryReader *reader, RemoteDebuggingState *state, + PyObject *collector, PyObject *progress_callback, + Py_ssize_t *replayed) +{ + size_t pos = 0; + size_t discarded = 0; + while (pos < reader->sample_data_size) { + ByteBuffer section; + size_t offset; + if (reader_load_samples(reader, pos, §ion, &offset) < 0) { + return -1; + } + size_t block_start = offset; + DecodedBlock block; + if (decode_block(reader, §ion, &offset, &block) < 0 || + replay_block(reader, state, collector, progress_callback, + &block, replayed) < 0) { + return -1; } + pos += offset - block_start; + discarded = reader_discard_mapped_pages( + reader, discarded, FILE_HEADER_PLACEHOLDER_SIZE + pos); + } + return 0; +} + +static Py_ssize_t +replay_all_samples(BinaryReader *reader, PyObject *collector, + PyObject *progress_callback) +{ + PyObject *collect_method; + int has_collect = PyObject_GetOptionalAttrString(collector, "collect", &collect_method); + if (has_collect < 0) { + return -1; + } + if (has_collect == 0) { + PyErr_SetString(PyExc_TypeError, "Collector must have a collect() method"); + return -1; + } + Py_DECREF(collect_method); + + /* Get module state for struct sequence types */ + PyObject *module = PyImport_ImportModule("_remote_debugging"); + if (!module) { + return -1; + } + RemoteDebuggingState *state = RemoteDebugging_GetState(module); + Py_DECREF(module); + + if (!state) { + PyErr_SetString(PyExc_RuntimeError, "Failed to get module state"); + return -1; + } + if (reader->sample_count > (uint64_t)PY_SSIZE_T_MAX) { + PyErr_SetString(PyExc_OverflowError, + "Sample count exceeds Py_ssize_t maximum"); + return -1; + } + + /* Initial progress callback at 0% */ + if (invoke_progress_callback(progress_callback, 0, reader->sample_count) < 0) { + return -1; + } + + memset(&reader->stats, 0, sizeof(reader->stats)); + Py_ssize_t replayed = 0; + if (replay_blocks(reader, state, collector, progress_callback, + &replayed) < 0) { + return -1; } if ((uint64_t)replayed != reader->sample_count) { @@ -1354,8 +1775,25 @@ binary_reader_replay(BinaryReader *reader, PyObject *collector, PyObject *progre (unsigned long long)reader->sample_count, replayed); return -1; } + return replayed; +} - /* Final progress callback at 100% */ +Py_ssize_t +binary_reader_replay(BinaryReader *reader, PyObject *collector, PyObject *progress_callback) +{ + if (reader->replaying) { + PyErr_SetString(PyExc_RuntimeError, "Reader is already replaying"); + return -1; + } + reader->replaying = 1; + Py_ssize_t replayed = replay_all_samples(reader, collector, + progress_callback); + reader->replaying = 0; + if (replayed < 0) { + return -1; + } + + /* Final progress callback at 100%; it may close the reader */ if (invoke_progress_callback(progress_callback, replayed, reader->sample_count) < 0) { return -1; } @@ -1453,8 +1891,9 @@ binary_writer_get_stats(BinaryWriter *writer) /* Calculate derived stats */ uint64_t total_records = s->repeat_records + s->full_records + - s->suffix_records + s->pop_push_records; + s->suffix_records + s->reuse_records; uint64_t total_samples = writer->total_samples; + uint64_t bytes_written = s->bytes_written + binary_writer_buffered_size(writer); uint64_t potential_frames = s->total_frames_written + s->frames_saved; double compression_ratio = (potential_frames > 0) ? (double)s->frames_saved / potential_frames * 100.0 : 0.0; @@ -1465,12 +1904,12 @@ binary_writer_get_stats(BinaryWriter *writer) "repeat_samples", s->repeat_samples, "full_records", s->full_records, "suffix_records", s->suffix_records, - "pop_push_records", s->pop_push_records, + "reuse_records", s->reuse_records, "total_records", total_records, "total_samples", total_samples, "total_frames_written", s->total_frames_written, "frames_saved", s->frames_saved, - "bytes_written", s->bytes_written, + "bytes_written", bytes_written, "frame_compression_pct", compression_ratio ); } @@ -1481,7 +1920,7 @@ binary_reader_get_stats(BinaryReader *reader) BinaryReaderStats *s = &reader->stats; uint64_t total_records = s->repeat_records + s->full_records + - s->suffix_records + s->pop_push_records; + s->suffix_records + s->reuse_records; return Py_BuildValue( "{s:K, s:K, s:K, s:K, s:K, s:K, s:K, s:K}", @@ -1489,7 +1928,7 @@ binary_reader_get_stats(BinaryReader *reader) "repeat_samples", s->repeat_samples, "full_records", s->full_records, "suffix_records", s->suffix_records, - "pop_push_records", s->pop_push_records, + "reuse_records", s->reuse_records, "total_records", total_records, "total_samples", s->total_samples, "stack_reconstructions", s->stack_reconstructions @@ -1517,14 +1956,28 @@ binary_reader_close(BinaryReader *reader) Py_fclose(reader->fp); reader->fp = NULL; } - if (reader->file_data) { - PyMem_Free(reader->file_data); - reader->file_data = NULL; - reader->file_size = 0; - } + PyMem_Free(reader->table_data); + reader->table_data = NULL; + buffer_clear(&reader->sample_window); + reader->file_size = 0; #endif - PyMem_Free(reader->decompressed_data); +#ifdef HAVE_ZSTD + ZSTD_freeDCtx(reader->dctx); +#endif + buffer_clear(&reader->block_data); + buffer_clear(&reader->block_stream_ids); + buffer_clear(&reader->block_timestamps); + buffer_clear(&reader->block_statuses); + buffer_clear(&reader->block_frames); + PyMem_Free(reader->block_streams); + + if (reader->frame_info_cache != NULL) { + for (size_t i = 0; i < reader->frame_info_cache_size; i++) { + Py_XDECREF(reader->frame_info_cache[i].frame_info); + } + PyMem_Free(reader->frame_info_cache); + } if (reader->strings) { for (uint32_t i = 0; i < reader->strings_count; i++) { @@ -1535,12 +1988,7 @@ binary_reader_close(BinaryReader *reader) PyMem_Free(reader->frames); - if (reader->thread_states) { - for (size_t i = 0; i < reader->thread_state_count; i++) { - PyMem_Free(reader->thread_states[i].current_stack); - } - PyMem_Free(reader->thread_states); - } + PyMem_Free(reader->thread_states); PyMem_Free(reader); } diff --git a/Modules/_remote_debugging/binary_io_writer.c b/Modules/_remote_debugging/binary_io_writer.c index 1794017053da50..50bbbedd5d5355 100644 --- a/Modules/_remote_debugging/binary_io_writer.c +++ b/Modules/_remote_debugging/binary_io_writer.c @@ -2,7 +2,7 @@ * Python Remote Debugging Module - Binary Writer Implementation * * High-performance binary file writer for profiling data with optional zstd - * streaming compression. + * compression. ******************************************************************************/ #ifndef Py_BUILD_CORE_MODULE @@ -11,6 +11,7 @@ #include "binary_io.h" #include "_remote_debugging.h" +#include "pycore_bitutils.h" // _Py_bit_length(), _Py_popcount32() #include "pycore_opcode_utils.h" // MAX_REAL_OPCODE #include #include @@ -27,16 +28,69 @@ * CONSTANTS FOR BINARY FORMAT SIZES * ============================================================================ */ -/* Sample header sizes */ -#define SAMPLE_HEADER_MAX_SIZE 26 /* fixed + max_varint(10) + status(1) + margin */ #define MAX_VARINT_SIZE 10 /* Maximum bytes for a varint64 */ #define MAX_VARINT_SIZE_U32 5 /* Maximum bytes for a varint32 */ -/* Frame buffer: depth varint (max 2 bytes for 256) + 256 frames * 5 bytes/varint + margin */ -#define MAX_FRAME_BUFFER_SIZE ((MAX_STACK_DEPTH * MAX_VARINT_SIZE_U32) + MAX_VARINT_SIZE_U32 + 16) -/* RLE pending buffer (per thread): one entry is a u64 delta varint + status byte */ -#define MAX_RLE_BUF_SIZE (16 * 1024) -#define MAX_RLE_ENTRY_SIZE (MAX_VARINT_SIZE + sizeof(uint8_t)) +#define MAX_BLOCK_HEADER_SIZE \ + (BLOCK_MAGIC_SIZE + MAX_VARINT_SIZE_U32 + MAX_VARINT_SIZE + \ + 3 * sizeof(uint8_t) + \ + COLUMN_COUNT * (2 * MAX_VARINT_SIZE_U32 + sizeof(uint8_t))) + +#define ZSTD_MIN_SIZE 32 +#define STACK_REUSE_MIN_FRAMES 3 + +#define MAX_SAMPLE_SIZE \ + (STREAM_ENTRY_SIZE + 5 * MAX_VARINT_SIZE + sizeof(uint8_t) + \ + MAX_STACK_DEPTH * sizeof(uint32_t) + sizeof(Repeat)) +#define MAX_BITMAP_PADDING (CHAR_BIT - 1) +#define BLOCK_FLUSH_SIZE \ + (MAX_BLOCK_SIZE - MAX_SAMPLE_SIZE - MAX_BITMAP_PADDING) + +static int +buffer_append(ByteBuffer *buffer, const void *data, size_t size) +{ + if (buffer->size > MAX_BLOCK_SIZE || + size > MAX_BLOCK_SIZE - buffer->size) { + PyErr_SetString(PyExc_RuntimeError, + "sample block buffer exceeds the writer safety limit"); + return -1; + } + if (buffer_reserve(buffer, buffer->size + size) < 0) { + return -1; + } + if (size != 0) { + memcpy(buffer->data + buffer->size, data, size); + } + buffer->size += size; + return 0; +} + +static int +buffer_append_varint(ByteBuffer *buffer, uint64_t value) +{ + uint8_t bytes[MAX_VARINT_SIZE]; + size_t size = encode_varint_u64(bytes, value); + return buffer_append(buffer, bytes, size); +} + +static int +column_write(SampleBlock *block, unsigned column, const void *data, + size_t size) +{ + if (buffer_append(&block->columns[column], data, size) < 0) { + return -1; + } + block->size += size; + return 0; +} + +static int +column_write_varint(SampleBlock *block, unsigned column, uint64_t value) +{ + uint8_t bytes[MAX_VARINT_SIZE]; + size_t size = encode_varint_u64(bytes, value); + return column_write(block, column, bytes, size); +} /* Helper macro: convert PyLong to int32, using default_val if conversion fails */ #define PYLONG_TO_INT32_OR_DEFAULT(obj, var, default_val) \ @@ -131,19 +185,6 @@ fwrite_checked_allow_threads(const void *data, size_t size, FILE *fp) return 0; } -/* Forward declaration for writer_write_bytes */ -static inline int writer_write_bytes(BinaryWriter *writer, const void *data, size_t size); - -/* Encode and write a varint u32 - returns 0 on success, -1 on error */ -static inline int -writer_write_varint_u32(BinaryWriter *writer, uint32_t value) -{ - uint8_t buf[MAX_VARINT_SIZE]; - size_t len = encode_varint_u32(buf, value); - return writer_write_bytes(writer, buf, len); -} - - /* ============================================================================ * UTILITY FUNCTIONS * ============================================================================ */ @@ -182,9 +223,9 @@ writer_init_zstd(BinaryWriter *writer) return -1; } - /* Compression level 5: better ratio for repetitive profiling data */ + /* Compression level 3: good ratio without slowing down each block flush */ size_t result = ZSTD_CCtx_setParameter(writer->zstd.cctx, - ZSTD_c_compressionLevel, 5); + ZSTD_c_compressionLevel, 3); if (ZSTD_isError(result)) { PyErr_Format(PyExc_RuntimeError, "Failed to set zstd compression level: %s", ZSTD_getErrorName(result)); @@ -193,16 +234,6 @@ writer_init_zstd(BinaryWriter *writer) return -1; } - /* Use large buffer (512KB) for fewer I/O syscalls */ - writer->zstd.compressed_buffer = PyMem_Malloc(COMPRESSED_BUFFER_SIZE); - if (!writer->zstd.compressed_buffer) { - ZSTD_freeCCtx(writer->zstd.cctx); - writer->zstd.cctx = NULL; - PyErr_NoMemory(); - return -1; - } - writer->zstd.compressed_buffer_size = COMPRESSED_BUFFER_SIZE; - return 0; #else PyErr_SetString(PyExc_RuntimeError, @@ -211,79 +242,6 @@ writer_init_zstd(BinaryWriter *writer) #endif } -static int -writer_flush_buffer(BinaryWriter *writer) -{ - if (writer->buffer_pos == 0) { - return 0; - } - -#ifdef HAVE_ZSTD - if (writer->compression_type == COMPRESSION_ZSTD) { - ZSTD_inBuffer input = { writer->write_buffer, writer->buffer_pos, 0 }; - - while (input.pos < input.size) { - ZSTD_outBuffer output = { - writer->zstd.compressed_buffer, - writer->zstd.compressed_buffer_size, - 0 - }; - - size_t result = ZSTD_compressStream2( - writer->zstd.cctx, &output, &input, ZSTD_e_continue - ); - - if (ZSTD_isError(result)) { - PyErr_Format(PyExc_IOError, "zstd compression error: %s", - ZSTD_getErrorName(result)); - return -1; - } - - _Py_MSAN_UNPOISON(writer->zstd.compressed_buffer, output.pos); - if (output.pos > 0) { - if (fwrite_checked_allow_threads(writer->zstd.compressed_buffer, output.pos, writer->fp) < 0) { - return -1; - } - } - } - } else -#endif - { - if (fwrite_checked_allow_threads(writer->write_buffer, writer->buffer_pos, writer->fp) < 0) { - return -1; - } - } - - writer->buffer_pos = 0; - return 0; -} - -static inline int -writer_write_bytes(BinaryWriter *writer, const void *data, size_t size) -{ - const uint8_t *src = (const uint8_t *)data; - size_t original_size = size; - - while (size > 0) { - size_t space = writer->buffer_size - writer->buffer_pos; - size_t to_copy = (size < space) ? size : space; - - memcpy(writer->write_buffer + writer->buffer_pos, src, to_copy); - writer->buffer_pos += to_copy; - src += to_copy; - size -= to_copy; - - if (writer->buffer_pos == writer->buffer_size) { - if (writer_flush_buffer(writer) < 0) { - return -1; - } - } - } - - writer->stats.bytes_written += original_size; - return 0; -} - /* ============================================================================ * HASH TABLE SUPPORT FUNCTIONS (using _Py_hashtable) * ============================================================================ */ @@ -393,12 +351,15 @@ writer_intern_string(BinaryWriter *writer, PyObject *string, uint32_t *index) } } - Py_ssize_t str_len; - const char *str_data = PyUnicode_AsUTF8AndSize(string, &str_len); - if (!str_data) { + /* Frame names can contain surrogates (e.g. from undecodable filenames). + * Escape them to keep the string table valid UTF-8. */ + PyObject *encoded = PyUnicode_AsEncodedString(string, "utf-8", "backslashreplace"); + if (encoded == NULL) { return -1; } + Py_ssize_t str_len = PyBytes_GET_SIZE(encoded); if ((uintmax_t)str_len > UINT32_MAX) { + Py_DECREF(encoded); writer->state = BINARY_WRITER_LIMIT_REACHED; PyErr_Format(PyExc_OverflowError, "string length %zd exceeds binary format maximum %u", @@ -408,10 +369,12 @@ writer_intern_string(BinaryWriter *writer, PyObject *string, uint32_t *index) char *str_copy = PyMem_Malloc(str_len + 1); if (!str_copy) { + Py_DECREF(encoded); PyErr_NoMemory(); return -1; } - memcpy(str_copy, str_data, str_len + 1); + memcpy(str_copy, PyBytes_AS_STRING(encoded), str_len + 1); + Py_DECREF(encoded); *index = (uint32_t)writer->string_count; @@ -481,21 +444,16 @@ writer_intern_frame(BinaryWriter *writer, const FrameEntry *entry, uint32_t *ind return 0; } -/* Get or create a thread entry for the given thread_id. - * Returns pointer to ThreadEntry, or NULL on allocation failure. - * If is_new is non-NULL, sets it to 1 if this is a new thread, 0 otherwise. */ +/* Get or create the entry for a (thread, interpreter) pair. */ static ThreadEntry * writer_get_or_create_thread_entry(BinaryWriter *writer, uint64_t thread_id, - uint32_t interpreter_id, int *is_new) + uint32_t interpreter_id) { /* Linear search is OK for small number of threads. * Key is (thread_id, interpreter_id) since same thread_id can exist in different interpreters. */ for (size_t i = 0; i < writer->thread_count; i++) { if (writer->thread_entries[i].thread_id == thread_id && writer->thread_entries[i].interpreter_id == interpreter_id) { - if (is_new) { - *is_new = 0; - } return &writer->thread_entries[i]; } } @@ -506,249 +464,620 @@ writer_get_or_create_thread_entry(BinaryWriter *writer, uint64_t thread_id, "too many threads for binary format"); return NULL; } - if (writer->thread_count >= writer->thread_capacity) { - ThreadEntry *new_entries = grow_array(writer->thread_entries, - &writer->thread_capacity, - sizeof(ThreadEntry)); - if (!new_entries) { - if (PyErr_ExceptionMatches(PyExc_OverflowError)) { - writer->state = BINARY_WRITER_LIMIT_REACHED; - } - return NULL; + if (GROW_ARRAY(writer->thread_entries, writer->thread_count, + writer->thread_capacity, ThreadEntry) < 0) { + if (PyErr_ExceptionMatches(PyExc_OverflowError)) { + writer->state = BINARY_WRITER_LIMIT_REACHED; } - writer->thread_entries = new_entries; + return NULL; } ThreadEntry *entry = &writer->thread_entries[writer->thread_count]; memset(entry, 0, sizeof(ThreadEntry)); entry->thread_id = thread_id; entry->interpreter_id = interpreter_id; - entry->prev_timestamp = writer->start_time_us; - entry->prev_stack_capacity = MAX_STACK_DEPTH; - - entry->prev_stack = PyMem_Calloc(entry->prev_stack_capacity, sizeof(uint32_t)); - if (!entry->prev_stack) { - PyErr_NoMemory(); - return NULL; - } + entry->block_stream_index = UINT32_MAX; writer->thread_count++; - if (is_new) { - *is_new = 1; - } return entry; } -/* Compare two stacks and return the encoding type and parameters. - * Sets: - * - shared_count: number of frames matching from bottom of stack - * - pop_count: frames to remove from prev stack - * - push_count: new frames to add - * - * Returns the best encoding type to use. */ static int -compare_stacks(const uint32_t *prev_stack, size_t prev_depth, - const uint32_t *curr_stack, size_t curr_depth, - size_t *shared_count, size_t *pop_count, size_t *push_count) +encode_column(BinaryWriter *writer, const ByteBuffer *decoded, + ColumnDescriptor *descriptor) { - if (prev_depth == curr_depth) { - int identical = 1; - for (size_t i = 0; i < prev_depth; i++) { - if (prev_stack[i] != curr_stack[i]) { - identical = 0; - break; - } + SampleBlock *block = writer->block; + const uint8_t *data = decoded->data; + size_t size = decoded->size; + uint8_t encoding = COMPRESSION_NONE; + if (size > 1 && memcmp(data, data + 1, size - 1) == 0) { + encoding = COLUMN_CONSTANT; + size = 1; + } +#ifdef HAVE_ZSTD + if (encoding == COMPRESSION_NONE && size >= ZSTD_MIN_SIZE && + writer->compression_type == COMPRESSION_ZSTD) { + size_t compress_bound = ZSTD_compressBound(size); + if (buffer_reserve(&block->compressed, compress_bound) < 0) { + return -1; } - if (identical) { - *shared_count = prev_depth; - *pop_count = 0; - *push_count = 0; - return STACK_REPEAT; + size_t compressed_size = ZSTD_compress2( + writer->zstd.cctx, block->compressed.data, compress_bound, + data, size); + if (ZSTD_isError(compressed_size)) { + PyErr_Format(PyExc_IOError, "zstd compression error: %s", + ZSTD_getErrorName(compressed_size)); + return -1; + } + _Py_MSAN_UNPOISON(block->compressed.data, compressed_size); + uint8_t stored_size[MAX_VARINT_SIZE]; + if (compressed_size + encode_varint_u64(stored_size, compressed_size) + < size) { + data = block->compressed.data; + size = compressed_size; + encoding = COMPRESSION_ZSTD; } } +#endif + descriptor->decoded_size = (uint32_t)decoded->size; + descriptor->stored_size = (uint32_t)size; + descriptor->encoding = encoding; + if (buffer_reserve(&block->output, block->output.size + size) < 0) { + return -1; + } + if (size != 0) { + memcpy(block->output.data + block->output.size, data, size); + } + block->output.size += size; + return 0; +} - /* Find longest common suffix (frames at the bottom/outer part of stack). - * Stacks are stored innermost-first, so suffix is at the end. */ - size_t suffix_len = 0; - size_t min_depth = (prev_depth < curr_depth) ? prev_depth : curr_depth; - - for (size_t i = 0; i < min_depth; i++) { - size_t prev_idx = prev_depth - 1 - i; - size_t curr_idx = curr_depth - 1 - i; - if (prev_stack[prev_idx] == curr_stack[curr_idx]) { - suffix_len++; - } else { - break; +static ByteBuffer * +transform_statuses(SampleBlock *block, uint8_t bitmap_flags) +{ + size_t plane_size = status_bitmap_size(block->sample_count); + unsigned plane_count = (unsigned)_Py_popcount32(bitmap_flags); + if (buffer_reserve(&block->transformed, + plane_size * plane_count) < 0) { + return NULL; + } + const uint8_t *statuses = + block->columns[COLUMN_STATUS_BITMAPS].data; + block->transformed.size = 0; + for (unsigned bit = 0; bit < CHAR_BIT; bit++) { + if (!(bitmap_flags & (1U << bit))) { + continue; + } + uint8_t *plane = block->transformed.data + block->transformed.size; + memset(plane, 0, plane_size); + for (uint32_t sample = 0; sample < block->sample_count; sample++) { + plane[sample / CHAR_BIT] |= ((statuses[sample] >> bit) & 1U) + << (sample % CHAR_BIT); } + block->transformed.size += plane_size; } + return &block->transformed; +} - *shared_count = suffix_len; - *pop_count = prev_depth - suffix_len; - *push_count = curr_depth - suffix_len; - - /* Choose best encoding based on byte cost */ - /* STACK_FULL: 1 (type) + 1-2 (depth) + sum(frame varints) */ - /* STACK_SUFFIX: 1 (type) + 1-2 (shared) + 1-2 (new_count) + sum(new frame varints) */ - /* STACK_POP_PUSH: 1 (type) + 1-2 (pop) + 1-2 (push) + sum(new frame varints) */ - - /* If no common suffix, use full stack */ - if (suffix_len == 0) { - return STACK_FULL; +static ByteBuffer * +transform_frame_ids(SampleBlock *block, unsigned width) +{ + ByteBuffer *frames = &block->columns[COLUMN_FRAME_IDS]; + size_t frame_count = frames->size / sizeof(uint32_t); + if (buffer_reserve(&block->transformed, + frame_count * width) < 0) { + return NULL; } - - /* If only adding frames (suffix == prev_depth), use SUFFIX */ - if (*pop_count == 0 && *push_count > 0) { - return STACK_SUFFIX; + const uint32_t *frame_ids = (const uint32_t *)frames->data; + for (unsigned byte = 0; frame_count && byte < width; byte++) { + uint8_t *plane = + block->transformed.data + (size_t)byte * frame_count; + for (uint32_t frame = 0; frame < frame_count; frame++) { + plane[frame] = + (uint8_t)(frame_ids[frame] >> (byte * CHAR_BIT)); + } } + block->transformed.size = frame_count * width; + return &block->transformed; +} - /* If popping and/or pushing, use POP_PUSH if it saves bytes */ - /* Heuristic: POP_PUSH is better when we're modifying top frames */ - if (*pop_count > 0 || *push_count > 0) { - /* Use full stack if sharing less than half the frames */ - if (suffix_len < curr_depth / 2) { - return STACK_FULL; +static ByteBuffer * +transform_stack_instructions(SampleBlock *block) +{ + ByteBuffer *instructions = + &block->columns[COLUMN_STACK_INSTRUCTIONS]; + size_t repeat_count = block->repeats.size / sizeof(Repeat); + if (repeat_count == 0) { + return instructions; + } + const Repeat *repeats = (const Repeat *)block->repeats.data; + size_t copied_offset = 0; + block->transformed.size = 0; + for (size_t i = 0; i < repeat_count; i++) { + if (buffer_append(&block->transformed, + instructions->data + copied_offset, + repeats[i].offset - copied_offset) < 0 || + buffer_append_varint( + &block->transformed, STACK_REPEAT) < 0 || + buffer_append_varint( + &block->transformed, repeats[i].count) < 0) { + return NULL; } - return STACK_POP_PUSH; + copied_offset = repeats[i].offset; + } + if (buffer_append(&block->transformed, + instructions->data + copied_offset, + instructions->size - copied_offset) < 0) { + return NULL; } + return &block->transformed; +} - return STACK_FULL; +static void +block_reset(BinaryWriter *writer) +{ + SampleBlock *block = writer->block; + /* Only threads with a stream in this block need resetting */ + for (uint32_t i = 0; i < block->stream_count; i++) { + size_t thread = block->streams[i].thread_index; + writer->thread_entries[thread].block_stream_index = UINT32_MAX; + } + for (unsigned column = 0; column < COLUMN_COUNT; column++) { + block->columns[column].size = 0; + } + block->sample_count = 0; + block->stream_count = 0; + block->repeats.size = 0; + block->max_frame = 0; + block->size = 0; + memset(block->cache, 0, sizeof(block->cache)); } -/* Write common sample header: thread_id(8) + interpreter_id(4) + encoding(1). - * Returns 0 on success, -1 on failure. */ -static inline int -write_sample_header(BinaryWriter *writer, ThreadEntry *entry, uint8_t encoding) +static unsigned +block_frame_width(const SampleBlock *block) +{ + if (block->max_frame == 0) { + return 1; + } + return (unsigned)(_Py_bit_length(block->max_frame) + CHAR_BIT - 1) + / CHAR_BIT; +} + +static void +block_status_flags(const SampleBlock *block, uint8_t *common_flags, + uint8_t *bitmap_flags) +{ + uint8_t common_status = UINT8_MAX; + uint8_t any_status = 0; + const uint8_t *statuses = block->columns[COLUMN_STATUS_BITMAPS].data; + for (uint32_t sample = 0; sample < block->sample_count; sample++) { + common_status &= statuses[sample]; + any_status |= statuses[sample]; + } + *common_flags = common_status; + *bitmap_flags = any_status ^ common_status; +} + +static size_t +write_block_header(const BlockHeader *header, uint8_t *bytes) { - uint8_t header[SAMPLE_HEADER_FIXED_SIZE]; - memcpy(header + SMP_OFF_THREAD_ID, &entry->thread_id, SMP_SIZE_THREAD_ID); - memcpy(header + SMP_OFF_INTERPRETER_ID, &entry->interpreter_id, SMP_SIZE_INTERPRETER_ID); - header[SMP_OFF_ENCODING] = encoding; - return writer_write_bytes(writer, header, SAMPLE_HEADER_FIXED_SIZE); + /* Magic "TBLK", sample count and base timestamp (varints), then frame + * ID width and the two status flag masks (1 byte each) */ + size_t size = BLOCK_MAGIC_SIZE; + memcpy(bytes, BLOCK_MAGIC, BLOCK_MAGIC_SIZE); + size += encode_varint_u32(bytes + size, header->sample_count); + size += encode_varint_u64(bytes + size, header->base_timestamp); + bytes[size++] = header->frame_width; + bytes[size++] = header->common_status_flags; + bytes[size++] = header->status_bitmap_flags; + + /* Each column: decoded size (varint), encoding (1 byte), and the stored + * size (varint) for zstd only */ + for (unsigned column = 0; column < COLUMN_COUNT; column++) { + const ColumnDescriptor *descriptor = &header->columns[column]; + size += encode_varint_u32(bytes + size, descriptor->decoded_size); + bytes[size++] = descriptor->encoding; + if (descriptor->encoding == COMPRESSION_ZSTD) { + size += encode_varint_u32(bytes + size, descriptor->stored_size); + } + } + return size; } -/* Flush pending RLE samples for a thread. - * Writes the RLE record to the output buffer. - * Returns 0 on success, -1 on failure. */ static int -flush_pending_rle(BinaryWriter *writer, ThreadEntry *entry) +block_flush(BinaryWriter *writer) { - if (entry->pending_rle_samples == 0) { + SampleBlock *block = writer->block; + if (block->sample_count == 0) { return 0; } + BlockHeader header = {0}; + header.sample_count = block->sample_count; + header.base_timestamp = block->base_timestamp; + header.frame_width = (uint8_t)block_frame_width(block); + block_status_flags(block, &header.common_status_flags, + &header.status_bitmap_flags); - /* Write RLE record: - * [thread_id: 8] [interpreter_id: 4] [STACK_REPEAT: 1] [count: varint] - * [timestamp_delta_1: varint] [status_1: 1] ... [timestamp_delta_N: varint] [status_N: 1] - */ - - if (write_sample_header(writer, entry, STACK_REPEAT) < 0) { + /* Convert each column to its file form, then encode it */ + /* Leave room for the header, which is written once its size is known */ + if (buffer_reserve(&block->output, MAX_BLOCK_HEADER_SIZE) < 0) { return -1; } - - if (writer_write_varint_u32(writer, (uint32_t)entry->pending_rle_samples) < 0) { + block->output.size = MAX_BLOCK_HEADER_SIZE; + size_t decoded_payload_size = 0; + for (unsigned column = 0; column < COLUMN_COUNT; column++) { + ByteBuffer *decoded = &block->columns[column]; + switch (column) { + case COLUMN_STATUS_BITMAPS: + decoded = transform_statuses(block, header.status_bitmap_flags); + break; + case COLUMN_STACK_INSTRUCTIONS: + decoded = transform_stack_instructions(block); + break; + case COLUMN_FRAME_IDS: + decoded = transform_frame_ids(block, header.frame_width); + break; + } + if (decoded == NULL) { + return -1; + } + if (decoded->size > + MAX_BLOCK_SIZE - decoded_payload_size) { + PyErr_SetString(PyExc_RuntimeError, + "sample block exceeds the decoded size limit"); + return -1; + } + decoded_payload_size += decoded->size; + if (encode_column(writer, decoded, &header.columns[column]) < 0) { + return -1; + } + } + /* Put the header right before the columns and write the block */ + uint8_t header_bytes[MAX_BLOCK_HEADER_SIZE]; + size_t header_size = write_block_header(&header, header_bytes); + size_t unused = MAX_BLOCK_HEADER_SIZE - header_size; + uint8_t *header_start = block->output.data + unused; + memcpy(header_start, header_bytes, header_size); + if (fwrite_checked_allow_threads(header_start, + block->output.size - unused, + writer->fp) < 0) { return -1; } + writer->stats.bytes_written += decoded_payload_size; + block_reset(writer); + return 0; +} - if (writer_write_bytes(writer, entry->pending_rle, entry->pending_rle_bytes) < 0) { - return -1; +/* FNV-1a over the frame indices, the same in every process */ +static Py_uhash_t +stack_hash(const uint32_t *stack, size_t depth) +{ + uint64_t hash = 0xcbf29ce484222325; + for (size_t i = 0; i < depth; i++) { + hash = (hash ^ stack[i]) * 0x100000001b3; + } + /* Mix the high bits into the low bits that select the cache bucket */ + hash ^= hash >> 33; + hash *= 0xff51afd7ed558ccd; + hash ^= hash >> 33; + return (Py_uhash_t)hash; +} + +static size_t +common_outer_frames(const BlockStream *stream, const uint32_t *stack, + size_t depth) +{ + size_t shared = 0; + while (shared < depth && shared < stream->prev_stack_depth && + stack[depth - 1 - shared] == + stream->prev_stack[stream->prev_stack_depth - 1 - shared]) { + shared++; } + return shared; +} - writer->stats.repeat_records++; - writer->stats.repeat_samples += entry->pending_rle_samples; - /* Each RLE sample saves writing the entire stack */ - writer->stats.frames_saved += entry->pending_rle_samples * entry->prev_stack_depth; +static int +cached_stack_matches(const StackCacheEntry *cached, + const ByteBuffer *frames, Py_uhash_t hash, + const uint32_t *stack, size_t depth) +{ + if (cached->hash != hash || cached->depth != depth) { + return 0; + } + const uint32_t *frame_ids = (const uint32_t *)frames->data; + for (size_t frame = 0; frame < depth; frame++) { + if (stack[frame] != + frame_ids[cached->offset + depth - 1 - frame]) { + return 0; + } + } + return 1; +} + +static StackCacheEntry * +stack_cache_lookup(StackCacheEntry *bucket, const ByteBuffer *frames, + Py_uhash_t hash, const uint32_t *stack, size_t depth) +{ + for (size_t i = 0; i < STACK_CACHE_BUCKET_SIZE; i++) { + if (cached_stack_matches(&bucket[i], frames, hash, stack, depth)) { + return &bucket[i]; + } + } + return NULL; +} - entry->pending_rle_bytes = 0; - entry->pending_rle_samples = 0; +static void +stack_cache_insert(StackCacheEntry *bucket, StackCacheEntry entry) +{ + memmove(&bucket[1], &bucket[0], + (STACK_CACHE_BUCKET_SIZE - 1) * sizeof(*bucket)); + bucket[0] = entry; +} +static int +write_stack_repeat(BinaryWriter *writer, BlockStream *stream, size_t depth) +{ + SampleBlock *block = writer->block; + if (stream->current_repeat == UINT32_MAX) { + Repeat repeat = { + (uint32_t)block->columns[COLUMN_STACK_INSTRUCTIONS].size, 1}; + if (buffer_append(&block->repeats, + &repeat, sizeof(repeat)) < 0) { + return -1; + } + block->size += sizeof(repeat); + stream->current_repeat = + (uint32_t)(block->repeats.size / sizeof(repeat) - 1); + writer->stats.repeat_records++; + } + else { + Repeat *repeats = (Repeat *)block->repeats.data; + repeats[stream->current_repeat].count++; + } + writer->stats.repeat_samples++; + writer->stats.frames_saved += depth; return 0; } -/* Write a single sample with the specified encoding. - * Returns 0 on success, -1 on failure. */ static int -write_sample_with_encoding(BinaryWriter *writer, ThreadEntry *entry, - uint64_t timestamp_delta, uint8_t status, - int encoding_type, - const uint32_t *frame_indices, size_t stack_depth, - size_t shared_count, size_t pop_count, size_t push_count) +write_stack_reuse(BinaryWriter *writer, + const StackCacheEntry *cached, size_t depth) { - /* Header: thread_id(8) + interpreter_id(4) + encoding(1) + delta(varint) + status(1) */ - uint8_t header_buf[SAMPLE_HEADER_MAX_SIZE]; - memcpy(header_buf + SMP_OFF_THREAD_ID, &entry->thread_id, SMP_SIZE_THREAD_ID); - memcpy(header_buf + SMP_OFF_INTERPRETER_ID, &entry->interpreter_id, SMP_SIZE_INTERPRETER_ID); - header_buf[SMP_OFF_ENCODING] = (uint8_t)encoding_type; - size_t varint_len = encode_varint_u64( - header_buf + SAMPLE_HEADER_FIXED_SIZE, - timestamp_delta); - header_buf[SAMPLE_HEADER_FIXED_SIZE + varint_len] = status; - - if (writer_write_bytes(writer, header_buf, - SAMPLE_HEADER_FIXED_SIZE + varint_len + 1) < 0) { + SampleBlock *block = writer->block; + if (column_write_varint(block, COLUMN_STACK_INSTRUCTIONS, + STACK_REUSE) < 0 || + column_write_varint(block, COLUMN_STACK_INSTRUCTIONS, + cached->offset) < 0 || + column_write_varint(block, COLUMN_STACK_INSTRUCTIONS, depth) < 0) { return -1; } + writer->stats.reuse_records++; + writer->stats.frames_saved += depth; + return 0; +} - uint8_t frame_buf[MAX_FRAME_BUFFER_SIZE]; - size_t frame_buf_pos = 0; - size_t frames_written = 0; +static int +write_stack_replace_suffix(BinaryWriter *writer, StackCacheEntry *bucket, + Py_uhash_t hash, const uint32_t *stack, + size_t depth, size_t shared) +{ + SampleBlock *block = writer->block; + ByteBuffer *frames = &block->columns[COLUMN_FRAME_IDS]; + size_t new_frames = depth - shared; + if (column_write_varint(block, COLUMN_STACK_INSTRUCTIONS, + STACK_REPLACE_SUFFIX) < 0 || + column_write_varint(block, COLUMN_STACK_INSTRUCTIONS, shared) < 0 || + column_write_varint(block, COLUMN_STACK_INSTRUCTIONS, + new_frames) < 0) { + return -1; + } - switch (encoding_type) { - case STACK_FULL: - /* [depth: varint] [frame_idx: varint]... */ - frame_buf_pos += encode_varint_u32(frame_buf, (uint32_t)stack_depth); - for (size_t i = 0; i < stack_depth; i++) { - frame_buf_pos += encode_varint_u32(frame_buf + frame_buf_pos, frame_indices[i]); + /* Append the new inner frames, outermost first */ + size_t stack_offset = frames->size / sizeof(uint32_t); + if (new_frames != 0) { + if (buffer_reserve( + frames, frames->size + new_frames * sizeof(uint32_t)) < 0) { + return -1; } - frames_written = stack_depth; - writer->stats.full_records++; - break; - - case STACK_SUFFIX: - /* [shared_count: varint] [new_count: varint] [new_frame_idx: varint]... */ - frame_buf_pos += encode_varint_u32(frame_buf, (uint32_t)shared_count); - frame_buf_pos += encode_varint_u32(frame_buf + frame_buf_pos, (uint32_t)push_count); - /* New frames are at the top (beginning) of current stack */ - for (size_t i = 0; i < push_count; i++) { - frame_buf_pos += encode_varint_u32(frame_buf + frame_buf_pos, frame_indices[i]); + uint32_t *new_frame_ids = (uint32_t *)(frames->data + frames->size); + for (size_t i = 0; i < new_frames; i++) { + uint32_t frame = stack[new_frames - 1 - i]; + new_frame_ids[i] = frame; + if (frame > block->max_frame) { + block->max_frame = frame; + } } - frames_written = push_count; - writer->stats.suffix_records++; - /* Saved writing shared_count frames */ - writer->stats.frames_saved += shared_count; - break; - - case STACK_POP_PUSH: - /* [pop_count: varint] [push_count: varint] [new_frame_idx: varint]... */ - frame_buf_pos += encode_varint_u32(frame_buf, (uint32_t)pop_count); - frame_buf_pos += encode_varint_u32(frame_buf + frame_buf_pos, (uint32_t)push_count); - /* New frames are at the top (beginning) of current stack */ - for (size_t i = 0; i < push_count; i++) { - frame_buf_pos += encode_varint_u32(frame_buf + frame_buf_pos, frame_indices[i]); + frames->size += new_frames * sizeof(uint32_t); + block->size += new_frames * sizeof(uint32_t); + } + /* Complete stacks are cached so later samples can REUSE them */ + if (shared == 0) { + if (bucket != NULL) { + stack_cache_insert(bucket, (StackCacheEntry){ + hash, (uint32_t)stack_offset, (uint32_t)depth}); } - frames_written = push_count; - writer->stats.pop_push_records++; - /* Saved writing shared_count frames (stack_depth - push_count if we had written full) */ - writer->stats.frames_saved += shared_count; - break; - - default: - PyErr_SetString(PyExc_RuntimeError, "Invalid stack encoding type"); + writer->stats.full_records++; + } + else { + writer->stats.suffix_records++; + } + writer->stats.total_frames_written += new_frames; + writer->stats.frames_saved += shared; + return 0; +} + +static int +write_stack(BinaryWriter *writer, BlockStream *stream, int new_stream, + const uint32_t *stack, size_t depth) +{ + SampleBlock *block = writer->block; + size_t shared = common_outer_frames(stream, stack, depth); + /* Same stack as this stream's previous sample: extend its REPEAT */ + if (!new_stream && shared == depth && + shared == stream->prev_stack_depth) { + return write_stack_repeat(writer, stream, depth); + } + + /* Enough new frames to be worth looking up an earlier complete stack */ + Py_uhash_t hash = 0; + StackCacheEntry *bucket = NULL; + StackCacheEntry *cached = NULL; + if (depth - shared >= STACK_REUSE_MIN_FRAMES) { + hash = stack_hash(stack, depth); + bucket = block->cache[hash % STACK_CACHE_BUCKET_COUNT]; + cached = stack_cache_lookup(bucket, &block->columns[COLUMN_FRAME_IDS], + hash, stack, depth); + } + + int result; + if (cached != NULL) { + /* Stack already stored in this block: point to its frames */ + result = write_stack_reuse(writer, cached, depth); + } + else { + /* Keep the shared outer frames and write only the new inner ones */ + result = write_stack_replace_suffix(writer, bucket, hash, stack, + depth, shared); + } + if (result < 0) { return -1; } + stream->current_repeat = UINT32_MAX; + memcpy(stream->prev_stack, stack, depth * sizeof(uint32_t)); + stream->prev_stack_depth = depth; + return 0; +} + +size_t +binary_writer_buffered_size(BinaryWriter *writer) +{ + SampleBlock *block = writer->block; + if (block == NULL || block->sample_count == 0) { + return 0; + } + size_t size = block->columns[COLUMN_STREAM_TABLE].size + + block->columns[COLUMN_SAMPLE_STREAM_IDS].size + + block->columns[COLUMN_TIMESTAMP_RESIDUALS].size + + block->columns[COLUMN_STACK_INSTRUCTIONS].size; + const Repeat *repeats = (const Repeat *)block->repeats.data; + size_t repeat_count = block->repeats.size / sizeof(Repeat); + uint8_t bytes[MAX_VARINT_SIZE]; + for (size_t i = 0; i < repeat_count; i++) { + size += encode_varint_u64(bytes, STACK_REPEAT) + + encode_varint_u64(bytes, repeats[i].count); + } + uint8_t common_flags, bitmap_flags; + block_status_flags(block, &common_flags, &bitmap_flags); + size += (size_t)_Py_popcount32(bitmap_flags) + * status_bitmap_size(block->sample_count); + size += block->columns[COLUMN_FRAME_IDS].size / sizeof(uint32_t) + * block_frame_width(block); + return size; +} - if (writer_write_bytes(writer, frame_buf, frame_buf_pos) < 0) { +static int +block_add_stream(BinaryWriter *writer, ThreadEntry *entry) +{ + SampleBlock *block = writer->block; + if (GROW_ARRAY(block->streams, block->stream_count, + block->stream_capacity, BlockStream) < 0) { return -1; } + /* Stream table entry: thread_id (8 bytes) + interpreter_id (4 bytes) */ + uint8_t stream_entry[STREAM_ENTRY_SIZE]; + memcpy(stream_entry + STM_OFF_THREAD_ID, + &entry->thread_id, sizeof(entry->thread_id)); + memcpy(stream_entry + STM_OFF_INTERPRETER_ID, + &entry->interpreter_id, sizeof(entry->interpreter_id)); + if (column_write(block, COLUMN_STREAM_TABLE, + stream_entry, sizeof(stream_entry)) < 0) { + return -1; + } + BlockStream *stream = &block->streams[block->stream_count]; + stream->thread_index = (size_t)(entry - writer->thread_entries); + stream->prev_timestamp = block->base_timestamp - writer->sample_interval_us; + stream->current_repeat = UINT32_MAX; + stream->prev_stack_depth = 0; + entry->block_stream_index = block->stream_count++; + return 0; +} - writer->stats.total_frames_written += frames_written; +static int +block_add_sample(BinaryWriter *writer, ThreadEntry *entry, + uint64_t timestamp, uint8_t status, + const uint32_t *stack, size_t depth) +{ + static_assert(BLOCK_FLUSH_SIZE + MAX_SAMPLE_SIZE + MAX_BITMAP_PADDING == + MAX_BLOCK_SIZE, + "block flush size needs headroom for one complete sample"); + + SampleBlock *block = writer->block; + /* Start a new block if this sample might not fit */ + if (block->size >= BLOCK_FLUSH_SIZE || + (entry->block_stream_index == UINT32_MAX && + block->stream_count == MAX_BLOCK_STREAMS)) { + if (block_flush(writer) < 0) { + return -1; + } + } + if (block->sample_count == 0) { + block->base_timestamp = timestamp; + } + int new_stream = entry->block_stream_index == UINT32_MAX; + if (new_stream && block_add_stream(writer, entry) < 0) { + return -1; + } + BlockStream *stream = &block->streams[entry->block_stream_index]; + /* Store the difference from the expected timestamp */ + uint64_t residual = + timestamp - stream->prev_timestamp - writer->sample_interval_us; + uint8_t residual_bytes[MAX_VARINT_SIZE]; + size_t residual_size = + encode_varint_i64(residual_bytes, (int64_t)residual); + if (column_write_varint(block, COLUMN_SAMPLE_STREAM_IDS, + entry->block_stream_index) < 0 || + column_write(block, COLUMN_TIMESTAMP_RESIDUALS, + residual_bytes, residual_size) < 0 || + column_write(block, COLUMN_STATUS_BITMAPS, + &status, sizeof(status)) < 0) { + return -1; + } + if (write_stack(writer, stream, new_stream, stack, depth) < 0) { + return -1; + } + stream->prev_timestamp = timestamp; + block->sample_count++; + writer->total_samples++; return 0; } +static void +block_destroy(SampleBlock *block) +{ + if (block == NULL) { + return; + } + for (unsigned i = 0; i < COLUMN_COUNT; i++) { + buffer_clear(&block->columns[i]); + } + buffer_clear(&block->repeats); + buffer_clear(&block->transformed); + buffer_clear(&block->compressed); + buffer_clear(&block->output); + PyMem_Free(block->streams); + PyMem_Free(block); +} + BinaryWriter * binary_writer_create(PyObject *path, uint64_t sample_interval_us, int compression_type, uint64_t start_time_us, int profiling_mode, int capture_features) { + if (compression_type != COMPRESSION_NONE && + compression_type != COMPRESSION_ZSTD) { + PyErr_SetString(PyExc_ValueError, "invalid compression type"); + return NULL; + } BinaryWriter *writer = PyMem_Calloc(1, sizeof(BinaryWriter)); if (!writer) { PyErr_NoMemory(); @@ -761,12 +1090,18 @@ binary_writer_create(PyObject *path, uint64_t sample_interval_us, int compressio writer->profiling_mode = profiling_mode; writer->capture_features = capture_features; - writer->write_buffer = PyMem_Malloc(WRITE_BUFFER_SIZE); - if (!writer->write_buffer) { + writer->block = PyMem_Calloc(1, sizeof(SampleBlock)); + if (writer->block == NULL) { + PyErr_NoMemory(); + goto error; + } + writer->block->streams = PyMem_Malloc( + INITIAL_BLOCK_STREAM_CAPACITY * sizeof(BlockStream)); + if (writer->block->streams == NULL) { PyErr_NoMemory(); goto error; } - writer->buffer_size = WRITE_BUFFER_SIZE; + writer->block->stream_capacity = INITIAL_BLOCK_STREAM_CAPACITY; writer->string_hash = _Py_hashtable_new_full( string_hash_func, @@ -964,74 +1299,29 @@ process_thread_sample(BinaryWriter *writer, PyObject *thread_info, if (status_long == -1 && PyErr_Occurred()) { return -1; } + if (status_long < 0 || status_long > UINT8_MAX) { + writer->state = BINARY_WRITER_LIMIT_REACHED; + PyErr_Format(PyExc_OverflowError, + "thread status %ld exceeds the binary format range", + status_long); + return -1; + } uint8_t status = (uint8_t)status_long; ThreadEntry *entry = writer_get_or_create_thread_entry( - writer, thread_id, interpreter_id, NULL); + writer, thread_id, interpreter_id); if (!entry) { return -1; } - /* Calculate timestamp delta */ - uint64_t delta = timestamp_us - entry->prev_timestamp; - /* Process frames and build current stack */ uint32_t curr_stack[MAX_STACK_DEPTH]; size_t curr_depth; if (build_frame_stack(writer, frame_list, curr_stack, &curr_depth) < 0) { return -1; } - - /* Compare with previous stack to determine encoding */ - size_t shared_count, pop_count, push_count; - int encoding = compare_stacks( - entry->prev_stack, entry->prev_stack_depth, - curr_stack, curr_depth, - &shared_count, &pop_count, &push_count); - - if (encoding == STACK_REPEAT) { - /* Buffer this sample for RLE. - * - * STACK_REPEAT also covers the "first sample for a fresh thread, - * empty stack" case: a new ThreadEntry has prev_stack_depth == 0 - * and a zero-initialized prev_stack, so compare_stacks() returns - * STACK_REPEAT against an empty curr_stack (depth 0). Buffering - * it here is correct; the RLE flush path emits it as a normal - * STACK_REPEAT record. */ - if (entry->pending_rle == NULL) { - entry->pending_rle = PyMem_Malloc(MAX_RLE_BUF_SIZE); - if (!entry->pending_rle) { - PyErr_NoMemory(); - return -1; - } - } - if (entry->pending_rle_bytes + MAX_RLE_ENTRY_SIZE > MAX_RLE_BUF_SIZE - && flush_pending_rle(writer, entry) < 0) { - return -1; - } - entry->pending_rle_bytes += encode_varint_u64( - entry->pending_rle + entry->pending_rle_bytes, delta); - entry->pending_rle[entry->pending_rle_bytes++] = status; - entry->pending_rle_samples++; - } else { - /* Stack changed - flush any pending RLE first */ - if (flush_pending_rle(writer, entry) < 0) { - return -1; - } - - if (write_sample_with_encoding(writer, entry, delta, status, encoding, - curr_stack, curr_depth, - shared_count, pop_count, push_count) < 0) { - return -1; - } - - memcpy(entry->prev_stack, curr_stack, curr_depth * sizeof(uint32_t)); - entry->prev_stack_depth = curr_depth; - } - - entry->prev_timestamp = timestamp_us; - writer->total_samples++; - return 0; + return block_add_sample( + writer, entry, timestamp_us, status, curr_stack, curr_depth); } int @@ -1078,47 +1368,10 @@ binary_writer_write_sample(BinaryWriter *writer, PyObject *stack_frames, uint64_ int binary_writer_finalize(BinaryWriter *writer) { - for (size_t i = 0; i < writer->thread_count; i++) { - if (flush_pending_rle(writer, &writer->thread_entries[i]) < 0) { - return -1; - } - } - - if (writer_flush_buffer(writer) < 0) { + if (block_flush(writer) < 0) { return -1; } -#ifdef HAVE_ZSTD - /* Finalize compression stream */ - if (writer->compression_type == COMPRESSION_ZSTD && writer->zstd.cctx) { - ZSTD_inBuffer input = { NULL, 0, 0 }; - size_t remaining; - - do { - ZSTD_outBuffer output = { - writer->zstd.compressed_buffer, - writer->zstd.compressed_buffer_size, - 0 - }; - - remaining = ZSTD_compressStream2(writer->zstd.cctx, &output, &input, ZSTD_e_end); - - if (ZSTD_isError(remaining)) { - PyErr_Format(PyExc_IOError, "zstd finalization error: %s", - ZSTD_getErrorName(remaining)); - return -1; - } - - _Py_MSAN_UNPOISON(writer->zstd.compressed_buffer, output.pos); - if (output.pos > 0) { - if (fwrite_checked_allow_threads(writer->zstd.compressed_buffer, output.pos, writer->fp) < 0) { - return -1; - } - } - } while (remaining > 0); - } -#endif - /* Use 64-bit file position for >2GB files */ file_offset_t string_table_offset = FTELL64(writer->fp); if (string_table_offset < 0) { @@ -1314,13 +1567,12 @@ binary_writer_destroy(BinaryWriter *writer) Py_fclose(writer->fp); } - PyMem_Free(writer->write_buffer); + block_destroy(writer->block); #ifdef HAVE_ZSTD if (writer->zstd.cctx) { ZSTD_freeCCtx(writer->zstd.cctx); } - PyMem_Free(writer->zstd.compressed_buffer); #endif if (writer->string_hash) { @@ -1339,13 +1591,7 @@ binary_writer_destroy(BinaryWriter *writer) } PyMem_Free(writer->frame_entries); - if (writer->thread_entries) { - for (size_t i = 0; i < writer->thread_count; i++) { - PyMem_Free(writer->thread_entries[i].prev_stack); - PyMem_Free(writer->thread_entries[i].pending_rle); - } - PyMem_Free(writer->thread_entries); - } + PyMem_Free(writer->thread_entries); PyMem_Free(writer); } diff --git a/Modules/_remote_debugging/clinic/module.c.h b/Modules/_remote_debugging/clinic/module.c.h index 9895286cdb8019..26e4a3c2c2e84b 100644 --- a/Modules/_remote_debugging/clinic/module.c.h +++ b/Modules/_remote_debugging/clinic/module.c.h @@ -295,14 +295,17 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_all_awaited_by__doc__, "\n" " - thread_id (int): Identifier of the thread, or 0 for tasks in the\n" " interpreter\'s fallback task list.\n" -" - awaited_by (list[TaskInfo]): Tasks registered with this thread.\n" +" - awaited_by (list[TaskInfo]): Tasks registered with this thread and\n" +" intermediate futures in their awaited_by chains.\n" "\n" "Each TaskInfo contains:\n" " - task_id (int): Identifier of the task.\n" -" - task_name (str): Name of the task.\n" +" - task_name (str): Name of the task, or a generated name for\n" +" a future.\n" " - coroutine_stack (list[CoroInfo]): Stack of coroutine frames.\n" " - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures\n" " awaiting this task.\n" +" - is_future (bool, named-only): True for a plain Future.\n" "\n" "Each CoroInfo contains:\n" " - call_stack (list[FrameInfo]): Call stack frames for the coroutine.\n" @@ -367,14 +370,16 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, "\n" " - thread_id (int): Identifier of the thread.\n" " - awaited_by (list[TaskInfo]): The running task and all its\n" -" transitive waiters.\n" +" transitive waiters, including intermediate futures.\n" "\n" "Each TaskInfo contains:\n" " - task_id (int): Identifier of the task.\n" -" - task_name (str): Name of the task.\n" +" - task_name (str): Name of the task, or a generated name for\n" +" a future.\n" " - coroutine_stack (list[CoroInfo]): Stack of coroutine frames.\n" " - awaited_by (list[CoroInfo]): Coroutine information for\n" " tasks or futures awaiting this task.\n" +" - is_future (bool, named-only): True for a plain Future.\n" "\n" "Each CoroInfo contains:\n" " - call_stack (list[FrameInfo]): Frames for the coroutine.\n" @@ -408,7 +413,9 @@ PyDoc_STRVAR(_remote_debugging_RemoteUnwinder_get_async_stack_trace__doc__, " coroutine_stack=[\n" " CoroInfo(call_stack=[...], task_name=1)\n" " ],\n" -" awaited_by=[CoroInfo(call_stack=[...], task_name=2)]\n" +" awaited_by=[\n" +" CoroInfo(call_stack=[...], task_name=2)\n" +" ]\n" " ),\n" " TaskInfo(\n" " task_id=2,\n" @@ -1168,7 +1175,7 @@ PyDoc_STRVAR(_remote_debugging_BinaryWriter_get_stats__doc__, "Get encoding statistics for the writer.\n" "\n" "Returns a dict with encoding statistics including\n" -"repeat/full/suffix/pop-push record counts, frames written/saved, and\n" +"repeat/full/suffix/reuse record counts, frames written/saved, and\n" "compression ratio."); #define _REMOTE_DEBUGGING_BINARYWRITER_GET_STATS_METHODDEF \ @@ -1740,4 +1747,4 @@ _remote_debugging_get_gc_stats(PyObject *module, PyObject *const *args, Py_ssize exit: return return_value; } -/*[clinic end generated code: output=2667550106e045bd input=a9049054013a1b77]*/ +/*[clinic end generated code: output=2be4cb3f27f6c8d8 input=a9049054013a1b77]*/ diff --git a/Modules/_remote_debugging/frame_cache.c b/Modules/_remote_debugging/frame_cache.c index 1b2d9a60f408b9..63d1331015a548 100644 --- a/Modules/_remote_debugging/frame_cache.c +++ b/Modules/_remote_debugging/frame_cache.c @@ -318,9 +318,8 @@ frame_cache_store( // Validate we have a complete stack before caching. // Only cache if last_frame_visited matches base_frame_addr (the sentinel // at the bottom of the stack). Note: we use last_frame_visited rather than - // addrs[num_addrs-1] because the base frame is visited but not added to the - // addrs array (it returns frame==NULL from is_frame_valid due to - // owner==FRAME_OWNED_BY_INTERPRETER). + // addrs[num_addrs-1] because the base frame's address is not added to the + // addrs array. if (base_frame_addr != 0 && last_frame_visited != base_frame_addr) { // Incomplete stack - don't cache (graceful degradation) return 0; diff --git a/Modules/_remote_debugging/frames.c b/Modules/_remote_debugging/frames.c index f5b9ed72f1f98e..3f8d628a6314e6 100644 --- a/Modules/_remote_debugging/frames.c +++ b/Modules/_remote_debugging/frames.c @@ -36,7 +36,7 @@ process_single_stack_chunk( return -1; } - if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, chunk_addr, current_size, this_chunk) < 0) { + if (_Py_RemoteDebug_ReadRemoteMemory(&unwinder->handle, chunk_addr, current_size, this_chunk) < 0) { PyMem_RawFree(this_chunk); set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read stack chunk"); return -1; @@ -65,7 +65,7 @@ process_single_stack_chunk( } this_chunk = tmp; - if (_Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, chunk_addr, actual_size, this_chunk) < 0) { + if (_Py_RemoteDebug_ReadRemoteMemory(&unwinder->handle, chunk_addr, actual_size, this_chunk) < 0) { PyMem_RawFree(this_chunk); set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to reread stack chunk with correct size"); return -1; @@ -195,11 +195,8 @@ parse_frame_buffer( RemoteUnwinderObject *unwinder, PyObject** result, const char *frame, - uintptr_t* address_of_code_object, uintptr_t* previous_frame ) { - *address_of_code_object = 0; - *previous_frame = GET_MEMBER(uintptr_t, frame, unwinder->debug_offsets.interpreter_frame.previous); uintptr_t code_object = GET_MEMBER_NO_TAG(uintptr_t, frame, unwinder->debug_offsets.interpreter_frame.executable); int frame_valid = is_frame_valid(unwinder, (uintptr_t)frame, code_object); @@ -217,8 +214,6 @@ parse_frame_buffer( } #endif - *address_of_code_object = code_object; - CodeObjectContext code_ctx = { .code_addr = code_object, .instruction_pointer = instruction_pointer, @@ -232,7 +227,6 @@ parse_frame_object( RemoteUnwinderObject *unwinder, PyObject** result, uintptr_t address, - uintptr_t* address_of_code_object, uintptr_t* previous_frame ) { char frame[SIZEOF_INTERP_FRAME]; @@ -249,7 +243,7 @@ parse_frame_object( STATS_INC(unwinder, memory_reads); STATS_ADD(unwinder, memory_bytes_read, SIZEOF_INTERP_FRAME); - return parse_frame_buffer(unwinder, result, frame, address_of_code_object, previous_frame); + return parse_frame_buffer(unwinder, result, frame, previous_frame); } int @@ -258,41 +252,16 @@ parse_frame_from_chunks( PyObject **result, uintptr_t address, uintptr_t *previous_frame, - uintptr_t *stackpointer, StackChunkList *chunks ) { - void *frame_ptr = find_frame_in_chunks(chunks, address); - if (!frame_ptr) { + const char *frame = find_frame_in_chunks(chunks, address); + if (!frame) { PyErr_Format(PyExc_RuntimeError, "Frame at address 0x%lx not found in stack chunks", address); set_exception_cause(unwinder, PyExc_RuntimeError, "Frame not found in stack chunks"); return -1; } - char *frame = (char *)frame_ptr; - *previous_frame = GET_MEMBER(uintptr_t, frame, unwinder->debug_offsets.interpreter_frame.previous); - *stackpointer = GET_MEMBER(uintptr_t, frame, unwinder->debug_offsets.interpreter_frame.stackpointer); - uintptr_t code_object = GET_MEMBER_NO_TAG(uintptr_t, frame_ptr, unwinder->debug_offsets.interpreter_frame.executable); - int frame_valid = is_frame_valid(unwinder, (uintptr_t)frame, code_object); - if (frame_valid != 1) { - return frame_valid; - } - - uintptr_t instruction_pointer = GET_MEMBER(uintptr_t, frame, unwinder->debug_offsets.interpreter_frame.instr_ptr); - - // Get tlbc_index for free threading builds - int32_t tlbc_index = 0; -#ifdef Py_GIL_DISABLED - if (unwinder->debug_offsets.interpreter_frame.tlbc_index != 0) { - tlbc_index = GET_MEMBER(int32_t, frame, unwinder->debug_offsets.interpreter_frame.tlbc_index); - } -#endif - - CodeObjectContext code_ctx = { - .code_addr = code_object, - .instruction_pointer = instruction_pointer, - .tlbc_index = tlbc_index, - }; - return parse_code_object(unwinder, result, &code_ctx); + return parse_frame_buffer(unwinder, result, frame, previous_frame); } /* ============================================================================ @@ -316,7 +285,6 @@ process_frame_chain( int parse_result = 0; PyObject *frame = NULL; uintptr_t next_frame_addr = 0; - uintptr_t stackpointer = 0; last_frame_addr = frame_addr; if (++frame_count > MAX_FRAME_CHAIN_DEPTH) { @@ -326,30 +294,33 @@ process_frame_chain( } assert(frame_count <= MAX_FRAME_CHAIN_DEPTH); + // The base frame is a sentinel with previous == NULL: reaching it is + // enough, no need to read it. Still read it when it is the GC frame + // so the marker gets emitted. + if (frame_addr == ctx->base_frame_addr + && !(unwinder->gc && frame_addr == ctx->gc_frame)) { + break; + } + if (ctx->chunks && ctx->chunks->count > 0) { parse_result = parse_frame_from_chunks( - unwinder, &frame, frame_addr, &next_frame_addr, &stackpointer, ctx->chunks); + unwinder, &frame, frame_addr, &next_frame_addr, ctx->chunks); if (parse_result == 0) { goto parsed_frame; } PyErr_Clear(); } - { - uintptr_t address_of_code_object = 0; - if (ctx->prefetch.frame && ctx->prefetch.frame_addr == frame_addr) { - parse_result = parse_frame_buffer( - unwinder, &frame, ctx->prefetch.frame, - &address_of_code_object, &next_frame_addr); - } - else { - parse_result = parse_frame_object( - unwinder, &frame, frame_addr, - &address_of_code_object, &next_frame_addr); - } - if (parse_result < 0) { - set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse frame object in chain"); - return -1; - } + if (ctx->prefetch.frame && ctx->prefetch.frame_addr == frame_addr) { + parse_result = parse_frame_buffer( + unwinder, &frame, ctx->prefetch.frame, &next_frame_addr); + } + else { + parse_result = parse_frame_object( + unwinder, &frame, frame_addr, &next_frame_addr); + } + if (parse_result < 0) { + set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to parse frame object in chain"); + return -1; } parsed_frame: @@ -465,7 +436,7 @@ clear_last_profiled_frames(RemoteUnwinderObject *unwinder) uintptr_t tstate_addr; if (_Py_RemoteDebug_PagedReadRemoteMemory( &unwinder->handle, - current_interp + unwinder->debug_offsets.interpreter_state.threads_head, + current_interp + (uintptr_t)unwinder->debug_offsets.interpreter_state.threads_head, sizeof(void*), &tstate_addr) < 0) { // Non-fatal: just skip clearing @@ -486,7 +457,7 @@ clear_last_profiled_frames(RemoteUnwinderObject *unwinder) // Move to next thread if (_Py_RemoteDebug_PagedReadRemoteMemory( &unwinder->handle, - tstate_addr + unwinder->debug_offsets.thread_state.next, + tstate_addr + (uintptr_t)unwinder->debug_offsets.thread_state.next, sizeof(void*), &tstate_addr) < 0) { PyErr_Clear(); @@ -497,7 +468,7 @@ clear_last_profiled_frames(RemoteUnwinderObject *unwinder) // Move to next interpreter if (_Py_RemoteDebug_PagedReadRemoteMemory( &unwinder->handle, - current_interp + unwinder->debug_offsets.interpreter_state.next, + current_interp + (uintptr_t)unwinder->debug_offsets.interpreter_state.next, sizeof(void*), ¤t_interp) < 0) { PyErr_Clear(); @@ -543,17 +514,16 @@ try_full_cache_hit( } PyObject *current_frame = NULL; - uintptr_t code_object_addr = 0; uintptr_t previous_frame = 0; int parse_result; if (ctx->prefetch.frame && ctx->prefetch.frame_addr == ctx->frame_addr) { parse_result = parse_frame_buffer(unwinder, ¤t_frame, ctx->prefetch.frame, - &code_object_addr, &previous_frame); + &previous_frame); } else { parse_result = parse_frame_object(unwinder, ¤t_frame, ctx->frame_addr, - &code_object_addr, &previous_frame); + &previous_frame); } if (parse_result < 0) { return -1; diff --git a/Modules/_remote_debugging/gc_stats.c b/Modules/_remote_debugging/gc_stats.c index 23fa879b503283..4dc77803a0a9f3 100644 --- a/Modules/_remote_debugging/gc_stats.c +++ b/Modules/_remote_debugging/gc_stats.c @@ -88,8 +88,8 @@ get_gc_stats_from_interpreter_state(RuntimeOffsets *offsets, uintptr_t gc_stats_addr = 0; uintptr_t gc_stats_pointer_address = interpreter_state_addr - + offsets->debug_offsets.interpreter_state.gc - + offsets->debug_offsets.gc.generation_stats; + + (uintptr_t)offsets->debug_offsets.interpreter_state.gc + + (uintptr_t)offsets->debug_offsets.gc.generation_stats; if (_Py_RemoteDebug_ReadRemoteMemory(&offsets->handle, gc_stats_pointer_address, sizeof(gc_stats_addr), diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index 28f73a86dcdbda..d9d7b61633a106 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -34,12 +34,13 @@ static PyStructSequence_Field TaskInfo_fields[] = { {"task_name", "Task name"}, {"coroutine_stack", "Coroutine call stack"}, {"awaited_by", "Tasks awaiting this task"}, + {"is_future", "Whether this node is a plain Future rather than a Task"}, {NULL} }; PyStructSequence_Desc TaskInfo_desc = { "_remote_debugging.TaskInfo", - "Information about an asyncio task", + "Information about an asyncio task or future", TaskInfo_fields, 4 }; @@ -384,12 +385,6 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self, return -1; } - self->runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress(&self->handle); - if (self->runtime_start_address == 0) { - set_exception_cause(self, PyExc_RuntimeError, "Failed to get Python runtime address"); - return -1; - } - if (_Py_RemoteDebug_ReadDebugOffsets(&self->handle, &self->runtime_start_address, &self->debug_offsets) < 0) @@ -404,20 +399,8 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self, return -1; } - // Try to read async debug offsets, but don't fail if they're not available - self->async_debug_offsets_available = 1; - int async_debug_result = read_async_debug(self); - if (async_debug_result == PY_REMOTE_DEBUG_INVALID_ASYNC_DEBUG_OFFSETS) { - return -1; - } - if (async_debug_result < 0) { - if (_Py_RemoteDebug_HasPermissionError()) { - return -1; - } - PyErr_Clear(); - memset(&self->async_debug_offsets, 0, sizeof(self->async_debug_offsets)); - self->async_debug_offsets_available = 0; - } + self->async_debug_offsets_available = 0; + memset(&self->async_debug_offsets, 0, sizeof(self->async_debug_offsets)); if (populate_initial_state_data(all_threads, self, self->runtime_start_address, &self->interpreter_addr ,&self->tstate_addr) < 0) @@ -456,15 +439,6 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self, } #endif -#if defined(__APPLE__) - self->thread_id_offset = 0; - self->thread_id_offset_initialized = 0; -#endif - -#ifdef MS_WINDOWS - self->win_process_buffer = NULL; - self->win_process_buffer_size = 0; -#endif #ifdef __linux__ self->thread_tids = NULL; self->thread_tids_capacity = 0; @@ -822,6 +796,9 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self goto exit; } // Thread was skipped due to mode filtering, continue to next thread + if (self->tstate_addr || self->only_active_thread) { + break; + } continue; } // This was an actual error @@ -914,14 +891,17 @@ Get awaited_by relationships for tasks in the remote process. - thread_id (int): Identifier of the thread, or 0 for tasks in the interpreter's fallback task list. - - awaited_by (list[TaskInfo]): Tasks registered with this thread. + - awaited_by (list[TaskInfo]): Tasks registered with this thread and + intermediate futures in their awaited_by chains. Each TaskInfo contains: - task_id (int): Identifier of the task. - - task_name (str): Name of the task. + - task_name (str): Name of the task, or a generated name for + a future. - coroutine_stack (list[CoroInfo]): Stack of coroutine frames. - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures awaiting this task. + - is_future (bool, named-only): True for a plain Future. Each CoroInfo contains: - call_stack (list[FrameInfo]): Call stack frames for the coroutine. @@ -952,7 +932,7 @@ Example output: static PyObject * _remote_debugging_RemoteUnwinder_get_all_awaited_by_impl(RemoteUnwinderObject *self) -/*[clinic end generated code: output=6a49cd345e8aec53 input=79e556b3973d21a8]*/ +/*[clinic end generated code: output=6a49cd345e8aec53 input=17fcd3d7bb835624]*/ { if (ensure_async_debug_offsets(self) < 0) { return NULL; @@ -1015,14 +995,16 @@ running task, tasks waiting for those tasks, etc.). - thread_id (int): Identifier of the thread. - awaited_by (list[TaskInfo]): The running task and all its - transitive waiters. + transitive waiters, including intermediate futures. Each TaskInfo contains: - task_id (int): Identifier of the task. - - task_name (str): Name of the task. + - task_name (str): Name of the task, or a generated name for + a future. - coroutine_stack (list[CoroInfo]): Stack of coroutine frames. - awaited_by (list[CoroInfo]): Coroutine information for tasks or futures awaiting this task. + - is_future (bool, named-only): True for a plain Future. Each CoroInfo contains: - call_stack (list[FrameInfo]): Frames for the coroutine. @@ -1056,7 +1038,9 @@ Example output: coroutine_stack=[ CoroInfo(call_stack=[...], task_name=1) ], - awaited_by=[CoroInfo(call_stack=[...], task_name=2)] + awaited_by=[ + CoroInfo(call_stack=[...], task_name=2) + ] ), TaskInfo( task_id=2, @@ -1073,7 +1057,7 @@ Example output: static PyObject * _remote_debugging_RemoteUnwinder_get_async_stack_trace_impl(RemoteUnwinderObject *self) -/*[clinic end generated code: output=6433d52b55e87bbe input=0532031a7c495491]*/ +/*[clinic end generated code: output=6433d52b55e87bbe input=a95958816358476a]*/ { if (ensure_async_debug_offsets(self) < 0) { return NULL; @@ -1347,11 +1331,6 @@ RemoteUnwinder_dealloc(PyObject *op) if (self->code_object_cache) { _Py_hashtable_destroy(self->code_object_cache); } -#ifdef MS_WINDOWS - if (self->win_process_buffer != NULL) { - PyMem_Free(self->win_process_buffer); - } -#endif #ifdef Py_GIL_DISABLED if (self->tlbc_cache) { @@ -1406,11 +1385,6 @@ init_runtime_offsets(RuntimeOffsets *offsets, pid_t pid, int debug) set_exception_cause(offsets, PyExc_RuntimeError, "Failed to initialize process handle"); return -1; } - offsets->runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress(&offsets->handle); - if (offsets->runtime_start_address == 0) { - set_exception_cause(offsets, PyExc_RuntimeError, "Failed to get Python runtime address"); - goto error; - } if (_Py_RemoteDebug_ReadDebugOffsets(&offsets->handle, &offsets->runtime_start_address, &offsets->debug_offsets) < 0) @@ -1879,8 +1853,8 @@ _remote_debugging_BinaryWriter_set_stats_impl(BinaryWriterObject *self, /* Finalize the writer, cache total_samples, and destroy it. * * The cache assignment must happen AFTER binary_writer_finalize(): finalize - * flushes pending RLE records and updates the file header before the writer is - * destroyed. Caching before finalize would preserve a count for a file that + * flushes the open sample block and updates the file header before the writer + * is destroyed. Caching before finalize would preserve a count for a file that * might still fail to finish. */ static int binary_writer_finalize_and_cache(BinaryWriterObject *self) @@ -1994,13 +1968,13 @@ _remote_debugging.BinaryWriter.get_stats Get encoding statistics for the writer. Returns a dict with encoding statistics including -repeat/full/suffix/pop-push record counts, frames written/saved, and +repeat/full/suffix/reuse record counts, frames written/saved, and compression ratio. [clinic start generated code]*/ static PyObject * _remote_debugging_BinaryWriter_get_stats_impl(BinaryWriterObject *self) -/*[clinic end generated code: output=06522cd52544df89 input=a8bb8c8682ccd34b]*/ +/*[clinic end generated code: output=06522cd52544df89 input=09c317b6289ae311]*/ { if (!self->writer) { PyErr_SetString(PyExc_ValueError, "Writer is closed"); @@ -2081,6 +2055,22 @@ static PyType_Spec BinaryWriter_spec = { #define BinaryReader_CAST(op) ((BinaryReaderObject *)(op)) +static int +binary_reader_close_checked(BinaryReaderObject *self) +{ + if (self->reader && self->reader->replaying) { + PyErr_SetString(PyExc_RuntimeError, + "Cannot close or reinitialize the reader " + "while it is replaying"); + return -1; + } + if (self->reader) { + binary_reader_close(self->reader); + self->reader = NULL; + } + return 0; +} + /*[clinic input] class _remote_debugging.BinaryReader "BinaryReaderObject *" "&PyBinaryReader_Type" [clinic start generated code]*/ @@ -2103,10 +2093,9 @@ _remote_debugging_BinaryReader___init___impl(BinaryReaderObject *self, PyObject *filename) /*[clinic end generated code: output=f04b33ee5c5e6dbf input=9d7cbe8b4f1a97c9]*/ { - if (self->reader) { - binary_reader_close(self->reader); + if (binary_reader_close_checked(self) < 0) { + return -1; } - self->reader = binary_reader_open(filename); if (!self->reader) { return -1; @@ -2200,9 +2189,8 @@ static PyObject * _remote_debugging_BinaryReader_close_impl(BinaryReaderObject *self) /*[clinic end generated code: output=ad0238cf5240b4f8 input=b919a66c737712d5]*/ { - if (self->reader) { - binary_reader_close(self->reader); - self->reader = NULL; + if (binary_reader_close_checked(self) < 0) { + return NULL; } Py_RETURN_NONE; } @@ -2237,9 +2225,8 @@ _remote_debugging_BinaryReader___exit___impl(BinaryReaderObject *self, PyObject *exc_tb) /*[clinic end generated code: output=2acdd36cfdc14e4a input=87284243d7935835]*/ { - if (self->reader) { - binary_reader_close(self->reader); - self->reader = NULL; + if (binary_reader_close_checked(self) < 0) { + return NULL; } Py_RETURN_FALSE; } diff --git a/Modules/_remote_debugging/object_reading.c b/Modules/_remote_debugging/object_reading.c index e449b446a1fe54..ed1f07c9143e0f 100644 --- a/Modules/_remote_debugging/object_reading.c +++ b/Modules/_remote_debugging/object_reading.c @@ -185,9 +185,6 @@ read_py_bytes( uintptr_t address, Py_ssize_t max_len ) { - PyObject *result = NULL; - char *buf = NULL; - // Read the entire PyBytesObject at once char bytes_obj[SIZEOF_BYTES_OBJ]; int res = _Py_RemoteDebug_PagedReadRemoteMemory( @@ -198,7 +195,7 @@ read_py_bytes( ); if (res < 0) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read PyBytesObject"); - goto err; + return NULL; } Py_ssize_t len = GET_MEMBER(Py_ssize_t, bytes_obj, unwinder->debug_offsets.bytes_object.ob_size); @@ -209,36 +206,26 @@ read_py_bytes( return NULL; } - buf = (char *)PyMem_RawMalloc(len+1); - if (buf == NULL) { - PyErr_NoMemory(); + PyBytesWriter *writer = PyBytesWriter_Create(len); + if (writer == NULL) { set_exception_cause(unwinder, PyExc_MemoryError, "Failed to allocate buffer for bytes reading"); return NULL; } size_t offset = (size_t)unwinder->debug_offsets.bytes_object.ob_sval; - res = _Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, address + offset, len, buf); + res = _Py_RemoteDebug_PagedReadRemoteMemory(&unwinder->handle, address + offset, + len, PyBytesWriter_GetData(writer)); if (res < 0) { + PyBytesWriter_Discard(writer); set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to read bytes data from remote memory"); - goto err; + return NULL; } - buf[len] = '\0'; - result = PyBytes_FromStringAndSize(buf, len); + PyObject *result = PyBytesWriter_Finish(writer); if (result == NULL) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create PyBytes from remote bytes data"); - goto err; } - - PyMem_RawFree(buf); - assert(result != NULL); return result; - -err: - if (buf != NULL) { - PyMem_RawFree(buf); - } - return NULL; } long @@ -279,7 +266,7 @@ read_py_long( } // Calculate how many digits fit inline in our local buffer - Py_ssize_t ob_digit_offset = unwinder->debug_offsets.long_object.ob_digit; + Py_ssize_t ob_digit_offset = (Py_ssize_t)unwinder->debug_offsets.long_object.ob_digit; Py_ssize_t inline_digits_space = SIZEOF_LONG_OBJ - ob_digit_offset; Py_ssize_t max_inline_digits = inline_digits_space / (Py_ssize_t)sizeof(digit); diff --git a/Modules/_remote_debugging/threads.c b/Modules/_remote_debugging/threads.c index 120065022ddc8e..66c40d875ab3b3 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -18,6 +18,14 @@ #include #endif +#ifdef MS_WINDOWS +enum { + /* ThreadSystemThreadInformation is not named in the Windows SDK. */ + WIN32_THREAD_SYSTEM_INFORMATION = 40, + WIN32_THREAD_RUNNING = 2, +}; +#endif + /* Bound traversal of corrupted remote exception chains. */ #define MAX_EXCEPTION_CHAIN_DEPTH (2 << 15) @@ -158,45 +166,22 @@ find_running_frame( * THREAD STATUS FUNCTIONS * ============================================================================ */ -int -get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread_id) { +static int +get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { #if defined(__APPLE__) && TARGET_OS_OSX - if (!unwinder->thread_id_offset_initialized) { - uint64_t *tids = (uint64_t *)PyMem_Malloc(MAX_NATIVE_THREADS * sizeof(uint64_t)); - if (!tids) { - // Non-fatal: thread status is best-effort - return THREAD_STATE_UNKNOWN; - } - int n = proc_pidinfo(unwinder->handle.pid, PROC_PIDLISTTHREADS, 0, tids, MAX_NATIVE_THREADS * sizeof(uint64_t)) / sizeof(uint64_t); - if (n <= 0) { - PyMem_Free(tids); - return THREAD_STATE_UNKNOWN; - } - uint64_t min_offset = UINT64_MAX; - for (int i = 0; i < n; i++) { - uint64_t offset = tids[i] - pthread_id; - if (offset < min_offset) { - min_offset = offset; - } - } - unwinder->thread_id_offset = min_offset; - unwinder->thread_id_offset_initialized = 1; - PyMem_Free(tids); - } - struct proc_threadinfo ti; - uint64_t tid_with_offset = pthread_id + unwinder->thread_id_offset; - if (proc_pidinfo(unwinder->handle.pid, PROC_PIDTHREADINFO, tid_with_offset, &ti, sizeof(ti)) != sizeof(ti)) { + struct proc_threadinfo thread; + int size = proc_pidinfo(unwinder->handle.pid, PROC_PIDTHREADID64INFO, + tid, &thread, sizeof(thread)); + if (size != sizeof(thread)) { return THREAD_STATE_UNKNOWN; } - if (ti.pth_run_state == TH_STATE_RUNNING) { - return THREAD_STATE_RUNNING; - } - return THREAD_STATE_IDLE; + return thread.pth_run_state == TH_STATE_RUNNING + ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; #elif defined(__linux__) char stat_path[256]; char buffer[2048] = ""; - snprintf(stat_path, sizeof(stat_path), "/proc/%d/task/%" PRIu64 "/stat", unwinder->handle.pid, tid); + snprintf(stat_path, sizeof(stat_path), "/proc/%d/task/%lu/stat", unwinder->handle.pid, tid); int fd = open(stat_path, O_RDONLY); if (fd == -1) { @@ -232,46 +217,24 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread return THREAD_STATE_UNKNOWN; } #elif defined(MS_WINDOWS) - ULONG n; - NTSTATUS status = NtQuerySystemInformation( - SystemProcessInformation, - unwinder->win_process_buffer, - unwinder->win_process_buffer_size, - &n - ); - if (status == STATUS_INFO_LENGTH_MISMATCH) { - // Buffer was too small so we reallocate a larger one and try again. - unwinder->win_process_buffer_size = n; - PVOID new_buffer = PyMem_Realloc(unwinder->win_process_buffer, n); - if (!new_buffer) { - // Match Linux/macOS: degrade gracefully on alloc failure - return THREAD_STATE_UNKNOWN; - } - unwinder->win_process_buffer = new_buffer; - return get_thread_status(unwinder, tid, pthread_id); - } - if (status != STATUS_SUCCESS) { + HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)tid); + if (thread == NULL) { return THREAD_STATE_UNKNOWN; } - - SYSTEM_PROCESS_INFORMATION *pi = (SYSTEM_PROCESS_INFORMATION *)unwinder->win_process_buffer; - while ((ULONG)(ULONG_PTR)pi->UniqueProcessId != unwinder->handle.pid) { - if (pi->NextEntryOffset == 0) { - // Process not found (may have exited) - return THREAD_STATE_UNKNOWN; - } - pi = (SYSTEM_PROCESS_INFORMATION *)(((BYTE *)pi) + pi->NextEntryOffset); - } - - SYSTEM_THREAD_INFORMATION *ti = (SYSTEM_THREAD_INFORMATION *)((char *)pi + sizeof(SYSTEM_PROCESS_INFORMATION)); - for (size_t i = 0; i < pi->NumberOfThreads; i++, ti++) { - if (ti->ClientId.UniqueThread == (HANDLE)tid) { - return ti->ThreadState != WIN32_THREADSTATE_RUNNING ? THREAD_STATE_IDLE : THREAD_STATE_RUNNING; - } + SYSTEM_THREAD_INFORMATION info; + ULONG size = 0; + NTSTATUS result = NtQueryInformationThread( + thread, (THREADINFOCLASS)WIN32_THREAD_SYSTEM_INFORMATION, + &info, sizeof(info), &size); + CloseHandle(thread); + if (result != STATUS_SUCCESS || size != sizeof(info) || + (ULONG_PTR)info.ClientId.UniqueProcess != (ULONG_PTR)unwinder->handle.pid || + (ULONG_PTR)info.ClientId.UniqueThread != tid) + { + return THREAD_STATE_UNKNOWN; } - - // Thread not found (may have exited) - return THREAD_STATE_UNKNOWN; + return info.ThreadState == WIN32_THREAD_RUNNING + ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; #else return THREAD_STATE_UNKNOWN; #endif @@ -398,7 +361,8 @@ unwind_stack_for_thread( STATS_ADD(unwinder, memory_bytes_read, SIZEOF_INTERP_FRAME); } - long tid = GET_MEMBER(long, ts, unwinder->debug_offsets.thread_state.native_thread_id); + unsigned long tid = GET_MEMBER(unsigned long, ts, + unwinder->debug_offsets.thread_state.native_thread_id); // Calculate thread status using flags (always) int status_flags = 0; @@ -447,9 +411,9 @@ unwind_stack_for_thread( unwinder->debug_offsets.thread_state.current_exception + sizeof(uintptr_t)); uintptr_t exc_state_addr = - *current_tstate + unwinder->debug_offsets.thread_state.exc_state; + *current_tstate + (uintptr_t)unwinder->debug_offsets.thread_state.exc_state; uintptr_t exc_value_offset = - unwinder->debug_offsets.err_stackitem.exc_value; + (uintptr_t)unwinder->debug_offsets.err_stackitem.exc_value; uintptr_t previous_item_offset = exc_value_offset + sizeof(uintptr_t); @@ -490,13 +454,9 @@ unwind_stack_for_thread( status_flags |= THREAD_STATUS_HAS_EXCEPTION; } - // Check CPU status - long pthread_id = GET_MEMBER(long, ts, unwinder->debug_offsets.thread_state.thread_id); - - // Optimization: only check CPU status if needed by mode because it's expensive int cpu_status = THREAD_STATE_UNKNOWN; if (unwinder->mode == PROFILING_MODE_CPU || unwinder->mode == PROFILING_MODE_ALL) { - cpu_status = get_thread_status(unwinder, tid, pthread_id); + cpu_status = get_thread_status(unwinder, tid); } if (cpu_status == THREAD_STATE_UNKNOWN) { @@ -597,7 +557,7 @@ unwind_stack_for_thread( } } if (thread_id == NULL) { - thread_id = PyLong_FromLongLong(tid); + thread_id = PyLong_FromUnsignedLong(tid); if (thread_id == NULL) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create thread ID"); goto error; diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c index 8b6b617aafa427..8b37dc7bb34bed 100644 --- a/Modules/_testbuffer.c +++ b/Modules/_testbuffer.c @@ -2171,7 +2171,6 @@ ndarray_tobytes(PyObject *self, PyObject *dummy) Py_buffer *src = &ndbuf->base; Py_buffer dest; PyObject *ret = NULL; - char *mem; if (ND_C_CONTIGUOUS(ndbuf->flags)) return PyBytes_FromStringAndSize(src->buf, src->len); @@ -2180,14 +2179,13 @@ ndarray_tobytes(PyObject *self, PyObject *dummy) assert(src->strides != NULL); assert(src->ndim > 0); - mem = PyMem_Malloc(src->len); - if (mem == NULL) { - PyErr_NoMemory(); + PyBytesWriter *writer = PyBytesWriter_Create(src->len); + if (writer == NULL) { return NULL; } dest = *src; - dest.buf = mem; + dest.buf = PyBytesWriter_GetData(writer); dest.suboffsets = NULL; dest.strides = strides_from_shape(ndbuf, 0); if (dest.strides == NULL) @@ -2195,11 +2193,12 @@ ndarray_tobytes(PyObject *self, PyObject *dummy) if (copy_buffer(&dest, src) < 0) goto out; - ret = PyBytes_FromStringAndSize(mem, src->len); + ret = PyBytesWriter_Finish(writer); + writer = NULL; out: PyMem_XFree(dest.strides); - PyMem_Free(mem); + PyBytesWriter_Discard(writer); return ret; } @@ -2445,7 +2444,7 @@ py_buffer_to_contiguous(PyObject *self, PyObject *args) int flags; char ord; Py_buffer view; - char *buf = NULL; + PyBytesWriter *writer = NULL; if (!PyArg_ParseTuple(args, "OOi", &obj, &order, &flags)) { return NULL; @@ -2460,21 +2459,21 @@ py_buffer_to_contiguous(PyObject *self, PyObject *args) goto out; } - buf = PyMem_Malloc(view.len); - if (buf == NULL) { - PyErr_NoMemory(); + writer = PyBytesWriter_Create(view.len); + if (writer == NULL) { goto out; } - if (PyBuffer_ToContiguous(buf, &view, view.len, ord) < 0) { + if (PyBuffer_ToContiguous(PyBytesWriter_GetData(writer), &view, view.len, ord) < 0) { goto out; } - ret = PyBytes_FromStringAndSize(buf, view.len); + ret = PyBytesWriter_Finish(writer); + writer = NULL; out: PyBuffer_Release(&view); - PyMem_XFree(buf); + PyBytesWriter_Discard(writer); return ret; } diff --git a/Modules/_testcapi/mem.c b/Modules/_testcapi/mem.c index 257568172de8b1..822ae661944a24 100644 --- a/Modules/_testcapi/mem.c +++ b/Modules/_testcapi/mem.c @@ -13,6 +13,8 @@ # include // proc_pidinfo(), PROC_PIDTASKINFO # include // struct proc_taskinfo # endif +#else +# define TARGET_OS_OSX 0 #endif #ifdef __FreeBSD__ diff --git a/Modules/_testcapi/object.c b/Modules/_testcapi/object.c index 8fcb86e33e55a8..1bb038aea654a1 100644 --- a/Modules/_testcapi/object.c +++ b/Modules/_testcapi/object.c @@ -153,6 +153,13 @@ pyobject_is_unique_temporary_new_object(PyObject *self, PyObject *unused) return PyLong_FromLong(result); } +static PyObject * +pyobject_clear_managed_dict(PyObject *self, PyObject *obj) +{ + PyObject_ClearManagedDict(obj); + Py_RETURN_NONE; +} + static int MyObject_dealloc_called = 0; static void @@ -358,6 +365,7 @@ static PyMethodDef test_methods[] = { {"pyobject_enable_deferred_refcount", pyobject_enable_deferred_refcount, METH_O}, {"pyobject_is_unique_temporary", pyobject_is_unique_temporary, METH_O}, {"pyobject_is_unique_temporary_new_object", pyobject_is_unique_temporary_new_object, METH_NOARGS}, + {"pyobject_clear_managed_dict", pyobject_clear_managed_dict, METH_O}, {"test_py_try_inc_ref", test_py_try_inc_ref, METH_NOARGS}, {"test_py_set_immortal", test_py_set_immortal, METH_NOARGS}, #ifdef Py_REF_DEBUG diff --git a/Modules/_testcapi/pyatomic.c b/Modules/_testcapi/pyatomic.c index 850de6f9c3366b..81947b55e9a81d 100644 --- a/Modules/_testcapi/pyatomic.c +++ b/Modules/_testcapi/pyatomic.c @@ -49,6 +49,12 @@ test_atomic_add_##suffix(PyObject *self, PyObject *obj) { \ assert(x == (dtype)-3); \ assert(_Py_atomic_add_##suffix(&x, 2) == (dtype)-3); \ assert(x == (dtype)-1); \ + assert(_Py_atomic_add_##suffix##_relaxed(&x, 1) == (dtype)-1); \ + assert(x == 0); \ + assert(_Py_atomic_add_##suffix##_relaxed(&x, 3) == 0); \ + assert(x == 3); \ + assert(_Py_atomic_add_##suffix##_relaxed(&x, -4) == 3); \ + assert(x == (dtype)-1); \ Py_RETURN_NONE; \ } FOR_ARITHMETIC_TYPES(IMPL_TEST_ADD) @@ -68,6 +74,17 @@ test_atomic_compare_exchange_##suffix(PyObject *self, PyObject *obj) { \ assert(_Py_atomic_compare_exchange_##suffix(&x, &y, z) == 0); \ assert(x == z); \ assert(y == z); \ + x = (dtype)0; \ + y = (dtype)1; \ + assert(_Py_atomic_compare_exchange_##suffix##_relaxed(&x, &y, z) == 0); \ + assert(x == 0); \ + assert(y == 0); \ + assert(_Py_atomic_compare_exchange_##suffix##_relaxed(&x, &y, z) == 1); \ + assert(x == z); \ + assert(y == 0); \ + assert(_Py_atomic_compare_exchange_##suffix##_relaxed(&x, &y, z) == 0); \ + assert(x == z); \ + assert(y == z); \ Py_RETURN_NONE; \ } FOR_ALL_TYPES(IMPL_TEST_COMPARE_EXCHANGE) diff --git a/Modules/_testclinic.c b/Modules/_testclinic.c index b875647598c8c0..fc87f521e4999b 100644 --- a/Modules/_testclinic.c +++ b/Modules/_testclinic.c @@ -1869,6 +1869,20 @@ static PyTypeObject TestClass = { .tp_methods = test_class_methods, }; +/* Heap type sharing TestClass's methods, to test defining_class on both. */ +static PyType_Slot HeapTestClass_slots[] = { + {Py_tp_new, PyType_GenericNew}, + {Py_tp_methods, test_class_methods}, + {0, NULL}, +}; + +static PyType_Spec HeapTestClass_spec = { + .name = "_testclinic.HeapTestClass", + .basicsize = sizeof(PyObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, + .slots = HeapTestClass_slots, +}; + /*[clinic input] output push @@ -1923,13 +1937,17 @@ static struct PyMethodDef depr_star_new_methods[] = { {NULL, NULL} }; -static PyTypeObject DeprStarNew = { - PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "_testclinic.DeprStarNew", - .tp_basicsize = sizeof(PyObject), - .tp_new = depr_star_new, - .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_methods = depr_star_new_methods, +static PyType_Slot DeprStarNew_slots[] = { + {Py_tp_new, depr_star_new}, + {Py_tp_methods, depr_star_new_methods}, + {0, NULL}, +}; + +static PyType_Spec DeprStarNew_spec = { + .name = "_testclinic.DeprStarNew", + .basicsize = sizeof(PyObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, + .slots = DeprStarNew_slots, }; @@ -1964,14 +1982,18 @@ static struct PyMethodDef depr_star_init_methods[] = { {NULL, NULL} }; -static PyTypeObject DeprStarInit = { - PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "_testclinic.DeprStarInit", - .tp_basicsize = sizeof(PyObject), - .tp_new = PyType_GenericNew, - .tp_init = depr_star_init, - .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_methods = depr_star_init_methods, +static PyType_Slot DeprStarInit_slots[] = { + {Py_tp_new, PyType_GenericNew}, + {Py_tp_init, depr_star_init}, + {Py_tp_methods, depr_star_init_methods}, + {0, NULL}, +}; + +static PyType_Spec DeprStarInit_spec = { + .name = "_testclinic.DeprStarInit", + .basicsize = sizeof(PyObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, + .slots = DeprStarInit_slots, }; @@ -1995,13 +2017,17 @@ depr_star_init_noinline_impl(PyObject *self, PyObject *a, PyObject *b, return 0; } -static PyTypeObject DeprStarInitNoInline = { - PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "_testclinic.DeprStarInitNoInline", - .tp_basicsize = sizeof(PyObject), - .tp_new = PyType_GenericNew, - .tp_init = depr_star_init_noinline, - .tp_flags = Py_TPFLAGS_DEFAULT, +static PyType_Slot DeprStarInitNoInline_slots[] = { + {Py_tp_new, PyType_GenericNew}, + {Py_tp_init, depr_star_init_noinline}, + {0, NULL}, +}; + +static PyType_Spec DeprStarInitNoInline_spec = { + .name = "_testclinic.DeprStarInitNoInline", + .basicsize = sizeof(PyObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, + .slots = DeprStarInitNoInline_slots, }; @@ -2021,12 +2047,16 @@ depr_kwd_new_impl(PyTypeObject *type, PyObject *a) return type->tp_alloc(type, 0); } -static PyTypeObject DeprKwdNew = { - PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "_testclinic.DeprKwdNew", - .tp_basicsize = sizeof(PyObject), - .tp_new = depr_kwd_new, - .tp_flags = Py_TPFLAGS_DEFAULT, +static PyType_Slot DeprKwdNew_slots[] = { + {Py_tp_new, depr_kwd_new}, + {0, NULL}, +}; + +static PyType_Spec DeprKwdNew_spec = { + .name = "_testclinic.DeprKwdNew", + .basicsize = sizeof(PyObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, + .slots = DeprKwdNew_slots, }; @@ -2045,13 +2075,17 @@ depr_kwd_init_impl(PyObject *self, PyObject *a) return 0; } -static PyTypeObject DeprKwdInit = { - PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "_testclinic.DeprKwdInit", - .tp_basicsize = sizeof(PyObject), - .tp_new = PyType_GenericNew, - .tp_init = depr_kwd_init, - .tp_flags = Py_TPFLAGS_DEFAULT, +static PyType_Slot DeprKwdInit_slots[] = { + {Py_tp_new, PyType_GenericNew}, + {Py_tp_init, depr_kwd_init}, + {0, NULL}, +}; + +static PyType_Spec DeprKwdInit_spec = { + .name = "_testclinic.DeprKwdInit", + .basicsize = sizeof(PyObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, + .slots = DeprKwdInit_slots, }; @@ -2075,13 +2109,17 @@ depr_kwd_init_noinline_impl(PyObject *self, PyObject *a, PyObject *b, return 0; } -static PyTypeObject DeprKwdInitNoInline = { - PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "_testclinic.DeprKwdInitNoInline", - .tp_basicsize = sizeof(PyObject), - .tp_new = PyType_GenericNew, - .tp_init = depr_kwd_init_noinline, - .tp_flags = Py_TPFLAGS_DEFAULT, +static PyType_Slot DeprKwdInitNoInline_slots[] = { + {Py_tp_new, PyType_GenericNew}, + {Py_tp_init, depr_kwd_init_noinline}, + {0, NULL}, +}; + +static PyType_Spec DeprKwdInitNoInline_spec = { + .name = "_testclinic.DeprKwdInitNoInline", + .basicsize = sizeof(PyObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, + .slots = DeprKwdInitNoInline_slots, }; @@ -2633,6 +2671,40 @@ static PyTypeObject VcKwOnly_Type = { }; +/* VcKwds: @vectorcall + **kwargs. __new__ returns the parsed arguments. */ + +/*[clinic input] +class _testclinic.VcKwds "PyObject *" "" +@classmethod +@vectorcall +_testclinic.VcKwds.__new__ as vc_kwds_new + a: object + b: object = None + / + **kwds: dict +[clinic start generated code]*/ + +static PyObject * +vc_kwds_new_impl(PyTypeObject *type, PyObject *a, PyObject *b, + PyObject *kwds) +/*[clinic end generated code: output=481e680d2e76fdbd input=3d8e5218fd593383]*/ +{ + return pack_arguments_newref(3, a, b, kwds); +} + +static PyType_Slot VcKwds_slots[] = { + {Py_tp_new, vc_kwds_new}, + {Py_tp_vectorcall, vc_kwds_vectorcall}, + {0, NULL}, +}; +static PyType_Spec VcKwds_spec = { + .name = "_testclinic.VcKwds", + .basicsize = sizeof(PyObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, + .slots = VcKwds_slots, +}; + + /*[clinic input] output push @@ -2843,62 +2915,56 @@ static PyMethodDef tester_methods[] = { {NULL, NULL} }; +static int +_testclinic_exec(PyObject *m) +{ + PyTypeObject *static_types[] = { + &TestClass, &VcNew_Type, &VcNewBase_Type, &VcKwOnly_Type, + }; + for (size_t i = 0; i < Py_ARRAY_LENGTH(static_types); i++) { + if (PyModule_AddType(m, static_types[i]) < 0) { + return -1; + } + } + + PyType_Spec *heap_specs[] = { + &HeapTestClass_spec, + &DeprStarNew_spec, &DeprStarInit_spec, &DeprStarInitNoInline_spec, + &DeprKwdNew_spec, &DeprKwdInit_spec, &DeprKwdInitNoInline_spec, + &VcInit_spec, &VcKwds_spec, + }; + for (size_t i = 0; i < Py_ARRAY_LENGTH(heap_specs); i++) { + PyObject *type = PyType_FromModuleAndSpec(m, heap_specs[i], NULL); + if (type == NULL) { + return -1; + } + int rc = PyModule_AddType(m, (PyTypeObject *)type); + Py_DECREF(type); + if (rc < 0) { + return -1; + } + } + return 0; +} + +static PyModuleDef_Slot _testclinic_slots[] = { + {Py_mod_exec, _testclinic_exec}, + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, + {0, NULL}, +}; + static struct PyModuleDef _testclinic_module = { PyModuleDef_HEAD_INIT, .m_name = "_testclinic", .m_size = 0, .m_methods = tester_methods, + .m_slots = _testclinic_slots, }; PyMODINIT_FUNC PyInit__testclinic(void) { - PyObject *m = PyModule_Create(&_testclinic_module); - if (m == NULL) { - return NULL; - } -#ifdef Py_GIL_DISABLED - PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); -#endif - if (PyModule_AddType(m, &TestClass) < 0) { - goto error; - } - if (PyModule_AddType(m, &DeprStarNew) < 0) { - goto error; - } - if (PyModule_AddType(m, &DeprStarInit) < 0) { - goto error; - } - if (PyModule_AddType(m, &DeprStarInitNoInline) < 0) { - goto error; - } - if (PyModule_AddType(m, &DeprKwdNew) < 0) { - goto error; - } - if (PyModule_AddType(m, &DeprKwdInit) < 0) { - goto error; - } - if (PyModule_AddType(m, &DeprKwdInitNoInline) < 0) { - goto error; - } - if (PyModule_AddType(m, &VcNew_Type) < 0) { - goto error; - } - if (PyModule_Add(m, "VcInit", - PyType_FromModuleAndSpec(m, &VcInit_spec, NULL)) < 0) { - goto error; - } - if (PyModule_AddType(m, &VcNewBase_Type) < 0) { - goto error; - } - if (PyModule_AddType(m, &VcKwOnly_Type) < 0) { - goto error; - } - return m; - -error: - Py_DECREF(m); - return NULL; + return PyModuleDef_Init(&_testclinic_module); } #undef RETURN_PACKED_ARGS diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 3efb70f662698b..586b687f233ce0 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -111,10 +111,20 @@ static const uintptr_t min_frame_pointer_addr = 0x1000; // address is stored at fp[-1]. # define FRAME_POINTER_NEXT_OFFSET -2 # define FRAME_POINTER_RETURN_OFFSET -1 +#elif defined(__sparc__) && defined(__arch64__) +// SPARC V9 has no frame record: fp points to the register window save area, +// where the caller's %fp is saved at fp[14] and its return address (%i7) at +// fp[15]. The saved %fp is biased by 2047. +# define FRAME_POINTER_NEXT_OFFSET 14 +# define FRAME_POINTER_RETURN_OFFSET 15 +# define FRAME_POINTER_BIAS 2047 #else # define FRAME_POINTER_NEXT_OFFSET 0 # define FRAME_POINTER_RETURN_OFFSET 1 #endif +#ifndef FRAME_POINTER_BIAS +# define FRAME_POINTER_BIAS 0 +#endif static PyObject * @@ -564,7 +574,8 @@ manual_unwind_from_fp(uintptr_t *frame_pointer) stack_min, stack_max)) { break; } - uintptr_t *next_fp = (uintptr_t *)frame_pointer[FRAME_POINTER_NEXT_OFFSET]; + uintptr_t *next_fp = (uintptr_t *)(frame_pointer[FRAME_POINTER_NEXT_OFFSET] + + FRAME_POINTER_BIAS); uintptr_t return_addr = frame_pointer[FRAME_POINTER_RETURN_OFFSET]; PyObject *addr_obj = PyLong_FromUnsignedLongLong(return_addr); @@ -636,6 +647,10 @@ gnu_backtrace_unwind(PyObject *self, PyObject *Py_UNUSED(args)) static PyObject * manual_frame_pointer_unwind(PyObject *self, PyObject *args) { +#if defined(__sparc__) && defined(__arch64__) + // The callers' register windows are only in their save areas once spilled. + __asm__ volatile ("flushw" ::: "memory"); +#endif uintptr_t *frame_pointer = (uintptr_t *)__builtin_frame_address(0); return manual_unwind_from_fp(frame_pointer); } diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 8e7ba1f69553dd..8bb3f857437792 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -6799,6 +6799,8 @@ INSTRUCTION_STATS(GET_ANEXT); _PyStackRef aiter; _PyStackRef awaitable; + _PyStackRef null; + _PyStackRef none; aiter = stack_pointer[-1]; _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); @@ -6808,8 +6810,12 @@ JUMP_TO_LABEL(error); } awaitable = PyStackRef_FromPyObjectSteal(awaitable_o); + null = PyStackRef_NULL; + none = PyStackRef_None; stack_pointer[0] = awaitable; - stack_pointer += 1; + stack_pointer[1] = null; + stack_pointer[2] = none; + stack_pointer += 3; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); DISPATCH(); } @@ -6824,6 +6830,8 @@ INSTRUCTION_STATS(GET_AWAITABLE); _PyStackRef iterable; _PyStackRef iter; + _PyStackRef null; + _PyStackRef none; iterable = stack_pointer[-1]; _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); @@ -6839,8 +6847,12 @@ JUMP_TO_LABEL(error); } iter = PyStackRef_FromPyObjectSteal(iter_o); + null = PyStackRef_NULL; + none = PyStackRef_None; stack_pointer[0] = iter; - stack_pointer += 1; + stack_pointer[1] = null; + stack_pointer[2] = none; + stack_pointer += 3; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); DISPATCH(); } @@ -10120,7 +10132,7 @@ assert(_PyOpcode_Deopt[opcode] == (LOAD_GLOBAL)); JUMP_TO_PREDICTED(LOAD_GLOBAL); } - #if Py_GIL_DISABLED + #ifdef Py_GIL_DISABLED int increfed = _Py_TryIncrefCompareStackRef(&entries[index].me_value, res_o, &res); if (!increfed) { UPDATE_MISS_STATS(LOAD_GLOBAL); @@ -10187,7 +10199,7 @@ assert(_PyOpcode_Deopt[opcode] == (LOAD_GLOBAL)); JUMP_TO_PREDICTED(LOAD_GLOBAL); } - #if Py_GIL_DISABLED + #ifdef Py_GIL_DISABLED int increfed = _Py_TryIncrefCompareStackRef(&entries[index].me_value, res_o, &res); if (!increfed) { UPDATE_MISS_STATS(LOAD_GLOBAL); diff --git a/Modules/_testlimitedcapi/heaptype_relative.c b/Modules/_testlimitedcapi/heaptype_relative.c index a5e15026f028ca..f21ca9250ed266 100644 --- a/Modules/_testlimitedcapi/heaptype_relative.c +++ b/Modules/_testlimitedcapi/heaptype_relative.c @@ -68,7 +68,7 @@ make_sized_heaptypes(PyObject *module, PyObject *args) } result = Py_BuildValue("OOOKnn", base, sub, instance, - (unsigned long long)data_ptr, + (unsigned long long)(uintptr_t)data_ptr, (Py_ssize_t)(data_ptr - (char*)instance), data_size); finally: diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 9a09687e55b981..5eadf6b235e1d7 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -31,6 +31,7 @@ Copyright (C) 1994 Steen Lumholt. #endif #include "pycore_long.h" // _PyLong_IsNegative() +#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_INT_RELAXED() #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8String #include // SIGINT @@ -42,9 +43,10 @@ Copyright (C) 1994 Steen Lumholt. #define CHECK_SIZE(size, elemsize) \ ((size_t)(size) <= Py_MIN((size_t)INT_MAX, UINT_MAX / (size_t)(elemsize))) -/* If Tcl is compiled for threads, we must also define TCL_THREAD. We define - it always; if Tcl is not threaded, the thread functions in - Tcl are empty. */ +/* Tcl 8 headers make the Tcl mutex and condition variable functions + no-ops unless TCL_THREADS is defined. Tcl is required to be built + with thread support (this is checked when creating a tkapp), so + always define it. */ #define TCL_THREADS #ifdef TK_FRAMEWORK @@ -250,102 +252,94 @@ Tkinter_TkInit(Tcl_Interp *interp) return result; } -/* The threading situation is complicated. Tcl is not thread-safe, except - when configured with --enable-threads. +/* Tcl is required to be built with thread support. A Tcl interpreter is + only valid in the thread that created it, and all Tk activity must + happen in this thread. That means that the mainloop must be invoked in + the thread that created the interpreter. Invoking commands from other + threads is possible: _tkinter queues an event for the interpreter + thread, which executes the command and passes back the result. If the + interpreter thread is not in the mainloop, invoking a command raises an + exception; if the mainloop is running but not processing events, the + invocation blocks. - So we need to use a lock around all uses of Tcl. Previously, the - Python interpreter lock was used for this. However, this causes - problems when other Python threads need to run while Tcl is blocked - waiting for events. - - To solve this problem, a separate lock for Tcl is introduced. - Holding it is incompatible with holding Python's interpreter lock. - The following four macros manipulate both locks together. + The Python interpreter lock is released while Tcl runs, so that other + Python threads can run while Tcl is blocked waiting for events. The + thread state saved for that is kept in tcl_tstate, which is + thread-local, since several Tcl interpreters may run in different + threads at the same time. The following macros manage this. ENTER_TCL and LEAVE_TCL are brackets, just like Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS. They should be used whenever a call into Tcl is made that could call an event handler, or otherwise affect the state of a Tcl interpreter. These assume that the surrounding code has the Python interpreter lock; - inside the brackets, the Python interpreter lock has been released - and the lock for Tcl has been acquired. + inside the brackets, the Python interpreter lock has been released. - Sometimes, it is necessary to have both the Python lock and the Tcl - lock. (For example, when transferring data from the Tcl - interpreter result to a Python string object.) This can be done by - using different macros to close the ENTER_TCL block: ENTER_OVERLAP - reacquires the Python lock (and restores the thread state) but - doesn't release the Tcl lock; LEAVE_OVERLAP_TCL releases the Tcl - lock. + Sometimes, it is necessary to use Python inside the ENTER_TCL block, + for example when transferring data from the Tcl interpreter result to + a Python object. This can be done by using different macros to close + the ENTER_TCL block: ENTER_OVERLAP reacquires the Python interpreter + lock (and restores the thread state); LEAVE_OVERLAP_TCL then closes + the block. By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event handlers when the handler needs to use Python. Such event handlers - are entered while the lock for Tcl is held; the event handler - presumably needs to use Python. ENTER_PYTHON releases the lock for - Tcl and acquires the Python interpreter lock, restoring the - appropriate thread state, and LEAVE_PYTHON releases the Python - interpreter lock and re-acquires the lock for Tcl. It is okay for - ENTER_TCL/LEAVE_TCL pairs to be contained inside the code between - ENTER_PYTHON and LEAVE_PYTHON. - - These locks expand to several statements and brackets; they should + are called inside an ENTER_TCL block. ENTER_PYTHON acquires the + Python interpreter lock, restoring the saved thread state, and + LEAVE_PYTHON releases it again. It is okay for ENTER_TCL/LEAVE_TCL + pairs to be contained inside the code between ENTER_PYTHON and + LEAVE_PYTHON. + + An event handler can also be called when Tcl is not entered with + ENTER_TCL, for example from a foreign event loop on Windows, which + dispatches a window message to Tk while the Python interpreter lock + is released. Then there is no saved thread state, so ENTER_PYTHON + acquires the Python interpreter lock with PyGILState_Ensure() and + LEAVE_PYTHON releases it with PyGILState_Release(). + + These macros expand to several statements and brackets; they should not be used in branches of if statements and the like. - - If Tcl is threaded, this approach won't work anymore. The Tcl - interpreter is only valid in the thread that created it, and all Tk - activity must happen in this thread, also. That means that the - mainloop must be invoked in the thread that created the - interpreter. Invoking commands from other threads is possible; - _tkinter will queue an event for the interpreter thread, which will - then execute the command and pass back the result. If the main - thread is not in the mainloop, and invoking commands causes an - exception; if the main loop is running but not processing events, - the command invocation will block. - - In addition, for a threaded Tcl, a single global tcl_tstate won't - be sufficient anymore, since multiple Tcl interpreters may - simultaneously dispatch in different threads. So we use the Tcl TLS - API. - */ -static PyThread_type_lock tcl_lock = 0; - -#ifdef TCL_THREADS static Tcl_ThreadDataKey state_key; #define tcl_tstate \ (*(PyThreadState**)Tcl_GetThreadData(&state_key, sizeof(PyThreadState*))) -#else -static PyThreadState *tcl_tstate = NULL; -#endif #define ENTER_TCL \ { PyThreadState *tstate = PyThreadState_Get(); \ Py_BEGIN_ALLOW_THREADS \ - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); \ tcl_tstate = tstate; #define LEAVE_TCL \ tcl_tstate = NULL; \ - if(tcl_lock)PyThread_release_lock(tcl_lock); \ Py_END_ALLOW_THREADS} #define ENTER_OVERLAP \ Py_END_ALLOW_THREADS #define LEAVE_OVERLAP_TCL \ - tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); } + tcl_tstate = NULL; } #define ENTER_PYTHON \ - { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \ - if(tcl_lock) \ - PyThread_release_lock(tcl_lock); \ - PyEval_RestoreThread((tstate)); } + { PyThreadState *tstate = tcl_tstate; \ + PyGILState_STATE gilstate = PyGILState_UNLOCKED; \ + if (tstate == NULL) { \ + gilstate = PyGILState_Ensure(); \ + } \ + else { \ + tcl_tstate = NULL; \ + PyEval_RestoreThread(tstate); \ + } #define LEAVE_PYTHON \ - { PyThreadState *tstate = PyEval_SaveThread(); \ - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); \ - tcl_tstate = tstate; } + if (tstate == NULL) { \ + PyGILState_Release(gilstate); \ + } \ + else { \ + tstate = PyEval_SaveThread(); \ + tcl_tstate = tstate; \ + } \ + } #ifndef FREECAST #define FREECAST (char *) @@ -359,9 +353,10 @@ typedef struct { PyObject_HEAD Tcl_Interp *interp; int wantobjects; - int threaded; /* True if tcl_platform[threaded] */ Tcl_ThreadId thread_id; int dispatching; + int quitting; /* Set by quit() to stop mainloop() */ + int event_hook; /* True if counted by EnableEventHook() */ PyObject *trace; /* We cannot include tclInt.h, as this is internal. So we cache interesting types here. */ @@ -393,7 +388,7 @@ typedef struct { static inline int check_tcl_appartment(TkappObject *app) { - if (app->threaded && app->thread_id != Tcl_GetCurrentThread()) { + if (app->thread_id != Tcl_GetCurrentThread()) { PyErr_SetString(PyExc_RuntimeError, "Calling Tcl from different apartment"); return -1; @@ -412,9 +407,14 @@ check_tcl_appartment(TkappObject *app) /**** Error Handling ****/ static PyObject *Tkinter_TclError; -static int quitMainLoop = 0; -static int errorInCmd = 0; -static PyObject *excInCmd; + +/* An exception raised in a Python callback, which should be raised by the + event loop (mainloop() or the event hook) that called the callback. + Callbacks are only called in the thread of their Tcl interpreter, so it + is thread-local. */ +static Tcl_ThreadDataKey excInCmd_key; +#define excInCmd \ + (*(PyObject **)Tcl_GetThreadData(&excInCmd_key, sizeof(PyObject *))) static PyObject *Tkapp_UnicodeResult(TkappObject *); @@ -644,8 +644,8 @@ Tcl_AppInit(Tcl_Interp *interp) * `tkMain.c'. */ -static void EnableEventHook(void); /* Forward */ -static void DisableEventHook(void); /* Forward */ +static void EnableEventHook(TkappObject *); /* Forward */ +static void DisableEventHook(TkappObject *); /* Forward */ static TkappObject * Tkapp_New(const char *screenName, const char *className, @@ -654,37 +654,37 @@ Tkapp_New(const char *screenName, const char *className, { TkappObject *v; char *argv0; +#if TCL_MAJOR_VERSION < 9 /* Tcl 9.x is always threaded */ + Tcl_Obj* threaded; +#endif PyTypeObject *tp = (PyTypeObject *)Tkapp_Type; v = (TkappObject *)tp->tp_alloc(tp, 0); if (v == NULL) return NULL; + /* Without the GIL, the first-time initialization in Tcl_CreateInterp() + is not thread-safe. Serialize it. */ + static PyMutex tcl_create_mutex; + PyMutex_Lock(&tcl_create_mutex); v->interp = Tcl_CreateInterp(); - v->wantobjects = wantobjects; -#if TCL_MAJOR_VERSION >= 9 - v->threaded = 1; -#else - v->threaded = Tcl_GetVar2Ex(v->interp, "tcl_platform", "threaded", - TCL_GLOBAL_ONLY) != NULL; -#endif + PyMutex_Unlock(&tcl_create_mutex); v->thread_id = Tcl_GetCurrentThread(); - v->dispatching = 0; - v->trace = NULL; - -#ifndef TCL_THREADS - if (v->threaded) { +#if TCL_MAJOR_VERSION < 9 /* Tcl 9.x is always threaded */ + threaded = Tcl_GetVar2Ex(v->interp, + "tcl_platform", + "threaded", + TCL_GLOBAL_ONLY); + if (threaded == NULL) { PyErr_SetString(PyExc_RuntimeError, - "Tcl is threaded but _tkinter is not"); + "Tcl must be compiled with thread support"); Py_DECREF(v); - return 0; + return NULL; } #endif - if (v->threaded && tcl_lock) { - /* If Tcl is threaded, we don't need the lock. */ - PyThread_free_lock(tcl_lock); - tcl_lock = NULL; - } + v->wantobjects = wantobjects; + v->dispatching = 0; + v->trace = NULL; v->OldBooleanType = Tcl_GetObjType("boolean"); { @@ -831,7 +831,7 @@ Tkapp_New(const char *screenName, const char *className, return (TkappObject *)result; } - EnableEventHook(); + EnableEventHook(v); return v; } @@ -1234,8 +1234,9 @@ AsObjNested(PyObject *value, int depth) { PyObject *v = PyObject_Str(value); - if (!v) - return 0; + if (!v) { + return NULL; + } result = AsObjNested(v, depth); Py_DECREF(v); return result; @@ -1657,8 +1658,7 @@ Tkapp_Trace(TkappObject *self, PyObject *args) } while (0) /* Tkapp_CallProc is the event procedure that is executed in the context of - the Tcl interpreter thread. Initially, it holds the Tcl lock, and doesn't - hold the Python lock. */ + the Tcl interpreter thread. Initially, it doesn't hold the Python lock. */ static int Tkapp_CallProc(Tcl_Event *evPtr, int flags) @@ -1706,13 +1706,10 @@ Tkapp_CallProc(Tcl_Event *evPtr, int flags) /* This is the main entry point for calling a Tcl command. - It supports three cases, with regard to threading: - 1. Tcl is not threaded: Must have the Tcl lock, then can invoke command in - the context of the calling thread. - 2. Tcl is threaded, caller of the command is in the interpreter thread: - Execute the command in the calling thread. Since the Tcl lock will - not be used, we can merge that with case 1. - 3. Tcl is threaded, caller is in a different thread: Must queue an event to + It supports two cases, with regard to threading: + 1. Caller of the command is in the interpreter thread: + Execute the command in the calling thread. + 2. Caller is in a different thread: Must queue an event to the interpreter thread. Allocation of Tcl objects needs to occur in the interpreter thread, so we ship the PyObject* args to the target thread, and perform processing there. */ @@ -1740,7 +1737,7 @@ _tkinter_tkapp_call_impl(TkappObject *self, PyObject *args) if (PyTuple_Check(item)) args = item; } - if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { + if (self->thread_id != Tcl_GetCurrentThread()) { /* We cannot call the command directly. Instead, we must marshal the parameters to the interpreter thread. */ Tkapp_CallEvent *ev; @@ -2017,7 +2014,7 @@ static PyObject* var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags) { TkappObject *self = TkappObject_CAST(selfptr); - if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { + if (self->thread_id != Tcl_GetCurrentThread()) { VarEvent *ev; // init 'res' and 'exc' to make static analyzers happy PyObject *res = NULL, *exc = NULL; @@ -2051,7 +2048,7 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags) } return res; } - /* Tcl is not threaded, or this is the interpreter thread. */ + /* This is the interpreter thread. */ return func(self, args, flags); } @@ -2068,7 +2065,6 @@ SetVar(TkappObject *self, PyObject *args, int flags) if (!PyArg_ParseTuple(args, "O&O:setvar", varname_converter, &name1, &newValue)) return NULL; - /* XXX Acquire tcl lock??? */ newval = AsObj(newValue); if (newval == NULL) return NULL; @@ -2099,8 +2095,10 @@ SetVar(TkappObject *self, PyObject *args, int flags) CHECK_STRING_LENGTH(name1); CHECK_STRING_LENGTH(name2); - /* XXX must hold tcl lock already??? */ newval = AsObj(newValue); + if (newval == NULL) { + return NULL; + } if (self->trace) { if (flags & TCL_GLOBAL_ONLY) { TRACE(self, ("((sssNO))", "uplevel", "#0", "set", @@ -2585,15 +2583,6 @@ typedef struct { PyObject *func; } PythonCmd_ClientData; -static int -PythonCmd_Error(Tcl_Interp *interp) -{ - errorInCmd = 1; - excInCmd = PyErr_GetRaisedException(); - LEAVE_PYTHON - return TCL_ERROR; -} - /* This is the Tcl command that acts as a wrapper for Python * function or method. */ @@ -2606,19 +2595,20 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int i; Tcl_Obj *obj_res; int objargs = data->self->wantobjects >= 2; + int result = TCL_ERROR; ENTER_PYTHON /* Create argument tuple (objv1, ..., objvN) */ if (!(args = PyTuple_New(objc - 1))) - return PythonCmd_Error(interp); + goto error; for (i = 0; i < (objc - 1); i++) { PyObject *s = objargs ? FromObj(data->self, objv[i + 1]) : unicodeFromTclObj(data->self, objv[i + 1]); if (!s) { Py_DECREF(args); - return PythonCmd_Error(interp); + goto error; } PyTuple_SET_ITEM(args, i, s); } @@ -2627,19 +2617,24 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, Py_DECREF(args); if (res == NULL) - return PythonCmd_Error(interp); + goto error; obj_res = AsObj(res); if (obj_res == NULL) { Py_DECREF(res); - return PythonCmd_Error(interp); + goto error; } Tcl_SetObjResult(interp, obj_res); Py_DECREF(res); + result = TCL_OK; + goto done; +error: + Py_XSETREF(excInCmd, PyErr_GetRaisedException()); +done: LEAVE_PYTHON - return TCL_OK; + return result; } @@ -2709,7 +2704,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name, return NULL; } - if (self->threaded && self->thread_id != Tcl_GetCurrentThread() && + if (self->thread_id != Tcl_GetCurrentThread() && !WaitForMainloop(self)) return NULL; @@ -2723,7 +2718,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name, (gh-80937). The command cannot outlive the interpreter. */ data->self = self; data->func = Py_NewRef(func); - if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { + if (self->thread_id != Tcl_GetCurrentThread()) { err = 0; // init to make static analyzers happy Tcl_Condition cond = NULL; @@ -2781,7 +2776,7 @@ _tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name) TRACE(self, ("((sss))", "rename", name, "")); - if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { + if (self->thread_id != Tcl_GetCurrentThread()) { err = 0; // init to make static analyzers happy Tcl_Condition cond = NULL; @@ -2873,8 +2868,7 @@ FileHandler(ClientData clientData, int mask) res = PyObject_CallFunction(func, "Oi", file, mask); if (res == NULL) { - errorInCmd = 1; - excInCmd = PyErr_GetRaisedException(); + Py_XSETREF(excInCmd, PyErr_GetRaisedException()); } Py_XDECREF(res); LEAVE_PYTHON @@ -3078,8 +3072,7 @@ TimerHandler(ClientData clientData) Py_DECREF(v); /* See Tktt_New() */ if (res == NULL) { - errorInCmd = 1; - excInCmd = PyErr_GetRaisedException(); + Py_XSETREF(excInCmd, PyErr_GetRaisedException()); } else Py_DECREF(res); @@ -3136,50 +3129,38 @@ static PyObject * _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold) /*[clinic end generated code: output=0ba8eabbe57841b0 input=036bcdcf03d5eca0]*/ { - PyThreadState *tstate = PyThreadState_Get(); - CHECK_TCL_APPARTMENT(self); + /* mainloop() can be nested, the outer mainloop() still dispatches + events after the inner one exits. */ + int dispatching = self->dispatching; self->dispatching = 1; - quitMainLoop = 0; + FT_ATOMIC_STORE_INT_RELAXED(self->quitting, 0); while (Tk_GetNumMainWindows() > threshold && - !quitMainLoop && - !errorInCmd) + !FT_ATOMIC_LOAD_INT_RELAXED(self->quitting) && + excInCmd == NULL) { int result; - if (self->threaded) { - /* Allow other Python threads to run. */ - ENTER_TCL - result = Tcl_DoOneEvent(0); - LEAVE_TCL - } - else { - Py_BEGIN_ALLOW_THREADS - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); - tcl_tstate = tstate; - result = Tcl_DoOneEvent(TCL_DONT_WAIT); - tcl_tstate = NULL; - if(tcl_lock)PyThread_release_lock(tcl_lock); - if (result == 0) - Sleep(Tkinter_busywaitinterval); - Py_END_ALLOW_THREADS - } + /* Allow other Python threads to run. */ + ENTER_TCL + result = Tcl_DoOneEvent(0); + LEAVE_TCL if (PyErr_CheckSignals() != 0) { - self->dispatching = 0; + self->dispatching = dispatching; return NULL; } if (result < 0) break; } - self->dispatching = 0; - quitMainLoop = 0; + self->dispatching = dispatching; + FT_ATOMIC_STORE_INT_RELAXED(self->quitting, 0); - if (errorInCmd) { - errorInCmd = 0; - PyErr_SetRaisedException(excInCmd); + PyObject *exc = excInCmd; + if (exc != NULL) { excInCmd = NULL; + PyErr_SetRaisedException(exc); return NULL; } Py_RETURN_NONE; @@ -3205,6 +3186,13 @@ _tkinter_tkapp_dooneevent_impl(TkappObject *self, int flags) return PyLong_FromLong(rv); } +static int +QuitEventProc(Tcl_Event *evPtr, int flags) +{ + /* Nothing to do: the event only wakes up the event loop. */ + return 1; +} + /*[clinic input] _tkinter.tkapp.quit [clinic start generated code]*/ @@ -3213,7 +3201,19 @@ static PyObject * _tkinter_tkapp_quit_impl(TkappObject *self) /*[clinic end generated code: output=7f21eeff481f754f input=e03020dc38aff23c]*/ { - quitMainLoop = 1; + FT_ATOMIC_STORE_INT_RELAXED(self->quitting, 1); + if (self->dispatching && self->thread_id != Tcl_GetCurrentThread()) { + /* mainloop() is running in other thread and can wait for an event. + Send it an event, so that it checks the flag. */ + Tcl_Event *ev = (Tcl_Event *)attemptckalloc(sizeof(Tcl_Event)); + if (ev == NULL) { + return PyErr_NoMemory(); + } + memset(ev, 0, sizeof(Tcl_Event)); + ev->proc = QuitEventProc; + Tcl_ThreadQueueEvent(self->thread_id, ev, TCL_QUEUE_TAIL); + Tcl_ThreadAlert(self->thread_id); + } Py_RETURN_NONE; } @@ -3368,7 +3368,7 @@ Tkapp_Dealloc(PyObject *op) TkappObject *self = (TkappObject *)op; PyTypeObject *tp = Py_TYPE(op); PyObject_GC_UnTrack(op); - if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { + if (self->thread_id != Tcl_GetCurrentThread()) { /* Deleting the interpreter from another thread aborts the process ("Tcl_AsyncDelete: async handler deleted by the wrong thread"). Leak it instead (gh-83274). */ @@ -3386,10 +3386,10 @@ Tkapp_Dealloc(PyObject *op) Tcl_DeleteInterp(Tkapp_Interp(self)); LEAVE_TCL } + DisableEventHook(self); (void)Tkapp_Clear(op); tp->tp_free(op); Py_DECREF(tp); - DisableEventHook(); } @@ -3545,21 +3545,21 @@ _tkinter_create_impl(PyObject *module, const char *screenName, } /*[clinic input] -@permit_long_summary _tkinter.setbusywaitinterval new_val: int / -Set the busy-wait interval in milliseconds between successive calls to Tcl_DoOneEvent in a threaded Python interpreter. +Set the busy-wait interval in milliseconds. -It should be set to a divisor of the maximum time between frames in -an animation. +It is the interval between successive calls to Tcl_DoOneEvent when +waiting for input at the interactive prompt on Windows. It should be +set to a divisor of the maximum time between frames in an animation. [clinic start generated code]*/ static PyObject * _tkinter_setbusywaitinterval_impl(PyObject *module, int new_val) -/*[clinic end generated code: output=42bf7757dc2d0ab6 input=0360dd95c8bd8619]*/ +/*[clinic end generated code: output=42bf7757dc2d0ab6 input=05733d51be719869]*/ { if (new_val < 0) { PyErr_SetString(PyExc_ValueError, @@ -3571,19 +3571,32 @@ _tkinter_setbusywaitinterval_impl(PyObject *module, int new_val) } /*[clinic input] -@permit_long_summary _tkinter.getbusywaitinterval -> int -Return the current busy-wait interval between successive calls to Tcl_DoOneEvent in a threaded Python interpreter. +Return the current busy-wait interval in milliseconds. [clinic start generated code]*/ static int _tkinter_getbusywaitinterval_impl(PyObject *module) -/*[clinic end generated code: output=23b72d552001f5c7 input=62d5b36ddab3976b]*/ +/*[clinic end generated code: output=23b72d552001f5c7 input=cd3d96a258e75ade]*/ { return Tkinter_busywaitinterval; } +/*[clinic input] +_tkinter._finalize_thread + +Clean up per-thread state. +[clinic start generated code]*/ + +static PyObject * +_tkinter__finalize_thread_impl(PyObject *module) +/*[clinic end generated code: output=4ed0d3be4d1f8de8 input=ea68096ee9b83d23]*/ +{ + Tcl_FinalizeThread(); + Py_RETURN_NONE; +} + #include "clinic/_tkinter.c.h" static PyMethodDef Tktt_methods[] = @@ -3681,10 +3694,15 @@ static PyMethodDef moduleMethods[] = _TKINTER_CREATE_METHODDEF _TKINTER_SETBUSYWAITINTERVAL_METHODDEF _TKINTER_GETBUSYWAITINTERVAL_METHODDEF + _TKINTER__FINALIZE_THREAD_METHODDEF {NULL, NULL} }; #ifdef WAIT_FOR_STDIN +/* The number of Tcl interpreters which use the event hook. */ +static Py_ssize_t event_hook_users = 0; +static PyMutex event_hook_mutex = {0}; + #ifndef MS_WINDOWS static int stdin_ready = 0; @@ -3702,8 +3720,6 @@ MyFileProc(void *clientData, int mask) } #endif -static PyThreadState *event_tstate = NULL; - static int EventHook(void) { @@ -3712,10 +3728,18 @@ EventHook(void) DWORD type; #else int tfile; +#endif + /* The hook is called with the GIL released, usually by PyOS_Readline() + in the thread which reads the input. Use the thread state of that + thread, not of the thread which created the Tcl interpreter. */ + PyThreadState *tstate = PyGILState_GetThisThreadState(); + if (tstate == NULL) { + return 0; + } +#ifndef MS_WINDOWS stdin_ready = 0; #endif - PyEval_RestoreThread(event_tstate); - errorInCmd = 0; + PyEval_RestoreThread(tstate); #ifdef MS_WINDOWS hStdin = GetStdHandle(STD_INPUT_HANDLE); type = GetFileType(hStdin); @@ -3743,27 +3767,32 @@ EventHook(void) else if (type == FILE_TYPE_DISK) { break; } -#endif + /* stdin is not a Tcl event source on Windows, so poll. */ Py_BEGIN_ALLOW_THREADS - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); - tcl_tstate = event_tstate; - + tcl_tstate = tstate; result = Tcl_DoOneEvent(TCL_DONT_WAIT); - tcl_tstate = NULL; - if(tcl_lock)PyThread_release_lock(tcl_lock); if (result == 0) Sleep(Tkinter_busywaitinterval); Py_END_ALLOW_THREADS +#else + /* stdin is watched by MyFileProc, so we can wait for an event or + input instead of polling. */ + Py_BEGIN_ALLOW_THREADS + tcl_tstate = tstate; + result = Tcl_DoOneEvent(0); + tcl_tstate = NULL; + Py_END_ALLOW_THREADS +#endif /* Report an exception raised in a callback, but keep pumping events instead of returning to the prompt: without readline there is no input waiting on stdin yet, so returning here would block in fgets until the user hits enter, freezing later callbacks. */ - if (errorInCmd) { - errorInCmd = 0; - PyErr_SetRaisedException(excInCmd); + PyObject *exc = excInCmd; + if (exc != NULL) { excInCmd = NULL; + PyErr_SetRaisedException(exc); PyErr_Print(); } if (result < 0) @@ -3780,23 +3809,32 @@ EventHook(void) #endif static void -EnableEventHook(void) +EnableEventHook(TkappObject *self) { #ifdef WAIT_FOR_STDIN + PyMutex_Lock(&event_hook_mutex); + event_hook_users++; if (PyOS_InputHook == NULL) { - event_tstate = PyThreadState_Get(); PyOS_InputHook = EventHook; } + self->event_hook = 1; + PyMutex_Unlock(&event_hook_mutex); #endif } static void -DisableEventHook(void) +DisableEventHook(TkappObject *self) { #ifdef WAIT_FOR_STDIN - if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { + if (!self->event_hook) { + return; + } + PyMutex_Lock(&event_hook_mutex); + self->event_hook = 0; + if (--event_hook_users == 0 && PyOS_InputHook == EventHook) { PyOS_InputHook = NULL; } + PyMutex_Unlock(&event_hook_mutex); #endif } @@ -3846,10 +3884,6 @@ PyInit__tkinter(void) PyObject *m, *uexe, *cexe; - tcl_lock = PyThread_allocate_lock(); - if (tcl_lock == NULL) - return PyErr_NoMemory(); - m = PyModule_Create(&_tkintermodule); if (m == NULL) return NULL; diff --git a/Modules/_uuidmodule.c b/Modules/_uuidmodule.c index e957071a4ecef9..04bce1bd18901b 100644 --- a/Modules/_uuidmodule.c +++ b/Modules/_uuidmodule.c @@ -187,7 +187,7 @@ typedef struct { // We overfetch entropy to speed up successive uuid generations; // this enables 10x peformance boost. uint8_t random_buf[RANDOM_BUF_SIZE]; - uint64_t random_idx; + Py_ssize_t random_idx; uint64_t random_last_pid; // UUID v7 state diff --git a/Modules/_winapi.c b/Modules/_winapi.c index 3cee151b997dee..51e7523b4bf9e2 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -2824,6 +2824,95 @@ _winapi_GetFileType_impl(PyObject *module, HANDLE handle) return result; } +/*[clinic input] +_winapi.GetConsoleMode -> DWORD + + handle: HANDLE + / + +Return the current mode of a console buffer. + +The handle may refer to a console input buffer or a console screen +buffer. +[clinic start generated code]*/ + +static DWORD +_winapi_GetConsoleMode_impl(PyObject *module, HANDLE handle) +/*[clinic end generated code: output=4c0155d4357673ac input=4768c2370a960135]*/ +{ + DWORD mode; + BOOL ret; + + Py_BEGIN_ALLOW_THREADS + ret = GetConsoleMode(handle, &mode); + Py_END_ALLOW_THREADS + + if (!ret) { + PyErr_SetFromWindowsErr(0); + return PY_DWORD_MAX; + } + + return mode; +} + +/*[clinic input] +_winapi.SetConsoleMode + + handle: HANDLE + mode: DWORD(bitwise=True) + / + +Set the mode of a console buffer. + +The handle may refer to a console input buffer or a console screen +buffer. +[clinic start generated code]*/ + +static PyObject * +_winapi_SetConsoleMode_impl(PyObject *module, HANDLE handle, DWORD mode) +/*[clinic end generated code: output=3351f12c46a8938f input=41ffc3f65998d7e1]*/ +{ + BOOL ret; + + Py_BEGIN_ALLOW_THREADS + ret = SetConsoleMode(handle, mode); + Py_END_ALLOW_THREADS + + if (!ret) { + return PyErr_SetFromWindowsErr(0); + } + + Py_RETURN_NONE; +} + +/*[clinic input] +_winapi.FlushConsoleInputBuffer + + handle: HANDLE + / + +Flush the console input buffer. + +All input records currently in the input buffer are discarded. +[clinic start generated code]*/ + +static PyObject * +_winapi_FlushConsoleInputBuffer_impl(PyObject *module, HANDLE handle) +/*[clinic end generated code: output=2edaf920d25233ab input=9684cbfada20c237]*/ +{ + BOOL ret; + + Py_BEGIN_ALLOW_THREADS + ret = FlushConsoleInputBuffer(handle); + Py_END_ALLOW_THREADS + + if (!ret) { + return PyErr_SetFromWindowsErr(0); + } + + Py_RETURN_NONE; +} + /*[clinic input] _winapi._mimetypes_read_windows_registry @@ -3232,6 +3321,9 @@ static PyMethodDef winapi_functions[] = { _WINAPI_GETACP_METHODDEF _WINAPI_GETOEMCP_METHODDEF _WINAPI_GETFILETYPE_METHODDEF + _WINAPI_GETCONSOLEMODE_METHODDEF + _WINAPI_SETCONSOLEMODE_METHODDEF + _WINAPI_FLUSHCONSOLEINPUTBUFFER_METHODDEF _WINAPI__MIMETYPES_READ_WINDOWS_REGISTRY_METHODDEF _WINAPI_NEEDCURRENTDIRECTORYFOREXEPATH_METHODDEF _WINAPI_COPYFILE2_METHODDEF diff --git a/Modules/_zoneinfo.c b/Modules/_zoneinfo.c index 45f65b9d30b637..d17b3df4dfbbb1 100644 --- a/Modules/_zoneinfo.c +++ b/Modules/_zoneinfo.c @@ -302,6 +302,7 @@ get_weak_cache(zoneinfo_state *state, PyTypeObject *type) } /*[clinic input] +@vectorcall @critical_section @classmethod zoneinfo.ZoneInfo.__new__ @@ -313,7 +314,7 @@ Create a new ZoneInfo instance. static PyObject * zoneinfo_ZoneInfo_impl(PyTypeObject *type, PyObject *key) -/*[clinic end generated code: output=95e61dab86bb95c3 input=ef73d7a83bf8790e]*/ +/*[clinic end generated code: output=95e61dab86bb95c3 input=8cb93934e6a8d11b]*/ { zoneinfo_state *state = zoneinfo_get_state_by_self(type); PyObject *instance = zone_from_strong_cache(state, type, key); @@ -2438,11 +2439,9 @@ find_in_strong_cache(const StrongCacheNode *const root, PyObject *const key) { const StrongCacheNode *node = root; while (node != NULL) { - int rv = PyObject_RichCompareBool(key, node->key, Py_EQ); - if (rv < 0) { - return NULL; - } - if (rv) { + if (key == node->key || + (PyUnicode_Check(key) && PyUnicode_Check(node->key) && + PyUnicode_Equal(key, node->key))) { return (StrongCacheNode *)node; } @@ -2687,6 +2686,7 @@ static PyType_Slot zoneinfo_slots[] = { {Py_tp_methods, zoneinfo_methods}, {Py_tp_members, zoneinfo_members}, {Py_tp_new, zoneinfo_ZoneInfo}, + {Py_tp_vectorcall, zoneinfo_ZoneInfo_vectorcall}, {Py_tp_dealloc, zoneinfo_dealloc}, {Py_tp_traverse, zoneinfo_traverse}, {Py_tp_clear, zoneinfo_clear}, diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c index b835fe670938cb..d572351e80f658 100644 --- a/Modules/atexitmodule.c +++ b/Modules/atexitmodule.c @@ -191,7 +191,8 @@ atexit_register_impl(PyObject *module, PyObject *func, PyObject *args, "the first argument must be callable"); return NULL; } - PyObject *callback = PyTuple_Pack(3, func, args, kwargs); + PyObject *callback = PyTuple_Pack(3, func, args, + kwargs ? kwargs : Py_None); if (callback == NULL) { return NULL; diff --git a/Modules/binascii.c b/Modules/binascii.c index 57684f8336e64e..406234cac1ed23 100644 --- a/Modules/binascii.c +++ b/Modules/binascii.c @@ -2390,10 +2390,8 @@ binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header, Py_ssize_t in, out; char ch; const unsigned char *ascii_data; - unsigned char *odata; unsigned char *stripped = NULL; Py_ssize_t datalen = 0; - PyObject *rv; ascii_data = data->buf; datalen = data->len; @@ -2443,14 +2441,12 @@ binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header, } } - /* We allocate the output same size as input, this is overkill. - */ - odata = (unsigned char *) PyMem_Calloc(1, datalen); - if (odata == NULL) { + PyBytesWriter *writer = PyBytesWriter_Create(datalen); + if (writer == NULL) { PyMem_Free(stripped); - PyErr_NoMemory(); return NULL; } + unsigned char *odata = PyBytesWriter_GetData(writer); in = out = 0; while (in < datalen) { @@ -2492,10 +2488,8 @@ binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header, out++; } } - rv = PyBytes_FromStringAndSize((char *)odata, out); - PyMem_Free(odata); PyMem_Free(stripped); - return rv; + return PyBytesWriter_FinishWithSize(writer, out); } static int @@ -2535,9 +2529,7 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs, { Py_ssize_t in, out; const unsigned char *databuf; - unsigned char *odata; Py_ssize_t datalen = 0, odatalen = 0; - PyObject *rv; unsigned int linelen = 0; unsigned char ch; int crlf = 0; @@ -2622,13 +2614,11 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs, odatalen += delta; } - /* We allocate the output same size as input, this is overkill. - */ - odata = (unsigned char *) PyMem_Calloc(1, odatalen); - if (odata == NULL) { - PyErr_NoMemory(); + PyBytesWriter *writer = PyBytesWriter_Create(odatalen); + if (writer == NULL) { return NULL; } + unsigned char *odata = PyBytesWriter_GetData(writer); in = out = linelen = 0; while (in < datalen) { @@ -2698,9 +2688,7 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs, } } } - rv = PyBytes_FromStringAndSize((char *)odata, out); - PyMem_Free(odata); - return rv; + return PyBytesWriter_FinishWithSize(writer, out); } /* List of functions defined in the module */ diff --git a/Modules/clinic/_abc.c.h b/Modules/clinic/_abc.c.h index fa1c57dc26bf85..8fdce59468e530 100644 --- a/Modules/clinic/_abc.c.h +++ b/Modules/clinic/_abc.c.h @@ -140,6 +140,18 @@ _abc__abc_subclasscheck(PyObject *module, PyObject *const *args, Py_ssize_t narg return return_value; } +PyDoc_STRVAR(_abc__set_subclasscheck__doc__, +"_set_subclasscheck($module, func, /)\n" +"--\n" +"\n" +"Internal ABC helper to register ABCMeta.__subclasscheck__.\n" +"\n" +"Classes whose metaclass resolves __subclasscheck__ to func are checked\n" +"natively when walking the subclass tree of an ABC."); + +#define _ABC__SET_SUBCLASSCHECK_METHODDEF \ + {"_set_subclasscheck", (PyCFunction)_abc__set_subclasscheck, METH_O, _abc__set_subclasscheck__doc__}, + PyDoc_STRVAR(_abc_get_cache_token__doc__, "get_cache_token($module, /)\n" "--\n" @@ -161,4 +173,4 @@ _abc_get_cache_token(PyObject *module, PyObject *Py_UNUSED(ignored)) { return _abc_get_cache_token_impl(module); } -/*[clinic end generated code: output=b05d599656aeb1e1 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=5d71c05c399149c5 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_asynciomodule.c.h b/Modules/clinic/_asynciomodule.c.h index 7ffddc20ea3153..c44e9f01bfcbc6 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -4,10 +4,10 @@ preserve #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) # include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() #endif #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() +#include "pycore_runtime.h" // _Py_SINGLETON() PyDoc_STRVAR(_asyncio_Future___init____doc__, "Future(*, loop=None)\n" @@ -30,7 +30,8 @@ static int _asyncio_Future___init___impl(FutureObj *self, PyObject *loop); static int -_asyncio_Future___init__(PyObject *self, PyObject *args, PyObject *kwargs) +_asyncio_Future___init___helper(PyObject *self, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { int return_value = -1; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -62,11 +63,10 @@ _asyncio_Future___init__(PyObject *self, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[1]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; + Py_ssize_t noptargs = nargs + nkw - 0; PyObject *loop = Py_None; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -82,6 +82,42 @@ _asyncio_Future___init__(PyObject *self, PyObject *args, PyObject *kwargs) return return_value; } +static int +_asyncio_Future___init__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + return _asyncio_Future___init___helper(self, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +_asyncio_Future_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *self; + int _result; + + assert(_PyType_CAST(type)->tp_init == _asyncio_Future___init__); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + return NULL; + } + _result = _asyncio_Future___init___helper(self, args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + if (_result != 0) { + Py_DECREF(self); + return NULL; + } + return self; +} + PyDoc_STRVAR(_asyncio_Future_result__doc__, "result($self, /)\n" "--\n" @@ -755,7 +791,8 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, int eager_start); static int -_asyncio_Task___init__(PyObject *self, PyObject *args, PyObject *kwargs) +_asyncio_Task___init___helper(PyObject *self, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { int return_value = -1; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -787,15 +824,14 @@ _asyncio_Task___init__(PyObject *self, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[5]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *coro; PyObject *loop = Py_None; PyObject *name = Py_None; PyObject *context = Py_None; int eager_start = 0; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -833,6 +869,42 @@ _asyncio_Task___init__(PyObject *self, PyObject *args, PyObject *kwargs) return return_value; } +static int +_asyncio_Task___init__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + return _asyncio_Task___init___helper(self, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +_asyncio_Task_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *self; + int _result; + + assert(_PyType_CAST(type)->tp_init == _asyncio_Task___init__); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + return NULL; + } + _result = _asyncio_Task___init___helper(self, args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + if (_result != 0) { + Py_DECREF(self); + return NULL; + } + return self; +} + static int _asyncio_Task__log_destroy_pending_get_impl(TaskObj *self); @@ -2076,6 +2148,24 @@ _asyncio_future_discard_from_awaited_by(PyObject *module, PyObject *const *args, exit: return return_value; } + +PyDoc_STRVAR(_asyncio__clear_caches__doc__, +"_clear_caches($module, /)\n" +"--\n" +"\n" +"Clear internal caches. Used for internal tests."); + +#define _ASYNCIO__CLEAR_CACHES_METHODDEF \ + {"_clear_caches", (PyCFunction)_asyncio__clear_caches, METH_NOARGS, _asyncio__clear_caches__doc__}, + +static PyObject * +_asyncio__clear_caches_impl(PyObject *module); + +static PyObject * +_asyncio__clear_caches(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _asyncio__clear_caches_impl(module); +} #define _ASYNCIO_FUTURE__ASYNCIO_AWAITED_BY_GETSETDEF {"_asyncio_awaited_by", (getter)_asyncio_Future__asyncio_awaited_by_get, (setter)NULL, NULL}, #define _ASYNCIO_FUTURE__ASYNCIO_FUTURE_BLOCKING_GETSETDEF {"_asyncio_future_blocking", (getter)_asyncio_Future__asyncio_future_blocking_get, (setter)_asyncio_Future__asyncio_future_blocking_set, NULL}, @@ -2104,4 +2194,4 @@ _asyncio_future_discard_from_awaited_by(PyObject *module, PyObject *const *args, #define _ASYNCIO_TASK__FUT_WAITER_GETSETDEF {"_fut_waiter", (getter)_asyncio_Task__fut_waiter_get, (setter)NULL, NULL}, -/*[clinic end generated code: output=419f6d2b73047616 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=32aeff4f33bb2087 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_csv.c.h b/Modules/clinic/_csv.c.h index df3bdbadf29aed..09dbcbea7e057b 100644 --- a/Modules/clinic/_csv.c.h +++ b/Modules/clinic/_csv.c.h @@ -70,15 +70,7 @@ _csv_Dialect___replace__(PyObject *self, PyObject *args, PyObject *kwargs) if (!_PyArg_NoPositional("__replace__", args)) { goto exit; } - if (kwargs == NULL) { - changes = PyDict_New(); - if (changes == NULL) { - goto exit; - } - } - else { - changes = Py_NewRef(kwargs); - } + changes = Py_XNewRef(kwargs); return_value = _csv_Dialect___replace___impl((DialectObj *)self, changes); exit: @@ -142,15 +134,7 @@ _csv_reader(PyObject *module, PyObject *args, PyObject *kwargs) } dialect = PyTuple_GET_ITEM(args, 1); skip_optional: - if (kwargs == NULL) { - fmtparams = PyDict_New(); - if (fmtparams == NULL) { - goto exit; - } - } - else { - fmtparams = Py_NewRef(kwargs); - } + fmtparams = Py_XNewRef(kwargs); return_value = _csv_reader_impl(module, iterable, dialect, fmtparams); exit: @@ -195,15 +179,7 @@ _csv_writer(PyObject *module, PyObject *args, PyObject *kwargs) } dialect = PyTuple_GET_ITEM(args, 1); skip_optional: - if (kwargs == NULL) { - fmtparams = PyDict_New(); - if (fmtparams == NULL) { - goto exit; - } - } - else { - fmtparams = Py_NewRef(kwargs); - } + fmtparams = Py_XNewRef(kwargs); return_value = _csv_writer_impl(module, output_file, dialect, fmtparams); exit: @@ -266,15 +242,7 @@ _csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs) } dialect_obj = PyTuple_GET_ITEM(args, 1); skip_optional: - if (kwargs == NULL) { - fmtparams = PyDict_New(); - if (fmtparams == NULL) { - goto exit; - } - } - else { - fmtparams = Py_NewRef(kwargs); - } + fmtparams = Py_XNewRef(kwargs); return_value = _csv_register_dialect_impl(module, name_obj, dialect_obj, fmtparams); exit: @@ -465,4 +433,4 @@ _csv_field_size_limit(PyObject *module, PyObject *const *args, Py_ssize_t nargs, exit: return return_value; } -/*[clinic end generated code: output=7e3a92d524d88023 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=30eac1c04ff9d38a input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h index 553843ab96a7af..ac9a4de4c960c8 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -520,7 +520,7 @@ _curses_window_attrset(PyObject *self, PyObject *arg) return return_value; } -#if defined(HAVE_CURSES_WATTR_GET) +#if defined(_Py_HAVE_CURSES_WATTR_GET) PyDoc_STRVAR(_curses_window_attr_get__doc__, "attr_get($self, /)\n" @@ -540,9 +540,9 @@ _curses_window_attr_get(PyObject *self, PyObject *Py_UNUSED(ignored)) return _curses_window_attr_get_impl((PyCursesWindowObject *)self); } -#endif /* defined(HAVE_CURSES_WATTR_GET) */ +#endif /* defined(_Py_HAVE_CURSES_WATTR_GET) */ -#if defined(HAVE_CURSES_WATTR_SET) +#if defined(_Py_HAVE_CURSES_WATTR_SET) PyDoc_STRVAR(_curses_window_attr_set__doc__, "attr_set($self, attr, pair=0, /)\n" @@ -583,9 +583,9 @@ _curses_window_attr_set(PyObject *self, PyObject *const *args, Py_ssize_t nargs) return return_value; } -#endif /* defined(HAVE_CURSES_WATTR_SET) */ +#endif /* defined(_Py_HAVE_CURSES_WATTR_SET) */ -#if defined(HAVE_CURSES_WATTR_ON) +#if defined(_Py_HAVE_CURSES_WATTR_ON) PyDoc_STRVAR(_curses_window_attr_on__doc__, "attr_on($self, attr, /)\n" @@ -614,9 +614,9 @@ _curses_window_attr_on(PyObject *self, PyObject *arg) return return_value; } -#endif /* defined(HAVE_CURSES_WATTR_ON) */ +#endif /* defined(_Py_HAVE_CURSES_WATTR_ON) */ -#if defined(HAVE_CURSES_WATTR_OFF) +#if defined(_Py_HAVE_CURSES_WATTR_OFF) PyDoc_STRVAR(_curses_window_attr_off__doc__, "attr_off($self, attr, /)\n" @@ -645,9 +645,9 @@ _curses_window_attr_off(PyObject *self, PyObject *arg) return return_value; } -#endif /* defined(HAVE_CURSES_WATTR_OFF) */ +#endif /* defined(_Py_HAVE_CURSES_WATTR_OFF) */ -#if defined(HAVE_CURSES_WCOLOR_SET) +#if defined(_Py_HAVE_CURSES_WCOLOR_SET) PyDoc_STRVAR(_curses_window_color_set__doc__, "color_set($self, pair, /)\n" @@ -676,7 +676,7 @@ _curses_window_color_set(PyObject *self, PyObject *arg) return return_value; } -#endif /* defined(HAVE_CURSES_WCOLOR_SET) */ +#endif /* defined(_Py_HAVE_CURSES_WCOLOR_SET) */ PyDoc_STRVAR(_curses_window_getattrs__doc__, "getattrs($self, /)\n" @@ -2867,7 +2867,7 @@ _curses_filter(PyObject *module, PyObject *Py_UNUSED(ignored)) #endif /* defined(HAVE_CURSES_FILTER) */ -#if defined(HAVE_CURSES_NOFILTER) +#if defined(_Py_HAVE_CURSES_NOFILTER) PyDoc_STRVAR(_curses_nofilter__doc__, "nofilter($module, /)\n" @@ -2891,7 +2891,7 @@ _curses_nofilter(PyObject *module, PyObject *Py_UNUSED(ignored)) return _curses_nofilter_impl(module); } -#endif /* defined(HAVE_CURSES_NOFILTER) */ +#endif /* defined(_Py_HAVE_CURSES_NOFILTER) */ PyDoc_STRVAR(_curses_baudrate__doc__, "baudrate($module, /)\n" @@ -3566,7 +3566,7 @@ PyDoc_STRVAR(_curses_getwin__doc__, #define _CURSES_GETWIN_METHODDEF \ {"getwin", (PyCFunction)_curses_getwin, METH_O, _curses_getwin__doc__}, -#if defined(HAVE_CURSES_SCR_DUMP) +#if defined(_Py_HAVE_CURSES_SCR_DUMP) PyDoc_STRVAR(_curses_scr_dump__doc__, "scr_dump($module, filename, /)\n" @@ -3583,9 +3583,9 @@ PyDoc_STRVAR(_curses_scr_dump__doc__, #define _CURSES_SCR_DUMP_METHODDEF \ {"scr_dump", (PyCFunction)_curses_scr_dump, METH_O, _curses_scr_dump__doc__}, -#endif /* defined(HAVE_CURSES_SCR_DUMP) */ +#endif /* defined(_Py_HAVE_CURSES_SCR_DUMP) */ -#if defined(HAVE_CURSES_SCR_DUMP) +#if defined(_Py_HAVE_CURSES_SCR_DUMP) PyDoc_STRVAR(_curses_scr_restore__doc__, "scr_restore($module, filename, /)\n" @@ -3602,9 +3602,9 @@ PyDoc_STRVAR(_curses_scr_restore__doc__, #define _CURSES_SCR_RESTORE_METHODDEF \ {"scr_restore", (PyCFunction)_curses_scr_restore, METH_O, _curses_scr_restore__doc__}, -#endif /* defined(HAVE_CURSES_SCR_DUMP) */ +#endif /* defined(_Py_HAVE_CURSES_SCR_DUMP) */ -#if defined(HAVE_CURSES_SCR_DUMP) +#if defined(_Py_HAVE_CURSES_SCR_DUMP) PyDoc_STRVAR(_curses_scr_init__doc__, "scr_init($module, filename, /)\n" @@ -3621,9 +3621,9 @@ PyDoc_STRVAR(_curses_scr_init__doc__, #define _CURSES_SCR_INIT_METHODDEF \ {"scr_init", (PyCFunction)_curses_scr_init, METH_O, _curses_scr_init__doc__}, -#endif /* defined(HAVE_CURSES_SCR_DUMP) */ +#endif /* defined(_Py_HAVE_CURSES_SCR_DUMP) */ -#if defined(HAVE_CURSES_SCR_DUMP) && defined(HAVE_CURSES_SCR_SET) +#if defined(_Py_HAVE_CURSES_SCR_DUMP) && defined(_Py_HAVE_CURSES_SCR_SET) PyDoc_STRVAR(_curses_scr_set__doc__, "scr_set($module, filename, /)\n" @@ -3639,7 +3639,7 @@ PyDoc_STRVAR(_curses_scr_set__doc__, #define _CURSES_SCR_SET_METHODDEF \ {"scr_set", (PyCFunction)_curses_scr_set, METH_O, _curses_scr_set__doc__}, -#endif /* defined(HAVE_CURSES_SCR_DUMP) && defined(HAVE_CURSES_SCR_SET) */ +#endif /* defined(_Py_HAVE_CURSES_SCR_DUMP) && defined(_Py_HAVE_CURSES_SCR_SET) */ PyDoc_STRVAR(_curses_halfdelay__doc__, "halfdelay($module, tenths, /)\n" @@ -3778,7 +3778,7 @@ _curses_has_key(PyObject *module, PyObject *arg) #endif /* defined(HAVE_CURSES_HAS_KEY) */ -#if defined(HAVE_CURSES_DEFINE_KEY) +#if defined(_Py_HAVE_CURSES_DEFINE_KEY) PyDoc_STRVAR(_curses_define_key__doc__, "define_key($module, definition, keycode, /)\n" @@ -3834,9 +3834,9 @@ _curses_define_key(PyObject *module, PyObject *const *args, Py_ssize_t nargs) return return_value; } -#endif /* defined(HAVE_CURSES_DEFINE_KEY) */ +#endif /* defined(_Py_HAVE_CURSES_DEFINE_KEY) */ -#if defined(HAVE_CURSES_KEY_DEFINED) +#if defined(_Py_HAVE_CURSES_KEY_DEFINED) PyDoc_STRVAR(_curses_key_defined__doc__, "key_defined($module, definition, /)\n" @@ -3876,9 +3876,9 @@ _curses_key_defined(PyObject *module, PyObject *arg) return return_value; } -#endif /* defined(HAVE_CURSES_KEY_DEFINED) */ +#endif /* defined(_Py_HAVE_CURSES_KEY_DEFINED) */ -#if defined(HAVE_CURSES_KEYOK) +#if defined(_Py_HAVE_CURSES_KEYOK) PyDoc_STRVAR(_curses_keyok__doc__, "keyok($module, keycode, enable, /)\n" @@ -3921,7 +3921,7 @@ _curses_keyok(PyObject *module, PyObject *const *args, Py_ssize_t nargs) return return_value; } -#endif /* defined(HAVE_CURSES_KEYOK) */ +#endif /* defined(_Py_HAVE_CURSES_KEYOK) */ PyDoc_STRVAR(_curses_init_color__doc__, "init_color($module, color_number, r, g, b, /)\n" @@ -4364,7 +4364,7 @@ PyDoc_STRVAR(_curses_set_term__doc__, #define _CURSES_SET_TERM_METHODDEF \ {"set_term", (PyCFunction)_curses_set_term, METH_O, _curses_set_term__doc__}, -#if defined(HAVE_CURSES_NEW_PRESCR) +#if defined(_Py_HAVE_CURSES_NEW_PRESCR) PyDoc_STRVAR(_curses_new_prescr__doc__, "new_prescr($module, /)\n" @@ -4387,7 +4387,7 @@ _curses_new_prescr(PyObject *module, PyObject *Py_UNUSED(ignored)) return _curses_new_prescr_impl(module); } -#endif /* defined(HAVE_CURSES_NEW_PRESCR) */ +#endif /* defined(_Py_HAVE_CURSES_NEW_PRESCR) */ #if defined(HAVE_CURSES_ESCDELAY) @@ -4731,7 +4731,7 @@ _curses_meta(PyObject *module, PyObject *arg) return return_value; } -#if (defined(HAVE_CURSES_GETMOUSE) || defined(PDCURSES)) && (defined(HAVE_CURSES_HAS_MOUSE) || defined(PDCURSES)) +#if (defined(HAVE_CURSES_GETMOUSE) || defined(PDCURSES)) && (defined(_Py_HAVE_CURSES_HAS_MOUSE) || defined(PDCURSES)) PyDoc_STRVAR(_curses_has_mouse__doc__, "has_mouse($module, /)\n" @@ -4751,7 +4751,7 @@ _curses_has_mouse(PyObject *module, PyObject *Py_UNUSED(ignored)) return _curses_has_mouse_impl(module); } -#endif /* (defined(HAVE_CURSES_GETMOUSE) || defined(PDCURSES)) && (defined(HAVE_CURSES_HAS_MOUSE) || defined(PDCURSES)) */ +#endif /* (defined(HAVE_CURSES_GETMOUSE) || defined(PDCURSES)) && (defined(_Py_HAVE_CURSES_HAS_MOUSE) || defined(PDCURSES)) */ #if (defined(HAVE_CURSES_GETMOUSE) || defined(PDCURSES)) @@ -5645,7 +5645,7 @@ _curses_termattrs(PyObject *module, PyObject *Py_UNUSED(ignored)) return _curses_termattrs_impl(module); } -#if defined(HAVE_CURSES_TERM_ATTRS) +#if defined(_Py_HAVE_CURSES_TERM_ATTRS) PyDoc_STRVAR(_curses_term_attrs__doc__, "term_attrs($module, /)\n" @@ -5668,7 +5668,7 @@ _curses_term_attrs(PyObject *module, PyObject *Py_UNUSED(ignored)) return _curses_term_attrs_impl(module); } -#endif /* defined(HAVE_CURSES_TERM_ATTRS) */ +#endif /* defined(_Py_HAVE_CURSES_TERM_ATTRS) */ PyDoc_STRVAR(_curses_termname__doc__, "termname($module, /)\n" @@ -5803,44 +5803,46 @@ _curses_tigetstr(PyObject *module, PyObject *arg) } PyDoc_STRVAR(_curses_tparm__doc__, -"tparm($module, str, i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0, i8=0,\n" -" i9=0, /)\n" +"tparm($module, str, /, *params)\n" "--\n" "\n" "Instantiate the specified byte string with the supplied parameters.\n" "\n" " str\n" -" Parameterized byte string obtained from the terminfo database."); +" Parameterized byte string obtained from the terminfo database.\n" +" params\n" +" Up to nine parameters.\n" +"\n" +"The parameters are integers, except in a few cases where they must be\n" +"bytes objects."); #define _CURSES_TPARM_METHODDEF \ {"tparm", _PyCFunction_CAST(_curses_tparm), METH_FASTCALL, _curses_tparm__doc__}, static PyObject * -_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3, - int i4, int i5, int i6, int i7, int i8, int i9); +_curses_tparm_impl(PyObject *module, PyObject *string, PyObject *params); static PyObject * _curses_tparm(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; - const char *str; - int i1 = 0; - int i2 = 0; - int i3 = 0; - int i4 = 0; - int i5 = 0; - int i6 = 0; - int i7 = 0; - int i8 = 0; - int i9 = 0; + PyObject *string; + PyObject *params = NULL; - if (!PyArg_ParseArray(args, nargs, "y|iiiiiiiii:tparm", - &str, &i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8, &i9)) { + if (!_PyArg_CheckPositional("tparm", nargs, 1, PY_SSIZE_T_MAX)) { + goto exit; + } + string = args[0]; + params = PyTuple_FromArray(args + 1, nargs - 1); + if (params == NULL) { goto exit; } - return_value = _curses_tparm_impl(module, str, i1, i2, i3, i4, i5, i6, i7, i8, i9); + return_value = _curses_tparm_impl(module, string, params); exit: + /* Cleanup for params */ + Py_XDECREF(params); + return return_value; } @@ -6244,7 +6246,7 @@ _curses_slk_attr(PyObject *module, PyObject *Py_UNUSED(ignored)) #endif /* (defined(NCURSES_EXT_FUNCS) || defined(PDCURSES)) */ -#if defined(HAVE_CURSES_SLK_ATTR_ON) +#if defined(_Py_HAVE_CURSES_SLK_ATTR_ON) PyDoc_STRVAR(_curses_slk_attr_on__doc__, "slk_attr_on($module, attr, /)\n" @@ -6273,9 +6275,9 @@ _curses_slk_attr_on(PyObject *module, PyObject *arg) return return_value; } -#endif /* defined(HAVE_CURSES_SLK_ATTR_ON) */ +#endif /* defined(_Py_HAVE_CURSES_SLK_ATTR_ON) */ -#if defined(HAVE_CURSES_SLK_ATTR_OFF) +#if defined(_Py_HAVE_CURSES_SLK_ATTR_OFF) PyDoc_STRVAR(_curses_slk_attr_off__doc__, "slk_attr_off($module, attr, /)\n" @@ -6304,9 +6306,9 @@ _curses_slk_attr_off(PyObject *module, PyObject *arg) return return_value; } -#endif /* defined(HAVE_CURSES_SLK_ATTR_OFF) */ +#endif /* defined(_Py_HAVE_CURSES_SLK_ATTR_OFF) */ -#if defined(HAVE_CURSES_SLK_ATTR_SET) +#if defined(_Py_HAVE_CURSES_SLK_ATTR_SET) PyDoc_STRVAR(_curses_slk_attr_set__doc__, "slk_attr_set($module, attr, pair=0, /)\n" @@ -6346,9 +6348,9 @@ _curses_slk_attr_set(PyObject *module, PyObject *const *args, Py_ssize_t nargs) return return_value; } -#endif /* defined(HAVE_CURSES_SLK_ATTR_SET) */ +#endif /* defined(_Py_HAVE_CURSES_SLK_ATTR_SET) */ -#if defined(HAVE_CURSES_SLK_COLOR) +#if defined(_Py_HAVE_CURSES_SLK_COLOR) PyDoc_STRVAR(_curses_slk_color__doc__, "slk_color($module, pair, /)\n" @@ -6377,7 +6379,7 @@ _curses_slk_color(PyObject *module, PyObject *arg) return return_value; } -#endif /* defined(HAVE_CURSES_SLK_COLOR) */ +#endif /* defined(_Py_HAVE_CURSES_SLK_COLOR) */ #if defined(HAVE_CURSES_USE_ENV) @@ -6715,4 +6717,4 @@ _curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored #ifndef _CURSES_ASSUME_DEFAULT_COLORS_METHODDEF #define _CURSES_ASSUME_DEFAULT_COLORS_METHODDEF #endif /* !defined(_CURSES_ASSUME_DEFAULT_COLORS_METHODDEF) */ -/*[clinic end generated code: output=422461e62cb86010 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=2e47ca767f378409 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_elementtree.c.h b/Modules/clinic/_elementtree.c.h index 9b0494f35fae9a..afc9daa5288f2c 100644 --- a/Modules/clinic/_elementtree.c.h +++ b/Modules/clinic/_elementtree.c.h @@ -51,15 +51,7 @@ _elementtree_SubElement(PyObject *module, PyObject *args, PyObject *kwargs) } attrib = PyTuple_GET_ITEM(args, 2); skip_optional: - if (kwargs == NULL) { - extra = PyDict_New(); - if (extra == NULL) { - goto exit; - } - } - else { - extra = Py_NewRef(kwargs); - } + extra = Py_XNewRef(kwargs); return_value = _elementtree_SubElement_impl(module, parent, tag, attrib, extra); exit: @@ -1482,4 +1474,4 @@ _elementtree_XMLParser__setevents(PyObject *self, PyObject *const *args, Py_ssiz exit: return return_value; } -/*[clinic end generated code: output=01693c4bcc6c63b2 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d454d27e90eb399e input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_interpretersmodule.c.h b/Modules/clinic/_interpretersmodule.c.h index f297280279c5cd..4d087b152d2b7f 100644 --- a/Modules/clinic/_interpretersmodule.c.h +++ b/Modules/clinic/_interpretersmodule.c.h @@ -50,15 +50,7 @@ _interpreters_new_config(PyObject *module, PyObject *args, PyObject *kwargs) goto exit; } skip_optional: - if (kwargs == NULL) { - overrides = PyDict_New(); - if (overrides == NULL) { - goto exit; - } - } - else { - overrides = Py_NewRef(kwargs); - } + overrides = Py_XNewRef(kwargs); return_value = _interpreters_new_config_impl(module, name, overrides); exit: @@ -1260,4 +1252,4 @@ _interpreters_capture_exception(PyObject *module, PyObject *const *args, Py_ssiz exit: return return_value; } -/*[clinic end generated code: output=fa37872d6ee729ef input=a9049054013a1b77]*/ +/*[clinic end generated code: output=ad79d44c2b139a83 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_queuemodule.c.h b/Modules/clinic/_queuemodule.c.h index b67dd23f260c9e..2ec21ce72d4c5a 100644 --- a/Modules/clinic/_queuemodule.c.h +++ b/Modules/clinic/_queuemodule.c.h @@ -38,6 +38,27 @@ simplequeue_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +simplequeue_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == simplequeue_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoPositionalStack("SimpleQueue", nargs) || + !_PyArg_NoKwnames("SimpleQueue", kwnames)) { + goto exit; + } + return_value = simplequeue_new_impl(_PyType_CAST(type)); + +exit: + return return_value; +} + PyDoc_STRVAR(_queue_SimpleQueue_put__doc__, "put($self, /, item, block=True, timeout=None)\n" "--\n" @@ -390,4 +411,4 @@ _queue_SimpleQueue___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored)) exit: return return_value; } -/*[clinic end generated code: output=8219fe2f2ed5f068 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=ab7862d3ec0c03c4 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_testclinic.c.h b/Modules/clinic/_testclinic.c.h index dcc60c7341cbc3..4527ae213f021e 100644 --- a/Modules/clinic/_testclinic.c.h +++ b/Modules/clinic/_testclinic.c.h @@ -5309,4 +5309,73 @@ vc_kwonly_vectorcall(PyObject *type, PyObject *const *args, kwnames ? PyTuple_GET_SIZE(kwnames) : 0, NULL, kwnames); } -/*[clinic end generated code: output=b54b37db716469cd input=a9049054013a1b77]*/ + +static PyObject * +vc_kwds_new_impl(PyTypeObject *type, PyObject *a, PyObject *b, + PyObject *kwds); + +static PyObject * +vc_kwds_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyObject *a; + PyObject *b = Py_None; + PyObject *__clinic_kwds = NULL; + + if (!_PyArg_CheckPositional("VcKwds", PyTuple_GET_SIZE(args), 1, 2)) { + goto exit; + } + a = PyTuple_GET_ITEM(args, 0); + if (PyTuple_GET_SIZE(args) < 2) { + goto skip_optional; + } + b = PyTuple_GET_ITEM(args, 1); +skip_optional: + __clinic_kwds = Py_XNewRef(kwargs); + return_value = vc_kwds_new_impl(type, a, b, __clinic_kwds); + +exit: + /* Cleanup for kwds */ + Py_XDECREF(__clinic_kwds); + + return return_value; +} + +static PyObject * +vc_kwds_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *a; + PyObject *b = Py_None; + PyObject *__clinic_kwds = NULL; + + assert(_PyType_CAST(type)->tp_new == vc_kwds_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_CheckPositional("VcKwds", nargs, 1, 2)) { + goto exit; + } + a = args[0]; + if (nargs < 2) { + goto skip_optional; + } + b = args[1]; +skip_optional: + if (kwnames != NULL) { + __clinic_kwds = _PyStack_AsDict(args + nargs, kwnames); + if (__clinic_kwds == NULL) { + goto exit; + } + } + return_value = vc_kwds_new_impl(_PyType_CAST(type), a, b, __clinic_kwds); + +exit: + /* Cleanup for kwds */ + Py_XDECREF(__clinic_kwds); + + return return_value; +} +/*[clinic end generated code: output=2e36c6f796a4ca09 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_testclinic_kwds.c.h b/Modules/clinic/_testclinic_kwds.c.h index faae3f3d0c2658..729ba1b6fa7c3d 100644 --- a/Modules/clinic/_testclinic_kwds.c.h +++ b/Modules/clinic/_testclinic_kwds.c.h @@ -33,15 +33,7 @@ lone_kwds(PyObject *module, PyObject *args, PyObject *kwargs) if (!_PyArg_NoPositional("lone_kwds", args)) { goto exit; } - if (kwargs == NULL) { - __clinic_kwds = PyDict_New(); - if (__clinic_kwds == NULL) { - goto exit; - } - } - else { - __clinic_kwds = Py_NewRef(kwargs); - } + __clinic_kwds = Py_XNewRef(kwargs); return_value = lone_kwds_impl(module, __clinic_kwds); exit: @@ -76,15 +68,7 @@ kwds_with_pos_only(PyObject *module, PyObject *args, PyObject *kwargs) } a = PyTuple_GET_ITEM(args, 0); b = PyTuple_GET_ITEM(args, 1); - if (kwargs == NULL) { - __clinic_kwds = PyDict_New(); - if (__clinic_kwds == NULL) { - goto exit; - } - } - else { - __clinic_kwds = Py_NewRef(kwargs); - } + __clinic_kwds = Py_XNewRef(kwargs); return_value = kwds_with_pos_only_impl(module, a, b, __clinic_kwds); exit: @@ -123,15 +107,7 @@ kwds_with_optional_pos_only(PyObject *module, PyObject *args, PyObject *kwargs) } b = PyTuple_GET_ITEM(args, 1); skip_optional: - if (kwargs == NULL) { - __clinic_kwds = PyDict_New(); - if (__clinic_kwds == NULL) { - goto exit; - } - } - else { - __clinic_kwds = Py_NewRef(kwargs); - } + __clinic_kwds = Py_XNewRef(kwargs); return_value = kwds_with_optional_pos_only_impl(module, a, b, __clinic_kwds); exit: @@ -160,18 +136,9 @@ kwds_with_stararg(PyObject *module, PyObject *args, PyObject *kwargs) PyObject *__clinic_kwds = NULL; __clinic_args = Py_NewRef(args); - if (kwargs == NULL) { - __clinic_kwds = PyDict_New(); - if (__clinic_kwds == NULL) { - goto exit; - } - } - else { - __clinic_kwds = Py_NewRef(kwargs); - } + __clinic_kwds = Py_XNewRef(kwargs); return_value = kwds_with_stararg_impl(module, __clinic_args, __clinic_kwds); -exit: /* Cleanup for args */ Py_XDECREF(__clinic_args); /* Cleanup for kwds */ @@ -211,15 +178,7 @@ kwds_with_pos_only_and_stararg(PyObject *module, PyObject *args, PyObject *kwarg if (!__clinic_args) { goto exit; } - if (kwargs == NULL) { - __clinic_kwds = PyDict_New(); - if (__clinic_kwds == NULL) { - goto exit; - } - } - else { - __clinic_kwds = Py_NewRef(kwargs); - } + __clinic_kwds = Py_XNewRef(kwargs); return_value = kwds_with_pos_only_and_stararg_impl(module, a, b, __clinic_args, __clinic_kwds); exit: @@ -230,4 +189,4 @@ kwds_with_pos_only_and_stararg(PyObject *module, PyObject *args, PyObject *kwarg return return_value; } -/*[clinic end generated code: output=27a7ba9440b9a884 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=ad46500147228faf input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_tkinter.c.h b/Modules/clinic/_tkinter.c.h index 1e197167409a03..6994ebdebaf480 100644 --- a/Modules/clinic/_tkinter.c.h +++ b/Modules/clinic/_tkinter.c.h @@ -901,10 +901,11 @@ PyDoc_STRVAR(_tkinter_setbusywaitinterval__doc__, "setbusywaitinterval($module, new_val, /)\n" "--\n" "\n" -"Set the busy-wait interval in milliseconds between successive calls to Tcl_DoOneEvent in a threaded Python interpreter.\n" +"Set the busy-wait interval in milliseconds.\n" "\n" -"It should be set to a divisor of the maximum time between frames in\n" -"an animation."); +"It is the interval between successive calls to Tcl_DoOneEvent when\n" +"waiting for input at the interactive prompt on Windows. It should be\n" +"set to a divisor of the maximum time between frames in an animation."); #define _TKINTER_SETBUSYWAITINTERVAL_METHODDEF \ {"setbusywaitinterval", (PyCFunction)_tkinter_setbusywaitinterval, METH_O, _tkinter_setbusywaitinterval__doc__}, @@ -932,7 +933,7 @@ PyDoc_STRVAR(_tkinter_getbusywaitinterval__doc__, "getbusywaitinterval($module, /)\n" "--\n" "\n" -"Return the current busy-wait interval between successive calls to Tcl_DoOneEvent in a threaded Python interpreter."); +"Return the current busy-wait interval in milliseconds."); #define _TKINTER_GETBUSYWAITINTERVAL_METHODDEF \ {"getbusywaitinterval", (PyCFunction)_tkinter_getbusywaitinterval, METH_NOARGS, _tkinter_getbusywaitinterval__doc__}, @@ -956,6 +957,24 @@ _tkinter_getbusywaitinterval(PyObject *module, PyObject *Py_UNUSED(ignored)) return return_value; } +PyDoc_STRVAR(_tkinter__finalize_thread__doc__, +"_finalize_thread($module, /)\n" +"--\n" +"\n" +"Clean up per-thread state."); + +#define _TKINTER__FINALIZE_THREAD_METHODDEF \ + {"_finalize_thread", (PyCFunction)_tkinter__finalize_thread, METH_NOARGS, _tkinter__finalize_thread__doc__}, + +static PyObject * +_tkinter__finalize_thread_impl(PyObject *module); + +static PyObject * +_tkinter__finalize_thread(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _tkinter__finalize_thread_impl(module); +} + #ifndef _TKINTER_TKAPP_CREATEFILEHANDLER_METHODDEF #define _TKINTER_TKAPP_CREATEFILEHANDLER_METHODDEF #endif /* !defined(_TKINTER_TKAPP_CREATEFILEHANDLER_METHODDEF) */ @@ -963,4 +982,4 @@ _tkinter_getbusywaitinterval(PyObject *module, PyObject *Py_UNUSED(ignored)) #ifndef _TKINTER_TKAPP_DELETEFILEHANDLER_METHODDEF #define _TKINTER_TKAPP_DELETEFILEHANDLER_METHODDEF #endif /* !defined(_TKINTER_TKAPP_DELETEFILEHANDLER_METHODDEF) */ -/*[clinic end generated code: output=229ae8ed00ee4e40 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=af52d6870d8c6065 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_winapi.c.h b/Modules/clinic/_winapi.c.h index 88e4f1b0708ff4..d53902b48e2afe 100644 --- a/Modules/clinic/_winapi.c.h +++ b/Modules/clinic/_winapi.c.h @@ -2622,6 +2622,127 @@ _winapi_GetFileType(PyObject *module, PyObject *const *args, Py_ssize_t nargs, P return return_value; } +PyDoc_STRVAR(_winapi_GetConsoleMode__doc__, +"GetConsoleMode($module, handle, /)\n" +"--\n" +"\n" +"Return the current mode of a console buffer.\n" +"\n" +"The handle may refer to a console input buffer or a console screen\n" +"buffer."); + +#define _WINAPI_GETCONSOLEMODE_METHODDEF \ + {"GetConsoleMode", (PyCFunction)_winapi_GetConsoleMode, METH_O, _winapi_GetConsoleMode__doc__}, + +static DWORD +_winapi_GetConsoleMode_impl(PyObject *module, HANDLE handle); + +static PyObject * +_winapi_GetConsoleMode(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + HANDLE handle; + DWORD _return_value; + + handle = PyLong_AsVoidPtr(arg); + if (!handle && PyErr_Occurred()) { + goto exit; + } + _return_value = _winapi_GetConsoleMode_impl(module, handle); + if ((_return_value == PY_DWORD_MAX) && PyErr_Occurred()) { + goto exit; + } + return_value = PyLong_FromUnsignedLong(_return_value); + +exit: + return return_value; +} + +PyDoc_STRVAR(_winapi_SetConsoleMode__doc__, +"SetConsoleMode($module, handle, mode, /)\n" +"--\n" +"\n" +"Set the mode of a console buffer.\n" +"\n" +"The handle may refer to a console input buffer or a console screen\n" +"buffer."); + +#define _WINAPI_SETCONSOLEMODE_METHODDEF \ + {"SetConsoleMode", _PyCFunction_CAST(_winapi_SetConsoleMode), METH_FASTCALL, _winapi_SetConsoleMode__doc__}, + +static PyObject * +_winapi_SetConsoleMode_impl(PyObject *module, HANDLE handle, DWORD mode); + +static PyObject * +_winapi_SetConsoleMode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + HANDLE handle; + DWORD mode; + + if (!_PyArg_CheckPositional("SetConsoleMode", nargs, 2, 2)) { + goto exit; + } + handle = PyLong_AsVoidPtr(args[0]); + if (!handle && PyErr_Occurred()) { + goto exit; + } + if (!PyIndex_Check(args[1])) { + _PyArg_BadArgument("SetConsoleMode", "argument 2", "int", args[1]); + goto exit; + } + { + Py_ssize_t _bytes = PyLong_AsNativeBytes(args[1], &mode, sizeof(DWORD), + Py_ASNATIVEBYTES_NATIVE_ENDIAN | + Py_ASNATIVEBYTES_ALLOW_INDEX | + Py_ASNATIVEBYTES_UNSIGNED_BUFFER); + if (_bytes < 0) { + goto exit; + } + if ((size_t)_bytes > sizeof(DWORD)) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "integer value out of range", 1) < 0) + { + goto exit; + } + } + } + return_value = _winapi_SetConsoleMode_impl(module, handle, mode); + +exit: + return return_value; +} + +PyDoc_STRVAR(_winapi_FlushConsoleInputBuffer__doc__, +"FlushConsoleInputBuffer($module, handle, /)\n" +"--\n" +"\n" +"Flush the console input buffer.\n" +"\n" +"All input records currently in the input buffer are discarded."); + +#define _WINAPI_FLUSHCONSOLEINPUTBUFFER_METHODDEF \ + {"FlushConsoleInputBuffer", (PyCFunction)_winapi_FlushConsoleInputBuffer, METH_O, _winapi_FlushConsoleInputBuffer__doc__}, + +static PyObject * +_winapi_FlushConsoleInputBuffer_impl(PyObject *module, HANDLE handle); + +static PyObject * +_winapi_FlushConsoleInputBuffer(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + HANDLE handle; + + handle = PyLong_AsVoidPtr(arg); + if (!handle && PyErr_Occurred()) { + goto exit; + } + return_value = _winapi_FlushConsoleInputBuffer_impl(module, handle); + +exit: + return return_value; +} + PyDoc_STRVAR(_winapi__mimetypes_read_windows_registry__doc__, "_mimetypes_read_windows_registry($module, /, on_type_read)\n" "--\n" @@ -3057,4 +3178,4 @@ _winapi_GetTickCount64(PyObject *module, PyObject *Py_UNUSED(ignored)) #ifndef _WINAPI_GETSHORTPATHNAME_METHODDEF #define _WINAPI_GETSHORTPATHNAME_METHODDEF #endif /* !defined(_WINAPI_GETSHORTPATHNAME_METHODDEF) */ -/*[clinic end generated code: output=15ab8b04851932e1 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=0242bed9703b56a7 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_zoneinfo.c.h b/Modules/clinic/_zoneinfo.c.h index 5d13e5b745562c..4ec36c2209a774 100644 --- a/Modules/clinic/_zoneinfo.c.h +++ b/Modules/clinic/_zoneinfo.c.h @@ -19,7 +19,8 @@ static PyObject * zoneinfo_ZoneInfo_impl(PyTypeObject *type, PyObject *key); static PyObject * -zoneinfo_ZoneInfo(PyTypeObject *type, PyObject *args, PyObject *kwargs) +zoneinfo_ZoneInfo_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -51,10 +52,9 @@ zoneinfo_ZoneInfo(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[1]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *key; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -68,6 +68,40 @@ zoneinfo_ZoneInfo(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +zoneinfo_ZoneInfo(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return zoneinfo_ZoneInfo_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +zoneinfo_ZoneInfo_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *key; + + assert(_PyType_CAST(type)->tp_new == zoneinfo_ZoneInfo); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 1 || nargs > 1) { + return zoneinfo_ZoneInfo_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + key = args[0]; + Py_BEGIN_CRITICAL_SECTION(type); + return_value = zoneinfo_ZoneInfo_impl(_PyType_CAST(type), key); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(zoneinfo_ZoneInfo_from_file__doc__, "from_file($type, file_obj, /, key=None)\n" "--\n" @@ -425,15 +459,7 @@ zoneinfo_ZoneInfo___init_subclass__(PyObject *type, PyObject *args, PyObject *kw if (!_PyArg_NoPositional("__init_subclass__", args)) { goto exit; } - if (kwargs == NULL) { - __clinic_kwargs = PyDict_New(); - if (__clinic_kwargs == NULL) { - goto exit; - } - } - else { - __clinic_kwargs = Py_NewRef(kwargs); - } + __clinic_kwargs = Py_XNewRef(kwargs); return_value = zoneinfo_ZoneInfo___init_subclass___impl((PyTypeObject *)type, __clinic_kwargs); exit: @@ -442,4 +468,4 @@ zoneinfo_ZoneInfo___init_subclass__(PyObject *type, PyObject *args, PyObject *kw return return_value; } -/*[clinic end generated code: output=d7aeeec4b950fc35 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e64d1d5b38e0c6e5 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/atexitmodule.c.h b/Modules/clinic/atexitmodule.c.h index 9afa3a15a29614..8297f03c0276b5 100644 --- a/Modules/clinic/atexitmodule.c.h +++ b/Modules/clinic/atexitmodule.c.h @@ -39,15 +39,7 @@ atexit_register(PyObject *module, PyObject *args, PyObject *kwargs) if (!__clinic_args) { goto exit; } - if (kwargs == NULL) { - __clinic_kwargs = PyDict_New(); - if (__clinic_kwargs == NULL) { - goto exit; - } - } - else { - __clinic_kwargs = Py_NewRef(kwargs); - } + __clinic_kwargs = Py_XNewRef(kwargs); return_value = atexit_register_impl(module, func, __clinic_args, __clinic_kwargs); exit: @@ -58,4 +50,4 @@ atexit_register(PyObject *module, PyObject *args, PyObject *kwargs) return return_value; } -/*[clinic end generated code: output=8fd8f691fdbc4e0a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=ab4b375d516aa59b input=a9049054013a1b77]*/ diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index d6365b46f04b26..3fd1b7a2fc7b90 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -36,7 +36,8 @@ batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n, int strict); static PyObject * -batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +batched_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -68,13 +69,12 @@ batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[3]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2; + Py_ssize_t noptargs = nargs + nkw - 2; PyObject *iterable; Py_ssize_t n; int strict = 0; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -106,6 +106,30 @@ batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return batched_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +batched_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == batched_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + return batched_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + PyDoc_STRVAR(pairwise_new__doc__, "pairwise(iterable, /)\n" "--\n" @@ -138,6 +162,31 @@ pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +pairwise_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + + assert(_PyType_CAST(type)->tp_new == pairwise_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("pairwise", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("pairwise", nargs, 1, 1)) { + goto exit; + } + iterable = args[0]; + return_value = pairwise_new_impl(_PyType_CAST(type), iterable); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_groupby__doc__, "groupby(iterable, key=None)\n" "--\n" @@ -155,7 +204,8 @@ static PyObject * itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc); static PyObject * -itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_groupby_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -187,12 +237,11 @@ itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *it; PyObject *keyfunc = Py_None; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -209,6 +258,44 @@ itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_groupby_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_groupby_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *it; + PyObject *keyfunc = Py_None; + + assert(_PyType_CAST(type)->tp_new == itertools_groupby); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 1 || nargs > 2) { + return itertools_groupby_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + it = args[0]; + if (nargs < 2) { + goto skip_optional; + } + keyfunc = args[1]; +skip_optional: + return_value = itertools_groupby_impl(_PyType_CAST(type), it, keyfunc); + + return return_value; +} + static PyObject * itertools__grouper_impl(PyTypeObject *type, PyObject *parent, PyObject *tgtkey); @@ -279,6 +366,39 @@ itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_teedataobject_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *it; + PyObject *values; + PyObject *next; + + assert(_PyType_CAST(type)->tp_new == itertools_teedataobject); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("teedataobject", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("teedataobject", nargs, 3, 3)) { + goto exit; + } + it = args[0]; + if (!PyList_Check(args[1])) { + _PyArg_BadArgument("teedataobject", "argument 2", "list", args[1]); + goto exit; + } + values = args[1]; + next = args[2]; + return_value = itertools_teedataobject_impl(_PyType_CAST(type), it, values, next); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools__tee__doc__, "_tee(iterable, /)\n" "--\n" @@ -389,6 +509,31 @@ itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_cycle_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + + assert(_PyType_CAST(type)->tp_new == itertools_cycle); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("cycle", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("cycle", nargs, 1, 1)) { + goto exit; + } + iterable = args[0]; + return_value = itertools_cycle_impl(_PyType_CAST(type), iterable); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_dropwhile__doc__, "dropwhile(predicate, iterable, /)\n" "--\n" @@ -423,6 +568,33 @@ itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_dropwhile_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *func; + PyObject *seq; + + assert(_PyType_CAST(type)->tp_new == itertools_dropwhile); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("dropwhile", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("dropwhile", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + seq = args[1]; + return_value = itertools_dropwhile_impl(_PyType_CAST(type), func, seq); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_takewhile__doc__, "takewhile(predicate, iterable, /)\n" "--\n" @@ -455,6 +627,33 @@ itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_takewhile_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *func; + PyObject *seq; + + assert(_PyType_CAST(type)->tp_new == itertools_takewhile); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("takewhile", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("takewhile", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + seq = args[1]; + return_value = itertools_takewhile_impl(_PyType_CAST(type), func, seq); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_starmap__doc__, "starmap(function, iterable, /)\n" "--\n" @@ -487,6 +686,33 @@ itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_starmap_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *func; + PyObject *seq; + + assert(_PyType_CAST(type)->tp_new == itertools_starmap); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("starmap", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("starmap", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + seq = args[1]; + return_value = itertools_starmap_impl(_PyType_CAST(type), func, seq); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_chain_from_iterable__doc__, "from_iterable($type, iterable, /)\n" "--\n" @@ -552,7 +778,8 @@ itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r); static PyObject * -itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_combinations_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -584,11 +811,10 @@ itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *iterable; Py_ssize_t r; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -617,6 +843,57 @@ itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_combinations_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_combinations_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + Py_ssize_t r; + + assert(_PyType_CAST(type)->tp_new == itertools_combinations); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 2 || nargs > 2) { + return itertools_combinations_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + iterable = args[0]; + { + Py_ssize_t ival = -1; + PyObject *iobj = _PyNumber_Index(args[1]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + r = ival; + if (r < 0) { + PyErr_SetString(PyExc_ValueError, + "r cannot be negative"); + goto exit; + } + } + return_value = itertools_combinations_impl(_PyType_CAST(type), iterable, r); + +exit: + return return_value; +} + static PyObject * combinations_next_impl(combinationsobject *self); @@ -646,7 +923,8 @@ itertools_combinations_with_replacement_impl(PyTypeObject *type, Py_ssize_t r); static PyObject * -itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_combinations_with_replacement_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -678,11 +956,10 @@ itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyOb #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *iterable; Py_ssize_t r; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -711,6 +988,57 @@ itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyOb return return_value; } +static PyObject * +itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_combinations_with_replacement_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_combinations_with_replacement_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + Py_ssize_t r; + + assert(_PyType_CAST(type)->tp_new == itertools_combinations_with_replacement); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 2 || nargs > 2) { + return itertools_combinations_with_replacement_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + iterable = args[0]; + { + Py_ssize_t ival = -1; + PyObject *iobj = _PyNumber_Index(args[1]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + r = ival; + if (r < 0) { + PyErr_SetString(PyExc_ValueError, + "r cannot be negative"); + goto exit; + } + } + return_value = itertools_combinations_with_replacement_impl(_PyType_CAST(type), iterable, r); + +exit: + return return_value; +} + static PyObject * cwr_next_impl(cwrobject *self); @@ -739,7 +1067,8 @@ itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, PyObject *robj); static PyObject * -itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_permutations_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -771,12 +1100,11 @@ itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *iterable; PyObject *robj = Py_None; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -793,6 +1121,44 @@ itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_permutations_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_permutations_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + PyObject *robj = Py_None; + + assert(_PyType_CAST(type)->tp_new == itertools_permutations); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 1 || nargs > 2) { + return itertools_permutations_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + iterable = args[0]; + if (nargs < 2) { + goto skip_optional; + } + robj = args[1]; +skip_optional: + return_value = itertools_permutations_impl(_PyType_CAST(type), iterable, robj); + + return return_value; +} + static PyObject * permutations_next_impl(permutationsobject *self); @@ -819,7 +1185,8 @@ itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, PyObject *binop, PyObject *initial); static PyObject * -itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_accumulate_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -851,13 +1218,12 @@ itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[3]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *iterable; PyObject *binop = Py_None; PyObject *initial = Py_None; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -884,6 +1250,30 @@ itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_accumulate_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_accumulate_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == itertools_accumulate); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + return itertools_accumulate_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + static PyObject * accumulate_next_impl(accumulateobject *self); @@ -912,7 +1302,8 @@ static PyObject * itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2); static PyObject * -itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_compress_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -944,11 +1335,10 @@ itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *seq1; PyObject *seq2; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -961,6 +1351,40 @@ itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_compress_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_compress_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *seq1; + PyObject *seq2; + + assert(_PyType_CAST(type)->tp_new == itertools_compress); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 2 || nargs > 2) { + return itertools_compress_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + seq1 = args[0]; + seq2 = args[1]; + return_value = itertools_compress_impl(_PyType_CAST(type), seq1, seq2); + + return return_value; +} + PyDoc_STRVAR(itertools_filterfalse__doc__, "filterfalse(function, iterable, /)\n" "--\n" @@ -995,6 +1419,33 @@ itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_filterfalse_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *func; + PyObject *seq; + + assert(_PyType_CAST(type)->tp_new == itertools_filterfalse); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("filterfalse", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("filterfalse", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + seq = args[1]; + return_value = itertools_filterfalse_impl(_PyType_CAST(type), func, seq); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_count__doc__, "count(start=0, step=1)\n" "--\n" @@ -1013,7 +1464,8 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, PyObject *long_step); static PyObject * -itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_count_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -1045,12 +1497,11 @@ itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; + Py_ssize_t noptargs = nargs + nkw - 0; PyObject *long_cnt = NULL; PyObject *long_step = NULL; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -1072,6 +1523,47 @@ itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_count_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_count_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *long_cnt = NULL; + PyObject *long_step = NULL; + + assert(_PyType_CAST(type)->tp_new == itertools_count); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs > 2) { + return itertools_count_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + if (nargs < 1) { + goto skip_optional; + } + long_cnt = args[0]; + if (nargs < 2) { + goto skip_optional; + } + long_step = args[1]; +skip_optional: + return_value = itertools_count_impl(_PyType_CAST(type), long_cnt, long_step); + + return return_value; +} + static PyObject * zip_longest_next_impl(ziplongestobject *self); @@ -1086,4 +1578,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=4b55088ac24582b7 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=60939958c9c9348b input=a9049054013a1b77]*/ diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h index e992a4d1b8f797..c2f183f7037836 100644 --- a/Modules/clinic/posixmodule.c.h +++ b/Modules/clinic/posixmodule.c.h @@ -6322,7 +6322,7 @@ os_wait(PyObject *module, PyObject *Py_UNUSED(ignored)) #endif /* defined(HAVE_WAIT) */ -#if (defined(HAVE_PIDFD_OPEN) || (defined(__linux__) && defined(__NR_pidfd_open) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) +#if (defined(_Py_HAVE_PIDFD_OPEN) || (defined(__linux__) && defined(__NR_pidfd_open) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) PyDoc_STRVAR(os_pidfd_open__doc__, "pidfd_open($module, /, pid, flags=0)\n" @@ -6397,9 +6397,9 @@ os_pidfd_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec return return_value; } -#endif /* (defined(HAVE_PIDFD_OPEN) || (defined(__linux__) && defined(__NR_pidfd_open) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) */ +#endif /* (defined(_Py_HAVE_PIDFD_OPEN) || (defined(__linux__) && defined(__NR_pidfd_open) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) */ -#if (defined(HAVE_PIDFD_GETFD) || (defined(__linux__) && defined(__NR_pidfd_getfd) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) +#if (defined(_Py_HAVE_PIDFD_GETFD) || (defined(__linux__) && defined(__NR_pidfd_getfd) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) PyDoc_STRVAR(os_pidfd_getfd__doc__, "pidfd_getfd($module, /, pidfd, targetfd, *, flags=0)\n" @@ -6484,7 +6484,7 @@ os_pidfd_getfd(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObje return return_value; } -#endif /* (defined(HAVE_PIDFD_GETFD) || (defined(__linux__) && defined(__NR_pidfd_getfd) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) */ +#endif /* (defined(_Py_HAVE_PIDFD_GETFD) || (defined(__linux__) && defined(__NR_pidfd_getfd) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) */ #if defined(HAVE_SETNS) @@ -13763,4 +13763,4 @@ os__emscripten_log(PyObject *module, PyObject *const *args, Py_ssize_t nargs, Py #ifndef OS__EMSCRIPTEN_LOG_METHODDEF #define OS__EMSCRIPTEN_LOG_METHODDEF #endif /* !defined(OS__EMSCRIPTEN_LOG_METHODDEF) */ -/*[clinic end generated code: output=c92854d21c8f6290 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=077c57e5f93bd790 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/signalmodule.c.h b/Modules/clinic/signalmodule.c.h index 6125f253ac0380..26139ff0fc0cd3 100644 --- a/Modules/clinic/signalmodule.c.h +++ b/Modules/clinic/signalmodule.c.h @@ -689,7 +689,7 @@ signal_pthread_kill(PyObject *module, PyObject *const *args, Py_ssize_t nargs) #endif /* defined(HAVE_PTHREAD_KILL) */ -#if (defined(HAVE_PIDFD_SEND_SIGNAL) || (defined(__linux__) && defined(__NR_pidfd_send_signal) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) +#if (defined(_Py_HAVE_PIDFD_SEND_SIGNAL) || (defined(__linux__) && defined(__NR_pidfd_send_signal) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) PyDoc_STRVAR(signal_pidfd_send_signal__doc__, "pidfd_send_signal($module, pidfd, signalnum, siginfo=None, flags=0, /)\n" @@ -754,7 +754,7 @@ signal_pidfd_send_signal(PyObject *module, PyObject *const *args, Py_ssize_t nar return return_value; } -#endif /* (defined(HAVE_PIDFD_SEND_SIGNAL) || (defined(__linux__) && defined(__NR_pidfd_send_signal) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) */ +#endif /* (defined(_Py_HAVE_PIDFD_SEND_SIGNAL) || (defined(__linux__) && defined(__NR_pidfd_send_signal) && !(defined(__ANDROID__) && __ANDROID_API__ < 31))) */ #ifndef SIGNAL_ALARM_METHODDEF #define SIGNAL_ALARM_METHODDEF @@ -807,4 +807,4 @@ signal_pidfd_send_signal(PyObject *module, PyObject *const *args, Py_ssize_t nar #ifndef SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF #define SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF #endif /* !defined(SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF) */ -/*[clinic end generated code: output=2a04ec31f49b1c93 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=48d30418971f312a input=a9049054013a1b77]*/ diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h index 09041d92a30579..5a1a128b304f02 100644 --- a/Modules/clinic/socketmodule.c.h +++ b/Modules/clinic/socketmodule.c.h @@ -2152,7 +2152,7 @@ _socket_if_nameindex(PyObject *module, PyObject *Py_UNUSED(ignored)) #endif /* (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS_DESKTOP)) */ -#if (defined(HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS_DESKTOP)) +#if (defined(_Py_HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS_DESKTOP)) PyDoc_STRVAR(_socket_if_nametoindex__doc__, "if_nametoindex($module, oname, /)\n" @@ -2184,9 +2184,9 @@ _socket_if_nametoindex(PyObject *module, PyObject *arg) return return_value; } -#endif /* (defined(HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS_DESKTOP)) */ +#endif /* (defined(_Py_HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS_DESKTOP)) */ -#if (defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP)) +#if (defined(_Py_HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP)) PyDoc_STRVAR(_socket_if_indextoname__doc__, "if_indextoname($module, if_index, /)\n" @@ -2215,7 +2215,7 @@ _socket_if_indextoname(PyObject *module, PyObject *arg) return return_value; } -#endif /* (defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP)) */ +#endif /* (defined(_Py_HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP)) */ #if defined(CMSG_LEN) @@ -2449,4 +2449,4 @@ _socket_CMSG_SPACE(PyObject *module, PyObject *arg) #ifndef _SOCKET_CMSG_SPACE_METHODDEF #define _SOCKET_CMSG_SPACE_METHODDEF #endif /* !defined(_SOCKET_CMSG_SPACE_METHODDEF) */ -/*[clinic end generated code: output=bca43f33969d67ad input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e6e677d3de0c63e4 input=a9049054013a1b77]*/ diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index e756b550e7753e..6e2d8e60c99e97 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -1004,12 +1004,11 @@ cmath_phase_impl(PyObject *module, Py_complex z) { double phi; - errno = 0; - phi = atan2(z.imag, z.real); /* should not cause any exception */ - if (errno != 0) - return math_error(); - else - return PyFloat_FromDouble(phi); + phi = atan2(z.imag, z.real); + /* gh-153144: Ignore atan2() errno on purpose since it can optionally be + EDOM, which we should ignore, or ERANGE if phi underflows, + which is silent on Python. Overflow is not possible. */ + return PyFloat_FromDouble(phi); } /*[clinic input] diff --git a/Modules/expat/expat.h b/Modules/expat/expat.h index 5756ed31936c49..4a7260d35b369d 100644 --- a/Modules/expat/expat.h +++ b/Modules/expat/expat.h @@ -781,21 +781,28 @@ XML_GetIdAttributeIndex(XML_Parser parser); /* Parses some input. Returns XML_STATUS_ERROR if a fatal error is detected. The last call to XML_Parse must have isFinal true; len may be zero for this call (or any other). - - Though the return values for these functions has always been - described as a Boolean value, the implementation, at least for the - 1.95.x series, has always returned exactly one of the XML_Status - values. */ XMLPARSEAPI(enum XML_Status) XML_Parse(XML_Parser parser, const char *s, int len, int isFinal); +// Added in Expat 2.11.0. +XMLPARSEAPI(enum XML_Status) +XML_Parse64(XML_Parser parser, const char *s, size_t len, XML_Bool isFinal); + XMLPARSEAPI(void *) XML_GetBuffer(XML_Parser parser, int len); +// Added in Expat 2.11.0. +XMLPARSEAPI(void *) +XML_GetBuffer64(XML_Parser parser, size_t len); + XMLPARSEAPI(enum XML_Status) XML_ParseBuffer(XML_Parser parser, int len, int isFinal); +// Added in Expat 2.11.0. +XMLPARSEAPI(enum XML_Status) +XML_ParseBuffer64(XML_Parser parser, size_t len, XML_Bool isFinal); + /* Stops parsing, causing XML_Parse() or XML_ParseBuffer() to return. Must be called from within a call-back handler, except when aborting (resumable = 0) an already suspended parser. Some call-backs may @@ -1147,7 +1154,7 @@ XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled); See https://semver.org */ # define XML_MAJOR_VERSION 2 -# define XML_MINOR_VERSION 10 +# define XML_MINOR_VERSION 11 # define XML_MICRO_VERSION 0 # ifdef __cplusplus diff --git a/Modules/expat/internal.h b/Modules/expat/internal.h index 3c50a1e404ab73..7ab186c0342b3c 100644 --- a/Modules/expat/internal.h +++ b/Modules/expat/internal.h @@ -129,6 +129,10 @@ union expat_align { typedef unsigned long long XmlBigCount; +// NOTE: Type `ssize_t` and macro `SSIZE_MAX` are not guaranteed by C99 +typedef long expat_ssize_t; +# define EXPAT_SSIZE_MAX LONG_MAX + void _INTERNAL_trim_to_complete_utf8_characters(const char *from, const char **fromLimRef); diff --git a/Modules/expat/latin1tab.h b/Modules/expat/latin1tab.h index 3793f4f3cc7841..df421f2e7d8719 100644 --- a/Modules/expat/latin1tab.h +++ b/Modules/expat/latin1tab.h @@ -9,7 +9,7 @@ Copyright (c) 1997-2000 Thai Open Source Software Center Ltd Copyright (c) 2000 Clark Cooper Copyright (c) 2002 Fred L. Drake, Jr. - Copyright (c) 2017 Sebastian Pipping + Copyright (c) 2017-2026 Sebastian Pipping Licensed under the MIT license: Permission is hereby granted, free of charge, to any person obtaining @@ -44,11 +44,11 @@ /* 0x9C */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER, /* 0xA0 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER, /* 0xA4 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER, - /* 0xA8 */ BT_OTHER, BT_OTHER, BT_NMSTRT, BT_OTHER, + /* 0xA8 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER, /* 0xAC */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER, /* 0xB0 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER, - /* 0xB4 */ BT_OTHER, BT_NMSTRT, BT_OTHER, BT_NAME, - /* 0xB8 */ BT_OTHER, BT_OTHER, BT_NMSTRT, BT_OTHER, + /* 0xB4 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_NAME, + /* 0xB8 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER, /* 0xBC */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER, /* 0xC0 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, /* 0xC4 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, diff --git a/Modules/expat/nametab.h b/Modules/expat/nametab.h index 2385851562e7dc..57407e87b693d4 100644 --- a/Modules/expat/nametab.h +++ b/Modules/expat/nametab.h @@ -6,8 +6,8 @@ \___/_/\_\ .__/ \__,_|\__| |_| XML parser - Copyright (c) 2000 Clark Cooper - Copyright (c) 2017 Sebastian Pipping + Copyright (c) 2000 Clark Cooper + Copyright (c) 2017-2026 Sebastian Pipping Licensed under the MIT license: Permission is hereby granted, free of charge, to any person obtaining @@ -37,102 +37,66 @@ static const unsigned namingBitmap[] = { 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x04000000, 0x87FFFFFE, 0x07FFFFFE, 0x00000000, 0x00000000, 0xFF7FFFFF, 0xFF7FFFFF, - 0xFFFFFFFF, 0x7FF3FFFF, 0xFFFFFDFE, 0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, - 0xFFFFE00F, 0xFC31FFFF, 0x00FFFFFF, 0x00000000, 0xFFFF0000, 0xFFFFFFFF, - 0xFFFFFFFF, 0xF80001FF, 0x00000003, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0xFFFFD740, 0xFFFFFFFB, 0x547F7FFF, 0x000FFFFD, - 0xFFFFDFFE, 0xFFFFFFFF, 0xDFFEFFFF, 0xFFFFFFFF, 0xFFFF0003, 0xFFFFFFFF, - 0xFFFF199F, 0x033FCFFF, 0x00000000, 0xFFFE0000, 0x027FFFFF, 0xFFFFFFFE, - 0x0000007F, 0x00000000, 0xFFFF0000, 0x000707FF, 0x00000000, 0x07FFFFFE, - 0x000007FE, 0xFFFE0000, 0xFFFFFFFF, 0x7CFFFFFF, 0x002F7FFF, 0x00000060, - 0xFFFFFFE0, 0x23FFFFFF, 0xFF000000, 0x00000003, 0xFFF99FE0, 0x03C5FDFF, - 0xB0000000, 0x00030003, 0xFFF987E0, 0x036DFDFF, 0x5E000000, 0x001C0000, - 0xFFFBAFE0, 0x23EDFDFF, 0x00000000, 0x00000001, 0xFFF99FE0, 0x23CDFDFF, - 0xB0000000, 0x00000003, 0xD63DC7E0, 0x03BFC718, 0x00000000, 0x00000000, - 0xFFFDDFE0, 0x03EFFDFF, 0x00000000, 0x00000003, 0xFFFDDFE0, 0x03EFFDFF, - 0x40000000, 0x00000003, 0xFFFDDFE0, 0x03FFFDFF, 0x00000000, 0x00000003, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFE, 0x000D7FFF, - 0x0000003F, 0x00000000, 0xFEF02596, 0x200D6CAE, 0x0000001F, 0x00000000, - 0x00000000, 0x00000000, 0xFFFFFEFF, 0x000003FF, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0xFFFFFFFF, 0xFFFF003F, 0x007FFFFF, 0x0007DAED, 0x50000000, - 0x82315001, 0x002C62AB, 0x40000000, 0xF580C900, 0x00000007, 0x02010800, - 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0FFFFFFF, 0xFFFFFFFF, - 0xFFFFFFFF, 0x03FFFFFF, 0x3F3FFFFF, 0xFFFFFFFF, 0xAAFF3F3F, 0x3FFFFFFF, - 0xFFFFFFFF, 0x5FDFFFFF, 0x0FCF1FDC, 0x1FDC1FFF, 0x00000000, 0x00004C40, - 0x00000000, 0x00000000, 0x00000007, 0x00000000, 0x00000000, 0x00000000, - 0x00000080, 0x000003FE, 0xFFFFFFFE, 0xFFFFFFFF, 0x001FFFFF, 0xFFFFFFFE, - 0xFFFFFFFF, 0x07FFFFFF, 0xFFFFFFE0, 0x00001FFF, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, - 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0000003F, 0x00000000, 0x00000000, - 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0000000F, - 0x00000000, 0x00000000, 0x00000000, 0x07FF6000, 0x87FFFFFE, 0x07FFFFFE, - 0x00000000, 0x00800000, 0xFF7FFFFF, 0xFF7FFFFF, 0x00FFFFFF, 0x00000000, - 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFFF, 0xF80001FF, 0x00030003, 0x00000000, - 0xFFFFFFFF, 0xFFFFFFFF, 0x0000003F, 0x00000003, 0xFFFFD7C0, 0xFFFFFFFB, - 0x547F7FFF, 0x000FFFFD, 0xFFFFDFFE, 0xFFFFFFFF, 0xDFFEFFFF, 0xFFFFFFFF, - 0xFFFF007B, 0xFFFFFFFF, 0xFFFF199F, 0x033FCFFF, 0x00000000, 0xFFFE0000, - 0x027FFFFF, 0xFFFFFFFE, 0xFFFE007F, 0xBBFFFFFB, 0xFFFF0016, 0x000707FF, - 0x00000000, 0x07FFFFFE, 0x0007FFFF, 0xFFFF03FF, 0xFFFFFFFF, 0x7CFFFFFF, - 0xFFEF7FFF, 0x03FF3DFF, 0xFFFFFFEE, 0xF3FFFFFF, 0xFF1E3FFF, 0x0000FFCF, - 0xFFF99FEE, 0xD3C5FDFF, 0xB080399F, 0x0003FFCF, 0xFFF987E4, 0xD36DFDFF, - 0x5E003987, 0x001FFFC0, 0xFFFBAFEE, 0xF3EDFDFF, 0x00003BBF, 0x0000FFC1, - 0xFFF99FEE, 0xF3CDFDFF, 0xB0C0398F, 0x0000FFC3, 0xD63DC7EC, 0xC3BFC718, - 0x00803DC7, 0x0000FF80, 0xFFFDDFEE, 0xC3EFFDFF, 0x00603DDF, 0x0000FFC3, - 0xFFFDDFEC, 0xC3EFFDFF, 0x40603DDF, 0x0000FFC3, 0xFFFDDFEC, 0xC3FFFDFF, - 0x00803DCF, 0x0000FFC3, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0xFFFFFFFE, 0x07FF7FFF, 0x03FF7FFF, 0x00000000, 0xFEF02596, 0x3BFF6CAE, - 0x03FF3F5F, 0x00000000, 0x03000000, 0xC2A003FF, 0xFFFFFEFF, 0xFFFE03FF, - 0xFEBF0FDF, 0x02FE3FFF, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x1FFF0000, 0x00000002, - 0x000000A0, 0x003EFFFE, 0xFFFFFFFE, 0xFFFFFFFF, 0x661FFFFF, 0xFFFFFFFE, - 0xFFFFFFFF, 0x77FFFFFF, + 0x00000000, 0x00000000, 0x00000000, 0xBFFF0000, 0xFFFFFFFF, 0xFFFFFFFF, + 0xFFFFFFFF, 0xFFFFFFFF, 0x00003000, 0x00000000, 0x00000000, 0xFFFF0000, + 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, + 0xFFFFFFFF, 0xFFFFFFFF, 0x0000FFFF, 0x00000000, 0x00000000, 0x00000000, + 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, + 0xFFFFFFFF, 0x0000FFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, + 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, + 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0000FFFF, 0xFFFF0000, + 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, + 0xFFFFFFFF, 0x3FFFFFFF, 0x00000000, 0x07FF6000, 0x87FFFFFE, 0x07FFFFFE, + 0x00000000, 0x00800000, 0xFF7FFFFF, 0xFF7FFFFF, 0xFFFFFFFF, 0xFFFFFFFF, + 0xFFFFFFFF, 0xBFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, + 0x00003000, 0x80000000, 0x00000001, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFFF, + 0xFFFFFFFF, 0xFFFFFFFF, }; static const unsigned char nmstrtPages[] = { - 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x09, 0x0A, 0x0B, - 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x13, 0x00, 0x14, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x15, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x02, 0x01, 0x01, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x05, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x06, + 0x07, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x01, 0x01, 0x01, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x18, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, + 0x01, 0x08, 0x01, 0x09, }; static const unsigned char namePages[] = { - 0x19, 0x03, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x00, 0x00, 0x1F, 0x20, 0x21, - 0x22, 0x23, 0x24, 0x25, 0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x13, 0x26, 0x14, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x27, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x0A, 0x01, 0x01, 0x0B, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0C, 0x05, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x06, + 0x07, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x01, 0x01, 0x01, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x18, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, + 0x01, 0x08, 0x01, 0x09, }; diff --git a/Modules/expat/refresh.sh b/Modules/expat/refresh.sh index 55f491da479e08..469fc5a09d4d2d 100755 --- a/Modules/expat/refresh.sh +++ b/Modules/expat/refresh.sh @@ -12,9 +12,9 @@ fi # Update this when updating to a new version after verifying that the changes # the update brings in are good. These values are used for verifying the SBOM, too. -expected_libexpat_tag="R_2_10_0" -expected_libexpat_version="2.10.0" -expected_libexpat_sha256="5466a341f9c692ef7a958a6881311be597f8e1ce345984cf78414f54793dea4c" +expected_libexpat_tag="R_2_11_0" +expected_libexpat_version="2.11.0" +expected_libexpat_sha256="b3e3fcbb629e4d357a9cc0af5b146b85959bcf1cefe87e529d2c0a9234be737f" expat_dir="$(realpath "$(dirname -- "${BASH_SOURCE[0]}")")" cd ${expat_dir} diff --git a/Modules/expat/xmlparse.c b/Modules/expat/xmlparse.c index 6c0c4506156176..40d34923e45246 100644 --- a/Modules/expat/xmlparse.c +++ b/Modules/expat/xmlparse.c @@ -1,4 +1,4 @@ -/* d0712c5efef760b5355ed61473a927256c6474aecb853016d2e335e571c64b0c (2.10.0+) +/* 643e4d909658296100c7d226d5c26bf60bf9d6377fb83d7b5415d7193809f8f7 (2.11.0+) __ __ _ ___\ \/ /_ __ __ _| |_ / _ \\ /| '_ \ / _` | __| @@ -229,7 +229,6 @@ typedef char ICHAR; /* Do safe (NULL-aware) pointer arithmetic */ #define EXPAT_SAFE_PTR_DIFF(p, q) (((p) && (q)) ? ((p) - (q)) : 0) -#define EXPAT_SAFE_PTR_ADD(p, a) ((p) ? ((p) + (a)) : NULL) #define EXPAT_MIN(a, b) (((a) < (b)) ? (a) : (b)) @@ -303,7 +302,7 @@ typedef struct { typedef struct tag { struct tag *parent; /* parent of this element */ const char *rawName; /* tagName in the original encoding */ - int rawNameLength; + size_t rawNameLength; TAG_NAME name; /* tagName in the API encoding */ union { char *raw; /* for byte-level access (rawName storage) */ @@ -317,17 +316,17 @@ typedef struct { enum XML_Content_Type type; enum XML_Content_Quant quant; const XML_Char *name; - int firstchild; - int lastchild; - int childcnt; - int nextsib; + size_t firstchild; + size_t lastchild; + size_t childcnt; + size_t nextsib; } CONTENT_SCAFFOLD; #define INIT_SCAFFOLD_ELEMENTS 32 typedef struct block { struct block *next; - int size; + size_t size; XML_Char s[]; } BLOCK; @@ -409,11 +408,11 @@ typedef struct { /* === scaffolding for building content model === */ bool in_eldecl; CONTENT_SCAFFOLD *scaffold; - unsigned contentStringLen; - unsigned scaffSize; - unsigned scaffCount; - int scaffLevel; - int *scaffIndex; + size_t contentStringLen; + size_t scaffSize; + size_t scaffCount; + size_t scaffLevel; + size_t *scaffIndex; size_t scaffIndexSize; } DTD; @@ -565,6 +564,8 @@ static int reportComment(XML_Parser parser, const ENCODING *enc, const char *start, const char *end); static void reportDefault(XML_Parser parser, const ENCODING *enc, const char *start, const char *end); +static void reportCharacterData(XML_Parser parser, const XML_Char *s, + size_t len); static const XML_Char *getContext(XML_Parser parser); static XML_Bool setContext(XML_Parser parser, const XML_Char *context); @@ -596,7 +597,7 @@ static const XML_Char *poolCopyStringN(STRING_POOL *pool, const XML_Char *s, size_t n); static const XML_Char *poolAppendString(STRING_POOL *pool, const XML_Char *s); -static int nextScaffoldPart(XML_Parser parser); +static expat_ssize_t nextScaffoldPart(XML_Parser parser); static XML_Content *build_model(XML_Parser parser); static ELEMENT_TYPE *getElementType(XML_Parser parser, const ENCODING *enc, const char *ptr, const char *end); @@ -614,6 +615,8 @@ static XML_Parser parserCreate(const XML_Char *encodingName, static void parserInit(XML_Parser parser, const XML_Char *encodingName, bool isReset); +static void *internalGetBuffer64(XML_Parser parser, size_t len); + #if XML_GE == 1 static float accountingGetCurrentAmplification(XML_Parser rootParser, XmlBigCount extraIndirectBytes); @@ -741,7 +744,7 @@ struct XML_ParserStruct { const char *m_parseEndPtr; size_t m_partialTokenBytesBefore; /* used in heuristic to avoid O(n^2) */ XML_Bool m_reparseDeferralEnabled; - int m_lastBufferRequestSize; + size_t m_lastBufferRequestSize; XML_Char *m_dataBuf; XML_Char *m_dataBufEnd; XML_StartElementHandler m_startElementHandler; @@ -1128,7 +1131,7 @@ gather_time_entropy(void) { static struct sipkey ENTROPY_DEBUG(const char *label, struct sipkey entropy_128) { - if (getDebugLevel("EXPAT_ENTROPY_DEBUG", 0) >= 1u) { + if (getDebugLevel("EXPAT_ENTROPY_DEBUG", 0) >= 1) { fprintf(stderr, "expat: Entropy: %s --> [0x" EXPAT_FMT_LLX( "016") ", 0x" EXPAT_FMT_LLX("016") "] (16 bytes)\n", @@ -1245,9 +1248,8 @@ callProcessor(XML_Parser parser, const char *start, const char *end, available_buffer += EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd); // m_lastBufferRequestSize is never assigned a value < 0, so the cast is ok - const bool enough - = (have_now >= 2 * had_before) - || ((size_t)parser->m_lastBufferRequestSize > available_buffer); + const bool enough = (have_now >= 2 * had_before) + || (parser->m_lastBufferRequestSize > available_buffer); if (! enough) { *endPtr = start; // callers may expect this to be set @@ -1380,8 +1382,7 @@ parserCreate(const XML_Char *encodingName, // Initialize .m_alloc_tracker memset(&parser->m_alloc_tracker, 0, sizeof(MALLOC_TRACKER)); if (parentParser == NULL) { - parser->m_alloc_tracker.debugLevel - = getDebugLevel("EXPAT_MALLOC_DEBUG", 0u); + parser->m_alloc_tracker.debugLevel = getDebugLevel("EXPAT_MALLOC_DEBUG", 0); parser->m_alloc_tracker.maximumAmplificationFactor = EXPAT_ALLOC_TRACKER_MAXIMUM_AMPLIFICATION_DEFAULT; parser->m_alloc_tracker.activationThresholdBytes @@ -1502,7 +1503,7 @@ parserInit(XML_Parser parser, const XML_Char *encodingName, bool isReset) { parser->m_protocolEncodingName = copyString(encodingName, parser); } parser->m_curBase = NULL; - XmlInitEncoding(&parser->m_initEncoding, &parser->m_encoding, 0); + XmlInitEncoding(&parser->m_initEncoding, &parser->m_encoding, NULL); parser->m_userData = NULL; parser->m_handlerArg = NULL; parser->m_startElementHandler = NULL; @@ -1558,6 +1559,7 @@ parserInit(XML_Parser parser, const XML_Char *encodingName, bool isReset) { parser->m_tagStack = NULL; parser->m_inheritedBindings = NULL; parser->m_nSpecifiedAtts = 0; + parser->m_idAttIndex = -1; parser->m_unknownEncodingMem = NULL; parser->m_unknownEncodingConvert = NULL; parser->m_unknownEncodingRelease = NULL; @@ -1581,7 +1583,7 @@ parserInit(XML_Parser parser, const XML_Char *encodingName, bool isReset) { parser->m_accounting.countBytesIndirect = 0; if (! isReset) { parser->m_accounting.debugLevel - = getDebugLevel("EXPAT_ACCOUNTING_DEBUG", 0u); + = getDebugLevel("EXPAT_ACCOUNTING_DEBUG", 0); parser->m_accounting.maximumAmplificationFactor = EXPAT_BILLION_LAUGHS_ATTACK_PROTECTION_MAXIMUM_AMPLIFICATION_DEFAULT; parser->m_accounting.activationThresholdBytes @@ -1592,7 +1594,7 @@ parserInit(XML_Parser parser, const XML_Char *encodingName, bool isReset) { parser->m_entity_stats.currentDepth = 0; parser->m_entity_stats.maximumDepthSeen = 0; if (! isReset) - parser->m_entity_stats.debugLevel = getDebugLevel("EXPAT_ENTITY_DEBUG", 0u); + parser->m_entity_stats.debugLevel = getDebugLevel("EXPAT_ENTITY_DEBUG", 0); #else UNUSED_P(isReset); #endif @@ -2334,14 +2336,33 @@ XML_SetHashSalt16Bytes(XML_Parser parser, const uint8_t entropy[16]) { return XML_TRUE; } +static bool +isInsideEntity(XML_Parser parser) { + return parser->m_openInternalEntities || parser->m_openAttributeEntities + || parser->m_openValueEntities; +} + enum XML_Status XMLCALL XML_Parse(XML_Parser parser, const char *s, int len, int isFinal) { if (parser == NULL || isCalledFromInsideHandler(parser)) return XML_STATUS_ERROR; - if ((len < 0) || ((s == NULL) && (len != 0))) { + if (len < 0) { + parser->m_errorCode = XML_ERROR_INVALID_ARGUMENT; + return XML_STATUS_ERROR; + } + return XML_Parse64(parser, s, (size_t)len, isFinal ? XML_TRUE : XML_FALSE); +} + +enum XML_Status XMLCALL +XML_Parse64(XML_Parser parser, const char *s, size_t len, XML_Bool isFinal) { + if (parser == NULL || isCalledFromInsideHandler(parser)) + return XML_STATUS_ERROR; + + if ((s == NULL) && (len != 0)) { parser->m_errorCode = XML_ERROR_INVALID_ARGUMENT; return XML_STATUS_ERROR; } + switch (parser->m_parsingStatus.parsing) { case XML_SUSPENDED: parser->m_errorCode = XML_ERROR_SUSPENDED; @@ -2359,8 +2380,16 @@ XML_Parse(XML_Parser parser, const char *s, int len, int isFinal) { parser->m_parsingStatus.parsing = XML_PARSING; } + // NOTE: This checks if we can get away without copying `s` into the buffer + // and allocating the needed space for it. #if XML_CONTEXT_BYTES == 0 + // Is the buffer empty? if (parser->m_bufferPtr == parser->m_bufferEnd) { +#else + // Is this the first parse on the document (that has no chance at any past + // context bytes anyway)? + if (parser->m_parseEndByteIndex == 0) { +#endif // This null-object trick allows us to stop worrying about case `s == NULL` // within this branch, e.g. with `s + len - end`. const char *const empty = ""; @@ -2383,87 +2412,198 @@ XML_Parse(XML_Parser parser, const char *s, int len, int isFinal) { parser->m_lastBufferRequestSize = len; parser->m_parseEndByteIndex += len; parser->m_positionPtr = s; - parser->m_parsingStatus.finalBuffer = (XML_Bool)isFinal; + parser->m_parsingStatus.finalBuffer = isFinal; + +#if XML_CONTEXT_BYTES > 0 + // Fake presence of a buffer to functions like `XML_GetInputContext64` that + // rely on these pointers being set + /*.*/ char *const bufferBackup = parser->m_buffer; + const char *const bufferPtrBackup = parser->m_bufferPtr; + /*.*/ char *const bufferEndBackup = parser->m_bufferEnd; + const char *const bufferLimBackup = parser->m_bufferLim; + parser->m_buffer = (char *)s; + parser->m_bufferPtr = /**/ s; + parser->m_bufferEnd = (char *)end; + parser->m_bufferLim = /*...*/ end; +#endif parser->m_errorCode = callProcessor(parser, s, parser->m_parseEndPtr = end, &end); +#if XML_CONTEXT_BYTES > 0 + // Undo fake buffer + parser->m_buffer = bufferBackup; + parser->m_bufferPtr = bufferPtrBackup; + parser->m_bufferEnd = bufferEndBackup; + parser->m_bufferLim = bufferLimBackup; +#endif + if (parser->m_errorCode != XML_ERROR_NONE) { - parser->m_eventEndPtr = parser->m_eventPtr; + // Do we need to rescue `parser->m_eventPtr`? + // It cannot continue pointing into `s` after the call because we have + // no guarantee that that that buffer lives on. + // + // Note: The `uintptr_t` casts intend to address `6.5.8 Relational + // operators` of C99 that says that comparison of pointers that do + // not point into the same array object is undefined behavior. + if (parser->m_eventPtr && (uintptr_t)parser->m_eventPtr >= (uintptr_t)s + && (uintptr_t)parser->m_eventPtr <= (uintptr_t)(s + len)) { + const size_t rewindBy = s + len - parser->m_eventPtr; + + XmlUpdatePosition(parser->m_encoding, parser->m_positionPtr, + parser->m_eventPtr, &parser->m_position); + parser->m_positionPtr = parser->m_eventPtr; + + // Ensure presence of a buffer so that we can point `parser->m_eventPtr` + // into it to avoid `parser->m_eventPtr == NULL`. + if (! parser->m_buffer) { + const enum XML_Error errorCodeBackup = parser->m_errorCode; + if (! internalGetBuffer64(parser, 1)) { + // NOTE: parser->m_errorCode has already been set by + // XML_GetBuffer(). + parser->m_errorCode = errorCodeBackup; + parser->m_eventPtr = parser->m_eventEndPtr = NULL; + parser->m_processor = errorProcessor; + return XML_STATUS_ERROR; + } + } + assert(parser->m_buffer != NULL); + + parser->m_eventPtr = parser->m_eventEndPtr = parser->m_bufferPtr; + parser->m_positionPtr = parser->m_eventPtr; + parser->m_parseEndPtr = parser->m_eventPtr; + // To make `XML_GetCurrentByteIndex64` work: + parser->m_parseEndByteIndex -= rewindBy; + } else { + parser->m_eventPtr = parser->m_eventEndPtr = NULL; + parser->m_positionPtr = NULL; + } parser->m_processor = errorProcessor; return XML_STATUS_ERROR; - } else { - switch (parser->m_parsingStatus.parsing) { - case XML_SUSPENDED: - result = XML_STATUS_SUSPENDED; - break; - case XML_INITIALIZED: - case XML_PARSING: - if (isFinal) { - parser->m_parsingStatus.parsing = XML_FINISHED; - return XML_STATUS_OK; - } - EXPAT_FALLTHROUGH; - default: - result = XML_STATUS_OK; + } + + // Note: The `uintptr_t` casts intend to address `6.5.8 Relational + // operators` of C99 that says that comparison of pointers that do not + // point into the same array object is undefined behavior. + const bool eventPtrUseful + = isInsideEntity(parser) && parser->m_eventPtr + && (uintptr_t)parser->m_eventPtr >= (uintptr_t)s + && (uintptr_t)parser->m_eventPtr <= (uintptr_t)end; + const size_t eventContextSize + = (eventPtrUseful ? (end - parser->m_eventPtr) : 0); + const size_t eventLen + = (eventPtrUseful + ? EXPAT_SAFE_PTR_DIFF(parser->m_eventEndPtr, parser->m_eventPtr) + : 0); + assert(eventLen <= eventContextSize); + + switch (parser->m_parsingStatus.parsing) { + case XML_SUSPENDED: + result = XML_STATUS_SUSPENDED; + break; + case XML_INITIALIZED: + case XML_PARSING: + if (isFinal) { + parser->m_parsingStatus.parsing = XML_FINISHED; } + EXPAT_FALLTHROUGH; + default: + result = XML_STATUS_OK; } XmlUpdatePosition(parser->m_encoding, parser->m_positionPtr, end, &parser->m_position); + parser->m_positionPtr = end; + nLeftOver = s + len - end; - if (nLeftOver) { - // Back up and restore the parsing status to avoid XML_ERROR_SUSPENDED - // (and XML_ERROR_FINISHED) from XML_GetBuffer. - const enum XML_Parsing originalStatus = parser->m_parsingStatus.parsing; - if (nLeftOver > INT_MAX) { - parser->m_errorCode = XML_ERROR_NO_MEMORY; - parser->m_eventPtr = parser->m_eventEndPtr = NULL; - parser->m_processor = errorProcessor; - return XML_STATUS_ERROR; - } - parser->m_parsingStatus.parsing = XML_PARSING; - void *const temp = XML_GetBuffer(parser, (int)nLeftOver); - parser->m_parsingStatus.parsing = originalStatus; - // GetBuffer may have overwritten this, but we want to remember what the - // app requested, not how many bytes were left over after parsing. - parser->m_lastBufferRequestSize = len; + assert(nLeftOver <= len); + + const size_t contextSize = (XML_CONTEXT_BYTES > eventContextSize) + ? XML_CONTEXT_BYTES + : eventContextSize; + + const size_t bytesToCopy + = (contextSize > len - nLeftOver) ? len : (contextSize + nLeftOver); + assert(bytesToCopy <= len); + + if (bytesToCopy) { + void *const temp = internalGetBuffer64(parser, bytesToCopy); if (temp == NULL) { // NOTE: parser->m_errorCode has already been set by XML_GetBuffer(). parser->m_eventPtr = parser->m_eventEndPtr = NULL; parser->m_processor = errorProcessor; return XML_STATUS_ERROR; } - // Since we know that the buffer was empty and XML_CONTEXT_BYTES is 0, we - // don't have any data to preserve, and can copy straight into the start - // of the buffer rather than the GetBuffer return pointer (which may be - // pointing further into the allocated buffer). - memcpy(parser->m_buffer, end, nLeftOver); + + // Since we know that the buffer was empty and either XML_CONTEXT_BYTES + // is 0 or this is the first parse, we don't have any data to preserve, + // and can copy straight into the start of the buffer rather than the + // XML_GetBuffer return pointer (which may be pointing further into the + // allocated buffer). + const char *const copyFrom = s + (len - bytesToCopy); + memcpy(parser->m_buffer, copyFrom, bytesToCopy); + } else { + // Ensure presence of a buffer so that we can point `parser->m_eventPtr` + // into it to avoid `parser->m_eventPtr == NULL`. + if (! parser->m_buffer) { + if (! internalGetBuffer64(parser, 1)) { + // NOTE: parser->m_errorCode has already been set by XML_GetBuffer(). + parser->m_eventPtr = parser->m_eventEndPtr = NULL; + parser->m_processor = errorProcessor; + return XML_STATUS_ERROR; + } + } } - parser->m_bufferPtr = parser->m_buffer; - parser->m_bufferEnd = EXPAT_SAFE_PTR_ADD(parser->m_buffer, nLeftOver); + assert(parser->m_buffer != NULL); + + parser->m_bufferPtr = parser->m_buffer + (bytesToCopy - nLeftOver); + parser->m_bufferEnd = parser->m_buffer + bytesToCopy; parser->m_positionPtr = parser->m_bufferPtr; parser->m_parseEndPtr = parser->m_bufferEnd; - parser->m_eventPtr = parser->m_bufferPtr; - parser->m_eventEndPtr = parser->m_bufferPtr; + + assert((size_t)(parser->m_bufferPtr - parser->m_buffer) + >= eventContextSize); + parser->m_eventPtr = parser->m_bufferPtr - eventContextSize; + parser->m_eventEndPtr = parser->m_eventPtr + eventLen; + return result; } -#endif /* XML_CONTEXT_BYTES == 0 */ - void *buff = XML_GetBuffer(parser, len); + + void *buff = XML_GetBuffer64(parser, len); if (buff == NULL) return XML_STATUS_ERROR; if (len > 0) { assert(s != NULL); // make sure s==NULL && len!=0 was rejected above memcpy(buff, s, len); } - return XML_ParseBuffer(parser, len, isFinal); + return XML_ParseBuffer64(parser, len, isFinal); +} + +// Advances or sets `parser->m_eventPtr` if (a) it is `NULL` or (b) it points +// into the parse buffer but behind `parser->m_bufferPtr`. +static void +advanceEventPtr(XML_Parser parser) { + if (! parser->m_bufferPtr) + return; + + if (isInsideEntity(parser)) + return; + + // Note: The `uintptr_t` casts intend to address `6.5.8 Relational operators` + // of C99 that says that comparison of pointers that do not point into + // the same array object is undefined behavior. + if (parser->m_eventPtr == NULL + || ((uintptr_t)parser->m_buffer <= (uintptr_t)parser->m_eventPtr + && (uintptr_t)parser->m_eventPtr < (uintptr_t)parser->m_bufferPtr)) { + parser->m_eventPtr = parser->m_bufferPtr; + if (parser->m_eventEndPtr != NULL + && (uintptr_t)parser->m_eventEndPtr < (uintptr_t)parser->m_eventPtr) + parser->m_eventEndPtr = parser->m_bufferPtr; + } } enum XML_Status XMLCALL XML_ParseBuffer(XML_Parser parser, int len, int isFinal) { - const char *start; - enum XML_Status result = XML_STATUS_OK; - if ((parser == NULL) || isCalledFromInsideHandler(parser)) return XML_STATUS_ERROR; @@ -2472,6 +2612,17 @@ XML_ParseBuffer(XML_Parser parser, int len, int isFinal) { return XML_STATUS_ERROR; } + return XML_ParseBuffer64(parser, (size_t)len, isFinal ? XML_TRUE : XML_FALSE); +} + +enum XML_Status XMLCALL +XML_ParseBuffer64(XML_Parser parser, size_t len, XML_Bool isFinal) { + if ((parser == NULL) || isCalledFromInsideHandler(parser)) + return XML_STATUS_ERROR; + + const char *start; + enum XML_Status result = XML_STATUS_OK; + switch (parser->m_parsingStatus.parsing) { case XML_SUSPENDED: parser->m_errorCode = XML_ERROR_SUSPENDED; @@ -2495,7 +2646,8 @@ XML_ParseBuffer(XML_Parser parser, int len, int isFinal) { parser->m_parsingStatus.parsing = XML_PARSING; } - if (len > EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd)) { + if (len + > (size_t)EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd)) { parser->m_errorCode = XML_ERROR_INVALID_ARGUMENT; return XML_STATUS_ERROR; } @@ -2513,7 +2665,7 @@ XML_ParseBuffer(XML_Parser parser, int len, int isFinal) { parser->m_bufferEnd += len; parser->m_parseEndPtr = parser->m_bufferEnd; parser->m_parseEndByteIndex += len; - parser->m_parsingStatus.finalBuffer = (XML_Bool)isFinal; + parser->m_parsingStatus.finalBuffer = isFinal; parser->m_errorCode = callProcessor(parser, start, parser->m_parseEndPtr, &parser->m_bufferPtr); @@ -2522,35 +2674,37 @@ XML_ParseBuffer(XML_Parser parser, int len, int isFinal) { parser->m_eventEndPtr = parser->m_eventPtr; parser->m_processor = errorProcessor; return XML_STATUS_ERROR; - } else { - switch (parser->m_parsingStatus.parsing) { - case XML_SUSPENDED: - result = XML_STATUS_SUSPENDED; - break; - case XML_INITIALIZED: - case XML_PARSING: - if (isFinal) { - parser->m_parsingStatus.parsing = XML_FINISHED; - return result; - } - break; - default:; /* should not happen */ + } + + switch (parser->m_parsingStatus.parsing) { + case XML_SUSPENDED: + result = XML_STATUS_SUSPENDED; + break; + case XML_INITIALIZED: + case XML_PARSING: + if (isFinal) { + parser->m_parsingStatus.parsing = XML_FINISHED; + return result; } + break; + default:; /* should not happen */ } XmlUpdatePosition(parser->m_encoding, parser->m_positionPtr, parser->m_bufferPtr, &parser->m_position); parser->m_positionPtr = parser->m_bufferPtr; + advanceEventPtr(parser); return result; } /* Modifies `parser`’s buffer to be backed by `newBuf`. */ static void -setParserBuffer(XML_Parser parser, char *newBuf, int newBufSize, int keep) { +setParserBuffer(XML_Parser parser, char *newBuf, size_t newBufSize, + size_t keep) { parser->m_bufferLim = newBuf + newBufSize; if (parser->m_bufferPtr) { - const int parsing - = (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr); + const size_t parsing + = EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr); memcpy(newBuf, parser->m_bufferPtr - keep, parsing + keep); // NOTE: We are avoiding FREE(..) here because parser->m_buffer // is not being allocated with MALLOC(..) but with plain @@ -2571,10 +2725,20 @@ void *XMLCALL XML_GetBuffer(XML_Parser parser, int len) { if ((parser == NULL) || isCalledFromInsideHandler(parser)) return NULL; + if (len < 0) { parser->m_errorCode = XML_ERROR_NO_MEMORY; return NULL; } + + return XML_GetBuffer64(parser, (size_t)len); +} + +void *XMLCALL +XML_GetBuffer64(XML_Parser parser, size_t len) { + if ((parser == NULL) || isCalledFromInsideHandler(parser)) + return NULL; + switch (parser->m_parsingStatus.parsing) { case XML_SUSPENDED: parser->m_errorCode = XML_ERROR_SUSPENDED; @@ -2588,37 +2752,49 @@ XML_GetBuffer(XML_Parser parser, int len) { // whether or not the request succeeds, `len` seems to be the app's preferred // buffer fill size; remember it. parser->m_lastBufferRequestSize = len; - if (len > EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd) + + return internalGetBuffer64(parser, len); +} + +// Like `XML_GetBuffer*` but: +// - without checking parser status +// - without recording `len` to `parser->m_lastBufferRequestSize` +static void * +internalGetBuffer64(XML_Parser parser, size_t len) { + assert(parser != NULL); + + if (len > (size_t)EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, + parser->m_bufferEnd) || parser->m_buffer == NULL) { /* Do not invoke signed arithmetic overflow: */ - int neededSize = (int)((unsigned)len - + (unsigned)EXPAT_SAFE_PTR_DIFF( - parser->m_bufferEnd, parser->m_bufferPtr)); - if (neededSize < 0) { + const size_t leftUnparsed + = EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr); + if (len > SIZE_MAX - leftUnparsed) { parser->m_errorCode = XML_ERROR_NO_MEMORY; return NULL; } + size_t neededSize = len + leftUnparsed; #if XML_CONTEXT_BYTES > 0 - const int parsed - = (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffer); - int keep = parsed; + const size_t parsed + = EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffer); + size_t keep = parsed; if (keep > XML_CONTEXT_BYTES) keep = XML_CONTEXT_BYTES; /* Detect and prevent integer overflow */ - if (keep > INT_MAX - neededSize) { + if (keep > SIZE_MAX - neededSize) { parser->m_errorCode = XML_ERROR_NO_MEMORY; return NULL; } #else - int keep = 0; + size_t keep = 0; #endif /* XML_CONTEXT_BYTES > 0 */ neededSize += keep; if (parser->m_buffer && parser->m_bufferPtr - && neededSize - <= EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_buffer)) { + && neededSize <= (size_t)EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, + parser->m_buffer)) { #if XML_CONTEXT_BYTES > 0 if (keep < parsed) { - int offset = parsed - keep; + size_t offset = parsed - keep; /* The buffer pointers cannot be NULL here; we have at least some bytes * in the buffer */ memmove(parser->m_buffer, &parser->m_buffer[offset], @@ -2635,15 +2811,13 @@ XML_GetBuffer(XML_Parser parser, int len) { parser->m_bufferPtr = parser->m_buffer; #endif /* XML_CONTEXT_BYTES > 0 */ } else { - int bufferSize - = (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_buffer); + size_t bufferSize + = EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_buffer); if (bufferSize == 0) bufferSize = INIT_BUFFER_SIZE; - do { - /* Do not invoke signed arithmetic overflow: */ - bufferSize = (int)(2U * (unsigned)bufferSize); - } while (bufferSize < neededSize && bufferSize > 0); - if (bufferSize <= 0) { + while (bufferSize < neededSize && bufferSize <= SIZE_MAX / 2) + bufferSize *= 2; + if (bufferSize < neededSize) { parser->m_errorCode = XML_ERROR_NO_MEMORY; return NULL; } @@ -2722,25 +2896,26 @@ XML_ResumeParser(XML_Parser parser) { parser->m_eventEndPtr = parser->m_eventPtr; parser->m_processor = errorProcessor; return XML_STATUS_ERROR; - } else { - switch (parser->m_parsingStatus.parsing) { - case XML_SUSPENDED: - result = XML_STATUS_SUSPENDED; - break; - case XML_INITIALIZED: - case XML_PARSING: - if (parser->m_parsingStatus.finalBuffer) { - parser->m_parsingStatus.parsing = XML_FINISHED; - return result; - } - break; - default:; + } + + switch (parser->m_parsingStatus.parsing) { + case XML_SUSPENDED: + result = XML_STATUS_SUSPENDED; + break; + case XML_INITIALIZED: + case XML_PARSING: + if (parser->m_parsingStatus.finalBuffer) { + parser->m_parsingStatus.parsing = XML_FINISHED; + return result; } + break; + default:; } XmlUpdatePosition(parser->m_encoding, parser->m_positionPtr, parser->m_bufferPtr, &parser->m_position); parser->m_positionPtr = parser->m_bufferPtr; + advanceEventPtr(parser); return result; } @@ -2803,7 +2978,7 @@ XML_GetCurrentByteCount(XML_Parser parser) { const char *XMLCALL XML_GetInputContext64(XML_Parser parser, int64_t *offset, uint64_t *size) { #if XML_CONTEXT_BYTES > 0 - if (parser == NULL) + if (parser == NULL || ! isCalledFromInsideHandler(parser)) return NULL; if (parser->m_eventPtr && parser->m_buffer) { if (offset != NULL) @@ -2824,7 +2999,7 @@ XML_GetInputContext64(XML_Parser parser, int64_t *offset, uint64_t *size) { const char *XMLCALL XML_GetInputContext(XML_Parser parser, int *offset, int *size) { #if XML_CONTEXT_BYTES > 0 - if (parser == NULL) + if (parser == NULL || ! isCalledFromInsideHandler(parser)) return NULL; int64_t offset64; @@ -3671,9 +3846,7 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, *eventEndPP = end; if (parser->m_characterDataHandler) { XML_Char c = 0xA; - beforeHandler(parser); - parser->m_characterDataHandler(parser->m_handlerArg, &c, 1); - afterHandler(parser); + reportCharacterData(parser, &c, 1); } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, end); /* We are at the end of the final buffer, should we check for @@ -3727,9 +3900,7 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, XML_ACCOUNT_ENTITY_EXPANSION); #endif /* XML_GE == 1 */ if (parser->m_characterDataHandler) { - beforeHandler(parser); - parser->m_characterDataHandler(parser->m_handlerArg, &ch, 1); - afterHandler(parser); + reportCharacterData(parser, &ch, 1); } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, next); break; @@ -3919,7 +4090,7 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, if (parser->m_tagLevel == startTagLevel) return XML_ERROR_ASYNC_ENTITY; else { - int len; + size_t len; const char *rawName; TAG *tag = parser->m_tagStack; rawName = s + enc->minBytesPerChar * 2; @@ -3990,10 +4161,8 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, return XML_ERROR_BAD_CHAR_REF; if (parser->m_characterDataHandler) { XML_Char buf[XML_ENCODE_MAX]; - beforeHandler(parser); - parser->m_characterDataHandler(parser->m_handlerArg, buf, - XmlEncode(n, (ICHAR *)buf)); - afterHandler(parser); + const size_t len = XmlEncode(n, (ICHAR *)buf); + reportCharacterData(parser, buf, len); } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, next); } break; @@ -4002,9 +4171,7 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, case XML_TOK_DATA_NEWLINE: if (parser->m_characterDataHandler) { XML_Char c = 0xA; - beforeHandler(parser); - parser->m_characterDataHandler(parser->m_handlerArg, &c, 1); - afterHandler(parser); + reportCharacterData(parser, &c, 1); } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, next); break; @@ -4028,10 +4195,7 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, easier to let the user deal with this. */ } else if ((0) && parser->m_characterDataHandler) { - beforeHandler(parser); - parser->m_characterDataHandler(parser->m_handlerArg, parser->m_dataBuf, - 0); - afterHandler(parser); + reportCharacterData(parser, parser->m_dataBuf, 0); /* END disabled code */ } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, next); @@ -4053,17 +4217,11 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, if (MUST_CONVERT(enc, s)) { ICHAR *dataPtr = (ICHAR *)parser->m_dataBuf; XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)parser->m_dataBufEnd); - beforeHandler(parser); - parser->m_characterDataHandler( - parser->m_handlerArg, parser->m_dataBuf, - (int)(dataPtr - (ICHAR *)parser->m_dataBuf)); - afterHandler(parser); + reportCharacterData(parser, parser->m_dataBuf, + dataPtr - (ICHAR *)parser->m_dataBuf); } else { - beforeHandler(parser); - parser->m_characterDataHandler( - parser->m_handlerArg, (const XML_Char *)s, - (int)((const XML_Char *)end - (const XML_Char *)s)); - afterHandler(parser); + reportCharacterData(parser, (const XML_Char *)s, + (const XML_Char *)end - (const XML_Char *)s); } } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, end); @@ -4081,8 +4239,7 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, *nextPtr = end; return XML_ERROR_NONE; case XML_TOK_DATA_CHARS: { - XML_CharacterDataHandler charDataHandler = parser->m_characterDataHandler; - if (charDataHandler) { + if (parser->m_characterDataHandler) { if (MUST_CONVERT(enc, s)) { for (;;) { ICHAR *dataPtr = (ICHAR *)parser->m_dataBuf; @@ -4090,10 +4247,8 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, enc, &s, next, &dataPtr, (ICHAR *)parser->m_dataBufEnd); *eventEndPP = s; if (parser->m_characterDataHandler) { - beforeHandler(parser); - charDataHandler(parser->m_handlerArg, parser->m_dataBuf, - (int)(dataPtr - (ICHAR *)parser->m_dataBuf)); - afterHandler(parser); + reportCharacterData(parser, parser->m_dataBuf, + dataPtr - (ICHAR *)parser->m_dataBuf); } if ((convert_res == XML_CONVERT_COMPLETED) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE)) @@ -4101,10 +4256,8 @@ doContent(XML_Parser parser, int startTagLevel, const ENCODING *enc, *eventPP = s; } } else { - beforeHandler(parser); - charDataHandler(parser->m_handlerArg, (const XML_Char *)s, - (int)((const XML_Char *)next - (const XML_Char *)s)); - afterHandler(parser); + reportCharacterData(parser, (const XML_Char *)s, + (const XML_Char *)next - (const XML_Char *)s); } } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, next); @@ -4215,10 +4368,6 @@ storeAtts(XML_Parser parser, const ENCODING *enc, const char *attStr, } const size_t nDefaultAtts = elementType->nDefaultAtts; - /* Detect and prevent integer overflow. */ - if (parser->m_attsSize > (size_t)INT_MAX) - return XML_ERROR_NO_MEMORY; - /* get the attributes from the tokenizer */ size_t n = XmlGetAttributes(enc, attStr, parser->m_attsSize, parser->m_atts); @@ -4252,9 +4401,6 @@ storeAtts(XML_Parser parser, const ENCODING *enc, const char *attStr, } parser->m_atts = temp; if (n > oldAttsSize) { - /* Detect and prevent integer overflow. */ - if (n > (size_t)INT_MAX) - return XML_ERROR_NO_MEMORY; XmlGetAttributes(enc, attStr, n, parser->m_atts); } } @@ -4384,7 +4530,7 @@ storeAtts(XML_Parser parser, const ENCODING *enc, const char *attStr, } } } - appAtts[attIndex] = 0; + appAtts[attIndex] = NULL; /* expand prefixed attribute names, check for duplicates, and clear flags that say whether attributes were specified */ @@ -4961,10 +5107,7 @@ doCdataSection(XML_Parser parser, const ENCODING *enc, const char **startPtr, /* BEGIN disabled code */ /* see comment under XML_TOK_CDATA_SECT_OPEN */ else if ((0) && parser->m_characterDataHandler) { - beforeHandler(parser); - parser->m_characterDataHandler(parser->m_handlerArg, parser->m_dataBuf, - 0); - afterHandler(parser); + reportCharacterData(parser, parser->m_dataBuf, 0); /* END disabled code */ } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, next); @@ -4977,35 +5120,30 @@ doCdataSection(XML_Parser parser, const ENCODING *enc, const char **startPtr, case XML_TOK_DATA_NEWLINE: if (parser->m_characterDataHandler) { XML_Char c = 0xA; - beforeHandler(parser); - parser->m_characterDataHandler(parser->m_handlerArg, &c, 1); - afterHandler(parser); + reportCharacterData(parser, &c, 1); } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, next); break; case XML_TOK_DATA_CHARS: { - XML_CharacterDataHandler charDataHandler = parser->m_characterDataHandler; - if (charDataHandler) { + if (parser->m_characterDataHandler) { if (MUST_CONVERT(enc, s)) { for (;;) { ICHAR *dataPtr = (ICHAR *)parser->m_dataBuf; const enum XML_Convert_Result convert_res = XmlConvert( enc, &s, next, &dataPtr, (ICHAR *)parser->m_dataBufEnd); *eventEndPP = next; - beforeHandler(parser); - charDataHandler(parser->m_handlerArg, parser->m_dataBuf, - (int)(dataPtr - (ICHAR *)parser->m_dataBuf)); - afterHandler(parser); + if (parser->m_characterDataHandler) { + reportCharacterData(parser, parser->m_dataBuf, + dataPtr - (ICHAR *)parser->m_dataBuf); + } if ((convert_res == XML_CONVERT_COMPLETED) || (convert_res == XML_CONVERT_INPUT_INCOMPLETE)) break; *eventPP = s; } } else { - beforeHandler(parser); - charDataHandler(parser->m_handlerArg, (const XML_Char *)s, - (int)((const XML_Char *)next - (const XML_Char *)s)); - afterHandler(parser); + reportCharacterData(parser, (const XML_Char *)s, + (const XML_Char *)next - (const XML_Char *)s); } } else if (parser->m_defaultHandler) reportDefault(parser, enc, s, next); @@ -5792,7 +5930,7 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, dtd->paramEntityRead = false; beforeHandler(parser); const int status = parser->m_externalEntityRefHandler( - parser->m_externalEntityRefHandlerArg, 0, entity->base, + parser->m_externalEntityRefHandlerArg, NULL, entity->base, entity->systemId, entity->publicId); afterHandler(parser); if (! status) @@ -5842,7 +5980,7 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, dtd->paramEntityRead = false; beforeHandler(parser); const int status = parser->m_externalEntityRefHandler( - parser->m_externalEntityRefHandlerArg, 0, entity->base, + parser->m_externalEntityRefHandlerArg, NULL, entity->base, entity->systemId, entity->publicId); afterHandler(parser); if (! status) @@ -5935,7 +6073,7 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, if (! defineAttribute(parser->m_declElementType, parser->m_declAttributeId, parser->m_declAttributeIsCdata, - parser->m_declAttributeIsId, 0, parser)) + parser->m_declAttributeIsId, NULL, parser)) return XML_ERROR_NO_MEMORY; if (parser->m_attlistDeclHandler && parser->m_declAttributeType) { if (*parser->m_declAttributeType == XML_T(ASCII_LPAREN) @@ -5952,8 +6090,8 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, beforeHandler(parser); parser->m_attlistDeclHandler( parser->m_handlerArg, parser->m_declElementType->name, - parser->m_declAttributeId->name, parser->m_declAttributeType, 0, - role == XML_ROLE_REQUIRED_ATTRIBUTE_VALUE); + parser->m_declAttributeId->name, parser->m_declAttributeType, + NULL, role == XML_ROLE_REQUIRED_ATTRIBUTE_VALUE); afterHandler(parser); handleDefault = XML_FALSE; } @@ -6013,11 +6151,6 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, return result; } if (parser->m_declEntity) { - /* Detect and prevent signed integer overflow */ - if ((size_t)poolLength(&dtd->entityValuePool) > (size_t)INT_MAX) { - poolDiscard(&dtd->entityValuePool); - return XML_ERROR_NO_MEMORY; - } const XML_Char *const valueStart = poolStart(&dtd->entityValuePool); parser->m_declEntity->textPtr = valueStart; parser->m_declEntity->textLen = poolLength(&dtd->entityValuePool); @@ -6025,11 +6158,15 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, poolFinish(&dtd->entityValuePool); if (parser->m_entityDeclHandler) { *eventEndPP = s; + // Check if follow-up cast to (int) would be truncating + if (parser->m_declEntity->textLen > INT_MAX) + return XML_ERROR_NO_MEMORY; beforeHandler(parser); parser->m_entityDeclHandler( parser->m_handlerArg, parser->m_declEntity->name, parser->m_declEntity->is_param, parser->m_declEntity->textPtr, - (int)parser->m_declEntity->textLen, parser->m_curBase, 0, 0, 0); + (int)parser->m_declEntity->textLen, parser->m_curBase, NULL, + NULL, NULL); afterHandler(parser); handleDefault = XML_FALSE; } @@ -6046,11 +6183,15 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, if (parser->m_entityDeclHandler) { *eventEndPP = s; + // Check if follow-up cast to (int) would be truncating + if (parser->m_declEntity->textLen > INT_MAX) + return XML_ERROR_NO_MEMORY; beforeHandler(parser); parser->m_entityDeclHandler( parser->m_handlerArg, parser->m_declEntity->name, parser->m_declEntity->is_param, parser->m_declEntity->textPtr, - (int)parser->m_declEntity->textLen, parser->m_curBase, 0, 0, 0); + (int)parser->m_declEntity->textLen, parser->m_curBase, NULL, + NULL, NULL); afterHandler(parser); handleDefault = XML_FALSE; } @@ -6134,8 +6275,9 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, beforeHandler(parser); parser->m_entityDeclHandler( parser->m_handlerArg, parser->m_declEntity->name, - parser->m_declEntity->is_param, 0, 0, parser->m_declEntity->base, - parser->m_declEntity->systemId, parser->m_declEntity->publicId, 0); + parser->m_declEntity->is_param, NULL, 0, parser->m_declEntity->base, + parser->m_declEntity->systemId, parser->m_declEntity->publicId, + NULL); afterHandler(parser); handleDefault = XML_FALSE; } @@ -6160,7 +6302,7 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, *eventEndPP = s; beforeHandler(parser); parser->m_entityDeclHandler( - parser->m_handlerArg, parser->m_declEntity->name, 0, 0, 0, + parser->m_handlerArg, parser->m_declEntity->name, 0, NULL, 0, parser->m_declEntity->base, parser->m_declEntity->systemId, parser->m_declEntity->publicId, parser->m_declEntity->notation); afterHandler(parser); @@ -6353,19 +6495,19 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, } parser->m_groupConnector[parser->m_prologState.level] = 0; if (dtd->in_eldecl) { - int myindex = nextScaffoldPart(parser); + expat_ssize_t myindex = nextScaffoldPart(parser); if (myindex < 0) return XML_ERROR_NO_MEMORY; assert(dtd->scaffIndex != NULL); - if ((size_t)dtd->scaffLevel >= dtd->scaffIndexSize) { + if (dtd->scaffLevel >= dtd->scaffIndexSize) { /* Detect and prevent integer overflow */ - if (dtd->scaffIndexSize > SIZE_MAX / 2 / sizeof(int)) { + if (dtd->scaffIndexSize > SIZE_MAX / 2 / sizeof(size_t)) { return XML_ERROR_NO_MEMORY; } assert(dtd->scaffIndexSize > 0); const size_t new_size = dtd->scaffIndexSize * 2; - int *const new_scaff_index - = REALLOC(parser, dtd->scaffIndex, new_size * sizeof(int)); + size_t *const new_scaff_index + = REALLOC(parser, dtd->scaffIndex, new_size * sizeof(size_t)); if (new_scaff_index == NULL) { return XML_ERROR_NO_MEMORY; } @@ -6476,7 +6618,7 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, entityTrackingOnOpen(parser, entity, __LINE__); beforeHandler(parser); const int status = parser->m_externalEntityRefHandler( - parser->m_externalEntityRefHandlerArg, 0, entity->base, + parser->m_externalEntityRefHandlerArg, NULL, entity->base, entity->systemId, entity->publicId); afterHandler(parser); if (! status) { @@ -6575,7 +6717,7 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, size_t nameLen; const char *nxt = (quant == XML_CQUANT_NONE ? next : next - enc->minBytesPerChar); - int myindex = nextScaffoldPart(parser); + expat_ssize_t myindex = nextScaffoldPart(parser); if (myindex < 0) return XML_ERROR_NO_MEMORY; dtd->scaffold[myindex].type = XML_CTYPE_NAME; @@ -6588,11 +6730,11 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end, nameLen = xcslen(name) + /*null terminator*/ 1; /* Detect and prevent integer overflow */ - if (nameLen > UINT_MAX - dtd->contentStringLen) { + if (nameLen > SIZE_MAX - dtd->contentStringLen) { return XML_ERROR_NO_MEMORY; } - dtd->contentStringLen += (unsigned)nameLen; + dtd->contentStringLen += nameLen; if (parser->m_elementDeclHandler) handleDefault = XML_FALSE; } @@ -7717,7 +7859,7 @@ storeEntityValue(XML_Parser parser, const ENCODING *enc, entityTrackingOnOpen(parser, entity, __LINE__); beforeHandler(parser); const int status = parser->m_externalEntityRefHandler( - parser->m_externalEntityRefHandlerArg, 0, entity->base, + parser->m_externalEntityRefHandlerArg, NULL, entity->base, entity->systemId, entity->publicId); afterHandler(parser); if (! status) { @@ -7911,11 +8053,6 @@ storeSelfEntityValue(XML_Parser parser, ENTITY *entity) { return XML_ERROR_NO_MEMORY; } - /* Detect and prevent signed integer overflow */ - if ((size_t)poolLength(pool) > (size_t)INT_MAX) { - poolDiscard(pool); - return XML_ERROR_NO_MEMORY; - } const XML_Char *const valueStart = poolStart(pool); entity->textPtr = valueStart; entity->textLen = poolLength(pool); @@ -7998,6 +8135,22 @@ reportComment(XML_Parser parser, const ENCODING *enc, const char *start, return 1; } +static void +callDefaultHandler(XML_Parser parser, const XML_Char *s, size_t len) { + assert(parser->m_defaultHandler != NULL); + beforeHandler(parser); + while (parser->m_defaultHandler && len > INT_MAX) { + parser->m_defaultHandler(parser->m_handlerArg, s, INT_MAX); + s += INT_MAX; + len -= INT_MAX; + } + if (parser->m_defaultHandler) { + assert(len <= INT_MAX); + parser->m_defaultHandler(parser->m_handlerArg, s, (int)len); + } + afterHandler(parser); +} + static void reportDefault(XML_Parser parser, const ENCODING *enc, const char *s, const char *end) { @@ -8034,22 +8187,33 @@ reportDefault(XML_Parser parser, const ENCODING *enc, const char *s, convert_res = XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)parser->m_dataBufEnd); *eventEndPP = s; - beforeHandler(parser); - parser->m_defaultHandler(parser->m_handlerArg, parser->m_dataBuf, - (int)(dataPtr - (ICHAR *)parser->m_dataBuf)); - afterHandler(parser); + callDefaultHandler(parser, parser->m_dataBuf, + dataPtr - (ICHAR *)parser->m_dataBuf); *eventPP = s; - } while ((convert_res != XML_CONVERT_COMPLETED) + } while (parser->m_defaultHandler && (convert_res != XML_CONVERT_COMPLETED) && (convert_res != XML_CONVERT_INPUT_INCOMPLETE)); } else { - beforeHandler(parser); - parser->m_defaultHandler( - parser->m_handlerArg, (const XML_Char *)s, - (int)((const XML_Char *)end - (const XML_Char *)s)); - afterHandler(parser); + callDefaultHandler(parser, (const XML_Char *)s, + (const XML_Char *)end - (const XML_Char *)s); } } +static void +reportCharacterData(XML_Parser parser, const XML_Char *s, size_t len) { + assert(parser->m_characterDataHandler != NULL); + beforeHandler(parser); + while (parser->m_characterDataHandler && len > INT_MAX) { + parser->m_characterDataHandler(parser->m_handlerArg, s, INT_MAX); + s += INT_MAX; + len -= INT_MAX; + } + if (parser->m_characterDataHandler) { + assert(len <= INT_MAX); + parser->m_characterDataHandler(parser->m_handlerArg, s, (int)len); + } + afterHandler(parser); +} + static int defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *attId, bool isCdata, bool isId, const XML_Char *value, XML_Parser parser) { @@ -8179,12 +8343,8 @@ getAttributeId(XML_Parser parser, const ENCODING *enc, const char *start, sizeof(PREFIX)); id->xmlns = true; } else { - int i; + size_t i; for (i = 0; name[i]; i++) { - /* Detect and prevent signed integer overflow */ - if (i == INT_MAX) { - return NULL; - } /* attributes without prefix are *not* in the default namespace */ if (name[i] == XML_T(ASCII_COLON)) { if (! poolAppendChars(&dtd->pool, name, i)) @@ -8781,13 +8941,12 @@ lookupWithLength(XML_Parser parser, HASH_TABLE *table, KEY name, size_t nameLen, size_t createSize) { size_t i; if (table->size == 0) { - size_t tsize; if (! createSize) return NULL; table->power = INIT_POWER; /* table->size is a power of 2 */ table->size = (size_t)1 << INIT_POWER; - tsize = table->size * sizeof(NAMED *); + const size_t tsize = table->size * sizeof(NAMED *); table->v = MALLOC(table->parser, tsize); if (! table->v) { table->size = 0; @@ -8798,9 +8957,8 @@ lookupWithLength(XML_Parser parser, HASH_TABLE *table, KEY name, size_t nameLen, } else { unsigned long h = hash(parser, name, nameLen); unsigned long mask = (unsigned long)table->size - 1; - unsigned char step = 0; i = h & mask; - while (table->v[i]) { + for (unsigned char step = 0; table->v[i];) { if (keyeq(name, nameLen, table->v[i]->name)) return table->v[i]; if (! step) @@ -8827,7 +8985,7 @@ lookupWithLength(XML_Parser parser, HASH_TABLE *table, KEY name, size_t nameLen, return NULL; } - size_t tsize = newSize * sizeof(NAMED *); + const size_t tsize = newSize * sizeof(NAMED *); NAMED **newV = MALLOC(table->parser, tsize); if (! newV) return NULL; @@ -8837,8 +8995,7 @@ lookupWithLength(XML_Parser parser, HASH_TABLE *table, KEY name, size_t nameLen, KEY const key = table->v[i]->name; unsigned long newHash = hash(parser, key, keylen(key)); size_t j = newHash & newMask; - step = 0; - while (newV[j]) { + for (unsigned char step = 0; newV[j];) { if (! step) step = PROBE_STEP(newHash, newMask, newPower); j < step ? (j += newSize - step) : (j -= step); @@ -8850,8 +9007,7 @@ lookupWithLength(XML_Parser parser, HASH_TABLE *table, KEY name, size_t nameLen, table->power = newPower; table->size = newSize; i = h & newMask; - step = 0; - while (table->v[i]) { + for (unsigned char step = 0; table->v[i];) { if (! step) step = PROBE_STEP(h, newMask, newPower); i < step ? (i += newSize - step) : (i -= step); @@ -9060,7 +9216,7 @@ poolStoreString(STRING_POOL *pool, const ENCODING *enc, const char *ptr, } static size_t -poolBytesToAllocateFor(int blockSize) { +poolBytesToAllocateFor(size_t blockSize) { /* Unprotected math would be: ** return offsetof(BLOCK, s) + blockSize * sizeof(XML_Char); ** @@ -9070,21 +9226,19 @@ poolBytesToAllocateFor(int blockSize) { */ const size_t stretch = sizeof(XML_Char); /* can be 4 bytes */ - if (blockSize <= 0) + // Detect integer overflow + if (blockSize > SIZE_MAX / stretch) return 0; - if (blockSize > (int)(INT_MAX / stretch)) + const size_t stretchedBlockSize = blockSize * stretch; + + // Detect integer overflow + if (offsetof(BLOCK, s) > SIZE_MAX - stretchedBlockSize) return 0; - { - const int stretchedBlockSize = blockSize * (int)stretch; - const int bytesToAllocate - = (int)(offsetof(BLOCK, s) + (unsigned)stretchedBlockSize); - if (bytesToAllocate < 0) - return 0; + const size_t bytesToAllocate = offsetof(BLOCK, s) + stretchedBlockSize; - return (size_t)bytesToAllocate; - } + return bytesToAllocate; } static XML_Bool @@ -9099,7 +9253,7 @@ poolGrow(STRING_POOL *pool) { pool->ptr = pool->start; return XML_TRUE; } - if (pool->end - pool->start < pool->freeBlocks->size) { + if ((size_t)(pool->end - pool->start) < pool->freeBlocks->size) { BLOCK *tem = pool->freeBlocks->next; pool->freeBlocks->next = pool->blocks; pool->blocks = pool->freeBlocks; @@ -9114,7 +9268,11 @@ poolGrow(STRING_POOL *pool) { } if (pool->blocks && pool->start == pool->blocks->s) { BLOCK *temp; - int blockSize = (int)((unsigned)(pool->end - pool->start) * 2U); + const size_t len = pool->end - pool->start; + // Detect integer overflow + if (len > SIZE_MAX / 2) + return XML_FALSE; /* LCOV_EXCL_LINE */ + const size_t blockSize = len * 2; size_t bytesToAllocate; /* NOTE: Needs to be calculated prior to calling `realloc` @@ -9122,16 +9280,6 @@ poolGrow(STRING_POOL *pool) { const ptrdiff_t offsetInsideBlock = EXPAT_SAFE_PTR_DIFF(pool->ptr, pool->start); - if (blockSize < 0) { - /* This condition traps a situation where either more than - * INT_MAX/2 bytes have already been allocated. This isn't - * readily testable, since it is unlikely that an average - * machine will have that much memory, so we exclude it from the - * coverage statistics. - */ - return XML_FALSE; /* LCOV_EXCL_LINE */ - } - bytesToAllocate = poolBytesToAllocateFor(blockSize); if (bytesToAllocate == 0) return XML_FALSE; @@ -9146,29 +9294,15 @@ poolGrow(STRING_POOL *pool) { pool->end = pool->start + blockSize; } else { BLOCK *tem; - int blockSize = (int)(pool->end - pool->start); + size_t blockSize = pool->end - pool->start; size_t bytesToAllocate; - if (blockSize < 0) { - /* This condition traps a situation where either more than - * INT_MAX bytes have already been allocated (which is prevented - * by various pieces of program logic, not least this one, never - * mind the unlikelihood of actually having that much memory) or - * the pool control fields have been corrupted (which could - * conceivably happen in an extremely buggy user handler - * function). Either way it isn't readily testable, so we - * exclude it from the coverage statistics. - */ - return XML_FALSE; /* LCOV_EXCL_LINE */ - } - if (blockSize < INIT_BLOCK_SIZE) blockSize = INIT_BLOCK_SIZE; else { - /* Detect overflow, avoiding _signed_ overflow undefined behavior */ - if ((int)((unsigned)blockSize * 2U) < 0) { - return XML_FALSE; - } + // Detect integer overflow + if (blockSize > SIZE_MAX / 2) + return XML_FALSE; /* LCOV_EXCL_LINE */ blockSize *= 2; } @@ -9205,34 +9339,29 @@ poolGrowUntil(STRING_POOL *pool, size_t needed) { } } -static int +static expat_ssize_t nextScaffoldPart(XML_Parser parser) { DTD *const dtd = parser->m_dtd; /* save one level of indirection */ CONTENT_SCAFFOLD *me; - int next; + expat_ssize_t next; if (! dtd->scaffIndex) { /* Detect and prevent integer overflow. */ - if (parser->m_groupSize > SIZE_MAX / sizeof(int)) { + if (parser->m_groupSize > SIZE_MAX / sizeof(size_t)) { return -1; } - dtd->scaffIndex = MALLOC(parser, parser->m_groupSize * sizeof(int)); + dtd->scaffIndex = MALLOC(parser, parser->m_groupSize * sizeof(size_t)); if (! dtd->scaffIndex) return -1; dtd->scaffIndexSize = parser->m_groupSize; dtd->scaffIndex[0] = 0; } - // Will casting to int be safe further down? - if (dtd->scaffCount > INT_MAX) { - return -1; - } - if (dtd->scaffCount >= dtd->scaffSize) { CONTENT_SCAFFOLD *temp; if (dtd->scaffold) { /* Detect and prevent integer overflow */ - if (dtd->scaffSize > UINT_MAX / 2u) { + if (dtd->scaffSize > SIZE_MAX / 2u) { return -1; } /* Detect and prevent integer overflow. @@ -9258,7 +9387,10 @@ nextScaffoldPart(XML_Parser parser) { } dtd->scaffold = temp; } - next = (int)dtd->scaffCount++; + if (dtd->scaffCount > EXPAT_SSIZE_MAX) { + return -1; + } + next = (expat_ssize_t)dtd->scaffCount++; me = &dtd->scaffold[next]; if (dtd->scaffLevel) { CONTENT_SCAFFOLD *parent @@ -9371,7 +9503,7 @@ build_model(XML_Parser parser) { for (; dest < destLimit; dest++) { /* Retrieve source tree array index from job storage */ - const int src_node = (int)dest->numchildren; + const size_t src_node = dest->numchildren; /* Convert item */ dest->type = dtd->scaffold[src_node].type; @@ -9399,15 +9531,24 @@ build_model(XML_Parser parser) { dest->children = NULL; } else { unsigned int i; - int cn; + size_t cn; + if (dtd->scaffold[src_node].childcnt > UINT_MAX) { + parser->m_mem.free_fcn(ret); + return NULL; + } dest->name = NULL; dest->numchildren = dtd->scaffold[src_node].childcnt; dest->children = jobDest; /* Append scaffold indices of children to array */ for (i = 0, cn = dtd->scaffold[src_node].firstchild; - i < dest->numchildren; i++, cn = dtd->scaffold[cn].nextsib) + i < dest->numchildren; i++, cn = dtd->scaffold[cn].nextsib) { + if (cn > UINT_MAX) { + parser->m_mem.free_fcn(ret); + return NULL; + } (jobDest++)->numchildren = (unsigned int)cn; + } } } @@ -9489,7 +9630,7 @@ accountingReportStats(XML_Parser originParser, const char *epilog, const XML_Parser rootParser = getRootParserOf(originParser, NULL); assert(! rootParser->m_parentParser); - if (rootParser->m_accounting.debugLevel == 0u) { + if (rootParser->m_accounting.debugLevel == 0) { return; } @@ -9540,7 +9681,7 @@ accountingReportDiff(XML_Parser rootParser, /* Note: Performance is of no concern here */ const char *walker = before; - if ((rootParser->m_accounting.debugLevel >= 3u) + if ((rootParser->m_accounting.debugLevel >= 3) || (after - before) <= (ptrdiff_t)(contextLength + ellipsisLength + contextLength)) { for (; walker < after; walker++) { @@ -9624,7 +9765,7 @@ accountingDiffTolerated(XML_Parser originParser, int tok, const char *before, const XML_Bool tolerated = accountingCurrentAmplificationTolerated(rootParser, 0); - if (rootParser->m_accounting.debugLevel >= 2u) { + if (rootParser->m_accounting.debugLevel >= 2) { accountingReportStats(rootParser, "", 0, 0); accountingReportDiff(rootParser, levelsAwayFromRootParser, before, after, bytesMore, source_line, account); @@ -9651,7 +9792,7 @@ static void entityTrackingReportStats(XML_Parser rootParser, ENTITY *entity, const char *action, int sourceLine) { assert(! rootParser->m_parentParser); - if (rootParser->m_entity_stats.debugLevel == 0u) + if (rootParser->m_entity_stats.debugLevel == 0) return; # if defined(XML_UNICODE) diff --git a/Modules/expat/xmltok.c b/Modules/expat/xmltok.c index 2f837acc4cbf72..e2c114a5229bb7 100644 --- a/Modules/expat/xmltok.c +++ b/Modules/expat/xmltok.c @@ -54,6 +54,7 @@ #include "expat_config.h" +#include #include #include /* memcpy */ #include @@ -137,12 +138,8 @@ : ((p)[1] & 0x80) == 0 \ || ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0))) -static int -isNever(const ENCODING *enc, const char *p) { - UNUSED_P(enc); - UNUSED_P(p); - return 0; -} +static const uint8_t b_0000_0111 = 0x07; +static const uint8_t b_0011_1111 = 0x3F; static int utf8_isName2(const ENCODING *enc, const char *p) { @@ -156,7 +153,7 @@ utf8_isName3(const ENCODING *enc, const char *p) { return UTF8_GET_NAMING3(namePages, (const unsigned char *)p); } -#define utf8_isName4 isNever +#define utf8_isName4 utf8_isNmstrt4 static int utf8_isNmstrt2(const ENCODING *enc, const char *p) { @@ -170,7 +167,18 @@ utf8_isNmstrt3(const ENCODING *enc, const char *p) { return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p); } -#define utf8_isNmstrt4 isNever +static int +utf8_isNmstrt4(const ENCODING *enc, const char *p) { + UNUSED_P(enc); + + // This is a four-byte-unit UTF-8 decoder but: + // - We only need the two upper bytes for the check below + // - Malformed continuation bytes have been rejected before + const unsigned codepoint + = ((p[0] & b_0000_0111) << 18) | ((p[1] & b_0011_1111) << 12); + + return ((codepoint >= 0x10000) && (codepoint <= 0xEFFFF)); +} static int utf8_isInvalid2(const ENCODING *enc, const char *p) { @@ -645,20 +653,31 @@ unicode_byte_type(char hi, char lo) { unsigned short **toP, const unsigned short *toLim) { \ enum XML_Convert_Result res = XML_CONVERT_COMPLETED; \ UNUSED_P(enc); \ - fromLim = *fromP + (((fromLim - *fromP) >> 1) << 1); /* shrink to even */ \ + const size_t maxBytesToCopy = (toLim - *toP) * sizeof(short); \ + \ + if ((size_t)(fromLim - *fromP) > maxBytesToCopy) { \ + /* Shrink to the (even) maximum amount able to copy */ \ + fromLim = *fromP + maxBytesToCopy; \ + res = XML_CONVERT_OUTPUT_EXHAUSTED; \ + } else { \ + /* Shrink to even number of bytes */ \ + fromLim = *fromP + (((fromLim - *fromP) >> 1) << 1); \ + } \ + \ /* Avoid copying the first half (2 bytes) of surrogate pairs (4 bytes) */ \ - if (fromLim - *fromP > ((toLim - *toP) << 1) \ - && /* are the last two bytes a high surrogate (0xD800-0xDBFF)? */ \ - (GET_HI(fromLim - 2) & 0xFC) == 0xD8) { \ + /* Are the last about-to-be-copied two bytes a high surrogate \ + * (0xD800-0xDBFF)? */ \ + if ((fromLim - *fromP) >= 2 && (GET_HI(fromLim - 2) & 0xFC) == 0xD8) { \ fromLim -= 2; \ - res = XML_CONVERT_INPUT_INCOMPLETE; \ + if (res != XML_CONVERT_OUTPUT_EXHAUSTED) \ + res = XML_CONVERT_INPUT_INCOMPLETE; \ } \ + \ + /* Copy bytes respecting endianness */ \ for (; *fromP < fromLim && *toP < toLim; *fromP += 2) \ *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \ - if ((*toP == toLim) && (*fromP < fromLim)) \ - return XML_CONVERT_OUTPUT_EXHAUSTED; \ - else \ - return res; \ + \ + return res; \ } #define GET_LO(ptr) ((unsigned char)(ptr)[0]) @@ -683,6 +702,7 @@ DEFINE_UTF16_TO_UTF16(big2_) ((p)[1] == 0 ? SB_BYTE_TYPE(enc, p) : unicode_byte_type((p)[1], (p)[0])) #define LITTLE2_BYTE_TO_ASCII(p) ((p)[1] == 0 ? (p)[0] : -1) #define LITTLE2_CHAR_MATCHES(p, c) ((p)[1] == 0 && (p)[0] == (c)) +#define LITTLE2_IS_NAME_CHAR(enc, p, n) LITTLE2_IS_NMSTRT_CHAR(enc, p, n) #define LITTLE2_IS_NAME_CHAR_MINBPC(p) \ UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0]) #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(p) \ @@ -693,6 +713,20 @@ DEFINE_UTF16_TO_UTF16(big2_) #define LITTLE2_IS_INVALID_CHAR(p, n) \ ((n) == 4 && ((unsigned char)(p)[3] & 0xFC) != 0xDC) +static bool +LITTLE2_IS_NMSTRT_CHAR(const ENCODING *enc, const char *p, size_t n) { + UNUSED_P(enc); + assert(n == 4); + const uint8_t *const source = (const uint8_t *)p; + + const uint16_t high = (source[1] << 8) | source[0]; + const uint16_t low = (source[3] << 8) | source[2]; + + const unsigned codepoint = (high - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000; + + return ((codepoint >= 0x10000) && (codepoint <= 0xEFFFF)); +} + #undef PREFIX #define PREFIX(ident) little2_##ident #define MINBPC(enc) 2 @@ -700,9 +734,9 @@ DEFINE_UTF16_TO_UTF16(big2_) #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p) #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(p) #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(p, c) -#define IS_NAME_CHAR(enc, p, n) 0 +#define IS_NAME_CHAR(enc, p, n) LITTLE2_IS_NAME_CHAR(enc, p, n) #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(p) -#define IS_NMSTRT_CHAR(enc, p, n) (0) +#define IS_NMSTRT_CHAR(enc, p, n) LITTLE2_IS_NMSTRT_CHAR(enc, p, n) #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(p) #define IS_INVALID_CHAR(enc, p, n) LITTLE2_IS_INVALID_CHAR(p, n) @@ -784,6 +818,7 @@ static const struct normal_encoding internal_little2_encoding ((p)[0] == 0 ? SB_BYTE_TYPE(enc, p + 1) : unicode_byte_type((p)[0], (p)[1])) #define BIG2_BYTE_TO_ASCII(p) ((p)[0] == 0 ? (p)[1] : -1) #define BIG2_CHAR_MATCHES(p, c) ((p)[0] == 0 && (p)[1] == (c)) +#define BIG2_IS_NAME_CHAR(enc, p, n) BIG2_IS_NMSTRT_CHAR(enc, p, n) #define BIG2_IS_NAME_CHAR_MINBPC(p) \ UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1]) #define BIG2_IS_NMSTRT_CHAR_MINBPC(p) \ @@ -794,6 +829,20 @@ static const struct normal_encoding internal_little2_encoding #define BIG2_IS_INVALID_CHAR(p, n) \ ((n) == 4 && ((unsigned char)(p)[2] & 0xFC) != 0xDC) +static bool +BIG2_IS_NMSTRT_CHAR(const ENCODING *enc, const char *p, size_t n) { + UNUSED_P(enc); + assert(n == 4); + const uint8_t *const source = (const uint8_t *)p; + + const uint16_t high = (source[0] << 8) | source[1]; + const uint16_t low = (source[2] << 8) | source[3]; + + const unsigned codepoint = (high - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000; + + return ((codepoint >= 0x10000) && (codepoint <= 0xEFFFF)); +} + #undef PREFIX #define PREFIX(ident) big2_##ident #define MINBPC(enc) 2 @@ -801,9 +850,9 @@ static const struct normal_encoding internal_little2_encoding #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p) #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(p) #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(p, c) -#define IS_NAME_CHAR(enc, p, n) 0 +#define IS_NAME_CHAR(enc, p, n) BIG2_IS_NAME_CHAR(enc, p, n) #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(p) -#define IS_NMSTRT_CHAR(enc, p, n) (0) +#define IS_NMSTRT_CHAR(enc, p, n) BIG2_IS_NMSTRT_CHAR(enc, p, n) #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(p) #define IS_INVALID_CHAR(enc, p, n) BIG2_IS_INVALID_CHAR(p, n) @@ -1283,13 +1332,17 @@ unknown_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim, utf8 = uenc->utf8[(unsigned char)**fromP]; n = *utf8++; if (n == 0) { + const size_t readLen + = (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP] + - (BT_LEAD2 - 2)); + if (readLen > (size_t)(fromLim - *fromP)) + return XML_CONVERT_INPUT_INCOMPLETE; int c = uenc->convert(uenc->userData, *fromP); n = XmlUtf8Encode(c, buf); if (n > toLim - *toP) return XML_CONVERT_OUTPUT_EXHAUSTED; utf8 = buf; - *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP] - - (BT_LEAD2 - 2)); + *fromP += readLen; } else { if (n > toLim - *toP) return XML_CONVERT_OUTPUT_EXHAUSTED; @@ -1307,9 +1360,13 @@ unknown_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim, while (*fromP < fromLim && *toP < toLim) { unsigned short c = uenc->utf16[(unsigned char)**fromP]; if (c == 0) { + const size_t readLen + = (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP] + - (BT_LEAD2 - 2)); + if (readLen > (size_t)(fromLim - *fromP)) + return XML_CONVERT_INPUT_INCOMPLETE; c = (unsigned short)uenc->convert(uenc->userData, *fromP); - *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP] - - (BT_LEAD2 - 2)); + *fromP += readLen; } else (*fromP)++; *(*toP)++ = c; @@ -1317,8 +1374,8 @@ unknown_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim, if ((*toP == toLim) && (*fromP < fromLim)) return XML_CONVERT_OUTPUT_EXHAUSTED; - else - return XML_CONVERT_COMPLETED; + + return XML_CONVERT_COMPLETED; } ENCODING * diff --git a/Modules/expat/xmltok.h b/Modules/expat/xmltok.h index 973f1fe2ef2181..1beac2b09d78ac 100644 --- a/Modules/expat/xmltok.h +++ b/Modules/expat/xmltok.h @@ -174,7 +174,7 @@ struct encoding { SCANNER literalScanners[XML_N_LITERAL_TYPES]; int (*nameMatchesAscii)(const ENCODING *, const char *, const char *, const char *); - int (*nameLength)(const ENCODING *, const char *); + size_t (*nameLength)(const ENCODING *, const char *); const char *(*skipS)(const ENCODING *, const char *); size_t (*getAtts)(const ENCODING *enc, const char *ptr, size_t attsMax, ATTRIBUTE *atts); @@ -292,6 +292,8 @@ int XmlInitEncoding(INIT_ENCODING *p, const ENCODING **encPtr, const char *name); const ENCODING *XmlGetUtf8InternalEncoding(void); const ENCODING *XmlGetUtf16InternalEncoding(void); +const ENCODING *XmlGetUtf16LittleEndianEncoding(void); +const ENCODING *XmlGetUtf16BigEndianEncoding(void); int XmlUtf8Encode(int charNumber, char *buf); int XmlUtf16Encode(int charNumber, unsigned short *buf); int XmlSizeOfUnknownEncoding(void); @@ -311,6 +313,8 @@ int XmlInitEncodingNS(INIT_ENCODING *p, const ENCODING **encPtr, const char *name); const ENCODING *XmlGetUtf8InternalEncodingNS(void); const ENCODING *XmlGetUtf16InternalEncodingNS(void); +const ENCODING *XmlGetUtf16LittleEndianEncodingNS(void); +const ENCODING *XmlGetUtf16BigEndianEncodingNS(void); ENCODING *XmlInitUnknownEncodingNS(void *mem, const int *table, CONVERTER convert, void *userData); diff --git a/Modules/expat/xmltok_impl.c b/Modules/expat/xmltok_impl.c index b2cb31b01c0121..0e867689633d0f 100644 --- a/Modules/expat/xmltok_impl.c +++ b/Modules/expat/xmltok_impl.c @@ -1729,7 +1729,7 @@ PREFIX(nameMatchesAscii)(const ENCODING *enc, const char *ptr1, return ptr1 == end1; } -static int +static size_t PREFIX(nameLength)(const ENCODING *enc, const char *ptr) { const char *start = ptr; for (;;) { @@ -1754,7 +1754,7 @@ PREFIX(nameLength)(const ENCODING *enc, const char *ptr) { ptr += MINBPC(enc); break; default: - return (int)(ptr - start); + return ptr - start; } } } diff --git a/Modules/expat/xmltok_ns.c b/Modules/expat/xmltok_ns.c index d9f69abb92ef39..6373641481f75b 100644 --- a/Modules/expat/xmltok_ns.c +++ b/Modules/expat/xmltok_ns.c @@ -57,6 +57,16 @@ NS(XmlGetUtf16InternalEncoding)(void) { # endif } +const ENCODING * +NS(XmlGetUtf16LittleEndianEncoding)(void) { + return &ns(little2_encoding).enc; +} + +const ENCODING * +NS(XmlGetUtf16BigEndianEncoding)(void) { + return &ns(big2_encoding).enc; +} + static const ENCODING *const NS(encodings)[] = { &ns(latin1_encoding).enc, &ns(ascii_encoding).enc, &ns(utf8_encoding).enc, &ns(big2_encoding).enc, diff --git a/Modules/getpath.py b/Modules/getpath.py index 2ffb1d964c0fb6..6afbe4c6898556 100644 --- a/Modules/getpath.py +++ b/Modules/getpath.py @@ -211,6 +211,28 @@ def search_up(prefix, *landmarks, test=isfile): prefix = dirname(prefix) +# The traditional Windows path length limit, not counting the terminating NUL. +MAX_PATH = 260 + + +def strip_extended_length_prefix(path): + """ + Remove the leading "\\\\?\\" of a Windows extended-length path, and + rewrite the "\\\\?\\UNC\\" form of a UNC path back to a plain "\\\\" one. + Windows does not resolve relative components (such as ".." or "/") in an + extended-length path, so keeping the prefix in a path that we later join + onto is liable to break consumers. A path that is still too long to be + used unprefixed keeps its prefix, as it has no other usable form. + """ + if path.startswith('\\\\?\\UNC\\'): + stripped = '\\\\' + path[8:] + elif path.startswith('\\\\?\\'): + stripped = path[4:] + else: + return path + return stripped if len(stripped) < MAX_PATH else path + + # ****************************************************************************** # READ VARIABLES FROM config # ****************************************************************************** @@ -320,6 +342,13 @@ def search_up(prefix, *landmarks, test=isfile): real_executable = base_executable #real_executable_dir = dirname(real_executable) executable = ENV_PYTHONEXECUTABLE or ENV___PYVENV_LAUNCHER__ + if os_name == 'nt': + # QUIRK: On Windows the venv launcher reports its own path through + # GetModuleFileNameW, which hands back a "\\?\" extended-length path + # whenever that is how the interpreter was invoked. Drop that prefix + # again, so the venv prefix and sys.executable stay usable by tools + # that join relative components onto them (gh-157917). + executable = strip_extended_length_prefix(executable) executable_dir = dirname(executable) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 636b28fa06ef2e..d7daad8c303858 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -120,6 +120,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.batched.__new__ as batched_new iterable: object @@ -149,7 +150,7 @@ than n. static PyObject * batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n, int strict) -/*[clinic end generated code: output=c6de11b061529d3e input=b31d8be8e8577a34]*/ +/*[clinic end generated code: output=c6de11b061529d3e input=e225f892d3eee347]*/ { PyObject *it; batchedobject *bo; @@ -270,6 +271,7 @@ static PyType_Slot batched_slots[] = { {Py_tp_iternext, batched_next}, {Py_tp_alloc, PyType_GenericAlloc}, {Py_tp_new, batched_new}, + {Py_tp_vectorcall, batched_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -295,6 +297,7 @@ typedef struct { #define pairwiseobject_CAST(op) ((pairwiseobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.pairwise.__new__ as pairwise_new iterable: object @@ -307,7 +310,7 @@ Return an iterator of overlapping pairs taken from the input iterator. static PyObject * pairwise_new_impl(PyTypeObject *type, PyObject *iterable) -/*[clinic end generated code: output=9f0267062d384456 input=6e7c3cddb431a8d6]*/ +/*[clinic end generated code: output=9f0267062d384456 input=32624f8712dcf0a3]*/ { PyObject *it; pairwiseobject *po; @@ -419,6 +422,7 @@ static PyType_Slot pairwise_slots[] = { {Py_tp_iternext, pairwise_next}, {Py_tp_alloc, PyType_GenericAlloc}, {Py_tp_new, pairwise_new}, + {Py_tp_vectorcall, pairwise_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -451,6 +455,7 @@ static PyObject *_grouper_create(groupbyobject *, PyObject *); /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.groupby.__new__ @@ -466,7 +471,7 @@ make an iterator that returns consecutive keys and groups from the iterable static PyObject * itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc) -/*[clinic end generated code: output=cbb1ae3a90fd4141 input=9f89fe625b20ef1a]*/ +/*[clinic end generated code: output=cbb1ae3a90fd4141 input=83700c6814fea634]*/ { groupbyobject *gbo; @@ -594,6 +599,7 @@ static PyType_Slot groupby_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, groupby_next}, {Py_tp_new, itertools_groupby}, + {Py_tp_vectorcall, itertools_groupby_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -884,6 +890,7 @@ teedataobject_dealloc(PyObject *op) } /*[clinic input] +@vectorcall @classmethod itertools.teedataobject.__new__ iterable as it: object @@ -896,7 +903,7 @@ Data container common to multiple tee objects. static PyObject * itertools_teedataobject_impl(PyTypeObject *type, PyObject *it, PyObject *values, PyObject *next) -/*[clinic end generated code: output=3343ceb07e08df5e input=be60f2fabd2b72ba]*/ +/*[clinic end generated code: output=3343ceb07e08df5e input=02647670a03714c0]*/ { teedataobject *tdo; Py_ssize_t i, len; @@ -944,6 +951,7 @@ static PyType_Slot teedataobject_slots[] = { {Py_tp_traverse, teedataobject_traverse}, {Py_tp_clear, teedataobject_clear}, {Py_tp_new, itertools_teedataobject}, + {Py_tp_vectorcall, itertools_teedataobject_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1221,6 +1229,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.cycle.__new__ iterable: object @@ -1230,7 +1239,7 @@ Return elements from the iterable until it is exhausted. Then repeat the sequenc static PyObject * itertools_cycle_impl(PyTypeObject *type, PyObject *iterable) -/*[clinic end generated code: output=f60e5ec17a45b35c input=ead392f4aac7afd8]*/ +/*[clinic end generated code: output=f60e5ec17a45b35c input=183a6615ee9f1440]*/ { PyObject *it; PyObject *saved; @@ -1329,6 +1338,7 @@ static PyType_Slot cycle_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, cycle_next}, {Py_tp_new, itertools_cycle}, + {Py_tp_vectorcall, itertools_cycle_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1354,6 +1364,7 @@ typedef struct { #define dropwhileobject_CAST(op) ((dropwhileobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.dropwhile.__new__ predicate as func: object @@ -1366,7 +1377,7 @@ Afterwards, return every element until the iterable is exhausted. static PyObject * itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq) -/*[clinic end generated code: output=92f9d0d89af149e4 input=d39737147c9f0a26]*/ +/*[clinic end generated code: output=92f9d0d89af149e4 input=38ed294cc91d3aa8]*/ { PyObject *it; dropwhileobject *lz; @@ -1453,6 +1464,7 @@ static PyType_Slot dropwhile_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, dropwhile_next}, {Py_tp_new, itertools_dropwhile}, + {Py_tp_vectorcall, itertools_dropwhile_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1479,6 +1491,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.takewhile.__new__ predicate as func: object @@ -1489,7 +1502,7 @@ Return successive entries from an iterable as long as the predicate evaluates to static PyObject * itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq) -/*[clinic end generated code: output=bb179ea7864e2ef6 input=61e42255dd0a7657]*/ +/*[clinic end generated code: output=bb179ea7864e2ef6 input=168fa0aa716792be]*/ { PyObject *it; takewhileobject *lz; @@ -1572,6 +1585,7 @@ static PyType_Slot takewhile_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, takewhile_next}, {Py_tp_new, itertools_takewhile}, + {Py_tp_vectorcall, itertools_takewhile_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1789,6 +1803,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.starmap.__new__ function as func: object @@ -1799,7 +1814,7 @@ Return an iterator whose values are returned from the function evaluated with an static PyObject * itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq) -/*[clinic end generated code: output=79eeb81d452c6e8d input=8c9068da0692d6d2]*/ +/*[clinic end generated code: output=79eeb81d452c6e8d input=a7986310dcdb9694]*/ { PyObject *it; starmapobject *lz; @@ -1874,6 +1889,7 @@ static PyType_Slot starmap_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, starmap_next}, {Py_tp_new, itertools_starmap}, + {Py_tp_vectorcall, itertools_starmap_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2339,6 +2355,7 @@ struct combinationsobject { #define combinationsobject_CAST(op) ((combinationsobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.combinations.__new__ iterable: object @@ -2351,7 +2368,7 @@ combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3) static PyObject * itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r) -/*[clinic end generated code: output=87a689b39c40039c input=a32f07a15cfa4676]*/ +/*[clinic end generated code: output=87a689b39c40039c input=119eb0d9196abfe3]*/ { combinationsobject *co; Py_ssize_t n; @@ -2531,6 +2548,7 @@ static PyType_Slot combinations_slots[] = { {Py_tp_iternext, combinations_next}, {Py_tp_methods, combinations_methods}, {Py_tp_new, itertools_combinations}, + {Py_tp_vectorcall, itertools_combinations_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2586,6 +2604,7 @@ struct cwrobject { /*[clinic input] @permit_long_summary @permit_long_docstring_body +@vectorcall @classmethod itertools.combinations_with_replacement.__new__ iterable: object @@ -2599,7 +2618,7 @@ static PyObject * itertools_combinations_with_replacement_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r) -/*[clinic end generated code: output=48b26856d4e659ca input=828696750169e84f]*/ +/*[clinic end generated code: output=48b26856d4e659ca input=8bd0382a7253de95]*/ { cwrobject *co; Py_ssize_t n; @@ -2773,6 +2792,7 @@ static PyType_Slot cwr_slots[] = { {Py_tp_iternext, cwr_next}, {Py_tp_methods, cwr_methods}, {Py_tp_new, itertools_combinations_with_replacement}, + {Py_tp_vectorcall, itertools_combinations_with_replacement_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2827,6 +2847,7 @@ struct permutationsobject { #define permutationsobject_CAST(op) ((permutationsobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.permutations.__new__ iterable: object @@ -2839,7 +2860,7 @@ permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1) static PyObject * itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, PyObject *robj) -/*[clinic end generated code: output=296a72fa76d620ea input=57d0170a4ac0ec7a]*/ +/*[clinic end generated code: output=296a72fa76d620ea input=3679c9c6e757d17d]*/ { permutationsobject *po; Py_ssize_t n; @@ -3049,6 +3070,7 @@ static PyType_Slot permutations_slots[] = { {Py_tp_iternext, permutations_next}, {Py_tp_methods, permuations_methods}, {Py_tp_new, itertools_permutations}, + {Py_tp_vectorcall, itertools_permutations_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3076,6 +3098,7 @@ struct accumulateobject { #define accumulateobject_CAST(op) ((accumulateobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.accumulate.__new__ iterable: object @@ -3088,7 +3111,7 @@ Return series of accumulated sums (or other binary function results). static PyObject * itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, PyObject *binop, PyObject *initial) -/*[clinic end generated code: output=66da2650627128f8 input=c4ce20ac59bf7ffd]*/ +/*[clinic end generated code: output=66da2650627128f8 input=9c7edffb7110edc9]*/ { PyObject *it; accumulateobject *lz; @@ -3188,6 +3211,7 @@ static PyType_Slot accumulate_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, accumulate_next}, {Py_tp_new, itertools_accumulate}, + {Py_tp_vectorcall, itertools_accumulate_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3219,6 +3243,7 @@ typedef struct { #define compressobject_CAST(op) ((compressobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.compress.__new__ data as seq1: object @@ -3231,7 +3256,7 @@ to choose the data elements. static PyObject * itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2) -/*[clinic end generated code: output=7e67157212ed09e0 input=32ca4347dbc46749]*/ +/*[clinic end generated code: output=7e67157212ed09e0 input=a886cf13635c9780]*/ { PyObject *data=NULL, *selectors=NULL; compressobject *lz; @@ -3324,6 +3349,7 @@ static PyType_Slot compress_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, compress_next}, {Py_tp_new, itertools_compress}, + {Py_tp_vectorcall, itertools_compress_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3348,6 +3374,7 @@ typedef struct { #define filterfalseobject_CAST(op) ((filterfalseobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.filterfalse.__new__ function as func: object @@ -3360,7 +3387,7 @@ If function is None, return the items that are false. static PyObject * itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq) -/*[clinic end generated code: output=55f87eab9fc0484e input=2d684a2c66f99cde]*/ +/*[clinic end generated code: output=55f87eab9fc0484e input=a80f7d973f6efc81]*/ { PyObject *it; filterfalseobject *lz; @@ -3447,6 +3474,7 @@ static PyType_Slot filterfalse_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, filterfalse_next}, {Py_tp_new, itertools_filterfalse}, + {Py_tp_vectorcall, itertools_filterfalse_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3490,6 +3518,7 @@ slow_mode: when cnt == PY_SSIZE_T_MAX, step is not int(1), or cnt is a float. /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.count.__new__ start as long_cnt: object(c_default="NULL") = 0 @@ -3507,7 +3536,7 @@ Equivalent to: static PyObject * itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, PyObject *long_step) -/*[clinic end generated code: output=09a9250aebd00b1c input=91e4b12c0e88b9f4]*/ +/*[clinic end generated code: output=09a9250aebd00b1c input=b8a57119ba1cdd54]*/ { countobject *lz; int fast_mode; @@ -3695,6 +3724,7 @@ static PyType_Slot count_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, count_next}, {Py_tp_new, itertools_count}, + {Py_tp_vectorcall, itertools_count_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; diff --git a/Modules/main.c b/Modules/main.c index d2587e9fffdad2..d826ed17e0293c 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -214,6 +214,10 @@ pymain_sys_path_add_path0(PyObject *path0) return -1; } + if (PyDict_SetItemString(sysdict, "_path0", path0) < 0) { + return -1; + } + if (PyList_Insert(sys_path, 0, path0)) { return -1; } diff --git a/Modules/makesetup b/Modules/makesetup index 932c0952e4781d..4b2d4fad02e10b 100755 --- a/Modules/makesetup +++ b/Modules/makesetup @@ -19,7 +19,8 @@ set -e # The Setup file following a -b option holds the *bootstrap* modules: # the static modules that the build helper programs (_freeze_module, # _bootstrap_python) are linked with. They are also regular modules of -# the final build. +# the final build. A *bootstrap* line in a Setup file does the same for +# the rest of that file. # Setup files after a -n option are used for their variables, modules # and libraries but not for their .o files. # diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 7b8348d25e5d97..dd2ac90cf5c7cb 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1017,7 +1017,7 @@ math_1a(PyObject *arg, double (*func) (double), const char *err_msg) The last rule is used to catch overflow on platforms which follow C89 but for which HUGE_VAL is not an infinity. - For most two-argument functions (copysign, fmod, hypot, atan2) + For most two-argument functions (fmod, hypot) these rules are enough to ensure that Python's functions behave as specified in 'Annex F' of the C99 standard, with the 'invalid' and 'divide-by-zero' floating-point exceptions mapping to Python's @@ -1030,8 +1030,9 @@ math_2(PyObject *const *args, Py_ssize_t nargs, double (*func) (double, double), const char *funcname) { double x, y, r; - if (!_PyArg_CheckPositional(funcname, nargs, 2, 2)) + if (!_PyArg_CheckPositional(funcname, nargs, 2, 2)) { return NULL; + } x = PyFloat_AsDouble(args[0]); if (x == -1.0 && PyErr_Occurred()) { return NULL; @@ -1060,6 +1061,29 @@ math_2(PyObject *const *args, Py_ssize_t nargs, return PyFloat_FromDouble(r); } +/* variant of math_2, to be used when the function being wrapped is known NOT + to need error checking (i.e., no overflow, invalid, or divide-by-zero). */ + +static PyObject * +math_2ne(PyObject *const *args, Py_ssize_t nargs, + double (*func) (double, double), const char *funcname) +{ + double x, y, r; + if (!_PyArg_CheckPositional(funcname, nargs, 2, 2)) { + return NULL; + } + x = PyFloat_AsDouble(args[0]); + if (x == -1.0 && PyErr_Occurred()) { + return NULL; + } + y = PyFloat_AsDouble(args[1]); + if (y == -1.0 && PyErr_Occurred()) { + return NULL; + } + r = (*func)(x, y); // Ignore errno on purpose. + return PyFloat_FromDouble(r); +} + #define FUNC1(funcname, func, can_overflow, docstring) \ static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ return math_1(args, func, can_overflow, NULL); \ @@ -1090,6 +1114,12 @@ math_2(PyObject *const *args, Py_ssize_t nargs, }\ PyDoc_STRVAR(math_##funcname##_doc, docstring); +#define FUNC2NE(funcname, func, docstring) \ + static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \ + return math_2ne(args, nargs, func, #funcname); \ + }\ + PyDoc_STRVAR(math_##funcname##_doc, docstring); + FUNC1D(acos, acos, 0, "acos($module, x, /)\n--\n\n" "Return the arc cosine (measured in radians) of x.\n\n" @@ -1121,11 +1151,14 @@ FUNC1(atan, atan, 0, "atan($module, x, /)\n--\n\n" "Return the arc tangent (measured in radians) of x.\n\n" "The result is between -pi/2 and pi/2.") -FUNC2(atan2, atan2, +/* gh-153144: Ignore atan2 and atan2pi errno on purpose since it can optionally + * be EDOM, which we should ignore, or ERANGE on underflow, which is + * silent on Python. Overflow is not possible. */ +FUNC2NE(atan2, atan2, "atan2($module, y, x, /)\n--\n\n" "Return the arc tangent (measured in radians) of y/x.\n\n" "Unlike atan(y/x), the signs of both x and y are considered.") -FUNC2(atan2pi, m_atan2pi, +FUNC2NE(atan2pi, m_atan2pi, "atan2pi($module, y, x, /)\n--\n\n" "Return the arc tangent (measured in half-turns) of y/x.\n\n" "Unlike atanpi(y/x), the signs of both x and y are considered.") @@ -1178,7 +1211,9 @@ math_ceil(PyObject *module, PyObject *number) return PyLong_FromDouble(ceil(x)); } -FUNC2(copysign, copysign, +/* gh-153144: Ignore copysign errno on purpose since IEEE Std 754-2019 requires + * it to operate on bit strings and to signal no exception. */ +FUNC2NE(copysign, copysign, "copysign($module, x, y, /)\n--\n\n" "Return a float with the magnitude (absolute value) of x but the sign of y.\n\n" "On platforms that support signed zeros, copysign(1.0, -0.0)\n" diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index ce5c59cb021270..06099a3b8b2e0b 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1567,22 +1567,16 @@ mmap_subscript_impl(mmap_object *self, PyObject *item) else if (step == 1) return _safe_PyBytes_FromStringAndSize(self->data + start, slicelen); else { - char *result_buf = (char *)PyMem_Malloc(slicelen); - PyObject *result; - - if (result_buf == NULL) - return PyErr_NoMemory(); - - if (safe_copy_to_slice(result_buf, self->data, start, step, - slicelen) < 0) - { - result = NULL; + PyBytesWriter *writer = PyBytesWriter_Create(slicelen); + if (writer == NULL) { + return NULL; } - else { - result = PyBytes_FromStringAndSize(result_buf, slicelen); + if (safe_copy_to_slice(PyBytesWriter_GetData(writer), self->data, + start, step, slicelen) < 0) { + PyBytesWriter_Discard(writer); + return NULL; } - PyMem_Free(result_buf); - return result; + return PyBytesWriter_Finish(writer); } } else { diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 9709c4e1c9454b..619154309b40e3 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2561,7 +2561,7 @@ static PyStructSequence_Field waitid_result_fields[] = { }; static PyStructSequence_Desc waitid_result_desc = { - MODNAME ".waitid_result", /* name */ + "os.waitid_result", /* name */ waitid_result__doc__, /* doc */ waitid_result_fields, 5 @@ -4021,6 +4021,12 @@ win32_hchmod(HANDLE hfile, int mode) } if (mode & _S_IWRITE) { info.FileAttributes &= ~FILE_ATTRIBUTE_READONLY; + if (info.FileAttributes == 0) { + /* SetFileInformationByHandle() interprets FileAttributes == 0 + as "leave the attributes unchanged"; FILE_ATTRIBUTE_NORMAL + is the documented way to clear all attributes. */ + info.FileAttributes = FILE_ATTRIBUTE_NORMAL; + } } else { info.FileAttributes |= FILE_ATTRIBUTE_READONLY; @@ -6768,7 +6774,7 @@ or via the attributes sysname, nodename, release, version, and machine.\n\ See os.uname for more information."); static PyStructSequence_Desc uname_result_desc = { - MODNAME ".uname_result", /* name */ + "os.uname_result", /* name */ uname_result__doc__, /* doc */ uname_result_fields, 5 @@ -8818,7 +8824,7 @@ static PyStructSequence_Field sched_param_fields[] = { }; static PyStructSequence_Desc sched_param_desc = { - MODNAME ".sched_param", /* name */ + "os.sched_param", /* name */ os_sched_param__doc__, /* doc */ sched_param_fields, 1 @@ -10864,7 +10870,7 @@ os_wait_impl(PyObject *module) // This system call always crashes on older Android versions. -#if defined(HAVE_PIDFD_OPEN) \ +#if defined(_Py_HAVE_PIDFD_OPEN) \ || (defined(__linux__) && defined(__NR_pidfd_open) \ && !(defined(__ANDROID__) && __ANDROID_API__ < 31)) /*[clinic input] @@ -10882,7 +10888,7 @@ static PyObject * os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) /*[clinic end generated code: output=5c7252698947dc41 input=03058b32c389f874]*/ { -#ifdef HAVE_PIDFD_OPEN +#ifdef _Py_HAVE_PIDFD_OPEN int fd = pidfd_open(pid, flags); #else int fd = syscall(__NR_pidfd_open, pid, flags); @@ -10895,7 +10901,7 @@ os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) #endif -#if defined(HAVE_PIDFD_GETFD) \ +#if defined(_Py_HAVE_PIDFD_GETFD) \ || (defined(__linux__) && defined(__NR_pidfd_getfd) \ && !(defined(__ANDROID__) && __ANDROID_API__ < 31)) /*[clinic input] @@ -10916,7 +10922,7 @@ os_pidfd_getfd_impl(PyObject *module, int pidfd, int targetfd, unsigned int flags) /*[clinic end generated code: output=e1a1415a13c7137f input=ef6417fb10deb1cc]*/ { -#ifdef HAVE_PIDFD_GETFD +#ifdef _Py_HAVE_PIDFD_GETFD int fd = pidfd_getfd(pidfd, targetfd, flags); #else int fd = syscall(__NR_pidfd_getfd, pidfd, targetfd, flags); @@ -11376,7 +11382,7 @@ and elapsed.\n\ See os.times for more information."); static PyStructSequence_Desc times_result_desc = { - MODNAME ".times_result", /* name */ + "os.times_result", /* name */ times_result__doc__, /* doc */ times_result_fields, 5 @@ -16835,7 +16841,7 @@ static PyType_Slot DirEntryType_slots[] = { }; static PyType_Spec DirEntryType_spec = { - .name = MODNAME ".DirEntry", + .name = "os.DirEntry", .basicsize = sizeof(DirEntry), .flags = ( Py_TPFLAGS_DEFAULT @@ -17392,7 +17398,7 @@ static PyType_Slot ScandirIteratorType_slots[] = { }; static PyType_Spec ScandirIteratorType_spec = { - .name = MODNAME ".ScandirIterator", + .name = "os.ScandirIterator", .basicsize = sizeof(ScandirIterator), // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance. diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index fb9d333babf70b..a6063f034463c8 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -293,10 +293,13 @@ error_external_entity_ref_handler(XML_Parser parser, /* Dummy character data handler used when an error (exception) has been detected, and the actual parsing can be terminated early. - This is needed since character data handler can't be safely removed - from within the character data handler, but can be replaced. It is - used only from the character data handler trampoline, and must be - used right after `flag_error()` is called. */ + This is needed since the character data and default handlers can't be + safely removed from within a callback, only replaced: Expat may call + them several times for one piece of text (a long comment in a document + that is not UTF-8, for example). The default handler has the same + signature, so this function serves for both. clear_handlers() installs + it after `flag_error()`, and the handler setter installs it when a + handler is set to None from within a callback. */ static void noop_character_data_handler(void *userData, const XML_Char *data, int len) { @@ -382,15 +385,18 @@ call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len) temp = call_with_frame("CharacterData", __LINE__, self->handlers[CharacterData], args, self); /* temp is an owned reference again, or NULL */ - self->in_callback = 0; Py_DECREF(args); if (temp == NULL) { + self->in_callback = 0; flag_error(self); XML_SetCharacterDataHandler(self->itself, noop_character_data_handler); return -1; } + /* Releasing the result can run arbitrary code, which must still be + treated as being within the callback by the handler setter. */ Py_DECREF(temp); + self->in_callback = 0; return 0; } @@ -579,14 +585,18 @@ my_ ## NAME ## Handler PARAMS { \ PyObject *rv = call_with_frame( \ #NAME, __LINE__, \ self->handlers[NAME], args, self); \ - self->in_callback = 0; \ Py_DECREF(args); \ if (rv == NULL) { \ + self->in_callback = 0; \ flag_error(self); \ return RETURN_VARIABLE; \ } \ CONVERSION \ + /* Releasing the result can run arbitrary code, \ + which must still be treated as being within \ + the callback by the handler setter. */ \ Py_DECREF(rv); \ + self->in_callback = 0; \ if (PyErr_Occurred()) { \ invalid_expat_handler_rv(#NAME); \ } \ @@ -1838,8 +1848,14 @@ xmlparse_handler_setter(PyObject *op, PyObject *v, void *closure) active, so Expat does a little more work than it otherwise would, but that's really an odd case. A more elaborate system of handlers and state could remove the - C handler more effectively. */ - if (handlernum == CharacterData && self->in_callback) { + C handler more effectively. The same applies to the + default handler, which Expat can call several times for one + piece of text, such as a long comment in a document that is + not UTF-8. */ + if (self->in_callback + && (handlernum == CharacterData + || handlernum == Default + || handlernum == DefaultHandlerExpand)) { c_handler = (xmlhandler)noop_character_data_handler; } v = NULL; @@ -2786,7 +2802,17 @@ clear_handlers(xmlparseobject *self, int initial) } else { Py_CLEAR(self->handlers[i]); - CALL_XML_HANDLER_SETTER(&handler_info[i], self->itself, NULL); + xmlhandler c_handler = NULL; + if (i == Default || i == DefaultHandlerExpand) { + /* This may be called from within a callback, where the + default handler can't be removed: for a document that is + not UTF-8, Expat passes a long piece of text to it in + several calls and re-reads the handler before each one + (reportDefault() in xmlparse.c). The no-op handler is + harmless otherwise. */ + c_handler = (xmlhandler)noop_character_data_handler; + } + CALL_XML_HANDLER_SETTER(&handler_info[i], self->itself, c_handler); } } } diff --git a/Modules/readline.c b/Modules/readline.c index 643b1fcb040290..fab0d9aeaa80ae 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -1543,7 +1543,7 @@ setup_readline(readlinestate *mod_state) /* The name must be defined before initialization */ rl_readline_name = "python"; -#ifdef HAVE_RL_CHANGE_ENVIRONMENT +#ifdef _Py_HAVE_RL_CHANGE_ENVIRONMENT /* Prevent readline from setting the LINES and COLUMNS environment * variables: ncurses prefers them over an ioctl() query, so a stale value * left after a resize breaks SIGWINCH / KEY_RESIZE handling (gh-46927). */ diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index addf6b456b1f95..85ed113a17e5e8 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -1304,7 +1304,7 @@ signal_pthread_kill_impl(PyObject *module, unsigned long thread_id, // This system call always crashes on older Android versions. -#if defined(HAVE_PIDFD_SEND_SIGNAL) \ +#if defined(_Py_HAVE_PIDFD_SEND_SIGNAL) \ || (defined(__linux__) && defined(__NR_pidfd_send_signal) \ && !(defined(__ANDROID__) && __ANDROID_API__ < 31)) /*[clinic input] @@ -1329,7 +1329,7 @@ signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum, PyErr_SetString(PyExc_TypeError, "siginfo must be None"); return NULL; } -#ifdef HAVE_PIDFD_SEND_SIGNAL +#ifdef _Py_HAVE_PIDFD_SEND_SIGNAL int res = pidfd_send_signal(pidfd, signalnum, NULL, flags); #else int res = syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags); diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 4df03284cbdd24..7a76882e1633e1 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -606,14 +606,14 @@ _PyLong_##NAME##_Converter(PyObject *obj, void *ptr) \ return 1; \ } -#if defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP) +#if defined(_Py_HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP) # ifdef MS_WINDOWS UNSIGNED_INT_CONVERTER(NetIfindex, NET_IFINDEX) # else # define _PyLong_NetIfindex_Converter _PyLong_UnsignedInt_Converter # define NET_IFINDEX unsigned int # endif -#endif // defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP) +#endif // defined(_Py_HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP) /*[python input] class NET_IFINDEX_converter(CConverter): @@ -7403,7 +7403,7 @@ _socket_if_nameindex_impl(PyObject *module) #endif // defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS_DESKTOP) -#if defined(HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS_DESKTOP) +#if defined(_Py_HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS_DESKTOP) /*[clinic input] _socket.if_nametoindex @@ -7433,9 +7433,9 @@ _socket_if_nametoindex_impl(PyObject *module, PyObject *oname) return PyLong_FromUnsignedLong(index); } -#endif // defined(HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS_DESKTOP) +#endif // defined(_Py_HAVE_IF_NAMETOINDEX) || defined(MS_WINDOWS_DESKTOP) -#if defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP) +#if defined(_Py_HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP) /*[clinic input] @permit_long_summary @@ -7460,7 +7460,7 @@ _socket_if_indextoname_impl(PyObject *module, NET_IFINDEX index) return PyUnicode_DecodeFSDefault(name); } -#endif // defined(HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP) +#endif // defined(_Py_HAVE_IF_INDEXTONAME) || defined(MS_WINDOWS_DESKTOP) #ifdef CMSG_LEN diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 064ab2cf2364f1..395d6ec0f92d5f 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -487,7 +487,7 @@ tmtotuple(time_module_state *state, struct tm *p #define SET(INDEX, VAL) \ SET_ITEM((INDEX), PyLong_FromLong((long) (VAL))) - SET(0, p->tm_year + 1900); + SET_ITEM(0, PyLong_FromLongLong((long long)p->tm_year + 1900)); SET(1, p->tm_mon + 1); /* Want January == 1 */ SET(2, p->tm_mday); SET(3, p->tm_hour); @@ -629,7 +629,7 @@ static int gettmarg(time_module_state *state, PyObject *args, struct tm *p, const char *format) { - int y; + long long y; memset((void *) p, '\0', sizeof(struct tm)); @@ -645,12 +645,12 @@ gettmarg(time_module_state *state, PyObject *args, &p->tm_wday, &p->tm_yday, &p->tm_isdst)) return 0; - if (y < INT_MIN + 1900) { + if (y < (long long)INT_MIN + 1900 || y > (long long)INT_MAX + 1900) { PyErr_SetString(PyExc_OverflowError, "year out of range"); return 0; } - p->tm_year = y - 1900; + p->tm_year = (int)(y - 1900); p->tm_mon--; p->tm_wday = (p->tm_wday + 1) % 7; p->tm_yday--; @@ -906,7 +906,7 @@ time_strftime(PyObject *module, PyObject *args) return NULL; } else if (!gettmarg(state, tup, &buf, - "iiiiiiiii;strftime(): illegal time tuple argument") || + "Liiiiiiii;strftime(): illegal time tuple argument") || !checktm(&buf)) { return NULL; @@ -918,7 +918,7 @@ time_strftime(PyObject *module, PyObject *args) // physical devices (#123017). #if defined(_MSC_VER) || (defined(__sun) && defined(__SVR4)) || defined(_AIX) \ || defined(__VXWORKS__) || defined(__ANDROID__) - if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) { + if (buf.tm_year < 1 - 1900 || buf.tm_year > 9999 - 1900) { PyErr_SetString(PyExc_ValueError, "strftime() requires year in [1; 9999]"); return NULL; @@ -1075,12 +1075,12 @@ _asctime(struct tm *timeptr) "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; return PyUnicode_FromFormat( - "%s %s%3d %.2d:%.2d:%.2d %d", + "%s %s%3d %.2d:%.2d:%.2d %lld", wday_name[timeptr->tm_wday], mon_name[timeptr->tm_mon], timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, - 1900 + timeptr->tm_year); + (long long)timeptr->tm_year + 1900); } /*[clinic input] @@ -1108,7 +1108,7 @@ time_asctime_impl(PyObject *module, PyObject *tup) return NULL; } else if (!gettmarg(state, tup, &buf, - "iiiiiiiii;asctime(): illegal time tuple argument") || + "Liiiiiiii;asctime(): illegal time tuple argument") || !checktm(&buf)) { return NULL; @@ -1164,7 +1164,7 @@ time_mktime(PyObject *module, PyObject *tm_tuple) time_module_state *state = get_time_state(module); if (!gettmarg(state, tm_tuple, &tm, - "iiiiiiiii;mktime(): illegal time tuple argument")) + "Liiiiiiii;mktime(): illegal time tuple argument")) { return NULL; } diff --git a/Objects/abstract.c b/Objects/abstract.c index da2a0b0c285b52..635e1e927f2337 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -853,7 +853,6 @@ _PyBuffer_ReleaseInInterpreterAndRawFree(PyInterpreterState *interp, PyObject * PyObject_Format(PyObject *obj, PyObject *format_spec) { - PyObject *meth; PyObject *empty = NULL; PyObject *result = NULL; @@ -880,21 +879,32 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) format_spec = empty; } - /* Find the (unbound!) __format__ method */ - meth = _PyObject_LookupSpecial(obj, &_Py_ID(__format__)); - if (meth == NULL) { - PyThreadState *tstate = _PyThreadState_GET(); - if (!_PyErr_Occurred(tstate)) { - _PyErr_Format(tstate, PyExc_TypeError, - "Type %.100s doesn't define __format__", - Py_TYPE(obj)->tp_name); + /* Find the __format__ method and call it, without creating + a temporary bound method where possible. */ + PyThreadState *tstate = _PyThreadState_GET(); + _PyCStackRef cref; + _PyThreadState_PushCStackRef(tstate, &cref); + _PyStackRef self = PyStackRef_FromPyObjectBorrow(obj); + int found = _PyObject_LookupSpecialMethod(&_Py_ID(__format__), + &cref.ref, &self); + if (found > 0) { + PyObject *meth = PyStackRef_AsPyObjectBorrow(cref.ref); + PyObject *args[2] = {PyStackRef_AsPyObjectBorrow(self), format_spec}; + if (args[0] != NULL) { + /* Unbound method: prepend self. */ + result = PyObject_Vectorcall(meth, args, 2, NULL); + } + else { + result = PyObject_Vectorcall(meth, args + 1, 1, NULL); } - goto done; } - - /* And call it. */ - result = PyObject_CallOneArg(meth, format_spec); - Py_DECREF(meth); + else if (found == 0) { + _PyErr_Format(tstate, PyExc_TypeError, + "Type %.100s doesn't define __format__", + Py_TYPE(obj)->tp_name); + } + PyStackRef_XCLOSE(self); + _PyThreadState_PopCStackRef(tstate, &cref); if (result && !PyUnicode_Check(result)) { PyErr_Format(PyExc_TypeError, @@ -1344,7 +1354,7 @@ PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) if (f != NULL) return sequence_repeat(f, v, w); } - else if (mw != NULL) { + if (mw != NULL) { /* Note that the right hand operand should not be * mutated in this case so sq_inplace_repeat is not * used. */ @@ -2600,6 +2610,28 @@ check_class(PyObject *cls, const char *error) return -1; } +/* Return 1 if inst.__class__ would resolve to object's own getset descriptor, + i.e. evaluate to Py_TYPE(inst). Types with a custom tp_getattro or an + overridden __class__ return 0 and take the generic lookup. */ +static inline int +class_attr_is_type(PyObject *inst) +{ + PyTypeObject *tp = Py_TYPE(inst); + if (tp->tp_getattro != PyObject_GenericGetAttr) { + return 0; + } + _PyStackRef cref; + _PyType_LookupStackRefAndVersion(tp, &_Py_ID(__class__), &cref); + if (PyStackRef_IsNull(cref)) { + return 0; + } + PyObject *descr = PyStackRef_AsPyObjectBorrow(cref); + int res = (Py_IS_TYPE(descr, &PyGetSetDescr_Type) + && PyDescr_TYPE(descr) == &PyBaseObject_Type); + PyStackRef_CLOSE(cref); + return res; +} + static int object_isinstance(PyObject *inst, PyObject *cls) { @@ -2608,6 +2640,9 @@ object_isinstance(PyObject *inst, PyObject *cls) if (PyType_Check(cls)) { retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls); if (retval == 0) { + if (class_attr_is_type(inst)) { + return 0; + } retval = PyObject_GetOptionalAttr(inst, &_Py_ID(__class__), &icls); if (icls != NULL) { if (icls != (PyObject *)(Py_TYPE(inst)) && PyType_Check(icls)) { diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 317534c99f038d..b9d547575bd2eb 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -266,13 +266,22 @@ bytearray_resize_storage(PyByteArrayObject *self, } +/* Resize without the checks of bytearray_resize_lock_held(): the caller must + have checked that the bytearray can be resized. + + Unlike bytearray_resize_lock_held(), this does not return early if + requested_size is already the size of the bytearray. A caller which + removed bytes by advancing ob_start and reducing the size uses that to + release the memory. */ static int -bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size) +bytearray_resize_unchecked(PyByteArrayObject *obj, Py_ssize_t requested_size) { - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); - PyByteArrayObject *obj = ((PyByteArrayObject *)self); + _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(obj); + PyObject *self = (PyObject *)obj; assert(obj->ob_bytes_object != NULL); + assert(requested_size >= 0); + assert(obj->ob_exports == 0); /* All computations are done unsigned to avoid integer overflows (see issue #22335). */ @@ -280,23 +289,8 @@ bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size) size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes); size_t size = (size_t) requested_size; - assert(self != NULL); - assert(PyByteArray_Check(self)); assert(logical_offset <= alloc); - if (requested_size < 0) { - PyErr_Format(PyExc_ValueError, - "Can only resize to positive sizes, got %zd", requested_size); - return -1; - } - - if (requested_size == Py_SIZE(self)) { - return 0; - } - if (!_canresize(obj)) { - return -1; - } - /* Resize to 0 resets to empty bytes (see issue #153419). */ if (requested_size == 0) { Py_SETREF(obj->ob_bytes_object, @@ -348,6 +342,30 @@ bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size) return 0; } +static int +bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size) +{ + _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); + PyByteArrayObject *obj = ((PyByteArrayObject *)self); + + assert(self != NULL); + assert(PyByteArray_Check(self)); + + if (requested_size < 0) { + PyErr_Format(PyExc_ValueError, + "Can only resize to positive sizes, got %zd", requested_size); + return -1; + } + + if (requested_size == Py_SIZE(self)) { + return 0; + } + if (!_canresize(obj)) { + return -1; + } + return bytearray_resize_unchecked(obj, requested_size); +} + int PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) { @@ -612,14 +630,22 @@ bytearray_setslice_linear(PyByteArrayObject *self, return -1; if (lo == 0) { - /* Shrink the buffer by advancing its logical start */ + /* Shrink the buffer by advancing its logical start. The bytes + are removed at this point; the resize below only releases + memory. */ self->ob_start -= growth; + Py_SET_SIZE(self, Py_SIZE(self) + growth); /* 0 lo hi old_size | |<----avail----->|<-----tail------>| | |<-bytes_len->|<-----tail------>| 0 new_lo new_hi new_size */ + if (bytearray_resize_unchecked(self, Py_SIZE(self)) < 0) { + /* The operation is completed, but a MemoryError is still + raised and the memory block is not shrunk. */ + res = -1; + } } else { /* @@ -630,26 +656,17 @@ bytearray_setslice_linear(PyByteArrayObject *self, */ memmove(buf + lo + bytes_len, buf + hi, Py_SIZE(self) - hi); - } - if (bytearray_resize_lock_held((PyObject *)self, - Py_SIZE(self) + growth) < 0) { - /* Issue #19578: Handling the memory allocation failure here is - tricky here because the bytearray object has already been - modified. Depending on growth and lo, the behaviour is - different. - - If growth < 0 and lo != 0, the operation is completed, but a - MemoryError is still raised and the memory block is not - shrunk. Otherwise, the bytearray is restored in its previous - state and a MemoryError is raised. */ - if (lo == 0) { - self->ob_start += growth; - return -1; + if (bytearray_resize_lock_held((PyObject *)self, + Py_SIZE(self) + growth) < 0) { + /* Issue #19578: memmove() removed bytes, the bytearray + object cannot be restored in its previous state. + + The operation is completed, but a MemoryError is still + raised and the memory block is not shrunk. */ + Py_SET_SIZE(self, Py_SIZE(self) + growth); + bytearray_write_trailing_null_byte(self); + res = -1; } - /* memmove() removed bytes, the bytearray object cannot be - restored in its previous state. */ - Py_SET_SIZE(self, Py_SIZE(self) + growth); - res = -1; } buf = PyByteArray_AS_STRING(self); } diff --git a/Objects/bytes_methods.c b/Objects/bytes_methods.c index 414afeb7bb003c..4021a0e8223694 100644 --- a/Objects/bytes_methods.c +++ b/Objects/bytes_methods.c @@ -377,6 +377,7 @@ _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to) } #define FASTSEARCH fastsearch +#define STRINGLIB_IS_UNICODE 0 #define STRINGLIB(F) stringlib_##F #define STRINGLIB_CHAR char #define STRINGLIB_SIZEOF_CHAR 1 diff --git a/Objects/cellobject.c b/Objects/cellobject.c index 823f3c91044c78..a1c7dd42912bcc 100644 --- a/Objects/cellobject.c +++ b/Objects/cellobject.c @@ -2,11 +2,17 @@ #include "Python.h" #include "pycore_cell.h" // PyCell_GetRef() -#include "pycore_modsupport.h" // _PyArg_NoKeywords() #include "pycore_object.h" +#include "clinic/cellobject.c.h" + #define _PyCell_CAST(op) _Py_CAST(PyCellObject*, (op)) +/*[clinic input] +class cell "PyCellObject *" "&PyCell_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3d6baba8cf810af0]*/ + PyObject * PyCell_New(PyObject *obj) { @@ -21,35 +27,28 @@ PyCell_New(PyObject *obj) return (PyObject *)op; } -PyDoc_STRVAR(cell_new_doc, -"cell([contents])\n" -"--\n" -"\n" -"Create a new cell object.\n" -"\n" -" contents\n" -" the contents of the cell. If not specified, the cell will be empty,\n" -" and further attempts to access its cell_contents attribute will\n" -" raise a ValueError."); +/*[clinic input] +@vectorcall +@classmethod +@text_signature "([contents])" +cell.__new__ as cell_new + contents: object = NULL + / -static PyObject * -cell_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - PyObject *return_value = NULL; - PyObject *obj = NULL; +Create a new cell object. - if (!_PyArg_NoKeywords("cell", kwargs)) { - goto exit; - } - /* min = 0: we allow the cell to be empty */ - if (!PyArg_UnpackTuple(args, "cell", 0, 1, &obj)) { - goto exit; - } - return_value = PyCell_New(obj); + contents + the contents of the cell. If not specified, the cell will be empty, + and further attempts to access its cell_contents attribute will + raise a ValueError. +[clinic start generated code]*/ -exit: - return return_value; +static PyObject * +cell_new_impl(PyTypeObject *type, PyObject *contents) +/*[clinic end generated code: output=0b7db264f0621bb4 input=4460339a82243798]*/ +{ + return PyCell_New(contents); } PyObject * @@ -190,7 +189,7 @@ PyTypeObject PyCell_Type = { 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ - cell_new_doc, /* tp_doc */ + cell_new__doc__, /* tp_doc */ cell_traverse, /* tp_traverse */ cell_clear, /* tp_clear */ cell_richcompare, /* tp_richcompare */ @@ -209,4 +208,5 @@ PyTypeObject PyCell_Type = { 0, /* tp_alloc */ cell_new, /* tp_new */ 0, /* tp_free */ + .tp_vectorcall = cell_vectorcall, }; diff --git a/Objects/classobject.c b/Objects/classobject.c index 238f1c1dad7d86..dbfdc9262cf020 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -182,8 +182,9 @@ method_getattro(PyObject *obj, PyObject *name) } /*[clinic input] +@vectorcall @classmethod -method.__new__ as method_new +method.__new__ as method_type_new function: object instance: object / @@ -192,8 +193,9 @@ Create a bound instance method object. [clinic start generated code]*/ static PyObject * -method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance) -/*[clinic end generated code: output=d33ef4ebf702e1f7 input=4e32facc3c3108ae]*/ +method_type_new_impl(PyTypeObject *type, PyObject *function, + PyObject *instance) +/*[clinic end generated code: output=e9877864a8bfdf70 input=70c1e0da5d6267f5]*/ { if (!PyCallable_Check(function)) { PyErr_SetString(PyExc_TypeError, @@ -323,7 +325,7 @@ PyTypeObject PyMethod_Type = { .tp_setattro = PyObject_GenericSetAttr, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_VECTORCALL, - .tp_doc = method_new__doc__, + .tp_doc = method_type_new__doc__, .tp_traverse = method_traverse, .tp_richcompare = method_richcompare, .tp_weaklistoffset = offsetof(PyMethodObject, im_weakreflist), @@ -331,7 +333,8 @@ PyTypeObject PyMethod_Type = { .tp_members = method_memberlist, .tp_getset = method_getset, .tp_descr_get = method_descr_get, - .tp_new = method_new, + .tp_new = method_type_new, + .tp_vectorcall = method_type_vectorcall, }; /* ------------------------------------------------------------------------ diff --git a/Objects/clinic/cellobject.c.h b/Objects/clinic/cellobject.c.h new file mode 100644 index 00000000000000..bf2b21d5dc4437 --- /dev/null +++ b/Objects/clinic/cellobject.c.h @@ -0,0 +1,74 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +#include "pycore_modsupport.h" // _PyArg_CheckPositional() + +PyDoc_STRVAR(cell_new__doc__, +"cell([contents])\n" +"--\n" +"\n" +"Create a new cell object.\n" +"\n" +" contents\n" +" the contents of the cell. If not specified, the cell will be empty,\n" +" and further attempts to access its cell_contents attribute will\n" +" raise a ValueError."); + +static PyObject * +cell_new_impl(PyTypeObject *type, PyObject *contents); + +static PyObject * +cell_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyTypeObject *base_tp = &PyCell_Type; + PyObject *contents = NULL; + + if ((type == base_tp || type->tp_init == base_tp->tp_init) && + !_PyArg_NoKeywords("cell", kwargs)) { + goto exit; + } + if (!_PyArg_CheckPositional("cell", PyTuple_GET_SIZE(args), 0, 1)) { + goto exit; + } + if (PyTuple_GET_SIZE(args) < 1) { + goto skip_optional; + } + contents = PyTuple_GET_ITEM(args, 0); +skip_optional: + return_value = cell_new_impl(type, contents); + +exit: + return return_value; +} + +static PyObject * +cell_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *contents = NULL; + + assert(_PyType_CAST(type)->tp_new == cell_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("cell", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("cell", nargs, 0, 1)) { + goto exit; + } + if (nargs < 1) { + goto skip_optional; + } + contents = args[0]; +skip_optional: + return_value = cell_new_impl(_PyType_CAST(type), contents); + +exit: + return return_value; +} +/*[clinic end generated code: output=687470647f1a75f8 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/classobject.c.h b/Objects/clinic/classobject.c.h index 5934f1c2a41669..1b96cc2212a826 100644 --- a/Objects/clinic/classobject.c.h +++ b/Objects/clinic/classobject.c.h @@ -21,17 +21,18 @@ method___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored)) return method___reduce___impl((PyMethodObject *)self); } -PyDoc_STRVAR(method_new__doc__, +PyDoc_STRVAR(method_type_new__doc__, "method(function, instance, /)\n" "--\n" "\n" "Create a bound instance method object."); static PyObject * -method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance); +method_type_new_impl(PyTypeObject *type, PyObject *function, + PyObject *instance); static PyObject * -method_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +method_type_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; PyTypeObject *base_tp = &PyMethod_Type; @@ -47,7 +48,34 @@ method_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) } function = PyTuple_GET_ITEM(args, 0); instance = PyTuple_GET_ITEM(args, 1); - return_value = method_new_impl(type, function, instance); + return_value = method_type_new_impl(type, function, instance); + +exit: + return return_value; +} + +static PyObject * +method_type_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *function; + PyObject *instance; + + assert(_PyType_CAST(type)->tp_new == method_type_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("method", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("method", nargs, 2, 2)) { + goto exit; + } + function = args[0]; + instance = args[1]; + return_value = method_type_new_impl(_PyType_CAST(type), function, instance); exit: return return_value; @@ -82,4 +110,4 @@ instancemethod_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=ab546abf90aac94e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=fd5c214ee6e55e88 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/descrobject.c.h b/Objects/clinic/descrobject.c.h index 744e5edf922b57..879b872d9b3ae6 100644 --- a/Objects/clinic/descrobject.c.h +++ b/Objects/clinic/descrobject.c.h @@ -18,7 +18,8 @@ static PyObject * mappingproxy_new_impl(PyTypeObject *type, PyObject *mapping); static PyObject * -mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +mappingproxy_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -50,10 +51,9 @@ mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[1]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *mapping; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -65,6 +65,38 @@ mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return mappingproxy_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +mappingproxy_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *mapping; + + assert(_PyType_CAST(type)->tp_new == mappingproxy_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 1 || nargs > 1) { + return mappingproxy_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + mapping = args[0]; + return_value = mappingproxy_new_impl(_PyType_CAST(type), mapping); + + return return_value; +} + PyDoc_STRVAR(property___set_name____doc__, "__set_name__($self, owner, name, /)\n" "--\n" @@ -210,4 +242,4 @@ property_init(PyObject *self, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=fb62ee8be13af793 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=41ae2d4147b31052 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/exceptions.c.h b/Objects/clinic/exceptions.c.h index c06cdb0444ee97..b77606c3c7a8c7 100644 --- a/Objects/clinic/exceptions.c.h +++ b/Objects/clinic/exceptions.c.h @@ -2,8 +2,12 @@ preserve [clinic start generated code]*/ +#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) +# include "pycore_gc.h" // PyGC_Head +#endif #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_BadArgument() +#include "pycore_runtime.h" // _Py_SINGLETON() static PyObject * BaseException_str_impl(PyBaseExceptionObject *self); @@ -362,6 +366,329 @@ BaseExceptionGroup_subgroup(PyObject *self, PyObject *matcher_value) return return_value; } + +static int +ImportError_init_impl(PyImportErrorObject *self, PyObject *args, + PyObject *name, PyObject *path, PyObject *name_from); + +static int +ImportError_init_helper(PyObject *self, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) +{ + int return_value = -1; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + + #define NUM_KEYWORDS 3 + static struct { + PyGC_Head _this_is_not_used; + PyObject_VAR_HEAD + Py_hash_t ob_hash; + PyObject *ob_item[NUM_KEYWORDS]; + } _kwtuple = { + .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) + .ob_hash = -1, + .ob_item = { &_Py_ID(name), &_Py_ID(path), &_Py_ID(name_from), }, + }; + #undef NUM_KEYWORDS + #define KWTUPLE (&_kwtuple.ob_base.ob_base) + + #else // !Py_BUILD_CORE + # define KWTUPLE NULL + #endif // !Py_BUILD_CORE + + static const char * const _keywords[] = {"name", "path", "name_from", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "ImportError", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[3]; + PyObject * const *fastargs; + Py_ssize_t noptargs = 0 + nkw - 0; + PyObject *__clinic_args = NULL; + PyObject *name = NULL; + PyObject *path = NULL; + PyObject *name_from = NULL; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + if (!noptargs) { + goto skip_optional_kwonly; + } + if (fastargs[0]) { + name = fastargs[0]; + if (!--noptargs) { + goto skip_optional_kwonly; + } + } + if (fastargs[1]) { + path = fastargs[1]; + if (!--noptargs) { + goto skip_optional_kwonly; + } + } + name_from = fastargs[2]; +skip_optional_kwonly: + __clinic_args = PyTuple_FromArray(args, nargs); + if (__clinic_args == NULL) { + goto exit; + } + return_value = ImportError_init_impl((PyImportErrorObject *)self, __clinic_args, name, path, name_from); + +exit: + /* Cleanup for args */ + Py_XDECREF(__clinic_args); + + return return_value; +} + +static int +ImportError_init(PyObject *self, PyObject *args, PyObject *kwargs) +{ + return ImportError_init_helper(self, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +ImportError_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *self; + int _result; + + assert(_PyType_CAST(type)->tp_init == ImportError_init); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + return NULL; + } + _result = ImportError_init_helper(self, args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + if (_result != 0) { + Py_DECREF(self); + return NULL; + } + return self; +} + +static int +NameError_init_impl(PyNameErrorObject *self, PyObject *args, PyObject *name); + +static int +NameError_init_helper(PyObject *self, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) +{ + int return_value = -1; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + + #define NUM_KEYWORDS 1 + static struct { + PyGC_Head _this_is_not_used; + PyObject_VAR_HEAD + Py_hash_t ob_hash; + PyObject *ob_item[NUM_KEYWORDS]; + } _kwtuple = { + .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) + .ob_hash = -1, + .ob_item = { &_Py_ID(name), }, + }; + #undef NUM_KEYWORDS + #define KWTUPLE (&_kwtuple.ob_base.ob_base) + + #else // !Py_BUILD_CORE + # define KWTUPLE NULL + #endif // !Py_BUILD_CORE + + static const char * const _keywords[] = {"name", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "NameError", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject * const *fastargs; + Py_ssize_t noptargs = 0 + nkw - 0; + PyObject *__clinic_args = NULL; + PyObject *name = NULL; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + if (!noptargs) { + goto skip_optional_kwonly; + } + name = fastargs[0]; +skip_optional_kwonly: + __clinic_args = PyTuple_FromArray(args, nargs); + if (__clinic_args == NULL) { + goto exit; + } + return_value = NameError_init_impl((PyNameErrorObject *)self, __clinic_args, name); + +exit: + /* Cleanup for args */ + Py_XDECREF(__clinic_args); + + return return_value; +} + +static int +NameError_init(PyObject *self, PyObject *args, PyObject *kwargs) +{ + return NameError_init_helper(self, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +NameError_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *self; + int _result; + + assert(_PyType_CAST(type)->tp_init == NameError_init); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + return NULL; + } + _result = NameError_init_helper(self, args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + if (_result != 0) { + Py_DECREF(self); + return NULL; + } + return self; +} + +static int +AttributeError_init_impl(PyAttributeErrorObject *self, PyObject *args, + PyObject *name, PyObject *obj); + +static int +AttributeError_init_helper(PyObject *self, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) +{ + int return_value = -1; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + + #define NUM_KEYWORDS 2 + static struct { + PyGC_Head _this_is_not_used; + PyObject_VAR_HEAD + Py_hash_t ob_hash; + PyObject *ob_item[NUM_KEYWORDS]; + } _kwtuple = { + .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) + .ob_hash = -1, + .ob_item = { &_Py_ID(name), &_Py_ID(obj), }, + }; + #undef NUM_KEYWORDS + #define KWTUPLE (&_kwtuple.ob_base.ob_base) + + #else // !Py_BUILD_CORE + # define KWTUPLE NULL + #endif // !Py_BUILD_CORE + + static const char * const _keywords[] = {"name", "obj", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "AttributeError", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[2]; + PyObject * const *fastargs; + Py_ssize_t noptargs = 0 + nkw - 0; + PyObject *__clinic_args = NULL; + PyObject *name = NULL; + PyObject *obj = NULL; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + if (!noptargs) { + goto skip_optional_kwonly; + } + if (fastargs[0]) { + name = fastargs[0]; + if (!--noptargs) { + goto skip_optional_kwonly; + } + } + obj = fastargs[1]; +skip_optional_kwonly: + __clinic_args = PyTuple_FromArray(args, nargs); + if (__clinic_args == NULL) { + goto exit; + } + return_value = AttributeError_init_impl((PyAttributeErrorObject *)self, __clinic_args, name, obj); + +exit: + /* Cleanup for args */ + Py_XDECREF(__clinic_args); + + return return_value; +} + +static int +AttributeError_init(PyObject *self, PyObject *args, PyObject *kwargs) +{ + return AttributeError_init_helper(self, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +AttributeError_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *self; + int _result; + + assert(_PyType_CAST(type)->tp_init == AttributeError_init); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + return NULL; + } + _result = AttributeError_init_helper(self, args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + if (_result != 0) { + Py_DECREF(self); + return NULL; + } + return self; +} #define BASEEXCEPTION_ARGS_GETSETDEF {"args", (getter)BaseException_args_get, (setter)BaseException_args_set, NULL}, #define BASEEXCEPTION___TRACEBACK___GETSETDEF {"__traceback__", (getter)BaseException___traceback___get, (setter)BaseException___traceback___set, NULL}, @@ -370,4 +697,4 @@ BaseExceptionGroup_subgroup(PyObject *self, PyObject *matcher_value) #define BASEEXCEPTION___CAUSE___GETSETDEF {"__cause__", (getter)BaseException___cause___get, (setter)BaseException___cause___set, NULL}, -/*[clinic end generated code: output=20829af0c376b1e9 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=c8eaffee13bc041d input=a9049054013a1b77]*/ diff --git a/Objects/clinic/frameobject.c.h b/Objects/clinic/frameobject.c.h index f7e764a0755ecf..9ea0eaa662a164 100644 --- a/Objects/clinic/frameobject.c.h +++ b/Objects/clinic/frameobject.c.h @@ -3,6 +3,65 @@ preserve [clinic start generated code]*/ #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() +#include "pycore_modsupport.h" // _PyArg_CheckPositional() + +PyDoc_STRVAR(framelocalsproxy_new__doc__, +"FrameLocalsProxy(frame, /)\n" +"--\n" +"\n" +"Create a write-through view of the locals dictionary for a frame.\n" +"\n" +" frame\n" +" the frame object to wrap."); + +static PyObject * +framelocalsproxy_new_impl(PyTypeObject *type, PyObject *frame); + +static PyObject * +framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyTypeObject *base_tp = &PyFrameLocalsProxy_Type; + PyObject *frame; + + if ((type == base_tp || type->tp_init == base_tp->tp_init) && + !_PyArg_NoKeywords("FrameLocalsProxy", kwargs)) { + goto exit; + } + if (!_PyArg_CheckPositional("FrameLocalsProxy", PyTuple_GET_SIZE(args), 1, 1)) { + goto exit; + } + frame = PyTuple_GET_ITEM(args, 0); + return_value = framelocalsproxy_new_impl(type, frame); + +exit: + return return_value; +} + +static PyObject * +framelocalsproxy_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *frame; + + assert(_PyType_CAST(type)->tp_new == framelocalsproxy_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("FrameLocalsProxy", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("FrameLocalsProxy", nargs, 1, 1)) { + goto exit; + } + frame = args[0]; + return_value = framelocalsproxy_new_impl(_PyType_CAST(type), frame); + +exit: + return return_value; +} PyDoc_STRVAR(frame_locals__doc__, "Return the mapping used by the frame to look up local variables."); @@ -322,4 +381,4 @@ frame_repr(PyObject *self) #define FRAME_F_GENERATOR_GETSETDEF {"f_generator", (getter)frame_generator_get, (setter)NULL, frame_generator__doc__}, -/*[clinic end generated code: output=e86fc251b0396a11 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e6d74bb87e4b1a95 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/funcobject.c.h b/Objects/clinic/funcobject.c.h index fa581e553aca8a..8bf0a8a855f172 100644 --- a/Objects/clinic/funcobject.c.h +++ b/Objects/clinic/funcobject.c.h @@ -159,7 +159,8 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals, PyObject *kwdefaults); static PyObject * -func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +func_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -191,8 +192,7 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[6]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2; + Py_ssize_t noptargs = nargs + nkw - 2; PyCodeObject *code; PyObject *globals; PyObject *name = Py_None; @@ -200,7 +200,7 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) PyObject *closure = Py_None; PyObject *kwdefaults = Py_None; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 6, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -240,6 +240,70 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) skip_optional_pos: return_value = func_new_impl(type, code, globals, name, defaults, closure, kwdefaults); +exit: + return return_value; +} + +static PyObject * +func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return func_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +func_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyCodeObject *code; + PyObject *globals; + PyObject *name = Py_None; + PyObject *defaults = Py_None; + PyObject *closure = Py_None; + PyObject *kwdefaults = Py_None; + + assert(_PyType_CAST(type)->tp_new == func_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 2 || nargs > 6) { + return func_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + if (!PyObject_TypeCheck(args[0], &PyCode_Type)) { + _PyArg_BadArgument("function", "argument 'code'", (&PyCode_Type)->tp_name, args[0]); + goto exit; + } + code = (PyCodeObject *)args[0]; + if (!PyDict_Check(args[1])) { + _PyArg_BadArgument("function", "argument 'globals'", "dict", args[1]); + goto exit; + } + globals = args[1]; + if (nargs < 3) { + goto skip_optional; + } + name = args[2]; + if (nargs < 4) { + goto skip_optional; + } + defaults = args[3]; + if (nargs < 5) { + goto skip_optional; + } + closure = args[4]; + if (nargs < 6) { + goto skip_optional; + } + kwdefaults = args[5]; +skip_optional: + return_value = func_new_impl(_PyType_CAST(type), code, globals, name, defaults, closure, kwdefaults); + exit: return return_value; } @@ -249,4 +313,4 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #define FUNCTION___TYPE_PARAMS___GETSETDEF {"__type_params__", (getter)function___type_params___get, (setter)function___type_params___set, function___type_params____doc__}, -/*[clinic end generated code: output=b722bea4e9d8b8be input=a9049054013a1b77]*/ +/*[clinic end generated code: output=3cd73f64e8bd846e input=a9049054013a1b77]*/ diff --git a/Objects/clinic/interpolationobject.c.h b/Objects/clinic/interpolationobject.c.h index 2030e17e49e47a..665f00462fe2c6 100644 --- a/Objects/clinic/interpolationobject.c.h +++ b/Objects/clinic/interpolationobject.c.h @@ -14,7 +14,8 @@ interpolation_new_impl(PyTypeObject *type, PyObject *value, PyObject *format_spec); static PyObject * -interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +interpolation_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -46,14 +47,13 @@ interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[4]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *value; PyObject *expression = &_Py_STR(empty); PyObject *conversion = Py_None; PyObject *format_spec = &_Py_STR(empty); - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -91,4 +91,63 @@ interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=2391391e2d7708c0 input=a9049054013a1b77]*/ + +static PyObject * +interpolation_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return interpolation_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +interpolation_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *value; + PyObject *expression = &_Py_STR(empty); + PyObject *conversion = Py_None; + PyObject *format_spec = &_Py_STR(empty); + + assert(_PyType_CAST(type)->tp_new == interpolation_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 1 || nargs > 4) { + return interpolation_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + value = args[0]; + if (nargs < 2) { + goto skip_optional; + } + if (!PyUnicode_Check(args[1])) { + _PyArg_BadArgument("Interpolation", "argument 'expression'", "str", args[1]); + goto exit; + } + expression = args[1]; + if (nargs < 3) { + goto skip_optional; + } + if (!_conversion_converter(args[2], &conversion)) { + goto exit; + } + if (nargs < 4) { + goto skip_optional; + } + if (!PyUnicode_Check(args[3])) { + _PyArg_BadArgument("Interpolation", "argument 'format_spec'", "str", args[3]); + goto exit; + } + format_spec = args[3]; +skip_optional: + return_value = interpolation_new_impl(_PyType_CAST(type), value, expression, conversion, format_spec); + +exit: + return return_value; +} +/*[clinic end generated code: output=d07caaaa0e78ca15 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/memoryobject.c.h b/Objects/clinic/memoryobject.c.h index 93daf550b0092c..bb81e11e9a0ce2 100644 --- a/Objects/clinic/memoryobject.c.h +++ b/Objects/clinic/memoryobject.c.h @@ -20,7 +20,8 @@ static PyObject * memoryview_impl(PyTypeObject *type, PyObject *object); static PyObject * -memoryview(PyTypeObject *type, PyObject *args, PyObject *kwargs) +memoryview_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -52,10 +53,9 @@ memoryview(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[1]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *object; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -67,6 +67,38 @@ memoryview(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +memoryview(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return memoryview_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +memoryview_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *object; + + assert(_PyType_CAST(type)->tp_new == memoryview); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 1 || nargs > 1) { + return memoryview_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + object = args[0]; + return_value = memoryview_impl(_PyType_CAST(type), object); + + return return_value; +} + PyDoc_STRVAR(memoryview__from_flags__doc__, "_from_flags($type, /, object, flags)\n" "--\n" @@ -556,4 +588,4 @@ memoryview_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=889b7cc1a94bfc9b input=a9049054013a1b77]*/ +/*[clinic end generated code: output=cdca13a132752f15 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/namespaceobject.c.h b/Objects/clinic/namespaceobject.c.h index cdd9ef586c79ec..9f6301592d2c84 100644 --- a/Objects/clinic/namespaceobject.c.h +++ b/Objects/clinic/namespaceobject.c.h @@ -25,15 +25,7 @@ namespace___replace__(PyObject *self, PyObject *args, PyObject *kwargs) if (!_PyArg_NoPositional("__replace__", args)) { goto exit; } - if (kwargs == NULL) { - changes = PyDict_New(); - if (changes == NULL) { - goto exit; - } - } - else { - changes = Py_NewRef(kwargs); - } + changes = Py_XNewRef(kwargs); return_value = namespace___replace___impl((_PyNamespaceObject *)self, changes); exit: @@ -42,4 +34,4 @@ namespace___replace__(PyObject *self, PyObject *args, PyObject *kwargs) return return_value; } -/*[clinic end generated code: output=67b5fcb6a6aee559 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=1b1c015f14360d64 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/picklebufobject.c.h b/Objects/clinic/picklebufobject.c.h new file mode 100644 index 00000000000000..e694942191a0fe --- /dev/null +++ b/Objects/clinic/picklebufobject.c.h @@ -0,0 +1,61 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +#include "pycore_modsupport.h" // _PyArg_CheckPositional() + +PyDoc_STRVAR(picklebuf_new__doc__, +"PickleBuffer(buffer, /)\n" +"--\n" +"\n" +"Wrapper for potentially out-of-band buffers."); + +static PyObject * +picklebuf_new_impl(PyTypeObject *type, PyObject *base); + +static PyObject * +picklebuf_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyTypeObject *base_tp = &PyPickleBuffer_Type; + PyObject *base; + + if ((type == base_tp || type->tp_init == base_tp->tp_init) && + !_PyArg_NoKeywords("PickleBuffer", kwargs)) { + goto exit; + } + if (!_PyArg_CheckPositional("PickleBuffer", PyTuple_GET_SIZE(args), 1, 1)) { + goto exit; + } + base = PyTuple_GET_ITEM(args, 0); + return_value = picklebuf_new_impl(type, base); + +exit: + return return_value; +} + +static PyObject * +picklebuf_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *base; + + assert(_PyType_CAST(type)->tp_new == picklebuf_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("PickleBuffer", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("PickleBuffer", nargs, 1, 1)) { + goto exit; + } + base = args[0]; + return_value = picklebuf_new_impl(_PyType_CAST(type), base); + +exit: + return return_value; +} +/*[clinic end generated code: output=4c32a9c4ea5b9bfe input=a9049054013a1b77]*/ diff --git a/Objects/clinic/structseq.c.h b/Objects/clinic/structseq.c.h index f115f8ddcf490c..ce5014311f4cca 100644 --- a/Objects/clinic/structseq.c.h +++ b/Objects/clinic/structseq.c.h @@ -87,15 +87,7 @@ structseq___replace__(PyObject *self, PyObject *args, PyObject *kwargs) if (!_PyArg_NoPositional("__replace__", args)) { goto exit; } - if (kwargs == NULL) { - changes = PyDict_New(); - if (changes == NULL) { - goto exit; - } - } - else { - changes = Py_NewRef(kwargs); - } + changes = Py_XNewRef(kwargs); return_value = structseq___replace___impl((PyStructSequence *)self, changes); exit: @@ -104,4 +96,4 @@ structseq___replace__(PyObject *self, PyObject *args, PyObject *kwargs) return return_value; } -/*[clinic end generated code: output=18fcf88e5df26774 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f0231faecca9248b input=a9049054013a1b77]*/ diff --git a/Objects/clinic/templateobject.c.h b/Objects/clinic/templateobject.c.h new file mode 100644 index 00000000000000..7719fcf47bb80e --- /dev/null +++ b/Objects/clinic/templateobject.c.h @@ -0,0 +1,55 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +#include "pycore_modsupport.h" // _PyArg_NoKeywords() +#include "pycore_tuple.h" // _PyTuple_ITEMS() + +static PyObject * +template_new_impl(PyTypeObject *type, PyObject * const *args, + Py_ssize_t args_length); + +static PyObject * +template_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyTypeObject *base_tp = &_PyTemplate_Type; + PyObject * const *__clinic_args; + Py_ssize_t args_length; + + if ((type == base_tp || type->tp_init == base_tp->tp_init) && + !_PyArg_NoKeywords("Template", kwargs)) { + goto exit; + } + __clinic_args = _PyTuple_ITEMS(args); + args_length = PyTuple_GET_SIZE(args); + return_value = template_new_impl(type, __clinic_args, args_length); + +exit: + return return_value; +} + +static PyObject * +template_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject * const *__clinic_args; + Py_ssize_t args_length; + + assert(_PyType_CAST(type)->tp_new == template_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("Template", kwnames)) { + goto exit; + } + __clinic_args = args; + args_length = nargs; + return_value = template_new_impl(_PyType_CAST(type), __clinic_args, args_length); + +exit: + return return_value; +} +/*[clinic end generated code: output=38d45f3b38a21517 input=a9049054013a1b77]*/ diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 4d2b5dc8e4613f..8a8fc240dd80ba 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -415,6 +415,9 @@ complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval) { PyObject *op; + if (type == &PyComplex_Type) { + return PyComplex_FromCComplex(cval); + } op = type->tp_alloc(type, 0); if (op != NULL) ((PyComplexObject *)op)->cval = cval; diff --git a/Objects/descrobject.c b/Objects/descrobject.c index c4e525797d19ae..6f3be87e0ca753 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1300,6 +1300,7 @@ PyDictProxy_New(PyObject *mapping) } /*[clinic input] +@vectorcall @classmethod mappingproxy.__new__ as mappingproxy_new @@ -1310,7 +1311,7 @@ Read-only proxy of a mapping. static PyObject * mappingproxy_new_impl(PyTypeObject *type, PyObject *mapping) -/*[clinic end generated code: output=65f27f02d5b68fa7 input=c156df096ef7590c]*/ +/*[clinic end generated code: output=65f27f02d5b68fa7 input=7388846480c052d2]*/ { return PyDictProxy_New(mapping); } @@ -2057,6 +2058,7 @@ PyTypeObject PyDictProxy_Type = { 0, /* tp_init */ 0, /* tp_alloc */ mappingproxy_new, /* tp_new */ + .tp_vectorcall = mappingproxy_vectorcall, }; PyTypeObject PyProperty_Type = { diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 88a8d61828503f..c9cc5d8c566fc6 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -4300,7 +4300,9 @@ static int dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **dupkey) { assert(can_modify_dict(mp)); - ASSERT_DICT_LOCKED(other); + if (!PyFrozenDict_Check(other)) { + ASSERT_DICT_LOCKED(other); + } if (other == mp || other->ma_used == 0) /* a.update(a) or a.update({}); nothing to do */ @@ -4666,7 +4668,14 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict) } if (copy == NULL) return NULL; - if (dict_merge(copy, o, 1, NULL) < 0) { + int res; + if (Py_TYPE(mp)->tp_iter == dict_iter) { + res = dict_dict_merge((PyDictObject *)copy, mp, 1, NULL); + } + else { + res = dict_merge(copy, o, 1, NULL); + } + if (res < 0) { Py_DECREF(copy); return NULL; } @@ -4970,7 +4979,7 @@ dict_setdefault_ref_lock_held(PyObject *d, PyObject *key, PyObject *default_valu } } - if (ix == DKIX_EMPTY) { + if (ix == DKIX_EMPTY || value == NULL) { value = default_value; // See comment to this function in insertdict. @@ -5696,7 +5705,10 @@ PyDict_DelItemString(PyObject *v, const char *key) typedef struct { PyObject_HEAD - PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ + /* Set to NULL when the iterator is exhausted, except in the free-threaded + build, where the reference is kept until the iterator is deallocated + (see dictiter_set_exhausted()). */ + PyDictObject *di_dict; Py_ssize_t di_used; Py_ssize_t di_pos; PyObject* di_result; /* reusable result tuple for iteritems */ @@ -5791,6 +5803,28 @@ static PyMethodDef dictiter_methods[] = { {NULL, NULL} /* sentinel */ }; +/* Mark the iterator as exhausted. In the free-threaded build an iterator + may be advanced by several threads at once, each holding a borrowed + pointer to di_dict, so the iterator keeps its reference to the dict until + it is deallocated (as list and tuple iterators do) and records exhaustion + by setting di_pos to DICTITER_EXHAUSTED. This is distinct from -1, the + starting position of a reverse iterator over an empty dict. */ +#ifdef Py_GIL_DISABLED +#define DICTITER_EXHAUSTED PY_SSIZE_T_MIN +#endif + +static void +dictiter_set_exhausted(dictiterobject *di, PyDictObject *d) +{ +#ifdef Py_GIL_DISABLED + _Py_atomic_store_ssize_relaxed(&di->di_pos, DICTITER_EXHAUSTED); + _Py_atomic_store_ssize_relaxed(&di->len, 0); +#else + di->di_dict = NULL; + Py_DECREF(d); +#endif +} + #ifdef Py_GIL_DISABLED static int @@ -6056,6 +6090,11 @@ dictiter_iternextitem_lock_held(PyDictObject *d, PyObject *self, assert (PyAnyDict_Check(d)); ASSERT_DICT_LOCKED(d); + i = FT_ATOMIC_LOAD_SSIZE_RELAXED(di->di_pos); + if (i < 0) { + return -1; /* exhausted */ + } + if (di->di_used != d->ma_used) { PyErr_SetString(PyExc_RuntimeError, "dictionary changed size during iteration"); @@ -6063,9 +6102,6 @@ dictiter_iternextitem_lock_held(PyDictObject *d, PyObject *self, return -1; } - i = FT_ATOMIC_LOAD_SSIZE_RELAXED(di->di_pos); - - assert(i >= 0); if (_PyDict_HasSplitTable(d)) { if (i >= d->ma_used) goto fail; @@ -6116,8 +6152,7 @@ dictiter_iternextitem_lock_held(PyDictObject *d, PyObject *self, return 0; fail: - di->di_dict = NULL; - Py_DECREF(d); + dictiter_set_exhausted(di, d); return -1; } @@ -6161,6 +6196,11 @@ dictiter_iternext_threadsafe(PyDictObject *d, PyObject *self, assert (PyAnyDict_Check(d)); + i = _Py_atomic_load_ssize_relaxed(&di->di_pos); + if (i == DICTITER_EXHAUSTED) { + return -1; + } + if (di->di_used != _Py_atomic_load_ssize_relaxed(&d->ma_used)) { PyErr_SetString(PyExc_RuntimeError, "dictionary changed size during iteration"); @@ -6170,9 +6210,7 @@ dictiter_iternext_threadsafe(PyDictObject *d, PyObject *self, ensure_shared_on_read(d); - i = _Py_atomic_load_ssize_relaxed(&di->di_pos); k = _Py_atomic_load_ptr_acquire(&d->ma_keys); - assert(i >= 0); if (_PyDict_HasSplitTable(d)) { PyDictValues *values = _Py_atomic_load_ptr_consume(&d->ma_values); if (values == NULL) { @@ -6251,8 +6289,7 @@ dictiter_iternext_threadsafe(PyDictObject *d, PyObject *self, "dictionary keys changed during iteration"); fail: - di->di_dict = NULL; - Py_DECREF(d); + dictiter_set_exhausted(di, d); return -1; try_locked: @@ -6354,6 +6391,12 @@ dictreviter_iter_lock_held(PyDictObject *d, PyObject *self) assert (PyAnyDict_Check(d)); ASSERT_DICT_LOCKED(d); +#ifdef Py_GIL_DISABLED + if (di->di_pos == DICTITER_EXHAUSTED) { + return NULL; + } +#endif + if (di->di_used != d->ma_used) { PyErr_SetString(PyExc_RuntimeError, "dictionary changed size during iteration"); @@ -6440,8 +6483,7 @@ dictreviter_iter_lock_held(PyDictObject *d, PyObject *self) } fail: - di->di_dict = NULL; - Py_DECREF(d); + dictiter_set_exhausted(di, d); return NULL; } @@ -8137,6 +8179,7 @@ PyObject_ClearManagedDict(PyObject *obj) /* Clear the dict */ Py_BEGIN_CRITICAL_SECTION(dict); PyDictKeysObject *oldkeys = dict->ma_keys; + ensure_shared_on_resize(dict); set_keys(dict, Py_EMPTY_KEYS); dict->ma_values = NULL; dictkeys_decref(oldkeys, IS_DICT_SHARED(dict)); @@ -8653,16 +8696,22 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return d; } -static void +static int transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict) { PyDictObject *new = (PyDictObject *)res; PyDictObject *old = (PyDictObject *)dict; assert(can_modify_dict(old)); + if (old->ma_values && old->ma_values->embedded) { + PyErr_SetString(PyExc_ValueError, + "can't convert dict with embedded values"); + return -1; + } + // Fast path: do nothing on an empty dict: if (old->ma_keys == Py_EMPTY_KEYS) { - return; + return 0; } Py_ssize_t used = old->ma_used; @@ -8680,6 +8729,7 @@ transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict) new->ma_keys = keys; new->ma_values = values; ASSERT_CONSISTENT(new); + return 0; } PyObject * @@ -8695,9 +8745,14 @@ PyDict_AsFrozenDictAndClear(PyObject *dict) return NULL; } + int transfer; Py_BEGIN_CRITICAL_SECTION(dict); - transfer_keys_and_values_lock_held(res, dict); + transfer = transfer_keys_and_values_lock_held(res, dict); Py_END_CRITICAL_SECTION(); + if (transfer < 0) { + Py_DECREF(res); + return NULL; + } _PyObject_GC_TRACK(res); assert(_PyFrozenDictObject_CAST(res)->ma_hash == -1); diff --git a/Objects/exceptions.c b/Objects/exceptions.c index e2f757c5ee9c0b..dc0ba0d7e533dd 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -24,8 +24,11 @@ /*[clinic input] class BaseException "PyBaseExceptionObject *" "&PyExc_BaseException" class BaseExceptionGroup "PyBaseExceptionGroupObject *" "&PyExc_BaseExceptionGroup" +class ImportError "PyImportErrorObject *" "&PyExc_ImportError" +class NameError "PyNameErrorObject *" "&PyExc_NameError" +class AttributeError "PyAttributeErrorObject *" "&PyExc_AttributeError" [clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b7c45e78cff8edc3]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=fce4682940ab8948]*/ static struct _Py_exc_state* @@ -1778,29 +1781,27 @@ PyImportErrorObject_CAST(PyObject *self) return (PyImportErrorObject *)self; } +/*[clinic input] +@vectorcall +ImportError.__init__ as ImportError_init + + *args: tuple + name: object = NULL + path: object = NULL + name_from: object = NULL + +[clinic start generated code]*/ + static int -ImportError_init(PyObject *op, PyObject *args, PyObject *kwds) +ImportError_init_impl(PyImportErrorObject *self, PyObject *args, + PyObject *name, PyObject *path, PyObject *name_from) +/*[clinic end generated code: output=4917add0f9473bd2 input=4e94a3b530f9be97]*/ { - static char *kwlist[] = {"name", "path", "name_from", 0}; - PyObject *empty_tuple; PyObject *msg = NULL; - PyObject *name = NULL; - PyObject *path = NULL; - PyObject *name_from = NULL; - if (BaseException_init(op, args, NULL) == -1) - return -1; - - PyImportErrorObject *self = PyImportErrorObject_CAST(op); - empty_tuple = PyTuple_New(0); - if (!empty_tuple) - return -1; - if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OOO:ImportError", kwlist, - &name, &path, &name_from)) { - Py_DECREF(empty_tuple); + if (BaseException_init((PyObject *)self, args, NULL) == -1) { return -1; } - Py_DECREF(empty_tuple); Py_XSETREF(self->name, Py_XNewRef(name)); Py_XSETREF(self->path, Py_XNewRef(path)); @@ -1814,6 +1815,7 @@ ImportError_init(PyObject *op, PyObject *args, PyObject *kwds) return 0; } + static int ImportError_clear(PyObject *op) { @@ -2579,33 +2581,29 @@ PyNameErrorObject_CAST(PyObject *self) return (PyNameErrorObject *)self; } -static int -NameError_init(PyObject *op, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"name", NULL}; - PyObject *name = NULL; +/*[clinic input] +@vectorcall +NameError.__init__ as NameError_init - if (BaseException_init(op, args, NULL) == -1) { - return -1; - } + *args: tuple + name: object = NULL - PyObject *empty_tuple = PyTuple_New(0); - if (!empty_tuple) { - return -1; - } - if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$O:NameError", kwlist, - &name)) { - Py_DECREF(empty_tuple); +[clinic start generated code]*/ + +static int +NameError_init_impl(PyNameErrorObject *self, PyObject *args, PyObject *name) +/*[clinic end generated code: output=f15305075d9aa272 input=2e2b7e781ae146b1]*/ +{ + if (BaseException_init((PyObject *)self, args, NULL) == -1) { return -1; } - Py_DECREF(empty_tuple); - PyNameErrorObject *self = PyNameErrorObject_CAST(op); Py_XSETREF(self->name, Py_XNewRef(name)); return 0; } + static int NameError_clear(PyObject *op) { @@ -2662,35 +2660,32 @@ PyAttributeErrorObject_CAST(PyObject *self) return (PyAttributeErrorObject *)self; } -static int -AttributeError_init(PyObject *op, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"name", "obj", NULL}; - PyObject *name = NULL; - PyObject *obj = NULL; +/*[clinic input] +@vectorcall +AttributeError.__init__ as AttributeError_init - if (BaseException_init(op, args, NULL) == -1) { - return -1; - } + *args: tuple + name: object = NULL + obj: object = NULL - PyObject *empty_tuple = PyTuple_New(0); - if (!empty_tuple) { - return -1; - } - if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:AttributeError", kwlist, - &name, &obj)) { - Py_DECREF(empty_tuple); +[clinic start generated code]*/ + +static int +AttributeError_init_impl(PyAttributeErrorObject *self, PyObject *args, + PyObject *name, PyObject *obj) +/*[clinic end generated code: output=3d346f9a389f60fa input=bed43aafea47f1a0]*/ +{ + if (BaseException_init((PyObject *)self, args, NULL) == -1) { return -1; } - Py_DECREF(empty_tuple); - PyAttributeErrorObject *self = PyAttributeErrorObject_CAST(op); Py_XSETREF(self->name, Py_XNewRef(name)); Py_XSETREF(self->obj, Py_XNewRef(obj)); return 0; } + static int AttributeError_clear(PyObject *op) { @@ -4565,6 +4560,15 @@ _PyExc_InitTypes(PyInterpreterState *interp) { exc->tp_vectorcall = BaseException_vectorcall; } + else if (exc->tp_init == ImportError_init) { + exc->tp_vectorcall = ImportError_vectorcall; + } + else if (exc->tp_init == NameError_init) { + exc->tp_vectorcall = NameError_vectorcall; + } + else if (exc->tp_init == AttributeError_init) { + exc->tp_vectorcall = AttributeError_vectorcall; + } } return 0; } diff --git a/Objects/floatobject.c b/Objects/floatobject.c index c19ed6e6b60935..e7b904c9b213b8 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -260,6 +260,11 @@ PyFloat_AsDouble(PyObject *op) return PyFloat_AS_DOUBLE(op); } + // int subclass can override __float__() method + if (PyLong_CheckExact(op)) { + return PyLong_AsDouble(op); + } + nb = Py_TYPE(op)->tp_as_number; if (nb == NULL || nb->nb_float == NULL) { if (nb && nb->nb_index) { diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 475be76dc3da21..909e5b7a2700cd 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -14,6 +14,8 @@ #include "pycore_object.h" // _PyObject_GC_UNTRACK() #include "pycore_opcode_metadata.h" // _PyOpcode_Caches #include "pycore_optimizer.h" // _Py_Executors_InvalidateDependency() +#include "pycore_pystate.h" // _PyEval_StopTheWorld() +#include "pycore_tstate.h" // _PyThreadStateImpl #include "pycore_tuple.h" // _PyTuple_FromPair #include "pycore_unicodeobject.h" // _PyUnicode_Equal() #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() @@ -40,6 +42,85 @@ class frame "PyFrameObject *" "&PyFrame_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2d1dbf2e06cf351f]*/ +/*[clinic input] +class FrameLocalsProxy "PyFrameLocalsProxyObject *" "&PyFrameLocalsProxy_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b877b1c7551e6925]*/ + + +#ifdef Py_GIL_DISABLED +// Returns 1 if the frame may be executing or resumed on another thread. +static int +_frame_is_on_other_thread(PyFrameObject *frame) +{ + _PyInterpreterFrame *iframe = frame->f_frame; + if (iframe->owner == FRAME_OWNED_BY_THREAD) { + PyThreadState *tstate = _PyThreadState_GET(); + int32_t our_tlbc = ((_PyThreadStateImpl *)tstate)->tlbc_index; + return iframe->tlbc_index != our_tlbc; + } + if (iframe->owner == FRAME_OWNED_BY_GENERATOR) { + // Another thread may be running or resume the generator. + return 1; + } + // FRAME_OWNED_BY_FRAME_OBJECT: the frame can no longer run. + return 0; +} +#endif + +// Evaluate CALL under the frame's critical section, or with the world +// stopped if the frame may be running on another thread. +#ifdef Py_GIL_DISABLED +#define FRAMELOCALSPROXY_LOCKED(FRAME, CALL, RESULT) \ + do { \ + int _stw; \ + Py_BEGIN_CRITICAL_SECTION(FRAME); \ + _stw = _frame_is_on_other_thread(FRAME); \ + if (!_stw) { \ + RESULT = CALL; \ + } \ + Py_END_CRITICAL_SECTION(); \ + if (_stw) { \ + PyInterpreterState *interp = _PyInterpreterState_GET(); \ + _PyEval_StopTheWorld(interp); \ + RESULT = CALL; \ + _PyEval_StartTheWorld(interp); \ + } \ + } while (0) +#else +#define FRAMELOCALSPROXY_LOCKED(FRAME, CALL, RESULT) \ + do { \ + RESULT = CALL; \ + } while (0) +#endif + + +// Returns a borrowed reference, the frame object keeps its code alive. +static PyCodeObject * +framelocalsproxy_getcode(PyFrameObject *frame) +{ + PyCodeObject *co; + Py_BEGIN_CRITICAL_SECTION(frame); + co = _PyFrame_GetCode(frame->f_frame); + Py_END_CRITICAL_SECTION(); + return co; +} + +// Returns a new reference to the extra locals dict or NULL. The running +// frame never touches it, so no stop the world is needed. +static PyObject * +framelocalsproxy_get_extra(PyFrameObject *frame, bool create) +{ + PyObject *extra; + Py_BEGIN_CRITICAL_SECTION(frame); + extra = frame->f_extra_locals; + if (extra == NULL && create) { + extra = frame->f_extra_locals = PyDict_New(); + } + Py_XINCREF(extra); + Py_END_CRITICAL_SECTION(); + return extra; +} // Returns new reference or NULL static PyObject * @@ -127,7 +208,7 @@ framelocalsproxy_getkeyindex(PyFrameObject *frame, PyObject *key, bool read, PyO // value_ptr should only be given if we are reading the value assert(read || value_ptr == NULL); - PyCodeObject *co = _PyFrame_GetCode(frame->f_frame); + PyCodeObject *co = framelocalsproxy_getcode(frame); // Ensure that the key is hashable. Py_hash_t key_hash = PyObject_Hash(key); @@ -143,7 +224,9 @@ framelocalsproxy_getkeyindex(PyFrameObject *frame, PyObject *key, bool read, PyO PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i); if (name == key) { if (read) { - PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i); + PyObject *value; + FRAMELOCALSPROXY_LOCKED(frame, + framelocalsproxy_getval(frame->f_frame, co, i), value); if (value != NULL) { if (value_ptr != NULL) { *value_ptr = value; @@ -181,7 +264,9 @@ framelocalsproxy_getkeyindex(PyFrameObject *frame, PyObject *key, bool read, PyO } if (same) { if (read) { - PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i); + PyObject *value; + FRAMELOCALSPROXY_LOCKED(frame, + framelocalsproxy_getval(frame->f_frame, co, i), value); if (value != NULL) { if (value_ptr != NULL) { *value_ptr = value; @@ -220,12 +305,11 @@ framelocalsproxy_getitem(PyObject *self, PyObject *key) // Okay not in the fast locals, try extra locals - PyObject *extra = frame->f_extra_locals; + PyObject *extra = framelocalsproxy_get_extra(frame, false); if (extra != NULL) { - if (PyDict_GetItemRef(extra, key, &value) < 0) { - return NULL; - } - if (value != NULL) { + int found = PyDict_GetItemRef(extra, key, &value); + Py_DECREF(extra); + if (found != 0) { return value; } } @@ -261,14 +345,55 @@ add_overwritten_fast_local(PyFrameObject *frame, PyObject *obj) return 0; } +// The replaced value is returned in `*old_value` so that it is decref'd +// outside stop the world. static int -framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value) +framelocalsproxy_setfast_lock_held(PyFrameObject *frame, int i, + PyObject *value, PyObject **old_value) { /* Merge locals into fast locals */ - PyFrameObject *frame = PyFrameLocalsProxyObject_CAST(self)->frame; _PyStackRef *fast = _PyFrame_GetLocalsArray(frame->f_frame); PyCodeObject *co = _PyFrame_GetCode(frame->f_frame); +#if _Py_TIER2 + _Py_Executors_InvalidateDependency(_PyInterpreterState_GET(), co, 1); +#endif + + _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i); + _PyStackRef oldvalue = fast[i]; + PyObject *cell = NULL; + if (kind == CO_FAST_FREE) { + // The cell was set when the frame was created from + // the function's closure, unless the frame has been cleared. + assert(PyStackRef_IsNull(oldvalue) || PyCell_Check(PyStackRef_AsPyObjectBorrow(oldvalue))); + cell = PyStackRef_AsPyObjectBorrow(oldvalue); + } else if (kind & CO_FAST_CELL && !PyStackRef_IsNull(oldvalue)) { + PyObject *as_obj = PyStackRef_AsPyObjectBorrow(oldvalue); + if (PyCell_Check(as_obj)) { + cell = as_obj; + } + } + if (cell != NULL) { + Py_XINCREF(value); + *old_value = PyCell_SwapTakeRef((PyCellObject *)cell, value); + } else if (value != PyStackRef_AsPyObjectBorrow(oldvalue)) { + PyObject *old_obj = PyStackRef_AsPyObjectBorrow(fast[i]); + if (old_obj != NULL && !_Py_IsImmortal(old_obj)) { + if (add_overwritten_fast_local(frame, old_obj) < 0) { + return -1; + } + PyStackRef_CLOSE(fast[i]); + } + fast[i] = PyStackRef_FromPyObjectNew(value); + } + return 0; +} + +static int +framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value) +{ + PyFrameObject *frame = PyFrameLocalsProxyObject_CAST(self)->frame; + int i = framelocalsproxy_getkeyindex(frame, key, false, NULL); if (i == -2) { return -1; @@ -278,64 +403,33 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value) PyErr_SetString(PyExc_ValueError, "cannot remove local variables from FrameLocalsProxy"); return -1; } - -#if _Py_TIER2 - _Py_Executors_InvalidateDependency(_PyInterpreterState_GET(), co, 1); -#endif - - _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i); - _PyStackRef oldvalue = fast[i]; - PyObject *cell = NULL; - if (kind == CO_FAST_FREE) { - // The cell was set when the frame was created from - // the function's closure. - assert(!PyStackRef_IsNull(oldvalue) && PyCell_Check(PyStackRef_AsPyObjectBorrow(oldvalue))); - cell = PyStackRef_AsPyObjectBorrow(oldvalue); - } else if (kind & CO_FAST_CELL && !PyStackRef_IsNull(oldvalue)) { - PyObject *as_obj = PyStackRef_AsPyObjectBorrow(oldvalue); - if (PyCell_Check(as_obj)) { - cell = as_obj; - } - } - if (cell != NULL) { - Py_XINCREF(value); - PyCell_SetTakeRef((PyCellObject *)cell, value); - } else if (value != PyStackRef_AsPyObjectBorrow(oldvalue)) { - PyObject *old_obj = PyStackRef_AsPyObjectBorrow(fast[i]); - if (old_obj != NULL && !_Py_IsImmortal(old_obj)) { - if (add_overwritten_fast_local(frame, old_obj) < 0) { - return -1; - } - PyStackRef_CLOSE(fast[i]); - } - fast[i] = PyStackRef_FromPyObjectNew(value); - } - return 0; + PyObject *old_value = NULL; + int result; + FRAMELOCALSPROXY_LOCKED(frame, + framelocalsproxy_setfast_lock_held(frame, i, value, &old_value), + result); + Py_XDECREF(old_value); + return result; } // Okay not in the fast locals, try extra locals - PyObject *extra = frame->f_extra_locals; - + PyObject *extra = framelocalsproxy_get_extra(frame, value != NULL); if (extra == NULL) { if (value == NULL) { _PyErr_SetKeyError(key); - return -1; - } - extra = PyDict_New(); - if (extra == NULL) { - return -1; } - frame->f_extra_locals = extra; + return -1; } - assert(PyDict_Check(extra)); - + int result; if (value == NULL) { - return PyDict_DelItem(extra, key); + result = PyDict_DelItem(extra, key); } else { - return PyDict_SetItem(extra, key, value); + result = PyDict_SetItem(extra, key, value); } + Py_DECREF(extra); + return result; } static int @@ -388,9 +482,8 @@ framelocalsproxy_merge(PyObject* self, PyObject* other) } static PyObject * -framelocalsproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored)) +framelocalsproxy_keys_lock_held(PyFrameObject *frame) { - PyFrameObject *frame = PyFrameLocalsProxyObject_CAST(self)->frame; PyCodeObject *co = _PyFrame_GetCode(frame->f_frame); PyObject *names = PyList_New(0); if (names == NULL) { @@ -426,13 +519,20 @@ framelocalsproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored)) Py_ssize_t i = 0; PyObject *key = NULL; PyObject *value = NULL; + int err = 0; + Py_BEGIN_CRITICAL_SECTION(frame->f_extra_locals); while (PyDict_Next(frame->f_extra_locals, &i, &key, &value)) { if (PyList_Append(names, key) < 0) { - Py_DECREF(names); - return NULL; + err = -1; + break; } } + Py_END_CRITICAL_SECTION(); + if (err < 0) { + Py_DECREF(names); + return NULL; + } } return names; @@ -443,6 +543,16 @@ framelocalsproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored)) return NULL; } +static PyObject * +framelocalsproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyFrameObject *frame = PyFrameLocalsProxyObject_CAST(self)->frame; + PyObject *result; + FRAMELOCALSPROXY_LOCKED(frame, + framelocalsproxy_keys_lock_held(frame), result); + return result; +} + static void framelocalsproxy_dealloc(PyObject *self) { @@ -452,36 +562,32 @@ framelocalsproxy_dealloc(PyObject *self) Py_TYPE(self)->tp_free(self); } +/*[clinic input] +@vectorcall +@classmethod +FrameLocalsProxy.__new__ as framelocalsproxy_new + frame: object + / + +Create a write-through view of the locals dictionary for a frame. + + frame + the frame object to wrap. +[clinic start generated code]*/ + static PyObject * -framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +framelocalsproxy_new_impl(PyTypeObject *type, PyObject *frame) +/*[clinic end generated code: output=803b377132a928b9 input=5ab4ffd26ec8e290]*/ { - if (PyTuple_GET_SIZE(args) != 1) { - PyErr_Format(PyExc_TypeError, - "FrameLocalsProxy expected 1 argument, got %zd", - PyTuple_GET_SIZE(args)); + if (!PyFrame_Check(frame)) { + PyErr_Format(PyExc_TypeError, "expect frame, not %T", frame); return NULL; } - PyObject *item = PyTuple_GET_ITEM(args, 0); - - if (!PyFrame_Check(item)) { - PyErr_Format(PyExc_TypeError, "expect frame, not %T", item); - return NULL; - } - PyFrameObject *frame = (PyFrameObject*)item; - - if (kwds != NULL && PyDict_Size(kwds) != 0) { - PyErr_SetString(PyExc_TypeError, - "FrameLocalsProxy takes no keyword arguments"); - return 0; - } - PyFrameLocalsProxyObject *self = (PyFrameLocalsProxyObject *)type->tp_alloc(type, 0); if (self == NULL) { return NULL; } - - ((PyFrameLocalsProxyObject*)self)->frame = (PyFrameObject*)Py_NewRef(frame); - + self->frame = (PyFrameObject *)Py_NewRef(frame); return (PyObject *)self; } @@ -614,9 +720,8 @@ framelocalsproxy_inplace_or(PyObject *self, PyObject *other) } static PyObject * -framelocalsproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored)) +framelocalsproxy_values_lock_held(PyFrameObject *frame) { - PyFrameObject *frame = PyFrameLocalsProxyObject_CAST(self)->frame; PyCodeObject *co = _PyFrame_GetCode(frame->f_frame); PyObject *values = PyList_New(0); if (values == NULL) { @@ -652,12 +757,19 @@ framelocalsproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored)) Py_ssize_t j = 0; PyObject *key = NULL; PyObject *value = NULL; + int err = 0; + Py_BEGIN_CRITICAL_SECTION(frame->f_extra_locals); while (PyDict_Next(frame->f_extra_locals, &j, &key, &value)) { if (PyList_Append(values, value) < 0) { - Py_DECREF(values); - return NULL; + err = -1; + break; } } + Py_END_CRITICAL_SECTION(); + if (err < 0) { + Py_DECREF(values); + return NULL; + } } return values; @@ -669,9 +781,18 @@ framelocalsproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored)) } static PyObject * -framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored)) +framelocalsproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored)) { PyFrameObject *frame = PyFrameLocalsProxyObject_CAST(self)->frame; + PyObject *result; + FRAMELOCALSPROXY_LOCKED(frame, + framelocalsproxy_values_lock_held(frame), result); + return result; +} + +static PyObject * +framelocalsproxy_items_lock_held(PyFrameObject *frame) +{ PyCodeObject *co = _PyFrame_GetCode(frame->f_frame); PyObject *items = PyList_New(0); if (items == NULL) { @@ -714,16 +835,21 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored)) Py_ssize_t j = 0; PyObject *key = NULL; PyObject *value = NULL; + int err = 0; + Py_BEGIN_CRITICAL_SECTION(frame->f_extra_locals); while (PyDict_Next(frame->f_extra_locals, &j, &key, &value)) { PyObject *pair = _PyTuple_FromPair(key, value); - if (pair == NULL) { - goto error; - } - - if (_PyList_AppendTakeRef((PyListObject *)items, pair) < 0) { - goto error; + if (pair == NULL + || _PyList_AppendTakeRef((PyListObject *)items, pair) < 0) + { + err = -1; + break; } } + Py_END_CRITICAL_SECTION(); + if (err < 0) { + goto error; + } } return items; @@ -734,10 +860,19 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored)) return NULL; } -static Py_ssize_t -framelocalsproxy_length(PyObject *self) +static PyObject * +framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored)) { PyFrameObject *frame = PyFrameLocalsProxyObject_CAST(self)->frame; + PyObject *result; + FRAMELOCALSPROXY_LOCKED(frame, + framelocalsproxy_items_lock_held(frame), result); + return result; +} + +static Py_ssize_t +framelocalsproxy_length_lock_held(PyFrameObject *frame) +{ PyCodeObject *co = _PyFrame_GetCode(frame->f_frame); Py_ssize_t size = 0; @@ -767,6 +902,16 @@ framelocalsproxy_length(PyObject *self) return size; } +static Py_ssize_t +framelocalsproxy_length(PyObject *self) +{ + PyFrameObject *frame = PyFrameLocalsProxyObject_CAST(self)->frame; + Py_ssize_t result; + FRAMELOCALSPROXY_LOCKED(frame, + framelocalsproxy_length_lock_held(frame), result); + return result; +} + static int framelocalsproxy_contains(PyObject *self, PyObject *key) { @@ -780,9 +925,11 @@ framelocalsproxy_contains(PyObject *self, PyObject *key) return 1; } - PyObject *extra = frame->f_extra_locals; + PyObject *extra = framelocalsproxy_get_extra(frame, false); if (extra != NULL) { - return PyDict_Contains(extra, key); + int result = PyDict_Contains(extra, key); + Py_DECREF(extra); + return result; } return 0; @@ -895,7 +1042,8 @@ framelocalsproxy_pop(PyObject* self, PyObject *const *args, Py_ssize_t nargs) PyObject *result = NULL; - if (frame->f_extra_locals == NULL) { + PyObject *extra = framelocalsproxy_get_extra(frame, false); + if (extra == NULL) { if (default_value != NULL) { return Py_XNewRef(default_value); } else { @@ -904,7 +1052,9 @@ framelocalsproxy_pop(PyObject* self, PyObject *const *args, Py_ssize_t nargs) } } - if (PyDict_Pop(frame->f_extra_locals, key, &result) < 0) { + int found = PyDict_Pop(extra, key, &result); + Py_DECREF(extra); + if (found < 0) { return NULL; } @@ -988,15 +1138,6 @@ static PyMethodDef framelocalsproxy_methods[] = { {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(framelocalsproxy_doc, -"FrameLocalsProxy($frame)\n" -"--\n" -"\n" -"Create a write-through view of the locals dictionary for a frame.\n" -"\n" -" frame\n" -" the frame object to wrap."); - PyTypeObject PyFrameLocalsProxy_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) .tp_name = "FrameLocalsProxy", @@ -1017,20 +1158,14 @@ PyTypeObject PyFrameLocalsProxy_Type = { .tp_alloc = PyType_GenericAlloc, .tp_new = framelocalsproxy_new, .tp_free = PyObject_GC_Del, - .tp_doc = framelocalsproxy_doc, + .tp_doc = framelocalsproxy_new__doc__, + .tp_vectorcall = framelocalsproxy_vectorcall, }; PyObject * _PyFrameLocalsProxy_New(PyFrameObject *frame) { - PyObject* args = PyTuple_Pack(1, frame); - if (args == NULL) { - return NULL; - } - - PyObject* proxy = framelocalsproxy_new(&PyFrameLocalsProxy_Type, args, NULL); - Py_DECREF(args); - return proxy; + return framelocalsproxy_new_impl(&PyFrameLocalsProxy_Type, (PyObject *)frame); } static PyMemberDef frame_memberlist[] = { @@ -2426,15 +2561,11 @@ _PyFrame_GetLocals(_PyInterpreterFrame *frame) } -PyObject * -PyFrame_GetVar(PyFrameObject *frame_obj, PyObject *name) +// Returns 1 if `name` is a bound fast local, storing its value in `*pvalue`. +static int +frame_getvar_lock_held(PyFrameObject *frame_obj, PyObject *name, + PyObject **pvalue) { - if (!PyUnicode_Check(name)) { - PyErr_Format(PyExc_TypeError, "name must be str, not %s", - Py_TYPE(name)->tp_name); - return NULL; - } - _PyInterpreterFrame *frame = frame_obj->f_frame; frame_init_get_vars(frame); @@ -2444,17 +2575,30 @@ PyFrame_GetVar(PyFrameObject *frame_obj, PyObject *name) if (!_PyUnicode_Equal(var_name, name)) { continue; } - - PyObject *value; - if (!frame_get_var(frame, co, i, &value)) { - break; - } - if (value == NULL) { - break; + if (!frame_get_var(frame, co, i, pvalue)) { + return 0; } - return value; + return *pvalue != NULL; } + return 0; +} +PyObject * +PyFrame_GetVar(PyFrameObject *frame_obj, PyObject *name) +{ + if (!PyUnicode_Check(name)) { + PyErr_Format(PyExc_TypeError, "name must be str, not %s", + Py_TYPE(name)->tp_name); + return NULL; + } + + PyObject *value = NULL; + int found; + FRAMELOCALSPROXY_LOCKED(frame_obj, + frame_getvar_lock_held(frame_obj, name, &value), found); + if (found) { + return value; + } PyErr_Format(PyExc_NameError, "variable %R does not exist", name); return NULL; } diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 99f08863da4604..2e0bcd7853b925 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -1096,6 +1096,7 @@ static PyGetSetDef func_getsetlist[] = { */ /*[clinic input] +@vectorcall @classmethod function.__new__ as func_new code: object(type="PyCodeObject *", subclass_of="&PyCode_Type") @@ -1118,7 +1119,7 @@ static PyObject * func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals, PyObject *name, PyObject *defaults, PyObject *closure, PyObject *kwdefaults) -/*[clinic end generated code: output=de72f4c22ac57144 input=20c9c9f04ad2d3f2]*/ +/*[clinic end generated code: output=de72f4c22ac57144 input=264767a5dc845c90]*/ { PyFunctionObject *newfunc; Py_ssize_t nclosure; @@ -1326,6 +1327,7 @@ PyTypeObject PyFunction_Type = { 0, /* tp_init */ 0, /* tp_alloc */ func_new, /* tp_new */ + .tp_vectorcall = func_vectorcall, }; diff --git a/Objects/interpolationobject.c b/Objects/interpolationobject.c index e37724fb7852a2..96fabcf201bb97 100644 --- a/Objects/interpolationobject.c +++ b/Objects/interpolationobject.c @@ -50,6 +50,7 @@ typedef struct { (assert(_PyInterpolation_CheckExact(op)), _Py_CAST(interpolationobject*, (op))) /*[clinic input] +@vectorcall @classmethod Interpolation.__new__ as interpolation_new @@ -63,7 +64,7 @@ static PyObject * interpolation_new_impl(PyTypeObject *type, PyObject *value, PyObject *expression, PyObject *conversion, PyObject *format_spec) -/*[clinic end generated code: output=6488e288765bc1a9 input=fc5c285c1dd23d36]*/ +/*[clinic end generated code: output=6488e288765bc1a9 input=de7e890ea8f801e5]*/ { interpolationobject *self = PyObject_GC_New(interpolationobject, type); if (!self) { @@ -150,6 +151,7 @@ PyTypeObject _PyInterpolation_Type = { .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, .tp_new = interpolation_new, + .tp_vectorcall = interpolation_vectorcall, .tp_alloc = PyType_GenericAlloc, .tp_dealloc = interpolation_dealloc, .tp_clear = interpolation_clear, diff --git a/Objects/iterobject.c b/Objects/iterobject.c index fcef1cc9c30143..683683ceaa6600 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -238,11 +238,33 @@ PyCallIter_New(PyObject *callable, PyObject *sentinel) return _PyCallIter_NewEx(callable, sentinel, NULL); } +// Sharing a callable iterator between threads, or calling __setstate__() +// on an iterator that is in use, is not supported: the values produced are +// unspecified, but the interpreter must not crash. + +static inline PyObject * +calliter_swap(PyObject **field, PyObject *value) +{ +#ifdef Py_GIL_DISABLED + return _Py_atomic_exchange_ptr(field, value); +#else + PyObject *old = *field; + *field = value; + return old; +#endif +} + +static inline PyObject * +calliter_load(PyObject **field) +{ + return Py_XNewRef(FT_ATOMIC_LOAD_PTR_ACQUIRE(*field)); +} + static void calliter_exhaust(calliterobject *it) { - Py_CLEAR(it->it_callable); - Py_CLEAR(it->it_sentinel); + _PyObject_XDecRefDelayed(calliter_swap(&it->it_callable, NULL)); + _PyObject_XDecRefDelayed(calliter_swap(&it->it_sentinel, NULL)); } static void @@ -270,19 +292,26 @@ static PyObject * calliter_iternext(PyObject *op) { calliterobject *it = (calliterobject*)op; - PyObject *result; + PyObject *callable, *sentinel, *stop_exc, *result; - if (it->it_callable == NULL) { + callable = calliter_load(&it->it_callable); + if (callable == NULL) { return NULL; } + result = _PyObject_CallNoArgs(callable); + Py_DECREF(callable); - result = _PyObject_CallNoArgs(it->it_callable); /* The call can exhaust the iterator re-entrantly. */ - if (result != NULL && it->it_callable != NULL) { - if (it->it_sentinel == NULL) { + if (result != NULL && + FT_ATOMIC_LOAD_PTR_RELAXED(it->it_callable) != NULL) + { + sentinel = calliter_load(&it->it_sentinel); + if (sentinel == NULL) { return result; /* Common case, fast path */ } - int ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ); + /* The comparison can run code that replaces the sentinel. */ + int ok = PyObject_RichCompareBool(sentinel, result, Py_EQ); + Py_DECREF(sentinel); if (ok == 0) { return result; /* Common case, fast path */ } @@ -291,14 +320,19 @@ calliter_iternext(PyObject *op) calliter_exhaust(it); } } - else if (PyErr_ExceptionMatches(it->it_stop_exc)) { - PyErr_Clear(); - calliter_exhaust(it); - } - else if (PyErr_ExceptionMatches(PyExc_StopIteration)) { - /* It would be mistaken for the end of the iteration (see PEP 479). */ - _PyErr_FormatFromCause(PyExc_RuntimeError, - "callable raised StopIteration"); + else if (result == NULL) { + stop_exc = calliter_load(&it->it_stop_exc); + if (PyErr_ExceptionMatches(stop_exc)) { + PyErr_Clear(); + calliter_exhaust(it); + } + else if (PyErr_ExceptionMatches(PyExc_StopIteration)) { + /* It would be mistaken for the end of the iteration (see + PEP 479). */ + _PyErr_FormatFromCause(PyExc_RuntimeError, + "callable raised StopIteration"); + } + Py_DECREF(stop_exc); } Py_XDECREF(result); return NULL; @@ -314,22 +348,29 @@ calliter_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) * call must be before access of iterator pointers. * see issue #101765 */ - if (it->it_callable == NULL) { - return Py_BuildValue("N(())", iter); + PyObject *callable = calliter_load(&it->it_callable); + PyObject *sentinel = calliter_load(&it->it_sentinel); + PyObject *stop_exc = calliter_load(&it->it_stop_exc); + PyObject *res; + if (callable == NULL) { + res = Py_BuildValue("N(())", iter); } /* Only the sentinel can be passed as an argument of iter(), so other attributes are restored from the state (see calliter_setstate()). */ - if (it->it_sentinel == NULL) { - return Py_BuildValue("N(OO)(()O)", iter, it->it_callable, Py_None, - it->it_stop_exc); + else if (sentinel == NULL) { + res = Py_BuildValue("N(OO)(()O)", iter, callable, Py_None, stop_exc); } - else if (it->it_stop_exc == PyExc_StopIteration) { - return Py_BuildValue("N(OO)", iter, it->it_callable, it->it_sentinel); + else if (stop_exc == PyExc_StopIteration) { + res = Py_BuildValue("N(OO)", iter, callable, sentinel); } else { - return Py_BuildValue("N(OO)((O)O)", iter, it->it_callable, Py_None, - it->it_sentinel, it->it_stop_exc); + res = Py_BuildValue("N(OO)((O)O)", iter, callable, Py_None, + sentinel, stop_exc); } + Py_XDECREF(callable); + Py_XDECREF(sentinel); + Py_DECREF(stop_exc); + return res; } static PyObject * @@ -349,11 +390,12 @@ calliter_setstate(PyObject *op, PyObject *state) if (_PyEval_CheckExceptTypeValid(_PyThreadState_GET(), stop_exc) < 0) { return NULL; } - if (it->it_callable != NULL) { - Py_XSETREF(it->it_sentinel, - PyTuple_GET_SIZE(sentinel) ? - Py_NewRef(PyTuple_GET_ITEM(sentinel, 0)) : NULL); - Py_SETREF(it->it_stop_exc, Py_NewRef(stop_exc)); + if (FT_ATOMIC_LOAD_PTR_RELAXED(it->it_callable) != NULL) { + PyObject *value = PyTuple_GET_SIZE(sentinel) ? + Py_NewRef(PyTuple_GET_ITEM(sentinel, 0)) : NULL; + _PyObject_XDecRefDelayed(calliter_swap(&it->it_sentinel, value)); + _PyObject_XDecRefDelayed(calliter_swap(&it->it_stop_exc, + Py_NewRef(stop_exc))); } Py_RETURN_NONE; diff --git a/Objects/listobject.c b/Objects/listobject.c index 4384a850780b7f..c5fe704df0e23d 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -273,10 +273,11 @@ PyList_New(Py_ssize_t size) return NULL; } } - if (size <= 0) { - op->ob_item = NULL; - } - else { + op->ob_item = NULL; + op->allocated = 0; + Py_SET_SIZE(op, size); + + if (size > 0) { #ifdef Py_GIL_DISABLED _PyListArray *array = list_allocate_array(size); if (array == NULL) { @@ -293,7 +294,7 @@ PyList_New(Py_ssize_t size) return PyErr_NoMemory(); } } - Py_SET_SIZE(op, size); + op->allocated = size; _PyObject_GC_TRACK(op); return (PyObject *) op; diff --git a/Objects/longobject.c b/Objects/longobject.c index c96ed6a215b5a1..b48328efa2f1b7 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -3325,8 +3325,7 @@ x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) int d; digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; twodigits vv; - sdigit zhi; - stwodigits z; + stwodigits z, zhi; /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd edn.), section 4.3.1, Algorithm D], except that we don't explicitly @@ -3416,16 +3415,15 @@ x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) for (i = 0; i < size_w; ++i) { /* invariants: -PyLong_BASE <= -q <= zhi <= 0; -PyLong_BASE * q <= z < PyLong_BASE */ - z = (sdigit)vk[i] + zhi - + z = (stwodigits)vk[i] + zhi - (stwodigits)q * (stwodigits)w0[i]; vk[i] = (digit)z & PyLong_MASK; - zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, - z, PyLong_SHIFT); + zhi = Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, z, PyLong_SHIFT); } /* add w back if q was too large (this branch taken rarely) */ - assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); - if ((sdigit)vtop + zhi < 0) { + assert((stwodigits)vtop + zhi == -1 || (stwodigits)vtop + zhi == 0); + if ((stwodigits)vtop + zhi < 0) { carry = 0; for (i = 0; i < size_w; ++i) { carry += vk[i] + w0[i]; @@ -3757,7 +3755,13 @@ x_add(PyLongObject *a, PyLongObject *b) size_a = size_b; size_b = size_temp; } } - z = long_alloc(size_a+1); + assert(size_a >= 1); + /* A carry out of the top digit is only possible if the top digits sum + to at least PyLong_MASK; only then allocate a digit for it. */ + digit top_sum = a->long_value.ob_digit[size_a - 1] + + (size_b == size_a ? b->long_value.ob_digit[size_b - 1] : 0); + int extra_digit = top_sum >= PyLong_MASK; + z = long_alloc(size_a + extra_digit); if (z == NULL) return NULL; for (i = 0; i < size_b; ++i) { @@ -3770,8 +3774,13 @@ x_add(PyLongObject *a, PyLongObject *b) z->long_value.ob_digit[i] = carry & PyLong_MASK; carry >>= PyLong_SHIFT; } - z->long_value.ob_digit[i] = carry; - return long_normalize(z); + if (extra_digit) { + z->long_value.ob_digit[i] = carry; + return long_normalize(z); + } + assert(carry == 0); + assert(z->long_value.ob_digit[i - 1] != 0); + return z; } /* Subtract the absolute values of two integers. */ @@ -4196,6 +4205,12 @@ k_mul(PyLongObject *a, PyLongObject *b) if (a == b) { t2 = (PyLongObject*)Py_NewRef(t1); } + else if (_PyLong_IsZero(bh)) { + /* b can be an unnormalized slice made by k_lopsided_mul(), so bh + * (and bl too) can be zero, and x_add() needs a nonzero operand. + */ + t2 = (PyLongObject*)Py_NewRef(bl); + } else if ((t2 = x_add(bh, bl)) == NULL) { Py_DECREF(t1); goto fail; @@ -4564,6 +4579,14 @@ l_mod(PyLongObject *v, PyLongObject *w, PyLongObject **pmod) *pmod = (PyLongObject *)fast_mod(v, w); return -(*pmod == NULL); } +#if WITH_PYLONG_MODULE + Py_ssize_t size_v = _PyLong_DigitCount(v); /* digits in numerator */ + Py_ssize_t size_w = _PyLong_DigitCount(w); /* digits in denominator */ + if (size_w > 300 && (size_v - size_w) > 150) { + /* Switch to _pylong.int_divmod(). See comments in l_divmod(). */ + return pylong_int_divmod(v, w, NULL, pmod); + } +#endif if (long_rem(v, w, &mod) < 0) return -1; if ((_PyLong_IsNegative(mod) && _PyLong_IsPositive(w)) || diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 11a0a7ab04f08b..d695d8cac2feda 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -1131,6 +1131,7 @@ PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order) /*[clinic input] +@vectorcall @classmethod memoryview.__new__ @@ -1141,7 +1142,7 @@ Create a new memoryview object which references the given object. static PyObject * memoryview_impl(PyTypeObject *type, PyObject *object) -/*[clinic end generated code: output=7de78e184ed66db8 input=f04429eb0bdf8c6e]*/ +/*[clinic end generated code: output=7de78e184ed66db8 input=8e10a66aebe8e657]*/ { return PyMemoryView_FromObject(object); } @@ -4136,4 +4137,5 @@ PyTypeObject PyMemoryView_Type = { 0, /* tp_init */ 0, /* tp_alloc */ memoryview, /* tp_new */ + .tp_vectorcall = memoryview_vectorcall, }; diff --git a/Objects/mimalloc/arena-abandon.c b/Objects/mimalloc/arena-abandon.c index 460c80fc22782f..fdc2a4a902a1da 100644 --- a/Objects/mimalloc/arena-abandon.c +++ b/Objects/mimalloc/arena-abandon.c @@ -240,17 +240,20 @@ static mi_segment_t* mi_arena_segment_clear_abandoned_next_field(mi_arena_field_ mi_arena_t* arena = mi_arena_from_index(arena_idx); if (arena != NULL) { bool has_lock = false; + // CPython: gh-161751: for a full visit, take the lock before reading the bitmap. + // A reclaim from another sub-process can clear our bit for a short time. + if (previous->visit_all && mi_option_is_enabled(mi_option_visit_abandoned)) { + mi_lock_acquire(&arena->abandoned_visit_lock); + has_lock = true; + } // visit the abandoned fields (starting at previous_idx) for (; field_idx < arena->field_count; field_idx++, bit_idx = 0) { size_t field = mi_atomic_load_relaxed(&arena->blocks_abandoned[field_idx]); if mi_unlikely(field != 0) { // skip zero fields quickly - // we only take the arena lock if there are actually abandoned segments present + // a reclaim tries to take the lock only when the field has abandoned segments if (!has_lock && mi_option_is_enabled(mi_option_visit_abandoned)) { - has_lock = (previous->visit_all ? (mi_lock_acquire(&arena->abandoned_visit_lock),true) : mi_lock_try_acquire(&arena->abandoned_visit_lock)); + has_lock = mi_lock_try_acquire(&arena->abandoned_visit_lock); if (!has_lock) { - if (previous->visit_all) { - _mi_error_message(EFAULT, "internal error: failed to visit all abandoned segments due to failure to acquire the visitor lock"); - } // skip to next arena break; } diff --git a/Objects/mimalloc/page.c b/Objects/mimalloc/page.c index 4bce8ce76044cb..2f26d04aaaa4d8 100644 --- a/Objects/mimalloc/page.c +++ b/Objects/mimalloc/page.c @@ -1071,6 +1071,12 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size, bool zero, size_t huge_al } mi_assert_internal(mi_heap_is_initialized(heap)); + // Process delayed frees from other threads (but skip contended ones). + // CPython: gh-161420 showed that calling _mi_heap_delayed_free_partial only + // once every 100 generic malloc calls led to high memory usage. + // To avoid this, we now process delayed frees on every generic malloc call again. + _mi_heap_delayed_free_partial(heap); + // do administrative tasks every N generic mallocs if mi_unlikely(++heap->generic_count >= 100) { heap->generic_collect_count += heap->generic_count; @@ -1078,9 +1084,6 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size, bool zero, size_t huge_al // call potential deferred free routines _mi_deferred_free(heap, false); - // free delayed frees from other threads (but skip contended ones) - _mi_heap_delayed_free_partial(heap); - // collect every once in a while (10000 by default) const long generic_collect = mi_option_get_clamp(mi_option_generic_collect, 1, 1000000L); if (heap->generic_collect_count >= generic_collect) { diff --git a/Objects/mimalloc/prim/unix/prim.c b/Objects/mimalloc/prim/unix/prim.c index c66198464e72a2..477e2d8b2274c4 100644 --- a/Objects/mimalloc/prim/unix/prim.c +++ b/Objects/mimalloc/prim/unix/prim.c @@ -78,10 +78,11 @@ terms of the MIT license. A copy of the license can be found in the file // Use syscalls for some primitives to allow for libraries that override open/read/close etc. // and do allocation themselves; using syscalls prevents recursion when mimalloc is // still initializing (issue #713) +// Not used on Android, see issue python/cpython/pull/158530 // Declare inline to avoid unused function warnings. //------------------------------------------------------------------------------------ -#if defined(MI_HAS_SYSCALL_H) && defined(SYS_open) && defined(SYS_close) && defined(SYS_read) && defined(SYS_access) +#if defined(MI_HAS_SYSCALL_H) && defined(SYS_open) && defined(SYS_close) && defined(SYS_read) && defined(SYS_access) && !defined(__ANDROID__) static inline int mi_prim_open(const char* fpath, int open_flags) { return syscall(SYS_open,fpath,open_flags,0); diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 7c842f3ed8b196..568711149a8aeb 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -409,7 +409,7 @@ module_from_slots_and_spec( createfunc_t create = NULL; PyObject *nameobj; PyObject *m = NULL; - uint64_t multiple_interpreters = (uint64_t)Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED; + uint64_t multiple_interpreters = (uint64_t)(uintptr_t)Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED; bool requires_gil = true; const char *name; int ret; @@ -471,7 +471,7 @@ module_from_slots_and_spec( case Py_mod_gil: { uint64_t val = it.current.sl_uint64; - requires_gil = (val != (uint64_t)Py_MOD_GIL_NOT_USED); + requires_gil = (val != (uint64_t)(uintptr_t)Py_MOD_GIL_NOT_USED); } break; case Py_mod_abi: diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c index e1ed2f6df9a6e2..6d5fced0ed73b3 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -261,7 +261,9 @@ namespace___replace___impl(_PyNamespaceObject *self, PyObject *changes) Py_DECREF(result); return NULL; } - if (PyDict_Update(((_PyNamespaceObject*)result)->ns_dict, changes) < 0) { + if (changes != NULL + && PyDict_Update(((_PyNamespaceObject*)result)->ns_dict, changes) < 0) + { Py_DECREF(result); return NULL; } diff --git a/Objects/object.c b/Objects/object.c index f8206e5d3eca32..1589e3523e6205 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -468,7 +468,12 @@ _Py_MergeZeroLocalRefcount(PyObject *op) Py_ssize_t _Py_ExplicitMergeRefcount(PyObject *op, Py_ssize_t extra) { - assert(!_Py_IsImmortal(op)); + if (_Py_IsImmortal(op)) { + // The object may have become immortal after it was queued to be + // merged (for example, by sys.intern()). There is nothing to merge + // and the reference total no longer includes the object. + return _Py_IMMORTAL_INITIAL_REFCNT; + } #ifdef Py_REF_DEBUG _Py_AddRefTotal(_PyThreadState_GET(), extra); @@ -583,13 +588,13 @@ PyObject_CallFinalizer(PyObject *self) if (tp->tp_finalize == NULL) return; /* tp_finalize should only be called once. */ - if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self)) - return; - - tp->tp_finalize(self); if (_PyType_IS_GC(tp)) { + if (_PyGC_FINALIZED(self)) + return; + _PyGC_SET_FINALIZED(self); } + tp->tp_finalize(self); } int @@ -956,6 +961,7 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization) clear_freelist(&freelists->pycfunctionobject, is_finalization, PyObject_GC_Del); clear_freelist(&freelists->pycmethodobject, is_finalization, PyObject_GC_Del); clear_freelist(&freelists->pymethodobjects, is_finalization, free_object); + clear_freelist(&freelists->tracebacks, is_finalization, PyObject_GC_Del); } /* diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index 3ad485620ede41..66931dc396fa95 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -1,6 +1,7 @@ /* Python's malloc wrappers (see pymem.h) */ #include "Python.h" +#include "pycore_ceval.h" #include "pycore_interp.h" // _PyInterpreterState_HasFeature #include "pycore_mmap.h" // _PyAnnotateMemoryMap() #include "pycore_object.h" // _PyDebugAllocatorStats() definition @@ -1424,7 +1425,7 @@ should_advance_qsbr_for_free(struct _qsbr_thread_state *qsbr, size_t size) if (size > QSBR_FREE_MEM_LIMIT) { qsbr->deferred_count = 0; qsbr->deferred_memory = 0; - qsbr->should_process = true; + _Py_set_eval_breaker_bit(qsbr->tstate, _PY_EVAL_PROCESS_DELAYED_BIT); return true; } qsbr->deferred_count++; @@ -1433,7 +1434,7 @@ should_advance_qsbr_for_free(struct _qsbr_thread_state *qsbr, size_t size) qsbr->deferred_memory > QSBR_FREE_MEM_LIMIT) { qsbr->deferred_count = 0; qsbr->deferred_memory = 0; - qsbr->should_process = true; + _Py_set_eval_breaker_bit(qsbr->tstate, _PY_EVAL_PROCESS_DELAYED_BIT); return true; } return false; @@ -1511,13 +1512,6 @@ free_delayed(uintptr_t ptr, size_t size) buf->array[buf->wr_idx].ptr = ptr; buf->array[buf->wr_idx].qsbr_goal = seq; buf->wr_idx++; - - if (buf->wr_idx == WORK_ITEMS_PER_CHUNK) { - // Normally the processing of delayed items is done from the eval - // breaker. Processing here is a safety measure to ensure too much - // work does not accumulate. - _PyMem_ProcessDelayed((PyThreadState *)tstate); - } #endif } @@ -1625,7 +1619,7 @@ _PyMem_ProcessDelayed(PyThreadState *tstate) PyInterpreterState *interp = tstate->interp; _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate; - tstate_impl->qsbr->should_process = false; + _Py_unset_eval_breaker_bit(tstate, _PY_EVAL_PROCESS_DELAYED_BIT); // Process thread-local work process_queue(&tstate_impl->mem_free_queue, tstate_impl, true, NULL, NULL); diff --git a/Objects/picklebufobject.c b/Objects/picklebufobject.c index 50f17687bc4365..18cfe9f9f64594 100644 --- a/Objects/picklebufobject.c +++ b/Objects/picklebufobject.c @@ -4,6 +4,18 @@ #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include +#include "clinic/picklebufobject.c.h" + +/*[clinic input] +module pickle +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6165c351cde067ba]*/ + +/*[clinic input] +class pickle.PickleBuffer "PyPickleBufferObject *" "&PyPickleBuffer_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b334ecd891c9cbc2]*/ + typedef struct { PyObject_HEAD /* The view exported by the original object */ @@ -66,19 +78,22 @@ PyPickleBuffer_Release(PyObject *obj) return 0; } -static PyObject * -picklebuf_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyPickleBufferObject *self; - PyObject *base; - char *keywords[] = {"", NULL}; +/*[clinic input] +@vectorcall +@classmethod +pickle.PickleBuffer.__new__ as picklebuf_new - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PickleBuffer", - keywords, &base)) { - return NULL; - } + buffer as base: object + / - self = (PyPickleBufferObject *) type->tp_alloc(type, 0); +Wrapper for potentially out-of-band buffers. +[clinic start generated code]*/ + +static PyObject * +picklebuf_new_impl(PyTypeObject *type, PyObject *base) +/*[clinic end generated code: output=b8269110aace6998 input=616fff5fc36c22d4]*/ +{ + PyPickleBufferObject *self = (PyPickleBufferObject *) type->tp_alloc(type, 0); if (self == NULL) { return NULL; } @@ -211,10 +226,11 @@ static PyMethodDef picklebuf_methods[] = { PyTypeObject PyPickleBuffer_Type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pickle.PickleBuffer", - .tp_doc = PyDoc_STR("Wrapper for potentially out-of-band buffers"), + .tp_doc = picklebuf_new__doc__, .tp_basicsize = sizeof(PyPickleBufferObject), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, .tp_new = picklebuf_new, + .tp_vectorcall = picklebuf_vectorcall, .tp_dealloc = picklebuf_dealloc, .tp_traverse = picklebuf_traverse, .tp_clear = picklebuf_clear, diff --git a/Objects/setobject.c b/Objects/setobject.c index 8c5f244d4894d8..6ba6e611d9448d 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1027,7 +1027,10 @@ frozenset_hash(PyObject *self) typedef struct { PyObject_HEAD - PySetObject *si_set; /* Set to NULL when iterator is exhausted */ + /* Set to NULL when the iterator is exhausted, except in the free-threaded + build, where the reference is kept until the iterator is deallocated + (see setiter_set_exhausted()). */ + PySetObject *si_set; Py_ssize_t si_used; Py_ssize_t si_pos; Py_ssize_t len; @@ -1056,8 +1059,14 @@ setiter_len(PyObject *op, PyObject *Py_UNUSED(ignored)) { setiterobject *si = (setiterobject*)op; Py_ssize_t len = 0; - if (si->si_set != NULL && si->si_used == si->si_set->used) - len = si->len; + PySetObject *so = si->si_set; + if (so != NULL) { + Py_ssize_t so_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used); + Py_ssize_t si_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(si->si_used); + if (si_used == so_used) { + len = FT_ATOMIC_LOAD_SSIZE_RELAXED(si->len); + } + } return PyLong_FromSsize_t(len); } @@ -1089,6 +1098,25 @@ static PyMethodDef setiter_methods[] = { {NULL, NULL} /* sentinel */ }; +/* Mark the iterator as exhausted. In the free-threaded build an iterator + may be advanced by several threads at once, each holding a borrowed + pointer to si_set, so the iterator keeps its reference to the set until + it is deallocated (as list and tuple iterators do) and records exhaustion + by setting si_pos to SETITER_EXHAUSTED. */ +#define SETITER_EXHAUSTED (-1) + +static void +setiter_set_exhausted(setiterobject *si, PySetObject *so) +{ +#ifdef Py_GIL_DISABLED + FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, SETITER_EXHAUSTED); + FT_ATOMIC_STORE_SSIZE_RELAXED(si->len, 0); +#else + si->si_set = NULL; + Py_DECREF(so); +#endif +} + static PyObject *setiter_iternext(PyObject *self) { setiterobject *si = (setiterobject*)self; @@ -1101,34 +1129,43 @@ static PyObject *setiter_iternext(PyObject *self) return NULL; assert (PyAnySet_Check(so)); +#ifdef Py_GIL_DISABLED + if (FT_ATOMIC_LOAD_SSIZE_RELAXED(si->si_pos) == SETITER_EXHAUSTED) { + return NULL; + } +#endif + Py_ssize_t so_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used); Py_ssize_t si_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(si->si_used); if (si_used != so_used) { PyErr_SetString(PyExc_RuntimeError, "Set changed size during iteration"); - si->si_used = -1; /* Make this state sticky */ + /* Make this state sticky */ + FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_used, -1); return NULL; } + /* si_pos and len are only written with the set locked, so that an + iterator marked as exhausted by another thread stays exhausted. */ Py_BEGIN_CRITICAL_SECTION(so); i = si->si_pos; - assert(i>=0); - entry = so->table; - mask = so->mask; - while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) { - i++; - } - if (i <= mask) { - key = Py_NewRef(entry[i].key); + if (i != SETITER_EXHAUSTED) { + assert(i >= 0); + entry = so->table; + mask = so->mask; + while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) { + i++; + } + if (i <= mask) { + key = Py_NewRef(entry[i].key); + FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, i + 1); + FT_ATOMIC_STORE_SSIZE_RELAXED(si->len, si->len - 1); + } + else { + setiter_set_exhausted(si, so); + } } Py_END_CRITICAL_SECTION(); - si->si_pos = i+1; - if (key == NULL) { - si->si_set = NULL; - Py_DECREF(so); - return NULL; - } - si->len--; return key; } @@ -1572,6 +1609,7 @@ _PySet_Freeze(PyObject *set) assert(PySet_CheckExact(set)); assert(_PyObject_IsUniquelyReferenced(set)); set->ob_type = &PyFrozenSet_Type; + _PyFrozenSet_MaybeUntrack(set); return Py_NewRef(set); } diff --git a/Objects/stringlib/find_max_char.h b/Objects/stringlib/find_max_char.h index 7ab3fc88b331b1..8c7da7b7fad38e 100644 --- a/Objects/stringlib/find_max_char.h +++ b/Objects/stringlib/find_max_char.h @@ -5,123 +5,157 @@ # error "find_max_char.h is specific to Unicode" #endif -/* Mask to quickly check whether a C 'size_t' contains a - non-ASCII, UTF8-encoded char. */ -#if (SIZEOF_SIZE_T == 8) -# define UCS1_ASCII_CHAR_MASK 0x8080808080808080ULL -#elif (SIZEOF_SIZE_T == 4) -# define UCS1_ASCII_CHAR_MASK 0x80808080U -#else -# error C 'size_t' size should be either 4 or 8! -#endif +#define MAX_CHAR_ASCII 0x7f +#define MAX_CHAR_UCS1 0xff +#define MAX_CHAR_UCS2 0xffff +#define MAX_CHAR_UCS4 0x10ffff +/* Masks selecting the bits that are set in a C 'size_t' holding several + characters when at least one of them is wider than ASCII, UCS1 or UCS2. */ #if STRINGLIB_SIZEOF_CHAR == 1 +# if SIZEOF_SIZE_T == 8 +# define MASK_ASCII 0x8080808080808080ULL +# elif SIZEOF_SIZE_T == 4 +# define MASK_ASCII 0x80808080U +# else +# error C 'size_t' size should be either 4 or 8! +# endif +# define MASK_MAX_CHAR MASK_ASCII +# define MAX_CHAR MAX_CHAR_UCS1 +#elif STRINGLIB_SIZEOF_CHAR == 2 +# if SIZEOF_SIZE_T == 8 +# define MASK_ASCII 0xFF80FF80FF80FF80ULL +# define MASK_UCS1 0xFF00FF00FF00FF00ULL +# elif SIZEOF_SIZE_T == 4 +# define MASK_ASCII 0xFF80FF80U +# define MASK_UCS1 0xFF00FF00U +# else +# error C 'size_t' size should be either 4 or 8! +# endif +# define MASK_MAX_CHAR MASK_UCS1 +# define MAX_CHAR MAX_CHAR_UCS2 +#elif STRINGLIB_SIZEOF_CHAR == 4 +# if SIZEOF_SIZE_T == 8 +# define MASK_ASCII 0xFFFFFF80FFFFFF80ULL +# define MASK_UCS1 0xFFFFFF00FFFFFF00ULL +# define MASK_UCS2 0xFFFF0000FFFF0000ULL +# elif SIZEOF_SIZE_T == 4 +# define MASK_ASCII 0xFFFFFF80U +# define MASK_UCS1 0xFFFFFF00U +# define MASK_UCS2 0xFFFF0000U +# else +# error C 'size_t' size should be either 4 or 8! +# endif +# define MASK_MAX_CHAR MASK_UCS2 +# define MAX_CHAR MAX_CHAR_UCS4 +#else +# error Invalid STRINGLIB_SIZEOF_CHAR (must be 1, 2 or 4) +#endif +/* OR all characters into a single size_t accumulator and test the high bits + at the end, instead of comparing each character. The accumulation loop has + no branches, so compilers vectorize it. The start is first read in pieces + until the pointer is aligned, so the loops can use plain word loads on + every platform. Every 16 words, check whether the widest kind is already + known and exit early. */ Py_LOCAL_INLINE(Py_UCS4) STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end) { - const unsigned char *p = (const unsigned char *) begin; + const unsigned char *p = (const unsigned char *)begin; const unsigned char *_end = (const unsigned char *)end; + size_t value = 0; - while (p < _end) { - if (_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) { - /* Help register allocation */ - const unsigned char *_p = p; - while (_p + SIZEOF_SIZE_T <= _end) { - size_t value = *(const size_t *) _p; - if (value & UCS1_ASCII_CHAR_MASK) - return 255; - _p += SIZEOF_SIZE_T; + if (p + SIZEOF_SIZE_T <= _end) { + if (!_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) { +#if STRINGLIB_SIZEOF_CHAR == 1 + if (!_Py_IS_ALIGNED(p, sizeof(uint16_t))) { + value |= *p++; } - p = _p; - if (p == _end) - break; - } - if (*p++ & 0x80) - return 255; - } - return 127; -} - -#undef ASCII_CHAR_MASK - -#else /* STRINGLIB_SIZEOF_CHAR == 1 */ - -#define MASK_ASCII 0xFFFFFF80 -#define MASK_UCS1 0xFFFFFF00 -#define MASK_UCS2 0xFFFF0000 - -#define MAX_CHAR_ASCII 0x7f -#define MAX_CHAR_UCS1 0xff -#define MAX_CHAR_UCS2 0xffff -#define MAX_CHAR_UCS4 0x10ffff - -Py_LOCAL_INLINE(Py_UCS4) -STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end) -{ -#if STRINGLIB_SIZEOF_CHAR == 2 - const Py_UCS4 mask_limit = MASK_UCS1; - const Py_UCS4 max_char_limit = MAX_CHAR_UCS2; -#elif STRINGLIB_SIZEOF_CHAR == 4 - const Py_UCS4 mask_limit = MASK_UCS2; - const Py_UCS4 max_char_limit = MAX_CHAR_UCS4; -#else -#error Invalid STRINGLIB_SIZEOF_CHAR (must be 1, 2 or 4) #endif - Py_UCS4 mask; - Py_ssize_t n = end - begin; - const STRINGLIB_CHAR *p = begin; - const STRINGLIB_CHAR *unrolled_end = begin + _Py_SIZE_ROUND_DOWN(n, 4); - Py_UCS4 max_char; - - max_char = MAX_CHAR_ASCII; - mask = MASK_ASCII; - while (p < unrolled_end) { - STRINGLIB_CHAR bits = p[0] | p[1] | p[2] | p[3]; - if (bits & mask) { - if (mask == mask_limit) { - /* Limit reached */ - return max_char_limit; +#if STRINGLIB_SIZEOF_CHAR <= 2 + if (!_Py_IS_ALIGNED(p, sizeof(uint32_t))) { + value |= *(const uint16_t *)p; + p += sizeof(uint16_t); } - if (mask == MASK_ASCII) { - max_char = MAX_CHAR_UCS1; - mask = MASK_UCS1; +#endif +#if SIZEOF_SIZE_T == 8 + if (!_Py_IS_ALIGNED(p, SIZEOF_SIZE_T)) { + value |= *(const uint32_t *)p; + p += sizeof(uint32_t); } - else { - /* mask can't be MASK_UCS2 because of mask_limit above */ - assert(mask == MASK_UCS1); - max_char = MAX_CHAR_UCS2; - mask = MASK_UCS2; +#endif + if (value & MASK_MAX_CHAR) { + return MAX_CHAR; } - /* We check the new mask on the same chars in the next iteration */ - continue; + assert(_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)); } - p += 4; - } - while (p < end) { - if (p[0] & mask) { - if (mask == mask_limit) { - /* Limit reached */ - return max_char_limit; - } - if (mask == MASK_ASCII) { - max_char = MAX_CHAR_UCS1; - mask = MASK_UCS1; + + for (; p + SIZEOF_SIZE_T * 16 <= _end; p += SIZEOF_SIZE_T * 16) { + /* Four independent accumulators so the loop does not serialize on + one register when the compiler does not vectorize it. */ + size_t v0 = 0, v1 = 0, v2 = 0, v3 = 0; + for (int i = 0; i < 16 * SIZEOF_SIZE_T; i += 4 * SIZEOF_SIZE_T) { + const size_t *w = (const size_t *)(p + i); + v0 |= w[0]; + v1 |= w[1]; + v2 |= w[2]; + v3 |= w[3]; } - else { - /* mask can't be MASK_UCS2 because of mask_limit above */ - assert(mask == MASK_UCS1); - max_char = MAX_CHAR_UCS2; - mask = MASK_UCS2; + size_t block = v0 | v1 | v2 | v3; + if (block & MASK_MAX_CHAR) { + return MAX_CHAR; } - /* We check the new mask on the same chars in the next iteration */ - continue; + value |= block; + } + + for (; p + SIZEOF_SIZE_T <= _end; p += SIZEOF_SIZE_T) { + value |= *(const size_t *)p; } - p++; } - return max_char; + + /* Fewer than SIZEOF_SIZE_T bytes are left. Read them as two overlapping + pieces, one from each end. OR does not mind seeing a byte twice. */ + if (p < _end) { + size_t n = (size_t)(_end - p); + if (n >= 4) { + uint32_t a, b; + memcpy(&a, p, 4); + memcpy(&b, _end - 4, 4); + value |= a | b; + } +#if STRINGLIB_SIZEOF_CHAR <= 2 + else if (n >= 2) { + uint16_t a, b; + memcpy(&a, p, 2); + memcpy(&b, _end - 2, 2); + value |= a | b; + } +#endif +#if STRINGLIB_SIZEOF_CHAR == 1 + else { + value |= *p; + } +#endif + } + +#ifdef MASK_UCS2 + if (value & MASK_UCS2) { + return MAX_CHAR_UCS4; + } +#endif +#ifdef MASK_UCS1 + if (value & MASK_UCS1) { + return MAX_CHAR_UCS2; + } +#endif + if (value & MASK_ASCII) { + return MAX_CHAR_UCS1; + } + return MAX_CHAR_ASCII; } +#undef MASK_MAX_CHAR +#undef MAX_CHAR #undef MASK_ASCII #undef MASK_UCS1 #undef MASK_UCS2 @@ -129,6 +163,3 @@ STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end) #undef MAX_CHAR_UCS1 #undef MAX_CHAR_UCS2 #undef MAX_CHAR_UCS4 - -#endif /* STRINGLIB_SIZEOF_CHAR == 1 */ - diff --git a/Objects/structseq.c b/Objects/structseq.c index 36daf523d8004f..949790e6f416e9 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -424,9 +424,10 @@ structseq___replace___impl(PyStructSequence *self, PyObject *changes) // We do not support types with unnamed fields, so we can iterate over // i >= n_visible_fields case without slicing with (i - n_unnamed_fields). for (i = 0; i < n_fields; ++i) { - PyObject *ob; - if (PyDict_PopString(changes, Py_TYPE(self)->tp_members[i].name, - &ob) < 0) { + PyObject *ob = NULL; + if (changes != NULL + && PyDict_PopString(changes, Py_TYPE(self)->tp_members[i].name, + &ob) < 0) { goto error; } if (ob == NULL) { @@ -435,7 +436,7 @@ structseq___replace___impl(PyStructSequence *self, PyObject *changes) result->ob_item[i] = ob; } // Check if there are any unexpected fields. - if (PyDict_GET_SIZE(changes) > 0) { + if (changes != NULL && PyDict_GET_SIZE(changes) > 0) { PyObject *names = PyDict_Keys(changes); if (names) { PyErr_Format(PyExc_TypeError, "Got unexpected field name(s): %R", names); diff --git a/Objects/templateobject.c b/Objects/templateobject.c index 994b352d7dcd63..de7448ae882e14 100644 --- a/Objects/templateobject.c +++ b/Objects/templateobject.c @@ -5,6 +5,13 @@ #include "pycore_runtime.h" // _Py_STR() #include "pycore_template.h" +#include "clinic/templateobject.c.h" + +/*[clinic input] +class Template "templateobject *" "&_PyTemplate_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ee41f8d8c49f0271]*/ + typedef struct { PyObject_HEAD PyObject *stringsiter; @@ -89,21 +96,24 @@ typedef struct { #define templateobject_CAST(op) \ (assert(_PyTemplate_CheckExact(op)), _Py_CAST(templateobject*, (op))) +/*[clinic input] +@vectorcall +@classmethod +Template.__new__ as template_new + *args: array +[clinic start generated code]*/ + static PyObject * -template_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +template_new_impl(PyTypeObject *type, PyObject * const *args, + Py_ssize_t args_length) +/*[clinic end generated code: output=5831a1056a1a68d5 input=65f04e91969ffccd]*/ { - if (kwds != NULL) { - PyErr_SetString(PyExc_TypeError, "Template.__new__ only accepts *args arguments"); - return NULL; - } - - Py_ssize_t argslen = PyTuple_GET_SIZE(args); Py_ssize_t stringslen = 0; Py_ssize_t interpolationslen = 0; int last_was_str = 0; - for (Py_ssize_t i = 0; i < argslen; i++) { - PyObject *item = PyTuple_GET_ITEM(args, i); + for (Py_ssize_t i = 0; i < args_length; i++) { + PyObject *item = args[i]; if (PyUnicode_Check(item)) { if (!last_was_str) { stringslen++; @@ -142,8 +152,8 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds) last_was_str = 0; Py_ssize_t stringsidx = 0, interpolationsidx = 0; - for (Py_ssize_t i = 0; i < argslen; i++) { - PyObject *item = PyTuple_GET_ITEM(args, i); + for (Py_ssize_t i = 0; i < args_length; i++) { + PyObject *item = args[i]; if (PyUnicode_Check(item)) { if (last_was_str) { PyObject *laststring = PyTuple_GET_ITEM(strings, stringsidx - 1); @@ -391,6 +401,7 @@ PyTypeObject _PyTemplate_Type = { .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, .tp_as_sequence = &template_as_sequence, .tp_new = template_new, + .tp_vectorcall = template_vectorcall, .tp_alloc = PyType_GenericAlloc, .tp_dealloc = template_dealloc, .tp_clear = template_clear, diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 68ca2772507295..4b096d209fd92e 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -8,6 +8,7 @@ #include "pycore_list.h" // _Py_memory_repeat() #include "pycore_modsupport.h" // _PyArg_NoKwnames() #include "pycore_object.h" // _PyObject_GC_TRACK() +#include "pycore_pyerrors.h" // _PyException_AddNote() #include "pycore_stackref.h" // PyStackRef_AsPyObjectSteal() #include "pycore_tuple.h" // _PyTupleIterObject @@ -49,11 +50,6 @@ tuple_alloc(Py_ssize_t size) return op; } } - /* Check for overflow */ - if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) - - sizeof(PyObject *))) / sizeof(PyObject *)) { - return (PyTupleObject *)PyErr_NoMemory(); - } PyTupleObject *result = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); if (result != NULL) { _PyTuple_RESET_HASH_CACHE(result); @@ -392,6 +388,23 @@ tuple_hash(PyObject *op) for (Py_ssize_t i = 0; i < len; i++) { Py_uhash_t lane = PyObject_Hash(item[i]); if (lane == (Py_uhash_t)-1) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) { + /* Preserve the original error while providing context for + the element that could not be hashed. */ + PyObject *exc = PyErr_GetRaisedException(); + PyObject *note = PyUnicode_FromFormat( + "when hashing tuple item %zd", i); + if (note != NULL) { + if (_PyException_AddNote(exc, note) < 0) { + PyErr_Clear(); + } + Py_DECREF(note); + } + else { + PyErr_Clear(); + } + PyErr_SetRaisedException(exc); + } _Py_LeaveRecursiveCall(); return -1; } @@ -1061,14 +1074,6 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) *pv = PyTuple_New(newsize); return *pv == NULL ? -1 : 0; } - /* Check for overflow */ - if ((size_t)newsize > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) - - sizeof(PyObject *))) / sizeof(PyObject *)) { - *pv = NULL; - Py_DECREF(v); - PyErr_NoMemory(); - return -1; - } if (_PyObject_GC_IS_TRACKED(v)) { _PyObject_GC_UNTRACK(v); @@ -1297,12 +1302,13 @@ maybe_freelist_push(PyTupleObject *op) void _PyTuple_DebugMallocStats(FILE *out) { + PyTypeObject *type = &PyTuple_Type; for (int i = 0; i < PyTuple_MAXSAVESIZE; i++) { int len = i + 1; char buf[128]; PyOS_snprintf(buf, sizeof(buf), "free %d-sized PyTupleObject", len); - _PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]), - _PyType_PreHeaderSize(&PyTuple_Type) + _PyObject_VAR_SIZE(&PyTuple_Type, len)); + size_t size = _PyType_PreHeaderSize(type) + _PyObject_VAR_SIZE(type, len); + _PyDebugAllocatorStats(out, buf, _Py_FREELIST_SIZE(tuples[i]), size); } } diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 66c7bc7f8902fc..68908c3f0bae62 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2537,25 +2537,43 @@ _PyType_NewManagedObject(PyTypeObject *type) PyObject * _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems) { - PyObject *obj; +#ifdef Py_DEBUG + if (nitems < 0) { + PyErr_BadInternalCall(); + return NULL; + } +#endif + /* The +1 on nitems is needed for most types but not all. We could save a * bit of space by allocating one less item in certain cases, depending on * the type. However, given the extra complexity (e.g. an additional type * flag to indicate when that is safe) it does not seem worth the memory * savings. An example type that doesn't need the +1 is a subclass of * tuple. See GH-100659 and GH-81381. */ + if (nitems > PY_SSIZE_T_MAX - 1) { + return PyErr_NoMemory(); + } size_t size = _PyObject_VAR_SIZE(type, nitems+1); - const size_t presize = _PyType_PreHeaderSize(type); if (type->tp_flags & Py_TPFLAGS_INLINE_VALUES) { assert(type->tp_itemsize == 0); - size += _PyInlineValuesSize(type); + size_t inline_values_size = _PyInlineValuesSize(type); + if (size > (size_t)PY_SSIZE_T_MAX - inline_values_size) { + return PyErr_NoMemory(); + } + size += inline_values_size; } + + const size_t presize = _PyType_PreHeaderSize(type); + if (size > (size_t)PY_SSIZE_T_MAX - presize) { + return PyErr_NoMemory(); + } + char *alloc = _PyObject_MallocWithType(type, size + presize); - if (alloc == NULL) { + if (alloc == NULL) { return PyErr_NoMemory(); } - obj = (PyObject *)(alloc + presize); + PyObject *obj = (PyObject *)(alloc + presize); if (presize) { ((PyObject **)alloc)[0] = NULL; ((PyObject **)alloc)[1] = NULL; @@ -2827,6 +2845,10 @@ subtype_dealloc(PyObject *self) } _PyObject_GC_UNTRACK(self); } + + /* Extract the type again; tp_finalize may have changed it */ + type = Py_TYPE(self); + /* If we added a weaklist, we clear it. Do this *before* calling tp_del, clearing slots, or clearing the instance dict. @@ -6667,11 +6689,11 @@ type_update_dict(PyTypeObject *type, PyDictObject *dict, PyObject *name, return 0; } -static void +static int update_slot_after_setattr(PyTypeObject *type, PyObject *name) { types_stop_world(); - update_slot(type, name); + int res = update_slot(type, name); types_start_world(); #ifdef Py_GIL_DISABLED // Invalidate again, after the slots are updated and while TYPE_LOCK is @@ -6682,6 +6704,7 @@ update_slot_after_setattr(PyTypeObject *type, PyObject *name) ASSERT_TYPE_LOCK_HELD(); _PyType_Modified_Unlocked(type); #endif + return res; } static int @@ -6770,7 +6793,7 @@ type_setattro(PyObject *self, PyObject *name, PyObject *value) if (res == 0) { if (is_dunder_name(name) && has_slotdef(name)) { // The name corresponds to a type slot. - update_slot_after_setattr(type, name); + res = update_slot_after_setattr(type, name); } } END_TYPE_DICT_LOCK(); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index da1ba44dba8129..309621acdaa3c8 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -5063,6 +5063,16 @@ find_first_nonascii(const unsigned char *start, const unsigned char *end) } #endif + // Long ASCII runs: test eight words per branch. + for (; end - p >= 8 * SIZEOF_SIZE_T; p += 8 * SIZEOF_SIZE_T) { + const size_t *w = (const size_t *)p; + if ((w[0] | w[1] | w[2] | w[3] | + w[4] | w[5] | w[6] | w[7]) & ASCII_CHAR_MASK) + { + break; + } + } + const unsigned char *e = end - SIZEOF_SIZE_T; while (p <= e) { size_t u = (*(const size_t *)p) & ASCII_CHAR_MASK; @@ -10444,10 +10454,10 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq PyObject *item; Py_ssize_t sz, i, res_offset; Py_UCS4 maxchar; - Py_UCS4 item_maxchar; + /* kinds is the bitwise OR of the PyUnicode_KIND() values (1, 2, 4). */ + unsigned int kinds = 0, all_ascii = 1; int use_memcpy; unsigned char *res_data = NULL, *sep_data = NULL; - PyObject *last_obj; int kind = 0; /* If empty sequence, return u"". */ @@ -10456,14 +10466,12 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq } /* If singleton sequence with an exact Unicode, return that. */ - last_obj = NULL; if (seqlen == 1) { if (PyUnicode_CheckExact(items[0])) { res = items[0]; return Py_NewRef(res); } seplen = 0; - maxchar = 0; } else { /* Set up sep and seplen */ @@ -10473,7 +10481,6 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq if (!sep) goto onError; seplen = 1; - maxchar = 32; } else { if (!PyUnicode_Check(separator)) { @@ -10485,12 +10492,12 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq } sep = separator; seplen = PyUnicode_GET_LENGTH(separator); - maxchar = PyUnicode_MAX_CHAR_VALUE(separator); /* inc refcount to keep this code path symmetric with the above case of a blank separator */ Py_INCREF(sep); } - last_obj = sep; + kinds = PyUnicode_KIND(sep); + all_ascii = PyUnicode_IS_ASCII(sep); } /* There are at least two things to join, or else we have a subclass @@ -10514,23 +10521,34 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq i, Py_TYPE(item)->tp_name); goto onError; } - add_sz = PyUnicode_GET_LENGTH(item); - item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); - maxchar = Py_MAX(maxchar, item_maxchar); - if (i != 0) { - add_sz += seplen; - } + /* Count one separator per item; the extra one is removed below. */ + add_sz = (size_t)PyUnicode_GET_LENGTH(item) + (size_t)seplen; + kinds |= PyUnicode_KIND(item); + all_ascii &= PyUnicode_IS_ASCII(item); if (add_sz > (size_t)(PY_SSIZE_T_MAX - sz)) { PyErr_SetString(PyExc_OverflowError, "join() result is too long for a Python string"); goto onError; } sz += add_sz; - if (use_memcpy && last_obj != NULL) { - if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) - use_memcpy = 0; - } - last_obj = item; + } + sz -= seplen; + /* memcpy() can be used if the separator and all items have the same + kind: the result then has that kind too. */ + if (kinds & (kinds - 1)) { + use_memcpy = 0; + } + if (all_ascii) { + maxchar = 0x7f; + } + else if (kinds & PyUnicode_4BYTE_KIND) { + maxchar = MAX_UNICODE; + } + else if (kinds & PyUnicode_2BYTE_KIND) { + maxchar = 0xffff; + } + else { + maxchar = 0xff; } res = PyUnicode_New(sz, maxchar); @@ -14727,7 +14745,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */, /* Statically allocated strings must be already interned. */ assert(!_PyUnicode_STATE(s).statically_allocated); -#if Py_GIL_DISABLED +#ifdef Py_GIL_DISABLED /* In the free-threaded build, all interned strings are immortal */ immortalize = 1; #endif diff --git a/PC/pyconfig.h b/PC/pyconfig.h index f969525c1db5c7..9de3786a0c769b 100644 --- a/PC/pyconfig.h +++ b/PC/pyconfig.h @@ -98,17 +98,15 @@ WIN32 is still required for the locale module. #endif /* Define to 1 when compiling for experimental free-threaded builds */ -#ifdef Py_GIL_DISABLED +#if defined(Py_GIL_DISABLED) && Py_GIL_DISABLED == 0 /* We undefine if it was set to zero because all later checks are #ifdef. * Note that non-Windows builds do not do this, and so every effort should * be made to avoid defining the variable at all when not desired. However, * sysconfig.get_config_var always returns a 1 or a 0, and so it seems likely * that a build backend will define it with the value. */ -#if Py_GIL_DISABLED == 0 #undef Py_GIL_DISABLED #endif -#endif /* Compiler specific defines */ diff --git a/PCbuild/get_externals.bat b/PCbuild/get_externals.bat index b5f070ca2d858d..36dfb7cc051c65 100644 --- a/PCbuild/get_externals.bat +++ b/PCbuild/get_externals.bat @@ -60,8 +60,8 @@ set libraries=%libraries% sqlite-3.53.4.0 if NOT "%IncludeTkinterSrc%"=="false" set libraries=%libraries% tcl-9.0.4.0 if NOT "%IncludeTkinterSrc%"=="false" set libraries=%libraries% tk-9.0.4.1 set libraries=%libraries% xz-5.8.1.1 -set libraries=%libraries% zlib-ng-2.2.4 -set libraries=%libraries% zstd-1.5.7 +set libraries=%libraries% zlib-ng-2.2.5 +set libraries=%libraries% zstd-1.6.0 for %%e in (%libraries%) do ( if exist "%EXTERNALS_DIR%\%%e" ( diff --git a/PCbuild/pcbuild.proj b/PCbuild/pcbuild.proj index 53aec0276beaee..95e135705dac2f 100644 --- a/PCbuild/pcbuild.proj +++ b/PCbuild/pcbuild.proj @@ -78,9 +78,9 @@ - - - + + + false diff --git a/PCbuild/pyproject.props b/PCbuild/pyproject.props index ca8330c004ccce..95998b6de2d866 100644 --- a/PCbuild/pyproject.props +++ b/PCbuild/pyproject.props @@ -41,7 +41,7 @@ true true - true + true @@ -52,7 +52,7 @@ <_DebugPreprocessorDefinition>NDEBUG; <_DebugPreprocessorDefinition Condition="$(Configuration) == 'Debug'">_DEBUG; <_PyStatsPreprocessorDefinition>PyStats; - <_PyStatsPreprocessorDefinition Condition="$(PySTATS) != ''">Py_STATS; + <_PyStatsPreprocessorDefinition Condition="$(PySTATS) != ''">Py_STATS=1; <_PlatformPreprocessorDefinition>_WIN32; <_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64'">_WIN64; <_PlatformPreprocessorDefinition Condition="$(Platform) == 'x64' and $(PlatformToolset) != 'ClangCL'">_M_X64;$(_PlatformPreprocessorDefinition) diff --git a/PCbuild/python.props b/PCbuild/python.props index 4c49decd7490d1..3c8fe34da93b61 100644 --- a/PCbuild/python.props +++ b/PCbuild/python.props @@ -110,8 +110,8 @@ $(opensslOutDir)include $(ExternalsDir)\nasm-2.11.06\ $(ExternalsDir)\zlib-1.3.1\ - $(ExternalsDir)\zlib-ng-2.2.4\ - $(ExternalsDir)\zstd-1.5.7\ + $(ExternalsDir)\zlib-ng-2.2.5\ + $(ExternalsDir)\zstd-1.6.0\ diff --git a/PCbuild/readme.txt b/PCbuild/readme.txt index d54c0ed3e3c633..2f3e44646b643b 100644 --- a/PCbuild/readme.txt +++ b/PCbuild/readme.txt @@ -260,24 +260,20 @@ _tkinter is able to load Tcl/Tk without having to change your PATH. _zstd - Python wrapper for version 1.5.7 of the zstd compression library + Python wrapper for version 1.6.0 of the zstd compression library Homepage: https://facebook.github.io/zstd/ zlib-ng Compiles zlib-ng as a static library, which is later included by pythoncore.vcxproj. This was generated using CMake against zlib-ng - version 2.2.4, and should be minimally updated as needed to adapt + version 2.2.5, and should be minimally updated as needed to adapt to changes in their source layout. The zbuild.h, zconf.h and zconf-ng.h file in the PC directory were likewise generated and vendored. Sources for zlib-ng are imported unmodified into our source repository at https://github.com/python/cpython-source-deps. -_zstd - Python wrapper for version 1.5.7 of the Zstandard compression library - Homepage: - https://facebook.github.io/zstd/ Getting External Sources diff --git a/Platforms/Android/__main__.py b/Platforms/Android/__main__.py index 27de5d01c1f19b..c8af1a02cd3e67 100755 --- a/Platforms/Android/__main__.py +++ b/Platforms/Android/__main__.py @@ -227,7 +227,7 @@ def unpack_deps(host, prefix_dir, cache_dir): "openssl-3.5.9-0", "sqlite-3.53.4-0", "xz-5.4.6-1", - "zstd-1.5.7-2" + "zstd-1.6.0-0" ]: filename = f"{name_ver}-{host}.tar.gz" out_path = download(f"{deps_url}/{name_ver}/{filename}", cache_dir) diff --git a/Platforms/Apple/__main__.py b/Platforms/Apple/__main__.py index b31b20c2c26f34..b4771652763488 100644 --- a/Platforms/Apple/__main__.py +++ b/Platforms/Apple/__main__.py @@ -418,7 +418,7 @@ def unpack_deps( "OpenSSL-3.5.9-1", "XZ-5.6.4-2", "mpdecimal-4.0.0-2", - "zstd-1.5.7-1", + "zstd-1.6.0-1", ]: filename = f"{name_ver.lower()}-{apple_target(host)}.tar.gz" archive_path = download( diff --git a/Platforms/Apple/testbed/Python.xcframework/build/utils.sh b/Platforms/Apple/testbed/Python.xcframework/build/utils.sh index bbe3620a139731..53801215377fd8 100755 --- a/Platforms/Apple/testbed/Python.xcframework/build/utils.sh +++ b/Platforms/Apple/testbed/Python.xcframework/build/utils.sh @@ -14,7 +14,7 @@ # * There is an `app_packages` folder that contains installed Python packages. # ----- # set -e -# source $PROJECT_DIR/Python.xcframework/build/build_utils.sh +# source "$PROJECT_DIR/Python.xcframework/build/utils.sh" # install_python Python.xcframework app app_packages # ----- @@ -101,9 +101,9 @@ install_dylib () { MODULE_PATH=$(dirname "$FULL_EXT") MODULE_NAME=$(echo $EXT | cut -d "." -f 1) # The location of the extension file, relative to the bundle - RELATIVE_EXT=${FULL_EXT#$CODESIGNING_FOLDER_PATH/} + RELATIVE_EXT=${FULL_EXT#"$CODESIGNING_FOLDER_PATH"/} # The path to the extension file, relative to the install base - PYTHON_EXT=${RELATIVE_EXT/$INSTALL_BASE/} + PYTHON_EXT=${RELATIVE_EXT/"$INSTALL_BASE"/} # The full dotted name of the extension module, constructed from the file path. FULL_MODULE_NAME=$(echo $PYTHON_EXT | cut -d "." -f 1 | tr "/" "."); # A bundle identifier; not actually used, but required by Xcode framework packaging @@ -152,8 +152,8 @@ install_dylib () { process_dylibs () { PYTHON_XCFRAMEWORK_PATH=$1 LIB_PATH=$2 - find "$CODESIGNING_FOLDER_PATH/$LIB_PATH" -name "*.so" | while read FULL_EXT; do - install_dylib $PYTHON_XCFRAMEWORK_PATH "$LIB_PATH/" "$FULL_EXT" + find "$CODESIGNING_FOLDER_PATH/$LIB_PATH" -name "*.so" | while read -r FULL_EXT; do + install_dylib "$PYTHON_XCFRAMEWORK_PATH" "$LIB_PATH/" "$FULL_EXT" done } @@ -168,13 +168,13 @@ install_python() { PYTHON_XCFRAMEWORK_PATH=$1 shift - install_stdlib $PYTHON_XCFRAMEWORK_PATH + install_stdlib "$PYTHON_XCFRAMEWORK_PATH" PYTHON_VER=$(ls -1 "$CODESIGNING_FOLDER_PATH/python/lib" | grep -E "^python3\.\d+[a-z]*$") echo "Install Python $PYTHON_VER standard library extension modules..." - process_dylibs $PYTHON_XCFRAMEWORK_PATH python/lib/$PYTHON_VER/lib-dynload + process_dylibs "$PYTHON_XCFRAMEWORK_PATH" python/lib/$PYTHON_VER/lib-dynload - for package_path in $@; do + for package_path in "$@"; do echo "Installing $package_path extension modules ..." - process_dylibs $PYTHON_XCFRAMEWORK_PATH $package_path + process_dylibs "$PYTHON_XCFRAMEWORK_PATH" "$package_path" done } diff --git a/Platforms/Apple/testbed/iOSTestbed.xcodeproj/project.pbxproj b/Platforms/Apple/testbed/iOSTestbed.xcodeproj/project.pbxproj index 6ac40a325045bd..b2131822424044 100644 --- a/Platforms/Apple/testbed/iOSTestbed.xcodeproj/project.pbxproj +++ b/Platforms/Apple/testbed/iOSTestbed.xcodeproj/project.pbxproj @@ -250,7 +250,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "set -e\nsource $PROJECT_DIR/Python{{abiflags}}.xcframework/build/utils.sh\ninstall_python Python{{abiflags}}.xcframework app app_packages\n"; + shellScript = "set -e\nsource \"$PROJECT_DIR/Python{{abiflags}}.xcframework/build/utils.sh\"\ninstall_python Python{{abiflags}}.xcframework app app_packages\n"; showEnvVarsInLog = 0; }; /* End PBXShellScriptBuildPhase section */ diff --git a/Platforms/Apple/testbed/tvOSTestbed.xcodeproj/project.pbxproj b/Platforms/Apple/testbed/tvOSTestbed.xcodeproj/project.pbxproj index e14489ad0770a0..941f6a266bc42a 100644 --- a/Platforms/Apple/testbed/tvOSTestbed.xcodeproj/project.pbxproj +++ b/Platforms/Apple/testbed/tvOSTestbed.xcodeproj/project.pbxproj @@ -236,7 +236,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "set -e\n\nsource $PROJECT_DIR/Python{{abiflags}}.xcframework/build/utils.sh\n\ninstall_python Python{{abiflags}}.xcframework app app_packages\n"; + shellScript = "set -e\n\nsource \"$PROJECT_DIR/Python{{abiflags}}.xcframework/build/utils.sh\"\n\ninstall_python Python{{abiflags}}.xcframework app app_packages\n"; showEnvVarsInLog = 0; }; /* End PBXShellScriptBuildPhase section */ diff --git a/Platforms/Apple/testbed/visionOSTestbed.xcodeproj/project.pbxproj b/Platforms/Apple/testbed/visionOSTestbed.xcodeproj/project.pbxproj index ee19e22d533cc8..53ddec1b6f29e4 100644 --- a/Platforms/Apple/testbed/visionOSTestbed.xcodeproj/project.pbxproj +++ b/Platforms/Apple/testbed/visionOSTestbed.xcodeproj/project.pbxproj @@ -250,7 +250,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "set -e\n\nsource $PROJECT_DIR/Python{{abiflags}}.xcframework/build/utils.sh\n\ninstall_python Python{{abiflags}}.xcframework app app_packages\n"; + shellScript = "set -e\n\nsource \"$PROJECT_DIR/Python{{abiflags}}.xcframework/build/utils.sh\"\n\ninstall_python Python{{abiflags}}.xcframework app app_packages\n"; showEnvVarsInLog = 0; }; /* End PBXShellScriptBuildPhase section */ diff --git a/Platforms/Apple/testbed/watchOSTestbed.xcodeproj/project.pbxproj b/Platforms/Apple/testbed/watchOSTestbed.xcodeproj/project.pbxproj index d5220c60e65c44..02b98a685cea6c 100644 --- a/Platforms/Apple/testbed/watchOSTestbed.xcodeproj/project.pbxproj +++ b/Platforms/Apple/testbed/watchOSTestbed.xcodeproj/project.pbxproj @@ -236,7 +236,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "set -e\n\nsource $PROJECT_DIR/Python{{abiflags}}.xcframework/build/utils.sh\n\ninstall_python Python{{abiflags}}.xcframework app app_packages\n"; + shellScript = "set -e\n\nsource \"$PROJECT_DIR/Python{{abiflags}}.xcframework/build/utils.sh\"\n\ninstall_python Python{{abiflags}}.xcframework app app_packages\n"; showEnvVarsInLog = 0; }; /* End PBXShellScriptBuildPhase section */ diff --git a/Platforms/WASI/README.md b/Platforms/WASI/README.md index 5fe610615aec33..c99b628d7b88a7 100644 --- a/Platforms/WASI/README.md +++ b/Platforms/WASI/README.md @@ -199,7 +199,36 @@ python3 Platforms/WASI clean ### Testing -To find out where the WASI build directory is, you can run: +After building, you can run the CI tests with: + +```shell +python3 Platforms/WASI test +``` + +The mutually exclusive CI flags to select which tests to run: + +- `--fast-ci` (the default): `make test` +- `--slow-ci`: `make buildbottest` + +The `--target` flag selects the files to test: + +- `build` (the default): the WASI build in `cross-build/`. +- `dist`: the distribution files gathered in `dist/`. +- `archive`: the contents of the release archive, unpacked into `dist/`. + +To thoroughly test the contents of the release archive, use: + +```shell +python3 Platforms/WASI test --slow-ci --target archive +``` + +This requires an archive created by `package` or `release`. It deletes the +selected versioned distribution directory inside `dist/`, then restores it +from the matching `.tar.xz` archive before running the tests. +The archive and other distributions in `dist/` are left untouched. + +You can also run tests more directly. To find out where the WASI build directory +is, you can run: ```shell python3 Platforms/WASI path diff --git a/Platforms/WASI/__main__.py b/Platforms/WASI/__main__.py index 0d504d43283297..2fea2b7d4476cb 100644 --- a/Platforms/WASI/__main__.py +++ b/Platforms/WASI/__main__.py @@ -5,6 +5,7 @@ "_build", "_package", "_shared", + "_test", ] import argparse @@ -14,6 +15,7 @@ import _build import _package import _shared +import _test def main(): @@ -76,6 +78,30 @@ def main(): release = subcommands.add_parser( "release", help="Build and package for a release" ) + test = subcommands.add_parser("test", help="Run the tests") + ci_mode = test.add_mutually_exclusive_group() + ci_mode.add_argument( + "--fast-ci", + action="store_const", + dest="ci_mode", + const="fast", + default="fast", + help="Run `make test` (the default)", + ) + ci_mode.add_argument( + "--slow-ci", + action="store_const", + dest="ci_mode", + const="slow", + help="Run `make buildbottest`", + ) + test.add_argument( + "--target", + choices={"build", "dist", "archive"}, + default="build", + help="Test the build (the default), gathered distribution, or release archive; " + "'archive' replaces the distribution directory with the archive's contents", + ) path = subcommands.add_parser( "path", help="Print the path to a build or distribution directory" ) @@ -100,6 +126,7 @@ def main(): pythoninfo_host, build_host, release, + test, ): subcommand.add_argument( "--quiet", @@ -129,6 +156,13 @@ def main(): dest="clean", help="Delete any relevant directories before building", ) + for subcommand in build, configure_build, configure_host: + subcommand.add_argument( + "--release", + help="Configure build for release mode", + action="store_true", + default=False, + ) for subcommand in ( build, configure_build, @@ -137,7 +171,7 @@ def main(): build_host, ): subcommand.add_argument( - "args", nargs="*", help="Extra arguments to pass to `configure`" + "_args", nargs="*", help="Extra arguments to pass to `configure`" ) for subcommand in build, configure_host, build_host, release: subcommand.add_argument( @@ -167,6 +201,7 @@ def main(): package, path, release, + test, ): subcommand.add_argument( "--host-triple", @@ -219,7 +254,7 @@ def main(): _package.gather(context) _package.archive(context) case "release": - context.args = ["--without-static-libpython"] + context.release = True _build.clean_contents(context) _build.configure_build_python(context) _build.make_build_python(context) @@ -227,6 +262,8 @@ def main(): _build.make_wasi_python(context) _package.gather(context) _package.archive(context) + case "test": + _test.test(context) case "path": paths = { "build-python": "build_python_path", diff --git a/Platforms/WASI/_build.py b/Platforms/WASI/_build.py index 9ea92a58ef5bb3..8efda324c4b048 100644 --- a/Platforms/WASI/_build.py +++ b/Platforms/WASI/_build.py @@ -1,22 +1,16 @@ __lazy_modules__ = [ - "contextlib", - "functools", "os", "pathlib", "shutil", "subprocess", "sys", - "tempfile", ] -import contextlib -import functools import os import pathlib import shutil import subprocess import sys -import tempfile try: from os import process_cpu_count as cpu_count @@ -29,131 +23,19 @@ WASMTIME_HOST_RUNNER_VAR = f"{{{WASMTIME_VAR_NAME}}}" -def separator(): - """Print a separator line across the terminal width.""" - try: - tput_output = subprocess.check_output( - ["tput", "cols"], encoding="utf-8" - ) - except subprocess.CalledProcessError: - terminal_width = 80 - else: - terminal_width = int(tput_output.strip()) - print("⎯" * terminal_width) - - -def updated_env(updates={}): - """Create a new dict representing the environment to use. - - The changes made to the execution environment are printed out. - """ - env_defaults = {} - # https://reproducible-builds.org/docs/source-date-epoch/ - git_epoch_cmd = ["git", "log", "-1", "--pretty=%ct"] - try: - epoch = subprocess.check_output( - git_epoch_cmd, encoding="utf-8" - ).strip() - env_defaults["SOURCE_DATE_EPOCH"] = epoch - except subprocess.CalledProcessError: - pass # Might be building from a tarball. - # This layering lets SOURCE_DATE_EPOCH from os.environ takes precedence. - environment = env_defaults | os.environ | updates - - env_diff = {} - for key, value in environment.items(): - if os.environ.get(key) != value: - env_diff[key] = value - - env_vars = [ - f"\n {key}={item}" for key, item in sorted(env_diff.items()) - ] - _shared.log("🌎", f"Environment changes:{''.join(env_vars)}") - - return environment - - -def subdir(context_attr, *, clean_ok=False): - """Decorator to change to a working directory.""" - - def decorator(func): - @functools.wraps(func) - def wrapper(context): - nonlocal context_attr - - working_dir = getattr(context, context_attr) - separator() - _shared.log("📁", os.fsdecode(working_dir)) - if clean_ok and context.clean and working_dir.exists(): - _shared.log("🚮", "Deleting directory (--clean)...") - shutil.rmtree(working_dir) - - working_dir.mkdir(parents=True, exist_ok=True) - - with contextlib.chdir(working_dir): - return func(context, working_dir) - - return wrapper - - return decorator - - -def call(command, *, context=None, quiet=False, **kwargs): - """Execute a command. - - If 'quiet' is true, then redirect stdout and stderr to a temporary file. - """ - if context is not None: - quiet = context.quiet - - _shared.log("❯", " ".join(map(str, command)), spacing=" ") - if not quiet: - stdout = None - stderr = None - else: - if (log_path := getattr(context, "log_path", None)) is None: - log_path = pathlib.Path(tempfile.gettempdir()) - log_path.mkdir(parents=True, exist_ok=True) - stdout = tempfile.NamedTemporaryFile( - "w", - encoding="utf-8", - delete=False, - dir=log_path, - prefix="cpython-wasi-", - suffix=".log", - ) - stderr = subprocess.STDOUT - _shared.log("📝", f"Logging output to {stdout.name} (--quiet)...") - - try: - subprocess.check_call(command, **kwargs, stdout=stdout, stderr=stderr) - except subprocess.CalledProcessError as error: - if quiet: - _shared.log("❌", f"Exit code {error.returncode}") - separator() - with open(stdout.name, encoding="utf-8") as file: - lines = file.readlines() - # Inefficient, but the log shouldn't be dramatically large. - print("".join(lines[-10:]), end="") - if not lines[-1].endswith("\n"): - print() - sys.exit(error.returncode) - - -@subdir("build_python_path", clean_ok=True) +@_shared.subdir("build_python_path", clean_ok=True) def configure_build_python(context, working_dir): """Configure the build/host Python.""" configure = [os.path.relpath(context.checkout / "configure", working_dir)] - if context.args: - configure.extend(context.args) + configure.extend(context.configure_args) - call(configure, context=context) + context.call(configure) -@subdir("build_python_path") +@_shared.subdir("build_python_path") def make_build_python(context, _working_dir): """Make/build the build Python.""" - call(["make", "--jobs", str(cpu_count()), "all"], context=context) + context.call(["make", "--jobs", str(cpu_count()), "all"]) binary = context.build_python_interpreter cmd = [ @@ -206,7 +88,7 @@ def wasi_sdk_env(context): return env -@subdir("wasi_build_path", clean_ok=True) +@_shared.subdir("wasi_build_path", clean_ok=True) def configure_wasi_python(context, working_dir): """Configure the WASI/host build.""" config_site = os.fsdecode(context.here / "config.site-wasm32-wasi") @@ -247,12 +129,10 @@ def configure_wasi_python(context, working_dir): ] if context.is_debug: configure.append("--with-pydebug") - if context.args: - configure.extend(context.args) - call( + configure.extend(context.configure_args) + context.call( configure, - env=updated_env(env_additions | wasi_sdk_env(context)), - context=context, + env=_shared.updated_env(env_additions | wasi_sdk_env(context)), ) python_wasm = working_dir / "python.wasm" @@ -264,17 +144,18 @@ def configure_wasi_python(context, working_dir): sys.stdout.flush() -@subdir("wasi_build_path") +@_shared.subdir("wasi_build_path") def make_wasi_python(context, working_dir): """Run `make` for the WASI/host build.""" - call( + context.call( ["make", "--jobs", str(cpu_count()), "all"], - env=updated_env(), - context=context, + env=_shared.updated_env(), ) exec_script = working_dir / "python.sh" - call([exec_script, "-c", "import sys; print(sys.version)"], quiet=False) + context.call( + [exec_script, "-c", "import sys; print(sys.version)"], quiet=False + ) _shared.log( "🎉", f"Use `{exec_script.relative_to(pathlib.Path().absolute())}` " @@ -291,13 +172,13 @@ def clean_contents(context): shutil.rmtree(path) -@subdir("build_python_path") +@_shared.subdir("build_python_path") def pythoninfo_build_python(context, working_dir): """Display build info of the build Python.""" - call(["make", "pythoninfo"], context=context) + context.call(["make", "pythoninfo"]) -@subdir("wasi_build_path") +@_shared.subdir("wasi_build_path") def pythoninfo_wasi_python(context, working_dir): """Display build info of the host/WASI Python.""" - call(["make", "pythoninfo"], context=context) + context.call(["make", "pythoninfo"]) diff --git a/Platforms/WASI/_package.py b/Platforms/WASI/_package.py index 862ffb30130d04..403247d4734a3d 100644 --- a/Platforms/WASI/_package.py +++ b/Platforms/WASI/_package.py @@ -26,32 +26,18 @@ def wasmtime_script(context): script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd -P) root=$(CDPATH= cd "$script_dir/.." && pwd -P) -wasm_file="$root/lib/python{python_version(context)}/lib-wasm/python{python_version(context, debug_ok=True)}.wasm" +wasm_file="$root/lib/python{context.python_version()}/lib-wasm/python{context.python_version(debug_ok=True)}.wasm" exec wasmtime run \\ --argv0 "$wasm_file" \\ - --config "$root/etc/python{python_version(context)}/wasmtime.toml" \\ + --config "$root/etc/python{context.python_version()}/wasmtime.toml" \\ --dir "/" \\ "$wasm_file" "$@" """ -def python_version(context, debug_ok=False): - """Calculate the M.N part of Python's version. - - If *debug_ok* is True, then "d" is appended as appropriate. - """ - details = context.wasi_build_details - major = details["language"]["version_info"]["major"] - minor = details["language"]["version_info"]["minor"] - version = f"{major}.{minor}" - if debug_ok and context.is_debug: - version += "d" - return version - - def lib_python(context): - return pathlib.PurePath("lib") / f"python{python_version(context)}" + return pathlib.PurePath("lib") / f"python{context.python_version()}" def license_file(context): @@ -101,12 +87,12 @@ def pkgconfig_files(context): pkgconfig = pathlib.PurePath("lib") / "pkgconfig" return [ ( - pkgconfig / f"python-{python_version(context, debug_ok=True)}.pc", + pkgconfig / f"python-{context.python_version(debug_ok=True)}.pc", misc_dir / "python.pc", ), ( pkgconfig - / f"python-{python_version(context, debug_ok=True)}-embed.pc", + / f"python-{context.python_version(debug_ok=True)}-embed.pc", misc_dir / "python-embed.pc", ), ] @@ -127,11 +113,11 @@ def pkgconfig_symlinks(pkgconfig_files, context): paths += [ ( embed_path.parent - / f"python-{python_version(context)}-embed.pc", + / f"python-{context.python_version()}-embed.pc", embed_path, ), ( - plain_path.parent / f"python-{python_version(context)}.pc", + plain_path.parent / f"python-{context.python_version()}.pc", plain_path, ), ] @@ -143,7 +129,7 @@ def pyconfig_file(context): """Have /pyconfig.h end up in include/pythonXYd?/pyconfig.h.""" return ( pathlib.PurePath("include") - / f"python{python_version(context, debug_ok=True)}" + / f"python{context.python_version(debug_ok=True)}" / "pyconfig.h", context.wasi_build_path / "pyconfig.h", ) @@ -158,7 +144,7 @@ def header_files(context): file_path = pathlib.Path(root) / filename details = ( pathlib.PurePath("include") - / f"python{python_version(context, debug_ok=True)}" + / f"python{context.python_version(debug_ok=True)}" / file_path.relative_to(include_dir), file_path, ) @@ -171,7 +157,7 @@ def man_file(context): man_dir = pathlib.PurePath("share", "man", "man1") man_file = context.checkout / "Misc" / "python.man" return ( - man_dir / f"python{python_version(context)}.1", + man_dir / f"python{context.python_version()}.1", man_file, ) @@ -187,7 +173,7 @@ def wasmtime_config_file(context): config = context.checkout / "Platforms" / "WASI" / "wasmtime.toml" return ( pathlib.PurePath("etc") - / f"python{python_version(context)}" + / f"python{context.python_version()}" / "wasmtime.toml", config, ) @@ -198,17 +184,15 @@ def wasm_file(context): return ( lib_python(context) / "lib-wasm" - / f"python{python_version(context, debug_ok=True)}.wasm", + / f"python{context.python_version(debug_ok=True)}.wasm", context.wasi_build_path / "python.wasm", ) -def python_wasmtime_script(base, context): +def python_wasmtime_script(context): """Create bin/pythonN.Md?.wasmtime.""" script = wasmtime_script(context) - path = ( - base / f"bin/python{python_version(context, debug_ok=True)}.wasmtime" - ) + path = context.dist_python_interpreter path.parent.mkdir(parents=True, exist_ok=True) with open(path, "w") as f: f.write(script) @@ -221,7 +205,7 @@ def python_wasmtime_symlink(path, context): """Symlink bin/pythonN.Md?.wasmtime. - bin/pythonN.M.wasmtime (if debug build) - - bin/pythonN.wasmtime (if debug build) + - bin/pythonN.wasmtime """ symlinks = [ path.parent / f"python{context.wasi_build_version['major']}.wasmtime" @@ -229,7 +213,7 @@ def python_wasmtime_symlink(path, context): if context.is_debug: # The file already has the debug name, so the missing symlink is the non-debug name. symlinks.append( - path.parent / f"python{python_version(context)}.wasmtime" + path.parent / f"python{context.python_version()}.wasmtime" ) return [(symlink, path) for symlink in symlinks] @@ -239,7 +223,7 @@ def config_file(context): """Have Misc/config.sh end up at bin/pythonN.Md?.config.""" return ( pathlib.PurePath("bin") - / f"python{python_version(context, debug_ok=True)}-config", + / f"python{context.python_version(debug_ok=True)}-config", context.wasi_build_path / "Misc" / "python-config.sh", ) @@ -258,7 +242,7 @@ def config_symlink(config_path, context): if context.is_debug: # The file already has the debug name, so the missing symlink is the non-debug name. symlinks.append( - config_path.parent / f"python{python_version(context)}-config" + config_path.parent / f"python{context.python_version()}-config" ) return [(symlink, config_path) for symlink in symlinks] @@ -281,8 +265,8 @@ def symlink_files(files, base): def gather(context): - py_version = python_version(context) - py_d_version = python_version(context, debug_ok=True) + py_version = context.python_version() + py_d_version = context.python_version(debug_ok=True) dist = context.dist_path if dist.exists(): @@ -300,7 +284,7 @@ def gather(context): symlink_files(config_symlink(config_location[0], context), base) _shared.log("📄", f"python{py_d_version}.wasmtime", spacing=indent * 3) - script_path = python_wasmtime_script(base, context) + script_path = python_wasmtime_script(context) symlink_files(python_wasmtime_symlink(script_path, context), base) _shared.log("📁", "etc/", spacing=indent * 2) @@ -353,8 +337,7 @@ def gather(context): def archive(context): - file_name = f"{context.archive_stem}.tar.xz" - file_path = context.dist_path / file_name + file_path = context.archive_path if file_path.exists(): _shared.log("🧹", f"Deleting {file_path} ...") file_path.unlink() @@ -399,6 +382,7 @@ def archive(context): "--owner=0", "--group=0", "--numeric-owner", + "--format=pax", "--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime", "--mode=go+u,go-w", # Explicitly using `-T` because if you don't compress with threads you can't diff --git a/Platforms/WASI/_shared.py b/Platforms/WASI/_shared.py index 306aabd1f1c097..8e5f13d03adc57 100644 --- a/Platforms/WASI/_shared.py +++ b/Platforms/WASI/_shared.py @@ -1,19 +1,25 @@ __lazy_modules__ = [ + "contextlib", "functools", "json", "os", "pathlib", + "shutil", "subprocess", + "sys", "sysconfig", "tempfile", "tomllib", ] +import contextlib import functools import json import os import pathlib +import shutil import subprocess +import sys import sysconfig import tempfile import tomllib @@ -178,6 +184,13 @@ def wasi_sdk_path(self): return wasi_sdk_path + @functools.cached_property + def configure_args(self): + args = getattr(self, "_args", []) + if self.release: + args.extend(["--without-static-libpython"]) + return args + @functools.cached_property def log_path(self): if self._log_path is not None: @@ -206,6 +219,83 @@ def archive_stem(self): def archive_dir(self): return self.dist_path / self.archive_stem + @functools.cached_property + def archive_path(self): + return self.dist_path / f"{self.archive_stem}.tar.xz" + + @functools.cached_property + def dist_python_interpreter(self): + version = self.python_version(debug_ok=True) + return self.archive_dir / "bin" / f"python{version}.wasmtime" + + def python_version(self, debug_ok=False): + """Calculate the M.N part of Python's version. + + If *debug_ok* is True, then "d" is appended as appropriate. + """ + major = self.wasi_build_version["major"] + minor = self.wasi_build_version["minor"] + version = f"{major}.{minor}" + if debug_ok and "d" in self.wasi_build_details["abi"]["flags"]: + version += "d" + return version + + def call(self, command, *, env=None, quiet=None, **kwargs): + """Execute a command. + + Print changes to the execution environment if 'env' is provided. + If 'quiet' is None, use the context's setting. If true, redirect + stdout and stderr to a temporary file. + """ + if quiet is None: + quiet = self.quiet + + if env is not None: + env_diff = {} + for key, value in env.items(): + if os.environ.get(key) != value: + env_diff[key] = value + + env_vars = [ + f"\n {key}={item}" + for key, item in sorted(env_diff.items()) + ] + log("🌎", f"Environment changes:{''.join(env_vars)}") + + log("❯", " ".join(map(str, command)), spacing=" ") + if not quiet: + stdout = None + stderr = None + else: + log_path = self.log_path + log_path.mkdir(parents=True, exist_ok=True) + stdout = tempfile.NamedTemporaryFile( + "w", + encoding="utf-8", + delete=False, + dir=log_path, + prefix="cpython-wasi-", + suffix=".log", + ) + stderr = subprocess.STDOUT + log("📝", f"Logging output to {stdout.name} (--quiet)...") + + try: + subprocess.check_call( + command, **kwargs, env=env, stdout=stdout, stderr=stderr + ) + except subprocess.CalledProcessError as error: + if quiet: + log("❌", f"Exit code {error.returncode}") + separator() + with open(stdout.name, encoding="utf-8") as file: + lines = file.readlines() + # Inefficient, but the log shouldn't be dramatically large. + print("".join(lines[-10:]), end="") + if not lines[-1].endswith("\n"): + print() + sys.exit(error.returncode) + def log(emoji, message, *, spacing=None): """Print a notification with an emoji. @@ -216,3 +306,59 @@ def log(emoji, message, *, spacing=None): if spacing is None: spacing = " " if len(emoji) == 1 else " " print("".join([emoji, spacing, message])) + + +def separator(): + """Print a separator line across the terminal width.""" + try: + tput_output = subprocess.check_output( + ["tput", "cols"], encoding="utf-8" + ) + except subprocess.CalledProcessError: + terminal_width = 80 + else: + terminal_width = int(tput_output.strip()) + print("⎯" * terminal_width) + + +def updated_env(updates={}): + """Create a new dict representing the environment to use.""" + env_defaults = {} + # https://reproducible-builds.org/docs/source-date-epoch/ + git_epoch_cmd = ["git", "log", "-1", "--pretty=%ct"] + try: + epoch = subprocess.check_output( + git_epoch_cmd, encoding="utf-8" + ).strip() + env_defaults["SOURCE_DATE_EPOCH"] = epoch + except subprocess.CalledProcessError: + pass # Might be building from a tarball. + # This layering lets SOURCE_DATE_EPOCH from os.environ takes precedence. + environment = env_defaults | os.environ | updates + + return environment + + +def subdir(context_attr, *, clean_ok=False): + """Decorator to change to a working directory.""" + + def decorator(func): + @functools.wraps(func) + def wrapper(context): + nonlocal context_attr + + working_dir = getattr(context, context_attr) + separator() + log("📁", os.fsdecode(working_dir)) + if clean_ok and context.clean and working_dir.exists(): + log("🚮", "Deleting directory (--clean)...") + shutil.rmtree(working_dir) + + working_dir.mkdir(parents=True, exist_ok=True) + + with contextlib.chdir(working_dir): + return func(context, working_dir) + + return wrapper + + return decorator diff --git a/Platforms/WASI/_test.py b/Platforms/WASI/_test.py new file mode 100644 index 00000000000000..a44838659c7011 --- /dev/null +++ b/Platforms/WASI/_test.py @@ -0,0 +1,73 @@ +__lazy_modules__ = [ + "shlex", + "shutil", + "_shared", +] + +import shlex +import shutil + +import _shared + + +def dist_test_args(context): + runner = context.dist_python_interpreter + if not runner.is_file(): + raise FileNotFoundError( + f"Packaged WASI interpreter not found at {runner}; " + "run `gather`, `package`, or `release` first" + ) + + test_opts = shlex.join(["--python", shlex.quote(str(runner))]) + return [ + "TESTPYTHON=$(RUNSHARED) $(PYTHON_FOR_FREEZE) $(TESTPYTHONOPTS)", + f"TESTOPTS=$(EXTRATESTOPTS) {test_opts}", + ] + + +@_shared.subdir("wasi_build_path") +def test(context, _working_dir): + match context.ci_mode: + case "fast": + make_target = "test" + case "slow": + make_target = "buildbottest" + case _: + raise ValueError(f"Unknown CI mode {context.ci_mode}") + command = ["make", make_target] + match context.target: + case "archive": + archive = context.archive_path + if not archive.is_file(): + raise FileNotFoundError( + f"Release archive not found at {archive}; " + "run `package` or `release` first" + ) + if context.archive_dir.exists(): + _shared.log("🧹", f"Deleting {context.archive_dir} ...") + shutil.rmtree(context.archive_dir) + context.call( + [ + "tar", + "--extract", + "--file", + archive, + "--directory", + context.dist_path, + "--", + context.archive_dir.name, + ], + ) + command.extend(dist_test_args(context)) + case "dist": + if not context.dist_python_interpreter.is_file(): + raise FileNotFoundError( + f"Python not found at {context.dist_python_interpreter}" + ) + command.extend(dist_test_args(context)) + case "build": + pass + case _: + raise ValueError(f"Unknown target {context.target}") + + context.call(command, env=_shared.updated_env()) diff --git a/Platforms/emscripten/config.site-wasm32-emscripten b/Platforms/emscripten/config.site-wasm32-emscripten index 3c9a1c2c2568b0..62640cc772d7e9 100644 --- a/Platforms/emscripten/config.site-wasm32-emscripten +++ b/Platforms/emscripten/config.site-wasm32-emscripten @@ -19,7 +19,6 @@ ac_cv_func_posix_spawn=no ac_cv_func_posix_spawnp=no ac_cv_func_eventfd=no ac_cv_func_memfd_create=no -ac_cv_func_prlimit=no # The rest is based on pyodide # https://github.com/pyodide/pyodide/blob/main/cpython/pyconfig.undefs.h @@ -45,8 +44,6 @@ ac_cv_func_seteuid=no ac_cv_func_setegid=no ac_cv_func_getresuid=no ac_cv_func_getresgid=no -ac_cv_func_setresuid=no -ac_cv_func_setresgid=no # Syscalls not implemented in emscripten # [Errno 52] Function not implemented @@ -89,7 +86,7 @@ ac_cv_func_setresuid=no ac_cv_func_setresgid=no # Emscripten does not support hard links, always fails with errno 34 -# "Too many links". See emscripten_syscall_stubs.c +# "Too many links". ac_cv_func_link=no ac_cv_func_linkat=no diff --git a/Platforms/emscripten/config.toml b/Platforms/emscripten/config.toml index faaa72e1d415f5..920169be2dba0c 100644 --- a/Platforms/emscripten/config.toml +++ b/Platforms/emscripten/config.toml @@ -1,7 +1,7 @@ # Any data that can vary between Python versions is to be kept in this file. # This allows for blanket copying of the Emscripten build code between supported # Python versions. -emscripten-version = "6.0.13" +emscripten-version = "7.0.1" node-version = "24" test-args = [ "-m", "test", @@ -17,8 +17,8 @@ pythoninfo-args = [ [dependencies.libffi] url = "https://github.com/libffi/libffi/releases/download/v{version}/libffi-{version}.tar.gz" -version = "3.4.6" -shasum = "b0dea9df23c863a7a50e825440f3ebffabd65df1497108e5d437747843895a4e" +version = "3.8.1" +shasum = "81e879c094cc5ec3186c48510c6385ac6b3e866192e8bc71a35245703757ee40" [dependencies.mpdec] url = "https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-{version}.tar.gz" diff --git a/Platforms/emscripten/make_libffi.sh b/Platforms/emscripten/make_libffi.sh index 3c75c4d5127102..c05e7c92904a81 100755 --- a/Platforms/emscripten/make_libffi.sh +++ b/Platforms/emscripten/make_libffi.sh @@ -1,7 +1,7 @@ #!/bin/bash set +e -export CFLAGS="-O2 -fPIC -DWASM_BIGINT" +export CFLAGS="-O2 -fPIC" export CXXFLAGS="$CFLAGS" # Build paths diff --git a/Programs/_freeze_module.c b/Programs/_freeze_module.c index 27a60171f3eca8..ae134f6caef1d5 100644 --- a/Programs/_freeze_module.c +++ b/Programs/_freeze_module.c @@ -122,11 +122,12 @@ read_text(const char *inpath) static PyObject * compile_and_marshal(const char *name, const char *text) { - char *filename = (char *) malloc(strlen(name) + 10); + int text_size = strlen(name) + 10; + char *filename = (char *) malloc(text_size); if (filename == NULL) { return PyErr_NoMemory(); } - sprintf(filename, "", name); + PyOS_snprintf(filename, text_size, "", name); PyObject *code = Py_CompileStringExFlags(text, filename, Py_file_input, NULL, 0); free(filename); diff --git a/Python/_warnings.c b/Python/_warnings.c index 86eae5deb85969..c6a2441ec937cf 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -1,9 +1,12 @@ #include "Python.h" +#include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED() #include "pycore_frame.h" // PyFrameObject #include "pycore_genobject.h" // PyAsyncGenObject #include "pycore_import.h" // _PyImport_GetModules() #include "pycore_interpframe.h" // _PyFrame_GetCode() #include "pycore_long.h" // _PyLong_GetZero() +#include "pycore_object.h" // _Py_XGetRef() +#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_SSIZE_ACQUIRE() #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_traceback.h" // _Py_DisplaySourceLine() @@ -142,6 +145,7 @@ _PyWarnings_InitState(PyInterpreterState *interp) if (st->filters == NULL) { return -1; } + PyUnstable_EnableTryIncRef(st->filters); } if (st->once_registry == NULL) { @@ -149,6 +153,7 @@ _PyWarnings_InitState(PyInterpreterState *interp) if (st->once_registry == NULL) { return -1; } + PyUnstable_EnableTryIncRef(st->once_registry); } if (st->default_action == NULL) { @@ -156,6 +161,7 @@ _PyWarnings_InitState(PyInterpreterState *interp) if (st->default_action == NULL) { return -1; } + PyUnstable_EnableTryIncRef(st->default_action); } if (st->context == NULL) { @@ -333,7 +339,30 @@ get_warnings_context_filters(PyInterpreterState *interp) return context_filters; } -// Returns a borrowed reference to the list. +/* Store 'value' in the warnings state field '*ptr'. */ +static void +update_state_ref(PyObject **ptr, PyObject *value) +{ + if (FT_ATOMIC_LOAD_PTR_ACQUIRE(*ptr) == value) { + return; + } + PyUnstable_EnableTryIncRef(value); + PyObject *old = _Py_atomic_exchange_ptr(ptr, Py_NewRef(value)); + Py_XDECREF(old); +} + +/* Return a new reference to the warnings state field '*ptr'. */ +static PyObject * +get_state_ref(PyObject **ptr) +{ +#ifdef Py_GIL_DISABLED + return _Py_XGetRef(ptr); +#else + return Py_XNewRef(*ptr); +#endif +} + +// Returns a new reference to the list. static PyObject * get_warnings_filters(PyInterpreterState *interp) { @@ -342,15 +371,17 @@ get_warnings_filters(PyInterpreterState *interp) if (warnings_filters == NULL) { if (PyErr_Occurred()) return NULL; + warnings_filters = get_state_ref(&st->filters); } else { - Py_SETREF(st->filters, warnings_filters); + update_state_ref(&st->filters, warnings_filters); } - PyObject *filters = st->filters; + PyObject *filters = warnings_filters; if (filters == NULL || !PyList_Check(filters)) { PyErr_SetString(PyExc_ValueError, MODULE_NAME ".filters must be a list"); + Py_XDECREF(filters); return NULL; } return filters; @@ -393,20 +424,20 @@ warnings_release_lock_impl(PyObject *module) Py_RETURN_NONE; } +// Returns a new reference. static PyObject * get_once_registry(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); assert(st != NULL); - assert(warnings_lock_held(st)); - PyObject *registry = GET_WARNINGS_ATTR(interp, onceregistry, 0); if (registry == NULL) { if (PyErr_Occurred()) return NULL; - assert(st->once_registry); - return st->once_registry; + registry = get_state_ref(&st->once_registry); + assert(registry != NULL); + return registry; } if (!PyDict_Check(registry)) { PyErr_Format(PyExc_TypeError, @@ -416,26 +447,26 @@ get_once_registry(PyInterpreterState *interp) Py_DECREF(registry); return NULL; } - Py_SETREF(st->once_registry, registry); + update_state_ref(&st->once_registry, registry); return registry; } +// Returns a new reference. static PyObject * get_default_action(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); assert(st != NULL); - assert(warnings_lock_held(st)); - PyObject *default_action = GET_WARNINGS_ATTR(interp, defaultaction, 0); if (default_action == NULL) { if (PyErr_Occurred()) { return NULL; } - assert(st->default_action); - return st->default_action; + default_action = get_state_ref(&st->default_action); + assert(default_action != NULL); + return default_action; } if (!PyUnicode_Check(default_action)) { PyErr_Format(PyExc_TypeError, @@ -445,7 +476,7 @@ get_default_action(PyInterpreterState *interp) Py_DECREF(default_action); return NULL; } - Py_SETREF(st->default_action, default_action); + update_state_ref(&st->default_action, default_action); return default_action; } @@ -531,18 +562,12 @@ filter_search(PyInterpreterState *interp, PyObject *category, return result; } -/* The item is a new reference. */ +/* The returned action and the item are new references. */ static PyObject* get_filter(PyInterpreterState *interp, PyObject *category, PyObject *text, Py_ssize_t lineno, PyObject *module, PyObject *filename, PyObject **item) { -#ifdef Py_DEBUG - WarningsState *st = warnings_get_state(interp); - assert(st != NULL); - assert(warnings_lock_held(st)); -#endif - /* check _warning_context _filters list */ PyObject *context_filters = get_warnings_context_filters(interp); if (context_filters == NULL) { @@ -560,7 +585,7 @@ get_filter(PyInterpreterState *interp, PyObject *category, } Py_DECREF(context_filters); if (context_action != NULL) { - return context_action; + return Py_NewRef(context_action); } } @@ -572,12 +597,14 @@ get_filter(PyInterpreterState *interp, PyObject *category, if (filters == NULL) { return NULL; } - if (!filter_search(interp, category, text, lineno, module, filename, "filters", - filters, item, &action)) { + bool ok = filter_search(interp, category, text, lineno, module, + filename, "filters", filters, item, &action); + Py_DECREF(filters); + if (!ok) { return NULL; } if (action != NULL) { - return action; + return Py_NewRef(action); } } @@ -591,72 +618,143 @@ get_filter(PyInterpreterState *interp, PyObject *category, } +/* Returns 1 if 'registry' is stamped with 'version', 0 if not, -1 on error. */ static int -already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key, - int should_set) +registry_version_matches(PyObject *registry, Py_ssize_t version) { - PyObject *already_warned; + PyObject *version_obj; + if (PyDict_GetItemRef(registry, &_Py_ID(version), &version_obj) < 0) { + return -1; + } + int rc = 0; + if (version_obj != NULL && PyLong_CheckExact(version_obj)) { + int overflow; + long long v = PyLong_AsLongLongAndOverflow(version_obj, &overflow); + rc = (!overflow && v == version); + } + Py_XDECREF(version_obj); + return rc; +} - if (key == NULL) +static int +set_registry_version(PyObject *registry, Py_ssize_t version) +{ + PyObject *version_obj = PyLong_FromSsize_t(version); + if (version_obj == NULL) { return -1; + } + int rc = PyDict_SetItem(registry, &_Py_ID(version), version_obj); + Py_DECREF(version_obj); + return rc; +} +/* Make 'registry' usable for filters 'version', clearing it if it is stamped + with a different version. Returns 1 if it can be used, 0 if 'version' is + no longer current, -1 on error. */ +static int +check_registry_version(PyInterpreterState *interp, PyObject *registry, + Py_ssize_t version) +{ + _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(registry); WarningsState *st = warnings_get_state(interp); - assert(st != NULL); - assert(warnings_lock_held(st)); - PyObject *version_obj; - if (PyDict_GetItemRef(registry, &_Py_ID(version), &version_obj) < 0) { + int rc = registry_version_matches(registry, version); + if (rc != 0) { + return rc; + } + if (version != FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version)) { + // The caller's filter search is stale, don't record its result. + return 0; + } + + PyDict_Clear(registry); + return set_registry_version(registry, version) < 0 ? -1 : 1; +} + +static int +registry_contains(PyObject *registry, PyObject *key) +{ + PyObject *value; + if (PyDict_GetItemRef(registry, key, &value) < 0) { return -1; } - bool should_update_version = ( - version_obj == NULL - || !PyLong_CheckExact(version_obj) - || PyLong_AsLong(version_obj) != st->filters_version - ); - Py_XDECREF(version_obj); - if (should_update_version) { - PyDict_Clear(registry); - version_obj = PyLong_FromLong(st->filters_version); - if (version_obj == NULL) - return -1; - if (PyDict_SetItem(registry, &_Py_ID(version), version_obj) < 0) { - Py_DECREF(version_obj); - return -1; - } - Py_DECREF(version_obj); + if (value == NULL) { + return 0; } - else { - if (PyDict_GetItemRef(registry, key, &already_warned) < 0) { - return -1; - } - if (already_warned != NULL) { - int rc = PyObject_IsTrue(already_warned); - Py_DECREF(already_warned); - if (rc != 0) - return rc; + int rc = PyObject_IsTrue(value); + Py_DECREF(value); + return rc; +} + +/* Returns 1 if 'key' is recorded in 'registry' for the current filters version, + 0 if not, -1 on error. Stores the version checked in '*pversion'. */ +static int +already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key, + Py_ssize_t *pversion) +{ + int rc; + Py_BEGIN_CRITICAL_SECTION(registry); + WarningsState *st = warnings_get_state(interp); + Py_ssize_t version = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version); + *pversion = version; + rc = registry_version_matches(registry, version); + if (rc > 0) { + rc = registry_contains(registry, key); + } + else if (rc == 0) { + /* Stamped with an old version, reset it. 'key' can't be present. */ + PyDict_Clear(registry); + if (set_registry_version(registry, version) < 0) { + rc = -1; } } + Py_END_CRITICAL_SECTION(); + return rc; +} - /* This warning wasn't found in the registry, set it. */ - if (should_set) - return PyDict_SetItem(registry, key, Py_True); - return 0; +/* Returns 1 if 'key' was already in 'registry', 0 if it was added, -1 on + error. */ +static int +registry_add(PyObject *registry, PyObject *key) +{ + int rc = registry_contains(registry, key); + if (rc == 0 && PyDict_SetItem(registry, key, Py_True) < 0) { + rc = -1; + } + return rc; } +/* Record 'key' and 'altkey' (either may be NULL) in 'registry' for filters + 'version'. Returns 0 if the warning should be shown, 1 if it was already + recorded, -1 on error. */ static int -update_registry(PyInterpreterState *interp, PyObject *registry, PyObject *text, - PyObject *category, int add_zero) +record_warning(PyInterpreterState *interp, PyObject *registry, + PyObject *key, PyObject *altkey, Py_ssize_t version) { - PyObject *altkey; int rc; - - if (add_zero) - altkey = PyTuple_Pack(3, text, category, _PyLong_GetZero()); - else - altkey = _PyTuple_FromPair(text, category); - - rc = already_warned(interp, registry, altkey, 1); - Py_XDECREF(altkey); + Py_BEGIN_CRITICAL_SECTION(registry); + rc = check_registry_version(interp, registry, version); + if (rc > 0) { + rc = 0; + if (key != NULL) { + rc = registry_add(registry, key); + } + if (rc == 0 && altkey != NULL) { + rc = registry_add(registry, altkey); + } + if (rc >= 0) { + /* The critical section may have been suspended while the keys + were added, letting another thread clear and re-stamp the + registry. If so, invalidate it. */ + int match = registry_version_matches(registry, version); + if (match < 0 || + (match == 0 && set_registry_version(registry, -1) < 0)) + { + rc = -1; + } + } + } + Py_END_CRITICAL_SECTION(); return rc; } @@ -795,8 +893,8 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message, PyObject *source) { PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; - PyObject *item = NULL; - PyObject *action; + PyObject *item = NULL, *action = NULL; + PyObject *altkey = NULL, *once_registry = NULL; int rc; PyInterpreterState *interp = tstate->interp; @@ -840,14 +938,23 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message, if (key == NULL) goto cleanup; + /* The warnings lock is not held. 'version' ties the registry updates + to the filters searched; a result from filters that changed since is + never recorded. */ + Py_ssize_t version; if ((registry != NULL) && (registry != Py_None)) { - rc = already_warned(interp, registry, key, 0); + rc = already_warned(interp, registry, key, &version); if (rc == -1) goto cleanup; else if (rc == 1) goto return_none; /* Else this warning hasn't been generated before. */ } + else { + registry = NULL; + WarningsState *st = warnings_get_state(interp); + version = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version); + } action = get_filter(interp, category, text, lineno, module, filename, &item); if (action == NULL) @@ -864,50 +971,58 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message, /* Store in the registry that we've been here, *except* when the action is "always" or "all". */ - rc = 0; if (!_PyUnicode_EqualToASCIIString(action, "always") && !_PyUnicode_EqualToASCIIString(action, "all")) { - if (registry != NULL && registry != Py_None && - PyDict_SetItem(registry, key, Py_True) < 0) + bool is_once = _PyUnicode_EqualToASCIIString(action, "once"); + bool is_module = _PyUnicode_EqualToASCIIString(action, "module"); + if (!is_once && !is_module && + !_PyUnicode_EqualToASCIIString(action, "default")) { + PyErr_Format(PyExc_RuntimeError, + "Unrecognized action (%R) in warnings.filters:\n %R", + action, item); goto cleanup; } - if (_PyUnicode_EqualToASCIIString(action, "once")) { - if (registry == NULL || registry == Py_None) { - registry = get_once_registry(interp); - if (registry == NULL) + if (is_once && registry == NULL) { + /* WarningsState.once_registry[(text, category)] = 1 */ + once_registry = get_once_registry(interp); + if (once_registry == NULL) + goto cleanup; + altkey = _PyTuple_FromPair(text, category); + if (altkey == NULL) + goto cleanup; + rc = record_warning(interp, once_registry, NULL, altkey, version); + } + else if (registry != NULL) { + /* registry[key] = 1, and for "once" and "module" also + registry[(text, category)] = 1 */ + if (is_once || is_module) { + altkey = _PyTuple_FromPair(text, category); + if (altkey == NULL) goto cleanup; } - /* WarningsState.once_registry[(text, category)] = 1 */ - rc = update_registry(interp, registry, text, category, 0); + rc = record_warning(interp, registry, key, altkey, version); } - else if (_PyUnicode_EqualToASCIIString(action, "module")) { - /* registry[(text, category, 0)] = 1 */ - if (registry != NULL && registry != Py_None) - rc = update_registry(interp, registry, text, category, 0); + else { + rc = 0; } - else if (!_PyUnicode_EqualToASCIIString(action, "default")) { - PyErr_Format(PyExc_RuntimeError, - "Unrecognized action (%R) in warnings.filters:\n %R", - action, item); + if (rc == -1) goto cleanup; - } + if (rc == 1) /* Already warned for this module. */ + goto return_none; } - if (rc == 1) /* Already warned for this module. */ - goto return_none; - if (rc == 0) { - if (call_show_warning(tstate, category, text, message, filename, - lineno, lineno_obj, sourceline, source, module) < 0) - goto cleanup; - } - else /* if (rc == -1) */ + if (call_show_warning(tstate, category, text, message, filename, + lineno, lineno_obj, sourceline, source, module) < 0) goto cleanup; return_none: result = Py_NewRef(Py_None); cleanup: + Py_XDECREF(once_registry); + Py_XDECREF(altkey); + Py_XDECREF(action); Py_XDECREF(item); Py_XDECREF(key); Py_XDECREF(text); @@ -1154,10 +1269,8 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level, &filename, &lineno, &module, ®istry)) return NULL; - warnings_lock(tstate->interp); res = warn_explicit(tstate, category, message, filename, lineno, module, registry, NULL, source); - warnings_unlock(tstate->interp); Py_DECREF(filename); Py_DECREF(registry); Py_DECREF(module); @@ -1335,10 +1448,8 @@ warnings_warn_explicit_impl(PyObject *module, PyObject *message, } } - warnings_lock(tstate->interp); returned = warn_explicit(tstate, category, message, filename, lineno, mod, registry, source_line, sourceobj); - warnings_unlock(tstate->interp); Py_XDECREF(source_line); return returned; } @@ -1366,7 +1477,7 @@ warnings_filters_mutated_lock_held_impl(PyObject *module) return NULL; } - st->filters_version++; + FT_ATOMIC_ADD_SSIZE(st->filters_version, 1); Py_RETURN_NONE; } @@ -1485,10 +1596,8 @@ PyErr_WarnExplicitObject(PyObject *category, PyObject *message, return -1; } - warnings_lock(tstate->interp); res = warn_explicit(tstate, category, message, filename, lineno, module, registry, NULL, NULL); - warnings_unlock(tstate->interp); if (res == NULL) return -1; Py_DECREF(res); @@ -1553,10 +1662,8 @@ PyErr_WarnExplicitFormat(PyObject *category, PyObject *res; PyThreadState *tstate = get_current_tstate(); if (tstate != NULL) { - warnings_lock(tstate->interp); res = warn_explicit(tstate, category, message, filename, lineno, module, registry, NULL, NULL); - warnings_unlock(tstate->interp); Py_DECREF(message); if (res != NULL) { Py_DECREF(res); diff --git a/Python/assemble.c b/Python/assemble.c index 8f68e233ab7b93..9f4ffb4050d259 100644 --- a/Python/assemble.c +++ b/Python/assemble.c @@ -4,7 +4,10 @@ #include "pycore_instruction_sequence.h" #include "pycore_opcode_utils.h" // IS_BACKWARDS_JUMP_OPCODE #include "pycore_opcode_metadata.h" // is_pseudo_target, _PyOpcode_Caches +#include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_symtable.h" // _Py_SourceLocation +#include "pycore_tuple.h" // _PyTuple_ITEMS() +#include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal() #include @@ -503,6 +506,27 @@ dict_keys_inorder(PyObject *dict, Py_ssize_t offset) extern void _Py_set_localsplus_info(int, PyObject *, unsigned char, PyObject *, PyObject *); +/* Intern the names in *tuple*, as _PyCode_New() does, before the tuple is + * merged with the constant cache. + * + * The merge can replace the tuple by an equal tuple constant of another + * code object, whose items _PyCode_New() then interns in place. A string + * of that constant would become the interned string or be replaced by it, + * depending on whether an equal string was already interned in the process, + * and so would the output of marshal for the same source. Names which the + * parser did not create, such as mangled private names or the identifiers + * of an AST passed to compile(), may not be interned yet. + */ +static void +intern_names(PyObject *tuple) +{ + PyInterpreterState *interp = _PyInterpreterState_GET(); + for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(tuple); i++) { + assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(tuple, i))); + _PyUnicode_InternImmortal(interp, &_PyTuple_ITEMS(tuple)[i]); + } +} + static int compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus, int flags, PyObject *names, PyObject *kinds) @@ -610,6 +634,7 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_ if (!names) { goto error; } + intern_names(names); if (_PyCompile_ConstCacheMergeOne(const_cache, &names) < 0) { goto error; } @@ -674,6 +699,7 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_ goto error; } + intern_names(localsplusnames); if (_PyCompile_ConstCacheMergeOne(const_cache, &localsplusnames) < 0) { goto error; } diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index c8e58f77d1d375..72f2e356c34f7c 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2240,7 +2240,7 @@ builtin_print_impl(PyObject *module, PyObject * const *objects, PyObject *file, int flush) /*[clinic end generated code: output=38d8def56c837bcc input=ff35cb3d59ee8115]*/ { - int i, err; + int i; if (file == Py_None) { file = PySys_GetAttr(&_Py_ID(stdout)); @@ -2259,7 +2259,7 @@ builtin_print_impl(PyObject *module, PyObject * const *objects, } if (sep == Py_None) { - sep = NULL; + sep = _Py_LATIN1_CHR(' '); } else if (sep && !PyUnicode_Check(sep)) { PyErr_Format(PyExc_TypeError, @@ -2269,7 +2269,7 @@ builtin_print_impl(PyObject *module, PyObject * const *objects, return NULL; } if (end == Py_None) { - end = NULL; + end = _Py_LATIN1_CHR('\n'); } else if (end && !PyUnicode_Check(end)) { PyErr_Format(PyExc_TypeError, @@ -2280,32 +2280,16 @@ builtin_print_impl(PyObject *module, PyObject * const *objects, } for (i = 0; i < objects_length; i++) { - if (i > 0) { - if (sep == NULL) { - err = PyFile_WriteString(" ", file); - } - else { - err = PyFile_WriteObject(sep, file, Py_PRINT_RAW); - } - if (err) { - Py_DECREF(file); - return NULL; - } + if (i > 0 && PyFile_WriteObject(sep, file, Py_PRINT_RAW) < 0) { + Py_DECREF(file); + return NULL; } - err = PyFile_WriteObject(objects[i], file, Py_PRINT_RAW); - if (err) { + if (PyFile_WriteObject(objects[i], file, Py_PRINT_RAW) < 0) { Py_DECREF(file); return NULL; } } - - if (end == NULL) { - err = PyFile_WriteString("\n", file); - } - else { - err = PyFile_WriteObject(end, file, Py_PRINT_RAW); - } - if (err) { + if (PyFile_WriteObject(end, file, Py_PRINT_RAW) < 0) { Py_DECREF(file); return NULL; } diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 42ad83daa1ea46..c6f1f61f3e08fe 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1641,19 +1641,23 @@ dummy_func( iter = PyStackRef_FromPyObjectSteal(iter_o); } - inst(GET_ANEXT, (aiter -- aiter, awaitable)) { + inst(GET_ANEXT, (aiter -- aiter, awaitable, null, none)) { PyObject *awaitable_o = _PyEval_GetANext(PyStackRef_AsPyObjectBorrow(aiter)); if (awaitable_o == NULL) { ERROR_NO_POP(); } awaitable = PyStackRef_FromPyObjectSteal(awaitable_o); + null = PyStackRef_NULL; + none = PyStackRef_None; } - inst(GET_AWAITABLE, (iterable -- iter)) { + inst(GET_AWAITABLE, (iterable -- iter, null, none)) { PyObject *iter_o = _PyEval_GetAwaitable(PyStackRef_AsPyObjectBorrow(iterable), oparg); PyStackRef_CLOSE(iterable); ERROR_IF(iter_o == NULL); iter = PyStackRef_FromPyObjectSteal(iter_o); + null = PyStackRef_NULL; + none = PyStackRef_None; } family(SEND, INLINE_CACHE_ENTRIES_SEND) = { @@ -2278,7 +2282,7 @@ dummy_func( assert(index < DK_SIZE(keys)); PyObject *res_o = FT_ATOMIC_LOAD_PTR_CONSUME(entries[index].me_value); DEOPT_IF(res_o == NULL); - #if Py_GIL_DISABLED + #ifdef Py_GIL_DISABLED int increfed = _Py_TryIncrefCompareStackRef(&entries[index].me_value, res_o, &res); DEOPT_IF(!increfed); #else @@ -2297,7 +2301,7 @@ dummy_func( PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(keys); PyObject *res_o = FT_ATOMIC_LOAD_PTR_CONSUME(entries[index].me_value); DEOPT_IF(res_o == NULL); - #if Py_GIL_DISABLED + #ifdef Py_GIL_DISABLED int increfed = _Py_TryIncrefCompareStackRef(&entries[index].me_value, res_o, &res); DEOPT_IF(!increfed); #else diff --git a/Python/ceval.c b/Python/ceval.c index 0d053cdc7a63e3..ccc0edca2fc794 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1200,7 +1200,13 @@ dtrace_function_return(_PyInterpreterFrame *frame) } #endif -PyObject* _Py_HOT_FUNCTION DONT_SLP_VECTORIZE +/* + * _PyEval_EvalFrameDefault must remain a separate frame in the C call stack: + * stack unwinders, profilers and other tools that walk the stack use it to + * find the interpreter loop. With the tail-calling interpreter it is small + * enough to be inlined into its callers, e.g. by Clang with PGO. + */ +Py_NO_INLINE PyObject* _Py_HOT_FUNCTION DONT_SLP_VECTORIZE _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) { _Py_EnsureTstateNotNULL(tstate); diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c index 91c75cdd2ed413..a06eb4f72ece37 100644 --- a/Python/ceval_gil.c +++ b/Python/ceval_gil.c @@ -1387,7 +1387,7 @@ _Py_HandlePending(PyThreadState *tstate) _Py_brc_merge_refcounts(tstate); } /* Process deferred memory frees held by QSBR */ - if (_Py_qsbr_should_process(((_PyThreadStateImpl *)tstate)->qsbr)) { + if ((breaker & _PY_EVAL_PROCESS_DELAYED_BIT) != 0) { _PyMem_ProcessDelayed(tstate); } #endif diff --git a/Python/clinic/context.c.h b/Python/clinic/context.c.h index ece7341d65d5fb..700e2ae3711309 100644 --- a/Python/clinic/context.c.h +++ b/Python/clinic/context.c.h @@ -2,6 +2,10 @@ preserve [clinic start generated code]*/ +#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) +# include "pycore_gc.h" // PyGC_Head +# include "pycore_runtime.h" // _Py_ID() +#endif #include "pycore_modsupport.h" // _PyArg_CheckPositional() PyDoc_STRVAR(_contextvars_Context_get__doc__, @@ -116,6 +120,88 @@ _contextvars_Context_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) return _contextvars_Context_copy_impl((PyContext *)self); } +static PyObject * +contextvar_tp_new_impl(PyTypeObject *type, PyObject *name, PyObject *def); + +static PyObject * +contextvar_tp_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + + #define NUM_KEYWORDS 1 + static struct { + PyGC_Head _this_is_not_used; + PyObject_VAR_HEAD + Py_hash_t ob_hash; + PyObject *ob_item[NUM_KEYWORDS]; + } _kwtuple = { + .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) + .ob_hash = -1, + .ob_item = { &_Py_ID(default), }, + }; + #undef NUM_KEYWORDS + #define KWTUPLE (&_kwtuple.ob_base.ob_base) + + #else // !Py_BUILD_CORE + # define KWTUPLE NULL + #endif // !Py_BUILD_CORE + + static const char * const _keywords[] = {"", "default", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "ContextVar", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[2]; + PyObject * const *fastargs; + Py_ssize_t noptargs = nargs + nkw - 1; + PyObject *name; + PyObject *def = NULL; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); + if (!fastargs) { + goto exit; + } + name = fastargs[0]; + if (!noptargs) { + goto skip_optional_kwonly; + } + def = fastargs[1]; +skip_optional_kwonly: + return_value = contextvar_tp_new_impl(type, name, def); + +exit: + return return_value; +} + +static PyObject * +contextvar_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return contextvar_tp_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +contextvar_tp_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == contextvar_tp_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + return contextvar_tp_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + PyDoc_STRVAR(_contextvars_ContextVar_get__doc__, "get($self, default=, /)\n" "--\n" @@ -259,4 +345,4 @@ token_exit(PyObject *self, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=90ec3e4375804e9b input=a9049054013a1b77]*/ +/*[clinic end generated code: output=488efab9f3932fbb input=a9049054013a1b77]*/ diff --git a/Python/codecs.c b/Python/codecs.c index ba6f618f77e6f6..6eb728e7f9f202 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -139,7 +139,10 @@ PyObject *_PyCodec_Lookup(const char *encoding) } PyInterpreterState *interp = _PyInterpreterState_GET(); - assert(interp->codecs.initialized); + if (!interp->codecs.initialized) { + PyErr_SetString(PyExc_SystemError, "Codecs are not initialized"); + return NULL; + } /* Convert the encoding to a normalized Python string: all ASCII letters are converted to lower case, spaces are diff --git a/Python/codegen.c b/Python/codegen.c index a975705576a167..ae60caf195f23c 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -618,8 +618,6 @@ codegen_unwind_fblock(compiler *c, location *ploc, RETURN_IF_ERROR(codegen_call_exit_with_nones(c, *ploc)); if (info->fb_type == COMPILE_FBLOCK_ASYNC_WITH) { ADDOP_I(c, *ploc, GET_AWAITABLE, 2); - ADDOP(c, *ploc, PUSH_NULL); - ADDOP_LOAD_CONST(c, *ploc, Py_None); ADD_YIELD_FROM(c, *ploc, 1); } ADDOP(c, *ploc, POP_TOP); @@ -2225,8 +2223,6 @@ codegen_async_for(compiler *c, stmt_ty s) /* SETUP_FINALLY to guard the __anext__ call */ ADDOP_JUMP(c, loc, SETUP_FINALLY, except); ADDOP(c, loc, GET_ANEXT); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); USE_LABEL(c, send); ADD_YIELD_FROM(c, loc, 1); ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */ @@ -4843,8 +4839,6 @@ codegen_async_comprehension_generator(compiler *c, location loc, ADDOP_JUMP(c, loc, SETUP_FINALLY, except); ADDOP(c, loc, GET_ANEXT); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); USE_LABEL(c, send); ADD_YIELD_FROM(c, loc, 1); ADDOP(c, loc, POP_BLOCK); @@ -5167,8 +5161,6 @@ codegen_comprehension(compiler *c, expr_ty e, int type, if (is_async_comprehension && type != COMP_GENEXP) { ADDOP_I(c, loc, GET_AWAITABLE, 0); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); ADD_YIELD_FROM(c, loc, 1); } @@ -5312,8 +5304,6 @@ codegen_async_with_inner(compiler *c, stmt_ty s, int pos) ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___AENTER__); ADDOP_I(c, loc, CALL, 0); ADDOP_I(c, loc, GET_AWAITABLE, 1); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); ADD_YIELD_FROM(c, loc, 1); ADDOP_JUMP(c, loc, SETUP_WITH, final); @@ -5349,8 +5339,6 @@ codegen_async_with_inner(compiler *c, stmt_ty s, int pos) */ RETURN_IF_ERROR(codegen_call_exit_with_nones(c, loc)); ADDOP_I(c, loc, GET_AWAITABLE, 2); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); ADD_YIELD_FROM(c, loc, 1); ADDOP(c, loc, POP_TOP); @@ -5364,8 +5352,6 @@ codegen_async_with_inner(compiler *c, stmt_ty s, int pos) ADDOP(c, loc, PUSH_EXC_INFO); ADDOP(c, loc, WITH_EXCEPT_START); ADDOP_I(c, loc, GET_AWAITABLE, 2); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); ADD_YIELD_FROM(c, loc, 1); RETURN_IF_ERROR(codegen_with_except_finish(c, cleanup)); @@ -5552,8 +5538,6 @@ codegen_visit_expr_impl(compiler *c, expr_ty e, bool result_is_unused) case Await_kind: VISIT(c, expr, e->v.Await.value); ADDOP_I(c, loc, GET_AWAITABLE, 0); - ADDOP(c, loc, PUSH_NULL); - ADDOP_LOAD_CONST(c, loc, Py_None); ADD_YIELD_FROM(c, loc, 1); break; case Compare_kind: diff --git a/Python/context.c b/Python/context.c index 29c483d2dc90d6..0d931ef74b5646 100644 --- a/Python/context.c +++ b/Python/context.c @@ -866,8 +866,13 @@ contextvar_set(PyContextVar *var, PyObject *val) return -1; } + // gh-148891: Make a strong reference so a GC during _PyHamt_Assoc will + // not free the HAMT during traversal. + PyHamtObject *old_vars = context_get_current_vars(ctx); + Py_INCREF(old_vars); PyHamtObject *new_vars = _PyHamt_Assoc( - context_get_current_vars(ctx), (PyObject *)var, val); + old_vars, (PyObject *)var, val); + Py_DECREF(old_vars); if (new_vars == NULL) { return -1; } @@ -894,8 +899,12 @@ contextvar_del(PyContextVar *var) return -1; } + // gh-148891: Make a strong reference so a GC during _PyHamt_Without will + // not free the HAMT during traversal. PyHamtObject *vars = context_get_current_vars(ctx); + Py_INCREF(vars); PyHamtObject *new_vars = _PyHamt_Without(vars, (PyObject *)var); + Py_DECREF(vars); if (new_vars == NULL) { return -1; } @@ -985,19 +994,20 @@ class _contextvars.ContextVar "PyContextVar *" "&PyContextVar_Type" #define _PyContextVar_CAST(op) ((PyContextVar *)(op)) +/*[clinic input] +@vectorcall +@classmethod +_contextvars.ContextVar.__new__ as contextvar_tp_new + name: object + / + * + default as def: object = NULL +[clinic start generated code]*/ + static PyObject * -contextvar_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +contextvar_tp_new_impl(PyTypeObject *type, PyObject *name, PyObject *def) +/*[clinic end generated code: output=1c8af17a14fc9dc2 input=d5e8cf845a1ddc27]*/ { - static char *kwlist[] = {"", "default", NULL}; - PyObject *name; - PyObject *def = NULL; - - if (!PyArg_ParseTupleAndKeywords( - args, kwds, "O|$O:ContextVar", kwlist, &name, &def)) - { - return NULL; - } - return (PyObject *)contextvar_new(name, def); } @@ -1191,6 +1201,7 @@ PyTypeObject PyContextVar_Type = { .tp_traverse = contextvar_tp_traverse, .tp_clear = contextvar_tp_clear, .tp_new = contextvar_tp_new, + .tp_vectorcall = contextvar_tp_vectorcall, .tp_free = PyObject_GC_Del, .tp_hash = contextvar_tp_hash, .tp_repr = contextvar_tp_repr, diff --git a/Python/crossinterp.c b/Python/crossinterp.c index b8dd5f11c2a44c..a91b45ba2cbcf4 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -388,6 +388,8 @@ _PyXIData_InitWithSize(_PyXIData_t *xidata, _PyXIData_Init(xidata, interp, NULL, obj, new_object); xidata->data = PyMem_RawCalloc(1, size); if (xidata->data == NULL) { + Py_CLEAR(xidata->obj); + PyErr_NoMemory(); return -1; } xidata->free = PyMem_RawFree; @@ -2016,6 +2018,14 @@ _PyXI_ApplyError(_PyXI_error *error, const char *failure) PyThreadState *tstate = PyThreadState_Get(); if (failure != NULL) { + if (error->override != NULL + && error->override->code == _PyXI_ERR_NO_MEMORY) + { + PyErr_NoMemory(); + } + else { + PyErr_SetString(PyExc_InterpreterError, failure); + } xi_error_clear(error); return NULL; } diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index bc85ccd9ddb4fc..5df8120e7424b9 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -8877,11 +8877,13 @@ break; } - case _GET_ANEXT_r12: { + case _GET_ANEXT_r13: { CHECK_CURRENT_CACHED_VALUES(1); ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(__FILE__, __LINE__); _PyStackRef aiter; _PyStackRef awaitable; + _PyStackRef null; + _PyStackRef none; _PyStackRef _stack_item_0 = _tos_cache0; aiter = _stack_item_0; stack_pointer[0] = aiter; @@ -8896,21 +8898,23 @@ JUMP_TO_ERROR(); } awaitable = PyStackRef_FromPyObjectSteal(awaitable_o); - _tos_cache1 = awaitable; - _tos_cache0 = aiter; - _tos_cache2 = PyStackRef_ZERO_BITS; - SET_CURRENT_CACHED_VALUES(2); - stack_pointer += -1; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); + null = PyStackRef_NULL; + none = PyStackRef_None; + _tos_cache2 = none; + _tos_cache1 = null; + _tos_cache0 = awaitable; + SET_CURRENT_CACHED_VALUES(3); ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(__FILE__, __LINE__); break; } - case _GET_AWAITABLE_r11: { + case _GET_AWAITABLE_r13: { CHECK_CURRENT_CACHED_VALUES(1); ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(__FILE__, __LINE__); _PyStackRef iterable; _PyStackRef iter; + _PyStackRef null; + _PyStackRef none; _PyStackRef _stack_item_0 = _tos_cache0; oparg = CURRENT_OPARG(); iterable = _stack_item_0; @@ -8932,10 +8936,12 @@ JUMP_TO_ERROR(); } iter = PyStackRef_FromPyObjectSteal(iter_o); + null = PyStackRef_NULL; + none = PyStackRef_None; + _tos_cache2 = none; + _tos_cache1 = null; _tos_cache0 = iter; - _tos_cache1 = PyStackRef_ZERO_BITS; - _tos_cache2 = PyStackRef_ZERO_BITS; - SET_CURRENT_CACHED_VALUES(1); + SET_CURRENT_CACHED_VALUES(3); ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(__FILE__, __LINE__); break; } @@ -10440,7 +10446,7 @@ SET_CURRENT_CACHED_VALUES(0); JUMP_TO_JUMP_TARGET(); } - #if Py_GIL_DISABLED + #ifdef Py_GIL_DISABLED int increfed = _Py_TryIncrefCompareStackRef(&entries[index].me_value, res_o, &res); if (!increfed) { UOP_STAT_INC(uopcode, miss); @@ -10485,7 +10491,7 @@ SET_CURRENT_CACHED_VALUES(0); JUMP_TO_JUMP_TARGET(); } - #if Py_GIL_DISABLED + #ifdef Py_GIL_DISABLED int increfed = _Py_TryIncrefCompareStackRef(&entries[index].me_value, res_o, &res); if (!increfed) { UOP_STAT_INC(uopcode, miss); diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 7b8a3e9cf9b57b..fc676dc6feb167 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -3557,8 +3557,9 @@ push_cold_blocks_to_end(cfg_builder *g) { if (!IS_LABEL(b->b_next->b_label)) { b->b_next->b_label.id = next_lbl++; } - basicblock_addop(explicit_jump, JUMP_NO_INTERRUPT, b->b_next->b_label.id, - NO_LOCATION, NULL); + RETURN_IF_ERROR( + basicblock_addop(explicit_jump, JUMP_NO_INTERRUPT, + b->b_next->b_label.id, NO_LOCATION, NULL)); explicit_jump->b_cold = 1; explicit_jump->b_next = b->b_next; explicit_jump->b_predecessors = 1; diff --git a/Python/gc.c b/Python/gc.c index 5dc79e20191ebc..aa727fc7a77d6a 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -996,11 +996,55 @@ clear_weakrefs(PyGC_Head *unreachable) } } -static void +/* "gc: " + " <" + " 0x" + 16 hex digits + ">\n" + NUL, excluding msg and + * tp_name */ +#define DEBUG_LINE_FIXED_LEN 32 + +static char * debug_cycle(const char *msg, PyObject *op) { - PySys_FormatStderr("gc: %s <%s %p>\n", - msg, Py_TYPE(op)->tp_name, op); + const char *name = Py_TYPE(op)->tp_name; + size_t size = strlen(msg) + strlen(name) + DEBUG_LINE_FIXED_LEN; + char *line = PyMem_RawMalloc(size); + if (line == NULL) { + return NULL; + } + /* Not %p: its output is platform-specific (no "0x" prefix on Windows). */ + PyOS_snprintf(line, size, "gc: %s <%s 0x%" PRIxPTR ">\n", + msg, name, (uintptr_t)op); + return line; +} + +/* Format a line for each object in `list` before printing any of them, + * since sys.stderr.write() can run Python code that frees objects in the + * list. + */ +static Py_ssize_t +debug_cycles(const char *msg, PyGC_Head *list) +{ + Py_ssize_t n = gc_list_size(list); + if (n == 0) { + return n; + } + + char **lines = PyMem_RawCalloc(n, sizeof(char *)); + if (lines == NULL) { + return n; + } + + Py_ssize_t i = 0; + for (PyGC_Head *gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc), i++) { + lines[i] = debug_cycle(msg, FROM_GC(gc)); + } + + for (i = 0; i < n; i++) { + if (lines[i] != NULL) { + PySys_FormatStderr("%s", lines[i]); + } + PyMem_RawFree(lines[i]); + } + PyMem_RawFree(lines); + return n; } /* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable @@ -1435,7 +1479,6 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason) PyGC_Head *old; /* next older generation */ PyGC_Head unreachable; /* non-problematic unreachable trash */ PyGC_Head finalizers; /* objects with, & reachable from, __del__ */ - PyGC_Head *gc; GCState *gcstate = &tstate->interp->gc; // gc_collect_main() must not be called before _PyGC_Init @@ -1558,9 +1601,7 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason) if (gcstate->debug & _PyGC_DEBUG_COLLECTABLE) { /* sys.stderr.write() can resurrect objects. */ needs_resurrection_check = true; - for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) { - debug_cycle("collectable", FROM_GC(gc)); - } + debug_cycles("collectable", &unreachable); } /* Clear weakrefs and invoke callbacks as necessary. */ @@ -1603,13 +1644,12 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason) /* Collect statistics on uncollectable objects found and print * debugging information. */ - Py_ssize_t n = 0; - for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) { - n++; - if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) - debug_cycle("uncollectable", FROM_GC(gc)); + if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) { + stats.uncollectable = debug_cycles("uncollectable", &finalizers); + } + else { + stats.uncollectable = gc_list_size(&finalizers); } - stats.uncollectable = n; (void)PyTime_PerfCounterRaw(&stats.ts_stop); stats.duration = PyTime_AsSecondsDouble(stats.ts_stop - stats.ts_start); if (gcstate->debug & _PyGC_DEBUG_STATS) { @@ -2027,7 +2067,7 @@ static PyObject * gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize) { PyThreadState *tstate = _PyThreadState_GET(); - if (basicsize > PY_SSIZE_T_MAX - presize) { + if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) { return _PyErr_NoMemory(tstate); } size_t size = presize + basicsize; @@ -2098,6 +2138,13 @@ PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size) PyVarObject * _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) { +#ifdef Py_DEBUG + if (nitems < 0) { + PyErr_BadInternalCall(); + return NULL; + } +#endif + const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); const size_t presize = _PyType_PreHeaderSize(Py_TYPE(op)); _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op)); diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index 6eaeeea8848cdb..f5e7461cded9d6 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -2048,7 +2048,7 @@ record_allocation(PyThreadState *tstate) if (gc->alloc_count >= LOCAL_ALLOC_COUNT_THRESHOLD) { // TODO: Use Py_ssize_t for the generation count. GCState *gcstate = &tstate->interp->gc; - _Py_atomic_add_int(&gcstate->young.count, (int)gc->alloc_count); + _Py_atomic_add_int_relaxed(&gcstate->young.count, (int)gc->alloc_count); gc->alloc_count = 0; if (gc_should_collect(gcstate) && @@ -2077,9 +2077,9 @@ record_deallocation(PyThreadState *tstate) if (new_count < 0) { new_count = 0; } - } while (!_Py_atomic_compare_exchange_int(&gcstate->young.count, - &count, - new_count)); + } while (!_Py_atomic_compare_exchange_int_relaxed(&gcstate->young.count, + &count, + new_count)); gc->alloc_count = 0; } } @@ -2158,20 +2158,24 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state, // Deallocations after the refcount merge may run Python code. bool needs_resurrection_check = state->objs_to_decref.head != 0; - // Print debugging information. - if (FT_ATOMIC_LOAD_INT_RELAXED(interp->gc.debug) & _PyGC_DEBUG_COLLECTABLE) { - needs_resurrection_check = true; - PyObject *op; - WORKSTACK_FOR_EACH(&state->unreachable, op) { - debug_cycle("collectable", op); - } - } + // The debug output is written with sys.stderr.write(), which may run + // Python code, so it is printed below after the world is started. + bool debug_print = (FT_ATOMIC_LOAD_INT_RELAXED(interp->gc.debug) & + _PyGC_DEBUG_COLLECTABLE) != 0; // Find weakref callbacks and pending finalizers (but do not call them). bool has_finalizers = prepare_finalization(state, &needs_resurrection_check); - if (needs_resurrection_check) { + if (needs_resurrection_check || debug_print) { _PyEval_StartTheWorld(interp); + // Print debugging information. + if (debug_print) { + PyObject *op; + WORKSTACK_FOR_EACH(&state->unreachable, op) { + debug_cycle("collectable", op); + } + } + // Deallocate any object from the refcount merge step cleanup_worklist(&state->objs_to_decref); @@ -2771,7 +2775,7 @@ static PyObject * gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize) { PyThreadState *tstate = _PyThreadState_GET(); - if (basicsize > PY_SSIZE_T_MAX - presize) { + if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) { return _PyErr_NoMemory(tstate); } size_t size = presize + basicsize; @@ -2843,6 +2847,13 @@ PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size) PyVarObject * _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) { +#ifdef Py_DEBUG + if (nitems < 0) { + PyErr_BadInternalCall(); + return NULL; + } +#endif + const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); const size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type); _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op)); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index ee9b64107bb9f2..3af7863e80d2c6 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -6799,6 +6799,8 @@ INSTRUCTION_STATS(GET_ANEXT); _PyStackRef aiter; _PyStackRef awaitable; + _PyStackRef null; + _PyStackRef none; aiter = stack_pointer[-1]; _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); @@ -6808,8 +6810,12 @@ JUMP_TO_LABEL(error); } awaitable = PyStackRef_FromPyObjectSteal(awaitable_o); + null = PyStackRef_NULL; + none = PyStackRef_None; stack_pointer[0] = awaitable; - stack_pointer += 1; + stack_pointer[1] = null; + stack_pointer[2] = none; + stack_pointer += 3; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); DISPATCH(); } @@ -6824,6 +6830,8 @@ INSTRUCTION_STATS(GET_AWAITABLE); _PyStackRef iterable; _PyStackRef iter; + _PyStackRef null; + _PyStackRef none; iterable = stack_pointer[-1]; _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); @@ -6839,8 +6847,12 @@ JUMP_TO_LABEL(error); } iter = PyStackRef_FromPyObjectSteal(iter_o); + null = PyStackRef_NULL; + none = PyStackRef_None; stack_pointer[0] = iter; - stack_pointer += 1; + stack_pointer[1] = null; + stack_pointer[2] = none; + stack_pointer += 3; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); DISPATCH(); } @@ -10118,7 +10130,7 @@ assert(_PyOpcode_Deopt[opcode] == (LOAD_GLOBAL)); JUMP_TO_PREDICTED(LOAD_GLOBAL); } - #if Py_GIL_DISABLED + #ifdef Py_GIL_DISABLED int increfed = _Py_TryIncrefCompareStackRef(&entries[index].me_value, res_o, &res); if (!increfed) { UPDATE_MISS_STATS(LOAD_GLOBAL); @@ -10185,7 +10197,7 @@ assert(_PyOpcode_Deopt[opcode] == (LOAD_GLOBAL)); JUMP_TO_PREDICTED(LOAD_GLOBAL); } - #if Py_GIL_DISABLED + #ifdef Py_GIL_DISABLED int increfed = _Py_TryIncrefCompareStackRef(&entries[index].me_value, res_o, &res); if (!increfed) { UPDATE_MISS_STATS(LOAD_GLOBAL); diff --git a/Python/getopt.c b/Python/getopt.c index f6a3ef960a4021..c64d6e65c2740a 100644 --- a/Python/getopt.c +++ b/Python/getopt.c @@ -160,7 +160,7 @@ _PyOS_GetOpt(struct _PyOS_GetOpt *getopt) wchar_t *ptr = wcschr(SHORT_OPTS, option); if (ptr == NULL) { if (error) { - fprintf(stderr, "Invalid option '-%lc'\n", option); + fprintf(stderr, "Invalid option '-%lc'\n", (wint_t)option); } return '_'; } @@ -174,7 +174,7 @@ _PyOS_GetOpt(struct _PyOS_GetOpt *getopt) if (getopt->index >= argc) { if (error) { fprintf(stderr, - "Argument expected for the -%lc option\n", option); + "Argument expected for the -%lc option\n", (wint_t)option); } return '_'; } diff --git a/Python/import.c b/Python/import.c index 0f01815c71ed36..23ca226adb78b0 100644 --- a/Python/import.c +++ b/Python/import.c @@ -531,7 +531,7 @@ remove_module(PyThreadState *tstate, PyObject *name) Py_ssize_t _PyImport_GetNextModuleIndex(void) { - return _Py_atomic_add_ssize(&LAST_MODULE_INDEX, 1) + 1; + return _Py_atomic_add_ssize_relaxed(&LAST_MODULE_INDEX, 1) + 1; } #ifndef NDEBUG @@ -1300,50 +1300,68 @@ del_extensions_cache_value(void *raw) } } +/* The key of the extensions cache: the raw content of two strings. + + The UTF-8 encoding is not used, because the strings can contain lone + surrogates, e.g. a file name undecodable in the filesystem encoding. */ +struct hashtable_key { + size_t size; /* the total size of the key */ + unsigned char kind1; + unsigned char kind2; + Py_ssize_t len1; + /* followed by the raw content of both strings */ +}; + static void * -hashtable_key_from_2_strings(PyObject *str1, PyObject *str2, const char sep) +hashtable_key_from_2_strings(PyObject *str1, PyObject *str2) { - const char *str1_data = _PyUnicode_AsUTF8NoNUL(str1); - const char *str2_data = _PyUnicode_AsUTF8NoNUL(str2); - if (str1_data == NULL || str2_data == NULL) { - return NULL; - } - Py_ssize_t str1_len = strlen(str1_data); - Py_ssize_t str2_len = strlen(str2_data); + Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); + Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); + int kind1 = PyUnicode_KIND(str1); + int kind2 = PyUnicode_KIND(str2); + size_t size1 = (size_t)len1 * kind1; + size_t size2 = (size_t)len2 * kind2; - /* Make sure sep and the NULL byte won't cause an overflow. */ - assert(SIZE_MAX - str1_len - str2_len > 2); - size_t size = str1_len + 1 + str2_len + 1; + assert(SIZE_MAX - sizeof(struct hashtable_key) - size1 > size2); + size_t size = sizeof(struct hashtable_key) + size1 + size2; // XXX Use a buffer if it's a temp value (every case but "set"). - char *key = PyMem_RawMalloc(size); + struct hashtable_key *key = PyMem_RawMalloc(size); if (key == NULL) { PyErr_NoMemory(); return NULL; } - memcpy(key, str1_data, str1_len); - key[str1_len] = sep; - memcpy(key + str1_len + 1, str2_data, str2_len); - key[size - 1] = '\0'; - assert(strlen(key) == size - 1); + /* Clear the padding: the key is hashed and compared as raw bytes. */ + memset(key, 0, sizeof(struct hashtable_key)); + key->size = size; + key->kind1 = (unsigned char)kind1; + key->kind2 = (unsigned char)kind2; + key->len1 = len1; + char *data = (char *)(key + 1); + memcpy(data, PyUnicode_DATA(str1), size1); + memcpy(data + size1, PyUnicode_DATA(str2), size2); return key; } static Py_uhash_t -hashtable_hash_str(const void *key) +hashtable_hash_key(const void *key) { - return Py_HashBuffer(key, strlen((const char *)key)); + return Py_HashBuffer(key, ((const struct hashtable_key *)key)->size); } static int -hashtable_compare_str(const void *key1, const void *key2) +hashtable_compare_key(const void *key1, const void *key2) { - return strcmp((const char *)key1, (const char *)key2) == 0; + size_t size = ((const struct hashtable_key *)key1)->size; + if (size != ((const struct hashtable_key *)key2)->size) { + return 0; + } + return memcmp(key1, key2, size) == 0; } static void -hashtable_destroy_str(void *ptr) +hashtable_destroy_key(void *ptr) { PyMem_RawFree(ptr); } @@ -1383,16 +1401,15 @@ _find_cached_def(PyModuleDef *def) } #endif -#define HTSEP ':' static int _extensions_cache_init(void) { _Py_hashtable_allocator_t alloc = {PyMem_RawMalloc, PyMem_RawFree}; EXTENSIONS.hashtable = _Py_hashtable_new_full( - hashtable_hash_str, - hashtable_compare_str, - hashtable_destroy_str, // key + hashtable_hash_key, + hashtable_compare_key, + hashtable_destroy_key, // key del_extensions_cache_value, // value &alloc ); @@ -1410,7 +1427,7 @@ _extensions_cache_find_unlocked(PyObject *path, PyObject *name, if (EXTENSIONS.hashtable == NULL) { return NULL; } - void *key = hashtable_key_from_2_strings(path, name, HTSEP); + void *key = hashtable_key_from_2_strings(path, name); if (key == NULL) { return NULL; } @@ -1420,7 +1437,7 @@ _extensions_cache_find_unlocked(PyObject *path, PyObject *name, *p_key = key; } else { - hashtable_destroy_str(key); + hashtable_destroy_key(key); } return entry; } @@ -1558,7 +1575,7 @@ _extensions_cache_set(PyObject *path, PyObject *name, finally_oldvalue: extensions_lock_release(); if (key != NULL) { - hashtable_destroy_str(key); + hashtable_destroy_key(key); } return value; @@ -1602,7 +1619,6 @@ _extensions_cache_clear_all(void) EXTENSIONS.hashtable = NULL; } -#undef HTSEP static bool diff --git a/Python/importdl.c b/Python/importdl.c index dbcf5c98ceb69b..0f843988c0cfc2 100644 --- a/Python/importdl.c +++ b/Python/importdl.c @@ -9,6 +9,7 @@ #include "pycore_pyerrors.h" // _PyErr_FormatFromCause() #include "pycore_runtime.h" // _Py_ID() #include "pycore_global_objects.h" // _Py_SINGLETON() +#include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL() /***********************************/ @@ -134,7 +135,7 @@ _Py_ext_module_loader_info_init(struct _Py_ext_module_loader_info *p_info, goto error; } - info.newcontext = PyUnicode_AsUTF8(info.name); + info.newcontext = _PyUnicode_AsUTF8NoNUL(info.name); if (info.newcontext == NULL) { goto error; } @@ -151,6 +152,11 @@ _Py_ext_module_loader_info_init(struct _Py_ext_module_loader_info *p_info, "module filename must be a string"); goto error; } + if (PyUnicode_FindChar(info.filename, 0, 0, + PyUnicode_GET_LENGTH(info.filename), 1) != -1) { + PyErr_SetString(PyExc_ValueError, "embedded null character"); + goto error; + } #ifndef MS_WINDOWS info.filename_encoded = PyUnicode_EncodeFSDefault(info.filename); diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 3c9b2afcde100f..48daa91259d3c9 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2041,7 +2041,7 @@ check_tool(PyInterpreterState *interp, int tool_id) } /* We share the eval-breaker with flags, so the monitoring - * version goes in the top 24 bits */ + * version goes in the bits above _PY_EVAL_EVENTS_BITS */ #define MONITORING_VERSION_INCREMENT (1 << _PY_EVAL_EVENTS_BITS) int diff --git a/Python/lock.c b/Python/lock.c index a2d47eea64a006..59dfe772a0365f 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -25,7 +25,7 @@ static const PyTime_t TIME_TO_BE_FAIR_NS = 1000*1000; // Spin for a bit before parking the thread. This is only enabled for // `--disable-gil` builds because it is unlikely to be helpful if the GIL is // enabled. -#if Py_GIL_DISABLED +#ifdef Py_GIL_DISABLED static const int MAX_SPIN_COUNT = 40; #else static const int MAX_SPIN_COUNT = 0; diff --git a/Python/modsupport.c b/Python/modsupport.c index bab21d1b2be5b5..f944b1c05ba988 100644 --- a/Python/modsupport.c +++ b/Python/modsupport.c @@ -767,7 +767,7 @@ int PyABIInfo_Check(PyABIInfo *info, const char *module_name) /* Free-threading/GIL */ uint16_t gilflags = info->flags & (PyABIInfo_GIL | PyABIInfo_FREETHREADED); -#if Py_GIL_DISABLED +#ifdef Py_GIL_DISABLED if (gilflags == PyABIInfo_GIL) { return _abiinfo_raise(module_name, "incompatible with free-threaded CPython"); diff --git a/Python/mysnprintf.c b/Python/mysnprintf.c index 2a505d14f82c99..69ef70de14d375 100644 --- a/Python/mysnprintf.c +++ b/Python/mysnprintf.c @@ -2,13 +2,10 @@ /* snprintf() and vsnprintf() wrappers. - If the platform has vsnprintf, we use it, else we - emulate it in a half-hearted way. Even if the platform has it, we wrap - it because platforms differ in what vsnprintf does in case the buffer - is too small: C99 behavior is to return the number of characters that - would have been written had the buffer not been too small, and to set - the last byte of the buffer to \0. At least MS _vsnprintf returns a - negative value instead, and fills the entire buffer with non-\0 data. + We wrap vsnprintf() to guarantee consistent behavior in corner cases: + C99 behavior is to return the number of characters that would have been + written had the buffer not been too small, and to set the last byte of the + buffer to \0. Unlike C99, our wrappers do not support passing a null buffer. The wrappers ensure that str[size-1] is always \0 upon return. @@ -28,10 +25,7 @@ When rv < 0, "something bad happened". str[size-1] is \0 in this case too, but the rest of str is unreliable. It could be that - an error in format codes was detected by libc, or on platforms - with a non-C99 vsnprintf simply that the buffer wasn't big enough - to avoid truncation, or on platforms without any vsnprintf that - PyMem_Malloc couldn't obtain space for a temp buffer. + an error in format codes was detected by libc. CAUTION: Unlike C99, str != NULL and size > 0 are required. Also, size must be smaller than INT_MAX. @@ -66,11 +60,7 @@ PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va) goto Done; } -#if defined(_MSC_VER) - len = _vsnprintf(str, size, format, va); -#else len = vsnprintf(str, size, format, va); -#endif Done: if (size > 0) { diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 6284885c428832..6648b72e66e281 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -2763,13 +2763,21 @@ dummy_func(void) { } } - op(_GET_ANEXT, (aiter -- aiter, awaitable)) { + op(_GET_ANEXT, (aiter -- aiter, awaitable, null, none)) { if (sym_matches_type(aiter, &PyAsyncGen_Type)) { awaitable = sym_new_type(ctx, &_PyAsyncGenASend_Type); } else { awaitable = sym_new_not_null(ctx); } + null = PyJitRef_Borrow(sym_new_null(ctx)); + none = PyJitRef_Borrow(sym_new_const(ctx, Py_None)); + } + + op(_GET_AWAITABLE, (iterable -- iter, null, none)) { + iter = sym_new_not_null(ctx); + null = PyJitRef_Borrow(sym_new_null(ctx)); + none = PyJitRef_Borrow(sym_new_const(ctx, Py_None)); } // END BYTECODES // diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 9cd20c495db441..c86121fcfc0e79 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1872,6 +1872,8 @@ case _GET_ANEXT: { JitOptRef aiter; JitOptRef awaitable; + JitOptRef null; + JitOptRef none; aiter = stack_pointer[-1]; if (sym_matches_type(aiter, &PyAsyncGen_Type)) { awaitable = sym_new_type(ctx, &_PyAsyncGenASend_Type); @@ -1879,17 +1881,30 @@ else { awaitable = sym_new_not_null(ctx); } - CHECK_STACK_BOUNDS(1); + null = PyJitRef_Borrow(sym_new_null(ctx)); + none = PyJitRef_Borrow(sym_new_const(ctx, Py_None)); + CHECK_STACK_BOUNDS(3); stack_pointer[0] = awaitable; - stack_pointer += 1; + stack_pointer[1] = null; + stack_pointer[2] = none; + stack_pointer += 3; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); break; } case _GET_AWAITABLE: { JitOptRef iter; + JitOptRef null; + JitOptRef none; iter = sym_new_not_null(ctx); + null = PyJitRef_Borrow(sym_new_null(ctx)); + none = PyJitRef_Borrow(sym_new_const(ctx, Py_None)); + CHECK_STACK_BOUNDS(2); stack_pointer[-1] = iter; + stack_pointer[0] = null; + stack_pointer[1] = none; + stack_pointer += 2; + ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); break; } diff --git a/Python/pathconfig.c b/Python/pathconfig.c index b4ba1c52066879..89684d9da8e629 100644 --- a/Python/pathconfig.c +++ b/Python/pathconfig.c @@ -340,7 +340,7 @@ Py_GetProgramName(void) /* Compute module search path from argv[0] or the current working - directory ("-m module" case) which will be prepended to sys.argv: + directory ("-m module" case) which will be prepended to sys.path: sys.path[0]. Return 1 if the path is correctly resolved and written into *path0_p. diff --git a/Python/perf_jit_trampoline.c b/Python/perf_jit_trampoline.c index 07aa8e769bf2d7..da6a7e69633db5 100644 --- a/Python/perf_jit_trampoline.c +++ b/Python/perf_jit_trampoline.c @@ -672,7 +672,7 @@ static void perf_map_jit_write_entry_with_name( uint64_t thread_id = 0; pthread_threadid_np(NULL, &thread_id); ev.thread_id = (uint32_t)thread_id; -#elif defined(HAVE_GETTID) +#elif defined(_Py_HAVE_GETTID) ev.thread_id = gettid(); #else ev.thread_id = syscall(SYS_gettid); // Get thread ID via system call diff --git a/Python/pystate.c b/Python/pystate.c index 81360f4999849d..d253f4dd89310d 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -309,9 +309,25 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS static const _PyRuntimeState initial = _PyRuntimeState_INIT(_PyRuntime, ""); _Py_COMP_DIAG_POP +/* Remote profilers access interpreter and thread states without synchronizing + with their lifetime. Until the runtime is finalized, keep deleted states in + per-type free lists, so that a stale access only reaches a state of the same + type. */ +typedef struct state_header { + struct state_header *next; // Next entry in the free list + void *mem; // Block to free at finalization +} state_header; + +static struct { + PyMutex mutex; + state_header *tstates; + state_header *interpreters; +} state_freelists; + #define LOCKS_INIT(runtime) \ { \ &(runtime)->interpreters.mutex, \ + &state_freelists.mutex, \ &(runtime)->xi.data_lookup.registry.mutex, \ &(runtime)->unicode_state.ids.mutex, \ &(runtime)->imports.extensions.mutex, \ @@ -390,6 +406,19 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime) assert(runtime->object_state.interpreter_leaks == 0); #endif gilstate_clear(); + + state_header **lists[] = {&state_freelists.tstates, &state_freelists.interpreters}; + for (size_t i = 0; i < Py_ARRAY_LENGTH(lists); i++) { + PyMutex_LockFlags(&state_freelists.mutex, _Py_LOCK_DONT_DETACH); + state_header *entry = *lists[i]; + *lists[i] = NULL; + PyMutex_Unlock(&state_freelists.mutex); + while (entry != NULL) { + state_header *next = entry->next; + PyMem_RawFree(entry->mem); + entry = next; + } + } } #ifdef HAVE_FORK @@ -445,24 +474,50 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime) return _PyStatus_OK(); } -static PyInterpreterState * -alloc_interpreter(void) +static void * +alloc_state(state_header **freelist, size_t size, size_t alignment) { - // Aligned allocation for PyInterpreterState. - // the first word of the memory block is used to store - // the original pointer to be used later to free the memory. - size_t alignment = _Alignof(PyInterpreterState); - size_t allocsize = sizeof(PyInterpreterState) + sizeof(void *) + alignment - 1; + PyMutex_LockFlags(&state_freelists.mutex, _Py_LOCK_DONT_DETACH); + state_header *entry = *freelist; + if (entry != NULL) { + *freelist = entry->next; + } + PyMutex_Unlock(&state_freelists.mutex); + if (entry != NULL) { + return entry + 1; + } + + size_t allocsize = size + sizeof(state_header) + alignment - 1; void *mem = PyMem_RawCalloc(1, allocsize); if (mem == NULL) { return NULL; } - void *ptr = _Py_ALIGN_UP((char *)mem + sizeof(void *), alignment); - ((void **)ptr)[-1] = mem; + void *ptr = _Py_ALIGN_UP((char *)mem + sizeof(state_header), alignment); + ((state_header *)ptr - 1)->mem = mem; assert(_Py_IS_ALIGNED(ptr, alignment)); return ptr; } +static void +recycle_state(state_header **freelist, void *state, size_t size) +{ + memset(state, 0, size); + state_header *entry = (state_header *)state - 1; + + PyMutex_LockFlags(&state_freelists.mutex, _Py_LOCK_DONT_DETACH); + entry->next = *freelist; + // Keep the list consistent for a child forked by another thread. + _Py_atomic_store_ptr_release(freelist, entry); + PyMutex_Unlock(&state_freelists.mutex); +} + +static PyInterpreterState * +alloc_interpreter(void) +{ + return alloc_state(&state_freelists.interpreters, sizeof(PyInterpreterState), + _Alignof(PyInterpreterState)); +} + static void free_interpreter(PyInterpreterState *interp) { @@ -491,7 +546,13 @@ free_interpreter(PyInterpreterState *interp) interp->obmalloc = NULL; } assert(_Py_IS_ALIGNED(interp, _Alignof(PyInterpreterState))); - PyMem_RawFree(((void **)interp)[-1]); + // A daemon thread may still own the embedded initial thread state. + if (interp->_initialized + && _Py_atomic_load_ptr(&interp->threads.preallocated) == NULL) + { + return; + } + recycle_state(&state_freelists.interpreters, interp, sizeof(*interp)); } } @@ -1517,7 +1578,8 @@ alloc_threadstate(PyInterpreterState *interp) // Fall back to the allocator. if (tstate == NULL) { - tstate = PyMem_RawCalloc(1, sizeof(_PyThreadStateImpl)); + tstate = alloc_state(&state_freelists.tstates, sizeof(*tstate), + _Alignof(_PyThreadStateImpl)); if (tstate == NULL) { return NULL; } @@ -1544,7 +1606,7 @@ free_threadstate(_PyThreadStateImpl *tstate) _Py_atomic_store_ptr(&interp->threads.preallocated, tstate); } else { - PyMem_RawFree(tstate); + recycle_state(&state_freelists.tstates, tstate, sizeof(*tstate)); } } @@ -1893,8 +1955,8 @@ PyThreadState_Clear(PyThreadState *tstate) // Flush the thread's local GC allocation count to the global count // before the thread state is cleared, otherwise the count is lost. _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate; - _Py_atomic_add_int(&tstate->interp->gc.young.count, - (int)tstate_impl->gc.alloc_count); + _Py_atomic_add_int_relaxed(&tstate->interp->gc.young.count, + (int)tstate_impl->gc.alloc_count); tstate_impl->gc.alloc_count = 0; // Merge our thread-local refcounts into the type's own refcount and @@ -2232,7 +2294,7 @@ tstate_deactivate(PyThreadState *tstate) assert(tstate_is_bound(tstate)); assert(tstate->_status.active); -#if Py_STATS +#ifdef Py_STATS _PyStats_Detach((_PyThreadStateImpl *)tstate); #endif diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 5e026a93d5c180..38003e60e985ca 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1157,6 +1157,13 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) } } + // gh-161863: Interpreter can be not fully initialized + // when calling this function. May be not safe to call the import. + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (interp->imports.importlib == NULL) { + goto fallback; + } + // Try first with the stdlib traceback module PyObject *print_exception_fn = PyImport_ImportModuleAttrString( "traceback", diff --git a/Python/remote_debug.h b/Python/remote_debug.h index a4a3a010926a7b..0c55fd3a0354e3 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -60,19 +60,13 @@ extern "C" { #endif #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX -# include -# include +# include # include -# include # include # include # include -# include # include # include -# include -# include -# include #endif #ifdef MS_WINDOWS @@ -161,8 +155,6 @@ get_page_size(void) { typedef struct page_cache_entry { uintptr_t page_addr; // page-aligned base address char *data; - int valid; - struct page_cache_entry *next; } page_cache_entry_t; #define MAX_PAGES 1024 @@ -201,7 +193,7 @@ _Py_RemoteDebug_ValidatePyRuntimeCookie(proc_handle_t *handle, uintptr_t address } char buf[sizeof(_Py_Debug_Cookie) - 1]; if (_Py_RemoteDebug_ReadRemoteMemory(handle, address, sizeof(buf), buf) != 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { + if (!_Py_RemoteDebug_IsFatalReadError()) { PyErr_Clear(); } return 0; @@ -217,7 +209,6 @@ _Py_RemoteDebug_FreePageCache(proc_handle_t *handle) PyMem_RawFree(handle->pages[i].data); } handle->pages[i].data = NULL; - handle->pages[i].valid = 0; } handle->page_cache_count = 0; } @@ -225,9 +216,6 @@ _Py_RemoteDebug_FreePageCache(proc_handle_t *handle) UNUSED static void _Py_RemoteDebug_ClearCache(proc_handle_t *handle) { - for (int i = 0; i < handle->page_cache_count; i++) { - handle->pages[i].valid = 0; - } handle->page_cache_count = 0; } @@ -250,7 +238,6 @@ _Py_RemoteDebug_InitProcHandle(proc_handle_t *handle, pid_t pid) { handle->page_cache_count = 0; for (int i = 0; i < MAX_PAGES; i++) { handle->pages[i].data = NULL; - handle->pages[i].valid = 0; } handle->pid = pid; @@ -282,6 +269,11 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) { CloseHandle(handle->hProcess); handle->hProcess = NULL; } +#elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX + if (handle->task != MACH_PORT_NULL) { + mach_port_deallocate(mach_task_self(), handle->task); + handle->task = MACH_PORT_NULL; + } #elif defined(__linux__) if (handle->memfd != -1) { close(handle->memfd); @@ -294,237 +286,134 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) { #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX -static uintptr_t -return_section_address64( - const char* section, - uintptr_t base, - void* map -) { - struct mach_header_64* hdr = (struct mach_header_64*)map; - int ncmds = hdr->ncmds; - - int cmd_cnt = 0; - struct segment_command_64* cmd = map + sizeof(struct mach_header_64); - - uintptr_t vmaddr = 0; - - for (int i = 0; cmd_cnt < 2 && i < ncmds; i++) { - if (cmd->cmd == LC_SEGMENT_64 && strcmp(cmd->segname, "__TEXT") == 0) { - vmaddr = cmd->vmaddr; - } - if (cmd->cmd == LC_SEGMENT_64 && strcmp(cmd->segname, "__DATA") == 0) { - // The section address only needs the image's ASLR slide. - // VM regions need not match the segment's on-disk size. - - int nsects = cmd->nsects; - struct section_64* sec = (struct section_64*)( - (void*)cmd + sizeof(struct segment_command_64) - ); - for (int j = 0; j < nsects; j++) { - if (strcmp(sec[j].sectname, section) == 0) { - return base + sec[j].addr - vmaddr; - } - } - cmd_cnt++; - } - - cmd = (struct segment_command_64*)((void*)cmd + cmd->cmdsize); - } +enum { + MACHO_NAME_SIZE = 16, + MACHO_MAX_LOAD_COMMAND_BYTES = 16 * 1024 * 1024, + DYLD_MAX_IMAGE_COUNT = 65536, +}; - return 0; -} +/* These 64-bit little-endian layouts work on both arm64 and x86_64 (Rosetta). */ +static_assert(sizeof(uintptr_t) == sizeof(uint64_t), "64-bit macOS required"); static uintptr_t -return_section_address32( - const char* section, - uintptr_t base, - void* map -) { - struct mach_header* hdr = (struct mach_header*)map; - int ncmds = hdr->ncmds; - - int cmd_cnt = 0; - struct segment_command* cmd = map + sizeof(struct mach_header); - - uintptr_t vmaddr = 0; - - for (int i = 0; cmd_cnt < 2 && i < ncmds; i++) { - if (cmd->cmd == LC_SEGMENT && strcmp(cmd->segname, "__TEXT") == 0) { - vmaddr = cmd->vmaddr; - } - if (cmd->cmd == LC_SEGMENT && strcmp(cmd->segname, "__DATA") == 0) { - // The section address only needs the image's ASLR slide. - // VM regions need not match the segment's on-disk size. - - int nsects = cmd->nsects; - struct section* sec = (struct section*)( - (void*)cmd + sizeof(struct segment_command) - ); - for (int j = 0; j < nsects; j++) { - if (strcmp(sec[j].sectname, section) == 0) { - return base + sec[j].addr - vmaddr; - } - } - cmd_cnt++; - } - - cmd = (struct segment_command*)((void*)cmd + cmd->cmdsize); - } - - return 0; -} - -static uintptr_t -return_section_address_fat( - const char* section, - uintptr_t base, - void* map -) { - struct fat_header* fat_hdr = (struct fat_header*)map; - - // Determine host CPU type for architecture selection - cpu_type_t cpu; - int is_abi64; - size_t cpu_size = sizeof(cpu), abi64_size = sizeof(is_abi64); - - if (sysctlbyname("hw.cputype", &cpu, &cpu_size, NULL, 0) != 0) { - int err = errno; - _set_debug_oserror_from_errno(err, - "Failed to determine CPU type via sysctlbyname " - "for fat binary analysis at 0x%lx: %s", - base, strerror(err)); +macho_section_address(const void *image, size_t image_size, + uintptr_t base, const char *name) +{ + struct mach_header_64 header; + if (image_size < sizeof(header)) { return 0; } - if (sysctlbyname("hw.cpu64bit_capable", &is_abi64, &abi64_size, NULL, 0) != 0) { - int err = errno; - _set_debug_oserror_from_errno(err, - "Failed to determine CPU ABI capability via sysctlbyname " - "for fat binary analysis at 0x%lx: %s", - base, strerror(err)); + memcpy(&header, image, sizeof(header)); + if (header.magic != MH_MAGIC_64 || + header.sizeofcmds > image_size - sizeof(header)) + { return 0; } - cpu |= is_abi64 * CPU_ARCH_ABI64; - - // Check endianness - int swap = fat_hdr->magic == FAT_CIGAM; - struct fat_arch* arch = (struct fat_arch*)(map + sizeof(struct fat_header)); - - // Get number of architectures in fat binary - uint32_t nfat_arch = swap ? __builtin_bswap32(fat_hdr->nfat_arch) : fat_hdr->nfat_arch; - - // Search for matching architecture - for (uint32_t i = 0; i < nfat_arch; i++) { - cpu_type_t arch_cpu = swap ? __builtin_bswap32(arch[i].cputype) : arch[i].cputype; - - if (arch_cpu == cpu) { - // Found matching architecture, now process it - uint32_t offset = swap ? __builtin_bswap32(arch[i].offset) : arch[i].offset; - struct mach_header_64* hdr = (struct mach_header_64*)(map + offset); - - // Determine which type of Mach-O it is and process accordingly - switch (hdr->magic) { - case MH_MAGIC: - case MH_CIGAM: - return return_section_address32(section, base, (void*)hdr); - - case MH_MAGIC_64: - case MH_CIGAM_64: - return return_section_address64(section, base, (void*)hdr); - - default: - PyErr_Format(PyExc_RuntimeError, - "Unknown Mach-O magic number 0x%x in fat binary architecture %u at base 0x%lx", - hdr->magic, i, base); - return 0; + const unsigned char *cursor = (const unsigned char *)image + sizeof(header); + size_t remaining = header.sizeofcmds; + uint64_t header_address = 0, section_address = 0; + int have_header = 0, have_section = 0; + for (uint32_t index = 0; index < header.ncmds; index++) { + struct load_command command; + if (remaining < sizeof(command)) { + return 0; + } + memcpy(&command, cursor, sizeof(command)); + size_t command_size = command.cmdsize; + if (command_size < sizeof(command) || command_size > remaining) { + return 0; + } + if (command.cmd == LC_SEGMENT_64) { + struct segment_command_64 segment; + if (command_size < sizeof(segment)) { + return 0; + } + memcpy(&segment, cursor, sizeof(segment)); + if (segment.nsects > + (command_size - sizeof(segment)) / sizeof(struct section_64)) + { + return 0; + } + /* __PAGEZERO has no file contents and does not define the slide. */ + if (segment.fileoff == 0 && segment.filesize != 0) { + header_address = segment.vmaddr; + have_header = 1; + } + if (strncmp(segment.segname, "__DATA", MACHO_NAME_SIZE) == 0) { + const unsigned char *record = cursor + sizeof(segment); + for (uint32_t item = 0; item < segment.nsects; + item++, record += sizeof(struct section_64)) + { + struct section_64 section; + memcpy(§ion, record, sizeof(section)); + if (strncmp(section.sectname, name, MACHO_NAME_SIZE) == 0) { + section_address = section.addr; + have_section = 1; + } + } } } + cursor += command_size; + remaining -= command_size; } - - PyErr_Format(PyExc_RuntimeError, - "No matching architecture found for CPU type 0x%x " - "in fat binary at base 0x%lx (%u architectures examined)", - cpu, base, nfat_arch); - return 0; + if (!have_header || !have_section || section_address < header_address || + section_address - header_address > UINTPTR_MAX - base) + { + return 0; + } + return base + (uintptr_t)(section_address - header_address); } static uintptr_t -search_section_in_file(const char* secname, char* path, uintptr_t base) +search_section_in_memory(proc_handle_t *handle, uintptr_t base, + const char *section) { - int fd = open(path, O_RDONLY); - if (fd == -1) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Cannot open binary file '%s' for section '%s' search: %s", - path, secname, strerror(err)); - return 0; + /* Most images fit in one read. Stay within the smallest macOS page size. */ + unsigned char prefix[4096]; + size_t prefix_size = sizeof(prefix) - base % sizeof(prefix); + struct mach_header_64 header; + if (prefix_size < sizeof(header) || + _Py_RemoteDebug_ReadRemoteMemory(handle, base, prefix_size, prefix) < 0) + { + if (_Py_RemoteDebug_IsFatalReadError()) { + return 0; + } + PyErr_Clear(); + prefix_size = sizeof(header); + if (_Py_RemoteDebug_ReadRemoteMemory(handle, base, prefix_size, prefix) < 0) { + return 0; + } } - - struct stat fs; - if (fstat(fd, &fs) == -1) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Cannot get file size for binary '%s' during section '%s' search: %s", - path, secname, strerror(err)); - close(fd); + memcpy(&header, prefix, sizeof(header)); + if (header.magic != MH_MAGIC_64 || + header.sizeofcmds > MACHO_MAX_LOAD_COMMAND_BYTES) + { return 0; } - - void* map = mmap(0, fs.st_size, PROT_READ, MAP_SHARED, fd, 0); - if (map == MAP_FAILED) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Cannot memory map binary file '%s' (size: %lld bytes) for section '%s' search: %s", - path, (long long)fs.st_size, secname, strerror(err)); - close(fd); + size_t size = sizeof(header) + header.sizeofcmds; + if (base > UINTPTR_MAX - size) { return 0; } - - uintptr_t result = 0; - uint32_t magic = *(uint32_t*)map; - - switch (magic) { - case MH_MAGIC: - case MH_CIGAM: - result = return_section_address32(secname, base, map); - break; - case MH_MAGIC_64: - case MH_CIGAM_64: - result = return_section_address64(secname, base, map); - break; - case FAT_MAGIC: - case FAT_CIGAM: - result = return_section_address_fat(secname, base, map); - break; - default: - PyErr_Format(PyExc_RuntimeError, - "Unrecognized Mach-O magic number 0x%x in binary file '%s' for section '%s' search", - magic, path, secname); - break; + if (size <= prefix_size) { + return macho_section_address(prefix, size, base, section); } - - if (munmap(map, fs.st_size) != 0) { - if (!PyErr_Occurred()) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Failed to unmap binary file '%s' (size: %lld bytes): %s", - path, (long long)fs.st_size, strerror(err)); - } - result = 0; + unsigned char *image = PyMem_Malloc(size); + if (image == NULL) { + PyErr_NoMemory(); + return 0; } - if (close(fd) != 0) { - if (!PyErr_Occurred()) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Failed to close binary file '%s': %s", - path, strerror(err)); - } - result = 0; + memcpy(image, prefix, prefix_size); + uintptr_t result = 0; + if (_Py_RemoteDebug_ReadRemoteMemory(handle, base + prefix_size, + size - prefix_size, image + prefix_size) == 0) + { + result = macho_section_address(image, size, base, section); } + PyMem_Free(image); return result; } - static mach_port_t pid_to_task(pid_t pid) { @@ -542,86 +431,166 @@ pid_to_task(pid_t pid) return task; } -static uintptr_t -search_map_for_section(proc_handle_t *handle, const char* secname, const char* substr, - section_validator_t validator) { - mach_vm_address_t address = 0; - mach_vm_size_t size = 0; - mach_msg_type_number_t count = sizeof(vm_region_basic_info_data_64_t); - vm_region_basic_info_data_64_t region_info; - mach_port_t object_name; - - mach_port_t proc_ref = pid_to_task(handle->pid); - if (proc_ref == 0) { - if (!PyErr_Occurred()) { - PyErr_Format(PyExc_PermissionError, - "Cannot get task port for PID %d during section search", - handle->pid); - } - return 0; +static int +read_dyld_image_metadata(proc_handle_t *handle, struct dyld_all_image_infos *images) +{ + task_dyld_info_data_t info = {0}; + mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; + kern_return_t status = task_info(handle->task, TASK_DYLD_INFO, + (task_info_t)&info, &count); + if (status != KERN_SUCCESS) { + PyErr_Format(status == KERN_PROTECTION_FAILURE + ? PyExc_PermissionError : PyExc_RuntimeError, + "Cannot read dyld information for PID %d (Mach error %d)", + handle->pid, status); + return -1; } - - char map_filename[MAXPATHLEN + 1]; - - kern_return_t kr; - while ((kr = mach_vm_region( - proc_ref, - &address, - &size, - VM_REGION_BASIC_INFO_64, - (vm_region_info_t)®ion_info, - &count, - &object_name)) == KERN_SUCCESS) + if (count < TASK_DYLD_INFO_COUNT || + info.all_image_info_format != TASK_DYLD_ALL_IMAGE_INFO_64) + { + PyErr_SetString(PyExc_RuntimeError, "Unsupported dyld image-info format"); + return -1; + } + size_t uuid_end = offsetof(struct dyld_all_image_infos, uuidArray) + + sizeof(images->uuidArray); + size_t size = (size_t)Py_MIN(info.all_image_info_size, uuid_end); + if (size < offsetof(struct dyld_all_image_infos, infoArray) + + sizeof(images->infoArray) || + info.all_image_info_addr > UINTPTR_MAX - size) { + PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image information"); + return -1; + } + if (_Py_RemoteDebug_ReadRemoteMemory(handle, info.all_image_info_addr, + size, images) < 0) + { + return -1; + } + /* The UUID array was added in version 8 of dyld_all_image_infos. */ + if (images->version < 8 || size < uuid_end) { + images->uuidArray = NULL; + images->uuidArrayCount = 0; + } + return 0; +} - if ((region_info.protection & VM_PROT_READ) == 0 - || (region_info.protection & VM_PROT_EXECUTE) == 0) { - address += size; - continue; +static uintptr_t +search_memory_regions_for_section(proc_handle_t *handle, const char *section, + section_validator_t validator) +{ + mach_vm_address_t address = 0; + while (1) { + mach_vm_size_t size = 0; + vm_region_basic_info_data_64_t region; + mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64; + mach_port_t object = MACH_PORT_NULL; + kern_return_t status = mach_vm_region( + handle->task, &address, &size, VM_REGION_BASIC_INFO_64, + (vm_region_info_t)®ion, &count, &object); + if (object != MACH_PORT_NULL) { + mach_port_deallocate(mach_task_self(), object); } - - int path_len = proc_regionfilename( - handle->pid, address, map_filename, MAXPATHLEN); - if (path_len == 0) { - address += size; - continue; + if (status == KERN_INVALID_ADDRESS) { + return 0; } - - char* filename = strrchr(map_filename, '/'); - if (filename != NULL) { - filename++; // Move past the '/' - } else { - filename = map_filename; // No path, use the whole string + if (status != KERN_SUCCESS) { + PyErr_Format(status == KERN_PROTECTION_FAILURE + ? PyExc_PermissionError : PyExc_RuntimeError, + "Cannot read memory regions for PID %d (Mach error %d)", + handle->pid, status); + return 0; } - - if (strncmp(filename, substr, strlen(substr)) == 0) { - PyErr_Clear(); - uintptr_t result = search_section_in_file( - secname, map_filename, address); - if (result != 0) { - if (validator == NULL || validator(handle, result)) { - return result; - } - if (_Py_RemoteDebug_HasPermissionError()) { - return 0; - } + vm_prot_t required = VM_PROT_READ | VM_PROT_EXECUTE; + if ((region.protection & required) == required) { + uintptr_t candidate = search_section_in_memory(handle, address, section); + if (candidate != 0 && (validator == NULL || validator(handle, candidate))) { + return candidate; } - else if (_Py_RemoteDebug_HasPermissionError()) { + if (_Py_RemoteDebug_IsFatalReadError()) { return 0; } + PyErr_Clear(); + } + if (size == 0 || size > UINT64_MAX - address) { + return 0; } - address += size; } +} - if (kr != KERN_INVALID_ADDRESS && !PyErr_Occurred()) { - PyErr_Format(PyExc_RuntimeError, - "mach_vm_region failed while searching PID %d for section '%s' " - "(kern_return_t: %d)", - handle->pid, secname, kr); +static uintptr_t +search_dyld_image_array(proc_handle_t *handle, uintptr_t array_address, + size_t count, size_t record_size, + const char *section, section_validator_t validator) +{ + if (array_address == 0 || count == 0 || count > DYLD_MAX_IMAGE_COUNT || + array_address > UINTPTR_MAX - count * record_size) + { + return 0; + } + size_t size = count * record_size; + unsigned char *images = PyMem_Malloc(size); + if (images == NULL) { + PyErr_NoMemory(); + return 0; + } + uintptr_t result = 0; + if (_Py_RemoteDebug_ReadRemoteMemory(handle, array_address, size, images) < 0) { + goto done; + } + for (size_t index = 0; index < count; index++) { + /* Both dyld_image_info and dyld_uuid_info begin with imageLoadAddress. */ + uintptr_t address; + memcpy(&address, images + index * record_size, sizeof(address)); + if (address == 0) { + continue; + } + uintptr_t candidate = search_section_in_memory(handle, address, section); + if (candidate != 0 && (validator == NULL || validator(handle, candidate))) { + result = candidate; + break; + } + if (_Py_RemoteDebug_IsFatalReadError()) { + break; + } + /* A library can unload after the image list is read. */ + PyErr_Clear(); } +done: + PyMem_Free(images); + if (!_Py_RemoteDebug_IsFatalReadError()) { + PyErr_Clear(); + } + return result; +} - return 0; +static uintptr_t +search_loaded_images_for_section(proc_handle_t *handle, const char *section, + section_validator_t validator) +{ + struct dyld_all_image_infos images = {0}; + if (read_dyld_image_metadata(handle, &images) == 0) { + /* Python is normally outside the shared cache: try the shorter list. */ + uintptr_t result = search_dyld_image_array( + handle, (uintptr_t)images.uuidArray, images.uuidArrayCount, + sizeof(struct dyld_uuid_info), section, validator); + if (result != 0 || _Py_RemoteDebug_IsFatalReadError()) { + return result; + } + result = search_dyld_image_array( + handle, (uintptr_t)images.infoArray, images.infoArrayCount, + sizeof(struct dyld_image_info), section, validator); + if (result != 0 || _Py_RemoteDebug_IsFatalReadError()) { + return result; + } + } + if (_Py_RemoteDebug_IsFatalReadError()) { + return 0; + } + /* dyld can leave both lists unavailable while a stopped target is loading + a library. Treat copied lists as hints and fall back to mapped images. */ + PyErr_Clear(); + return search_memory_regions_for_section(handle, section, validator); } #endif // (__APPLE__ && defined(TARGET_OS_OSX) && TARGET_OS_OSX) @@ -1173,22 +1142,14 @@ _Py_RemoteDebug_GetPyRuntimeAddress(proc_handle_t* handle) } } #elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX - // On macOS, try libpython first, then fall back to python - const char* candidates[] = {"libpython", "python", "Python", NULL}; - for (const char** candidate = candidates; *candidate; candidate++) { - PyErr_Clear(); - address = search_map_for_section(handle, "PyRuntime", *candidate, - _Py_RemoteDebug_ValidatePyRuntimeCookie); - if (address != 0 || _Py_RemoteDebug_HasPermissionError()) { - break; - } - } + address = search_loaded_images_for_section( + handle, "PyRuntime", _Py_RemoteDebug_ValidatePyRuntimeCookie); if (address == 0) { if (!_Py_RemoteDebug_HasPermissionError()) { PyObject *exc = PyErr_GetRaisedException(); PyErr_Format(PyExc_RuntimeError, "Failed to find the PyRuntime section in process %d " - "on macOS platform (tried both libpython and python)", + "on macOS platform", handle->pid); _PyErr_ChainExceptions1(exc); } @@ -1571,7 +1532,7 @@ _Py_RemoteDebug_PagedReadRemoteMemory(proc_handle_t *handle, // between profiler samples, so entries are packed at the front. for (int i = 0; i < handle->page_cache_count; i++) { page_cache_entry_t *entry = &handle->pages[i]; - if (entry->valid && entry->page_addr == page_base) { + if (entry->page_addr == page_base) { memcpy(out, entry->data + offset_in_page, size); return 0; } @@ -1598,7 +1559,6 @@ _Py_RemoteDebug_PagedReadRemoteMemory(proc_handle_t *handle, } entry->page_addr = page_base; - entry->valid = 1; handle->page_cache_count++; memcpy(out, entry->data + offset_in_page, size); return 0; @@ -1665,7 +1625,6 @@ _Py_RemoteDebug_ReadDebugOffsets( "Failed to locate PyRuntime address for PID %d", handle->pid); } - _set_debug_exception_cause(PyExc_RuntimeError, "PyRuntime address lookup failed during debug offsets initialization"); return -1; } size_t size = sizeof(struct _Py_DebugOffsets); diff --git a/Python/specialize.c b/Python/specialize.c index 067bae59f6073b..b5cf4ebc31142a 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -30,7 +30,7 @@ * InternalDocs/interpreter.md `Specialization` section. */ -#if Py_STATS +#ifdef Py_STATS #define SPECIALIZATION_FAIL(opcode, kind) \ do { \ PyStats *s = _PyStats_GET(); \ @@ -42,7 +42,7 @@ do { \ } while (0) #else # define SPECIALIZATION_FAIL(opcode, kind) ((void)0) -#endif // Py_STATS +#endif // !Py_STATS static void fixup_getiter(_Py_CODEUNIT *instruction, int flags) diff --git a/Python/symtable.c b/Python/symtable.c index 7f945dad1984ed..429c204dba29b9 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -753,7 +753,9 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags, // namespace and the globals (not the namespace providing the bound). // Similarly, if the name is explicitly global in the class namespace (through the // global statement), we want to also treat it as a global in this scope. - if (class_entry != NULL) { + // __classdict__ is the exception: these scopes load it from the cell that + // the class creates for them, whatever the class body does with that name. + if (class_entry != NULL && !_PyUnicode_Equal(name, &_Py_ID(__classdict__))) { long class_flags = _PyST_GetSymbol(class_entry, name); if (class_flags < 0) { return 0; diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 6f336dac058379..f8c72b096eb452 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -448,7 +448,7 @@ _PySys_ClearAuditHooks(PyThreadState *ts) runtime->audit_hooks.head = NULL; while (e) { n = e->next; - PyMem_RawFree(e); + free(e); e = n; } } @@ -496,7 +496,11 @@ PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData) } } - _Py_AuditHookEntry *e = (_Py_AuditHookEntry*)PyMem_RawMalloc( + /* Use malloc(), not PyMem_RawMalloc(): a hook can be added before + Py_Initialize(), and the entry is freed during finalization, after + PYTHONMALLOC may have changed the raw allocator (and, in debug builds, + the allocator under the debug hooks). */ + _Py_AuditHookEntry *e = (_Py_AuditHookEntry*)malloc( sizeof(_Py_AuditHookEntry)); if (!e) { if (tstate != NULL) { @@ -4157,6 +4161,8 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict) SET_SYS("path_importer_cache", PyDict_New()); SET_SYS("path_hooks", PyList_New(0)); + SET_SYS("_path0", Py_None); + if (_PyErr_Occurred(tstate)) { goto err_occurred; } diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index e660bca8406860..c7f245acddb290 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -18,7 +18,7 @@ #include #include /* pause(), also getthrid() on OpenBSD */ -#ifdef HAVE_GETTID +#ifdef _Py_HAVE_GETTID # include // gettid() #elif defined(__linux__) # include /* syscall(SYS_gettid) */ @@ -384,7 +384,7 @@ PyThread_get_thread_native_id(void) #ifdef __APPLE__ uint64_t native_id; (void) pthread_threadid_np(NULL, &native_id); -#elif defined(HAVE_GETTID) +#elif defined(_Py_HAVE_GETTID) pid_t native_id; native_id = gettid(); #elif defined(__linux__) diff --git a/Python/traceback.c b/Python/traceback.c index 7dc72d48274567..7e99dcb5a99249 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -4,6 +4,7 @@ #include "Python.h" #include "pycore_call.h" // _PyObject_CallMethod() #include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH +#include "pycore_freelist.h" // _Py_FREELIST_POP() #include "pycore_frame.h" // PyFrameObject #include "pycore_interp.h" // PyInterpreterState.gc #include "pycore_interpframe.h" // _PyFrame_GetCode() @@ -79,15 +80,16 @@ tb_create_raw(PyTracebackObject *next, PyFrameObject *frame, int lasti, int lineno) { PyTracebackObject *tb; - if ((next != NULL && !PyTraceBack_Check(next)) || - frame == NULL || !PyFrame_Check(frame)) { - PyErr_BadInternalCall(); - return NULL; + assert(next == NULL || PyTraceBack_Check(next)); + assert(frame != NULL); + assert(PyFrame_Check(frame)); + tb = _Py_FREELIST_POP(PyTracebackObject, tracebacks); + if (tb == NULL) { + tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); } - tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); if (tb != NULL) { tb->tb_next = (PyTracebackObject*)Py_XNewRef(next); - tb->tb_frame = (PyFrameObject*)Py_XNewRef(frame); + tb->tb_frame = (PyFrameObject*)Py_NewRef(frame); tb->tb_lasti = lasti; tb->tb_lineno = lineno; PyObject_GC_Track(tb); @@ -237,7 +239,7 @@ tb_dealloc(PyObject *op) PyObject_GC_UnTrack(tb); Py_XDECREF(tb->tb_next); Py_XDECREF(tb->tb_frame); - PyObject_GC_Del(tb); + _Py_FREELIST_FREE(tracebacks, tb, PyObject_GC_Del); } static int diff --git a/README.rst b/README.rst index d4bea5edcac674..b3058f63c27a12 100644 --- a/README.rst +++ b/README.rst @@ -1,4 +1,4 @@ -This is Python version 3.16.0 alpha 1+dev +This is Python version 3.16.0 alpha 2+dev ========================================= .. image:: https://github.com/python/cpython/actions/workflows/build.yml/badge.svg?branch=main&event=push diff --git a/Tools/build/check_capi_macros.py b/Tools/build/check_capi_macros.py old mode 100644 new mode 100755 index 62ae1fd0bb1c94..86c433a1168348 --- a/Tools/build/check_capi_macros.py +++ b/Tools/build/check_capi_macros.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python3 """ Check that all macros defined by the Python C API have a name starting with "Py". Ignore names listed by check_capi_macros_ignored.txt: macros with an diff --git a/Tools/build/check_capi_macros_ignored.txt b/Tools/build/check_capi_macros_ignored.txt index 1800a2c4ec8946..154f1dbfca039c 100644 --- a/Tools/build/check_capi_macros_ignored.txt +++ b/Tools/build/check_capi_macros_ignored.txt @@ -116,45 +116,23 @@ HAVE_CONNECT HAVE_COPY_FILE_RANGE HAVE_CTERMID HAVE_CTERMID_R -HAVE_CURSES_DEFINE_KEY HAVE_CURSES_ESCDELAY HAVE_CURSES_FILTER HAVE_CURSES_GETMOUSE HAVE_CURSES_H HAVE_CURSES_HAS_KEY -HAVE_CURSES_HAS_MOUSE HAVE_CURSES_IMMEDOK -HAVE_CURSES_IS_KEYPAD -HAVE_CURSES_IS_LEAVEOK HAVE_CURSES_IS_PAD HAVE_CURSES_IS_TERM_RESIZED -HAVE_CURSES_KEYOK -HAVE_CURSES_KEY_DEFINED -HAVE_CURSES_NEW_PRESCR -HAVE_CURSES_NOFILTER HAVE_CURSES_RESIZETERM HAVE_CURSES_RESIZE_TERM -HAVE_CURSES_SCR_DUMP -HAVE_CURSES_SCR_SET HAVE_CURSES_SET_ESCDELAY HAVE_CURSES_SET_TABSIZE -HAVE_CURSES_SLK_ATTR_OFF -HAVE_CURSES_SLK_ATTR_ON -HAVE_CURSES_SLK_ATTR_SET -HAVE_CURSES_SLK_COLOR HAVE_CURSES_SYNCOK HAVE_CURSES_TABSIZE -HAVE_CURSES_TERM_ATTRS HAVE_CURSES_TYPEAHEAD HAVE_CURSES_USE_ENV -HAVE_CURSES_USE_SCREEN -HAVE_CURSES_USE_WINDOW -HAVE_CURSES_WATTR_GET -HAVE_CURSES_WATTR_OFF -HAVE_CURSES_WATTR_ON -HAVE_CURSES_WATTR_SET HAVE_CURSES_WCHGAT -HAVE_CURSES_WCOLOR_SET HAVE_DB_H HAVE_DECLSPEC_DLL HAVE_DECL_PR_SET_VMA_ANON_NAME @@ -235,7 +213,6 @@ HAVE_GDBM_H HAVE_GDBM_NDBM_H HAVE_GETADDRINFO HAVE_GETC_UNLOCKED -HAVE_GETDENTS64 HAVE_GETEGID HAVE_GETENTROPY HAVE_GETEUID @@ -279,16 +256,13 @@ HAVE_GETSERVBYNAME HAVE_GETSERVBYPORT HAVE_GETSID HAVE_GETSOCKNAME -HAVE_GETTID HAVE_GETUID HAVE_GRANTPT HAVE_GRP_H HAVE_HSTRERROR HAVE_HTOLE64 HAVE_ICONV_H -HAVE_IF_INDEXTONAME HAVE_IF_NAMEINDEX -HAVE_IF_NAMETOINDEX HAVE_INET_ATON HAVE_INET_NTOA HAVE_INET_PTON @@ -389,9 +363,6 @@ HAVE_PANELW HAVE_PANEL_H HAVE_PATHCONF HAVE_PAUSE -HAVE_PIDFD_GETFD -HAVE_PIDFD_OPEN -HAVE_PIDFD_SEND_SIGNAL HAVE_PIPE HAVE_PIPE2 HAVE_PLOCK @@ -442,7 +413,6 @@ HAVE_RECVFROM HAVE_RENAMEAT HAVE_RL_APPEND_HISTORY HAVE_RL_CATCH_SIGNAL -HAVE_RL_CHANGE_ENVIRONMENT HAVE_RL_COMPDISP_FUNC_T HAVE_RL_COMPLETION_APPEND_CHARACTER HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK diff --git a/Tools/build/check_extension_modules.py b/Tools/build/check_extension_modules.py index c619a9a0c1c5a1..e907442b61ea22 100644 --- a/Tools/build/check_extension_modules.py +++ b/Tools/build/check_extension_modules.py @@ -421,7 +421,7 @@ def parse_setup_file(self, setup_file: pathlib.Path) -> Iterable[ModuleInfo]: state = ModuleState.BUILTIN case ["*disabled*"]: state = ModuleState.DISABLED - case ["*noconfig*"]: + case ["*noconfig*"] | ["*bootstrap*"]: continue case [*items]: if state == ModuleState.DISABLED: diff --git a/Tools/build/generate-build-details.py b/Tools/build/generate-build-details.py index 4fb49bf139b225..a6f537fc0d020d 100644 --- a/Tools/build/generate-build-details.py +++ b/Tools/build/generate-build-details.py @@ -10,6 +10,7 @@ import importlib.machinery import json import os +import re import sys import sysconfig @@ -18,17 +19,98 @@ from typing import Any +PATCHLEVEL_H = os.path.join( + os.path.dirname(__file__), '..', '..', 'Include', 'patchlevel.h', +) +RELEASE_LEVELS = {0xA: 'alpha', 0xB: 'beta', 0xC: 'candidate', 0xF: 'final'} + + def version_info_to_dict(obj: sys._version_info) -> dict[str, Any]: field_names = ('major', 'minor', 'micro', 'releaselevel', 'serial') return {field: getattr(obj, field) for field in field_names} +def get_version_info() -> dict[str, Any]: + """Return the target's version as a version_info dict. + + sys.version_info describes the running interpreter, which under + --with-build-python can differ from the target in micro version and + release level. Read Include/patchlevel.h instead when it is available. + """ + try: + with open(PATCHLEVEL_H, encoding='utf-8') as f: + patchlevel = f.read() + except FileNotFoundError: + return version_info_to_dict(sys.version_info) + defines = dict(re.findall(r'^#define\s+(PY_\w+)\s+(\S+)', patchlevel, re.M)) + # PY_RELEASE_LEVEL is defined as another macro, e.g. PY_RELEASE_LEVEL_ALPHA. + level = int(defines[defines['PY_RELEASE_LEVEL']], 16) + return { + 'major': int(defines['PY_MAJOR_VERSION']), + 'minor': int(defines['PY_MINOR_VERSION']), + 'micro': int(defines['PY_MICRO_VERSION']), + 'releaselevel': RELEASE_LEVELS[level], + 'serial': int(defines['PY_RELEASE_SERIAL']), + } + + +def hexversion(version_info: dict[str, Any]) -> int: + level = {name: value for value, name in RELEASE_LEVELS.items()} + return int( + version_info['major'] << 24 + | version_info['minor'] << 16 + | version_info['micro'] << 8 + | level[version_info['releaselevel']] << 4 + | version_info['serial'] + ) + + def get_dict_key(container: dict[str, Any], key: str) -> dict[str, Any]: for part in key.split('.'): container = container[part] return container +def get_extension_suffixes() -> list[str]: + """Return the extension module suffixes of the target interpreter. + + importlib.machinery.EXTENSION_SUFFIXES describes the running interpreter, + which is not the target under --with-build-python. On POSIX, rebuild the + list from the target configuration instead, following + _PyImport_DynLoadFiletab in Python/dynload_*.c. The two are kept in sync + by test_build_details.BuildDetailsTargetTests.test_native_extension_suffixes. + """ + if os.name != 'posix': + return importlib.machinery.EXTENSION_SUFFIXES + + dynloadfile = sysconfig.get_config_var('DYNLOADFILE') + if dynloadfile == 'dynload_stub.o': + return [] + if dynloadfile == 'dynload_hpux.o': + return [sysconfig.get_config_var('EXT_SUFFIX'), '.sl'] + if dynloadfile != 'dynload_shlib.o': + raise ValueError(f'Unsupported DYNLOADFILE: {dynloadfile!r}') + + soabi = sysconfig.get_config_var('SOABI') + if sysconfig.get_config_var('MACHDEP') == 'cygwin': + return [f'.{soabi}.dll', '.dll'] + + suffixes = [f'.{soabi}.so'] + alt_soabi = sysconfig.get_config_var('ALT_SOABI') + if alt_soabi: + suffixes.append(f'.{alt_soabi}.so') + platform = sysconfig.get_config_var('SOABI_PLATFORM') + if not sysconfig.get_config_var('Py_GIL_DISABLED'): + if platform: + suffixes.append(f'.abi3-{platform}.so') + suffixes.append('.abi3.so') + if platform: + suffixes.append(f'.abi3t-{platform}.so') + suffixes.append('.abi3t.so') + suffixes.append('.so') + return suffixes + + def generate_data(schema_version: str) -> collections.defaultdict[str, Any]: """Generate the build-details.json data (PEP 739). @@ -54,22 +136,30 @@ def generate_data(schema_version: str) -> collections.defaultdict[str, Any]: ) data['platform'] = sysconfig.get_platform() + version_info = get_version_info() data['language']['version'] = sysconfig.get_python_version() - data['language']['version_info'] = version_info_to_dict(sys.version_info) + data['language']['version_info'] = version_info data['implementation'] = vars(sys.implementation).copy() - data['implementation']['version'] = version_info_to_dict(sys.implementation.version) + data['implementation']['version'] = version_info + data['implementation']['hexversion'] = hexversion(version_info) # Fix cross-compilation if '_multiarch' in data['implementation']: data['implementation']['_multiarch'] = sysconfig.get_config_var('MULTIARCH') + if 'supports_isolated_interpreters' in data['implementation']: + # Mirrors Python/sysmodule.c. + data['implementation']['supports_isolated_interpreters'] = ( + sysconfig.get_config_var('MACHDEP') not in ('wasi', 'emscripten') + ) - data['abi']['flags'] = list(sys.abiflags) + data['abi']['flags'] = list(sysconfig.get_config_var('ABIFLAGS')) data['suffixes']['source'] = importlib.machinery.SOURCE_SUFFIXES data['suffixes']['bytecode'] = importlib.machinery.BYTECODE_SUFFIXES #data['suffixes']['optimized_bytecode'] = importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES #data['suffixes']['debug_bytecode'] = importlib.machinery.DEBUG_BYTECODE_SUFFIXES - data['suffixes']['extensions'] = importlib.machinery.EXTENSION_SUFFIXES + extension_suffixes = get_extension_suffixes() + data['suffixes']['extensions'] = extension_suffixes LIBDIR = sysconfig.get_config_var('LIBDIR') LIBPL = sysconfig.get_config_var('LIBPL') @@ -118,17 +208,16 @@ def generate_data(schema_version: str) -> collections.defaultdict[str, Any]: # regardless. # # We only want to set abi.extension_suffix and stable_abi_suffix if - # extension modules are supported. - if has_dynamic_library: + # extension modules are supported, regardless of how libpython is built. + if extension_suffixes: data['abi']['extension_suffix'] = sysconfig.get_config_var('EXT_SUFFIX') - # EXTENSION_SUFFIXES has been constant for a long time, and currently we - # don't have a better information source to find the stable ABI suffix. - for suffix in importlib.machinery.EXTENSION_SUFFIXES: + for suffix in extension_suffixes: if suffix.startswith('.abi'): data['abi']['stable_abi_suffix'] = suffix break + if has_dynamic_library: data['libpython']['dynamic'] = os.path.join(dynamic_library_dir, LDLIBRARY) # FIXME: Not sure if windows has a different dll for the stable ABI, and # even if it does, currently we don't have a way to get its name. diff --git a/Tools/build/regen-configure.sh b/Tools/build/regen-configure.sh index c7683eb36763af..a7a95b7878dc79 100755 --- a/Tools/build/regen-configure.sh +++ b/Tools/build/regen-configure.sh @@ -26,4 +26,27 @@ if command -v selinuxenabled >/dev/null && selinuxenabled; then PATH_OPT=":Z" fi -"$RUNTIME" run --rm -v "$abs_srcdir:$WORK_DIR$PATH_OPT" "$IMAGE" +# Run the container as the invoking user so that generated files are owned by +# that user and not by root. Rootless runtimes already map root to the invoking +# user, so a user mapping is only needed for rootful Docker. +USER_OPT="" +case "$RUNTIME" in + podman) + USER_OPT="--userns=keep-id" + ;; + docker) + if ! docker info --format '{{.SecurityOptions}}' | grep -q rootless; then + USER_OPT="-u $(id -u):$(id -g)" + fi + ;; +esac + +purge_autom4te_cache() { + [ -d "$abs_srcdir/autom4te.cache" ] || return 0 + "$RUNTIME" run --rm -v "$abs_srcdir:$WORK_DIR$PATH_OPT" \ + "$IMAGE" sh -c "rm -rf $WORK_DIR/autom4te.cache" +} + +purge_autom4te_cache +trap 'purge_autom4te_cache || true' EXIT +"$RUNTIME" run --rm $USER_OPT -v "$abs_srcdir:$WORK_DIR$PATH_OPT" "$IMAGE" diff --git a/Tools/c-analyzer/c_parser/parser/_regexes.py b/Tools/c-analyzer/c_parser/parser/_regexes.py index c1a8ab3ad2f15d..65fe2f929a942e 100644 --- a/Tools/c-analyzer/c_parser/parser/_regexes.py +++ b/Tools/c-analyzer/c_parser/parser/_regexes.py @@ -340,7 +340,10 @@ def _ind(text, level=1, edges='both'): # This is actually a "constant expression". \d+ | - [^'",}}]+ + [^'",(}}]+ + | + # Parens may hold commas (e.g. from offsetof()). + [^'";}}]+ (?= ; ) ) )? \s* diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index e8bb6b7661e12c..3695208a09f0d5 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -344,12 +344,6 @@ Modules/_testcapi/vectorcall.c - MethodDescriptorBase_Type - Modules/_testcapi/vectorcall.c - MethodDescriptorDerived_Type - Modules/_testcapi/vectorcall.c - MethodDescriptorNopGet_Type - Modules/_testcapi/vectorcall.c - MethodDescriptor2_Type - -Modules/_testclinic.c - DeprStarInit - -Modules/_testclinic.c - DeprStarInitNoInline - -Modules/_testclinic.c - DeprStarNew - -Modules/_testclinic.c - DeprKwdInit - -Modules/_testclinic.c - DeprKwdInitNoInline - -Modules/_testclinic.c - DeprKwdNew - Modules/_testclinic.c - TestClass - Modules/_testclinic.c - VcKwOnly_Type - Modules/_testclinic.c - VcNew_Type - @@ -401,10 +395,6 @@ Modules/xxmodule.c - ErrorObject - ## state Modules/_datetimemodule.c - _datetime_global_state - -Modules/_tkinter.c - tcl_lock - -Modules/_tkinter.c - excInCmd - -Modules/_tkinter.c - valInCmd - -Modules/_tkinter.c - trbInCmd - Modules/socketmodule.c - netdb_lock - @@ -435,15 +425,14 @@ Modules/readline.c - libedit_history_start - Modules/_ctypes/malloc_closure.c - free_list - Modules/_curses_panel.c - lop - Modules/_ssl/debughelpers.c _PySSL_keylog_callback lock - -Modules/_tkinter.c - quitMainLoop - -Modules/_tkinter.c - errorInCmd - Modules/_tkinter.c - Tkinter_busywaitinterval - Modules/_tkinter.c - call_mutex - Modules/_tkinter.c - var_mutex - Modules/_tkinter.c - command_mutex - Modules/_tkinter.c - HeadFHCD - Modules/_tkinter.c - stdin_ready - -Modules/_tkinter.c - event_tstate - +Modules/_tkinter.c - event_hook_users - +Modules/_tkinter.c - event_hook_mutex - Modules/readline.c - completer_word_break_characters - Modules/readline.c - _history_length - Modules/readline.c - should_auto_add_history - diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv index a51ac686268f41..4b1039269377df 100644 --- a/Tools/c-analyzer/cpython/ignored.tsv +++ b/Tools/c-analyzer/cpython/ignored.tsv @@ -26,6 +26,7 @@ Modules/posixmodule.c os_pipe_impl pipe2_works_atomic - ## guards around resource init Python/thread_pthread.h PyThread__init_thread lib_initialized - Modules/_struct.c - endian_tables_init_once - +Modules/_ctypes/callbacks.c - closure_alloc_init_once - ##----------------------- ## other values (not Python-specific) @@ -56,6 +57,7 @@ Python/pyhash.c - _Py_HashSecret - ## thread-safe hashtable (internal locks) Python/parking_lot.c - buckets - +Python/pystate.c - state_freelists - ## shared empty sentinel for the per-type method cache Python/typecache.c - empty_cache - @@ -261,6 +263,7 @@ Modules/_struct.c - bigendian_table - Modules/_struct.c - lilendian_table - Modules/_struct.c - native_table - Modules/_tkinter.c - state_key - +Modules/_tkinter.c - excInCmd_key - Modules/_interpchannelsmodule.c - _channelid_end_recv - Modules/_interpchannelsmodule.c - _channelid_end_send - Modules/_zoneinfo.c - DAYS_BEFORE_MONTH - diff --git a/Tools/clinic/libclinic/converters.py b/Tools/clinic/libclinic/converters.py index c54dab302b6658..014486f7f469de 100644 --- a/Tools/clinic/libclinic/converters.py +++ b/Tools/clinic/libclinic/converters.py @@ -1437,7 +1437,7 @@ class VarKeywordCConverter(CConverter): def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None: raise AssertionError('should never be called') - def parse_var_keyword(self) -> str: + def parse_var_keyword(self, *, stackargs: bool) -> str: raise NotImplementedError @@ -1448,16 +1448,16 @@ class var_keyword_dict_converter(VarKeywordCConverter): def cleanup(self) -> str: return f'Py_XDECREF({self.parser_name});\n' - def parse_var_keyword(self) -> str: + def parse_var_keyword(self, *, stackargs: bool) -> str: + # NULL when there are no keywords: no call allocates an empty dict. param_name = self.parser_name - return f""" - if (kwargs == NULL) {{{{ - {param_name} = PyDict_New(); - if ({param_name} == NULL) {{{{ - goto exit; + if stackargs: + return f""" + if (kwnames != NULL) {{{{ + {param_name} = _PyStack_AsDict(args + nargs, kwnames); + if ({param_name} == NULL) {{{{ + goto exit; + }}}} }}}} - }}}} - else {{{{ - {param_name} = Py_NewRef(kwargs); - }}}} - """ + """ + return f"{param_name} = Py_XNewRef(kwargs);\n" diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index d4fcf4d6b350c6..c514844bb8866d 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -996,7 +996,7 @@ def parse_parameter(self, line: str) -> None: # handle "as" for parameters too c_name = None - m = re.match(r'(?:\* *)?\w+( +as +(\w+))', line) + m = re.match(r'(?:\*{1,2} *)?\w+( +as +(\w+))', line) if m: c_name = m[2] line = line[:m.start(1)] + line[m.end(1):] @@ -1643,6 +1643,9 @@ def check_vectorcall_parameters(self, lineno: int) -> None: if p.group: fail("@vectorcall does not support optional groups", line_number=lineno) + if p.deprecated_positional or p.deprecated_keyword: + fail("@vectorcall does not support deprecated parameters", + line_number=lineno) if p.is_vararg() or p.is_var_keyword(): continue if isinstance(p.converter, (self_converter, diff --git a/Tools/clinic/libclinic/parse_args.py b/Tools/clinic/libclinic/parse_args.py index 0d62e230b4a56f..4608947cb7fff7 100644 --- a/Tools/clinic/libclinic/parse_args.py +++ b/Tools/clinic/libclinic/parse_args.py @@ -933,11 +933,11 @@ def _parse_vararg(self, *, fastcall: bool) -> str: fastcall=fastcall, limited_capi=self.limited_capi) - def _parse_kwarg(self) -> str: + def _parse_kwarg(self, *, stackargs: bool) -> str: assert self.var_keyword is not None c = self.var_keyword.converter assert isinstance(c, libclinic.converters.VarKeywordCConverter) - return c.parse_var_keyword() + return c.parse_var_keyword(stackargs=stackargs) def _check_positional(self, nargs: str, *, indent: int = 4) -> list[str]: @@ -967,9 +967,8 @@ def _parse_positional_args( ) -> list[str] | None: """Emit per-parameter positional argument parsing. - Shared by parse_pos_only() and the vectorcall paths. Returns the - code snippets, or None if a converter doesn't support parse_arg - (the caller must fall back to a tuple/stack parser). + Returns the code snippets, or None if a converter doesn't support + parse_arg (the caller must fall back to a tuple/stack parser). """ parser_code: list[str] = [] for i, p in enumerate(self.parameters): @@ -998,9 +997,9 @@ def _parse_positional_args( if self.varpos: parser_code.append(libclinic.normalize_snippet( self._parse_vararg(fastcall=fastcall), indent=4)) - elif self.var_keyword: - parser_code.append(libclinic.normalize_snippet(self._parse_kwarg(), - indent=4)) + if self.var_keyword: + parser_code.append(libclinic.normalize_snippet( + self._parse_kwarg(stackargs=fastcall), indent=4)) return parser_code def select_positional_convention(self) -> tuple[str, str]: @@ -1112,48 +1111,38 @@ def parse_pos_only(self) -> None: """, indent=4)) self.parser_body(*parser_code) - def parse_var_keyword(self) -> None: + def parse_var_keyword(self, *, vectorcall: bool = False) -> None: self.flags = "METH_VARARGS|METH_KEYWORDS" - self.parser_prototype = PARSER_PROTOTYPE_KEYWORD - nargs = 'PyTuple_GET_SIZE(args)' + # The vectorcall gets an array of arguments and kwnames, not a tuple + # and a dict: use the converters' fastcall convention, which handles + # that. + if vectorcall: + nargs, argname_fmt = 'nargs', 'args[%d]' + check, arg = '_PyArg_NoPositionalStack', 'nargs' + parser_code = self._vectorcall_type_check() + else: + self.parser_prototype = PARSER_PROTOTYPE_KEYWORD + nargs, argname_fmt = 'PyTuple_GET_SIZE(args)', 'PyTuple_GET_ITEM(args, %d)' + check, arg = '_PyArg_NoPositional', 'args' + parser_code = [] - parser_code = [] if self.varpos is None and self.min_pos == self.max_pos == 0: - self.codegen.add_include('pycore_modsupport.h', - '_PyArg_NoPositional()') - parser_code.append(libclinic.normalize_snippet(""" - if (!_PyArg_NoPositional("{name}", args)) {{ + self.codegen.add_include('pycore_modsupport.h', f'{check}()') + parser_code.append(libclinic.normalize_snippet(f""" + if (!{check}("{{name}}", {arg})) {{{{ goto exit; - }} + }}}} """, indent=4)) else: parser_code.extend(self._check_positional(nargs)) - has_optional = False - for i, p in enumerate(self.parameters): - parse_arg = p.converter.parse_arg( - f'PyTuple_GET_ITEM(args, {i})', - p.get_displayname(i+1), - limited_capi=self.limited_capi, - ) - assert parse_arg is not None - if has_optional or p.is_optional(): - has_optional = True - parser_code.append(libclinic.normalize_snippet(""" - if (%s < %d) {{ - goto skip_optional; - }} - """, indent=4) % (nargs, i + 1)) - parser_code.append(libclinic.normalize_snippet(parse_arg, indent=4)) - if has_optional: - parser_code.append("skip_optional:") - - if self.varpos: - parser_code.append(libclinic.normalize_snippet( - self._parse_vararg(fastcall=self.fastcall), indent=4)) - if self.var_keyword: - parser_code.append(libclinic.normalize_snippet(self._parse_kwarg(), indent=4)) - self.parser_body(*parser_code) + pos_code = self._parse_positional_args( + argname_fmt=argname_fmt, nargs=nargs, + limited_capi=self.limited_capi, fastcall=vectorcall) + assert pos_code is not None + parser_code.extend(pos_code) + body = self.vectorcall_body if vectorcall else self.parser_body + body(*parser_code) def use_converters(self) -> None: """Prepare for parsing all arguments by a single call. @@ -1456,7 +1445,7 @@ def handle_new_or_init(self) -> None: if self.func.kind is METHOD_INIT: self.return_value_declaration = "int {parser_retval} = -1;" - if self.func.vectorcall and 'METH_KEYWORDS' in self.flags: + if self.parser_prototype == PARSER_PROTOTYPE_KEYWORD_HELPER: self._new_or_init_delegate_to_helper() else: self._new_or_init_parser_body() @@ -1624,8 +1613,6 @@ def _vectorcall_positional(self, *, arity_checked: Already have a number of arguments check. """ - # The vectorcall gets an array of arguments, not a tuple: use the - # converters' fastcall convention, which handles that. pos_code = self._parse_positional_args( argname_fmt='args[%d]', nargs='nargs', limited_capi=False, fastcall=True) @@ -1781,7 +1768,7 @@ def parse_vectorcall(self) -> None: if not self.parameters and not self.varpos and not self.var_keyword: self.parse_vectorcall_no_args() elif self.var_keyword is not None: - self.parse_vectorcall_kw_required() + self.parse_var_keyword(vectorcall=True) elif self.pos_only == len(self.parameters): self.parse_vectorcall_pos_only() elif any(p.is_keyword_only() for p in self.parameters) or self.varpos: diff --git a/Tools/i18n/msgfmt.py b/Tools/i18n/msgfmt.py index 3351511bbc8f2b..0ca61a0af9e35a 100755 --- a/Tools/i18n/msgfmt.py +++ b/Tools/i18n/msgfmt.py @@ -59,21 +59,39 @@ def add(ctxt, id, str, fuzzy): def generate(): "Return the generated output." global MESSAGES + + # The hash table uses open addressing with double hashing, as in GNU + # gettext's write-mo.c: the first probe is V % M and each following probe + # adds 1 + V % (M - 2), where V is the hash of the string and M is the + # table size. M is the smallest prime greater than 4/3 * N (N being the + # number of strings), which keeps the expected number of probes small. + # See Knuth, The Art of Computer Programming, Volume 3, section 6.4. + hash_tab_size = _next_prime((len(MESSAGES) * 4) // 3) + if hash_tab_size <= 2: + hash_tab_size = 3 + hash_table = array.array("I", [0] * hash_tab_size) + # the keys are sorted in the .mo file keys = sorted(MESSAGES.keys()) offsets = [] ids = strs = b'' - for id in keys: + for i, id in enumerate(keys): + hash_val = _hashpjw(id) + idx = hash_val % hash_tab_size + inc = 1 + (hash_val % (hash_tab_size - 2)) + while hash_table[idx]: + idx = (idx + inc) % hash_tab_size + hash_table[idx] = i + 1 # For each string, we need size and file offset. Each string is NUL # terminated; the NUL does not count into the size. offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id]))) ids += id + b'\0' strs += MESSAGES[id] + b'\0' - output = '' - # The header is 7 32-bit unsigned integers. We don't use hash tables, so - # the keys start right after the index tables. - # translated string. - keystart = 7*4+16*len(keys) + + # The header is 7 32-bit unsigned integers, followed by the index tables, + # then the hash table, then the keys. + hashstart = 7*4+16*len(keys) + keystart = hashstart+hash_tab_size*4 # and the values start after the keys valuestart = keystart + len(ids) koffsets = [] @@ -85,13 +103,15 @@ def generate(): voffsets += [l2, o2+valuestart] offsets = koffsets + voffsets output = struct.pack("Iiiiiii", - 0x950412de, # Magic + 0x950412de, # Magic 0, # Version len(keys), # # of entries 7*4, # start of key index 7*4+len(keys)*8, # start of value index - 0, 0) # size and offset of hash table + hash_tab_size, # size of hash table + hashstart) # offset of hash table output += array.array("i", offsets).tobytes() + output += hash_table.tobytes() output += ids output += strs return output @@ -257,5 +277,39 @@ def main(): make(filename, outfile) +# Utilities for writing hash table + +# Peter J. Weinberger hash function +# See: https://www.drdobbs.com/database/hashing-rehashed/184409859 +def _hashpjw(strs): + hval = 0 + for s in strs: + if not s: + break + hval <<= 4 + hval += s + g = hval & (0xF << 28) + if g: + hval ^= g >> 24 + hval ^= g + return hval & 0xFFFFFFFF + + +def _next_prime(start): + def is_prime(num): + divn = 3 + sq = divn * divn + while sq < num and num % divn != 0: + divn += 1 + sq += 4 * divn + divn += 1 + + return num % divn != 0 + + candidate = start | 1 + while not is_prime(candidate): + candidate += 2 + return candidate + if __name__ == '__main__': main() diff --git a/configure b/configure index fca5669c3756b4..da44380d27a1ab 100755 --- a/configure +++ b/configure @@ -1035,12 +1035,14 @@ CONFIG_ARGS SOVERSION VERSION PYTHON_FOR_REGEN +RUNSHARED_FOR_BUILD PYTHON_FOR_BUILD_DEPS FREEZE_MODULE_DEPS FREEZE_MODULE FREEZE_MODULE_BOOTSTRAP_DEPS FREEZE_MODULE_BOOTSTRAP PYTHON_FOR_FREEZE +MODULE_CHECK_COMMAND PYTHON_FOR_BUILD host_os host_vendor @@ -3829,7 +3831,12 @@ fi fi ac_cv_prog_PYTHON_FOR_REGEN=$with_build_python PYTHON_FOR_FREEZE="$with_build_python" - PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) _PYTHON_SYSCONFIGDATA_PATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`) '$with_build_python + PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) PYTHONPATH=$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) _PYTHON_SYSCONFIGDATA_PATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`) '$with_build_python + if test "x$cross_compiling" = xyes +then : + PYTHON_FOR_BUILD='_PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) '"$PYTHON_FOR_BUILD" + +fi { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $with_build_python" >&5 printf "%s\n" "$with_build_python" >&6; } @@ -3849,6 +3856,17 @@ fi +# Module import checks must run with the target interpreter on native builds. +if test "x$cross_compiling" = xyes +then : + MODULE_CHECK_COMMAND='$(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --cross-compiling' +else case e in #( + e) MODULE_CHECK_COMMAND='$(RUNSHARED) ./$(BUILDPYTHON) -E $(srcdir)/Tools/build/check_extension_modules.py --no-cross-compiling' + ;; +esac +fi + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for Python interpreter freezing" >&5 printf %s "checking for Python interpreter freezing... " >&6; } { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $PYTHON_FOR_FREEZE" >&5 @@ -3863,6 +3881,9 @@ then : FREEZE_MODULE='$(FREEZE_MODULE_BOOTSTRAP)' FREEZE_MODULE_DEPS='$(FREEZE_MODULE_BOOTSTRAP_DEPS)' PYTHON_FOR_BUILD_DEPS='' + # The build Python is a separate interpreter. RUNSHARED would make it + # load the in-tree libpython, which may need a different libc. + RUNSHARED_FOR_BUILD='' else case e in #( e) @@ -3871,6 +3892,7 @@ else case e in #( FREEZE_MODULE='$(PYTHON_FOR_FREEZE) $(srcdir)/Programs/_freeze_module.py' FREEZE_MODULE_DEPS="_bootstrap_python \$(srcdir)/Programs/_freeze_module.py" PYTHON_FOR_BUILD_DEPS='$(BUILDPYTHON)' + RUNSHARED_FOR_BUILD='$(RUNSHARED)' ;; esac @@ -3881,6 +3903,7 @@ fi + for ac_prog in python$PACKAGE_VERSION python3.16 python3.15 python3.14 python3.13 python3.12 python3.11 python3.10 python3 python do # Extract the first word of "$ac_prog", so it can be a program name with args. @@ -9308,6 +9331,92 @@ fi else LTOFLAGS="-flto=${Py_LTO_POLICY}" fi + LTOCFLAGS="$LTOFLAGS" + # GNU ld needs LLVMgold.so for Clang LTO. If the configured linker + # cannot link LTO objects, try lld, which does not need that plugin. + save_CFLAGS=$CFLAGS +save_CPPFLAGS=$CPPFLAGS +save_LDFLAGS=$LDFLAGS +save_LIBS=$LIBS + + + LDFLAGS="$LDFLAGS $LDFLAGS_NODIST $LTOFLAGS" + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether Clang LTO works with the configured linker" >&5 +printf %s "checking whether Clang LTO works with the configured linker... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +int +main (void) +{ + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO" +then : + clang_lto_link=yes +else case e in #( + e) clang_lto_link=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam \ + conftest$ac_exeext conftest.$ac_ext + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $clang_lto_link" >&5 +printf "%s\n" "$clang_lto_link" >&6; } + +CFLAGS=$save_CFLAGS +CPPFLAGS=$save_CPPFLAGS +LDFLAGS=$save_LDFLAGS +LIBS=$save_LIBS + + + if test "$clang_lto_link" = no; then + save_CFLAGS=$CFLAGS +save_CPPFLAGS=$CPPFLAGS +save_LDFLAGS=$LDFLAGS +save_LIBS=$LIBS + + + LDFLAGS="$LDFLAGS -fuse-ld=lld" + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether Clang LTO works with lld" >&5 +printf %s "checking whether Clang LTO works with lld... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +int +main (void) +{ + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO" +then : + clang_lto_link=yes +else case e in #( + e) clang_lto_link=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam \ + conftest$ac_exeext conftest.$ac_ext + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $clang_lto_link" >&5 +printf "%s\n" "$clang_lto_link" >&6; } + +CFLAGS=$save_CFLAGS +CPPFLAGS=$save_CPPFLAGS +LDFLAGS=$save_LDFLAGS +LIBS=$save_LIBS + + + if test "$clang_lto_link" = no; then + as_fn_error $? "Clang LTO requires a working LTO-aware linker. Install lld or the LLVMgold.so plugin, select a linker with LDFLAGS, or disable LTO with --without-lto." "$LINENO" 5 + fi + LTOFLAGS="$LTOFLAGS -fuse-ld=lld" + LDFLAGS_NOLTO="$LDFLAGS_NOLTO -fuse-ld=lld" + fi ;; esac ;; @@ -20917,12 +21026,6 @@ if test "x$ac_cv_func_gai_strerror" = xyes then : printf "%s\n" "#define HAVE_GAI_STRERROR 1" >>confdefs.h -fi -ac_fn_c_check_func "$LINENO" "getdents64" "ac_cv_func_getdents64" -if test "x$ac_cv_func_getdents64" = xyes -then : - printf "%s\n" "#define HAVE_GETDENTS64 1" >>confdefs.h - fi ac_fn_c_check_func "$LINENO" "getegid" "ac_cv_func_getegid" if test "x$ac_cv_func_getegid" = xyes @@ -21085,12 +21188,6 @@ if test "x$ac_cv_func_getsid" = xyes then : printf "%s\n" "#define HAVE_GETSID 1" >>confdefs.h -fi -ac_fn_c_check_func "$LINENO" "gettid" "ac_cv_func_gettid" -if test "x$ac_cv_func_gettid" = xyes -then : - printf "%s\n" "#define HAVE_GETTID 1" >>confdefs.h - fi ac_fn_c_check_func "$LINENO" "getuid" "ac_cv_func_getuid" if test "x$ac_cv_func_getuid" = xyes @@ -21103,24 +21200,12 @@ if test "x$ac_cv_func_grantpt" = xyes then : printf "%s\n" "#define HAVE_GRANTPT 1" >>confdefs.h -fi -ac_fn_c_check_func "$LINENO" "if_indextoname" "ac_cv_func_if_indextoname" -if test "x$ac_cv_func_if_indextoname" = xyes -then : - printf "%s\n" "#define HAVE_IF_INDEXTONAME 1" >>confdefs.h - fi ac_fn_c_check_func "$LINENO" "if_nameindex" "ac_cv_func_if_nameindex" if test "x$ac_cv_func_if_nameindex" = xyes then : printf "%s\n" "#define HAVE_IF_NAMEINDEX 1" >>confdefs.h -fi -ac_fn_c_check_func "$LINENO" "if_nametoindex" "ac_cv_func_if_nametoindex" -if test "x$ac_cv_func_if_nametoindex" = xyes -then : - printf "%s\n" "#define HAVE_IF_NAMETOINDEX 1" >>confdefs.h - fi ac_fn_c_check_func "$LINENO" "initgroups" "ac_cv_func_initgroups" if test "x$ac_cv_func_initgroups" = xyes @@ -21265,24 +21350,6 @@ if test "x$ac_cv_func_pause" = xyes then : printf "%s\n" "#define HAVE_PAUSE 1" >>confdefs.h -fi -ac_fn_c_check_func "$LINENO" "pidfd_open" "ac_cv_func_pidfd_open" -if test "x$ac_cv_func_pidfd_open" = xyes -then : - printf "%s\n" "#define HAVE_PIDFD_OPEN 1" >>confdefs.h - -fi -ac_fn_c_check_func "$LINENO" "pidfd_getfd" "ac_cv_func_pidfd_getfd" -if test "x$ac_cv_func_pidfd_getfd" = xyes -then : - printf "%s\n" "#define HAVE_PIDFD_GETFD 1" >>confdefs.h - -fi -ac_fn_c_check_func "$LINENO" "pidfd_send_signal" "ac_cv_func_pidfd_send_signal" -if test "x$ac_cv_func_pidfd_send_signal" = xyes -then : - printf "%s\n" "#define HAVE_PIDFD_SEND_SIGNAL 1" >>confdefs.h - fi ac_fn_c_check_func "$LINENO" "pipe" "ac_cv_func_pipe" if test "x$ac_cv_func_pipe" = xyes @@ -22009,6 +22076,299 @@ fi + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for getdents64" >&5 +printf %s "checking for getdents64... " >&6; } +if test ${ac_cv_func_getdents64+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main (void) +{ +(void)getdents64 + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + ac_cv_func_getdents64=yes +else case e in #( + e) ac_cv_func_getdents64=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_getdents64" >&5 +printf "%s\n" "$ac_cv_func_getdents64" >&6; } + if test "x$ac_cv_func_getdents64" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_GETDENTS64 1" >>confdefs.h + +fi + + + + + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for gettid" >&5 +printf %s "checking for gettid... " >&6; } +if test ${ac_cv_func_gettid+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main (void) +{ +(void)gettid + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + ac_cv_func_gettid=yes +else case e in #( + e) ac_cv_func_gettid=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_gettid" >&5 +printf "%s\n" "$ac_cv_func_gettid" >&6; } + if test "x$ac_cv_func_gettid" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_GETTID 1" >>confdefs.h + +fi + + + + + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for if_indextoname" >&5 +printf %s "checking for if_indextoname... " >&6; } +if test ${ac_cv_func_if_indextoname+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main (void) +{ +(void)if_indextoname + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + ac_cv_func_if_indextoname=yes +else case e in #( + e) ac_cv_func_if_indextoname=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_if_indextoname" >&5 +printf "%s\n" "$ac_cv_func_if_indextoname" >&6; } + if test "x$ac_cv_func_if_indextoname" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_IF_INDEXTONAME 1" >>confdefs.h + +fi + + + + + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for if_nametoindex" >&5 +printf %s "checking for if_nametoindex... " >&6; } +if test ${ac_cv_func_if_nametoindex+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main (void) +{ +(void)if_nametoindex + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + ac_cv_func_if_nametoindex=yes +else case e in #( + e) ac_cv_func_if_nametoindex=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_if_nametoindex" >&5 +printf "%s\n" "$ac_cv_func_if_nametoindex" >&6; } + if test "x$ac_cv_func_if_nametoindex" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_IF_NAMETOINDEX 1" >>confdefs.h + +fi + + + + + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for pidfd_open" >&5 +printf %s "checking for pidfd_open... " >&6; } +if test ${ac_cv_func_pidfd_open+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main (void) +{ +(void)pidfd_open + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + ac_cv_func_pidfd_open=yes +else case e in #( + e) ac_cv_func_pidfd_open=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_pidfd_open" >&5 +printf "%s\n" "$ac_cv_func_pidfd_open" >&6; } + if test "x$ac_cv_func_pidfd_open" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_PIDFD_OPEN 1" >>confdefs.h + +fi + + + + + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for pidfd_getfd" >&5 +printf %s "checking for pidfd_getfd... " >&6; } +if test ${ac_cv_func_pidfd_getfd+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main (void) +{ +(void)pidfd_getfd + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + ac_cv_func_pidfd_getfd=yes +else case e in #( + e) ac_cv_func_pidfd_getfd=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_pidfd_getfd" >&5 +printf "%s\n" "$ac_cv_func_pidfd_getfd" >&6; } + if test "x$ac_cv_func_pidfd_getfd" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_PIDFD_GETFD 1" >>confdefs.h + +fi + + + + + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for pidfd_send_signal" >&5 +printf %s "checking for pidfd_send_signal... " >&6; } +if test ${ac_cv_func_pidfd_send_signal+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main (void) +{ +(void)pidfd_send_signal + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + ac_cv_func_pidfd_send_signal=yes +else case e in #( + e) ac_cv_func_pidfd_send_signal=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_pidfd_send_signal" >&5 +printf "%s\n" "$ac_cv_func_pidfd_send_signal" >&6; } + if test "x$ac_cv_func_pidfd_send_signal" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_PIDFD_SEND_SIGNAL 1" >>confdefs.h + +fi + + + + + # os.statx uses Linux's statx function. AIX also has a function named statx, # but it's unrelated. Check only on Linux (including Android). case $ac_sys_system in #( @@ -29761,7 +30121,7 @@ printf "%s\n" "$ac_cv_readline_rl_change_environment" >&6; } then : -printf "%s\n" "#define HAVE_RL_CHANGE_ENVIRONMENT 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_RL_CHANGE_ENVIRONMENT 1" >>confdefs.h fi @@ -31934,6 +32294,9 @@ fi + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function is_pad" >&5 printf %s "checking for curses function is_pad... " >&6; } if test ${ac_cv_lib_curses_is_pad+y} @@ -31995,6 +32358,7 @@ fi + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function is_term_resized" >&5 printf %s "checking for curses function is_term_resized... " >&6; } if test ${ac_cv_lib_curses_is_term_resized+y} @@ -32056,6 +32420,7 @@ fi + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function resize_term" >&5 printf %s "checking for curses function resize_term... " >&6; } if test ${ac_cv_lib_curses_resize_term+y} @@ -32117,6 +32482,7 @@ fi + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function resizeterm" >&5 printf %s "checking for curses function resizeterm... " >&6; } if test ${ac_cv_lib_curses_resizeterm+y} @@ -32178,6 +32544,7 @@ fi + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function immedok" >&5 printf %s "checking for curses function immedok... " >&6; } if test ${ac_cv_lib_curses_immedok+y} @@ -32239,6 +32606,7 @@ fi + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function syncok" >&5 printf %s "checking for curses function syncok... " >&6; } if test ${ac_cv_lib_curses_syncok+y} @@ -32267,8 +32635,70 @@ int main (void) { - #ifndef syncok - void * volatile x = (void *)syncok; (void)x; + #ifndef syncok + void * volatile x = (void *)syncok; (void)x; + #endif + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO" +then : + ac_cv_lib_curses_syncok=yes +else case e in #( + e) ac_cv_lib_curses_syncok=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam \ + conftest$ac_exeext conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_syncok" >&5 +printf "%s\n" "$ac_cv_lib_curses_syncok" >&6; } + if test "x$ac_cv_lib_curses_syncok" = xyes +then : + +printf "%s\n" "#define HAVE_CURSES_SYNCOK 1" >>confdefs.h + +fi + + + + + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wchgat" >&5 +printf %s "checking for curses function wchgat... " >&6; } +if test ${ac_cv_lib_curses_wchgat+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +#define NCURSES_OPAQUE 0 +#if defined(HAVE_NCURSESW_NCURSES_H) +# include +#elif defined(HAVE_NCURSESW_CURSES_H) +# include +#elif defined(HAVE_NCURSES_NCURSES_H) +# include +#elif defined(HAVE_NCURSES_CURSES_H) +# include +#elif defined(HAVE_NCURSES_H) +# include +#elif defined(HAVE_CURSES_H) +# include +#endif + +int +main (void) +{ + + #ifndef wchgat + void * volatile x = (void *)wchgat; (void)x; #endif ; @@ -32277,9 +32707,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_syncok=yes + ac_cv_lib_curses_wchgat=yes else case e in #( - e) ac_cv_lib_curses_syncok=no ;; + e) ac_cv_lib_curses_wchgat=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32287,12 +32717,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_syncok" >&5 -printf "%s\n" "$ac_cv_lib_curses_syncok" >&6; } - if test "x$ac_cv_lib_curses_syncok" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wchgat" >&5 +printf "%s\n" "$ac_cv_lib_curses_wchgat" >&6; } + if test "x$ac_cv_lib_curses_wchgat" = xyes then : -printf "%s\n" "#define HAVE_CURSES_SYNCOK 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_WCHGAT 1" >>confdefs.h fi @@ -32300,9 +32730,10 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wchgat" >&5 -printf %s "checking for curses function wchgat... " >&6; } -if test ${ac_cv_lib_curses_wchgat+y} + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function filter" >&5 +printf %s "checking for curses function filter... " >&6; } +if test ${ac_cv_lib_curses_filter+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32328,8 +32759,8 @@ int main (void) { - #ifndef wchgat - void * volatile x = (void *)wchgat; (void)x; + #ifndef filter + void * volatile x = (void *)filter; (void)x; #endif ; @@ -32338,9 +32769,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_wchgat=yes + ac_cv_lib_curses_filter=yes else case e in #( - e) ac_cv_lib_curses_wchgat=no ;; + e) ac_cv_lib_curses_filter=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32348,12 +32779,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wchgat" >&5 -printf "%s\n" "$ac_cv_lib_curses_wchgat" >&6; } - if test "x$ac_cv_lib_curses_wchgat" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_filter" >&5 +printf "%s\n" "$ac_cv_lib_curses_filter" >&6; } + if test "x$ac_cv_lib_curses_filter" = xyes then : -printf "%s\n" "#define HAVE_CURSES_WCHGAT 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_FILTER 1" >>confdefs.h fi @@ -32361,9 +32792,10 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function filter" >&5 -printf %s "checking for curses function filter... " >&6; } -if test ${ac_cv_lib_curses_filter+y} + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function has_key" >&5 +printf %s "checking for curses function has_key... " >&6; } +if test ${ac_cv_lib_curses_has_key+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32389,8 +32821,8 @@ int main (void) { - #ifndef filter - void * volatile x = (void *)filter; (void)x; + #ifndef has_key + void * volatile x = (void *)has_key; (void)x; #endif ; @@ -32399,9 +32831,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_filter=yes + ac_cv_lib_curses_has_key=yes else case e in #( - e) ac_cv_lib_curses_filter=no ;; + e) ac_cv_lib_curses_has_key=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32409,12 +32841,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_filter" >&5 -printf "%s\n" "$ac_cv_lib_curses_filter" >&6; } - if test "x$ac_cv_lib_curses_filter" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_has_key" >&5 +printf "%s\n" "$ac_cv_lib_curses_has_key" >&6; } + if test "x$ac_cv_lib_curses_has_key" = xyes then : -printf "%s\n" "#define HAVE_CURSES_FILTER 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_HAS_KEY 1" >>confdefs.h fi @@ -32422,9 +32854,10 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function nofilter" >&5 -printf %s "checking for curses function nofilter... " >&6; } -if test ${ac_cv_lib_curses_nofilter+y} + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function typeahead" >&5 +printf %s "checking for curses function typeahead... " >&6; } +if test ${ac_cv_lib_curses_typeahead+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32450,8 +32883,8 @@ int main (void) { - #ifndef nofilter - void * volatile x = (void *)nofilter; (void)x; + #ifndef typeahead + void * volatile x = (void *)typeahead; (void)x; #endif ; @@ -32460,9 +32893,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_nofilter=yes + ac_cv_lib_curses_typeahead=yes else case e in #( - e) ac_cv_lib_curses_nofilter=no ;; + e) ac_cv_lib_curses_typeahead=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32470,12 +32903,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_nofilter" >&5 -printf "%s\n" "$ac_cv_lib_curses_nofilter" >&6; } - if test "x$ac_cv_lib_curses_nofilter" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_typeahead" >&5 +printf "%s\n" "$ac_cv_lib_curses_typeahead" >&6; } + if test "x$ac_cv_lib_curses_typeahead" = xyes then : -printf "%s\n" "#define HAVE_CURSES_NOFILTER 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_TYPEAHEAD 1" >>confdefs.h fi @@ -32483,9 +32916,10 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function has_key" >&5 -printf %s "checking for curses function has_key... " >&6; } -if test ${ac_cv_lib_curses_has_key+y} + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function use_env" >&5 +printf %s "checking for curses function use_env... " >&6; } +if test ${ac_cv_lib_curses_use_env+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32511,8 +32945,8 @@ int main (void) { - #ifndef has_key - void * volatile x = (void *)has_key; (void)x; + #ifndef use_env + void * volatile x = (void *)use_env; (void)x; #endif ; @@ -32521,9 +32955,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_has_key=yes + ac_cv_lib_curses_use_env=yes else case e in #( - e) ac_cv_lib_curses_has_key=no ;; + e) ac_cv_lib_curses_use_env=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32531,12 +32965,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_has_key" >&5 -printf "%s\n" "$ac_cv_lib_curses_has_key" >&6; } - if test "x$ac_cv_lib_curses_has_key" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_use_env" >&5 +printf "%s\n" "$ac_cv_lib_curses_use_env" >&6; } + if test "x$ac_cv_lib_curses_use_env" = xyes then : -printf "%s\n" "#define HAVE_CURSES_HAS_KEY 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_USE_ENV 1" >>confdefs.h fi @@ -32544,9 +32978,10 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function has_mouse" >&5 -printf %s "checking for curses function has_mouse... " >&6; } -if test ${ac_cv_lib_curses_has_mouse+y} + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function set_escdelay" >&5 +printf %s "checking for curses function set_escdelay... " >&6; } +if test ${ac_cv_lib_curses_set_escdelay+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32572,8 +33007,8 @@ int main (void) { - #ifndef has_mouse - void * volatile x = (void *)has_mouse; (void)x; + #ifndef set_escdelay + void * volatile x = (void *)set_escdelay; (void)x; #endif ; @@ -32582,9 +33017,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_has_mouse=yes + ac_cv_lib_curses_set_escdelay=yes else case e in #( - e) ac_cv_lib_curses_has_mouse=no ;; + e) ac_cv_lib_curses_set_escdelay=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32592,12 +33027,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_has_mouse" >&5 -printf "%s\n" "$ac_cv_lib_curses_has_mouse" >&6; } - if test "x$ac_cv_lib_curses_has_mouse" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_set_escdelay" >&5 +printf "%s\n" "$ac_cv_lib_curses_set_escdelay" >&6; } + if test "x$ac_cv_lib_curses_set_escdelay" = xyes then : -printf "%s\n" "#define HAVE_CURSES_HAS_MOUSE 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_SET_ESCDELAY 1" >>confdefs.h fi @@ -32605,9 +33040,10 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function is_keypad" >&5 -printf %s "checking for curses function is_keypad... " >&6; } -if test ${ac_cv_lib_curses_is_keypad+y} + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function set_tabsize" >&5 +printf %s "checking for curses function set_tabsize... " >&6; } +if test ${ac_cv_lib_curses_set_tabsize+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32633,8 +33069,8 @@ int main (void) { - #ifndef is_keypad - void * volatile x = (void *)is_keypad; (void)x; + #ifndef set_tabsize + void * volatile x = (void *)set_tabsize; (void)x; #endif ; @@ -32643,9 +33079,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_is_keypad=yes + ac_cv_lib_curses_set_tabsize=yes else case e in #( - e) ac_cv_lib_curses_is_keypad=no ;; + e) ac_cv_lib_curses_set_tabsize=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32653,12 +33089,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_is_keypad" >&5 -printf "%s\n" "$ac_cv_lib_curses_is_keypad" >&6; } - if test "x$ac_cv_lib_curses_is_keypad" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_set_tabsize" >&5 +printf "%s\n" "$ac_cv_lib_curses_set_tabsize" >&6; } + if test "x$ac_cv_lib_curses_set_tabsize" = xyes then : -printf "%s\n" "#define HAVE_CURSES_IS_KEYPAD 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_SET_TABSIZE 1" >>confdefs.h fi @@ -32666,9 +33102,9 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function is_leaveok" >&5 -printf %s "checking for curses function is_leaveok... " >&6; } -if test ${ac_cv_lib_curses_is_leaveok+y} + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses variable ESCDELAY" >&5 +printf %s "checking for curses variable ESCDELAY... " >&6; } +if test ${ac_cv_lib_curses_ESCDELAY+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32694,32 +33130,29 @@ int main (void) { - #ifndef is_leaveok - void * volatile x = (void *)is_leaveok; (void)x; - #endif + int x = ESCDELAY; (void)x; ; return 0; } _ACEOF -if ac_fn_c_try_link "$LINENO" +if ac_fn_c_try_compile "$LINENO" then : - ac_cv_lib_curses_is_leaveok=yes + ac_cv_lib_curses_ESCDELAY=yes else case e in #( - e) ac_cv_lib_curses_is_leaveok=no ;; + e) ac_cv_lib_curses_ESCDELAY=no ;; esac fi -rm -f core conftest.err conftest.$ac_objext conftest.beam \ - conftest$ac_exeext conftest.$ac_ext +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_is_leaveok" >&5 -printf "%s\n" "$ac_cv_lib_curses_is_leaveok" >&6; } - if test "x$ac_cv_lib_curses_is_leaveok" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_ESCDELAY" >&5 +printf "%s\n" "$ac_cv_lib_curses_ESCDELAY" >&6; } + if test "x$ac_cv_lib_curses_ESCDELAY" = xyes then : -printf "%s\n" "#define HAVE_CURSES_IS_LEAVEOK 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_ESCDELAY 1" >>confdefs.h fi @@ -32727,9 +33160,9 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function typeahead" >&5 -printf %s "checking for curses function typeahead... " >&6; } -if test ${ac_cv_lib_curses_typeahead+y} + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses variable TABSIZE" >&5 +printf %s "checking for curses variable TABSIZE... " >&6; } +if test ${ac_cv_lib_curses_TABSIZE+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32755,32 +33188,29 @@ int main (void) { - #ifndef typeahead - void * volatile x = (void *)typeahead; (void)x; - #endif + int x = TABSIZE; (void)x; ; return 0; } _ACEOF -if ac_fn_c_try_link "$LINENO" +if ac_fn_c_try_compile "$LINENO" then : - ac_cv_lib_curses_typeahead=yes + ac_cv_lib_curses_TABSIZE=yes else case e in #( - e) ac_cv_lib_curses_typeahead=no ;; + e) ac_cv_lib_curses_TABSIZE=no ;; esac fi -rm -f core conftest.err conftest.$ac_objext conftest.beam \ - conftest$ac_exeext conftest.$ac_ext +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_typeahead" >&5 -printf "%s\n" "$ac_cv_lib_curses_typeahead" >&6; } - if test "x$ac_cv_lib_curses_typeahead" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_TABSIZE" >&5 +printf "%s\n" "$ac_cv_lib_curses_TABSIZE" >&6; } + if test "x$ac_cv_lib_curses_TABSIZE" = xyes then : -printf "%s\n" "#define HAVE_CURSES_TYPEAHEAD 1" >>confdefs.h +printf "%s\n" "#define HAVE_CURSES_TABSIZE 1" >>confdefs.h fi @@ -32788,9 +33218,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function use_env" >&5 -printf %s "checking for curses function use_env... " >&6; } -if test ${ac_cv_lib_curses_use_env+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function define_key" >&5 +printf %s "checking for curses function define_key... " >&6; } +if test ${ac_cv_lib_curses_define_key+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32816,8 +33248,8 @@ int main (void) { - #ifndef use_env - void * volatile x = (void *)use_env; (void)x; + #ifndef define_key + void * volatile x = (void *)define_key; (void)x; #endif ; @@ -32826,9 +33258,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_use_env=yes + ac_cv_lib_curses_define_key=yes else case e in #( - e) ac_cv_lib_curses_use_env=no ;; + e) ac_cv_lib_curses_define_key=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32836,12 +33268,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_use_env" >&5 -printf "%s\n" "$ac_cv_lib_curses_use_env" >&6; } - if test "x$ac_cv_lib_curses_use_env" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_define_key" >&5 +printf "%s\n" "$ac_cv_lib_curses_define_key" >&6; } + if test "x$ac_cv_lib_curses_define_key" = xyes then : -printf "%s\n" "#define HAVE_CURSES_USE_ENV 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_DEFINE_KEY 1" >>confdefs.h fi @@ -32849,9 +33281,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function new_prescr" >&5 -printf %s "checking for curses function new_prescr... " >&6; } -if test ${ac_cv_lib_curses_new_prescr+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function has_mouse" >&5 +printf %s "checking for curses function has_mouse... " >&6; } +if test ${ac_cv_lib_curses_has_mouse+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32877,8 +33311,8 @@ int main (void) { - #ifndef new_prescr - void * volatile x = (void *)new_prescr; (void)x; + #ifndef has_mouse + void * volatile x = (void *)has_mouse; (void)x; #endif ; @@ -32887,9 +33321,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_new_prescr=yes + ac_cv_lib_curses_has_mouse=yes else case e in #( - e) ac_cv_lib_curses_new_prescr=no ;; + e) ac_cv_lib_curses_has_mouse=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32897,12 +33331,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_new_prescr" >&5 -printf "%s\n" "$ac_cv_lib_curses_new_prescr" >&6; } - if test "x$ac_cv_lib_curses_new_prescr" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_has_mouse" >&5 +printf "%s\n" "$ac_cv_lib_curses_has_mouse" >&6; } + if test "x$ac_cv_lib_curses_has_mouse" = xyes then : -printf "%s\n" "#define HAVE_CURSES_NEW_PRESCR 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_HAS_MOUSE 1" >>confdefs.h fi @@ -32910,9 +33344,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function use_screen" >&5 -printf %s "checking for curses function use_screen... " >&6; } -if test ${ac_cv_lib_curses_use_screen+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function is_keypad" >&5 +printf %s "checking for curses function is_keypad... " >&6; } +if test ${ac_cv_lib_curses_is_keypad+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32938,8 +33374,8 @@ int main (void) { - #ifndef use_screen - void * volatile x = (void *)use_screen; (void)x; + #ifndef is_keypad + void * volatile x = (void *)is_keypad; (void)x; #endif ; @@ -32948,9 +33384,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_use_screen=yes + ac_cv_lib_curses_is_keypad=yes else case e in #( - e) ac_cv_lib_curses_use_screen=no ;; + e) ac_cv_lib_curses_is_keypad=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -32958,12 +33394,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_use_screen" >&5 -printf "%s\n" "$ac_cv_lib_curses_use_screen" >&6; } - if test "x$ac_cv_lib_curses_use_screen" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_is_keypad" >&5 +printf "%s\n" "$ac_cv_lib_curses_is_keypad" >&6; } + if test "x$ac_cv_lib_curses_is_keypad" = xyes then : -printf "%s\n" "#define HAVE_CURSES_USE_SCREEN 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_IS_KEYPAD 1" >>confdefs.h fi @@ -32971,9 +33407,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function use_window" >&5 -printf %s "checking for curses function use_window... " >&6; } -if test ${ac_cv_lib_curses_use_window+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function is_leaveok" >&5 +printf %s "checking for curses function is_leaveok... " >&6; } +if test ${ac_cv_lib_curses_is_leaveok+y} then : printf %s "(cached) " >&6 else case e in #( @@ -32999,8 +33437,8 @@ int main (void) { - #ifndef use_window - void * volatile x = (void *)use_window; (void)x; + #ifndef is_leaveok + void * volatile x = (void *)is_leaveok; (void)x; #endif ; @@ -33009,9 +33447,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_use_window=yes + ac_cv_lib_curses_is_leaveok=yes else case e in #( - e) ac_cv_lib_curses_use_window=no ;; + e) ac_cv_lib_curses_is_leaveok=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33019,12 +33457,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_use_window" >&5 -printf "%s\n" "$ac_cv_lib_curses_use_window" >&6; } - if test "x$ac_cv_lib_curses_use_window" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_is_leaveok" >&5 +printf "%s\n" "$ac_cv_lib_curses_is_leaveok" >&6; } + if test "x$ac_cv_lib_curses_is_leaveok" = xyes then : -printf "%s\n" "#define HAVE_CURSES_USE_WINDOW 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_IS_LEAVEOK 1" >>confdefs.h fi @@ -33032,6 +33470,8 @@ fi + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function key_defined" >&5 printf %s "checking for curses function key_defined... " >&6; } if test ${ac_cv_lib_curses_key_defined+y} @@ -33085,7 +33525,7 @@ printf "%s\n" "$ac_cv_lib_curses_key_defined" >&6; } if test "x$ac_cv_lib_curses_key_defined" = xyes then : -printf "%s\n" "#define HAVE_CURSES_KEY_DEFINED 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_KEY_DEFINED 1" >>confdefs.h fi @@ -33093,9 +33533,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function term_attrs" >&5 -printf %s "checking for curses function term_attrs... " >&6; } -if test ${ac_cv_lib_curses_term_attrs+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function keyok" >&5 +printf %s "checking for curses function keyok... " >&6; } +if test ${ac_cv_lib_curses_keyok+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33121,8 +33563,8 @@ int main (void) { - #ifndef term_attrs - void * volatile x = (void *)term_attrs; (void)x; + #ifndef keyok + void * volatile x = (void *)keyok; (void)x; #endif ; @@ -33131,9 +33573,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_term_attrs=yes + ac_cv_lib_curses_keyok=yes else case e in #( - e) ac_cv_lib_curses_term_attrs=no ;; + e) ac_cv_lib_curses_keyok=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33141,12 +33583,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_term_attrs" >&5 -printf "%s\n" "$ac_cv_lib_curses_term_attrs" >&6; } - if test "x$ac_cv_lib_curses_term_attrs" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_keyok" >&5 +printf "%s\n" "$ac_cv_lib_curses_keyok" >&6; } + if test "x$ac_cv_lib_curses_keyok" = xyes then : -printf "%s\n" "#define HAVE_CURSES_TERM_ATTRS 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_KEYOK 1" >>confdefs.h fi @@ -33154,9 +33596,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function define_key" >&5 -printf %s "checking for curses function define_key... " >&6; } -if test ${ac_cv_lib_curses_define_key+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function new_prescr" >&5 +printf %s "checking for curses function new_prescr... " >&6; } +if test ${ac_cv_lib_curses_new_prescr+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33182,8 +33626,8 @@ int main (void) { - #ifndef define_key - void * volatile x = (void *)define_key; (void)x; + #ifndef new_prescr + void * volatile x = (void *)new_prescr; (void)x; #endif ; @@ -33192,9 +33636,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_define_key=yes + ac_cv_lib_curses_new_prescr=yes else case e in #( - e) ac_cv_lib_curses_define_key=no ;; + e) ac_cv_lib_curses_new_prescr=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33202,12 +33646,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_define_key" >&5 -printf "%s\n" "$ac_cv_lib_curses_define_key" >&6; } - if test "x$ac_cv_lib_curses_define_key" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_new_prescr" >&5 +printf "%s\n" "$ac_cv_lib_curses_new_prescr" >&6; } + if test "x$ac_cv_lib_curses_new_prescr" = xyes then : -printf "%s\n" "#define HAVE_CURSES_DEFINE_KEY 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_NEW_PRESCR 1" >>confdefs.h fi @@ -33215,9 +33659,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function keyok" >&5 -printf %s "checking for curses function keyok... " >&6; } -if test ${ac_cv_lib_curses_keyok+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function nofilter" >&5 +printf %s "checking for curses function nofilter... " >&6; } +if test ${ac_cv_lib_curses_nofilter+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33243,8 +33689,8 @@ int main (void) { - #ifndef keyok - void * volatile x = (void *)keyok; (void)x; + #ifndef nofilter + void * volatile x = (void *)nofilter; (void)x; #endif ; @@ -33253,9 +33699,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_keyok=yes + ac_cv_lib_curses_nofilter=yes else case e in #( - e) ac_cv_lib_curses_keyok=no ;; + e) ac_cv_lib_curses_nofilter=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33263,12 +33709,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_keyok" >&5 -printf "%s\n" "$ac_cv_lib_curses_keyok" >&6; } - if test "x$ac_cv_lib_curses_keyok" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_nofilter" >&5 +printf "%s\n" "$ac_cv_lib_curses_nofilter" >&6; } + if test "x$ac_cv_lib_curses_nofilter" = xyes then : -printf "%s\n" "#define HAVE_CURSES_KEYOK 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_NOFILTER 1" >>confdefs.h fi @@ -33276,9 +33722,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function set_escdelay" >&5 -printf %s "checking for curses function set_escdelay... " >&6; } -if test ${ac_cv_lib_curses_set_escdelay+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function slk_attr_off" >&5 +printf %s "checking for curses function slk_attr_off... " >&6; } +if test ${ac_cv_lib_curses_slk_attr_off+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33304,8 +33752,8 @@ int main (void) { - #ifndef set_escdelay - void * volatile x = (void *)set_escdelay; (void)x; + #ifndef slk_attr_off + void * volatile x = (void *)slk_attr_off; (void)x; #endif ; @@ -33314,9 +33762,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_set_escdelay=yes + ac_cv_lib_curses_slk_attr_off=yes else case e in #( - e) ac_cv_lib_curses_set_escdelay=no ;; + e) ac_cv_lib_curses_slk_attr_off=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33324,12 +33772,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_set_escdelay" >&5 -printf "%s\n" "$ac_cv_lib_curses_set_escdelay" >&6; } - if test "x$ac_cv_lib_curses_set_escdelay" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_slk_attr_off" >&5 +printf "%s\n" "$ac_cv_lib_curses_slk_attr_off" >&6; } + if test "x$ac_cv_lib_curses_slk_attr_off" = xyes then : -printf "%s\n" "#define HAVE_CURSES_SET_ESCDELAY 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_SLK_ATTR_OFF 1" >>confdefs.h fi @@ -33337,9 +33785,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function set_tabsize" >&5 -printf %s "checking for curses function set_tabsize... " >&6; } -if test ${ac_cv_lib_curses_set_tabsize+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function slk_attr_on" >&5 +printf %s "checking for curses function slk_attr_on... " >&6; } +if test ${ac_cv_lib_curses_slk_attr_on+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33365,8 +33815,8 @@ int main (void) { - #ifndef set_tabsize - void * volatile x = (void *)set_tabsize; (void)x; + #ifndef slk_attr_on + void * volatile x = (void *)slk_attr_on; (void)x; #endif ; @@ -33375,9 +33825,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_set_tabsize=yes + ac_cv_lib_curses_slk_attr_on=yes else case e in #( - e) ac_cv_lib_curses_set_tabsize=no ;; + e) ac_cv_lib_curses_slk_attr_on=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33385,12 +33835,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_set_tabsize" >&5 -printf "%s\n" "$ac_cv_lib_curses_set_tabsize" >&6; } - if test "x$ac_cv_lib_curses_set_tabsize" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_slk_attr_on" >&5 +printf "%s\n" "$ac_cv_lib_curses_slk_attr_on" >&6; } + if test "x$ac_cv_lib_curses_slk_attr_on" = xyes then : -printf "%s\n" "#define HAVE_CURSES_SET_TABSIZE 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_SLK_ATTR_ON 1" >>confdefs.h fi @@ -33398,9 +33848,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wattr_get" >&5 -printf %s "checking for curses function wattr_get... " >&6; } -if test ${ac_cv_lib_curses_wattr_get+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function slk_attr_set" >&5 +printf %s "checking for curses function slk_attr_set... " >&6; } +if test ${ac_cv_lib_curses_slk_attr_set+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33426,8 +33878,8 @@ int main (void) { - #ifndef wattr_get - void * volatile x = (void *)wattr_get; (void)x; + #ifndef slk_attr_set + void * volatile x = (void *)slk_attr_set; (void)x; #endif ; @@ -33436,9 +33888,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_wattr_get=yes + ac_cv_lib_curses_slk_attr_set=yes else case e in #( - e) ac_cv_lib_curses_wattr_get=no ;; + e) ac_cv_lib_curses_slk_attr_set=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33446,12 +33898,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wattr_get" >&5 -printf "%s\n" "$ac_cv_lib_curses_wattr_get" >&6; } - if test "x$ac_cv_lib_curses_wattr_get" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_slk_attr_set" >&5 +printf "%s\n" "$ac_cv_lib_curses_slk_attr_set" >&6; } + if test "x$ac_cv_lib_curses_slk_attr_set" = xyes then : -printf "%s\n" "#define HAVE_CURSES_WATTR_GET 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_SLK_ATTR_SET 1" >>confdefs.h fi @@ -33459,9 +33911,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wattr_set" >&5 -printf %s "checking for curses function wattr_set... " >&6; } -if test ${ac_cv_lib_curses_wattr_set+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function slk_color" >&5 +printf %s "checking for curses function slk_color... " >&6; } +if test ${ac_cv_lib_curses_slk_color+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33487,8 +33941,8 @@ int main (void) { - #ifndef wattr_set - void * volatile x = (void *)wattr_set; (void)x; + #ifndef slk_color + void * volatile x = (void *)slk_color; (void)x; #endif ; @@ -33497,9 +33951,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_wattr_set=yes + ac_cv_lib_curses_slk_color=yes else case e in #( - e) ac_cv_lib_curses_wattr_set=no ;; + e) ac_cv_lib_curses_slk_color=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33507,12 +33961,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wattr_set" >&5 -printf "%s\n" "$ac_cv_lib_curses_wattr_set" >&6; } - if test "x$ac_cv_lib_curses_wattr_set" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_slk_color" >&5 +printf "%s\n" "$ac_cv_lib_curses_slk_color" >&6; } + if test "x$ac_cv_lib_curses_slk_color" = xyes then : -printf "%s\n" "#define HAVE_CURSES_WATTR_SET 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_SLK_COLOR 1" >>confdefs.h fi @@ -33520,9 +33974,74 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wattr_on" >&5 -printf %s "checking for curses function wattr_on... " >&6; } -if test ${ac_cv_lib_curses_wattr_on+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function term_attrs" >&5 +printf %s "checking for curses function term_attrs... " >&6; } +if test ${ac_cv_lib_curses_term_attrs+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +#define NCURSES_OPAQUE 0 +#if defined(HAVE_NCURSESW_NCURSES_H) +# include +#elif defined(HAVE_NCURSESW_CURSES_H) +# include +#elif defined(HAVE_NCURSES_NCURSES_H) +# include +#elif defined(HAVE_NCURSES_CURSES_H) +# include +#elif defined(HAVE_NCURSES_H) +# include +#elif defined(HAVE_CURSES_H) +# include +#endif + +int +main (void) +{ + + #ifndef term_attrs + void * volatile x = (void *)term_attrs; (void)x; + #endif + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO" +then : + ac_cv_lib_curses_term_attrs=yes +else case e in #( + e) ac_cv_lib_curses_term_attrs=no ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam \ + conftest$ac_exeext conftest.$ac_ext + ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_term_attrs" >&5 +printf "%s\n" "$ac_cv_lib_curses_term_attrs" >&6; } + if test "x$ac_cv_lib_curses_term_attrs" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_CURSES_TERM_ATTRS 1" >>confdefs.h + +fi + + + + + + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function use_screen" >&5 +printf %s "checking for curses function use_screen... " >&6; } +if test ${ac_cv_lib_curses_use_screen+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33548,8 +34067,8 @@ int main (void) { - #ifndef wattr_on - void * volatile x = (void *)wattr_on; (void)x; + #ifndef use_screen + void * volatile x = (void *)use_screen; (void)x; #endif ; @@ -33558,9 +34077,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_wattr_on=yes + ac_cv_lib_curses_use_screen=yes else case e in #( - e) ac_cv_lib_curses_wattr_on=no ;; + e) ac_cv_lib_curses_use_screen=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33568,12 +34087,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wattr_on" >&5 -printf "%s\n" "$ac_cv_lib_curses_wattr_on" >&6; } - if test "x$ac_cv_lib_curses_wattr_on" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_use_screen" >&5 +printf "%s\n" "$ac_cv_lib_curses_use_screen" >&6; } + if test "x$ac_cv_lib_curses_use_screen" = xyes then : -printf "%s\n" "#define HAVE_CURSES_WATTR_ON 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_USE_SCREEN 1" >>confdefs.h fi @@ -33581,9 +34100,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wattr_off" >&5 -printf %s "checking for curses function wattr_off... " >&6; } -if test ${ac_cv_lib_curses_wattr_off+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function use_window" >&5 +printf %s "checking for curses function use_window... " >&6; } +if test ${ac_cv_lib_curses_use_window+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33609,8 +34130,8 @@ int main (void) { - #ifndef wattr_off - void * volatile x = (void *)wattr_off; (void)x; + #ifndef use_window + void * volatile x = (void *)use_window; (void)x; #endif ; @@ -33619,9 +34140,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_wattr_off=yes + ac_cv_lib_curses_use_window=yes else case e in #( - e) ac_cv_lib_curses_wattr_off=no ;; + e) ac_cv_lib_curses_use_window=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33629,12 +34150,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wattr_off" >&5 -printf "%s\n" "$ac_cv_lib_curses_wattr_off" >&6; } - if test "x$ac_cv_lib_curses_wattr_off" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_use_window" >&5 +printf "%s\n" "$ac_cv_lib_curses_use_window" >&6; } + if test "x$ac_cv_lib_curses_use_window" = xyes then : -printf "%s\n" "#define HAVE_CURSES_WATTR_OFF 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_USE_WINDOW 1" >>confdefs.h fi @@ -33642,9 +34163,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wcolor_set" >&5 -printf %s "checking for curses function wcolor_set... " >&6; } -if test ${ac_cv_lib_curses_wcolor_set+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wattr_get" >&5 +printf %s "checking for curses function wattr_get... " >&6; } +if test ${ac_cv_lib_curses_wattr_get+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33670,8 +34193,8 @@ int main (void) { - #ifndef wcolor_set - void * volatile x = (void *)wcolor_set; (void)x; + #ifndef wattr_get + void * volatile x = (void *)wattr_get; (void)x; #endif ; @@ -33680,9 +34203,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_wcolor_set=yes + ac_cv_lib_curses_wattr_get=yes else case e in #( - e) ac_cv_lib_curses_wcolor_set=no ;; + e) ac_cv_lib_curses_wattr_get=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33690,12 +34213,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wcolor_set" >&5 -printf "%s\n" "$ac_cv_lib_curses_wcolor_set" >&6; } - if test "x$ac_cv_lib_curses_wcolor_set" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wattr_get" >&5 +printf "%s\n" "$ac_cv_lib_curses_wattr_get" >&6; } + if test "x$ac_cv_lib_curses_wattr_get" = xyes then : -printf "%s\n" "#define HAVE_CURSES_WCOLOR_SET 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_WATTR_GET 1" >>confdefs.h fi @@ -33703,9 +34226,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function slk_attr_on" >&5 -printf %s "checking for curses function slk_attr_on... " >&6; } -if test ${ac_cv_lib_curses_slk_attr_on+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wattr_off" >&5 +printf %s "checking for curses function wattr_off... " >&6; } +if test ${ac_cv_lib_curses_wattr_off+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33731,8 +34256,8 @@ int main (void) { - #ifndef slk_attr_on - void * volatile x = (void *)slk_attr_on; (void)x; + #ifndef wattr_off + void * volatile x = (void *)wattr_off; (void)x; #endif ; @@ -33741,9 +34266,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_slk_attr_on=yes + ac_cv_lib_curses_wattr_off=yes else case e in #( - e) ac_cv_lib_curses_slk_attr_on=no ;; + e) ac_cv_lib_curses_wattr_off=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33751,12 +34276,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_slk_attr_on" >&5 -printf "%s\n" "$ac_cv_lib_curses_slk_attr_on" >&6; } - if test "x$ac_cv_lib_curses_slk_attr_on" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wattr_off" >&5 +printf "%s\n" "$ac_cv_lib_curses_wattr_off" >&6; } + if test "x$ac_cv_lib_curses_wattr_off" = xyes then : -printf "%s\n" "#define HAVE_CURSES_SLK_ATTR_ON 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_WATTR_OFF 1" >>confdefs.h fi @@ -33764,9 +34289,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function slk_attr_off" >&5 -printf %s "checking for curses function slk_attr_off... " >&6; } -if test ${ac_cv_lib_curses_slk_attr_off+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wattr_on" >&5 +printf %s "checking for curses function wattr_on... " >&6; } +if test ${ac_cv_lib_curses_wattr_on+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33792,8 +34319,8 @@ int main (void) { - #ifndef slk_attr_off - void * volatile x = (void *)slk_attr_off; (void)x; + #ifndef wattr_on + void * volatile x = (void *)wattr_on; (void)x; #endif ; @@ -33802,9 +34329,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_slk_attr_off=yes + ac_cv_lib_curses_wattr_on=yes else case e in #( - e) ac_cv_lib_curses_slk_attr_off=no ;; + e) ac_cv_lib_curses_wattr_on=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33812,12 +34339,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_slk_attr_off" >&5 -printf "%s\n" "$ac_cv_lib_curses_slk_attr_off" >&6; } - if test "x$ac_cv_lib_curses_slk_attr_off" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wattr_on" >&5 +printf "%s\n" "$ac_cv_lib_curses_wattr_on" >&6; } + if test "x$ac_cv_lib_curses_wattr_on" = xyes then : -printf "%s\n" "#define HAVE_CURSES_SLK_ATTR_OFF 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_WATTR_ON 1" >>confdefs.h fi @@ -33825,9 +34352,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function slk_attr_set" >&5 -printf %s "checking for curses function slk_attr_set... " >&6; } -if test ${ac_cv_lib_curses_slk_attr_set+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wattr_set" >&5 +printf %s "checking for curses function wattr_set... " >&6; } +if test ${ac_cv_lib_curses_wattr_set+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33853,8 +34382,8 @@ int main (void) { - #ifndef slk_attr_set - void * volatile x = (void *)slk_attr_set; (void)x; + #ifndef wattr_set + void * volatile x = (void *)wattr_set; (void)x; #endif ; @@ -33863,9 +34392,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_slk_attr_set=yes + ac_cv_lib_curses_wattr_set=yes else case e in #( - e) ac_cv_lib_curses_slk_attr_set=no ;; + e) ac_cv_lib_curses_wattr_set=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33873,12 +34402,12 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_slk_attr_set" >&5 -printf "%s\n" "$ac_cv_lib_curses_slk_attr_set" >&6; } - if test "x$ac_cv_lib_curses_slk_attr_set" = xyes +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wattr_set" >&5 +printf "%s\n" "$ac_cv_lib_curses_wattr_set" >&6; } + if test "x$ac_cv_lib_curses_wattr_set" = xyes then : -printf "%s\n" "#define HAVE_CURSES_SLK_ATTR_SET 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_WATTR_SET 1" >>confdefs.h fi @@ -33886,9 +34415,11 @@ fi - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function slk_color" >&5 -printf %s "checking for curses function slk_color... " >&6; } -if test ${ac_cv_lib_curses_slk_color+y} + + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function wcolor_set" >&5 +printf %s "checking for curses function wcolor_set... " >&6; } +if test ${ac_cv_lib_curses_wcolor_set+y} then : printf %s "(cached) " >&6 else case e in #( @@ -33914,8 +34445,8 @@ int main (void) { - #ifndef slk_color - void * volatile x = (void *)slk_color; (void)x; + #ifndef wcolor_set + void * volatile x = (void *)wcolor_set; (void)x; #endif ; @@ -33924,9 +34455,9 @@ main (void) _ACEOF if ac_fn_c_try_link "$LINENO" then : - ac_cv_lib_curses_slk_color=yes + ac_cv_lib_curses_wcolor_set=yes else case e in #( - e) ac_cv_lib_curses_slk_color=no ;; + e) ac_cv_lib_curses_wcolor_set=no ;; esac fi rm -f core conftest.err conftest.$ac_objext conftest.beam \ @@ -33934,130 +34465,15 @@ rm -f core conftest.err conftest.$ac_objext conftest.beam \ ;; esac fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_slk_color" >&5 -printf "%s\n" "$ac_cv_lib_curses_slk_color" >&6; } - if test "x$ac_cv_lib_curses_slk_color" = xyes -then : - -printf "%s\n" "#define HAVE_CURSES_SLK_COLOR 1" >>confdefs.h - -fi - - - - - - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses variable ESCDELAY" >&5 -printf %s "checking for curses variable ESCDELAY... " >&6; } -if test ${ac_cv_lib_curses_ESCDELAY+y} -then : - printf %s "(cached) " >&6 -else case e in #( - e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -#define NCURSES_OPAQUE 0 -#if defined(HAVE_NCURSESW_NCURSES_H) -# include -#elif defined(HAVE_NCURSESW_CURSES_H) -# include -#elif defined(HAVE_NCURSES_NCURSES_H) -# include -#elif defined(HAVE_NCURSES_CURSES_H) -# include -#elif defined(HAVE_NCURSES_H) -# include -#elif defined(HAVE_CURSES_H) -# include -#endif - -int -main (void) -{ - - int x = ESCDELAY; (void)x; - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO" -then : - ac_cv_lib_curses_ESCDELAY=yes -else case e in #( - e) ac_cv_lib_curses_ESCDELAY=no ;; -esac -fi -rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext - ;; -esac -fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_ESCDELAY" >&5 -printf "%s\n" "$ac_cv_lib_curses_ESCDELAY" >&6; } - if test "x$ac_cv_lib_curses_ESCDELAY" = xyes -then : - -printf "%s\n" "#define HAVE_CURSES_ESCDELAY 1" >>confdefs.h - -fi - - - - - - { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses variable TABSIZE" >&5 -printf %s "checking for curses variable TABSIZE... " >&6; } -if test ${ac_cv_lib_curses_TABSIZE+y} +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_wcolor_set" >&5 +printf "%s\n" "$ac_cv_lib_curses_wcolor_set" >&6; } + if test "x$ac_cv_lib_curses_wcolor_set" = xyes then : - printf %s "(cached) " >&6 -else case e in #( - e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -#define NCURSES_OPAQUE 0 -#if defined(HAVE_NCURSESW_NCURSES_H) -# include -#elif defined(HAVE_NCURSESW_CURSES_H) -# include -#elif defined(HAVE_NCURSES_NCURSES_H) -# include -#elif defined(HAVE_NCURSES_CURSES_H) -# include -#elif defined(HAVE_NCURSES_H) -# include -#elif defined(HAVE_CURSES_H) -# include -#endif -int -main (void) -{ +printf "%s\n" "#define _Py_HAVE_CURSES_WCOLOR_SET 1" >>confdefs.h - int x = TABSIZE; (void)x; - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO" -then : - ac_cv_lib_curses_TABSIZE=yes -else case e in #( - e) ac_cv_lib_curses_TABSIZE=no ;; -esac fi -rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext - ;; -esac -fi -{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_curses_TABSIZE" >&5 -printf "%s\n" "$ac_cv_lib_curses_TABSIZE" >&6; } - if test "x$ac_cv_lib_curses_TABSIZE" = xyes -then : -printf "%s\n" "#define HAVE_CURSES_TABSIZE 1" >>confdefs.h - -fi @@ -34115,6 +34531,7 @@ fi + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function scr_dump" >&5 printf %s "checking for curses function scr_dump... " >&6; } if test ${ac_cv_lib_curses_scr_dump+y} @@ -34168,7 +34585,7 @@ printf "%s\n" "$ac_cv_lib_curses_scr_dump" >&6; } if test "x$ac_cv_lib_curses_scr_dump" = xyes then : -printf "%s\n" "#define HAVE_CURSES_SCR_DUMP 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_SCR_DUMP 1" >>confdefs.h fi @@ -34176,6 +34593,8 @@ fi + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for curses function scr_set" >&5 printf %s "checking for curses function scr_set... " >&6; } if test ${ac_cv_lib_curses_scr_set+y} @@ -34229,12 +34648,14 @@ printf "%s\n" "$ac_cv_lib_curses_scr_set" >&6; } if test "x$ac_cv_lib_curses_scr_set" = xyes then : -printf "%s\n" "#define HAVE_CURSES_SCR_SET 1" >>confdefs.h +printf "%s\n" "#define _Py_HAVE_CURSES_SCR_SET 1" >>confdefs.h fi + + CPPFLAGS=$ac_save_cppflags fi diff --git a/configure.ac b/configure.ac index 753f997a20da4a..c73f8f2fd28a1c 100644 --- a/configure.ac +++ b/configure.ac @@ -170,7 +170,11 @@ AC_ARG_WITH([build-python], dnl Build Python interpreter is used for regeneration and freezing. ac_cv_prog_PYTHON_FOR_REGEN=$with_build_python PYTHON_FOR_FREEZE="$with_build_python" - PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) _PYTHON_SYSCONFIGDATA_PATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`) '$with_build_python + PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) PYTHONPATH=$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) _PYTHON_SYSCONFIGDATA_PATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`) '$with_build_python + dnl _PYTHON_HOST_PLATFORM is only set when cross compiling. + AS_VAR_IF([cross_compiling], [yes], + [PYTHON_FOR_BUILD='_PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) '"$PYTHON_FOR_BUILD"] + ) AC_MSG_RESULT([$with_build_python]) ], [ AS_VAR_IF([cross_compiling], [yes], @@ -182,6 +186,13 @@ AC_ARG_WITH([build-python], ) AC_SUBST([PYTHON_FOR_BUILD]) +# Module import checks must run with the target interpreter on native builds. +AS_VAR_IF([cross_compiling], [yes], + [MODULE_CHECK_COMMAND='$(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/check_extension_modules.py --cross-compiling'], + [MODULE_CHECK_COMMAND='$(RUNSHARED) ./$(BUILDPYTHON) -E $(srcdir)/Tools/build/check_extension_modules.py --no-cross-compiling'] +) +AC_SUBST([MODULE_CHECK_COMMAND]) + AC_MSG_CHECKING([for Python interpreter freezing]) AC_MSG_RESULT([$PYTHON_FOR_FREEZE]) AC_SUBST([PYTHON_FOR_FREEZE]) @@ -194,6 +205,9 @@ AS_VAR_IF([cross_compiling], [yes], FREEZE_MODULE='$(FREEZE_MODULE_BOOTSTRAP)' FREEZE_MODULE_DEPS='$(FREEZE_MODULE_BOOTSTRAP_DEPS)' PYTHON_FOR_BUILD_DEPS='' + # The build Python is a separate interpreter. RUNSHARED would make it + # load the in-tree libpython, which may need a different libc. + RUNSHARED_FOR_BUILD='' ], [ dnl internal build tools also depend on Programs/_freeze_module and _bootstrap_python. @@ -202,6 +216,7 @@ AS_VAR_IF([cross_compiling], [yes], FREEZE_MODULE='$(PYTHON_FOR_FREEZE) $(srcdir)/Programs/_freeze_module.py' FREEZE_MODULE_DEPS="_bootstrap_python \$(srcdir)/Programs/_freeze_module.py" PYTHON_FOR_BUILD_DEPS='$(BUILDPYTHON)' + RUNSHARED_FOR_BUILD='$(RUNSHARED)' ] ) AC_SUBST([FREEZE_MODULE_BOOTSTRAP]) @@ -209,6 +224,7 @@ AC_SUBST([FREEZE_MODULE_BOOTSTRAP_DEPS]) AC_SUBST([FREEZE_MODULE]) AC_SUBST([FREEZE_MODULE_DEPS]) AC_SUBST([PYTHON_FOR_BUILD_DEPS]) +AC_SUBST([RUNSHARED_FOR_BUILD]) AC_CHECK_PROGS([PYTHON_FOR_REGEN], [python$PACKAGE_VERSION python3.16 python3.15 python3.14 python3.13 python3.12 python3.11 python3.10 python3 python], @@ -2215,6 +2231,32 @@ if test "$Py_LTO" = 'true' ; then else LTOFLAGS="-flto=${Py_LTO_POLICY}" fi + LTOCFLAGS="$LTOFLAGS" + # GNU ld needs LLVMgold.so for Clang LTO. If the configured linker + # cannot link LTO objects, try lld, which does not need that plugin. + WITH_SAVE_ENV([ + LDFLAGS="$LDFLAGS $LDFLAGS_NODIST $LTOFLAGS" + AC_MSG_CHECKING([whether Clang LTO works with the configured linker]) + AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [clang_lto_link=yes], [clang_lto_link=no]) + AC_MSG_RESULT([$clang_lto_link]) + ]) + if test "$clang_lto_link" = no; then + WITH_SAVE_ENV([ + LDFLAGS="$LDFLAGS -fuse-ld=lld" + AC_MSG_CHECKING([whether Clang LTO works with lld]) + AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])], [clang_lto_link=yes], [clang_lto_link=no]) + AC_MSG_RESULT([$clang_lto_link]) + ]) + if test "$clang_lto_link" = no; then + AC_MSG_ERROR([m4_normalize([ + Clang LTO requires a working LTO-aware linker. + Install lld or the LLVMgold.so plugin, select a linker + with LDFLAGS, or disable LTO with --without-lto. + ])]) + fi + LTOFLAGS="$LTOFLAGS -fuse-ld=lld" + LDFLAGS_NOLTO="$LDFLAGS_NOLTO -fuse-ld=lld" + fi ;; esac ;; @@ -5726,15 +5768,15 @@ AC_CHECK_FUNCS([ \ copy_file_range ctermid dladdr dup explicit_bzero explicit_memset \ faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \ fpathconf fstatat ftruncate futimens futimes futimesat \ - gai_strerror getdents64 getegid geteuid getgid getgrent getgrgid getgrgid_r \ + gai_strerror getegid geteuid getgid getgrent getgrgid getgrgid_r \ getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin getlogin_r \ getpeername getpgid getpid getppid getpriority _getpty \ getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid \ - gettid getuid grantpt if_indextoname if_nameindex \ - if_nametoindex initgroups kill killpg lchown linkat \ + getuid grantpt if_nameindex \ + initgroups kill killpg lchown linkat \ lockf lstat lutimes madvise mbrtowc memrchr mkdirat mkfifo mkfifoat \ mknod mknodat mktime mmap mremap nice openat opendir pathconf pause \ - pidfd_open pidfd_getfd pidfd_send_signal pipe \ + pipe \ plock poll ppoll posix_fadvise posix_fallocate posix_openpt \ pread preadv preadv2 process_vm_readv \ pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init \ @@ -5757,7 +5799,13 @@ PY_CHECK_FUNC_PRIVATE([freadlink], [@%:@include ]) PY_CHECK_FUNC_PRIVATE([strerror_r], [@%:@include ]) # FreeBSD pthread_attr_get_np() PY_CHECK_FUNC_PRIVATE([pthread_attr_get_np], [@%:@include ]) - +PY_CHECK_FUNC_PRIVATE([getdents64], [@%:@include ]) +PY_CHECK_FUNC_PRIVATE([gettid], [@%:@include ]) +PY_CHECK_FUNC_PRIVATE([if_indextoname], [@%:@include ]) +PY_CHECK_FUNC_PRIVATE([if_nametoindex], [@%:@include ]) +PY_CHECK_FUNC_PRIVATE([pidfd_open], [@%:@include ]) +PY_CHECK_FUNC_PRIVATE([pidfd_getfd], [@%:@include ]) +PY_CHECK_FUNC_PRIVATE([pidfd_send_signal], [@%:@include ]) # os.statx uses Linux's statx function. AIX also has a function named statx, # but it's unrelated. Check only on Linux (including Android). @@ -7224,7 +7272,7 @@ AS_VAR_IF([with_readline], [no], [ ) ]) AS_VAR_IF([ac_cv_readline_rl_change_environment], [yes], [ - AC_DEFINE([HAVE_RL_CHANGE_ENVIRONMENT], [1], [Define if you have readline 6.3]) + AC_DEFINE([_Py_HAVE_RL_CHANGE_ENVIRONMENT], [1], [Define if you have readline 6.3]) ]) # check for readline 4.2 @@ -7629,6 +7677,7 @@ dnl lacks, for example term_attrs() in the narrow ncurses library. AC_DEFUN([PY_CHECK_CURSES_FUNC], [ AS_VAR_PUSHDEF([py_var], [ac_cv_lib_curses_$1]) AS_VAR_PUSHDEF([py_define], [HAVE_CURSES_]m4_toupper($1)) + AS_VAR_PUSHDEF([py_define], m4_ifblank([$3], [[HAVE_CURSES_]m4_toupper($1)], [$3])) AC_CACHE_CHECK( [for curses function $1], [py_var], @@ -7649,6 +7698,13 @@ AC_DEFUN([PY_CHECK_CURSES_FUNC], AS_VAR_POPDEF([py_define]) ]) +dnl PY_CHECK_CURSES_FUNC_PRIVATE(FUNCTION, [INCLUDES], [AC_DEFINE-VAR]) +dnl Similar to PY_CHECK_CURSES_FUNC but define macro _Py_HAVE_CURSES_xxx, +dnl instead of HAVE_CURSES_xxx +AC_DEFUN([PY_CHECK_CURSES_FUNC_PRIVATE], +[ PY_CHECK_CURSES_FUNC([$1], [$2], m4_ifblank([$3], [[_Py_HAVE_CURSES_]m4_toupper($1)], [$3])) +]) + dnl PY_CHECK_CURSES_VAR(VARIABLE) dnl Like PY_CHECK_CURSES_FUNC, but for an integer variable (or macro), such as dnl ESCDELAY, which a function probe cannot detect. @@ -7681,36 +7737,37 @@ PY_CHECK_CURSES_FUNC([immedok]) PY_CHECK_CURSES_FUNC([syncok]) PY_CHECK_CURSES_FUNC([wchgat]) PY_CHECK_CURSES_FUNC([filter]) -PY_CHECK_CURSES_FUNC([nofilter]) PY_CHECK_CURSES_FUNC([has_key]) -PY_CHECK_CURSES_FUNC([has_mouse]) -PY_CHECK_CURSES_FUNC([is_keypad]) -PY_CHECK_CURSES_FUNC([is_leaveok]) PY_CHECK_CURSES_FUNC([typeahead]) PY_CHECK_CURSES_FUNC([use_env]) -PY_CHECK_CURSES_FUNC([new_prescr]) -PY_CHECK_CURSES_FUNC([use_screen]) -PY_CHECK_CURSES_FUNC([use_window]) -PY_CHECK_CURSES_FUNC([key_defined]) -PY_CHECK_CURSES_FUNC([term_attrs]) -PY_CHECK_CURSES_FUNC([define_key]) -PY_CHECK_CURSES_FUNC([keyok]) PY_CHECK_CURSES_FUNC([set_escdelay]) PY_CHECK_CURSES_FUNC([set_tabsize]) dnl The X/Open attr_t and soft-label attribute functions are absent on old dnl SVr4 curses (e.g. illumos). -PY_CHECK_CURSES_FUNC([wattr_get]) -PY_CHECK_CURSES_FUNC([wattr_set]) -PY_CHECK_CURSES_FUNC([wattr_on]) -PY_CHECK_CURSES_FUNC([wattr_off]) -PY_CHECK_CURSES_FUNC([wcolor_set]) -PY_CHECK_CURSES_FUNC([slk_attr_on]) -PY_CHECK_CURSES_FUNC([slk_attr_off]) -PY_CHECK_CURSES_FUNC([slk_attr_set]) -PY_CHECK_CURSES_FUNC([slk_color]) PY_CHECK_CURSES_VAR([ESCDELAY]) PY_CHECK_CURSES_VAR([TABSIZE]) +PY_CHECK_CURSES_FUNC_PRIVATE([define_key]) +PY_CHECK_CURSES_FUNC_PRIVATE([has_mouse]) +PY_CHECK_CURSES_FUNC_PRIVATE([is_keypad]) +PY_CHECK_CURSES_FUNC_PRIVATE([is_leaveok]) +PY_CHECK_CURSES_FUNC_PRIVATE([key_defined]) +PY_CHECK_CURSES_FUNC_PRIVATE([keyok]) +PY_CHECK_CURSES_FUNC_PRIVATE([new_prescr]) +PY_CHECK_CURSES_FUNC_PRIVATE([nofilter]) +PY_CHECK_CURSES_FUNC_PRIVATE([slk_attr_off]) +PY_CHECK_CURSES_FUNC_PRIVATE([slk_attr_on]) +PY_CHECK_CURSES_FUNC_PRIVATE([slk_attr_set]) +PY_CHECK_CURSES_FUNC_PRIVATE([slk_color]) +PY_CHECK_CURSES_FUNC_PRIVATE([term_attrs]) +PY_CHECK_CURSES_FUNC_PRIVATE([use_screen]) +PY_CHECK_CURSES_FUNC_PRIVATE([use_window]) +PY_CHECK_CURSES_FUNC_PRIVATE([wattr_get]) +PY_CHECK_CURSES_FUNC_PRIVATE([wattr_off]) +PY_CHECK_CURSES_FUNC_PRIVATE([wattr_on]) +PY_CHECK_CURSES_FUNC_PRIVATE([wattr_set]) +PY_CHECK_CURSES_FUNC_PRIVATE([wcolor_set]) + dnl Probe for the X/Open getmouse(MEVENT *) signature specifically: PDCurses dnl declares an incompatible getmouse(void) unless built for the ncurses mouse API. AC_CACHE_CHECK([for ncurses-style curses function getmouse], @@ -7725,8 +7782,9 @@ AS_VAR_IF([ac_cv_lib_curses_getmouse], [yes], dnl scr_dump gates scr_dump/scr_restore/scr_init; scr_set is separate, since dnl old SVr4 curses (e.g. illumos) has the former but not scr_set. -PY_CHECK_CURSES_FUNC([scr_dump]) -PY_CHECK_CURSES_FUNC([scr_set]) +PY_CHECK_CURSES_FUNC_PRIVATE([scr_dump]) +PY_CHECK_CURSES_FUNC_PRIVATE([scr_set]) + CPPFLAGS=$ac_save_cppflags ])dnl have_curses != no ])dnl save env diff --git a/pyconfig.h.in b/pyconfig.h.in index dba7d193bcb3d1..1d4f445a8b1fda 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -185,9 +185,6 @@ /* Define if you have the 'ctermid_r' function. */ #undef HAVE_CTERMID_R -/* Define if you have the 'define_key' function. */ -#undef HAVE_CURSES_DEFINE_KEY - /* Define if you have the 'ESCDELAY' variable. */ #undef HAVE_CURSES_ESCDELAY @@ -203,105 +200,42 @@ /* Define if you have the 'has_key' function. */ #undef HAVE_CURSES_HAS_KEY -/* Define if you have the 'has_mouse' function. */ -#undef HAVE_CURSES_HAS_MOUSE - /* Define if you have the 'immedok' function. */ #undef HAVE_CURSES_IMMEDOK -/* Define if you have the 'is_keypad' function. */ -#undef HAVE_CURSES_IS_KEYPAD - -/* Define if you have the 'is_leaveok' function. */ -#undef HAVE_CURSES_IS_LEAVEOK - /* Define if you have the 'is_pad' function. */ #undef HAVE_CURSES_IS_PAD /* Define if you have the 'is_term_resized' function. */ #undef HAVE_CURSES_IS_TERM_RESIZED -/* Define if you have the 'keyok' function. */ -#undef HAVE_CURSES_KEYOK - -/* Define if you have the 'key_defined' function. */ -#undef HAVE_CURSES_KEY_DEFINED - -/* Define if you have the 'new_prescr' function. */ -#undef HAVE_CURSES_NEW_PRESCR - -/* Define if you have the 'nofilter' function. */ -#undef HAVE_CURSES_NOFILTER - /* Define if you have the 'resizeterm' function. */ #undef HAVE_CURSES_RESIZETERM /* Define if you have the 'resize_term' function. */ #undef HAVE_CURSES_RESIZE_TERM -/* Define if you have the 'scr_dump' function. */ -#undef HAVE_CURSES_SCR_DUMP - -/* Define if you have the 'scr_set' function. */ -#undef HAVE_CURSES_SCR_SET - /* Define if you have the 'set_escdelay' function. */ #undef HAVE_CURSES_SET_ESCDELAY /* Define if you have the 'set_tabsize' function. */ #undef HAVE_CURSES_SET_TABSIZE -/* Define if you have the 'slk_attr_off' function. */ -#undef HAVE_CURSES_SLK_ATTR_OFF - -/* Define if you have the 'slk_attr_on' function. */ -#undef HAVE_CURSES_SLK_ATTR_ON - -/* Define if you have the 'slk_attr_set' function. */ -#undef HAVE_CURSES_SLK_ATTR_SET - -/* Define if you have the 'slk_color' function. */ -#undef HAVE_CURSES_SLK_COLOR - /* Define if you have the 'syncok' function. */ #undef HAVE_CURSES_SYNCOK /* Define if you have the 'TABSIZE' variable. */ #undef HAVE_CURSES_TABSIZE -/* Define if you have the 'term_attrs' function. */ -#undef HAVE_CURSES_TERM_ATTRS - /* Define if you have the 'typeahead' function. */ #undef HAVE_CURSES_TYPEAHEAD /* Define if you have the 'use_env' function. */ #undef HAVE_CURSES_USE_ENV -/* Define if you have the 'use_screen' function. */ -#undef HAVE_CURSES_USE_SCREEN - -/* Define if you have the 'use_window' function. */ -#undef HAVE_CURSES_USE_WINDOW - -/* Define if you have the 'wattr_get' function. */ -#undef HAVE_CURSES_WATTR_GET - -/* Define if you have the 'wattr_off' function. */ -#undef HAVE_CURSES_WATTR_OFF - -/* Define if you have the 'wattr_on' function. */ -#undef HAVE_CURSES_WATTR_ON - -/* Define if you have the 'wattr_set' function. */ -#undef HAVE_CURSES_WATTR_SET - /* Define if you have the 'wchgat' function. */ #undef HAVE_CURSES_WCHGAT -/* Define if you have the 'wcolor_set' function. */ -#undef HAVE_CURSES_WCOLOR_SET - /* Define to 1 if you have the header file. */ #undef HAVE_DB_H @@ -552,9 +486,6 @@ /* Define this if you have flockfile(), getc_unlocked(), and funlockfile() */ #undef HAVE_GETC_UNLOCKED -/* Define to 1 if you have the 'getdents64' function. */ -#undef HAVE_GETDENTS64 - /* Define to 1 if you have the 'getegid' function. */ #undef HAVE_GETEGID @@ -684,9 +615,6 @@ /* Define if you have the 'getsockname' function. */ #undef HAVE_GETSOCKNAME -/* Define to 1 if you have the 'gettid' function. */ -#undef HAVE_GETTID - /* Define to 1 if you have the 'getuid' function. */ #undef HAVE_GETUID @@ -705,15 +633,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_ICONV_H -/* Define to 1 if you have the 'if_indextoname' function. */ -#undef HAVE_IF_INDEXTONAME - /* Define to 1 if you have the 'if_nameindex' function. */ #undef HAVE_IF_NAMEINDEX -/* Define to 1 if you have the 'if_nametoindex' function. */ -#undef HAVE_IF_NAMETOINDEX - /* Define if you have the 'inet_aton' function. */ #undef HAVE_INET_ATON @@ -1009,15 +931,6 @@ /* Define to 1 if you have the 'pause' function. */ #undef HAVE_PAUSE -/* Define to 1 if you have the 'pidfd_getfd' function. */ -#undef HAVE_PIDFD_GETFD - -/* Define to 1 if you have the 'pidfd_open' function. */ -#undef HAVE_PIDFD_OPEN - -/* Define to 1 if you have the 'pidfd_send_signal' function. */ -#undef HAVE_PIDFD_SEND_SIGNAL - /* Define to 1 if you have the 'pipe' function. */ #undef HAVE_PIPE @@ -1164,9 +1077,6 @@ /* Define if you can turn off readline's signal handling. */ #undef HAVE_RL_CATCH_SIGNAL -/* Define if you have readline 6.3 */ -#undef HAVE_RL_CHANGE_ENVIRONMENT - /* Define to 1 if the system has the type 'rl_compdisp_func_t'. */ #undef HAVE_RL_COMPDISP_FUNC_T @@ -2141,6 +2051,72 @@ /* Define if you have the 'cospi' function. */ #undef _Py_HAVE_COSPI +/* Define if you have the 'define_key' function. */ +#undef _Py_HAVE_CURSES_DEFINE_KEY + +/* Define if you have the 'has_mouse' function. */ +#undef _Py_HAVE_CURSES_HAS_MOUSE + +/* Define if you have the 'is_keypad' function. */ +#undef _Py_HAVE_CURSES_IS_KEYPAD + +/* Define if you have the 'is_leaveok' function. */ +#undef _Py_HAVE_CURSES_IS_LEAVEOK + +/* Define if you have the 'keyok' function. */ +#undef _Py_HAVE_CURSES_KEYOK + +/* Define if you have the 'key_defined' function. */ +#undef _Py_HAVE_CURSES_KEY_DEFINED + +/* Define if you have the 'new_prescr' function. */ +#undef _Py_HAVE_CURSES_NEW_PRESCR + +/* Define if you have the 'nofilter' function. */ +#undef _Py_HAVE_CURSES_NOFILTER + +/* Define if you have the 'scr_dump' function. */ +#undef _Py_HAVE_CURSES_SCR_DUMP + +/* Define if you have the 'scr_set' function. */ +#undef _Py_HAVE_CURSES_SCR_SET + +/* Define if you have the 'slk_attr_off' function. */ +#undef _Py_HAVE_CURSES_SLK_ATTR_OFF + +/* Define if you have the 'slk_attr_on' function. */ +#undef _Py_HAVE_CURSES_SLK_ATTR_ON + +/* Define if you have the 'slk_attr_set' function. */ +#undef _Py_HAVE_CURSES_SLK_ATTR_SET + +/* Define if you have the 'slk_color' function. */ +#undef _Py_HAVE_CURSES_SLK_COLOR + +/* Define if you have the 'term_attrs' function. */ +#undef _Py_HAVE_CURSES_TERM_ATTRS + +/* Define if you have the 'use_screen' function. */ +#undef _Py_HAVE_CURSES_USE_SCREEN + +/* Define if you have the 'use_window' function. */ +#undef _Py_HAVE_CURSES_USE_WINDOW + +/* Define if you have the 'wattr_get' function. */ +#undef _Py_HAVE_CURSES_WATTR_GET + +/* Define if you have the 'wattr_off' function. */ +#undef _Py_HAVE_CURSES_WATTR_OFF + +/* Define if you have the 'wattr_on' function. */ +#undef _Py_HAVE_CURSES_WATTR_ON + +/* Define if you have the 'wattr_set' function. */ +#undef _Py_HAVE_CURSES_WATTR_SET + +/* Define if you have the 'wcolor_set' function. */ +#undef _Py_HAVE_CURSES_WCOLOR_SET + /* Define if compiler supports __builtin_shufflevector with 128-bit vectors AND the target architecture has native SIMD (not just API availability) */ #undef _Py_HAVE_EFFICIENT_BUILTIN_SHUFFLEVECTOR @@ -2151,19 +2127,43 @@ /* Define if you have the 'freadlink' function. */ #undef _Py_HAVE_FREADLINK +/* Define if you have the 'getdents64' function. */ +#undef _Py_HAVE_GETDENTS64 + +/* Define if you have the 'gettid' function. */ +#undef _Py_HAVE_GETTID + /* Define if you have a working iconv() function. */ #undef _Py_HAVE_ICONV +/* Define if you have the 'if_indextoname' function. */ +#undef _Py_HAVE_IF_INDEXTONAME + +/* Define if you have the 'if_nametoindex' function. */ +#undef _Py_HAVE_IF_NAMETOINDEX + /* Define to 1 if libgcc __register_frame and __deregister_frame are linkable. */ #undef _Py_HAVE_LIBGCC_EH_FRAME_REGISTRATION +/* Define if you have the 'pidfd_getfd' function. */ +#undef _Py_HAVE_PIDFD_GETFD + +/* Define if you have the 'pidfd_open' function. */ +#undef _Py_HAVE_PIDFD_OPEN + +/* Define if you have the 'pidfd_send_signal' function. */ +#undef _Py_HAVE_PIDFD_SEND_SIGNAL + /* Define if you have the 'PR_SET_VMA_ANON_NAME' constant. */ #undef _Py_HAVE_PR_SET_VMA_ANON_NAME /* Define if you have the 'pthread_attr_get_np' function. */ #undef _Py_HAVE_PTHREAD_ATTR_GET_NP +/* Define if you have readline 6.3 */ +#undef _Py_HAVE_RL_CHANGE_ENVIRONMENT + /* Define if you have the 'sinpi' function. */ #undef _Py_HAVE_SINPI